Fawkes API  Fawkes Development Version
skel_if_observer.cpp
1 
2 /***************************************************************************
3  * skel_if_observer.cpp - Skeleton interface observer
4  *
5  * Created: Sat Apr 02 18:20:29 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/skel_if_observer.h>
24 
25 #include <blackboard/blackboard.h>
26 #include <interfaces/HumanSkeletonInterface.h>
27 #include <interfaces/HumanSkeletonProjectionInterface.h>
28 
29 #include <cstdio>
30 
31 namespace fawkes {
32  namespace openni {
33 #if 0 /* just to make Emacs auto-indent happy */
34  }
35 }
36 #endif
37 
38 /** @class SkelIfObserver <plugins/openni/utils/skel_if_observer.h>
39  * Skeleton interface observer.
40  * This class opens all OpenNI skeleton interfaces and registers as an
41  * observer to open any newly opened interface.
42  * @author Tim Niemueller
43  */
44 
45 /** Constructor.
46  * @param bb blackboard to interact with
47  * @param users user map for exchange with others
48  */
50  : __users(users)
51 {
52  __queue_lock = new Mutex();
53  __bb = bb;
54 
55  std::list<HumanSkeletonInterface *> skels =
56  __bb->open_multiple_for_reading<HumanSkeletonInterface>("OpenNI Human *");
57 
58  std::list<HumanSkeletonProjectionInterface *> projs;
59 
60  std::list<HumanSkeletonInterface *>::iterator i;
61  for (i = skels.begin(); i != skels.end(); ++i) {
62  printf("Opened %s\n", (*i)->uid());
63 
64  UserInfo user;
65  user.skel_if = *i;
66  user.proj_if =
67  __bb->open_for_reading<HumanSkeletonProjectionInterface>(user.skel_if->id());
68 
69  __users[user.skel_if->id()] = user;
70  }
71 
72  bbio_add_observed_create("HumanSkeletonInterface", "OpenNI Human *");
73  __bb->register_observer(this);
74 }
75 
76 
77 /** Destructor. */
79 {
80  __bb->unregister_observer(this);
81  delete __queue_lock;
82 }
83 
84 void
85 SkelIfObserver::bb_interface_created(const char *type, const char *id) throw()
86 {
87  if (__users.find(id) == __users.end()) {
88  __queue_lock->lock();
89  __queues[__active_queue].push(id);
90  __queue_lock->unlock();
91  }
92 }
93 
94 /** Process internal queue.
95  * This should be called regularly to process incoming events.
96  */
97 void
99 {
100  __queue_lock->lock();
101  unsigned int proc_queue = __active_queue;
102  __active_queue = 1 - __active_queue;
103  __queue_lock->unlock();
104  while (! __queues[proc_queue].empty()) {
105  std::string id = __queues[proc_queue].front();
106 
107  try {
108  UserInfo user;
109  printf("Opening %s\n", id.c_str());
110  user.skel_if = __bb->open_for_reading<HumanSkeletonInterface>(id.c_str());
111  try {
112  user.proj_if =
114  } catch (Exception &e) {
115  __bb->close(user.skel_if);
116  throw;
117  }
118 
119  __users[id] = user;
120  } catch (Exception &e) {
121  e.print_trace();
122  continue;
123  }
124 
125  __queues[proc_queue].pop();
126  }
127 }
128 
129 } // end namespace fawkes::openni
130 } // end namespace fawkes
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:230
HumanSkeletonProjectionInterface Fawkes BlackBoard Interface.
Fawkes library namespace.
void unlock()
Unlock the mutex.
Definition: mutex.cpp:135
fawkes::HumanSkeletonProjectionInterface * proj_if
Projection interface.
Definition: types.h:43
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
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:244
fawkes::HumanSkeletonInterface * skel_if
Skeleton interface.
Definition: types.h:42
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.
virtual void bb_interface_created(const char *type, const char *id)
BlackBoard interface created notification.
SkelIfObserver(BlackBoard *bb, UserMap &users)
Constructor.
HumanSkeletonInterface Fawkes BlackBoard Interface.
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
User info to pass to draw_skeletons().
Definition: types.h:41
virtual void close(Interface *interface)=0
Close interface.