Fawkes API  Fawkes Development Version
dp_thread.h
00001 
00002 /***************************************************************************
00003  *  dp_thread.h - DirectedPerception pan/tilt unit act thread
00004  *
00005  *  Created: Sun Jun 21 17:26:33 2009
00006  *  Copyright  2006-2009  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 #ifndef __PLUGINS_PANTILT_DIRPERC_DP_THREAD_H_
00024 #define __PLUGINS_PANTILT_DIRPERC_DP_THREAD_H_
00025 
00026 #include "../act_thread.h"
00027 
00028 #include <blackboard/interface_listener.h>
00029 
00030 #ifdef USE_TIMETRACKER
00031 #  include <utils/time/tracker.h>
00032 #endif
00033 #include <string>
00034 #include <memory>
00035 
00036 namespace fawkes {
00037   class PanTiltInterface;
00038 }
00039 
00040 class DirectedPerceptionPTU;
00041 
00042 class PanTiltDirectedPerceptionThread
00043 : public PanTiltActThread,
00044   public fawkes::BlackBoardInterfaceListener
00045 {
00046  public:
00047   PanTiltDirectedPerceptionThread(std::string &pantilt_cfg_prefix,
00048                                   std::string &ptu_cfg_prefix,
00049                                   std::string &ptu_name);
00050 
00051   virtual void init();
00052   virtual void finalize();
00053   virtual void loop();
00054 
00055   // For BlackBoardInterfaceListener
00056   virtual bool bb_interface_message_received(fawkes::Interface *interface,
00057                                              fawkes::Message *message) throw();
00058 
00059   void update_sensor_values();
00060 
00061  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
00062  protected: virtual void run() { Thread::run(); }
00063 
00064  private:
00065   fawkes::PanTiltInterface *__pantilt_if;
00066 
00067   fawkes::RefPtr<DirectedPerceptionPTU> __ptu;
00068 
00069   std::string  __pantilt_cfg_prefix;
00070   std::string  __ptu_cfg_prefix;
00071   std::string  __ptu_name;
00072   std::string  __cfg_device;
00073   unsigned int __cfg_read_timeout_ms;
00074 
00075 
00076   class WorkerThread : public fawkes::Thread
00077   {
00078   public:
00079     WorkerThread(std::string ptu_name, fawkes::Logger *logger,
00080                  fawkes::RefPtr<DirectedPerceptionPTU> ptu);
00081 
00082     ~WorkerThread();
00083     void goto_pantilt(float pan, float tilt);
00084     void get_pantilt(float &pan, float &tilt);
00085     bool is_final();
00086     void stop_motion();
00087     bool has_fresh_data();
00088     void reset();
00089 
00090     virtual void loop();
00091 
00092   private:
00093     void exec_goto_pantilt(float pan, float tilt);
00094 
00095   private:
00096     fawkes::RefPtr<DirectedPerceptionPTU>  __ptu;
00097     fawkes::Logger                        *__logger;
00098 
00099     float         __pan_min;
00100     float         __pan_max;
00101     float         __tilt_min;
00102     float         __tilt_max;
00103 
00104     fawkes::Mutex *__move_mutex;
00105     bool  __move_pending;
00106     float __target_pan;
00107     float __target_tilt;
00108 
00109     float __cur_pan;
00110     float __cur_tilt;
00111 
00112     bool  __reset_pending;
00113     bool  __fresh_data;
00114   };
00115 
00116   WorkerThread *__wt;
00117 };
00118 
00119 #endif