Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions camerad/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ list (APPEND INTERFACE_SOURCES
add_library(${INTERFACE_TARGET} ${INTERFACE_SOURCES})
target_link_libraries(${INTERFACE_TARGET}
common
shared_memory_writer
frame_output_factory
)
target_include_directories(${INTERFACE_TARGET} PUBLIC ${INTERFACE_INCLUDES})

Expand Down Expand Up @@ -173,7 +173,6 @@ target_link_libraries(camerad
network
utilities
logentry
shared_memory_writer
${INTERFACE_TARGET}
${INTERFACE_LIBS}
${CMAKE_THREAD_LIBS_INIT}
Expand Down
2 changes: 0 additions & 2 deletions camerad/archon_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,8 +1584,6 @@ namespace Camera {
}
else error=this->fetchlog();

this->is_camera_mode = false; // require that a mode be selected after loading new firmware

return error;
}
/***** Camera::ArchonController::load_acf ***********************************/
Expand Down
1 change: 0 additions & 1 deletion camerad/archon_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ namespace Camera {
std::atomic_flag archon_busy = ATOMIC_FLAG_INIT; //!< indicates a thread is accessing Archon
bool is_firmwareloaded;
std::string firmware;
bool is_camera_mode{false}; //!< has a camera mode been selected
int msgref;
std::string backplaneversion;
std::vector<int> modtype; //!< type of each module from SYSTEM command
Expand Down
56 changes: 32 additions & 24 deletions camerad/archon_exposure_modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "archon_exposure_modes.h"
#include "archon_interface.h"

#include <cstdlib>

namespace Camera {

/***** Camera::ExposureModeSingle *******************************************/
Expand Down Expand Up @@ -111,43 +113,49 @@ namespace Camera {

/***** Camera::ExposureModeSingle::image_processing_thread ******************/
/**
* @brief implementation of Archon-specific expose for Single
*
* @brief Consumer thread: pop each frame off the queue and fan out to
* every configured FrameOutput on the interface
*/
void ExposureModeSingle::image_processing_thread() {
const std::string function("Camera::ExposureModeSingle::image_processing_thread");
logwrite(function, "enter");

// open FITS file ?
auto* camera_info = &this->interface->camera_info;
auto* controller = this->interface->controller;
auto* mode = &controller->modemap[controller->selectedmode];
const size_t bufferbytes = static_cast<size_t>(camera_info->image_data_bytes) * camera_info->cubedepth;
const uint32_t bpp = (mode->samplemode == 1) ? 4 : 2;
const uint32_t width = static_cast<uint32_t>(mode->geometry.pixelcount);
const uint32_t height = static_cast<uint32_t>(mode->geometry.linecount);

// pop an image out of the queue,
// wait until producer stops producing data, or aborted
//
while (!this->interface->is_aborted()) {
std::shared_ptr<ArchonImageBuffer> buf;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
// keep trying to get the queue lock until success or aborted
this->queue_cv.wait(lock, [this] {
return !this->imagebuf_queue.empty() || this->is_producer_finished || this->interface->is_aborted();
});
if (this->interface->is_aborted()) break;
if (this->imagebuf_queue.empty()) {
if (this->is_producer_finished) {
logwrite(function, "queue empty and producer finished");
break;
}
else {
logwrite(function, "queue empty, producer not finished");
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->queue_cv.wait(lock, [this] {
return !this->imagebuf_queue.empty() || this->is_producer_finished || this->interface->is_aborted();
});
if (this->interface->is_aborted()) break;
if (this->imagebuf_queue.empty()) {
if (this->is_producer_finished) {
logwrite(function, "queue empty and producer finished");
break;
}
continue;
}
buf = this->imagebuf_queue.front();
this->imagebuf_queue.pop();
}
this->imagebuf_queue.pop();
}
// process_image
std::this_thread::sleep_for(std::chrono::milliseconds(500));

Camera::FrameMetadata meta;
meta.frame_number = buf->bufframen_slice.empty() ? 0 : static_cast<uint64_t>(buf->bufframen_slice[0]);
meta.timestamp = buf->buftimestamp_slice.empty() ? 0 : buf->buftimestamp_slice[0];
meta.width = width;
meta.height = height;
meta.bytes_per_pixel = bpp;
this->interface->dispatch_frame(buf->rawpixels.get(), bufferbytes, meta);
}

// close FITS file ?
logwrite(function, "exit");
}
/***** Camera::ExposureModeSingle::image_processing_thread ******************/
Expand Down
3 changes: 1 addition & 2 deletions camerad/archon_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,8 @@ namespace Camera {

// if we made it all the way to the end then this is the selected mode
this->controller->selectedmode = modeselect;
this->controller->is_camera_mode = true;

std::string target = ArchonExposureMode::SINGLE;
std::string target = this->default_exposure_mode_name();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I ask for an exposure mode that doesn't exist then this will silently give me some other mode.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@astronomerdave It allows each instrument to have a default exposure mode.

for (const auto &m : this->get_exposure_modes()) {
if (m == modeselect) { target = modeselect; break; }
}
Expand Down
3 changes: 3 additions & 0 deletions camerad/archon_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ namespace Camera {
long set_vcpu_inreg(const std::string &args, std::string &retstring);
long autofetch_mode(const std::string &args, std::string &retstring);

// Fallback for set_camera_mode when the camera-mode name is unknown
virtual std::string default_exposure_mode_name() const { return "SINGLE"; }

char* get_framebuf() { return controller->framebuf; }

bool is_autofetch_mode{false};
Expand Down
14 changes: 14 additions & 0 deletions camerad/camera_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include "camera_information.h"
#include "camerad_commands.h"
#include "exposure_modes.h"
#include "frame_output.h"

#include <memory>
#include <vector>

namespace Camera {

Expand Down Expand Up @@ -54,6 +58,16 @@ namespace Camera {

Config configfile;
Camera::Information camera_info;

// Frame output destinations populated by Camera::make_frame_outputs()
std::vector<std::unique_ptr<FrameOutput>> frame_outputs;

// Fan a frame out to every configured FrameOutput
void dispatch_frame(const char* data, size_t size, const FrameMetadata &meta) {
for (auto &output : this->frame_outputs) {
output->write(data, size, meta);
}
}
// Common::FitsKeys systemkeys; move to Camera::Information?

// These functions are shared by all interfaces with common implementations,
Expand Down
2 changes: 1 addition & 1 deletion camerad/exposure_modes.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace Camera {
}

virtual ~ExposureMode() = default;
virtual long expose() = 0;
virtual long expose() { return NO_ERROR; }
virtual void image_acquisition_thread() { };
virtual void image_processing_thread() { };

Expand Down
29 changes: 29 additions & 0 deletions utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ add_library(shared_memory_writer STATIC
target_include_directories(shared_memory_writer PRIVATE ${PROJECT_BASE_DIR}/common ${PROJECT_BASE_DIR}/utils)
target_link_libraries(shared_memory_writer nlohmann_json::nlohmann_json $<IF:$<PLATFORM_ID:Linux>,rt,>)

add_library(fits_writer STATIC
${PROJECT_UTILS_DIR}/fits_writer.cpp
)
target_include_directories(fits_writer PRIVATE ${PROJECT_BASE_DIR}/common ${PROJECT_BASE_DIR}/utils)
find_library(FITSWRITER_CCFITS_LIB CCfits NAMES libCCfits PATHS /usr/local/lib /opt/homebrew/lib)
find_library(FITSWRITER_CFITS_LIB cfitsio NAMES libcfitsio PATHS /usr/local/lib /opt/homebrew/lib)
target_link_libraries(fits_writer
nlohmann_json::nlohmann_json
${FITSWRITER_CCFITS_LIB}
${FITSWRITER_CFITS_LIB}
)

add_library(cadence_gate STATIC
${PROJECT_UTILS_DIR}/cadence_gate.cpp
)
target_include_directories(cadence_gate PRIVATE ${PROJECT_BASE_DIR}/common ${PROJECT_BASE_DIR}/utils)
target_link_libraries(cadence_gate nlohmann_json::nlohmann_json)

add_library(frame_output_factory STATIC
${PROJECT_UTILS_DIR}/frame_output_factory.cpp
)
target_include_directories(frame_output_factory PRIVATE ${PROJECT_BASE_DIR}/common ${PROJECT_BASE_DIR}/utils)
target_link_libraries(frame_output_factory
nlohmann_json::nlohmann_json
shared_memory_writer
fits_writer
cadence_gate
)

add_library(md5 STATIC
${PROJECT_UTILS_DIR}/md5.cpp
)
Expand Down
36 changes: 36 additions & 0 deletions utils/cadence_gate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @file cadence_gate.cpp
* @brief FrameOutput decorator that rate-limits frames to a wrapped output
*/

#include "cadence_gate.h"
#include "common.h"

#include <utility>

namespace Camera {

CadenceGate::CadenceGate(std::unique_ptr<FrameOutput> inner, uint32_t write_interval_ms)
: inner_(std::move(inner)),
interval_(std::chrono::milliseconds(write_interval_ms)) {
}

long CadenceGate::open() {
return inner_->open();
}

long CadenceGate::write(const char* data, size_t size, const FrameMetadata& meta) {
const auto now = std::chrono::steady_clock::now();
if (now - last_forwarded_ < interval_) {
n_skipped_.fetch_add(1, std::memory_order_relaxed);
return NO_ERROR;
}
last_forwarded_ = now;
return inner_->write(data, size, meta);
}

void CadenceGate::close() {
inner_->close();
}

}
46 changes: 46 additions & 0 deletions utils/cadence_gate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file cadence_gate.h
* @brief FrameOutput decorator that rate-limits frames to a wrapped output
*
* Forwards a frame only when at least write_interval_ms has elapsed since the
* last forwarded frame; otherwise the frame is dropped. Keeps the cadence
* policy out of the concrete writers, which then just do their I/O.
*/
#pragma once

#include "frame_output.h"

#include <atomic>
#include <chrono>
#include <cstdint>
#include <memory>

namespace Camera {

/// FrameOutput that thins frames by minimum interval before delegating
class CadenceGate : public FrameOutput {
public:
CadenceGate(std::unique_ptr<FrameOutput> inner, uint32_t write_interval_ms);
~CadenceGate() override = default;

CadenceGate(const CadenceGate&) = delete;
CadenceGate& operator=(const CadenceGate&) = delete;

long open() override;
long write(const char* data, size_t size, const FrameMetadata& meta) override;
void close() override;

uint64_t frames_skipped() const { return n_skipped_.load(); }

private:
std::unique_ptr<FrameOutput> inner_;
std::chrono::milliseconds interval_;

// Only touched on the producer thread
std::chrono::steady_clock::time_point last_forwarded_{
std::chrono::steady_clock::time_point::min()};

std::atomic<uint64_t> n_skipped_{0};
};

}
Loading
Loading