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