Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PanTiltControlInterface.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS 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 Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef PanTiltControlInterface_HH_
26 #define PanTiltControlInterface_HH_
27 
28 #include <PanTilt/DPPanTiltControl.hh>
29 #include <math.h>
30 #include <iostream>
31 
32 namespace BIAS
33 {
34  /**
35  @brief abstract control class for control of Directed Perception Pan Tilt Unit.
36 
37  Currently uses DPPanTiltControl to send commands but could be extended in the future
38  to use any other PTU controller class with common interface.
39 
40  @author fkellner, ischiller
41  */
42  class BIASPanTilt_EXPORT PanTiltControlInterface
43  {
44  public:
45 
46  /** @brief Starts controlling the PTU */
47  virtual void StartControl() = 0;
48 
49  /** @brief Stops/pauses control */
50  virtual void StopControl() = 0;
51 
52  /** @brief Resumes control */
53  virtual void ResumeControl() = 0;
54 
55  /** @brief Starts movement to given pan and tilt position */
56  virtual void StartMoveTo(const long int pan, const long int tilt) {};
57 
58  /** @brief Initialize PTU, called by derived classes.
59  @param[in] device The device string, e.g. COM0 or /dev/ttyUSB0
60  @param[n] heavyDutyMode Enables the heavy duty mode for PTU
61  */
62  PanTiltControlInterface(std::string device, bool heavyDutyMode)
63  {
64  bool bRet = ptControl.InitPanTiltUnit(device.c_str());
65  if (!bRet) {
66  std::cout << "PanTiltControlInterface : Initialising PTU failed!" << std::endl;
67  }
68 
69  // next line sets heavy duty mode
70  if (heavyDutyMode) {
71  ptControl.SendRawCommand("PU2000 PA2000 PB60 TU2000 TA2000 TB60 PMR TMR %%1R600 DS\n");
72  }
73 
74  std::string s;
75  while (ptControl.ReadRawLine(s) != 0) {
76  std::cout << "Read from PTU controller : " << s.c_str() << std::endl;
77  }
78  ptControl.SetMode(ABSOLUTEMODE);
79  await_completion();
80  ptControl.SetSpeed(DPPTU_MAXSPEED, DPPTU_MAXSPEED);
81  ptControl.SetPosition(0,0);
82  await_completion();
83  ptControl.GetCurrentPosition(cur_pan, cur_tilt);
84  ptControl.GetCurrentPosition(cur_pan, cur_tilt);
85  ptControl.QueryResolution(presolution,tresolution);
86  }
87 
88  /** @brief Default destructor. Moves PTU to position (0,0) and releases interface. */
90  {
91  ptControl.SetMode(ABSOLUTEMODE);
92  ptControl.SetSpeed(DPPTU_MAXSPEED/4, DPPTU_MAXSPEED/4);
93  ptControl.SetPosition(0,0);
94  await_completion();
95  ptControl.ClosePanTiltUnit();
96  }
97 
98  /** @brief Get position, values should be updated by derived classes. */
99  static void GetPosition(long int &pan, long int &tilt)
100  {
101  pan = cur_pan;
102  tilt = cur_tilt;
103  }
104 
105  /** @brief Get position in degrees, values should be updated by derived classes. */
106  static void GetPositionDegrees(double &panDegree, double &tiltDegree)
107  {
108  panDegree = -cur_pan * presolution * M_PI / 180.0f;
109  tiltDegree = cur_tilt * tresolution * M_PI / 180.0f;
110  }
111 
112  /** @brief Set the position to move to. */
113  void SetPosition(long int pan, long int tilt, bool waitComplete = true)
114  {
115  ptControl.SetMode(ABSOLUTEMODE);
116  //ptControl.SetSpeed(DPPTU_MAXSPEED/4, DPPTU_MAXSPEED/4);
117  ptControl.SetPosition(pan, tilt, waitComplete);
118  cur_pan = pan;
119  cur_tilt = tilt;
120  }
121 
122  /** @brief Waits until the current command is completed. */
124  {
125  await_completion();
126  ptControl.GetCurrentPosition(cur_pan, cur_tilt);
127  }
128 
129  protected:
130 
132  static long int cur_pan;
133  static long int cur_tilt;
134  static double presolution;
135  static double tresolution;
136  };
137 
138  /**
139  @class PanTiltControlFactory
140  @brief Abstract controller factory used to create all different kinds of PTU controls.
141  */
143  {
144  public:
145  virtual PanTiltControlInterface* Create(std::string device, bool heavyDutyMode) = 0;
147  };
148 
149 }
150 
151 #endif // PanTiltControlInterface_HH_
abstract control class for control of Directed Perception Pan Tilt Unit.
Basic controller interface for Directed Perception Pan Tilt Unit.
static void GetPosition(long int &pan, long int &tilt)
Get position, values should be updated by derived classes.
static void GetPositionDegrees(double &panDegree, double &tiltDegree)
Get position in degrees, values should be updated by derived classes.
void SetPosition(long int pan, long int tilt, bool waitComplete=true)
Set the position to move to.
virtual ~PanTiltControlInterface()
Default destructor.
void AwaitCompletion()
Waits until the current command is completed.
virtual PanTiltControlInterface * Create(std::string device, bool heavyDutyMode)=0
virtual void StartMoveTo(const long int pan, const long int tilt)
Starts movement to given pan and tilt position.
Abstract controller factory used to create all different kinds of PTU controls.
PanTiltControlInterface(std::string device, bool heavyDutyMode)
Initialize PTU, called by derived classes.