Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PanTiltSoftMotion.cpp
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 #include <Base/Common/BIASpragma.hh>
26 #include <PanTilt/DPPanTiltControl.hh>
27 #include <cmath>
28 #include "PanTiltSoftMotion.hh"
29 
30 using namespace std;
31 using namespace BIAS;
32 
33 PanTiltSoftMotion::PanTiltSoftMotion() {
34  tilt_active = false;
35  min_pan = -2000;
36  max_tilt = 2000;
37  min_tilt = -2000;
38  max_pan = 2000;
39  min_pan_speed = 50;
40  max_pan_speed = DPPTU_MAXSPEED / 8;
41  min_tilt_speed = 50;
42  max_tilt_speed = DPPTU_MAXSPEED / 16;
43 }
44 PanTiltSoftMotion::~PanTiltSoftMotion() {}
45 
46 void PanTiltSoftMotion::setLimits(int _min_pan, int _max_pan, int _min_pan_speed, int _max_pan_speed, int _min_tilt_speed, int _max_tilt_speed) {
47  min_pan = _min_pan;
48  max_pan = _max_pan;
49  min_pan_speed = _min_pan_speed;
50  max_pan_speed = _max_pan_speed;
51  min_tilt_speed = _min_tilt_speed;
52  max_tilt_speed = _max_tilt_speed;
53 }
54 
55 void PanTiltSoftMotion::assert_pan(int &pan) {
56  pan = max(min_pan, pan);
57  pan = min(max_pan, pan);
58 }
59 
60 void PanTiltSoftMotion::assert_tilt(int &tilt) {
61  tilt = max(min_tilt, tilt);
62  tilt = min(max_tilt, tilt);
63 }
64 
65 int PanTiltSoftMotion::calcSpeed(int start_val, int end_val, int cur_val, int damp) {
66  float diff_val = (float)(end_val - start_val);
67  //float val = (DPPTU_MAXSPEED / damp) * ((1+cos(2*M_PI*((float)cur_val - (diff_val/2+(float)start_val)) / diff_val)) / 2.0f);
68  float val = (DPPTU_MAXSPEED / damp) * cos ( ((2*((float)cur_val-(float)start_val)) / diff_val) -1 );
69  return (int) val;
70 }
71 
72 int PanTiltSoftMotion::getPanSpeed(int start_pan, int end_pan, int cur_pan, int damp) {
73  //assert_pan(start_pan);
74  //assert_pan(end_pan);
75  int panmod = 1;
76  if (end_pan < start_pan) {
77  panmod = -1;
78  if (cur_pan < end_pan)
79  return 0;
80  } else {
81  if (cur_pan > end_pan)
82  return 0;
83  }
84  int val = calcSpeed(start_pan, end_pan, cur_pan, damp);
85  val = max(min_pan_speed,val);
86  val = min(max_pan_speed,val);
87  val *= panmod;
88  return val;
89 }
90 
91 int PanTiltSoftMotion::getTiltSpeed(int start_pan, int end_pan, int start_tilt, int end_tilt, int cur_pan, int cur_tilt, int damp) {
92  //assert_tilt(start_tilt);
93  //assert_tilt(end_tilt);
94 
95  int tiltmod = 1;
96  if (end_tilt < start_tilt) {
97  tiltmod = -1;
98  if (cur_tilt < end_tilt)
99  return 0;
100  } else {
101  if (cur_tilt > end_tilt)
102  return 0;
103  }
104  int val = 0;
105  if (start_pan == 0 && end_pan == 0) {
106  val = calcSpeed(start_tilt, end_tilt, cur_tilt, damp);
107  val = max(min_tilt_speed,val);
108  val = min(max_tilt_speed,val);
109  val *= tiltmod;
110  return val;
111  }
112 
113  if (abs(cur_pan) > abs(start_pan) && abs(cur_pan) < abs(end_pan)) {
114  if ((start_tilt < end_tilt && cur_tilt >= end_tilt) || (start_tilt > end_tilt && cur_tilt <= end_tilt)) {
115  tilt_active = false;
116  } else {
117  tilt_active = true;
118  val = calcSpeed(start_tilt, end_tilt, cur_tilt, damp);
119  }
120  } else {
121  if (tilt_active) {
122  val = calcSpeed(start_tilt, end_tilt, cur_tilt, damp);
123  }
124  }
125  if (tilt_active) {
126  val = max(min_tilt_speed,val);
127  val = min(max_tilt_speed,val);
128  val *= tiltmod;
129  }
130  return val;
131 }