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 /** Speed table for supported pan speed values in radians.
93  * Has been created empirically.
94  */
95 const float SonyEviD100PVisca::SPEED_TABLE_PAN[] = {0.03548, 0.04138, 0.05319, 0.06497, 0.08262,
96  0.10608, 0.12951, 0.15865, 0.19933, 0.24535,
97  0.30159, 0.35137, 0.43540, 0.53611, 0.67246,
98  0.81519, 0.99870, 1.20673, 1.45304, 1.70703,
99  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  */
104 const float SonyEviD100PVisca::SPEED_TABLE_TILT[] = {0.03541, 0.04127, 0.05298, 0.06449, 0.08195,
105  0.10480, 0.12741, 0.15535, 0.19356, 0.23685,
106  0.28438, 0.33367, 0.41066, 0.49517, 0.59622,
107  0.71474, 0.83085, 0.97431, 1.08745, 1.20977};
108 
109 /** Constructor.
110  * @param device_file serial device file (e.g. /dev/ttyUSB0)
111  * @param def_timeout_ms default read timeout, used if no specific timeout
112  * is passed
113  * @param blocking true to make gathering pan/tilt information wait for
114  * the reponse, false to be able to split the operation
115  */
116 SonyEviD100PVisca::SonyEviD100PVisca(const char * device_file,
117  unsigned int def_timeout_ms,
118  bool blocking)
119 : Visca(device_file, def_timeout_ms, blocking)
120 {
121 }
122 
123 /** Destructor. */
125 {
126 }
127 
128 /** Set pan/tilt in radians.
129  * @param pan pan value in radians
130  * @param tilt tilt value in radians
131  */
132 void
134 {
135  if ((pan < MIN_PAN_RAD) || (pan > MAX_PAN_RAD)) {
136  throw OutOfBoundsException("Illegal pan value", pan, MIN_PAN_RAD, MAX_PAN_RAD);
137  }
138  if ((tilt < MIN_TILT_RAD) || (tilt > MAX_TILT_RAD)) {
139  throw OutOfBoundsException("Illegal tilt value", tilt, MIN_TILT_RAD, MAX_TILT_RAD);
140  }
141 
142  int tpan = 0, ttilt = 0;
143 
144  tpan = (int)rintf(pan * PAN_STEPS_PER_RAD);
145  ttilt = (int)rintf(tilt * TILT_STEPS_PER_RAD);
146 
147  set_pan_tilt(tpan, ttilt);
148 }
149 
150 /** Get pan/tilt in radians.
151  * @param pan upon return contains the current pan value
152  * @param tilt upone return contains the current tilt value
153  */
154 void
155 SonyEviD100PVisca::get_pan_tilt_rad(float &pan, float &tilt)
156 {
157  int tpan = 0, ttilt = 0;
158  get_pan_tilt(tpan, ttilt);
159 
160  pan = tpan / PAN_STEPS_PER_RAD;
161  tilt = ttilt / PAN_STEPS_PER_RAD;
162 }
163 
164 /** Set speed given in rad/sec.
165  * Note that not the exact speed is taken, but rather the closes equivalent in
166  * motor ticks is taken.
167  * @param pan_speed desired pan speed in rad/sec
168  * @param tilt_speed desired tilt speed in rad/sec
169  * @exception OutOfBoundsException thrown if desired speed is out of range
170  */
171 void
172 SonyEviD100PVisca::set_speed_radsec(float pan_speed, float tilt_speed)
173 {
174  if ((pan_speed < 0) || (pan_speed > SPEED_TABLE_PAN[SONY_EVID100P_NUM_PAN_SPEEDS - 1])) {
175  throw OutOfBoundsException("Illegal pan speed",
176  pan_speed,
177  0,
178  SPEED_TABLE_PAN[SONY_EVID100P_NUM_PAN_SPEEDS - 1]);
179  }
180  if ((tilt_speed < 0) || (tilt_speed > SPEED_TABLE_TILT[SONY_EVID100P_NUM_TILT_SPEEDS - 1])) {
181  throw OutOfBoundsException("Illegal tilt speed",
182  tilt_speed,
183  0,
184  SPEED_TABLE_TILT[SONY_EVID100P_NUM_TILT_SPEEDS - 1]);
185  }
186 
187  unsigned int pan_ind = SONY_EVID100P_NUM_PAN_SPEEDS - 1;
188  float min_pan_dist = SPEED_TABLE_PAN[pan_ind];
189  float last_dist = min_pan_dist;
190  ;
191  for (unsigned int i = 0; i < SONY_EVID100P_NUM_PAN_SPEEDS; ++i) {
192  float dist = 0;
193  if ((dist = fabs(pan_speed - SPEED_TABLE_PAN[i])) < min_pan_dist) {
194  min_pan_dist = dist;
195  pan_ind = i;
196  } else if (dist > last_dist) {
197  break; // times are growing now, found best
198  }
199  last_dist = dist;
200  }
201 
202  unsigned int tilt_ind = SONY_EVID100P_NUM_TILT_SPEEDS - 1;
203  float min_tilt_dist = SPEED_TABLE_TILT[tilt_ind];
204  last_dist = min_tilt_dist;
205  for (unsigned int i = 0; i < SONY_EVID100P_NUM_TILT_SPEEDS; ++i) {
206  float dist = 0;
207  if ((dist = fabs(tilt_speed - SPEED_TABLE_TILT[i])) < min_tilt_dist) {
208  min_tilt_dist = dist;
209  tilt_ind = i;
210  } else if (dist > last_dist) {
211  break; // times are growing now, found best
212  }
213  last_dist = dist;
214  }
215 
216  set_pan_tilt_speed(pan_ind, tilt_ind);
217 }
218 
219 /** Get current speed in rad/sec.
220  * @param pan_speed upon return contains pan speed in rad/sec
221  * @param tilt_speed upon return contains tilt speed in rad/sec
222  */
223 void
224 SonyEviD100PVisca::get_speed_radsec(float &pan_speed, float &tilt_speed)
225 {
226  unsigned char ps, ts;
227  get_pan_tilt_speed(ps, ts);
228  pan_speed = SPEED_TABLE_PAN[ps - 1];
229  tilt_speed = SPEED_TABLE_TILT[ps - 1];
230 }
231 
232 /** Get speed limits.
233  * @param pan_min minimum pan speed possible
234  * @param pan_max maximum pan speed possible
235  * @param tilt_min minimum tilt speed possible
236  * @param tilt_max maximum tilt speed possible
237  */
238 void
240  float &pan_max,
241  float &tilt_min,
242  float &tilt_max)
243 {
244  pan_min = SPEED_TABLE_PAN[0];
245  pan_max = SPEED_TABLE_PAN[SONY_EVID100P_NUM_PAN_SPEEDS - 1];
246  tilt_min = SPEED_TABLE_TILT[0];
247  tilt_max = SPEED_TABLE_TILT[SONY_EVID100P_NUM_TILT_SPEEDS - 1];
248 }
static const unsigned int EFFECT_BW
B/W effect.
Definition: evid100p.h:72
static const unsigned int EFFECT_SEPIA
Sepia effect.
Definition: evid100p.h:71
static const float SPEED_TABLE_TILT[SONY_EVID100P_NUM_TILT_SPEEDS]
Speed table for supported tilt speed values in radians.
Definition: evid100p.h:79
~SonyEviD100PVisca()
Destructor.
Definition: evid100p.cpp:124
void get_pan_tilt_rad(float &pan, float &tilt)
Get pan/tilt in radians.
Definition: evid100p.cpp:155
static const float MIN_TILT_RAD
Min tilt in rad.
Definition: evid100p.h:61
static const float TILT_STEPS_PER_RAD
Tilt steps per rad.
Definition: evid100p.h:67
static const float PAN_STEPS_PER_RAD
Pan steps per rad.
Definition: evid100p.h:66
static const unsigned int EFFECT_STRETCH
Stretch effect.
Definition: evid100p.h:76
static const float MAX_PAN_DEG
Max pan in degrees.
Definition: evid100p.h:53
static const float TILT_STEPS_PER_DEG
Tilt steps per degree.
Definition: evid100p.h:64
static const unsigned int EFFECT_SOLARIZE
Solarize effect.
Definition: evid100p.h:73
static const unsigned int EFFECT_MOSAIC
Mosaic effect.
Definition: evid100p.h:74
SonyEviD100PVisca(const char *device_file, unsigned int def_timeout_ms=30, bool blocking=true)
Constructor.
Definition: evid100p.cpp:116
static const float SPEED_TABLE_PAN[SONY_EVID100P_NUM_PAN_SPEEDS]
Speed table for supported pan speed values in radians.
Definition: evid100p.h:78
static const float MAX_TILT_RAD
Max tilt in rad.
Definition: evid100p.h:60
static const float MIN_PAN_RAD
Min pan in rad.
Definition: evid100p.h:59
static const float MIN_PAN_DEG
Min pan in degrees.
Definition: evid100p.h:54
static const int MIN_TILT
Min tilt .
Definition: evid100p.h:51
void set_pan_tilt_rad(float pan, float tilt)
Set pan/tilt in radians.
Definition: evid100p.cpp:133
static const int MAX_TILT
Max Tilt.
Definition: evid100p.h:50
static const float MIN_TILT_DEG
Min tilt in degrees.
Definition: evid100p.h:56
void set_speed_radsec(float pan_speed, float tilt_speed)
Set speed given in rad/sec.
Definition: evid100p.cpp:172
void get_speed_limits(float &pan_min, float &pan_max, float &tilt_min, float &tilt_max)
Get speed limits.
Definition: evid100p.cpp:239
static const float MAX_PAN_RAD
Max pan in rad.
Definition: evid100p.h:58
static const float PAN_STEPS_PER_DEG
Pan steps per degree.
Definition: evid100p.h:63
void get_speed_radsec(float &pan_speed, float &tilt_speed)
Get current speed in rad/sec.
Definition: evid100p.cpp:224
static const int MIN_PAN
Minimum pan.
Definition: evid100p.h:49
static const unsigned int EFFECT_PASTEL
Pastel effect.
Definition: evid100p.h:69
static const int MAX_PAN
Maximum pan.
Definition: evid100p.h:48
static const unsigned int EFFECT_SLIM
Slim effect.
Definition: evid100p.h:75
static const unsigned int EFFECT_NEGATIVE
Negative effect.
Definition: evid100p.h:70
static const float MAX_TILT_DEG
Max tilt in degrees.
Definition: evid100p.h:55
Visca control protocol implementation over a serial line.
Definition: visca.h:51
void set_pan_tilt(int pan, int tilt)
Set pan tilt.
Definition: visca.cpp:610
void get_pan_tilt_speed(unsigned char &pan_speed, unsigned char &tilt_speed)
Get pan/tilt speed.
Definition: visca.cpp:684
void get_pan_tilt(int &pan, int &tilt)
Get pan and tilt values.
Definition: visca.cpp:720
void set_pan_tilt_speed(unsigned char pan_speed, unsigned char tilt_speed)
Set pan/tilt speed.
Definition: visca.cpp:666
Index out of bounds.
Definition: software.h:86
Fawkes library namespace.
float deg2rad(float deg)
Convert an angle given in degrees to radians.
Definition: angle.h:36