Fawkes API  Fawkes Development Version
hand_if_observer.cpp
00001 
00002 /***************************************************************************
00003  *  hand_if_observer.cpp - Skeleton hand interface observer
00004  *
00005  *  Created: Sat Apr 02 19:39:31 2011 (RoboCup German Open 2011, Magdeburg)
00006  *  Copyright  2006-2011  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include <plugins/openni/utils/hand_if_observer.h>
00024 
00025 #include <blackboard/blackboard.h>
00026 #include <interfaces/ObjectPositionInterface.h>
00027 
00028 namespace fawkes {
00029   namespace openni {
00030 #if 0 /* just to make Emacs auto-indent happy */
00031   }
00032 }
00033 #endif
00034 
00035 /** @class HandIfObserver <plugins/openni/utils/hand_if_observer.h>
00036  * Hand interface observer.
00037  * This class opens all OpenNI hand interfaces and registers as an
00038  * observer to open any newly opened interface.
00039  * @author Tim Niemueller
00040  */
00041 
00042 /** Constructor.
00043  * @param bb blackboard to interact with
00044  * @param hands hand map for exchange with others
00045  */
00046 HandIfObserver::HandIfObserver(BlackBoard *bb, HandMap &hands)
00047   : __hands(hands)
00048 {
00049   __queue_lock = new Mutex();
00050   __bb = bb;
00051 
00052   std::list<ObjectPositionInterface *> hand_ifs =
00053     __bb->open_multiple_for_reading<ObjectPositionInterface>("OpenNI Hand *");
00054 
00055   std::list<ObjectPositionInterface *>::iterator i;
00056   for (i = hand_ifs.begin(); i != hand_ifs.end(); ++i) {
00057     HandInfo hand;
00058     hand.hand_if = *i;
00059     __hands[hand.hand_if->id()] = hand;
00060   }
00061 
00062   bbio_add_observed_create("ObjectPositionInterface", "OpenNI Hand *");
00063   __bb->register_observer(this);
00064 }
00065 
00066 
00067 /** Destructor. */
00068 HandIfObserver::~HandIfObserver()
00069 {
00070   __bb->unregister_observer(this);
00071   delete __queue_lock;
00072 }
00073 
00074 void
00075 HandIfObserver::bb_interface_created(const char *type, const char *id) throw()
00076 {
00077   if (__hands.find(id) == __hands.end()) {
00078     __queue_lock->lock();
00079     __queues[__active_queue].push(id);
00080     __queue_lock->unlock();
00081   }
00082 }
00083 
00084 /** Process internal queue.
00085  * This should be called regularly to process incoming events.
00086  */
00087 void
00088 HandIfObserver::process_queue()
00089 {
00090   __queue_lock->lock();
00091   unsigned int proc_queue = __active_queue;
00092   __active_queue = 1 - __active_queue;
00093   __queue_lock->unlock();
00094   while (! __queues[proc_queue].empty()) {
00095     std::string id = __queues[proc_queue].front();
00096 
00097     try {
00098       HandInfo hand;
00099       hand.hand_if = __bb->open_for_reading<ObjectPositionInterface>(id.c_str());
00100 
00101       __hands[id] = hand;
00102     } catch (Exception &e) {
00103       e.print_trace();
00104       continue;
00105     }
00106 
00107     __queues[proc_queue].pop();
00108   }
00109 }
00110 
00111 } // end namespace fawkes::openni
00112 } // end namespace fawkes