YARP
Yet Another Robot Platform
SerialServoBoard.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2008 Giacomo Spigler
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #include "SerialServoBoard.h"
11 
12 #include <yarp/os/LogComponent.h>
13 
15 #include <yarp/dev/Drivers.h>
16 #include <yarp/dev/ISerialDevice.h>
17 #include <yarp/dev/PolyDriver.h>
18 
19 #include <cstdio>
20 #include <cstdlib>
21 
22 
23 //TODO: check limits of operation (range of angles)?
24 
25 
26 using namespace yarp::os;
27 using namespace yarp::dev;
28 
29 namespace {
30 YARP_LOG_COMPONENT(SERIALSERVOBOARD, "yarp.devices.SerialServoBoard")
31 }
32 
33 
35 {
36  if (config.check("help") == true) {
37  yCInfo(SERIALSERVOBOARD, "SerialServoBoard Available Options:");
38  yCInfo(SERIALSERVOBOARD, " -board NAME, where name is one of ssc32, minissc, pontech_sv203x, mondotronic_smi, parallax, pololu_usb_16servo, picopic");
39  yCInfo(SERIALSERVOBOARD, " -comport NAME, where name is COMx on Windows, and /dev/ttySx on Linux");
40  yCInfo(SERIALSERVOBOARD, " -baudrate RATE, where RATE is the Board baudrate");
41  yCInfo(SERIALSERVOBOARD, " -help shows this help");
42 
43  return false;
44  }
45 
46 
47  char servoboard_[80];
48 
49  strcpy(servoboard_, config.check("board", yarp::os::Value("ssc32")).asString().c_str());
50 
51  if (strcmp(servoboard_, "ssc32") == 0) {
52  servoboard = SSC32;
53  move = &movessc32;
54  } else if (strcmp(servoboard_, "minissc") == 0) {
55  servoboard = MINISSC;
56  move = &moveminissc;
57  } else if (strcmp(servoboard_, "pontech_sv203x") == 0) {
58  servoboard = PONTECHSV203X;
59  move = &movepontech;
60  } else if (strcmp(servoboard_, "mondotronic_smi") == 0) {
61  servoboard = MONDOTRONICSMI;
62  move = &movemondotronic;
63  } else if (strcmp(servoboard_, "pololu_usb_16servo") == 0) {
64  servoboard = POLOLUUSB16;
65  move = &movepololu;
66  } else if (strcmp(servoboard_, "picopic") == 0) {
67  servoboard = PICOPIC;
68  move = &movepicopic;
69  }
70 
71 
72  char comport[80];
73 
74  strcpy(comport, config.check("comport", yarp::os::Value("/dev/ttyS0")).asString().c_str());
75 
76  int baudrate = config.check("baudrate", yarp::os::Value(38400)).asInt32();
77 
78  Property conf;
79  // no arguments, use a default
80  conf.put("device", "serialport");
81  conf.put("comport", comport);
82  conf.put("baudrate", baudrate);
83  conf.put("rcvenb", 1);
84  conf.put("stopbits", 2);
85  conf.put("databits", 8);
86  conf.put("paritymode", "none");
87 
88  dd.open(conf);
89  if (!dd.isValid()) {
90  yCError(SERIALSERVOBOARD, "Failed to create and configure a device");
91  std::exit(1);
92  }
93 
94  if (!dd.view(serial)) {
95  yCError(SERIALSERVOBOARD, "Failed to view device through ISerialDevice interface");
96  std::exit(1);
97  }
98 
99 
100  positions = (double*)malloc(sizeof(double) * 32);
101  speeds = (double*)malloc(sizeof(double) * 32);
102 
103  return true;
104 }
105 
107 {
108  dd.close();
109 
110  free(positions);
111  free(speeds);
112 
113  return true;
114 }
115 
116 
118 {
119  if (servoboard == SSC32) {
120  *ax = 32;
121  } else if (servoboard == MINISSC) {
122  *ax = 8;
123  } else if (servoboard == PONTECHSV203X) {
124  *ax = 8;
125  } else if (servoboard == MONDOTRONICSMI) {
126  *ax = 2;
127  } else if (servoboard == POLOLUUSB16) {
128  *ax = 16;
129  } else if (servoboard == PICOPIC) {
130  *ax = 20;
131  }
132 
133  return true;
134 }
135 
136 
137 bool SerialServoBoard::positionMove(int j, double ref)
138 {
139  positions[j] = ref;
140 
141  return move(j, ref, positions, speeds, serial);
142 }
143 
144 
145 bool SerialServoBoard::positionMove(const double* refs)
146 {
147  for (int k = 0; k < 32; k++) {
148  this->positionMove(k, refs[k]);
149  }
150 
151  return true;
152 }
153 
154 
155 bool SerialServoBoard::relativeMove(int j, double delta)
156 {
157  this->positionMove(j, positions[j] + delta);
158 
159  return true;
160 }
161 
162 
163 bool SerialServoBoard::relativeMove(const double* deltas)
164 {
165  for (int k = 0; k < 32; k++) {
166  this->positionMove(k, positions[k] + deltas[k]);
167  }
168 
169  return true;
170 }
171 
172 
173 bool SerialServoBoard::checkMotionDone(int j, bool* flag)
174 {
175  //TODO: Q?
176 
177  return true;
178 }
179 
180 
182 {
183  //TODO: Q?
184 
185  return true;
186 }
187 
188 
189 bool SerialServoBoard::setRefSpeed(int j, double sp)
190 {
191  speeds[j] = sp;
192 
193  return true;
194 }
195 
196 
197 bool SerialServoBoard::setRefSpeeds(const double* spds)
198 {
199  for (int k = 0; k < 32; k++) {
200  setRefSpeed(k, spds[k]);
201  }
202 
203  return true;
204 }
205 
206 
208 {
209  return true;
210 }
211 
212 
214 {
215  return true;
216 }
217 
218 
219 bool SerialServoBoard::getRefSpeed(int j, double* ref)
220 {
221  *ref = speeds[j];
222 
223  return true;
224 }
225 
226 
228 {
229  for (int k = 0; k < 32; k++) {
230  spds[k] = speeds[k];
231  }
232 
233  return true;
234 }
235 
236 
237 bool SerialServoBoard::getRefAcceleration(int j, double* acc)
238 {
239  return true;
240 }
241 
242 
244 {
245  return true;
246 }
247 
248 
250 {
251  return true;
252 }
253 
254 
256 {
257  return true;
258 }
259 
260 bool SerialServoBoard::positionMove(const int n_joint, const int* joints, const double* refs)
261 {
262  return true;
263 }
264 bool SerialServoBoard::relativeMove(const int n_joint, const int* joints, const double* deltas)
265 {
266  return true;
267 }
268 bool SerialServoBoard::checkMotionDone(const int n_joint, const int* joints, bool* flags)
269 {
270  return true;
271 }
272 bool SerialServoBoard::setRefSpeeds(const int n_joint, const int* joints, const double* spds)
273 {
274  return true;
275 }
276 bool SerialServoBoard::setRefAccelerations(const int n_joint, const int* joints, const double* accs)
277 {
278  return true;
279 }
280 bool SerialServoBoard::getRefSpeeds(const int n_joint, const int* joints, double* spds)
281 {
282  return true;
283 }
284 bool SerialServoBoard::getRefAccelerations(const int n_joint, const int* joints, double* accs)
285 {
286  return true;
287 }
288 bool SerialServoBoard::stop(const int n_joint, const int* joints)
289 {
290  return true;
291 }
292 
293 
294 bool movessc32(int j, double ref, double* positions, double* speeds, ISerialDevice* serial)
295 {
296  int pos = 1500 + round(positions[j] * 11.11);
297 
298  Bottle bot;
299  char str[80];
300  if (FABS(speeds[j]) < 0.1) {
301  std::snprintf(str, 80, "#%dP%d\r", j, pos);
302  } else {
303  int speed = round(speeds[j] * 11.11);
304 
305  std::snprintf(str, 80, "#%dP%dS%d\r", j, pos, speed);
306  }
307  //if(j==0) {
308  bot.addString(str);
309  serial->send(bot);
310  //} else {
311  // serial->send(str, 2+strlen(str+3));
312  //}
313 
314  return true;
315 }
316 
317 
318 bool moveminissc(int j, double ref, double* positions, double* speeds, ISerialDevice* serial)
319 {
320  auto pos = (unsigned char)((int)(positions[j] * 1.411) + 127);
321 
322  char cmd[3];
323 
324  //ignore speed;
325  cmd[0] = 255; //sync byte
326  cmd[1] = (unsigned char)j; //servo number byte
327  cmd[2] = pos; //position byte
328 
329  serial->send(cmd, 3);
330 
331  return true;
332 }
333 
334 
335 bool movepontech(int j, double ref, double* positions, double* speeds, ISerialDevice* serial)
336 {
337  auto pos = (unsigned char)((int)(positions[j] * 1.411) + 127);
338 
339  Bottle bot;
340  char str[80];
341  std::snprintf(str, 80, "BD1SV%dM%d", j + 1, pos);
342 
343  bot.addString(str);
344 
345  serial->send(bot);
346 
347  return true;
348 }
349 
350 
351 bool movemondotronic(int j, double ref, double* positions, double* speeds, ISerialDevice* serial)
352 {
353  auto pos = (unsigned char)((int)(positions[j] * 1.411) + 127);
354 
355  char cmd[3];
356 
357  //ignore speed;
358  cmd[0] = 255; //sync byte
359  cmd[1] = (unsigned char)j; //servo number byte
360  cmd[2] = pos; //position byte (speed. this board controls speed for dc motors)
361 
362  serial->send(cmd, 3);
363 
364  return true;
365 }
366 
367 
368 bool movepololu(int j, double ref, double* positions, double* speeds, ISerialDevice* serial)
369 {
370  int pos = 1500 + round(positions[j] * 11.11);
371 
372  char cmd[6];
373 
374  cmd[0] = 0x80;
375  cmd[1] = 0x01;
376 
377  cmd[2] = 0x04;
378  cmd[3] = (unsigned char)j;
379 
380  cmd[4] = (unsigned char)(pos >> 8); //high pos byte
381  cmd[5] = (unsigned char)pos; //low pos byte
382 
383  serial->send(cmd, 6);
384 
385  return true;
386 }
387 
388 
389 bool movepicopic(int j, double ref, double* positions, double* speeds, ISerialDevice* serial)
390 {
391  int pos = 1500 + round(positions[j] * 11.11);
392 
393  char cmd[5];
394 
395  cmd[0] = (int)(j / 20) + 1; //board address
396  cmd[1] = (j + 1); //servo number (1-....)
397 
398  cmd[2] = (unsigned char)(pos >> 8); //high pos byte
399  cmd[3] = (unsigned char)pos; //low pos byte
400 
401  if (FABS(speeds[j]) < 0.1) {
402  cmd[4] = 255; //speed
403  } else {
404  auto speed = (unsigned char)((int)(speeds[j] * 1.411) + 127);
405 
406  cmd[4] = speed; //speed
407  }
408 
409  serial->send(cmd, 5);
410 
411  return true;
412 }
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
PONTECHSV203X
#define PONTECHSV203X
Definition: SerialServoBoard.h:24
Drivers.h
MINISSC
#define MINISSC
Definition: SerialServoBoard.h:23
MONDOTRONICSMI
#define MONDOTRONICSMI
Definition: SerialServoBoard.h:25
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
SerialServoBoard::getRefAcceleration
bool getRefAcceleration(int j, double *acc) override
Get reference acceleration for a joint.
Definition: SerialServoBoard.cpp:237
SerialServoBoard::setRefSpeeds
bool setRefSpeeds(const double *spds) override
Set reference speed on all joints.
Definition: SerialServoBoard.cpp:197
movessc32
bool movessc32(int j, double ref, double *positions, double *speeds, ISerialDevice *serial)
Definition: SerialServoBoard.cpp:294
yarp::dev::ISerialDevice
A generic interface to serial port devices.
Definition: ISerialDevice.h:28
movepololu
bool movepololu(int j, double ref, double *positions, double *speeds, ISerialDevice *serial)
Definition: SerialServoBoard.cpp:368
ControlBoardInterfaces.h
define control board standard interfaces
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
SerialServoBoard::getRefAccelerations
bool getRefAccelerations(double *accs) override
Get reference acceleration of all joints.
Definition: SerialServoBoard.cpp:243
movepicopic
bool movepicopic(int j, double ref, double *positions, double *speeds, ISerialDevice *serial)
Definition: SerialServoBoard.cpp:389
FABS
#define FABS(x)
Definition: SerialServoBoard.h:20
SerialServoBoard::checkMotionDone
bool checkMotionDone(int j, bool *flag) override
Check if the current trajectory is terminated.
Definition: SerialServoBoard.cpp:173
SerialServoBoard::getAxes
bool getAxes(int *ax) override
Get the number of controlled axes.
Definition: SerialServoBoard.cpp:117
SerialServoBoard.h
movemondotronic
bool movemondotronic(int j, double ref, double *positions, double *speeds, ISerialDevice *serial)
Definition: SerialServoBoard.cpp:351
SerialServoBoard::getRefSpeeds
bool getRefSpeeds(double *spds) override
Get reference speed of all joints.
Definition: SerialServoBoard.cpp:227
PolyDriver.h
SerialServoBoard::setRefSpeed
bool setRefSpeed(int j, double sp) override
Set reference speed for a joint, this is the speed used during the interpolation of the trajectory.
Definition: SerialServoBoard.cpp:189
SerialServoBoard::close
bool close() override
Close the DeviceDriver.
Definition: SerialServoBoard.cpp:106
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
SSC32
#define SSC32
Definition: SerialServoBoard.h:22
ISerialDevice.h
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
SerialServoBoard::positionMove
bool positionMove(int j, double ref) override
Set new reference point for a single axis.
Definition: SerialServoBoard.cpp:137
LogComponent.h
movepontech
bool movepontech(int j, double ref, double *positions, double *speeds, ISerialDevice *serial)
Definition: SerialServoBoard.cpp:335
SerialServoBoard::getRefSpeed
bool getRefSpeed(int j, double *ref) override
Get reference speed for a joint.
Definition: SerialServoBoard.cpp:219
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
SerialServoBoard::setRefAcceleration
bool setRefAcceleration(int j, double acc) override
Set reference acceleration for a joint.
Definition: SerialServoBoard.cpp:207
SerialServoBoard::setRefAccelerations
bool setRefAccelerations(const double *accs) override
Set reference acceleration on all joints.
Definition: SerialServoBoard.cpp:213
SerialServoBoard::stop
bool stop() override
Stop motion, multiple joints.
Definition: SerialServoBoard.cpp:255
yarp::dev::ISerialDevice::send
virtual bool send(const yarp::os::Bottle &msg)=0
Sends a string of chars to the serial communications channel.
POLOLUUSB16
#define POLOLUUSB16
Definition: SerialServoBoard.h:26
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
round
#define round(x)
Definition: SerialServoBoard.h:19
SerialServoBoard::relativeMove
bool relativeMove(int j, double delta) override
Set relative position.
Definition: SerialServoBoard.cpp:155
SerialServoBoard::open
bool open(Searchable &config) override
Open the DeviceDriver.
Definition: SerialServoBoard.cpp:34
moveminissc
bool moveminissc(int j, double ref, double *positions, double *speeds, ISerialDevice *serial)
Definition: SerialServoBoard.cpp:318
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
PICOPIC
#define PICOPIC
Definition: SerialServoBoard.h:27