dxl_interface.cpp
Go to the documentation of this file.
1 
2 
4 
5 namespace dxl
6 {
7 
8 /***** DXL INTERFACE *****/
9 
11  {
12 
13  }
14 
16  unsigned int baudrate,
17  float protocol)
18  {
19  protocol_ = protocol;
20  if (!loadProtocol(protocol_))
21  return INVALID_PROTOCOL;
23  if (port_handler_->openPort())
24  {
25  if (port_handler_->setBaudRate(baudrate))
26  {
27  return SUCCESS;
28  }
29  return BAUDRATE_FAIL;
30  }
31  return PORT_FAIL;
32  }
33 
34  bool DxlInterface::loadProtocol(uint16_t protocol)
35  {
36  if (protocol == DXL_PROTOCOL1)
37  {
39  return true;
40  }
41  if (protocol == DXL_PROTOCOL2)
42  {
44  return true;
45  }
46  return true;
47  }
48 
49  /* ping motor, if success - motor model will be filled */
51  {
52  int result = COMM_TX_FAIL;
53  uint8_t error = 0;
54 
56  motor.id,
57  &(motor.spec.model),
58  &error);
59 
60  if (result != COMM_SUCCESS || error != 0)
61  return false;
62  return true;
63  }
64 
65 
67  {
68  uint8_t error = 0;
69  int result = COMM_TX_FAIL;
70 
72  motor.id,
73  motor.spec.torque_write_addr,
74  flag,
75  &error);
76 
77  if (result != COMM_SUCCESS || error != 0)
78  return false;
79 
80  motor.in_torque = flag;
81  return true;
82  }
83 
85  {
86 
87  // Try reboot
88  // Dynamixel LED will flicker while it reboots
89  uint8_t error = 0;
90  int result = COMM_TX_FAIL;
91 
92  result = pkt_handler_->reboot(port_handler_, motor.id, &error);
93  if (result != COMM_SUCCESS || error != 0)
94  return false;
95  return true;
96  }
97 
98 
99  bool DxlInterface::broadcastPing(std::vector<uint8_t> result_vec, uint16_t protocol)
100  {
101  int result = COMM_TX_FAIL;
102 
103  result = pkt_handler_->broadcastPing(port_handler_, result_vec);
104  if (result != COMM_SUCCESS)
105  return false;
106  return true;
107  }
108 
110  {
112  }
113 
114  bool DxlInterface::readMotorsPos(std::vector<dxl::motor> &motors)
115  {
117  for (motor &motor : motors)
118  {
119  bool read_success = bulk_read.addParam(motor.id,
122  if (!read_success)
123  return false;
124  }
125 
126  int comm_result = bulk_read.txRxPacket();
127  if (comm_result != COMM_SUCCESS)
128  //packet_handler_->printTxRxResult(comm_result);
129  return false;
130 
131  for (motor &motor : motors)
132  {
133  bool getdata_result = bulk_read.isAvailable(motor.id,
136  if (!getdata_result)
137  return false;
138 
139  uint32_t ticks = bulk_read.getData(motor.id,
143 
144  if (motor.first_pos_read)
145  {
146  motor.first_pos_read = false;
148  }
149  }
150  return true;
151  }
152 
153  bool DxlInterface::readMotorsVel(std::vector<dxl::motor> &motors)
154  {
156  for (motor &motor : motors)
157  {
158  bool read_success = bulk_read.addParam(motor.id,
161  if (!read_success)
162  return false;
163  }
164 
165  int comm_result = bulk_read.txRxPacket();
166  if (comm_result != COMM_SUCCESS)
167  {
168  //packet_handler_->printTxRxResult(comm_result);
169  return false;
170  }
171 
172  for (motor &motor : motors)
173  {
174  bool getdata_result = bulk_read.isAvailable(motor.id,
177  if (!getdata_result)
178  return false;
179 
180  uint32_t ticks_per_sec = bulk_read.getData(motor.id,
184  }
185 
186  return true;
187  }
188 
189  bool DxlInterface::readMotorsError(std::vector<dxl::motor> &motors)
190  {
192  for (motor &motor : motors)
193  {
194  bool read_success = bulk_read.addParam(motor.id,
196  DXL_ERR_LEN);
197  if (!read_success)
198  return false;
199  }
200 
201  int comm_result = bulk_read.txRxPacket();
202  if (comm_result != COMM_SUCCESS)
203  {
204  //packet_handler_->printTxRxResult(comm_result);
205  return false;
206  }
207 
208  for (motor &motor : motors)
209  {
210  bool getdata_result = bulk_read.isAvailable(motor.id,
212  DXL_ERR_LEN);
213  if (!getdata_result)
214  return false;
215 
216  motor.error = bulk_read.getData(motor.id,
218  DXL_ERR_LEN);
219  }
220  return true;
221  }
222 
223  bool DxlInterface::readMotorsLoad(std::vector<dxl::motor> &motors)
224  {
226  for (motor &motor : motors)
227  {
228  bool read_success = bulk_read.addParam(motor.id,
231  if (!read_success)
232  return false;
233  }
234 
235  int comm_result = bulk_read.txRxPacket();
236  if (comm_result != COMM_SUCCESS)
237  {
238  //pkt_handler_->printTxRxResult(comm_result);
239  return false;
240  }
241 
242  for (motor &motor : motors)
243  {
244  bool getdata_result = bulk_read.isAvailable(motor.id,
247  if (!getdata_result)
248  return false;
249 
250 
251  motor.current = (int16_t)bulk_read.getData(motor.id,
254 
256  }
257  return true;
258  }
259 
260  bool DxlInterface::bulkWriteVelocity(std::vector<dxl::motor> &motors)
261  {
263 
264  for (motor &motor : motors)
265  {
266  bool addparam_success = false;
267 
269  /* dxl api interperate 0 ticks velocity as the highest velocity. */
270  /* set set_first_pos_write_to_curr_pos field to true prevent it */
271  /* by setting velocity to the last non-zero value */
273  {
274  double min_ticks;
275  if (motor.id>=1 && motor.id <=6) min_ticks=700;
276  else min_ticks=0;
277  if (abs(motor_vel) <= min_ticks){
278 
280  // printf("id: %d motor_vel_tics %d motor_vel_rad_s: %f\n",motor.id,motor_vel,((double)motor_vel) * 2.0 * M_PI / 60.0 *motor.spec.rpm_scale_factor);
281  }
282  else
284  }
285 
286  addparam_success = bulk_write.addParam(motor.id,
289  (uint8_t*)&motor_vel);
290  if (!addparam_success)
291  return false;
292  }
293 
294  int8_t comm_result_ = bulk_write.txPacket();
295 
296  if (comm_result_ != COMM_SUCCESS)
297  return false;
298 
299  bulk_write.clearParam();
300  return true;
301  }
302 
303  bool DxlInterface::bulkWritePosition(std::vector<dxl::motor> &motors)
304  {
306 
307  for (motor &motor : motors)
308  {
309  bool addparam_success = false;
310 
312  addparam_success = bulk_write.addParam(motor.id,
315  (uint8_t*)&motor_pos);
316  if (!addparam_success)
317  return false;
318  }
319 
320  int8_t comm_result_ = bulk_write.txPacket();
321 
322  if (comm_result_ != COMM_SUCCESS)
323  return false;
324 
325  bulk_write.clearParam();
326  return true;
327  }
328 
329  bool DxlInterface::setMotorsLed(std::vector<dxl::motor> &motors,
330  const led_color &color)
331  {
332  /* todo: implement. reason for not implementing so far: some motor models
333  * have only red led, and some have rgb led. red led looks like warning */
334  }
335 
336  /**** DXL MATH *****/
337  double convertions::ticks2rads(int32_t ticks, struct motor &motor, float protocol)
338  {
339  if (protocol == DXL_PROTOCOL2)
340  {
341  if (motor.spec.model==1040)
342  {
343  const double FromTicks = 1.0 / (motor.spec.cpr / 2.0);
344  return static_cast<double>(M_PI-(ticks) * FromTicks * M_PI);
345  }
346  else if (motor.spec.model==30)
347  {
348  double cprDev2 = motor.spec.cpr / 2.0f;
349  return (static_cast<double>(ticks) - cprDev2) * M_PI / cprDev2;
350  }
351  else
352  {
353  const double FromTicks = 1.0 / (motor.spec.cpr / 2.0);
354  return static_cast<double>((ticks) * FromTicks * M_PI);
355  }
356  }
357  else
358  {
359  double cprDev2 = motor.spec.cpr / 2.0f;
360  return (static_cast<double>(ticks) - cprDev2) * M_PI / cprDev2;
361  }
362  }
363 
364  int32_t convertions::rads2ticks(double rads, struct motor &motor, float protocol)
365  {
366 
367  if (protocol == DXL_PROTOCOL2) {
368  if (motor.spec.model==1040) {
369  return static_cast<int32_t>(round((-rads *180.0/ M_PI+180.0)/ 0.088));
370  }
371  else if (motor.spec.model==30) {
372  double cprDev2 = motor.spec.cpr / 2.0f;
373  return static_cast<int32_t>(round(cprDev2 + (rads * cprDev2 / M_PI)));
374  }
375  else {
376  double cprDev2 = motor.spec.cpr / 2.0f;
377  return static_cast<int32_t>(round((rads / M_PI) * cprDev2));
378  }
379  }
380  else {
381  double cprDev2 = motor.spec.cpr / 2.0f;
382  return static_cast<int32_t>(round(cprDev2 + (rads * cprDev2 / M_PI)));
383  }
384  }
385 
386  /* rads per sec to ticks per sec */
387  int32_t convertions::rad_s2ticks_s(double rads, struct motor &motor, float protocol)
388  {
389  if (protocol == DXL_PROTOCOL2) {
390  // if (motor.id==7) printf("id: %d rad_s %f ticks: %d\n",motor.id,rads,static_cast<int32_t >(rads / 2.0 / M_PI * 60.0 / motor.spec.rpm_scale_factor));
391  return static_cast<int32_t >(rads / 2.0 / M_PI * 60.0 / motor.spec.rpm_scale_factor);
392  }
393  else
394  return static_cast<int32_t >(83.49f * (rads)-0.564f);
395  }
396 
397  /* ticks per sec to rads per sec */
398  double convertions::ticks_s2rad_s(int32_t ticks, struct motor &motor, float protocol)
399  {
400  if (protocol == DXL_PROTOCOL2) {
401  //
402  return ((double)ticks) * 2.0 * M_PI / 60.0 *motor.spec.rpm_scale_factor;
403 
404  }
405  else
406  return (100.0f / 8349.0f) * ((double)ticks) + (94.0f / 13915.0f);
407  }
408 
409 
410 }
dynamixel::PortHandler * port_handler_
virtual int broadcastPing(PortHandler *port, std::vector< uint8_t > &id_list)=0
bool isAvailable(uint8_t id, uint16_t address, uint16_t data_length)
uint16_t vel_read_addr
Definition: dxl_interface.h:29
static PortHandler * getPortHandler(const char *port_name)
double command_position
Definition: dxl_interface.h:83
uint16_t pos_read_addr
Definition: dxl_interface.h:28
uint16_t vel_write_addr
Definition: dxl_interface.h:34
uint16_t len_goal_speed
Definition: dxl_interface.h:40
virtual bool openPort()=0
f
bool broadcastPing(std::vector< uint8_t > result_vec, uint16_t protocol)
#define COMM_SUCCESS
bool loadProtocol(uint16_t protocol)
int32_t rads2ticks(double rads, struct motor &motor, float protocol)
bool addParam(uint8_t id, uint16_t start_address, uint16_t data_length)
uint16_t torque_write_addr
Definition: dxl_interface.h:33
bool first_pos_read
uint16_t len_present_speed
Definition: dxl_interface.h:37
uint16_t current_read_addr
Definition: dxl_interface.h:30
bool readMotorsError(std::vector< motor > &motors)
dxl::spec spec
Definition: dxl_interface.h:75
uint16_t pos_write_addr
Definition: dxl_interface.h:35
int32_t rad_s2ticks_s(double rads, struct motor &motor, float protocol)
bool dont_allow_zero_ticks_vel
Definition: dxl_interface.h:97
uint16_t error_read_addr
Definition: dxl_interface.h:31
bool in_torque
Definition: dxl_interface.h:79
virtual int reboot(PortHandler *port, uint8_t id, uint8_t *error=0)=0
bool readMotorsVel(std::vector< motor > &motors)
double velocity
Definition: dxl_interface.h:81
#define DXL_PROTOCOL1
Definition: dxl_interface.h:12
bool reboot(const motor &motor)
#define COMM_TX_FAIL
bool setMotorsLed(std::vector< dxl::motor > &motors, const led_color &color)
double current
Definition: dxl_interface.h:82
virtual void closePort()=0
#define DXL_ERR_LEN
Definition: dxl_interface.h:14
uint16_t model
Definition: dxl_interface.h:21
uint8_t error
Definition: dxl_interface.h:85
double rpm_scale_factor
Definition: dxl_interface.h:25
uint16_t len_present_pos
Definition: dxl_interface.h:38
virtual int write1ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint8_t data, uint8_t *error=0)=0
uint16_t len_present_curr
Definition: dxl_interface.h:39
static PacketHandler * getPacketHandler(float protocol_version=2.0)
uint16_t len_goal_pos
Definition: dxl_interface.h:41
double ticks2rads(int32_t ticks, struct motor &motor, float protocol)
bool bulkWritePosition(std::vector< motor > &motors)
#define DXL_PROTOCOL2
Definition: dxl_interface.h:13
dynamixel::PacketHandler * pkt_handler_
uint32_t getData(uint8_t id, uint16_t address, uint16_t data_length)
bool readMotorsPos(std::vector< motor > &motors)
double current_ratio
Definition: dxl_interface.h:26
virtual int ping(PortHandler *port, uint8_t id, uint8_t *error=0)=0
double command_velocity
Definition: dxl_interface.h:84
double ticks_s2rad_s(int32_t ticks, struct motor &motor, float protocol)
virtual bool setBaudRate(const int baudrate)=0
uint8_t id
Definition: dxl_interface.h:77
PortState openPort(std::string port_name, unsigned int baudrate, float protocol)
bool readMotorsLoad(std::vector< motor > &motors)
bool setTorque(motor &motor, bool flag)
double position
Definition: dxl_interface.h:80
bool addParam(uint8_t id, uint16_t start_address, uint16_t data_length, uint8_t *data)
int32_t prev_non_zero_velocity_ticks
Definition: dxl_interface.h:94
bool bulkWriteVelocity(std::vector< motor > &motors)
bool ping(motor &motor)


dxl_interface
Author(s):
autogenerated on Wed Jan 3 2018 03:47:56