RIG InMoov Project
servo.ino
Go to the documentation of this file.
1 
16 #ifndef SIMULATION
17 // allow simulations to define their own servo libraries
18 #include <Servo.h>
19 // assume that simulation has its own callibration
20 #include "settings.h"
21 #else
22 // prototyping is only required if not using arduino
23 #include <servo.h>
24 #endif
25 
26 #include <stdint.h>
27 
33 Servo servo [SERVOS];
34 
39 uint8_t current_pos [SERVOS] = { 0 };
40 
42 char buf [BUFSIZE];
43 
44 enum
45 {
46  MIN_LIM = 0,
47  MAX_LIM = 1,
48  CANCEL_SIGNAL = 255,
49  WAIT_RESPONSE = 254,
50  DUMP_SIGNAL = 253,
51  START_RESPONSE = 252,
52  END_RESPONSE = 251,
55 };
56 
61 void setup ()
62 {
63  int angle, pin;
64 
65  Serial.begin(SERIAL_BAUDRATE);
66 
67  serialPrint ("starting...\n");
68  for (uint8_t servo_index = 0; servo_index < SERVOS; servo_index++)
69  {
70 // simulations don't have pins
71 #ifndef SIMULATION
72  serialPrintIntPretty ("assinging servo: ", servo_index, "\n");
73  pin = getServoPinFromIndex (servo_index);
74  if (pin < 0)
75  {
76  serialPrint ("invalid servo index.\n");
77  }
78  else
79  {
80  serialPrintIntPretty ("pin: ", pin, "\n");
81  servo [servo_index].attach (pin);
82  pinMode (pin, OUTPUT);
83  }
84 #endif
85 
86  serialPrintIntPretty ("default: ", default_pos [servo_index], "\n");
87  setServoFromIndex (servo_index, default_pos [servo_index]);
88  }
89 }
90 
100 void loop ()
101 {
102  static int servo_id, servo_angle;
103 
104  // indicate that we are waiting for input
105  if (! Serial.available ())
106  {
107  serialPrint ("WAIT\n");
108  Serial.write (WAIT_RESPONSE);
109  }
110 
111  servo_id = serialGetByte ();
112  // WAIT_SIGNAL recieved
113  if (servo_id < 0)
114  {
115  return;
116  }
117  if (servo_id == DUMP_SIGNAL)
118  {
119  dump ();
120  return;
121  }
122  serialPrintIntPretty ("recieved servo id: ", servo_id, "\n");
123 
124  servo_angle = serialGetByte ();
125  if (servo_angle < 0)
126  {
127  return;
128  }
129  if (servo_angle == DUMP_SIGNAL)
130  {
131  dump ();
132  return;
133  }
134  serialPrintIntPretty ("recieved servo value: ", servo_angle, "\n");
135 
136  setServoFromID (servo_id, servo_angle);
137 }
138 
142 void serialPrint (const char* s)
143 {
144  if (VERBOSE)
145  {
146  Serial.print (s);
147  }
148 }
149 
153 void serialPrintInt (uint8_t i)
154 {
155  if (VERBOSE)
156  {
157  Serial.print (i, DEC);
158  }
159 }
160 
165 void serialPrintIntPretty (const char* pre, uint8_t i, const char* post)
166 {
167  if (VERBOSE)
168  {
169  Serial.write (pre);
170  serialPrintInt (i);
171  Serial.write (post);
172  }
173 }
174 
181 {
182  int i;
183 
184  serialWait ();
185  i = Serial.read ();
186  if (i == CANCEL_SIGNAL)
187  {
188  serialPrint ("CANCEL_SIGNAL\n");
189  return -1;
190  }
191 
192  return i;
193 }
194 
205 void setServoFromIndex (uint8_t servo_index, uint8_t servo_angle)
206 {
207  if (servo_index >= SERVOS || servo_index < 0)
208  {
209  serialPrint ("servo index out of range.\n");
210  return;
211  }
212 
213  if (servo_angle > 180)
214  {
215  servo_angle = 180;
216  }
217  if (servo_angle < 0)
218  {
219  servo_angle = 0;
220  }
221 
222  current_pos [servo_index] = servo_angle;
223 
224  if (reverse [servo_index])
225  {
226  servo_angle = map (servo_angle, 180, 0,
227  limit [servo_index] [MIN_LIM], limit [servo_index] [MAX_LIM]);
228  }
229  else
230  {
231  servo_angle = map (servo_angle, 0, 180,
232  limit [servo_index] [MIN_LIM], limit [servo_index] [MAX_LIM]);
233  }
234 
235  serialPrintIntPretty ("readjusted value: ", servo_angle, "\n");
236 
237  servo [servo_index].write (servo_angle);
238 }
239 
244 void setServoFromID (int servo_id, uint8_t servo_angle)
245 {
246  int servo_index;
247 
248  servo_index = getServoIndexFromID (servo_id);
249  if (servo_index < 0)
250  {
251  serialPrint ("servo ID not valid.\n");
252  return;
253  }
254 
255  setServoFromIndex (servo_index, servo_angle);
256 }
257 
265 void dump ()
266 {
267  int i = 0;
268 
269  // if you change this function, please verify DUMP_RESPONSE_LEN
271  {
272  return;
273  }
274 
275  buf [i++] = START_RESPONSE;
276  buf [i++] = BOARD_ID;
277  buf [i++] = SERVOS;
278  for (int servo_index = 0; servo_index < SERVOS; servo_index++)
279  {
280  buf [i++] = getServoIDFromIndex (servo_index);
281  buf [i++] = current_pos [servo_index];
282  }
283  buf [i++] = END_RESPONSE;
284 
285  Serial.write (buf, i);
286 }
287 
291 void serialWait ()
292 {
293  while (!Serial.available ());
294 }
uint8_t reverse[SERVOS]
Definition: servo_rhand.h:47
int serialGetByte()
Definition: servo.ino:180
The length of the response to the dump signal.
Definition: servo.ino:54
void serialPrint(const char *s)
Definition: servo.ino:142
uint8_t current_pos[SERVOS]
Definition: servo.ino:39
int getServoIDFromIndex(uint8_t servo_index)
Definition: servo_rhand.h:87
void dump()
Definition: servo.ino:265
void serialPrintInt(uint8_t i)
Definition: servo.ino:153
#define SERVOS
Definition: servo_rhand.h:14
void serialWait()
Definition: servo.ino:291
void serialPrintIntPretty(const char *pre, uint8_t i, const char *post)
Definition: servo.ino:165
Whether to print debug output to the serial port.
Definition: servo_rhand.h:57
void setup()
Definition: servo.ino:61
void loop()
Definition: servo.ino:100
uint8_t default_pos[SERVOS]
Definition: servo_rhand.h:42
The unique identification for this board.
Definition: servo_rhand.h:59
void setServoFromIndex(uint8_t servo_index, uint8_t servo_angle)
Definition: servo.ino:205
Servo servo[SERVOS]
Definition: servo.ino:33
char buf[BUFSIZE]
The print buffer.
Definition: servo.ino:42
int getServoIndexFromID(uint8_t servo_id)
Definition: servo_rhand.h:74
uint8_t limit[SERVOS][2]
Definition: servo_rhand.h:34
#define BUFSIZE
Definition: servo_rhand.h:18
int getServoPinFromIndex(uint8_t servo_index)
Definition: servo_rhand.h:100
void setServoFromID(int servo_id, uint8_t servo_angle)
Definition: servo.ino:244