23 #include "base_thread.h" 24 #include "acquisition_thread.h" 25 #include "aqt_vision_threads.h" 27 #include <core/threading/thread.h> 28 #include <core/threading/mutex.h> 29 #include <core/threading/mutex_locker.h> 30 #include <core/threading/barrier.h> 31 #include <logging/logger.h> 33 #include <fvutils/system/camargp.h> 34 #include <fvutils/ipc/shm_image.h> 35 #include <fvutils/ipc/shm_lut.h> 36 #include <fvcams/factory.h> 37 #include <fvcams/cam_exceptions.h> 38 #include <fvcams/control/factory.h> 39 #include <core/exceptions/software.h> 41 #include <aspect/vision.h> 79 SharedMemoryImageBuffer::cleanup(
false);
80 SharedMemoryLookupTable::cleanup(
false);
88 for (__ait = __aqts.begin(); __ait != __aqts.end(); ++__ait) {
94 __owned_controls.
lock();
96 for (i = __owned_controls.begin(); i != __owned_controls.end(); ++i) {
99 __owned_controls.clear();
100 __owned_controls.
unlock();
111 for (__ait = __aqts.begin(); __ait != __aqts.end(); ++__ait) {
112 __ait->second->set_vt_prepfin_hold(
true);
116 for (__ait = __aqts.begin(); __ait != __aqts.end(); ++__ait) {
117 __ait->second->set_vt_prepfin_hold(
false);
124 for (__ait = __aqts.begin(); __ait != __aqts.end(); ++__ait) {
127 __ait->second->wakeup(__aqt_barrier);
131 __aqt_barrier->
wait();
134 for (__ait = __aqts.begin(); __ait != __aqts.end();) {
135 if ( __ait->second->vision_threads->empty() &&
136 (__ait->second->vision_threads->empty_time() > __aqt_timeout) ) {
139 __ait->second->name());
143 delete __ait->second;
144 __aqts.erase(__ait++);
150 __started_threads.
lock();
152 while (stit != __started_threads.end()) {
155 stit->second->name(), __started_threads.size());
158 stit->second->vision_threads->set_thread_running(stit->first);
160 if ( stit->second->vision_threads->has_cyclic_thread() ) {
162 stit->second->set_enabled(
true);
166 stit->second->name());
168 stit->second->prepare_finalize();
169 stit->second->cancel();
170 stit->second->join();
172 stit->second->start();
173 stit->second->cancel_finalize();
175 }
else if ( stit->second->vision_threads->has_cont_thread() ) {
177 stit->second->set_enabled(
true);
181 stit->second->name());
182 stit->second->prepare_finalize();
183 stit->second->cancel();
184 stit->second->join();
186 stit->second->start();
187 stit->second->cancel_finalize();
190 logger->
log_warn(
name(),
"Acquisition thread %s has no threads while we expected some",
191 stit->second->name());
193 stit->second->set_enabled(
false);
198 __started_threads.erase( stittmp );
200 __started_threads.
unlock();
203 unsigned int num_cyclic_threads = 0;
204 for (__ait = __aqts.begin(); __ait != __aqts.end(); ++__ait) {
205 if ( __ait->second->vision_threads->has_cyclic_thread() ) {
206 ++num_cyclic_threads;
209 cond_recreate_barrier(num_cyclic_threads);
211 for (__ait = __aqts.begin(); __ait != __aqts.end(); ++__ait) {
212 __ait->second->set_vt_prepfin_hold(
false);
239 if ( vision_thread == NULL ) {
246 if ( __aqts.find(
id) != __aqts.end() ) {
248 c = __aqts[id]->camera_instance(cspace,
250 VisionAspect::CONTINUOUS));
252 __aqts[id]->vision_threads->add_waiting_thread(thread);
257 cam = CameraFactory::instance(cap);
261 e.
append(
"Could not open camera");
268 VisionAspect::CONTINUOUS));
277 logger->
log_info(
name(),
"Acquisition thread '%s' started for thread '%s' and camera '%s'",
278 aqt->
name(), thread->
name(),
id.c_str());
286 e.
append(
"FvBaseVisionMaster: could not instantiate camera");
291 e.
append(
"FvBaseVisionMaster: could not open or start camera");
311 if ( __aqts.find(
id) != __aqts.end() ) {
312 __aqts[id]->raw_subscriber_thread = thread;
323 FvBaseThread::create_camctrl(
const char *camera_string)
325 CameraControl *cc = CameraControlFactory::instance(camera_string);
327 __owned_controls.
lock();
328 __owned_controls.push_back(cc);
329 __owned_controls.sort();
330 __owned_controls.unique();
331 __owned_controls.
unlock();
334 throw Exception(
"Cannot create camera control of desired type");
346 if (__aqts.find(
id) != __aqts.end()) {
347 return CameraControlFactory::instance(__aqts[
id]->get_camera());
349 return create_camctrl(cam_string);
356 const std::type_info &typeinf)
363 if (__aqts.find(
id) != __aqts.end()) {
364 return CameraControlFactory::instance(typeinf, __aqts[
id]->get_camera());
366 return create_camctrl(cam_string);
374 __owned_controls.
lock();
376 if ((f = std::find(__owned_controls.begin(), __owned_controls.end(), cc)) != __owned_controls.end()) {
378 __owned_controls.erase(f);
380 __owned_controls.
unlock();
389 FvBaseThread::cond_recreate_barrier(
unsigned int num_cyclic_threads)
391 if ( (num_cyclic_threads + 1) != __aqt_barrier->
count() ) {
392 delete __aqt_barrier;
393 __aqt_barrier =
new Barrier( num_cyclic_threads + 1 );
402 unsigned int num_cyclic_threads = 0;
404 for (__ait = __aqts.begin(); __ait != __aqts.end(); ++__ait) {
407 __ait->second->vision_threads->remove_thread(thread);
409 if (__ait->second->raw_subscriber_thread == thread) {
410 __ait->second->raw_subscriber_thread = NULL;
413 if ( __ait->second->vision_threads->has_cyclic_thread() ) {
414 ++num_cyclic_threads;
418 "on unregister", __ait->second->name());
420 __ait->second->prepare_finalize();
421 __ait->second->cancel();
422 __ait->second->join();
424 __ait->second->start();
425 __ait->second->cancel_finalize();
428 if (__ait->second->vision_threads->empty()) {
431 __ait->second->name());
432 __ait->second->set_enabled(
false);
436 cond_recreate_barrier(num_cyclic_threads);
446 for (__ait = __aqts.begin(); __ait != __aqts.end(); ++__ait) {
447 if (__ait->second->vision_threads->has_waiting_thread(thread)) {
448 __started_threads.
lock();
449 __started_threads[thread] = __ait->second;
450 __started_threads.
unlock();
463 for (__ait = __aqts.begin(); __ait != __aqts.end(); ++__ait) {
464 __ait->second->vision_threads->remove_waiting_thread(thread);
void add_notification_listener(ThreadNotificationListener *notification_listener)
Add notification listener.
virtual bool thread_started(fawkes::Thread *thread)
Thread started successfully.
virtual firevision::Camera * register_for_raw_camera(const char *camera_string, fawkes::Thread *thread)
Register thread for camera.
unsigned int count()
Get number of threads this barrier will wait for.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Camera interface for image aquiring devices in FireVision.
void lock() const
Lock list.
virtual void lock() const
Lock list.
virtual void remove(ThreadList &tl)=0
Remove multiple threads.
Fawkes library namespace.
virtual void wait()
Wait for other threads.
virtual firevision::Camera * register_for_camera(const char *camera_string, fawkes::Thread *thread, firevision::colorspace_t cspace=firevision::YUV422_PLANAR)
Register thread for camera.
Unknown camera type exception.
FvAqtVisionThreads * vision_threads
Vision threads assigned to this acquisition thread.
virtual void finalize()
Finalize the thread.
Thread class encapsulation of pthreads.
FireVision base application acquisition thread.
Logger * logger
This is the Logger member used to access the logger.
std::string cam_id() const
Get camera ID.
virtual firevision::VisionMaster * vision_master()
Get vision master.
ThreadCollector * thread_collector
Thread collector.
Clock * clock
By means of this member access to the clock is given.
Thread aspect to use blocked timing.
virtual void release_camctrl(firevision::CameraControl *cc)
Release a camera control.
virtual firevision::CameraControl * acquire_camctrl(const char *cam_string)
Retrieve a CameraControl for the specified camera string.
Camera control interface base class.
Base class for exceptions in Fawkes.
FvBaseThread()
Constructor.
Thread aspect to use in FireVision apps.
VisionThreadMode vision_thread_mode()
Get the vision thread mode of this thread.
virtual void unregister_thread(fawkes::Thread *thread)
Unregister a thread.
virtual void unlock() const
Unlock list.
void unlock() const
Unlock list.
const char * name() const
Get name of thread.
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 add(ThreadList &tl)=0
Add multiple threads.
virtual ~FvBaseThread()
Destructor.
virtual void open()=0
Open the camera.
void add_waiting_thread(fawkes::Thread *thread)
Add a thread in waiting state.
cyclic mode, use if there is at least one cyclic thread for this acquisition thread.
std::string cam_type() const
Get camera type.
virtual void loop()
Thread loop.
virtual bool thread_init_failed(fawkes::Thread *thread)
Thread initialization failed.
virtual void init()
Initialize the thread.
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
continuous mode, use if there are only continuous threads for this acquisition thread.
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
void append(const char *format,...)
Append messages to the message list.