1 #include "DPPanTiltControlExtended.hh"
2 #include "Base/Common/W32Compat.hh"
3 #include "Base/Common/BIASpragma.hh"
9 #ifdef BIAS_HAVE_PTHREADS
11 pthread_mutex_t DPPanTiltControlExtended::condition_mutex = PTHREAD_MUTEX_INITIALIZER;
12 long DPPanTiltControlExtended::panThreaded_ = 0;
13 long DPPanTiltControlExtended::tiltThreaded_ = 0;
17 DPPanTiltControlExtended::DPPanTiltControlExtended()
20 xApertureAngle_ = 32.97;
21 yApertureAngle_ = 25.03;
24 followObjectMaxDiff_ = 400;
25 followObjectMinDiff_ = 30;
30 xPTDeltaSpeedTMinus1_ = 0;
31 yPTDeltaSpeedTMinus1_ = 0;
38 ZiNiErrorTminus1X_ = 0;
39 ZiNiErrorTminus1Y_ = 0;
42 ptuMatrix_.SetZYX(0,0,0);
43 deltaMatrix_.SetZYX(0,0,0);
44 sensorMatrixMinus1_.SetZYX(0,0,0);
45 handEyeMatrix_.SetZYX(0.0, 0.0, 0.0);
51 DPPanTiltControlExtended::~DPPanTiltControlExtended()
79 void DPPanTiltControlExtended::
89 GetCurrentPosition(pan, tilt);
92 anglesPTU[2] = ((double)pan * panResolution_)* M_PI/180.0;
93 anglesPTU[1] = ((double)tilt * tiltResolution_)* M_PI/180.0;
96 ptuMatrix_.SetZYX(anglesPTU[0],anglesPTU[1],anglesPTU[2]);
97 ptuMatrix_.GetRotationAnglesZYX(anglesPTU[0],anglesPTU[1],anglesPTU[2]);
101 deltaMatrix_ = R * sensorMatrixMinus1_.
Transpose();
103 sensorMatrixMinus1_.GetRotationAnglesZYX(anglesPTU[0],anglesPTU[1],anglesPTU[2]);
105 deltaMatrix_.GetRotationAnglesZYX(anglesPTU[0],anglesPTU[1],anglesPTU[2]);
107 compMatrix = ptuMatrix_ * handEyeMatrix_ * deltaMatrix_ *
112 steps[0] = (int)(anglesPTU[2] * 180.0/M_PI / panResolution_);
113 steps[1] = (int)(anglesPTU[1] * 180.0/M_PI / tiltResolution_);
115 panSpeedComp_ = (
short int)(steps[0]/T_*1e6);
116 tiltSpeedComp_ =(
short int)(steps[1]/T_*1e6);
117 if (!bDefaultOrientation_) {
118 panSpeedComp_ = -panSpeedComp_;
119 tiltSpeedComp_ = tiltSpeedComp_;
121 panSpeedComp_ = panSpeedComp_;
122 tiltSpeedComp_ = tiltSpeedComp_;
129 sensorMatrixMinus1_ = R;
132 #ifdef BIAS_HAVE_PTHREADS
134 void DPPanTiltControlExtended::StartCircularMovementThread()
136 pthread_create(&control_thread, NULL, this->threadFuncCircle_,
this);
139 void DPPanTiltControlExtended::
140 GetCurrentPositionDegreesThreaded(
double &dPanDeg,
double& dTiltDeg)
142 pthread_mutex_lock(&condition_mutex);
143 dPanDeg = panThreaded_ * panResolution_;
144 dTiltDeg = tiltThreaded_ * tiltResolution_;
145 pthread_mutex_unlock(&condition_mutex);
148 void DPPanTiltControlExtended::
149 GetCurrentPositionRadiansThreaded(
double &dPanRad,
double& dTiltRad)
151 pthread_mutex_lock(&condition_mutex);
152 dPanRad = panThreaded_ * panResolution_ * M_PI / 180.0;
153 dTiltRad = tiltThreaded_ * tiltResolution_ * M_PI / 180.0;
154 pthread_mutex_unlock(&condition_mutex);
157 void* DPPanTiltControlExtended::threadFuncCircle_(
void *ptr)
167 pthread_mutex_lock(&condition_mutex);
169 pthread_mutex_unlock(&condition_mutex);
170 while(panThreaded_ != 0 && tiltThreaded_ != 200){
172 pthread_mutex_lock(&condition_mutex);
174 pthread_mutex_unlock(&condition_mutex);
178 while(panThreaded_ != 200 && tiltThreaded_ != 200){
180 pthread_mutex_lock(&condition_mutex);
182 pthread_mutex_unlock(&condition_mutex);
186 while(panThreaded_ != 200 && tiltThreaded_ != -200){
188 pthread_mutex_lock(&condition_mutex);
190 pthread_mutex_unlock(&condition_mutex);
194 while(panThreaded_ != -200 && tiltThreaded_ != -200){
196 pthread_mutex_lock(&condition_mutex);
198 pthread_mutex_unlock(&condition_mutex);
202 while(panThreaded_ != -200 && tiltThreaded_ != 200){
204 pthread_mutex_lock(&condition_mutex);
206 pthread_mutex_unlock(&condition_mutex);
210 while(panThreaded_ != 0 && tiltThreaded_ != 200){
212 pthread_mutex_lock(&condition_mutex);
214 pthread_mutex_unlock(&condition_mutex);
218 while(panThreaded_ != 0 && tiltThreaded_ != 0){
220 pthread_mutex_lock(&condition_mutex);
222 pthread_mutex_unlock(&condition_mutex);
227 #endif // BIAS_HAVE_PTHREADS
229 void DPPanTiltControlExtended::
234 d[0] = -((int)xPictureSize_/2)+(int)newMean[0];
235 d[1] = -((int)yPictureSize_/2)+(int)newMean[1];
239 beta[0] = ((float)d[0]/(
float)xPictureSize_) * xApertureAngle_;
240 beta[1] = ((float)d[1]/(
float)yPictureSize_) * yApertureAngle_;
243 int stepsPerDegreePan = (int)(1.0/panResolution_);
244 int stepsPerDegreeTilt = (int)(1.0/tiltResolution_);
246 steps[0] = (int)(beta[0] * stepsPerDegreePan);
247 steps[1] = (int)(beta[1] * stepsPerDegreeTilt);
250 if (bDefaultOrientation_) {
251 steps[0] = -steps[0];
256 if (steps[0] > followObjectMinDiff_ && steps[0] < followObjectMaxDiff_)
258 set_desired(PAN, POSITION, (PTU_PARM_PTR *) &steps[0],RELATIVE);
262 if (steps[1] > followObjectMinDiff_ && steps[1] < followObjectMaxDiff_)
264 set_desired(TILT, POSITION, (PTU_PARM_PTR *) &steps[1],RELATIVE);
270 void DPPanTiltControlExtended::
275 error[0] = -((double)xPictureSize_/2)+newMean[0];
276 error[1] = -((double)yPictureSize_/2)+newMean[1];
278 ZiNiErrorX_+=(int)error[0];
279 ZiNiErrorY_+=(int)error[1];
281 double maxSpeedScaleX = (error[0]/xPictureSize_);
282 double maxSpeedScaleY = (error[1]/yPictureSize_);
285 xPTSpeed_ = (
short int)(maxSpeed_ * maxSpeedScaleX
287 yPTSpeed_ = (
short int)(maxSpeed_ * maxSpeedScaleY
291 xPTSpeed_ += (
short int)KiX_ * ZiNiErrorX_;
292 yPTSpeed_ += (
short int)KiY_ * ZiNiErrorY_;
295 xPTSpeed_ += (
short int)(KdX_ * (error[0]- ZiNiErrorTminus1X_));
296 yPTSpeed_ += (
short int)(KdY_ * (error[1]- ZiNiErrorTminus1Y_));
299 ZiNiErrorTminus1X_=(int)error[0];
300 ZiNiErrorTminus1Y_=(int)error[1];
303 if(bDefaultOrientation_) {
304 xPTSpeed_ = -xPTSpeed_;
309 xPTSpeed_ += panSpeedComp_;
310 yPTSpeed_ += tiltSpeedComp_;
313 set_desired(PAN, SPEED, (PTU_PARM_PTR *) &xPTSpeed_, ABSOLUTE);
314 set_desired(TILT, SPEED, (PTU_PARM_PTR *) &yPTSpeed_, ABSOLUTE);
316 cout <<
" DPPanTiltControlExtended : Set speed to " << xPTSpeed_ <<
", " << yPTSpeed_ << endl;
319 void DPPanTiltControlExtended::
324 error[0] = (int)(newMean[0]-xPictureSize_/2);
325 error[1] = (int)(newMean[1]-yPictureSize_/2);
331 xPTDeltaSpeed_=(
short int)(KpX_*(1+T_/(2*Tn_))*error[0]);
332 yPTDeltaSpeed_=(
short int)(KpY_*(1+T_/(2*Tn_))*error[1]);
335 if(bDefaultOrientation_)
336 xPTDeltaSpeed_ = -xPTDeltaSpeed_;
338 yPTDeltaSpeed_ = -yPTDeltaSpeed_;
341 set_desired(PAN,SPEED,(PTU_PARM_PTR *) &xPTDeltaSpeed_, RELATIVE);
342 set_desired(TILT,SPEED,(PTU_PARM_PTR *) &yPTDeltaSpeed_, RELATIVE);
345 xPTDeltaSpeedTMinus1_ = xPTDeltaSpeed_;
346 yPTDeltaSpeedTMinus1_ = yPTDeltaSpeed_;
349 ErrorTMinus1_.push_back(error[0]);
350 ErrorTMinus1_.push_back(error[1]);
357 xPTDeltaSpeed_ = (
short int)(KpX_*(1+(T_/(2*Tn_)))
358 *error[0]+KpX_*(-1+(T_/(2*Tn_)))*ErrorTMinus1_[0]
359 +0.1*xPTDeltaSpeedTMinus1_);
361 yPTDeltaSpeed_ = (
short int)(KpY_*(1+(T_/(2*Tn_)))
362 *error[1]+KpY_*(-1+(T_/(2*Tn_)))*ErrorTMinus1_[1]
363 +yPTDeltaSpeedTMinus1_);
367 if(bDefaultOrientation_)
368 xPTDeltaSpeed_ = -xPTDeltaSpeed_;
370 yPTDeltaSpeed_ = -yPTDeltaSpeed_;
373 set_desired(PAN,SPEED,(PTU_PARM_PTR *) &xPTDeltaSpeed_, RELATIVE);
374 set_desired(TILT,SPEED,(PTU_PARM_PTR *) &yPTDeltaSpeed_, RELATIVE);
377 xPTDeltaSpeedTMinus1_ = xPTDeltaSpeed_;
378 yPTDeltaSpeedTMinus1_ = yPTDeltaSpeed_;
380 ErrorTMinus1_[0] = error[0];
381 ErrorTMinus1_[1] = error[1];
int GetRotationAnglesZYX(double &PhiX, double &PhiY, double &PhiZ) const
Get Euler angles for this rotation matrix in order ZYX.
char SetSpeed(int nPanSpeed, int nTiltSpeed)
Set speed for movement.
Extended controller interface for Directed Perception Pan Tilt Unit.
void GetCurrentPosition(long &lPanPos, long &lTiltPos)
void SetPosition(int nPanPos, int nTiltPos, bool bWaitComplete=true)
Set the current position of the PTU in steps.
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
int SetMode(int nMode)
Set the mode in which the camera is to operate.