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 <plugins/openni/utils/hand_if_observer.h>
24 
25 #include <blackboard/blackboard.h>
26 #include <interfaces/ObjectPositionInterface.h>
27 
28 namespace fawkes {
29  namespace openni {
30 #if 0 /* just to make Emacs auto-indent happy */
31  }
32 }
33 #endif
34 
35 /** @class HandIfObserver <plugins/openni/utils/hand_if_observer.h>
36  * Hand interface observer.
37  * This class opens all OpenNI hand interfaces and registers as an
38  * observer to open any newly opened interface.
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor.
43  * @param bb blackboard to interact with
44  * @param hands hand map for exchange with others
45  */
47  : __hands(hands)
48 {
49  __queue_lock = new Mutex();
50  __bb = bb;
51 
52  std::list<ObjectPositionInterface *> hand_ifs =
53  __bb->open_multiple_for_reading<ObjectPositionInterface>("OpenNI Hand *");
54 
55  std::list<ObjectPositionInterface *>::iterator i;
56  for (i = hand_ifs.begin(); i != hand_ifs.end(); ++i) {
57  HandInfo hand;
58  hand.hand_if = *i;
59  __hands[hand.hand_if->id()] = hand;
60  }
61 
62  bbio_add_observed_create("ObjectPositionInterface", "OpenNI Hand *");
63  __bb->register_observer(this);
64 }
65 
66 
67 /** Destructor. */
69 {
70  __bb->unregister_observer(this);
71  delete __queue_lock;
72 }
73 
74 void
75 HandIfObserver::bb_interface_created(const char *type, const char *id) throw()
76 {
77  if (__hands.find(id) == __hands.end()) {
78  __queue_lock->lock();
79  __queues[__active_queue].push(id);
80  __queue_lock->unlock();
81  }
82 }
83 
84 /** Process internal queue.
85  * This should be called regularly to process incoming events.
86  */
87 void
89 {
90  __queue_lock->lock();
91  unsigned int proc_queue = __active_queue;
92  __active_queue = 1 - __active_queue;
93  __queue_lock->unlock();
94  while (! __queues[proc_queue].empty()) {
95  std::string id = __queues[proc_queue].front();
96 
97  try {
98  HandInfo hand;
99  hand.hand_if = __bb->open_for_reading<ObjectPositionInterface>(id.c_str());
100 
101  __hands[id] = hand;
102  } catch (Exception &e) {
103  e.print_trace();
104  continue;
105  }
106 
107  __queues[proc_queue].pop();
108  }
109 }
110 
111 } // end namespace fawkes::openni
112 } // end namespace fawkes
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:230
ObjectPositionInterface Fawkes BlackBoard Interface.
Hand info to pass to draw_skeletons().
Definition: types.h:51
Fawkes library namespace.
void unlock()
Unlock the mutex.
Definition: mutex.cpp:135
const char * id() const
Get identifier of interface.
Definition: interface.cpp:661
virtual void bb_interface_created(const char *type, const char *id)
BlackBoard interface created notification.
void process_queue()
Process internal queue.
void bbio_add_observed_create(const char *type_pattern, const char *id_pattern="*")
Add interface creation type to watch list.
Base class for exceptions in Fawkes.
Definition: exception.h:36
HandIfObserver(BlackBoard *bb, HandMap &hands)
Constructor.
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:244
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.
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
fawkes::ObjectPositionInterface * hand_if
Hand pos interface.
Definition: types.h:52
void lock()
Lock this mutex.
Definition: mutex.cpp:89
The BlackBoard abstract class.
Definition: blackboard.h:48
Mutex mutual exclusion lock.
Definition: mutex.h:32