Fawkes API  Fawkes Development Version
evid100p.cpp
1 
2 /***************************************************************************
3  * evid100p.cpp - Sony EviD100P Visca wrapper
4  *
5  * Created: Sun Jun 21 13:10:51 2009
6  * Copyright 2005-2009 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. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include "evid100p.h"
25 
26 #include <core/exceptions/software.h>
27 #include <utils/math/angle.h>
28 
29 using namespace fawkes;
30 
31 /** @class SonyEviD100PVisca "evid100p.h"
32  * Sony EviD100P Visca controller.
33  * This sub-class using the Visca protocol contains some constants specific
34  * for the Sony EviD100P camera.
35  * @author Tim Niemueller
36  */
37 
38 /** Maximum pan. */
39 const int SonyEviD100PVisca::MAX_PAN = 1440;
40 /** Minimum pan. */
41 const int SonyEviD100PVisca::MIN_PAN = -1439;
42 /** Max Tilt. */
43 const int SonyEviD100PVisca::MAX_TILT = 360;
44 /** Min tilt .*/
45 const int SonyEviD100PVisca::MIN_TILT = - 359;
46 
47 /** Max pan in degrees. */
48 const float SonyEviD100PVisca::MAX_PAN_DEG = 100.f;
49 /** Min pan in degrees. */
50 const float SonyEviD100PVisca::MIN_PAN_DEG = -100.f;
51 /** Max tilt in degrees. */
52 const float SonyEviD100PVisca::MAX_TILT_DEG = 25.f;
53 /** Min tilt in degrees. */
54 const float SonyEviD100PVisca::MIN_TILT_DEG = - 25.f;
55 
56 /** Max pan in rad. */
57 const float SonyEviD100PVisca::MAX_PAN_RAD = deg2rad(MAX_PAN_DEG);
58 /** Min pan in rad. */
59 const float SonyEviD100PVisca::MIN_PAN_RAD = deg2rad(MIN_PAN_DEG);
60 /** Max tilt in rad. */
61 const float SonyEviD100PVisca::MAX_TILT_RAD = deg2rad(MAX_TILT_DEG);
62 /** Min tilt in rad. */
63 const float SonyEviD100PVisca::MIN_TILT_RAD = deg2rad(MIN_TILT_DEG);
64 
65 /** Pan steps per degree */
66 const float SonyEviD100PVisca::PAN_STEPS_PER_DEG = MAX_PAN / MAX_PAN_DEG;
67 /** Tilt steps per degree */
68 const float SonyEviD100PVisca::TILT_STEPS_PER_DEG = MAX_TILT / MAX_TILT_DEG;
69 
70 /** Pan steps per rad */
71 const float SonyEviD100PVisca::PAN_STEPS_PER_RAD = MAX_PAN / MAX_PAN_RAD;
72 /** Tilt steps per rad */
73 const float SonyEviD100PVisca::TILT_STEPS_PER_RAD = MAX_TILT / MAX_TILT_RAD;
74 
75 /** Pastel effect. */
76 const unsigned int SonyEviD100PVisca::EFFECT_PASTEL = 1;
77 /** Negative effect. */
78 const unsigned int SonyEviD100PVisca::EFFECT_NEGATIVE = 2;
79 /** Sepia effect. */
80 const unsigned int SonyEviD100PVisca::EFFECT_SEPIA = 3;
81 /** B/W effect. */
82 const unsigned int SonyEviD100PVisca::EFFECT_BW = 4;
83 /** Solarize effect. */
84 const unsigned int SonyEviD100PVisca::EFFECT_SOLARIZE = 5;
85 /** Mosaic effect. */
86 const unsigned int SonyEviD100PVisca::EFFECT_MOSAIC = 6;
87 /** Slim effect. */
88 const unsigned int SonyEviD100PVisca::EFFECT_SLIM = 7;
89 /** Stretch effect. */
90 const unsigned int SonyEviD100PVisca::EFFECT_STRETCH = 8;
91 
92 
93 /** Speed table for supported pan speed values in radians.
94  * Has been created empirically.
95  */
97  {0.03548, 0.04138, 0.05319, 0.06497, 0.08262, 0.10608, 0.12951, 0.15865,
98  0.19933, 0.24535, 0.30159, 0.35137, 0.43540, 0.53611, 0.67246, 0.81519,
99  0.99870, 1.20673, 1.45304, 1.70703, 1.99278, 2.25729, 2.44293, 2.71852};
100 
101 /** Speed table for supported tilt speed values in radians.
102  * Has been created empirically.
103  */
105  {0.03541, 0.04127, 0.05298, 0.06449, 0.08195, 0.10480, 0.12741, 0.15535,
106  0.19356, 0.23685, 0.28438, 0.33367, 0.41066, 0.49517, 0.59622, 0.71474,
107  0.83085, 0.97431, 1.08745, 1.20977};
108 
109 
110 /** Constructor.
111  * @param device_file serial device file (e.g. /dev/ttyUSB0)
112  * @param def_timeout_ms default read timeout, used if no specific timeout
113  * is passed
114  * @param blocking true to make gathering pan/tilt information wait for
115  * the reponse, false to be able to split the operation
116  */
117 SonyEviD100PVisca::SonyEviD100PVisca(const char *device_file,
118  unsigned int def_timeout_ms,
119  bool blocking)
120  : Visca(device_file, def_timeout_ms, blocking)
121 {
122 }
123 
124 
125 /** Destructor. */
127 {
128 }
129 
130 
131 /** Set pan/tilt in radians.
132  * @param pan pan value in radians
133  * @param tilt tilt value in radians
134  */
135 void
137 {
138  if ( (pan < MIN_PAN_RAD) || (pan > MAX_PAN_RAD) ) {
139  throw OutOfBoundsException("Illegal pan value", pan, MIN_PAN_RAD, MAX_PAN_RAD);
140  }
141  if ( (tilt < MIN_TILT_RAD) || (tilt > MAX_TILT_RAD) ) {
142  throw OutOfBoundsException("Illegal tilt value", tilt, MIN_TILT_RAD, MAX_TILT_RAD);
143  }
144 
145  int tpan = 0, ttilt = 0;
146 
147  tpan = (int)rint( pan * PAN_STEPS_PER_RAD );
148  ttilt = (int)rint( tilt * TILT_STEPS_PER_RAD );
149 
150  set_pan_tilt(tpan, ttilt);
151 }
152 
153 
154 /** Get pan/tilt in radians.
155  * @param pan upon return contains the current pan value
156  * @param tilt upone return contains the current tilt value
157  */
158 void
159 SonyEviD100PVisca::get_pan_tilt_rad(float &pan, float &tilt)
160 {
161  int tpan = 0, ttilt = 0;
162  get_pan_tilt(tpan, ttilt);
163 
164  pan = tpan / PAN_STEPS_PER_RAD;
165  tilt = ttilt / PAN_STEPS_PER_RAD;
166 }
167 
168 
169 /** Set speed given in rad/sec.
170  * Note that not the exact speed is taken, but rather the closes equivalent in
171  * motor ticks is taken.
172  * @param pan_speed desired pan speed in rad/sec
173  * @param tilt_speed desired tilt speed in rad/sec
174  * @exception OutOfBoundsException thrown if desired speed is out of range
175  */
176 void
177 SonyEviD100PVisca::set_speed_radsec(float pan_speed, float tilt_speed)
178 {
179  if ( (pan_speed < 0) ||
180  (pan_speed > SPEED_TABLE_PAN[SONY_EVID100P_NUM_PAN_SPEEDS - 1]) ) {
181  throw OutOfBoundsException("Illegal pan speed", pan_speed, 0,
182  SPEED_TABLE_PAN[SONY_EVID100P_NUM_PAN_SPEEDS - 1]);
183  }
184  if ( (tilt_speed < 0) ||
185  (tilt_speed > SPEED_TABLE_TILT[SONY_EVID100P_NUM_TILT_SPEEDS - 1]) ) {
186  throw OutOfBoundsException("Illegal tilt speed", tilt_speed, 0,
187  SPEED_TABLE_TILT[SONY_EVID100P_NUM_TILT_SPEEDS - 1]);
188  }
189 
190  unsigned int pan_ind = SONY_EVID100P_NUM_PAN_SPEEDS - 1;
191  float min_pan_dist = SPEED_TABLE_PAN[pan_ind];
192  float last_dist = min_pan_dist;;
193  for (unsigned int i = 0; i < SONY_EVID100P_NUM_PAN_SPEEDS; ++i) {
194  float dist = 0;
195  if ( (dist = fabs(pan_speed - SPEED_TABLE_PAN[i])) < min_pan_dist ) {
196  min_pan_dist = dist;
197  pan_ind = i;
198  } else if (dist > last_dist) {
199  break; // times are growing now, found best
200  }
201  last_dist = dist;
202  }
203 
204  unsigned int tilt_ind = SONY_EVID100P_NUM_TILT_SPEEDS - 1;
205  float min_tilt_dist = SPEED_TABLE_TILT[tilt_ind];
206  last_dist = min_tilt_dist;
207  for (unsigned int i = 0; i < SONY_EVID100P_NUM_TILT_SPEEDS; ++i) {
208  float dist = 0;
209  if ( (dist = fabs(tilt_speed - SPEED_TABLE_TILT[i])) < min_tilt_dist ) {
210  min_tilt_dist = dist;
211  tilt_ind = i;
212  } else if (dist > last_dist) {
213  break; // times are growing now, found best
214  }
215  last_dist = dist;
216  }
217 
218  set_pan_tilt_speed(pan_ind, tilt_ind);
219 }
220 
221 
222 /** Get current speed in rad/sec.
223  * @param pan_speed upon return contains pan speed in rad/sec
224  * @param tilt_speed upon return contains tilt speed in rad/sec
225  */
226 void
227 SonyEviD100PVisca::get_speed_radsec(float &pan_speed, float &tilt_speed)
228 {
229  unsigned char ps, ts;
230  get_pan_tilt_speed(ps, ts);
231  pan_speed = SPEED_TABLE_PAN[ps - 1];
232  tilt_speed = SPEED_TABLE_TILT[ps - 1];
233 }
234 
235 
236 /** Get speed limits.
237  * @param pan_min minimum pan speed possible
238  * @param pan_max maximum pan speed possible
239  * @param tilt_min minimum tilt speed possible
240  * @param tilt_max maximum tilt speed possible
241  */
242 void
243 SonyEviD100PVisca::get_speed_limits(float &pan_min, float &pan_max,
244  float &tilt_min, float &tilt_max)
245 {
246  pan_min = SPEED_TABLE_PAN[0];
247  pan_max = SPEED_TABLE_PAN[SONY_EVID100P_NUM_PAN_SPEEDS - 1];
248  tilt_min = SPEED_TABLE_TILT[0];
249  tilt_max = SPEED_TABLE_TILT[SONY_EVID100P_NUM_TILT_SPEEDS - 1];
250 }
static const float MIN_TILT_RAD
Min tilt in rad.
Definition: evid100p.h:61
static const unsigned int EFFECT_NEGATIVE
Negative effect.
Definition: evid100p.h:70
void set_pan_tilt(int pan, int tilt)
Set pan tilt.
Definition: visca.cpp:638
static const float MAX_PAN_DEG
Max pan in degrees.
Definition: evid100p.h:53
static const float PAN_STEPS_PER_RAD
Pan steps per rad.
Definition: evid100p.h:66
void get_speed_radsec(float &pan_speed, float &tilt_speed)
Get current speed in rad/sec.
Definition: evid100p.cpp:227
static const int MIN_PAN
Minimum pan.
Definition: evid100p.h:49
static const float MIN_PAN_RAD
Min pan in rad.
Definition: evid100p.h:59
static const float MAX_PAN_RAD
Max pan in rad.
Definition: evid100p.h:58
Fawkes library namespace.
static const unsigned int EFFECT_SEPIA
Sepia effect.
Definition: evid100p.h:71
void set_pan_tilt_speed(unsigned char pan_speed, unsigned char tilt_speed)
Set pan/tilt speed.
Definition: visca.cpp:696
static const unsigned int EFFECT_BW
B/W effect.
Definition: evid100p.h:72
static const float MAX_TILT_DEG
Max tilt in degrees.
Definition: evid100p.h:55
void set_speed_radsec(float pan_speed, float tilt_speed)
Set speed given in rad/sec.
Definition: evid100p.cpp:177
void set_pan_tilt_rad(float pan, float tilt)
Set pan/tilt in radians.
Definition: evid100p.cpp:136
void get_speed_limits(float &pan_min, float &pan_max, float &tilt_min, float &tilt_max)
Get speed limits.
Definition: evid100p.cpp:243
SonyEviD100PVisca(const char *device_file, unsigned int def_timeout_ms=30, bool blocking=true)
Constructor.
Definition: evid100p.cpp:117
static const float TILT_STEPS_PER_DEG
Tilt steps per degree.
Definition: evid100p.h:64
static const unsigned int EFFECT_SLIM
Slim effect.
Definition: evid100p.h:75
static const int MAX_PAN
Maximum pan.
Definition: evid100p.h:48
void get_pan_tilt_rad(float &pan, float &tilt)
Get pan/tilt in radians.
Definition: evid100p.cpp:159
void get_pan_tilt(int &pan, int &tilt)
Get pan and tilt values.
Definition: visca.cpp:752
static const unsigned int EFFECT_SOLARIZE
Solarize effect.
Definition: evid100p.h:73
Visca control protocol implementation over a serial line.
Definition: visca.h:50
static const int MAX_TILT
Max Tilt.
Definition: evid100p.h:50
static const float PAN_STEPS_PER_DEG
Pan steps per degree.
Definition: evid100p.h:63
static const float TILT_STEPS_PER_RAD
Tilt steps per rad.
Definition: evid100p.h:67
static const float MIN_PAN_DEG
Min pan in degrees.
Definition: evid100p.h:54
~SonyEviD100PVisca()
Destructor.
Definition: evid100p.cpp:126
static const unsigned int EFFECT_STRETCH
Stretch effect.
Definition: evid100p.h:76
static const unsigned int EFFECT_MOSAIC
Mosaic effect.
Definition: evid100p.h:74
static const float MAX_TILT_RAD
Max tilt in rad.
Definition: evid100p.h:60
static const int MIN_TILT
Min tilt .
Definition: evid100p.h:51
void get_pan_tilt_speed(unsigned char &pan_speed, unsigned char &tilt_speed)
Get pan/tilt speed.
Definition: visca.cpp:715
static const float SPEED_TABLE_TILT[SONY_EVID100P_NUM_TILT_SPEEDS]
Speed table for supported tilt speed values in radians.
Definition: evid100p.h:79
static const unsigned int EFFECT_PASTEL
Pastel effect.
Definition: evid100p.h:69
float deg2rad(float deg)
Convert an angle given in degrees to radians.
Definition: angle.h:37
Index out of bounds.
Definition: software.h:88
static const float MIN_TILT_DEG
Min tilt in degrees.
Definition: evid100p.h:56
static const float SPEED_TABLE_PAN[SONY_EVID100P_NUM_PAN_SPEEDS]
Speed table for supported pan speed values in radians.
Definition: evid100p.h:78