23 #include "acquisition_thread.h" 24 #include "aqt_vision_threads.h" 26 #include <core/exceptions/system.h> 27 #include <core/exceptions/software.h> 28 #include <core/threading/mutex_locker.h> 29 #include <core/threading/mutex.h> 30 #include <core/threading/wait_condition.h> 31 #ifdef FVBASE_TIMETRACKER 32 #include <utils/time/clock.h> 33 #include <utils/time/tracker.h> 35 #include <logging/logger.h> 37 #include <fvcams/shmem.h> 38 #include <fvutils/color/conversions.h> 39 #include <interfaces/SwitchInterface.h> 69 :
Thread(
"FvAcquisitionThread"),
73 set_name(
"FvAcquisitionThread::%s",
id);
75 __image_id = strdup(
id);
80 __enabled_mutex =
new Mutex(Mutex::RECURSIVE);
91 #ifdef FVBASE_TIMETRACKER 94 __ttc_capture = __tt->add_class(
"Capture");
95 __ttc_lock = __tt->add_class(
"Lock");
96 __ttc_convert = __tt->add_class(
"Convert");
97 __ttc_unlock = __tt->add_class(
"Unlock");
98 __ttc_dispose = __tt->add_class(
"Dispose");
108 for (__shmit = __shm.begin(); __shmit != __shm.end(); ++__shmit) {
109 delete __shmit->second;
116 delete __enabled_waitcond;
117 delete __enabled_mutex;
124 colorspace_to_string(__colorspace));
126 std::string if_id = std::string(
"Camera ") + __image_id;
129 __enabled_if->
write();
180 const char *img_id = NULL;
182 if (cspace == CS_UNKNOWN) {
185 throw Exception(
"Only one vision thread may access the raw camera.");
191 if (__shm.find(cspace) == __shm.end()) {
192 if ( asprintf(&tmp,
"%s.%zu", __image_id, __shm.size()) == -1) {
198 img_id = __shm[cspace]->image_id();
251 if (__enabled && ! enabled) {
255 __enabled_if->
write();
257 }
else if (! __enabled && enabled) {
261 __enabled_if->
write();
291 logger->
log_warn(
name(),
"At least one thread was being finalized while prepfin hold " 292 "was about to be acquired");
322 #ifdef FVBASE_TIMETRACKER 325 __tt->ping_start(__ttc_capture);
327 __tt->ping_end(__ttc_capture);
329 for (__shmit = __shm.begin(); __shmit != __shm.end(); ++__shmit) {
330 if (__shmit->first == CS_UNKNOWN)
continue;
331 __tt->ping_start(__ttc_lock);
332 __shmit->second->lock_for_write();
333 __tt->ping_end(__ttc_lock);
334 __tt->ping_start(__ttc_convert);
335 convert(__colorspace, __shmit->first,
336 __camera->
buffer(), __shmit->second->buffer(),
339 __shmit->second->set_capture_time(__camera->
capture_time());
343 __tt->ping_end(__ttc_convert);
344 __tt->ping_start(__ttc_unlock);
345 __shmit->second->unlock();
346 __tt->ping_end(__ttc_unlock);
354 __tt->ping_start(__ttc_dispose);
356 __tt->ping_end(__ttc_dispose);
359 if ( (++__loop_count % FVBASE_TT_PRINT_INT) == 0 ) {
360 __tt->print_to_stdout();
363 #else // no time tracking 367 for (__shmit = __shm.begin(); __shmit != __shm.end(); ++__shmit) {
368 if (__shmit->first == CS_UNKNOWN)
continue;
369 __shmit->second->lock_for_write();
370 convert(__colorspace, __shmit->first,
371 __camera->
buffer(), __shmit->second->buffer(),
374 __shmit->second->set_capture_time(__camera->
capture_time());
378 __shmit->second->unlock();
389 if ( __mode ==
AqtCyclic && __enabled ) {
398 __enabled_waitcond->
wait();
403 FvAcquisitionThread::bb_interface_message_received(
Interface *interface,
void set_aqtmode(AqtMode mode)
Set acquisition thread mode.
void wakeup_and_wait_cyclic_threads()
Wakeup and wait for all cyclic threads.
Wait until a given condition holds.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
AqtMode
Acquisition thread mode.
bool msgq_empty()
Check if queue is empty.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Camera interface for image aquiring devices in FireVision.
virtual void stop()=0
Stop image transfer from the camera.
Fawkes library namespace.
fawkes::Thread * raw_subscriber_thread
Vision thread registered for raw camera access on this camera.
Called method has not been implemented.
void wake_all()
Wake up all waiting threads.
This is supposed to be the central clock in Fawkes.
virtual unsigned int pixel_width()=0
Width of image in pixels.
FvAqtVisionThreads * vision_threads
Vision threads assigned to this acquisition thread.
Aquisition-dependant threads.
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Thread class encapsulation of pthreads.
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
void write()
Write from local copy into BlackBoard memory.
Base class for all Fawkes BlackBoard interfaces.
void set_prepfin_hold(bool hold)
Set prepfin hold fo cyclic threads.
virtual colorspace_t colorspace()=0
Colorspace of returned image.
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
Logger * logger
This is the Logger member used to access the logger.
virtual ~FvAcquisitionThread()
Destructor.
virtual void finalize()
Finalize the thread.
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
void msgq_pop()
Erase first message from queue.
SwitchInterface Fawkes BlackBoard Interface.
void set_name(const char *format,...)
Set name of thread.
Base class for exceptions in Fawkes.
Message * msgq_first()
Get the first message from the message queue.
virtual void capture()=0
Capture an image.
virtual fawkes::Time * capture_time()
Get the Time of the last successfully captured image.
void set_enabled(const bool new_enabled)
Set enabled value.
virtual void init()
Initialize the thread.
void set_opmode(OpMode op_mode)
Set operation mode.
Shared memory image buffer.
firevision::Camera * get_camera()
Get the Camera of this acquisition thread.
DisableSwitchMessage Fawkes BlackBoard Interface Message.
const char * name() const
Get name of thread.
void set_vt_prepfin_hold(bool hold)
Set prepfin hold status for vision threads.
bool msgq_first_is()
Check if first message has desired type.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
firevision::Camera * camera_instance(firevision::colorspace_t cspace, bool deep_copy)
Get a camera instance.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
virtual void loop()
Code to execute in the thread.
bool is_of_type()
Check if message has desired type.
void wait()
Wait for the condition forever.
cyclic mode, use if there is at least one cyclic thread for this acquisition thread.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
void set_enabled(bool enabled)
Enable or disable image retrieval.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void close()=0
Close camera.
virtual unsigned char * buffer()=0
Get access to current image buffer.
FvAcquisitionThread(const char *id, firevision::Camera *camera, fawkes::Logger *logger, fawkes::Clock *clock)
Constructor.
virtual unsigned int pixel_height()=0
Height of image in pixels.
virtual void start()=0
Start image transfer from the camera.
Mutex mutual exclusion lock.
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
const char * type() const
Get message type.
continuous mode, use if there are only continuous threads for this acquisition thread.
System ran out of memory and desired operation could not be fulfilled.
BlackBoard interface listener.
AqtMode aqtmode()
Get acquisition thread mode.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
virtual void dispose_buffer()=0
Dispose current buffer.
virtual void close(Interface *interface)=0
Close interface.