YARP
Yet Another Robot Platform
DynamixelAX12FtdiDriver.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2010 Ze Ji
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  /*
11  * Yarp Driver for Dynamixel AX-12, controlled using USB to Dynamixel Adapter
12  * The default IDs for the motors are for Kaspar robot. As different robots would have different settings,
13  * it is necessary to define the IDs, this can be done via a configuration file. In this example, check the
14  * file sensorindex.cfg. The format is:
15  * SENSORINDEX 16 101 116 132 102 117 133 103 118 134 107 119 135 106 104 105 108
16  * This needs to be done by running configure(), before opening the device
17  *
18  * As it is using libftdi library, it is not possible to identify a device by specifying the serial port number, such as
19  * /dev/ttyUSB0
20  * Instead, this driver requires precise information of the ftdi device, such as the dynamixel usb manufacture id, serial number, etc. See class FtdiDeviceSettings for details.
21  *
22  * The motor does not support Torque control, but provide torque feedback. Therefore, several functions
23  * have been implemented for such purpose, though not very rigorous.
24  *
25  * Author: Ze Ji - z.ji@herts.ac.uk (University of Hertfordshire, UK), 2010
26  * as part of the RoboSkin project (EU FP7).
27  * Part of the work is initially from Sven Magg, 2009
28  */
29 
30 #ifndef DynamixelAX12Driverh
31 #define DynamixelAX12Driverh
32 
33 //#define __DEBUG__
34 
35 #include <yarp/dev/DeviceDriver.h>
37 
38 #include <cstdio>
39 #include <cstdlib>
40 #include <cmath>
41 #include <cstring>
42 #include <ftdi.h>
43 //#include <libftdi/ftdi.h>
44 #include <usb.h>
45 #include <iostream>
46 #include <yarp/os/Time.h>
47 
48 #include <mutex>
49 
50 #define MOTION_COMPLETION_TOLERANCE 3
51 
54 #define INST_PING 0x01
55 #define INST_READ 0x02
56 #define INST_WRITE 0x03
57 #define INST_REGWRITE 0x04
58 #define INST_ACTION 0x05
59 #define INST_RESET 0x06
60 #define INST_SYNCWRITE 0x83
61 
63 #define AX12_BROADCAST_ID 0xFE
64 
68 #define CT_MODEL_NUMBER 00
69 #define CT_FIRMWARE_VERSION 2
70 #define CT_RETURN_DELAY_TIME 5
71 #define CT_CW_ANGLE_LIMIT 06
72 #define CT_CWW_ANGLE_LIMIT 08
73 #define CT_LIMIT_TEMPERATURE_H 11
74 #define CT_LIMIT_VOLTAGE_L 12
75 #define CT_LIMIT_VOLTAGE_H 13
76 #define CT_MAX_TORQUE 14
77 #define CT_STATUS_RETURN_LEVEL 16
78 
81 #define CT_TORQUE_ENABLE 24
82 #define CT_CW_COMP_MARGIN 26
83 #define CT_CWW_COMP_MARGIN 27
84 #define CT_CW_COMP_SLOPE 28
85 #define CT_CWW_COMP_SLOPE 29
86 #define CT_GOAL_POSITION 30
87 #define CT_MOVING_SPEED 32
88 #define CT_TORQUE_LIMIT 34
89 #define CT_PRESENT_POSITION 0x24
90 #define CT_PRESENT_SPEED 0x26
91 #define CT_PRESENT_LOAD 0x28
92 #define CT_PRESENT_VOLTAGE 42
93 #define CT_PRESENT_TEMPERATURE 43
94 #define CT_REG_INSTRUCTION 44
95 #define CT_MOVING 46
96 #define CT_LOCK 47
97 #define CT_PUNCH 48
98 
99 enum ErrorCode {
107  OK
108 };
109 
110 using namespace yarp::os;
111 using namespace yarp::dev;
112 
119 {
120 public:
121  int vendor; //0x0403 normally. Can be found by lsusb on linux
122  int product; //0x6001 Can be found by lsusb on linux
123  char description[100]; // Contains the description
124  char manufacturer[100]; // Contains manufacturer
125  char serial[100]; // used mainly, as this seems unique
126  int baudrate; // if ((retCode = ftdi_set_baudrate(&ftdic, config2.SerialParams.baudrate)) != 0)
128  // int bits; // if (ftdi_set_line_property(&ftdic, BITS_8, STOP_BIT_1, NONE) == -1)
129  // int parity;
130  // int sbit;
131  int flowctrol; // flow control to use. should be SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
132 
133  unsigned int write_chunksize; // e.g. ftdi_write_data_set_chunksize(&ftdic, 3);
134  unsigned int read_chunksize; // ftdi_read_data_set_chunksize(&ftdic, 256);
135 };
136 
141  public yarp::dev::IEncoders
142 {
143 private:
145  struct ftdi_context ftdic;
147  bool deviceOpen;
148 
149  ErrorCode checkAnswerPacket(unsigned char* packet, const char*& message);
150 
151  std::mutex mutex;
152 
153  unsigned char *jointNumbers;
154 
155  int numOfAxes; // default = 16
156 
157 public:
158 
163 
167  virtual ~DynamixelAX12FtdiDriver();
168 
175  bool open(yarp::os::Searchable& config) override;
176 
182  bool close() override;
183 
190  bool configure(yarp::os::Searchable& config) override;
191 
201  virtual int sendCommand(unsigned char id, unsigned char inst[], int size, unsigned char ret[], int &retSize);
202 
210  virtual int readParameter(unsigned char id, unsigned char param);
211 
212  bool getAxes(int *ax) override;
213 
217  bool positionMove(int j, double ref) override;
218  bool positionMove(const double *refs) override;
219  bool relativeMove(int j, double delta) override;
220  bool relativeMove(const double *deltas) override;
221  bool checkMotionDone(int j, bool *flag) override;
222  bool checkMotionDone(bool *flag) override;
223  bool setRefSpeed(int j, double sp) override;
224  bool setRefSpeeds(const double *spds) override;
225  bool setRefAcceleration(int j, double acc) override;
226  bool setRefAccelerations(const double *accs) override;
227  bool getRefSpeed(int j, double *ref) override;
228  bool getRefSpeeds(double *spds) override;
229  bool getRefAcceleration(int j, double *acc) override;
230  bool getRefAccelerations(double *accs) override;
231  bool stop(int j) override;
232  bool stop() override;
233 
234  bool getRefTorques(double *t) override;
235  bool getRefTorque(int j, double *t) override;
236  bool setTorques(const double *t);
237  bool setTorque(int j, double t);
238  bool setTorquePid(int j, const Pid &pid);
239  bool getTorque(int j, double *t) override;
240  bool getTorques(double *t) override;
241  bool setTorquePids(const Pid *pids);
242  bool setTorqueErrorLimit(int j, double limit);
243  bool setTorqueErrorLimits(const double *limits);
244  bool getTorqueError(int j, double *err);
245  bool getTorqueErrors(double *errs);
246  bool getTorquePidOutput(int j, double *out);
247  bool getTorquePidOutputs(double *outs);
248  bool getTorquePid(int j, Pid *pid);
249  bool getTorquePids(Pid *pids);
250  bool getTorqueErrorLimit(int j, double *limit);
251  bool getTorqueErrorLimits(double *limits);
252  bool resetTorquePid(int j);
253  bool disableTorquePid(int j);
254  bool enableTorquePid(int j);
255  bool setTorqueOffset(int j, double v);
256 
257  bool getBemfParam(int j, double *bemf);
258  bool setBemfParam(int j, double bemf);
259 
260  bool resetEncoder(int j) override;
261  bool resetEncoders() override;
262 
263  bool setEncoder(int j, double val) override;
264  bool setEncoders(const double *vals) override;
268  bool getEncoder(int j, double *v) override;
269  bool getEncoders(double *encs) override;
270  bool getEncoderSpeed(int j, double *sp) override;
271  bool getEncoderSpeeds(double *spds) override;
272  bool getEncoderAcceleration(int j, double *spds) override;
273  bool getEncoderAccelerations(double *accs) override;
274 
276  bool setRefTorques(const double* t) override;
277 
278  bool setRefTorque(int j, double t) override;
279  bool getTorqueRange(int j, double* min, double* max) override;
280  bool getTorqueRanges(double* min, double* max) override;
281 
282  // multiple joint version
283  bool positionMove(const int n_joint, const int *joints, const double *refs) override;
284  bool relativeMove(const int n_joint, const int *joints, const double *deltas) override;
285  bool checkMotionDone(const int n_joint, const int *joints, bool *flag) override;
286  bool setRefSpeeds(const int n_joint, const int *joints, const double *spds) override;
287  bool setRefAccelerations(const int n_joint, const int *joints, const double *accs) override;
288  bool getRefSpeeds(const int n_joint, const int *joints, double *spds) override;
289  bool getRefAccelerations(const int n_joint, const int *joints, double *accs) override;
290  bool stop(const int n_joint, const int *joints) override;
291 
292 private:
293 
294  double *positions;
295  double *speeds;
296 
297  struct ftdi_device_list *devlist;
298  struct ftdi_device_list *curdev;
300  int *torques;
301 
302  int normalisePosition(double position);
303  int normaliseSpeed(double speed);
304 
310  bool initMotorIndex(yarp::os::Bottle *sensorIndex);
311 
312  int syncSendCommand(unsigned char id, unsigned char inst[], int size, unsigned char ret[], int &retSize);
313 
314  //bool lock; // lock to ensure sendcommand can be only accessed by one request at one time.
315 };
316 
317 //#undef __DEBUG__
318 
319 #endif
define control board standard interfaces
@ OVERHEATING_ERROR
@ INSTRUCTION_ERROR
float t
bool ret
FtdiDeviceSettings contains information to identify specific device Such a device can contain informa...
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
Control board, encoder interface.
Definition: IEncoders.h:121
Interface for a generic control board device implementing position control.
Interface for control boards implementing torque control.
virtual bool setRefTorques(const double *t)=0
Set the reference value of the torque for all joints.
Contains the parameters for a PID.
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
A base class for nested structures that can be searched.
Definition: Searchable.h:69
An interface for the device drivers.
An interface to the operating system, including Port based communication.