Fawkes API  Fawkes Development Version
hand_if_observer.cpp
1 
2 /***************************************************************************
3  * hand_if_observer.cpp - Skeleton hand interface observer
4  *
5  * Created: Sat Apr 02 19:39:31 2011 (RoboCup German Open 2011, Magdeburg)
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <blackboard/blackboard.h>
24 #include <interfaces/ObjectPositionInterface.h>
25 #include <plugins/openni/utils/hand_if_observer.h>
26 
27 namespace fawkes {
28 namespace openni {
29 
30 /** @class HandIfObserver <plugins/openni/utils/hand_if_observer.h>
31  * Hand interface observer.
32  * This class opens all OpenNI hand interfaces and registers as an
33  * observer to open any newly opened interface.
34  * @author Tim Niemueller
35  */
36 
37 /** Constructor.
38  * @param bb blackboard to interact with
39  * @param hands hand map for exchange with others
40  */
41 HandIfObserver::HandIfObserver(BlackBoard *bb, HandMap &hands) : hands_(hands)
42 {
43  queue_lock_ = new Mutex();
44  bb_ = bb;
45 
46  std::list<ObjectPositionInterface *> hand_ifs =
48 
49  std::list<ObjectPositionInterface *>::iterator i;
50  for (i = hand_ifs.begin(); i != hand_ifs.end(); ++i) {
51  HandInfo hand;
52  hand.hand_if = *i;
53  hands_[hand.hand_if->id()] = hand;
54  }
55 
56  bbio_add_observed_create("ObjectPositionInterface", "OpenNI Hand *");
57  bb_->register_observer(this);
58 }
59 
60 /** Destructor. */
62 {
63  bb_->unregister_observer(this);
64  delete queue_lock_;
65 }
66 
67 void
68 HandIfObserver::bb_interface_created(const char *type, const char *id) noexcept
69 {
70  if (hands_.find(id) == hands_.end()) {
71  queue_lock_->lock();
72  queues_[active_queue_].push(id);
73  queue_lock_->unlock();
74  }
75 }
76 
77 /** Process internal queue.
78  * This should be called regularly to process incoming events.
79  */
80 void
82 {
83  queue_lock_->lock();
84  unsigned int proc_queue = active_queue_;
85  active_queue_ = 1 - active_queue_;
86  queue_lock_->unlock();
87  while (!queues_[proc_queue].empty()) {
88  std::string id = queues_[proc_queue].front();
89 
90  try {
91  HandInfo hand;
92  hand.hand_if = bb_->open_for_reading<ObjectPositionInterface>(id.c_str());
93 
94  hands_[id] = hand;
95  } catch (Exception &e) {
96  e.print_trace();
97  continue;
98  }
99 
100  queues_[proc_queue].pop();
101  }
102 }
103 
104 } // namespace openni
105 } // end namespace fawkes
void bbio_add_observed_create(const char *type_pattern, const char *id_pattern="*") noexcept
Add interface creation type to watch list.
The BlackBoard abstract class.
Definition: blackboard.h:46
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:240
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:225
virtual std::list< Interface * > open_multiple_for_reading(const char *type_pattern, const char *id_pattern="*", const char *owner=NULL)=0
Open multiple interfaces for reading.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace() noexcept
Prints trace to stderr.
Definition: exception.cpp:601
const char * id() const
Get identifier of interface.
Definition: interface.cpp:661
Mutex mutual exclusion lock.
Definition: mutex.h:33
void lock()
Lock this mutex.
Definition: mutex.cpp:87
void unlock()
Unlock the mutex.
Definition: mutex.cpp:131
ObjectPositionInterface Fawkes BlackBoard Interface.
virtual void bb_interface_created(const char *type, const char *id) noexcept
BlackBoard interface created notification.
HandIfObserver(BlackBoard *bb, HandMap &hands)
Constructor.
void process_queue()
Process internal queue.
Fawkes library namespace.
Hand info to pass to draw_skeletons().
Definition: types.h:48
fawkes::ObjectPositionInterface * hand_if
Hand pos interface.
Definition: types.h:49