Fawkes API
Fawkes Development Version
|
23 #include "acquisition_thread.h"
25 #include "aqt_vision_threads.h"
27 #include <core/exceptions/software.h>
28 #include <core/exceptions/system.h>
29 #include <core/threading/mutex.h>
30 #include <core/threading/mutex_locker.h>
31 #include <core/threading/wait_condition.h>
32 #ifdef FVBASE_TIMETRACKER
33 # include <utils/time/clock.h>
34 # include <utils/time/tracker.h>
36 #include <fvcams/shmem.h>
37 #include <fvutils/color/conversions.h>
38 #include <interfaces/SwitchInterface.h>
39 #include <logging/logger.h>
51 using namespace firevision;
75 set_name(
"FvAcquisitionThread::%s",
id);
77 image_id_ = strdup(
id);
82 enabled_mutex_ =
new Mutex(Mutex::RECURSIVE);
93 #ifdef FVBASE_TIMETRACKER
96 ttc_capture_ = tt_->add_class(
"Capture");
97 ttc_lock_ = tt_->add_class(
"Lock");
98 ttc_convert_ = tt_->add_class(
"Convert");
99 ttc_unlock_ = tt_->add_class(
"Unlock");
100 ttc_dispose_ = tt_->add_class(
"Dispose");
109 for (shmit_ = shm_.begin(); shmit_ != shm_.end(); ++shmit_) {
110 delete shmit_->second;
117 delete enabled_waitcond_;
118 delete enabled_mutex_;
125 name(),
"Camera opened, w=%u h=%u c=%s", width_, height_, colorspace_to_string(colorspace_));
127 std::string if_id = std::string(
"Camera ") + image_id_;
130 enabled_if_->
write();
179 const char *img_id = NULL;
181 if (cspace == CS_UNKNOWN) {
184 throw Exception(
"Only one vision thread may access the raw camera.");
190 if (shm_.find(cspace) == shm_.end()) {
191 if (asprintf(&tmp,
"%s.%zu", image_id_, shm_.size()) == -1) {
197 img_id = shm_[cspace]->image_id();
248 if (enabled_ && !enabled) {
252 enabled_if_->
write();
254 }
else if (!enabled_ && enabled) {
258 enabled_if_->
write();
287 "At least one thread was being finalized while prepfin hold "
288 "was about to be acquired");
316 #ifdef FVBASE_TIMETRACKER
319 tt_->ping_start(ttc_capture_);
321 tt_->ping_end(ttc_capture_);
323 for (shmit_ = shm_.begin(); shmit_ != shm_.end(); ++shmit_) {
324 if (shmit_->first == CS_UNKNOWN)
326 tt_->ping_start(ttc_lock_);
327 shmit_->second->lock_for_write();
328 tt_->ping_end(ttc_lock_);
329 tt_->ping_start(ttc_convert_);
331 colorspace_, shmit_->first, camera_->
buffer(), shmit_->second->buffer(), width_, height_);
333 shmit_->second->set_capture_time(camera_->
capture_time());
337 tt_->ping_end(ttc_convert_);
338 tt_->ping_start(ttc_unlock_);
339 shmit_->second->unlock();
340 tt_->ping_end(ttc_unlock_);
348 tt_->ping_start(ttc_dispose_);
350 tt_->ping_end(ttc_dispose_);
353 if ((++loop_count_ % FVBASE_TT_PRINT_INT) == 0) {
354 tt_->print_to_stdout();
357 #else // no time tracking
361 for (shmit_ = shm_.begin(); shmit_ != shm_.end(); ++shmit_) {
362 if (shmit_->first == CS_UNKNOWN)
364 shmit_->second->lock_for_write();
366 colorspace_, shmit_->first, camera_->
buffer(), shmit_->second->buffer(), width_, height_);
368 shmit_->second->set_capture_time(camera_->
capture_time());
372 shmit_->second->unlock();
392 enabled_waitcond_->
wait();
397 FvAcquisitionThread::bb_interface_message_received(
Interface *interface,
Message *message)
throw()
401 if (mode_ == AqtContinuous && !enabled_) {
403 logger->
log_info(name(),
"Enabling on blackboard request");
virtual fawkes::Time * capture_time()
Get the Time of the last successfully captured image.
bool msgq_first_is()
Check if first message has desired type.
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
void msgq_pop()
Erase first message from queue.
bool msgq_empty()
Check if queue is empty.
void set_enabled(bool enabled)
Enable or disable image retrieval.
virtual void start()=0
Start image transfer from the camera.
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Mutex mutual exclusion lock.
SwitchInterface Fawkes BlackBoard Interface.
Aquisition-dependant threads.
Wait until a given condition holds.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
virtual colorspace_t colorspace()=0
Colorspace of returned image.
AqtMode
Acquisition thread mode.
virtual void stop()=0
Stop image transfer from the camera.
AqtMode aqtmode()
Get acquisition thread mode.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
BlackBoard interface listener.
firevision::Camera * camera_instance(firevision::colorspace_t cspace, bool deep_copy)
Get a camera instance.
virtual void dispose_buffer()=0
Dispose current buffer.
Shared memory image buffer.
DisableSwitchMessage Fawkes BlackBoard Interface Message.
const char * name() const
Get name of thread.
virtual void close()=0
Close camera.
void wait()
Wait for the condition forever.
virtual void finalize()
Finalize the thread.
virtual unsigned char * buffer()=0
Get access to current image buffer.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
void set_prepfin_hold(bool hold)
Set prepfin hold fo cyclic threads.
Logger * logger
This is the Logger member used to access the logger.
virtual void close(Interface *interface)=0
Close interface.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
void set_enabled(const bool new_enabled)
Set enabled value.
fawkes::Thread * raw_subscriber_thread
Vision thread registered for raw camera access on this camera.
void set_vt_prepfin_hold(bool hold)
Set prepfin hold status for vision threads.
@ AqtContinuous
continuous mode, use if there are only continuous threads for this acquisition thread.
Fawkes library namespace.
virtual unsigned int pixel_height()=0
Height of image in pixels.
virtual void loop()
Code to execute in the thread.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
void set_aqtmode(AqtMode mode)
Set acquisition thread mode.
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
Base class for all Fawkes BlackBoard interfaces.
void wake_all()
Wake up all waiting threads.
const char * type() const
Get message type.
virtual unsigned int pixel_width()=0
Width of image in pixels.
@ AqtCyclic
cyclic mode, use if there is at least one cyclic thread for this acquisition thread.
virtual ~FvAcquisitionThread()
Destructor.
FvAcquisitionThread(const char *id, firevision::Camera *camera, fawkes::Logger *logger, fawkes::Clock *clock)
Constructor.
FvAqtVisionThreads * vision_threads
Vision threads assigned to this acquisition thread.
Called method has not been implemented.
virtual void capture()=0
Capture an image.
Thread class encapsulation of pthreads.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
System ran out of memory and desired operation could not be fulfilled.
Camera interface for image aquiring devices in FireVision.
Message * msgq_first()
Get the first message from the message queue.
firevision::Camera * get_camera()
Get the Camera of this acquisition thread.
virtual void init()
Initialize the thread.
void wakeup_and_wait_cyclic_threads()
Wakeup and wait for all cyclic threads.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
void write()
Write from local copy into BlackBoard memory.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
void set_opmode(OpMode op_mode)
Set operation mode.
void set_name(const char *format,...)
Set name of thread.
This is supposed to be the central clock in Fawkes.
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Base class for exceptions in Fawkes.