direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
servo.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #include "servo.h"
22 
23 Servo::Servo(InterfaceAvr *i, QMutex *m)
24 {
25  // get the name of this class (this is for debugging messages)
26  className = this->staticMetaObject.className();
27 
28  // copy the pointer from the original object
29  interface1 = i;
30  mutex = m;
31 
32  // init arrays
33  for (int servo=0; servo<NUMBEROFSERVOS; servo++)
34  {
35  servoStartPosition[servo] = 0;
36  servoMinPosition[servo] = 0;
37  servoDefaultPosition[servo] = 64;
38  servoEndPosition[servo] = 255;
39  servoMaxPosition[servo] = 255;
40  servoPosition[servo] = servoDefaultPosition[servo];
41  }
42 
43  robotState = ON; // Wer're thinking positive. The robot is ON until we know nothing other. :-)
44 }
45 
46 
48 {
49 }
50 
51 
52 bool Servo::moveServo(unsigned char servo, unsigned char position)
53 {
54  QString answer = "error";
55 
56 
57  if (robotState == ON)
58  {
59  // servo number okay?
60  if (servo > NUMBEROFSERVOS-1)
61  {
62  emit message(QString("<font color=\"#FF0000\">Servo%1 is not an allowed servo numer (1-%2)! (moveServo)</font>").arg(servo+1).arg(NUMBEROFSERVOS-1) );
63  return false;
64  }
65 
66 
67  // wanted servo position okay?
68  if ( (position < servoMinPosition[servo]) || (position > servoMaxPosition[servo]) )
69  {
70  emit message(QString("<font color=\"#FF0000\">Servo%1 position %2 out of allowed range (%3-%4)! (moveServo)</font>").arg(servo+1).arg(position).arg(servoStartPosition[servo]).arg(servoEndPosition[servo]));
71  return false;
72  }
73 
74 
75  // store the new servo position
76  servoPosition[servo] = position;
77 
78  // Lock the mutex.
79  mutex->lock();
80 
81 
82  // move servo
83  // send command to microcontroller
84  if (interface1->sendString(QString("*sv%1%2#").arg(servo + 1).arg(position), className) == true)
85  {
86  // check if the robot answers with "ok"
87  if ( interface1->receiveString(answer, className) == true)
88  {
89  if (answer == QString("*sv%1#").arg(servo + 1))
90  {
91  // Unlock the mutex
92  mutex->unlock();
93  return true;
94  }
95  }
96  }
97 
98 
99  // Unlock the mutex.
100  mutex->unlock();
101 
102  emit message(QString("<font color=\"#FF0000\">Error moving servo%1 (moveServo)</font>").arg(servo+1));
103  return false;
104 
105  } // robot is ON
106 
107  // robot is OFF
108  return false;
109 }
110 
111 
112 void Servo::setServoPosition(int servo, unsigned char type, unsigned char position)
113 {
114  switch (type)
115  {
116  case SVSTART:
117  servoStartPosition[servo] = position;
118  return;
119  break;
120  case SVEND:
121  servoEndPosition[servo] = position;
122  return;
123  break;
124  case SVMIN:
125  servoMinPosition[servo] = position;
126  return;
127  break;
128  case SVMAX:
129  servoMaxPosition[servo] = position;
130  return;
131  break;
132  case SVDEFAULT:
133  servoDefaultPosition[servo] = position;
134  return;
135  break;
136  case SVCURRENT:
137  // converting SERVO5 value, because of reverse fixing on the robot!
138  if (servo==SERVO5)
139  {
140  }
141 
142  // only within the allowd ranges!
143  if (position < servoMinPosition[servo])
144  {
145  position = servoMinPosition[servo];
146  }
147  if (position > servoMaxPosition[servo])
148  {
149  position = servoMaxPosition[servo];
150  }
151  servoPosition[servo] = position;
152  return;
153  break;
154  }
155 }
156 
157 
158 void Servo::init(void)
159 {
160  if (robotState == ON)
161  {
162  for (int servo=0; servo<NUMBEROFSERVOS; servo++)
163  {
164  moveServo(servo, servoDefaultPosition[servo]);
165  //emit message(QString("Init servo%1 to def-pos: %2").arg(servo+1).arg(servoDefaultPosition[servo]));
166  }
167  } // robot is ON
168 }
169 
170 
171 unsigned char Servo::getServoPosition(int servo, unsigned char type)
172 {
173  if ( (servo < SERVO1) || (servo > (NUMBEROFSERVOS-1)) )
174  {
175  emit message(QString("<font color=\"#FF0000\">Servo%1 out of allowed range! (getServoPosition)</font>").arg(servo+1));
176  return 0;
177  }
178 
179 
180  switch (type)
181  {
182  case SVSTART:
183  return servoStartPosition[servo];
184  break;
185  case SVEND:
186  return servoEndPosition[servo];
187  break;
188  case SVMIN:
189  return servoMinPosition[servo];
190  break;
191  case SVMAX:
192  return servoMaxPosition[servo];
193  break;
194  case SVDEFAULT:
195  return servoDefaultPosition[servo];
196  break;
197  case SVCURRENT:
198  return servoPosition[servo];
199  break;
200  }
201 
202  // this line is never reached
203  return 0;
204 }
205 
206 
207 void Servo::setRobotState(bool state)
208 {
209  // store the state within this class
210  robotState = state;
211 
212  emit message(QString("Robot state set to %1 in %2").arg(state).arg(className));
213 }