xarm_api.cc
Go to the documentation of this file.
1 /*
2 # Software License Agreement (MIT License)
3 #
4 # Copyright (c) 2019, UFACTORY, Inc.
5 # All rights reserved.
6 #
7 # Author: Vinman <vinman.wen@ufactory.cc> <vinman.cub@gmail.com>
8 */
9 #define _CRT_SECURE_NO_WARNINGS
10 #include <regex>
11 #include <iostream>
12 #include <string>
13 #include <stdarg.h>
14 // #include <unistd.h>
15 #include <string.h>
16 #include "xarm/wrapper/xarm_api.h"
17 
18 #define REPORT_BUF_SIZE 1024
19 
20 using namespace std;
21 
22 static int BAUDRATES[13] = { 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1000000, 1500000, 2000000, 2500000 };
23 
24 static int get_baud_inx(int baud) {
25  for (int i = 0; i < 13; i++) { if (BAUDRATES[i] == baud) return i; }
26  return -1;
27 }
28 
29 static bool compare_version(int v1[3], int v2[3]) {
30  for (int i = 0; i < 3; i++) {
31  if (v1[i] > v2[i]) {
32  return true;
33  }
34  else if (v1[i] < v2[i]) {
35  return false;
36  }
37  }
38  return false;
39 }
40 
42  const std::string &port,
43  bool is_radian,
44  bool do_not_open,
45  bool check_tcp_limit,
46  bool check_joint_limit,
47  bool check_cmdnum_limit,
48  bool check_robot_sn,
49  bool check_is_ready,
50  bool check_is_pause,
51  int max_callback_thread_count,
52  int max_cmdnum)
53  : default_is_radian(is_radian), port_(port),
54  check_tcp_limit_(check_tcp_limit), check_joint_limit_(check_joint_limit),
55  check_cmdnum_limit_(check_cmdnum_limit), check_robot_sn_(check_robot_sn),
56  check_is_ready_(check_is_ready), check_is_pause_(check_is_pause) {
57  // default_is_radian = is_radian;
58  // check_tcp_limit_ = check_tcp_limit;
59  pool.set_max_thread_count(max_callback_thread_count);
60  callback_in_thread_ = max_callback_thread_count != 0;
61  max_cmdnum_ = max_cmdnum > 0 ? max_cmdnum : 256;
62  _init();
63  printf("SDK_VERSION: %s\n", SDK_VERSION);
64  if (!do_not_open) {
65  connect();
66  }
67 }
68 
70  disconnect();
71 }
72 
73 void XArmAPI::_init(void) {
74  core = NULL;
75  stream_tcp_ = NULL;
76  stream_tcp_report_ = NULL;
77  stream_ser_ = NULL;
78  is_ready_ = true;
79  is_tcp_ = true;
80  is_old_protocol_ = false;
81  is_first_report_ = true;
82  is_sync_ = false;
83  arm_type_is_1300_ = false;
85 
90 
91  mt_brake_ = 0;
92  mt_able_ = 0;
93  min_tcp_speed_ = (float)0.1; // mm/s
94  max_tcp_speed_ = 1000; // mm/s
95  min_tcp_acc_ = 1.0; // mm/s^2
96  max_tcp_acc_ = 50000; // mm/s^2
97  min_joint_speed_ = (float)0.01; // rad/s
98  max_joint_speed_ = 4.0; // rad/s
99  min_joint_acc_ = (float)0.01; // rad/s^2
100  max_joint_acc_ = 20.0; // rad/s^2
101  count = -1;
102 
104 
105  angles = new fp32[7]{ 0, 0, 0, 0, 0, 0, 0 };
106  last_used_angles = new fp32[7]{ 0, 0, 0, 0, 0, 0, 0 };
107  tcp_offset = new fp32[6]{ 0, 0, 0, 0, 0, 0 };
108  if (default_is_radian) {
111  last_used_joint_speed = (float)0.3490658503988659; // rad/s (20°/s);
112  last_used_joint_acc = (float)8.726646259971648; // rad/s^2 (500°/s^2);
113  position = new fp32[6]{ 201.5, 0, 140.5, (fp32)3.1415926, 0, 0 };
114  last_used_position = new fp32[6]{ 201.5, 0, 140.5, (fp32)3.1415926, 0, 0 };
115  }
116  else {
117  joint_speed_limit = new fp32[2]{ (fp32)(min_joint_speed_ * RAD_DEGREE), (fp32)(max_joint_speed_ * RAD_DEGREE) };
118  joint_acc_limit = new fp32[2]{ (fp32)(min_joint_acc_ * RAD_DEGREE), (fp32)(max_joint_acc_ * RAD_DEGREE) };
119  last_used_joint_speed = (fp32)(0.3490658503988659 * RAD_DEGREE); // rad/s (20°/s);
120  last_used_joint_acc = (fp32)(8.726646259971648 * RAD_DEGREE); // rad/s^2 (500°/s^2);
121  position = new fp32[6]{ 201.5, 0, 140.5, (fp32)(3.1415926 * RAD_DEGREE), 0, 0 };
122  last_used_position = new fp32[6]{ 201.5, 0, 140.5, (fp32)(3.1415926 * RAD_DEGREE), 0, 0 };
123  }
124 
125  state = 4;
126  mode = 0;
127  cmd_num = 0;
128  joints_torque = new fp32[7]{ 0, 0, 0, 0, 0, 0, 0 };
129  motor_brake_states = new bool[8]{ 0, 0, 0, 0, 0, 0, 0, 0 };
130  motor_enable_states = new bool[8]{ 0, 0, 0, 0, 0, 0, 0, 0 };
131  error_code = 0;
132  warn_code = 0;
133  tcp_load = new fp32[4]{ 0, 0, 0, 0 };
135  teach_sensitivity = 0;
136  device_type = 7;
137  axis = 7;
138  master_id = 0;
139  slave_id = 0;
140  motor_tid = 0;
141  motor_fid = 0;
142  tcp_jerk = 1000; // mm/s^3
143  joint_jerk = default_is_radian ? 20 : (fp32)(20 * RAD_DEGREE); // 20 rad/s^3
144  rot_jerk = (float)2.3;
145  max_rot_acc = (float)2.7;
148  last_used_tcp_speed = 100; // mm/s
149  last_used_tcp_acc = 2000; // mm/s^2
150  gravity_direction = new fp32[3]{ 0, 0, -1 };
151  realtime_tcp_speed = 0;
152  realtime_joint_speeds = new fp32[7]{ 0, 0, 0, 0, 0, 0, 0 };
153  world_offset = new fp32[6]{ 0, 0, 0, 0, 0, 0 };
154  temperatures = new fp32[7]{ 0, 0, 0, 0, 0, 0 };
155  gpio_reset_config = new unsigned char[2]{0, 0};
156  modbus_baud_ = -1;
157  ignore_error_ = false;
158  ignore_state_ = false;
159 
160  gripper_is_enabled_ = false;
161  bio_gripper_is_enabled_ = false;
162  robotiq_is_activated_ = false;
165  voltages = new fp32[7]{ 0, 0, 0, 0, 0, 0, 0 };
166  currents = new fp32[7]{ 0, 0, 0, 0, 0, 0, 0 };
170  collision_model_params = new fp32[6]{ 0, 0, 0, 0, 0, 0};
171  cgpio_state = 0;
172  cgpio_code = 0;
173  cgpio_input_digitals = new int[2]{ 0, 0 };
174  cgpio_output_digitals = new int[2]{ 0, 0 };
175  cgpio_intput_anglogs = new fp32[2]{ 0, 0 };
176  cgpio_output_anglogs = new fp32[2]{ 0, 0 };
177  cgpio_input_conf = new int[16]{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
178  cgpio_output_conf = new int[16]{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
179  cmd_timeout_ = -1;
180 
184  gripper_version_numbers_[0] = -1;
185  gripper_version_numbers_[1] = -1;
186  gripper_version_numbers_[2] = -1;
187 }
188 
190  return has_error() || has_warn();
191 }
192 
193 bool XArmAPI::has_error(void) {
194  return error_code != 0;
195 }
196 
197 bool XArmAPI::has_warn(void) {
198  return warn_code != 0;
199 }
200 
202  return is_tcp_ ? (stream_tcp_ == NULL ? false : stream_tcp_->is_ok() == 0) : (stream_ser_ == NULL ? false : stream_ser_->is_ok() == 0);
203 }
204 
206  return is_tcp_ ? (stream_tcp_report_ == NULL ? false : stream_tcp_report_->is_ok() == 0) : false;
207 }
208 
209 template<typename callable_vector, class... arguments>
210 inline void XArmAPI::_report_callback(callable_vector&& callbacks, arguments&&... args) {
211  for (size_t i = 0; i < callbacks.size(); i++) {
212  if (callback_in_thread_) pool.dispatch(callbacks[i], std::forward<arguments>(args)...);
213  else pool.commit(callbacks[i], std::forward<arguments>(args)...);
214  }
215 }
216 
219  // for (size_t i = 0; i < report_location_callbacks_.size(); i++) {
220  // if (callback_in_thread_) pool.dispatch(report_location_callbacks_[i], position, angles);
221  // else pool.commit(report_location_callbacks_[i], position, angles);
222  // }
223 }
224 
226  bool connected = stream_tcp_ == NULL ? false : stream_tcp_->is_ok() == 0;
227  bool reported = stream_tcp_report_ == NULL ? false : stream_tcp_report_->is_ok() == 0;
228  _report_callback(connect_changed_callbacks_, connected, reported);
229  // for (size_t i = 0; i < connect_changed_callbacks_.size(); i++) {
230  // if (callback_in_thread_) pool.dispatch(connect_changed_callbacks_[i], connected, reported);
231  // else pool.commit(connect_changed_callbacks_[i], connected, reported);
232  // }
233 }
234 
236  if (ignore_state_) return;
238  // for (size_t i = 0; i < state_changed_callbacks_.size(); i++) {
239  // if (callback_in_thread_) pool.dispatch(state_changed_callbacks_[i], state);
240  // else pool.commit(state_changed_callbacks_[i], state);
241  // }
242 }
243 
246  // for (size_t i = 0; i < mode_changed_callbacks_.size(); i++) {
247  // if (callback_in_thread_) pool.dispatch(mode_changed_callbacks_[i], mode);
248  // else pool.commit(mode_changed_callbacks_[i], mode);
249  // }
250 }
251 
254  // for (size_t i = 0; i < mtable_mtbrake_changed_callbacks_.size(); i++) {
255  // if (callback_in_thread_) pool.dispatch(mtable_mtbrake_changed_callbacks_[i], mt_able_, mt_brake_);
256  // else pool.commit(mtable_mtbrake_changed_callbacks_[i], mt_able_, mt_brake_);
257  // }
258 }
259 
261  if (ignore_error_) return;
263  // for (size_t i = 0; i < error_warn_changed_callbacks_.size(); i++) {
264  // if (callback_in_thread_) pool.dispatch(error_warn_changed_callbacks_[i], error_code, warn_code);
265  // else pool.commit(error_warn_changed_callbacks_[i], error_code, warn_code);
266  // }
267 }
268 
271  // for (size_t i = 0; i < cmdnum_changed_callbacks_.size(); i++) {
272  // if (callback_in_thread_) pool.dispatch(cmdnum_changed_callbacks_[i], cmd_num);
273  // else pool.commit(cmdnum_changed_callbacks_[i], cmd_num);
274  // }
275 }
276 
279  // for (size_t i = 0; i < temperature_changed_callbacks_.size(); i++) {
280  // if (callback_in_thread_) pool.dispatch(temperature_changed_callbacks_[i], temperatures);
281  // else pool.commit(temperature_changed_callbacks_[i], temperatures);
282  // }
283 }
284 
287  // for (size_t i = 0; i < count_changed_callbacks_.size(); i++) {
288  // if (callback_in_thread_) pool.dispatch(count_changed_callbacks_[i], count);
289  // else pool.commit(count_changed_callbacks_[i], count);
290  // }
291 }
292 
293 void XArmAPI::_update_old(unsigned char *rx_data) {
294  unsigned char *data_fp = &rx_data[4];
295  int sizeof_data = bin8_to_32(rx_data);
296  if (sizeof_data >= 87) {
297  int state_ = state;
298  state = data_fp[4];
299  if (state != 3) {
300  std::unique_lock<std::mutex> locker(mutex_);
301  cond_.notify_all();
302  locker.unlock();
303  }
304  if (state != state_) _report_state_changed_callback();
305  if (sizeof_data < 187) is_ready_ = (state == 4 || state == 5) ? false : true;
306 
307  int brake = mt_brake_;
308  int able = mt_able_;
309  mt_brake_ = data_fp[5];
310  mt_able_ = data_fp[6];
312 
313  if (!is_first_report_) {
314  bool ready = true;
315  for (int i = 0; i < 8; i++) {
316  motor_brake_states[i] = mt_brake_ >> i & 0x01;
317  ready = (i < axis && !motor_brake_states[i]) ? false : ready;
318  }
319  for (int i = 0; i < 8; i++) {
320  motor_enable_states[i] = mt_able_ >> i & 0x01;
321  ready = (i < axis && !motor_enable_states[i]) ? false : ready;
322  }
323  is_ready_ = (state == 4 || state == 5 || !ready) ? false : true;
324  }
325  else {
326  is_ready_ = false;
327  }
328  is_first_report_ = false;
329  if (!is_ready_) sleep_finish_time_ = 0;
330 
331  int err = error_code;
332  int warn = warn_code;
333  error_code = data_fp[7];
334  warn_code = data_fp[8];
336 
337  if ((error_code >= 10 && error_code <= 17) || error_code ==1 || error_code == 19 || error_code == 28) {
338  modbus_baud_ = -1;
339  robotiq_is_activated_ = false;
340  gripper_is_enabled_ = false;
341  bio_gripper_is_enabled_ = false;
342  bio_gripper_speed_ = -1;
343  gripper_version_numbers_[0] = -1;
344  gripper_version_numbers_[1] = -1;
345  gripper_version_numbers_[2] = -1;
346  }
347 
348  hex_to_nfp32(&data_fp[9], angles, 7);
349  for (int i = 0; i < 7; i++) {
350  angles[i] = (float)(default_is_radian ? angles[i] : angles[i] * RAD_DEGREE);
351  }
352  hex_to_nfp32(&data_fp[37], position, 6);
353  for (int i = 0; i < 6; i++) {
354  position[i] = (float)(default_is_radian || i < 3 ? position[i] : position[i] * RAD_DEGREE);
355  }
357 
358  int cmdnum_ = cmd_num;
359  cmd_num = bin8_to_16(&data_fp[61]);
360  if (cmd_num != cmdnum_) _report_cmdnum_changed_callback();
361 
362  hex_to_nfp32(&data_fp[63], tcp_offset, 6);
363  for (int i = 0; i < 6; i++) {
364  tcp_offset[i] = (float)(default_is_radian || i < 3 ? tcp_offset[i] : tcp_offset[i] * RAD_DEGREE);
365  }
366  }
367  if (sizeof_data >= 187) {
368  device_type = data_fp[87];
369  int _axis = data_fp[88];
370  master_id = data_fp[89];
371  slave_id = data_fp[90];
372  motor_tid = data_fp[91];
373  motor_fid = data_fp[92];
374 
375  axis = (device_type == 5) ? 5 : (device_type == 6) ? 6 : (device_type == 3) ? 7 : (_axis >= 5 && _axis <= 7) ? _axis : axis;
376 
377  memcpy(version, &data_fp[93], 30);
378 
379  hex_to_nfp32(&data_fp[123], trs_msg_, 5);
380  tcp_jerk = trs_msg_[0];
381  min_tcp_acc_ = trs_msg_[1];
382  max_tcp_acc_ = trs_msg_[2];
389 
390  hex_to_nfp32(&data_fp[143], p2p_msg_, 5);
396  if (default_is_radian) {
401  }
402  else {
403  joint_speed_limit[0] = (float)(min_joint_acc_ * RAD_DEGREE);
404  joint_speed_limit[1] = (float)(max_joint_acc_ * RAD_DEGREE);
405  joint_acc_limit[0] = (float)(min_joint_speed_ * RAD_DEGREE);
406  joint_acc_limit[1] = (float)(max_joint_speed_ * RAD_DEGREE);
407  }
408 
409  hex_to_nfp32(&data_fp[163], rot_msg_, 2);
410  rot_jerk = rot_msg_[0];
411  max_rot_acc = rot_msg_[1];
412 
413  for (int i = 0; i < 17; i++) sv3msg_[i] = data_fp[171 + i];
414  }
415 }
416 
417 void XArmAPI::_update(unsigned char *rx_data) {
418  long long report_time = get_system_time();
419  long long interval = report_time - last_report_time_;
421  last_report_time_ = report_time;
422  if (is_old_protocol_) {
423  _update_old(rx_data);
424  return;
425  }
426  unsigned char *data_fp = &rx_data[4];
427  int sizeof_data = bin8_to_32(rx_data);
428  if (sizeof_data >= 87) {
429  int state_ = state;
430  state = data_fp[4] & 0x0F;
431  if (state != 3) {
432  std::unique_lock<std::mutex> locker(mutex_);
433  cond_.notify_all();
434  locker.unlock();
435  }
436  if (state != state_) _report_state_changed_callback();
437  if (sizeof_data < 133) is_ready_ = (state == 4 || state == 5) ? false : true;
438 
439  int mode_ = mode;
440  mode = data_fp[4] >> 4;
441  if (mode != mode_) _report_mode_changed_callback();
442  int cmdnum_ = cmd_num;
443  cmd_num = bin8_to_16(&data_fp[5]);
444  if (cmd_num != cmdnum_) _report_cmdnum_changed_callback();
445 
446  hex_to_nfp32(&data_fp[7], angles, 7);
447  for (int i = 0; i < 7; i++) {
448  angles[i] = (float)(default_is_radian ? angles[i] : angles[i] * RAD_DEGREE);
449  }
450  hex_to_nfp32(&data_fp[35], position, 6);
451  for (int i = 0; i < 6; i++) {
452  position[i] = (float)(default_is_radian || i < 3 ? position[i] : position[i] * RAD_DEGREE);
453  }
455  hex_to_nfp32(&data_fp[59], joints_torque, 7);
456  }
457  if (sizeof_data >= 133) {
458  if (data_fp[131] < 0 || data_fp[131] > 6 || data_fp[132] < 0 || data_fp[132] > 6) {
460  printf("DataException\n");
461  return;
462  }
463  int brake = mt_brake_;
464  int able = mt_able_;
465  mt_brake_ = data_fp[87];
466  mt_able_ = data_fp[88];
468  if (!is_first_report_) {
469  bool ready = true;
470  for (int i = 0; i < 8; i++) {
471  motor_brake_states[i] = mt_brake_ >> i & 0x01;
472  ready = (i < axis && !motor_brake_states[i]) ? false : ready;
473  }
474  for (int i = 0; i < 8; i++) {
475  motor_enable_states[i] = mt_able_ >> i & 0x01;
476  ready = (i < axis && !motor_enable_states[i]) ? false : ready;
477  }
478  is_ready_ = (state == 4 || state == 5 || !ready) ? false : true;
479  }
480  else {
481  is_ready_ = false;
482  }
483  is_first_report_ = false;
484  if (!is_ready_) sleep_finish_time_ = 0;
485 
486  int err = error_code;
487  int warn = warn_code;
488  error_code = data_fp[89];
489  warn_code = data_fp[90];
491 
492  if ((error_code >= 10 && error_code <= 17) || error_code == 1 || error_code == 19 || error_code == 28) {
493  modbus_baud_ = -1;
494  robotiq_is_activated_ = false;
495  gripper_is_enabled_ = false;
496  bio_gripper_is_enabled_ = false;
497  bio_gripper_speed_ = -1;
498  gripper_version_numbers_[0] = -1;
499  gripper_version_numbers_[1] = -1;
500  gripper_version_numbers_[2] = -1;
501  }
502  if (!is_sync_ && error_code != 0 && state != 4 && state != 5) {
503  _sync();
504  is_sync_ = true;
505  }
506 
507  hex_to_nfp32(&data_fp[91], tcp_offset, 6);
508  for (int i = 0; i < 6; i++) {
509  tcp_offset[i] = (float)(default_is_radian || i < 3 ? tcp_offset[i] : tcp_offset[i] * RAD_DEGREE);
510  }
511  hex_to_nfp32(&data_fp[115], tcp_load, 4);
512 
513  if (!compare_version(version_number, new int[3]{ 0, 2, 0 })) {
514  tcp_load[1] = tcp_load[1] * 1000;
515  tcp_load[2] = tcp_load[2] * 1000;
516  tcp_load[3] = tcp_load[3] * 1000;
517  }
518 
519  collision_sensitivity = data_fp[131];
520  teach_sensitivity = data_fp[132];
521  hex_to_nfp32(&data_fp[133], gravity_direction, 3);
522  }
523  if (sizeof_data >= 245) {
524  device_type = data_fp[145];
525  int _axis = data_fp[146];
526  master_id = data_fp[147];
527  slave_id = data_fp[148];
528  motor_tid = data_fp[149];
529  motor_fid = data_fp[150];
530 
531  axis = (_axis >= 5 && _axis <= 7) ? _axis : axis;
532 
533  memcpy(version, &data_fp[151], 30);
534 
535  hex_to_nfp32(&data_fp[181], trs_msg_, 5);
536  tcp_jerk = trs_msg_[0];
537  min_tcp_acc_ = trs_msg_[1];
538  max_tcp_acc_ = trs_msg_[2];
545 
546  hex_to_nfp32(&data_fp[201], p2p_msg_, 5);
552  if (default_is_radian) {
557  }
558  else {
559  joint_speed_limit[0] = (float)(min_joint_acc_ * RAD_DEGREE);
560  joint_speed_limit[1] = (float)(max_joint_acc_ * RAD_DEGREE);
561  joint_acc_limit[0] = (float)(min_joint_speed_ * RAD_DEGREE);
562  joint_acc_limit[1] = (float)(max_joint_speed_ * RAD_DEGREE);
563  }
564 
565  hex_to_nfp32(&data_fp[221], rot_msg_, 2);
566  rot_jerk = rot_msg_[0];
567  max_rot_acc = rot_msg_[1];
568 
569  for (int i = 0; i < 17; i++) sv3msg_[i] = data_fp[229 + i];
570 
571  if (sizeof_data >= 252) {
572  bool isChange = false;
573  for (int i = 0; i < 7; i++) {
574  isChange = (temperatures[i] != data_fp[245 + i]) ? true : isChange;
575  temperatures[i] = data_fp[245 + i];
576  }
577  if (isChange) {
579  }
580  }
581  if (sizeof_data >= 284) {
582  fp32 speeds[8];
583  hex_to_nfp32(&data_fp[252], speeds, 8);
584  realtime_tcp_speed = speeds[0];
585  realtime_joint_speeds = &speeds[1];
586  }
587  if (sizeof_data >= 288) {
588  int cnt = bin8_to_32(&data_fp[284]);
589  if (count != -1 && count != cnt) {
590  count = cnt;
592  }
593  count = cnt;
594  }
595  if (sizeof_data >= 312) {
596  hex_to_nfp32(&data_fp[288], world_offset, 6);
597  for (int i = 0; i < 6; i++) {
598  world_offset[i] = (float)(default_is_radian || i < 3 ? world_offset[i] : world_offset[i] * RAD_DEGREE);
599  }
600  }
601  if (sizeof_data >= 314) {
602  gpio_reset_config[0] = data_fp[312];
603  gpio_reset_config[1] = data_fp[313];
604  }
605  if (sizeof_data >= 417) {
606  is_simulation_robot = data_fp[314];
607  is_collision_detection = data_fp[315];
608  collision_tool_type = data_fp[316];
609  hex_to_nfp32(&data_fp[317], collision_model_params, 6);
610 
611  for (int i = 0; i < 7; i++) {
612  voltages[i] = (fp32)bin8_to_16(&data_fp[341 + 2 * i]) / 100;
613  }
614  hex_to_nfp32(&data_fp[355], currents, 7);
615 
616  cgpio_state = data_fp[383];
617  cgpio_code = data_fp[384];
618  cgpio_input_digitals[0] = bin8_to_16(&data_fp[385]);
619  cgpio_input_digitals[1] = bin8_to_16(&data_fp[387]);
620  cgpio_output_digitals[0] = bin8_to_16(&data_fp[389]);
621  cgpio_output_digitals[1] = bin8_to_16(&data_fp[391]);
622  cgpio_intput_anglogs[0] = (fp32)(bin8_to_16(&data_fp[393])) / 4095 * 10;
623  cgpio_intput_anglogs[1] = (fp32)(bin8_to_16(&data_fp[395])) / 4095 * 10;
624  cgpio_output_anglogs[0] = (fp32)(bin8_to_16(&data_fp[397])) / 4095 * 10;
625  cgpio_output_anglogs[1] = (fp32)(bin8_to_16(&data_fp[399])) / 4095 * 10;
626  for (int i = 0; i < 8; i++) {
627  cgpio_input_conf[i] = data_fp[401 + i];
628  cgpio_output_conf[i] = data_fp[409 + i];
629  }
630  if (sizeof_data >= 433) {
631  for (int i = 0; i < 8; i++) {
632  cgpio_input_conf[i+8] = data_fp[417 + i];
633  cgpio_output_conf[i+8] = data_fp[425 + i];
634  }
635  }
636  }
637  }
638 }
639 
641  unsigned char rx_data[REPORT_BUF_SIZE];
642  unsigned char ret_data[REPORT_BUF_SIZE * 2];
643  int size = 0;
644  int num = 0;
645  int offset = 0;
646  int ret;
647  int fail_count = 0;
648  while (is_connected()) {
650  if (fail_count > 5) break;
651  if (stream_tcp_report_->is_ok() != 0) {
652  fail_count += 1;
653  size = 0;
654  num = 0;
655  offset = 0;
657  sleep_milliseconds(500);
658  continue;
659  }
660  ret = stream_tcp_report_->read_frame(rx_data);
661  fail_count = 0;
662  if (ret != 0) continue;
663  // _update(rx_data);
664  num = bin8_to_32(rx_data);
665  if (num < 4 && size <= 0) continue;
666  if (size <= 0) {
667  size = bin8_to_32(rx_data + 4);
668  bin32_to_8(size, &ret_data[0]);
669  }
670  if (num + offset < size) {
671  memcpy(ret_data + offset + 4, rx_data + 4, num);
672  offset += num;
673  continue;
674  }
675  else {
676  memcpy(ret_data + offset + 4, rx_data + 4, size - offset);
677  // _update(ret_data);
678  // memcpy(ret_data + 4, rx_data + 4 + size - offset, num + offset - size);
679  // offset = num + offset - size;
680  int offset2 = size - offset;
681  while (num - offset2 >= size) {
682  memcpy(ret_data + 4, rx_data + 4 + offset2, size);
683  // _update(ret_data);
684  offset2 += size;
685  }
686  _update(ret_data);
687  memcpy(ret_data + 4, rx_data + 4 + offset2, num - offset2);
688  offset = num - offset2;
689  }
690  }
691  pool.stop();
692 }
693 
694 static void report_thread_handle_(void *arg) {
695  XArmAPI *my_this = (XArmAPI *)arg;
696  my_this->_recv_report_data();
697 }
698 
699 void XArmAPI::_sync(void) {
700  memcpy(last_used_position, position, 24);
701  memcpy(last_used_angles, angles, 28);
702 }
703 
705  int cnt = 5;
706  unsigned char version_[40];
707  int ret = -1;
708  while ((ret < 0 || ret > 2) && cnt > 0) {
709  ret = get_version(version_);
710  sleep_milliseconds(100);
711  cnt -= 1;
712  }
713  std::string v((const char *)version_);
714  std::regex pattern_new(".*(\\d+),(\\d+),(\\S+),(\\S+),.*[vV](\\d+)\\.(\\d+)\\.(\\d+)");
715  std::regex pattern(".*[vV](\\d+)\\.(\\d+)\\.(\\d+)");
716  // std::regex pattern(".*[vV](\\d+)[.](\\d+)[.](\\d+).*");
717  std::smatch result;
718  int arm_type = 0;
719  int control_type = 0;
720  if (std::regex_match(v, result, pattern_new)) {
721  auto it = result.begin();
722  sscanf(std::string(*++it).data(), "%d", &axis);
723  sscanf(std::string(*++it).data(), "%d", &device_type);
724  sscanf(std::string(*++it).substr(2, 4).data(), "%d", &arm_type);
725  sscanf(std::string(*++it).substr(2, 4).data(), "%d", &control_type);
726 
727  arm_type_is_1300_ = arm_type >= 1300;
728  control_box_type_is_1300_ = control_type >= 1300;
729 
730  sscanf(std::string(*++it).data(), "%d", &major_version_number_);
731  sscanf(std::string(*++it).data(), "%d", &minor_version_number_);
732  sscanf(std::string(*++it).data(), "%d", &revision_version_number_);
733  }
734  else if (std::regex_match(v, result, pattern)) {
735  auto it = result.begin();
736  sscanf(std::string(*++it).data(), "%d", &major_version_number_);
737  sscanf(std::string(*++it).data(), "%d", &minor_version_number_);
738  sscanf(std::string(*++it).data(), "%d", &revision_version_number_);
739  }
740  else {
741  std::vector<std::string> tmpList = split(v, "-");
742  int size = tmpList.size();
743  if (size >= 3) {
744  int year = atoi(tmpList[size - 3].c_str());
745  int month = atoi(tmpList[size - 2].c_str());
746  if (year < 2019) is_old_protocol_ = true;
747  else if (year == 2019) {
748  is_old_protocol_ = month >= 2 ? false : true;
749  }
750  else {
751  is_old_protocol_ = false;
752  }
753  }
754  if (is_old_protocol_) {
758  }
759  else {
763  }
764  }
768  printf("is_old_protocol: %d\n", is_old_protocol_);
769  printf("version_number: %d.%d.%d\n", major_version_number_, minor_version_number_, revision_version_number_);
770  printf("hardware_type: %d, control_box_type: %d\n", arm_type, control_type);
771  if (check_robot_sn_) {
772  cnt = 5;
773  int err_warn[2];
774  ret = -1;
775  while ((ret < 0 || ret > 2) && cnt > 0 && warn_code == 0) {
776  ret = get_robot_sn(version_);
777  get_err_warn_code(err_warn);
778  sleep_milliseconds(100);
779  cnt -= 1;
780  }
781  printf("robot_sn: %s\n", sn);
782  }
783 }
784 
786  if (check_is_pause_ && state == 3) {
787  std::unique_lock<std::mutex> locker(mutex_);
788  cond_.wait(locker, [this] { return state != 3 || !is_connected(); });
789  locker.unlock();
790  }
791 }
792 
794  if (!check_cmdnum_limit_) return 0;
795  while (cmd_num >= max_cmdnum_) {
796  if (!is_connected()) return API_CODE::NOT_CONNECTED;
797  if (error_code != 0) return API_CODE::HAS_ERROR;
798  if (state == 4 || state == 5) return API_CODE::NOT_READY;
799  sleep_milliseconds(50);
800  }
801  return 0;
802 }
803 
804 int XArmAPI::_check_code(int code, bool is_move_cmd) {
805  if (is_move_cmd) {
806  return ((code == 0 || code == UXBUS_STATE::WAR_CODE) && core->state_is_ready) ? 0 : !core->state_is_ready ? UXBUS_STATE::STATE_NOT_READY : code;
807  }
808  else {
809  return (code == 0 || code == UXBUS_STATE::ERR_CODE || code == UXBUS_STATE::WAR_CODE || code == UXBUS_STATE::STATE_NOT_READY) ? 0 : code;
810  }
811 }
812 
813 bool XArmAPI::_version_is_ge(int major, int minor, int revision) {
815  unsigned char version_[40];
816  get_version(version_);
817 
818  std::string v((const char *)version_);
819  std::regex pattern_new(".*(\\d+),(\\d+),(\\S+),(\\S+),.*[vV](\\d+)\\.(\\d+)\\.(\\d+)");
820  std::regex pattern(".*[vV](\\d+)\\.(\\d+)\\.(\\d+)");
821  std::smatch result;
822  int arm_type = 0;
823  int control_type = 0;
824  if (std::regex_match(v, result, pattern_new)) {
825  auto it = result.begin();
826  sscanf(std::string(*++it).data(), "%d", &axis);
827  sscanf(std::string(*++it).data(), "%d", &device_type);
828  sscanf(std::string(*++it).substr(2).data(), "%d", &arm_type);
829  sscanf(std::string(*++it).substr(2).data(), "%d", &control_type);
830 
831  arm_type_is_1300_ = arm_type >= 1300;
832  control_box_type_is_1300_ = control_type >= 1300;
833 
834  sscanf(std::string(*++it).data(), "%d", &major_version_number_);
835  sscanf(std::string(*++it).data(), "%d", &minor_version_number_);
836  sscanf(std::string(*++it).data(), "%d", &revision_version_number_);
837  }
838  else if (std::regex_match(v, result, pattern)) {
839  auto it = result.begin();
840  sscanf(std::string(*++it).data(), "%d", &major_version_number_);
841  sscanf(std::string(*++it).data(), "%d", &minor_version_number_);
842  sscanf(std::string(*++it).data(), "%d", &revision_version_number_);
843  }
844  else {
845  std::vector<std::string> tmpList = split(v, "-");
846  int size = tmpList.size();
847  if (size >= 3) {
848  int year = atoi(tmpList[size - 3].c_str());
849  int month = atoi(tmpList[size - 2].c_str());
850  if (year < 2019) is_old_protocol_ = true;
851  else if (year == 2019) {
852  is_old_protocol_ = month >= 2 ? false : true;
853  }
854  else {
855  is_old_protocol_ = false;
856  }
857  }
858  if (is_old_protocol_) {
862  }
863  else {
867  }
868  }
872  }
873  return major_version_number_ > major || (major_version_number_ == major && minor_version_number_ > minor) || (major_version_number_ == major && minor_version_number_ == minor && revision_version_number_ >= revision);
874 }
875 
876 int XArmAPI::connect(const std::string &port) {
877  if (is_connected()) return 0;
878  if (port != "" && port != port_) {
879  port_ = port;
880  }
881  if (port_ == "") {
882  printf("can not connect to port/ip: %s\n", port_.data());
884  }
885  // std::regex pattern("(\\d|\\d{1,2}|(1\\d{1,2})|2[0-5]{1,2})[.](\\d|\\d{1,2}|(1\\d{1,2})|2[0-5]{1,2})[.](\\d|\\d{1,2}|(1\\d{1,2})|2[0-5]{1,2})[.](\\d|\\d{1,2}|(1\\d{1,2})|2[0-5]{1,2})");
886  std::regex pattern("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.]){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)");
887  is_ready_ = true;
888  if (port_ == "localhost" || std::regex_match(port_, pattern)) {
889  is_tcp_ = true;
890  stream_tcp_ = new SocketPort((char *)port_.data(), XARM_CONF::TCP_PORT_CONTROL, 3, 128);
891  if (stream_tcp_->is_ok() != 0) {
892  printf("Error: Tcp control connection failed\n");
893  return -2;
894  }
896  printf("Tcp control connection successful\n");
897 
898  sleep_milliseconds(200);
899  _check_version();
900 
902  if (stream_tcp_report_->is_ok() != 0) {
904  printf("Error: Tcp report connection failed\n");
905  return -3;
906  }
908  printf("Tcp report connection successful\n");
909  // report_thread_ = thread_init(report_thread_handle_, this);
910  report_thread_ = std::thread(report_thread_handle_, this);
911  report_thread_.detach();
912 
913  // stream_tcp_report_ = new SocketPort(server_ip, XARM_CONF::TCP_PORT_REPORT_NORM, 5, REPORT_BUF_SIZE);
914  // stream_tcp_report_ = new SocketPort(server_ip, XARM_CONF::TCP_PORT_REPORT_RICH, 5, REPORT_BUF_SIZE);
915  // stream_tcp_report_ = new SocketPort(server_ip, XARM_CONF::TCP_PORT_REPORT_DEVL, 5, REPORT_BUF_SIZE);
916  }
917  else {
918  is_tcp_ = false;
919  stream_ser_ = new SerialPort((const char *)port_.data(), XARM_CONF::SERIAL_BAUD, 3, 128);
921  sleep_milliseconds(200);
922  _check_version();
923  }
924  if (cmd_timeout_ > 0)
926 
927  return 0;
928 }
929 
931  if (stream_tcp_ != NULL) {
933  }
934  if (stream_ser_ != NULL) {
936  }
937  if (stream_tcp_report_ != NULL) {
939  }
940  is_ready_ = false;
941 }
942 
944  if (!is_connected()) return API_CODE::NOT_CONNECTED;
945  assert(timeout > 0);
946  cmd_timeout_ = timeout;
947  return core->set_timeout(timeout);
948 }
949 
950 int XArmAPI::get_version(unsigned char version_[40]) {
951  if (!is_connected()) return API_CODE::NOT_CONNECTED;
952  return core->get_version(version_);
953 }
954 
955 int XArmAPI::get_robot_sn(unsigned char robot_sn[40]) {
956  if (!is_connected()) return API_CODE::NOT_CONNECTED;
957  int ret = core->get_robot_sn(robot_sn);
958  if (ret == 0 || ret == 1 || ret == 2) {
959  memcpy(sn, robot_sn, 40);
960  }
961  return ret;
962 }
963 
964 int XArmAPI::shutdown_system(int value) {
965  if (!is_connected()) return API_CODE::NOT_CONNECTED;
966  return core->shutdown_system(value);
967 }
968 
969 int XArmAPI::get_state(int *state_) {
970  if (!is_connected()) return API_CODE::NOT_CONNECTED;
971  int ret = core->get_state(state_);
972  if (ret == 0 || ret == API_CODE::ERR_CODE || ret == API_CODE::WAR_CODE) {
973  state = *state_;
974  }
975  return ret;
976 }
977 
978 int XArmAPI::get_cmdnum(int *cmdnum_) {
979  if (!is_connected()) return API_CODE::NOT_CONNECTED;
980  int ret = core->get_cmdnum(cmdnum_);
981  if (ret == 0 || ret == API_CODE::ERR_CODE || ret == API_CODE::WAR_CODE) {
982  cmd_num = *cmdnum_;
983  }
984  return ret;
985 }
986 
987 int XArmAPI::get_err_warn_code(int err_warn[2]) {
988  if (!is_connected()) return API_CODE::NOT_CONNECTED;
989  int ret = core->get_err_code(err_warn);
990  if (ret == 0 || ret == API_CODE::ERR_CODE || ret == API_CODE::WAR_CODE) {
991  error_code = err_warn[0];
992  warn_code = err_warn[1];
993  }
994  return ret;
995 }
996 
998  if (!is_connected()) return API_CODE::NOT_CONNECTED;
999  int ret = core->get_tcp_pose(pose);
1000  if (ret >= 0) {
1001  for (int i = 0; i < 6; i++) {
1002  if (!default_is_radian && i > 2) {
1003  pose[i] = (float)(pose[i] * RAD_DEGREE);
1004  }
1005  position[i] = pose[i];
1006  }
1007  }
1008  return ret;
1009 }
1010 
1012  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1013  int ret = core->get_joint_pose(angs);
1014  if (ret >= 0) {
1015  for (int i = 0; i < 7; i++) {
1016  if (!default_is_radian) {
1017  angs[i] = (float)(angs[i] * RAD_DEGREE);
1018  }
1019  angles[i] = angs[i];
1020  }
1021  }
1022  return ret;
1023 }
1024 
1025 int XArmAPI::motion_enable(bool enable, int servo_id) {
1026  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1027  int ret = core->motion_en(servo_id, int(enable));
1028  get_state(&state);
1029  if (state == 4 || state == 5) {
1030  sleep_finish_time_ = 0;
1031  if (is_ready_) {
1032  printf("[motion_enable], xArm is not ready to move\n");
1033  }
1034  is_ready_ = false;
1035  }
1036  else {
1037  if (!is_ready_) {
1038  printf("[motion_enable], xArm is ready to move\n");
1039  }
1040  is_ready_ = true;
1041  }
1042  return ret;
1043 }
1044 
1045 int XArmAPI::set_state(int state_) {
1046  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1047  int ret = core->set_state(state_);
1048  get_state(&state);
1049  if (state == 4 || state == 5) {
1050  // is_sync_ = false;
1051  sleep_finish_time_ = 0;
1052  if (is_ready_) {
1053  printf("[set_state], xArm is not ready to move\n");
1054  }
1055  is_ready_ = false;
1056  }
1057  else {
1058  if (!is_ready_) {
1059  printf("[set_state], xArm is ready to move\n");
1060  }
1061  is_ready_ = true;
1062  }
1063  return ret;
1064 }
1065 
1066 int XArmAPI::set_mode(int mode_) {
1067  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1068  return core->set_mode(mode_);
1069 }
1070 
1071 int XArmAPI::set_servo_attach(int servo_id) {
1072  // if (!is_connected()) return API_CODE::NOT_CONNECTED;
1073  // return core->set_brake(servo_id, 0);
1074  return motion_enable(true, servo_id);
1075 }
1076 
1077 int XArmAPI::set_servo_detach(int servo_id) {
1078  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1079  return core->set_brake(servo_id, 1);
1080 }
1081 
1083  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1084  int ret = core->clean_err();
1085  get_state(&state);
1086  if (state == 4 || state == 5) {
1087  sleep_finish_time_ = 0;
1088  if (is_ready_) {
1089  printf("[clean_error], xArm is not ready to move\n");
1090  }
1091  is_ready_ = false;
1092  }
1093  else {
1094  if (!is_ready_) {
1095  printf("[clean_error], xArm is ready to move\n");
1096  }
1097  is_ready_ = true;
1098  }
1099  return ret;
1100 }
1101 
1103  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1104  return core->clean_war();
1105 }
1106 
1108  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1109  int wait_code = _wait_until_cmdnum_lt_max();
1110  if (wait_code != 0) return wait_code;
1111  int ret = core->sleep_instruction(sltime);
1113  sleep_finish_time_ = get_system_time() + (long long)(sltime * 1000);
1114  }
1115  else {
1116  sleep_finish_time_ = sleep_finish_time_ + (long long)(sltime * 1000);
1117  }
1118  return ret;
1119 }
1120 
1122  long long start_time = get_system_time();
1123  long long expired = timeout <= 0 ? 0 : (get_system_time() + timeout * 1000 + sleep_finish_time_ > start_time ? sleep_finish_time_ : 0);
1124  int cnt = 0;
1125  int state_;
1126  int err_warn[2];
1127  int ret = get_state(&state_);
1128  int max_cnt = (ret == 0 && state_ == 1) ? 2 : 10;
1129  while (timeout <= 0 || get_system_time() < expired) {
1130  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1131  if (get_system_time() - last_report_time_ > 400) {
1132  get_state(&state_);
1133  get_err_warn_code(err_warn);
1134  }
1135  if (error_code != 0) {
1136  return API_CODE::ERR_CODE;
1137  }
1138  if (state == 4 || state == 5) {
1139  ret = get_state(&state_);
1140  if (ret != 0 || (state_ != 4 && state_ != 5)) {
1141  sleep_milliseconds(20);
1142  continue;
1143  }
1144  sleep_finish_time_ = 0;
1145  return API_CODE::EMERGENCY_STOP;
1146  }
1147  if (get_system_time() < sleep_finish_time_ || state == 3) {
1148  sleep_milliseconds(20);
1149  cnt = 0;
1150  continue;
1151  }
1152  if (state != 1) {
1153  cnt += 1;
1154  if (cnt >= max_cnt) {
1155  ret = get_state(&state_);
1156  get_err_warn_code(err_warn);
1157  if (ret == 0 && state_ != 1) {
1158  return 0;
1159  }
1160  else {
1161  cnt = 0;
1162  }
1163  }
1164  }
1165  else {
1166  cnt = 0;
1167  }
1168  sleep_milliseconds(50);
1169  }
1171 }
1172 
1173 int XArmAPI::set_position(fp32 pose[6], fp32 radius, fp32 speed, fp32 acc, fp32 mvtime, bool wait, fp32 timeout) {
1174  _check_is_pause();
1175  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1176  int wait_code = _wait_until_cmdnum_lt_max();
1177  if (wait_code != 0) return wait_code;
1178  int ret = 0;
1179  last_used_tcp_speed = speed > 0 ? speed : last_used_tcp_speed;
1180  last_used_tcp_acc = acc > 0 ? acc : last_used_tcp_acc;
1181  fp32 mvpose[6];
1182  for (int i = 0; i < 6; i++) {
1183  last_used_position[i] = pose[i];
1184  mvpose[i] = (float)(default_is_radian || i < 3 ? last_used_position[i] : last_used_position[i] / RAD_DEGREE);
1185  }
1186 
1187  if (radius >= 0) {
1188  ret = core->move_lineb(mvpose, last_used_tcp_speed, last_used_tcp_acc, mvtime, radius);
1189  }
1190  else {
1191  ret = core->move_line(mvpose, last_used_tcp_speed, last_used_tcp_acc, mvtime);
1192  }
1193  ret = _check_code(ret, true);
1194  if (wait && ret == 0) {
1195  ret = _wait_move(timeout);
1196  _sync();
1197  }
1198 
1199  return ret;
1200 }
1201 
1202 int XArmAPI::set_position(fp32 pose[6], fp32 radius, bool wait, fp32 timeout) {
1203  return set_position(pose, radius, 0, 0, 0, wait, timeout);
1204 }
1205 
1206 int XArmAPI::set_position(fp32 pose[6], bool wait, fp32 timeout) {
1207  return set_position(pose, -1, 0, 0, 0, wait, timeout);
1208 }
1209 
1210 int XArmAPI::set_tool_position(fp32 pose[6], fp32 speed, fp32 acc, fp32 mvtime, bool wait, fp32 timeout) {
1211  _check_is_pause();
1212  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1213  int wait_code = _wait_until_cmdnum_lt_max();
1214  if (wait_code != 0) return wait_code;
1215  last_used_tcp_speed = speed > 0 ? speed : last_used_tcp_speed;
1216  last_used_tcp_acc = acc > 0 ? acc : last_used_tcp_acc;
1217  fp32 mvpose[6];
1218  for (int i = 0; i < 6; i++) {
1219  mvpose[i] = (float)(default_is_radian || i < 3 ? pose[i] : pose[i] / RAD_DEGREE);
1220  }
1221  int ret = core->move_line_tool(mvpose, last_used_tcp_speed, last_used_tcp_acc, mvtime);
1222 
1223  ret = _check_code(ret, true);
1224  if (wait && ret == 0) {
1225  ret = _wait_move(timeout);
1226  _sync();
1227  }
1228 
1229  return ret;
1230 }
1231 
1232 int XArmAPI::set_tool_position(fp32 pose[6], bool wait, fp32 timeout) {
1233  return set_tool_position(pose, 0, 0, 0, wait, timeout);
1234 }
1235 
1236 
1237 int XArmAPI::set_servo_angle(fp32 angs[7], fp32 speed, fp32 acc, fp32 mvtime, bool wait, fp32 timeout, fp32 radius) {
1238  _check_is_pause();
1239  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1240  int wait_code = _wait_until_cmdnum_lt_max();
1241  if (wait_code != 0) return wait_code;
1242  last_used_joint_speed = speed > 0 ? speed : last_used_joint_speed;
1243  last_used_joint_acc = acc > 0 ? acc : last_used_joint_acc;
1244  fp32 mvjoint[7];
1245  for (int i = 0; i < 7; i++) {
1246  last_used_angles[i] = angs[i];
1247  mvjoint[i] = (float)(default_is_radian ? last_used_angles[i] : last_used_angles[i] / RAD_DEGREE);
1248  }
1251 
1252  int ret = 0;
1253  if (_version_is_ge(1, 5, 20) && radius >= 0) {
1254  ret = core->move_jointb(mvjoint, speed_, acc_, radius);
1255  }
1256  else {
1257  ret = core->move_joint(mvjoint, speed_, acc_, mvtime);
1258  }
1259  ret = _check_code(ret, true);
1260  if (wait && ret == 0) {
1261  ret = _wait_move(timeout);
1262  _sync();
1263  }
1264  return ret;
1265 }
1266 
1267 int XArmAPI::set_servo_angle(fp32 angs[7], bool wait, fp32 timeout, fp32 radius) {
1268  return set_servo_angle(angs, 0, 0, 0, wait, timeout, radius);
1269 }
1270 
1271 int XArmAPI::set_servo_angle(int servo_id, fp32 angle, fp32 speed, fp32 acc, fp32 mvtime, bool wait, fp32 timeout, fp32 radius) {
1272  assert(servo_id > 0 && servo_id <= 7);
1273  last_used_angles[servo_id - 1] = angle;
1274  return set_servo_angle(last_used_angles, speed, acc, mvtime, wait, timeout, radius);
1275 }
1276 
1277 int XArmAPI::set_servo_angle(int servo_id, fp32 angle, bool wait, fp32 timeout, fp32 radius) {
1278  return set_servo_angle(servo_id, angle, 0, 0, 0, wait, timeout, radius);
1279 }
1280 
1282  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1283  fp32 mvjoint[7];
1284  for (int i = 0; i < 7; i++) {
1285  mvjoint[i] = (float)(default_is_radian ? angs[i] : angs[i] / RAD_DEGREE);
1286  }
1287  return core->move_servoj(mvjoint, last_used_joint_speed, last_used_joint_acc, mvtime);
1288 }
1289 
1290 int XArmAPI::set_servo_cartesian(fp32 pose[6], fp32 speed, fp32 acc, fp32 mvtime, bool is_tool_coord) {
1291  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1292  fp32 mvpose[6];
1293  for (int i = 0; i < 6; i++) {
1294  mvpose[i] = (float)(i < 3 || default_is_radian ? pose[i] : pose[i] / RAD_DEGREE);
1295  }
1296  mvtime = (float)(is_tool_coord ? 1.0 : 0.0);
1297  return core->move_servo_cartesian(mvpose, speed, acc, mvtime);
1298 }
1299 
1300 int XArmAPI::move_circle(fp32 pose1[6], fp32 pose2[6], fp32 percent, fp32 speed, fp32 acc, fp32 mvtime, bool wait, fp32 timeout) {
1301  _check_is_pause();
1302  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1303  int wait_code = _wait_until_cmdnum_lt_max();
1304  if (wait_code != 0) return wait_code;
1305  last_used_tcp_speed = speed > 0 ? speed : last_used_tcp_speed;
1306  last_used_tcp_acc = acc > 0 ? acc : last_used_tcp_acc;
1307  fp32 pose_1[6];
1308  fp32 pose_2[6];
1309  for (int i = 0; i < 6; i++) {
1310  pose_1[i] = (float)(default_is_radian || i < 3 ? pose1[i] : pose1[i] / RAD_DEGREE);
1311  pose_2[i] = (float)(default_is_radian || i < 3 ? pose2[i] : pose2[i] / RAD_DEGREE);
1312  }
1313  int ret = core->move_circle(pose_1, pose_2, last_used_tcp_speed, last_used_tcp_acc, mvtime, percent);
1314  ret = _check_code(ret, true);
1315  if (wait && ret == 0) {
1316  ret = _wait_move(timeout);
1317  _sync();
1318  }
1319 
1320  return ret;
1321 }
1322 
1323 int XArmAPI::move_gohome(fp32 speed, fp32 acc, fp32 mvtime, bool wait, fp32 timeout) {
1324  _check_is_pause();
1325  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1326  int wait_code = _wait_until_cmdnum_lt_max();
1327  if (wait_code != 0) return wait_code;
1328  fp32 speed_ = (float)(default_is_radian ? speed : speed / RAD_DEGREE);
1329  fp32 acc_ = (float)(default_is_radian ? acc : acc / RAD_DEGREE);
1330  speed_ = speed_ > 0 ? speed_ : (float)0.8726646259971648; // 50 °/s
1331  acc_ = acc_ > 0 ? acc_ : (float)17.453292519943297; // 1000 °/s^2
1332  int ret = core->move_gohome(speed_, acc_, mvtime);
1333  ret = _check_code(ret, true);
1334  if (wait && ret == 0) {
1335  ret = _wait_move(timeout);
1336  _sync();
1337  }
1338 
1339  return ret;
1340 }
1341 
1342 int XArmAPI::move_gohome(bool wait, fp32 timeout) {
1343  return move_gohome(0, 0, 0, wait, timeout);
1344 }
1345 
1346 void XArmAPI::reset(bool wait, fp32 timeout) {
1347  int err_warn[2];
1348  int state_;
1349  if (!is_tcp_) {
1350  get_err_warn_code(err_warn);
1351  get_state(&state_);
1352  }
1353  if (warn_code != 0) {
1354  clean_warn();
1355  }
1356  if (error_code != 0) {
1357  clean_error();
1358  motion_enable(true, 8);
1359  set_mode(0);
1360  set_state(0);
1361  }
1362  if (!is_ready_) {
1363  motion_enable(true, 8);
1364  set_mode(0);
1365  set_state(0);
1366  }
1367  move_gohome(wait, timeout);
1368 }
1369 
1371  long long start_time = get_system_time();
1372  while (state != 4 && state != 5 && get_system_time() - start_time < 3000) {
1373  set_state(4);
1374  sleep_milliseconds(100);
1375  }
1376  sleep_finish_time_ = 0;
1377  // motion_enable(true, 8);
1378  // while ((state == 0 || state == 3 || state == 4) && get_system_time() - start_time < 3000) {
1379  // set_state(0);
1380  // sleep_milliseconds(100);
1381  // }
1382 }
1383 
1384 int XArmAPI::get_inverse_kinematics(fp32 source_pose[6], fp32 target_angles[7]) {
1385  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1386  fp32 pose[6];
1387  for (int i = 0; i < 6; i++) {
1388  pose[i] = (float)(default_is_radian || i < 3 ? source_pose[i] : source_pose[i] / RAD_DEGREE);
1389  }
1390  fp32 angs[7];
1391  int ret = core->get_ik(pose, angs);
1392  for (int i = 0; i < 7; i++) {
1393  target_angles[i] = (float)(default_is_radian ? angs[i] : angs[i] * RAD_DEGREE);
1394  }
1395  return ret;
1396 }
1397 
1398 int XArmAPI::get_forward_kinematics(fp32 source_angles[7], fp32 target_pose[6]) {
1399  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1400  fp32 angs[7];
1401  for (int i = 0; i < 7; i++) {
1402  angs[i] = (float)(default_is_radian ? source_angles[i] : source_angles[i] / RAD_DEGREE);
1403  }
1404  fp32 pose[6];
1405  int ret = core->get_fk(angs, pose);
1406  for (int i = 0; i < 6; i++) {
1407  target_pose[i] = (float)(default_is_radian || i < 3 ? pose[i] : pose[i] * RAD_DEGREE);
1408  }
1409  return ret;
1410 }
1411 
1412 int XArmAPI::is_tcp_limit(fp32 source_pose[6], int *limit) {
1413  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1414  fp32 pose[6];
1415  for (int i = 0; i < 6; i++) {
1416  pose[i] = (float)(default_is_radian || i < 3 ? source_pose[i] : source_pose[i] / RAD_DEGREE);
1417  }
1418  return core->is_tcp_limit(pose, limit);
1419 }
1420 
1421 int XArmAPI::is_joint_limit(fp32 source_angles[7], int *limit) {
1422  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1423  fp32 angs[7];
1424  for (int i = 0; i < 7; i++) {
1425  angs[i] = (float)(default_is_radian ? source_angles[i] : source_angles[i] / RAD_DEGREE);
1426  }
1427  return core->is_joint_limit(angs, limit);
1428 }
1429 
1431  _check_is_pause();
1432  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1433  return core->set_collis_sens(sensitivity);
1434 }
1435 
1436 int XArmAPI::set_teach_sensitivity(int sensitivity) {
1437  _check_is_pause();
1438  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1439  return core->set_teach_sens(sensitivity);
1440 }
1441 
1443  _check_is_pause();
1444  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1445  return core->set_gravity_dir(gravity_dir);
1446 }
1447 
1449  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1450  return core->clean_conf();
1451 }
1452 
1454  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1455  return core->save_conf();
1456 }
1457 
1458 int XArmAPI::set_tcp_offset(fp32 pose_offset[6]) {
1459  _check_is_pause();
1460  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1461  fp32 offset[6];
1462  for (int i = 0; i < 6; i++) {
1463  offset[i] = (float)(default_is_radian || i < 3 ? pose_offset[i] : pose_offset[i] / RAD_DEGREE);
1464  }
1466  return core->set_tcp_offset(offset);
1467 }
1468 
1469 int XArmAPI::set_tcp_load(fp32 weight, fp32 center_of_gravity[3]) {
1470  _check_is_pause();
1471  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1472  int wait_code = _wait_until_cmdnum_lt_max();
1473  if (wait_code != 0) return wait_code;
1474  float _gravity[3];
1475  if (compare_version(version_number, new int[3]{ 0, 2, 0 })) {
1476  _gravity[0] = center_of_gravity[0];
1477  _gravity[1] = center_of_gravity[1];
1478  _gravity[2] = center_of_gravity[2];
1479  }
1480  else {
1481  _gravity[0] = (float)(center_of_gravity[0] / 1000.0);
1482  _gravity[1] = (float)(center_of_gravity[1] / 1000.0);
1483  _gravity[2] = (float)(center_of_gravity[2] / 1000.0);
1484  }
1485  return core->set_tcp_load(weight, _gravity);
1486 }
1487 
1489  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1490  int wait_code = _wait_until_cmdnum_lt_max();
1491  if (wait_code != 0) return wait_code;
1492  return core->set_tcp_jerk(jerk);
1493 }
1494 
1496  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1497  int wait_code = _wait_until_cmdnum_lt_max();
1498  if (wait_code != 0) return wait_code;
1499  return core->set_tcp_maxacc(acc);
1500 }
1501 
1503  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1504  int wait_code = _wait_until_cmdnum_lt_max();
1505  if (wait_code != 0) return wait_code;
1506  return core->set_joint_jerk(default_is_radian ? jerk : (float)(jerk / RAD_DEGREE));
1507 }
1508 
1510  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1511  int wait_code = _wait_until_cmdnum_lt_max();
1512  if (wait_code != 0) return wait_code;
1513  return core->set_joint_maxacc(default_is_radian ? acc : (float)(acc / RAD_DEGREE));
1514 }
1515 
1517  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1518  if (_checkset_modbus_baud(2000000) != 0) return API_CODE::MODBUS_BAUD_NOT_CORRECT;
1519  int ret = core->gripper_modbus_set_en(int(enable));
1520  int err;
1521  get_gripper_err_code(&err);
1522  ret = _check_modbus_code(ret);
1523  if (ret == 0 && xarm_gripper_error_code_ == 0) gripper_is_enabled_ = true;
1525 }
1526 
1528  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1529  if (_checkset_modbus_baud(2000000) != 0) return API_CODE::MODBUS_BAUD_NOT_CORRECT;
1530  int ret = core->gripper_modbus_set_mode(mode);
1531  int err;
1532  get_gripper_err_code(&err);
1533  ret = _check_modbus_code(ret);
1535 }
1536 
1538  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1539  if (_checkset_modbus_baud(2000000) != 0) return API_CODE::MODBUS_BAUD_NOT_CORRECT;
1540  int ret = core->gripper_modbus_set_posspd(speed);
1541  int err;
1542  get_gripper_err_code(&err);
1543  ret = _check_modbus_code(ret);
1545 }
1546 
1548  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1549  if (_checkset_modbus_baud(2000000) != 0) return API_CODE::MODBUS_BAUD_NOT_CORRECT;
1550  int ret = core->gripper_modbus_get_pos(pos);
1551  int err;
1552  get_gripper_err_code(&err);
1553  ret = _check_modbus_code(ret);
1555 }
1556 
1558  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1559  if (_checkset_modbus_baud(2000000) != 0) return API_CODE::MODBUS_BAUD_NOT_CORRECT;
1560  int ret = core->gripper_modbus_get_errcode(err);
1561  ret = _check_modbus_code(ret);
1562  if (ret == 0) {
1563  if (*err < 128) {
1564  xarm_gripper_error_code_ = *err;
1565  if (xarm_gripper_error_code_ != 0)
1566  gripper_is_enabled_ = false;
1567  }
1568  }
1569  return ret;
1570 }
1571 
1573  if (gripper_version_numbers_[0] == -1 || gripper_version_numbers_[1] == -1 || gripper_version_numbers_[2] == -1) {
1574  unsigned char ver[3];
1575  get_gripper_version(ver);
1576  }
1577  return gripper_version_numbers_[0] > 3
1578  || (gripper_version_numbers_[0] == 3 && gripper_version_numbers_[1] > 4)
1580 }
1581 
1583  unsigned char val[5];
1584  int ret = core->gripper_modbus_r16s(0x0000, 1, val);
1585  ret = _check_modbus_code(ret);
1586  if (ret == 0) {
1587  *status = bin8_to_16(&val[4]);
1588  }
1589  return ret;
1590 }
1591 
1592 int XArmAPI::_check_gripper_position(fp32 target_pos, fp32 timeout) {
1593  int ret2 = 0;
1594  float last_pos = 0, pos_tmp, cur_pos;
1595  bool is_add = true;
1596  ret2 = get_gripper_position(&pos_tmp);
1597  if (ret2 == 0) {
1598  last_pos = pos_tmp;
1599  if (int(last_pos) == int(target_pos))
1600  return 0;
1601  is_add = target_pos > last_pos ? true : false;
1602  }
1603 
1604  int cnt = 0;
1605  int cnt2 = 0;
1606  int failed_cnt = 0;
1607  int code = API_CODE::WAIT_FINISH_TIMEOUT;
1608  long long expired = get_system_time() + (long long)(timeout * 1000);
1609  while (timeout <= 0 || get_system_time() < expired) {
1610  ret2 = get_gripper_position(&pos_tmp);
1612  failed_cnt = ret2 == 0 ? 0 : failed_cnt + 1;
1613  if (ret2 == 0) {
1614  cur_pos = pos_tmp;
1615  if (fabs(target_pos - cur_pos) < 1) {
1616  last_pos = cur_pos;
1617  return 0;
1618  }
1619  if (is_add) {
1620  if (cur_pos <= last_pos) {
1621  cnt += 1;
1622  }
1623  else if (cur_pos <= target_pos) {
1624  last_pos = cur_pos;
1625  cnt = 0;
1626  cnt2 = 0;
1627  }
1628  else {
1629  cnt2 += 1;
1630  if (cnt2 >= 10) {
1631  return 0;
1632  }
1633  }
1634  }
1635  else {
1636  if (cur_pos >= last_pos) {
1637  cnt += 1;
1638  }
1639  else if (cur_pos >= target_pos) {
1640  last_pos = cur_pos;
1641  cnt = 0;
1642  cnt2 = 0;
1643  }
1644  else {
1645  cnt2 += 1;
1646  if (cnt2 >= 10) {
1647  return 0;
1648  }
1649  }
1650 
1651  }
1652  if (cnt >= 8) {
1653  return 0;
1654  }
1655  }
1656  else {
1657  if (failed_cnt > 10) return API_CODE::CHECK_FAILED;
1658  }
1659  sleep_milliseconds(200);
1660  }
1661  return code;
1662 }
1663 
1665  bool start_move = false;
1666  int not_start_move_cnt = 0;
1667  int failed_cnt = 0;
1668  int ret;
1669  int status;
1670  int code = API_CODE::WAIT_FINISH_TIMEOUT;
1671  long long expired = get_system_time() + (long long)(timeout * 1000);
1672  while (timeout <= 0 || get_system_time() < expired) {
1673  ret = _get_gripper_status(&status);
1674  failed_cnt = ret == 0 ? 0 : failed_cnt + 1;
1675  if (ret == 0) {
1676  if ((status & 0x03) == 0 || (status & 0x03) == 2) {
1677  if (start_move) return 0;
1678  not_start_move_cnt += 1;
1679  if (not_start_move_cnt > 20) return 0;
1680  }
1681  else if (!start_move) {
1682  not_start_move_cnt = 0;
1683  start_move = true;
1684  }
1685  }
1686  else {
1687  if (failed_cnt > 10) return API_CODE::CHECK_FAILED;
1688  }
1689  sleep_milliseconds(100);
1690  }
1691  return code;
1692 }
1693 
1694 int XArmAPI::set_gripper_position(fp32 pos, bool wait, fp32 timeout) {
1695  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1696  bool has_error = error_code != 0;
1697  bool is_stop = state == 4 || state == 5;
1698  int code = _wait_move(NO_TIMEOUT);
1699  if (!(code == 0 || (is_stop && code == API_CODE::EMERGENCY_STOP) || (has_error && code == API_CODE::HAS_ERROR))) {
1700  return code;
1701  }
1702  if (_checkset_modbus_baud(2000000) != 0) return API_CODE::MODBUS_BAUD_NOT_CORRECT;
1703  int ret = core->gripper_modbus_set_pos(pos);
1704  int err;
1705  get_gripper_err_code(&err);
1706  ret = _check_modbus_code(ret);
1708  if (wait && ret == 0) {
1710  return _check_gripper_status(timeout);
1711  }
1712  else {
1713  return _check_gripper_position(pos, timeout);
1714  }
1715  }
1716  return ret;
1717 }
1718 
1720  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1721  if (_checkset_modbus_baud(2000000) != 0) return API_CODE::MODBUS_BAUD_NOT_CORRECT;
1722  int ret = core->gripper_modbus_clean_err();
1723  int err;
1724  get_gripper_err_code(&err);
1725  ret = _check_modbus_code(ret);
1727 }
1728 
1729 int XArmAPI::get_tgpio_digital(int *io0, int *io1) {
1730  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1731  return core->tgpio_get_digital(io0, io1);
1732 }
1733 
1734 int XArmAPI::set_tgpio_digital(int ionum, int value, float delay_sec) {
1735  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1736  int wait_code = _wait_until_cmdnum_lt_max();
1737  if (wait_code != 0) return wait_code;
1738  assert(ionum == 0 || ionum == 1);
1739  if (delay_sec > 0) {
1740  return core->tgpio_delay_set_digital(ionum + 1, value, delay_sec);
1741  }
1742  else {
1743  return core->tgpio_set_digital(ionum + 1, value);
1744  }
1745 }
1746 
1747 int XArmAPI::get_tgpio_analog(int ionum, float *value) {
1748  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1749  assert(ionum == 0 || ionum == 1);
1750  if (ionum == 0) {
1751  return core->tgpio_get_analog1(value);
1752  }
1753  else {
1754  return core->tgpio_get_analog2(value);
1755  }
1756 }
1757 
1758 int XArmAPI::get_cgpio_digital(int *digitals, int *digitals2) {
1759  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1760  int tmp;
1761  int ret = core->cgpio_get_auxdigit(&tmp);
1762  for (int i = 0; i < 8; i++) {
1763  digitals[i] = tmp >> i & 0x0001;
1764  }
1765  if (digitals2 != NULL) {
1766  for (int i = 8; i < 16; i++) {
1767  digitals2[i-8] = tmp >> i & 0x0001;
1768  }
1769  }
1770  return ret;
1771 }
1772 
1773 int XArmAPI::get_cgpio_analog(int ionum, fp32 *value) {
1774  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1775  assert(ionum == 0 || ionum == 1);
1776  if (ionum == 0) {
1777  return core->cgpio_get_analog1(value);
1778  }
1779  else {
1780  return core->cgpio_get_analog2(value);
1781  }
1782 }
1783 
1784 int XArmAPI::set_cgpio_digital(int ionum, int value, float delay_sec) {
1785  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1786  int wait_code = _wait_until_cmdnum_lt_max();
1787  if (wait_code != 0) return wait_code;
1788  assert(ionum >= 0 && ionum <= 16);
1789  if (delay_sec > 0) {
1790  return core->cgpio_delay_set_digital(ionum, value, delay_sec);
1791  }
1792  else {
1793  return core->cgpio_set_auxdigit(ionum, value);
1794  }
1795 }
1796 
1797 int XArmAPI::set_cgpio_analog(int ionum, fp32 value) {
1798  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1799  int wait_code = _wait_until_cmdnum_lt_max();
1800  if (wait_code != 0) return wait_code;
1801  assert(ionum == 0 || ionum == 1);
1802  if (ionum == 0) {
1803  return core->cgpio_set_analog1(value);
1804  }
1805  else {
1806  return core->cgpio_set_analog2(value);
1807  }
1808 }
1809 
1811  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1812  assert(ionum >= 0 && ionum <= 16);
1813  return core->cgpio_set_infun(ionum, fun);
1814 }
1815 
1817  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1818  assert(ionum >= 0 && ionum <= 16);
1819  return core->cgpio_set_outfun(ionum, fun);
1820 }
1821 
1822 int XArmAPI::get_cgpio_state(int *state_, int *digit_io, fp32 *analog, int *input_conf, int *output_conf, int *input_conf2, int *output_conf2) {
1823  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1824  return core->cgpio_get_state(state_, digit_io, analog, input_conf, output_conf, input_conf2, output_conf2);
1825 }
1826 
1828  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1829  return core->set_reduced_mode(int(on));
1830 }
1831 
1833  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1834  return core->set_reduced_linespeed(speed);
1835 }
1836 
1838  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1839  return core->set_reduced_jointspeed(default_is_radian ? speed : (float)(speed / RAD_DEGREE));
1840 }
1841 
1843  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1844  return core->get_reduced_mode(mode);
1845 }
1846 
1847 int XArmAPI::get_reduced_states(int *on, int *xyz_list, float *tcp_speed, float *joint_speed, float jrange[14], int *fense_is_on, int *collision_rebound_is_on) {
1848  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1849  int ret = core->get_reduced_states(on, xyz_list, tcp_speed, joint_speed, jrange, fense_is_on, collision_rebound_is_on, _version_is_ge() ? 79 : 21);
1850  if (!default_is_radian) {
1851  *joint_speed = (float)(*joint_speed * RAD_DEGREE);
1852  }
1853  if (_version_is_ge()) {
1854  if (jrange != NULL && !default_is_radian) {
1855  for (int i = 0; i < 14; i++) {
1856  jrange[i] = (float)(jrange[i] * RAD_DEGREE);
1857  }
1858  }
1859  }
1860  return ret;
1861 }
1862 
1864  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1865  return core->set_xyz_limits(boundary);
1866 }
1867 
1868 int XArmAPI::set_reduced_joint_range(float jrange[14]) {
1869  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1870  float joint_range[14];
1871  for (int i = 0; i < 14; i++) {
1872  joint_range[i] = default_is_radian ? jrange[i] : (float)(jrange[i] / RAD_DEGREE);
1873  }
1874  return core->set_reduced_jrange(joint_range);
1875 }
1876 
1878  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1879  return core->set_fense_on(int(on));
1880 }
1881 
1883  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1884  return core->set_collis_reb(int(on));
1885 }
1886 
1887 int XArmAPI::set_world_offset(float pose_offset[6]) {
1888  _check_is_pause();
1889  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1890  fp32 offset[6];
1891  for (int i = 0; i < 6; i++) {
1892  offset[i] = default_is_radian || i < 3 ? pose_offset[i] : (float)(pose_offset[i] / RAD_DEGREE);
1893  }
1894  return core->set_world_offset(offset);
1895 }
1896 
1898  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1899  return core->set_record_traj(1);
1900 }
1901 
1902 int XArmAPI::stop_record_trajectory(char* filename) {
1903  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1904  int ret = core->set_record_traj(0);
1905  if (ret == 0 && filename != NULL) {
1906  int ret2 = save_record_trajectory(filename, 10);
1907  return ret2;
1908  }
1909  return ret;
1910 }
1911 
1912 int XArmAPI::save_record_trajectory(char* filename, float timeout) {
1913  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1914  int ret = core->save_traj(filename);
1915  if (ret == 0) {
1916  int ret2 = 0;
1917  int status = 0;
1918  long long start_time = get_system_time();
1919  while (get_system_time() - start_time < timeout * 1000) {
1920  ret2 = get_trajectory_rw_status(&status);
1921  if (ret2 == 0) {
1922  if (status == TRAJ_STATE::IDLE) {
1923  return API_CODE::TRAJ_RW_FAILED;
1924  }
1925  else if (status == TRAJ_STATE::SAVE_SUCCESS) {
1926  return 0;
1927  }
1928  else if (status == TRAJ_STATE::SAVE_FAIL) {
1929  return API_CODE::TRAJ_RW_FAILED;
1930  }
1931  }
1932  sleep_milliseconds(100);
1933  }
1934  return API_CODE::TRAJ_RW_TOUT;
1935  }
1936  return ret;
1937 }
1938 
1939 int XArmAPI::load_trajectory(char* filename, float timeout) {
1940  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1941  int ret = core->load_traj(filename);
1942  if (ret == 0) {
1943  int ret2 = 0;
1944  int status = 0;
1945  long long start_time = get_system_time();
1946  while (get_system_time() - start_time < timeout * 1000) {
1947  ret2 = get_trajectory_rw_status(&status);
1948  if (ret2 == 0) {
1949  if (status == TRAJ_STATE::IDLE) {
1950  return API_CODE::TRAJ_RW_FAILED;
1951  }
1952  else if (status == TRAJ_STATE::LOAD_SUCCESS) {
1953  return 0;
1954  }
1955  else if (status == TRAJ_STATE::LOAD_FAIL) {
1956  return API_CODE::TRAJ_RW_FAILED;
1957  }
1958  }
1959  sleep_milliseconds(100);
1960  }
1961  return API_CODE::TRAJ_RW_TOUT;
1962  }
1963  return ret;
1964 }
1965 
1966 int XArmAPI::playback_trajectory(int times, char* filename, bool wait, int double_speed) {
1967  if (!is_connected()) return API_CODE::NOT_CONNECTED;
1968  int ret = 0;
1969  if (filename != NULL) {
1970  ret = load_trajectory(filename, 10);
1971  if (ret != 0) {
1972  return ret;
1973  }
1974  }
1975  if (state == 4) return API_CODE::NOT_READY;
1976  ret = core->playback_traj(times, double_speed);
1977  if (ret == 0 && wait) {
1978  long long start_time = get_system_time();
1979  while (state != 1) {
1980  if (state == 4) return API_CODE::NOT_READY;
1981  if (get_system_time() - start_time > 5000) return API_CODE::TRAJ_PLAYBACK_TOUT;
1982  sleep_milliseconds(100);
1983  }
1984  int max_count = int((get_system_time() - start_time) * 100);
1985  max_count = max_count > 10 ? max_count : 10;
1986  start_time = get_system_time();
1987  while (mode != 11) {
1988  if (state == 1) {
1989  start_time = get_system_time();
1990  sleep_milliseconds(100);
1991  continue;
1992  }
1993  if (state == 4) {
1994  return API_CODE::NOT_READY;
1995  }
1996  if (get_system_time() - start_time > 5000) {
1998  }
1999  sleep_milliseconds(100);
2000  }
2001  sleep_milliseconds(100);
2002  int cnt = 0;
2003  while (state != 4) {
2004  if (state == 2) {
2005  if (times == 1) break;
2006  cnt += 1;
2007  }
2008  else {
2009  cnt = 0;
2010  }
2011  if (cnt > max_count) break;
2012  sleep_milliseconds(100);
2013  }
2014  if (state != 4) {
2015  set_mode(0);
2016  set_state(0);
2017  }
2018  }
2019  return ret;
2020 }
2021 
2023  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2024  return core->get_traj_rw_status(status);
2025 }
2026 
2027 template<typename callable_vector, typename callable>
2028 inline int XArmAPI::_register_event_callback(callable_vector&& callbacks, callable&& callback) {
2029  if (callback == NULL) return API_CODE::NOT_CONNECTED;
2030  for (size_t i = 0; i < callbacks.size(); i++) {
2031  if (callbacks[i] == callback) return 1;
2032  }
2033  callbacks.push_back(callback);
2034  return 0;
2035 }
2036 
2037 template<typename callable_vector, typename callable>
2038 inline int XArmAPI::_release_event_callback(callable_vector&& callbacks, callable&& callback) {
2039  if (callback == NULL) {
2040  callbacks.clear();
2041  return 0;
2042  }
2043  for (size_t i = 0; i < callbacks.size(); i++) {
2044  if (callbacks[i] == callback) {
2045  callbacks.erase(callbacks.begin() + i);
2046  return 0;
2047  }
2048  }
2049  return -1;
2050 }
2051 
2052 int XArmAPI::register_report_location_callback(void(*callback)(const fp32 *pose, const fp32 *angles)) {
2054 }
2055 
2056 int XArmAPI::register_connect_changed_callback(void(*callback)(bool connected, bool reported)) {
2058 }
2059 
2062 }
2063 
2066 }
2067 
2068 int XArmAPI::register_mtable_mtbrake_changed_callback(void(*callback)(int mtable, int mtbrake)) {
2070 }
2071 
2072 int XArmAPI::register_error_warn_changed_callback(void(*callback)(int err_code, int warn_code)) {
2074 }
2075 
2076 int XArmAPI::register_cmdnum_changed_callback(void(*callback)(int cmdnum)) {
2078 }
2079 
2080 int XArmAPI::register_temperature_changed_callback(void(*callback)(const fp32 *temps)) {
2082 }
2083 
2086 }
2087 
2088 int XArmAPI::release_report_location_callback(void(*callback)(const fp32 *pose, const fp32 *angles)) {
2090 }
2091 
2092 int XArmAPI::release_connect_changed_callback(void(*callback)(bool connected, bool reported)) {
2094 }
2095 
2098 }
2099 
2102 }
2103 
2104 int XArmAPI::release_mtable_mtbrake_changed_callback(void(*callback)(int mtable, int mtbrake)) {
2106 }
2107 
2108 int XArmAPI::release_error_warn_changed_callback(void(*callback)(int err_code, int warn_code)) {
2110 }
2111 
2112 int XArmAPI::release_cmdnum_changed_callback(void(*callback)(int cmdnum)) {
2114 }
2115 
2116 int XArmAPI::release_temperature_changed_callback(void(*callback)(const fp32 *temps)) {
2118 }
2121 }
2122 
2124  int io1;
2125  return get_tgpio_digital(val, &io1);
2126 }
2127 
2128 int XArmAPI::set_suction_cup(bool on, bool wait, float timeout, float delay_sec) {
2129  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2130  int wait_code = _wait_until_cmdnum_lt_max();
2131  if (wait_code != 0) return wait_code;
2132  int code1, code2;
2133  if (on) {
2134  code1 = set_tgpio_digital(0, 1, delay_sec);
2135  code2 = set_tgpio_digital(1, 0, delay_sec);
2136  }
2137  else {
2138  code1 = set_tgpio_digital(0, 0, delay_sec);
2139  code2 = set_tgpio_digital(1, 1, delay_sec);
2140  }
2141  int code = code1 == 0 ? code2 : code1;
2142  if (code == 0 && wait) {
2143  long long start_time = get_system_time();
2144  int val, ret;
2146  while (get_system_time() - start_time < timeout * 1000) {
2147  ret = get_suction_cup(&val);
2148  if (ret == API_CODE::ERR_CODE) {
2149  code = API_CODE::ERR_CODE;
2150  break;
2151  }
2152  if (ret == 0) {
2153  if (on && val == 1) {
2154  code = 0;
2155  break;
2156  }
2157  if (!on && val == 0) {
2158  code = 0;
2159  break;
2160  }
2161  }
2162  sleep_milliseconds(100);
2163  }
2164  }
2165 
2166  return code;
2167 }
2168 
2169 int XArmAPI::get_gripper_version(unsigned char versions[3]) {
2170  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2171  unsigned char val1[5], val2[5], val3[5];
2172  int code;
2173  versions[0] = 0;
2174  versions[1] = 0;
2175  versions[2] = 0;
2176  int ret1 = core->gripper_modbus_r16s(0x0801, 1, val1);
2177  int ret2 = core->gripper_modbus_r16s(0x0802, 1, val2);
2178  int ret3 = core->gripper_modbus_r16s(0x0803, 1, val3);
2179  ret1 = _check_modbus_code(ret1);
2180  ret2 = _check_modbus_code(ret2);
2181  ret3 = _check_modbus_code(ret3);
2182  if (ret1 == 0) {
2183  versions[0] = (unsigned char)bin8_to_16(&val1[4]);
2185  }
2186  else { code = ret1; }
2187  if (ret2 == 0) {
2188  versions[1] = (unsigned char)bin8_to_16(&val2[4]);
2190  }
2191  else { code = ret2; }
2192  if (ret3 == 0) {
2193  versions[2] = (unsigned char)bin8_to_16(&val3[4]);
2195  }
2196  else { code = ret3; }
2197  return code;
2198 }
2199 
2200 int XArmAPI::get_servo_version(unsigned char versions[3], int servo_id) {
2201  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2202  float val1, val2, val3;
2203  int code;
2204  versions[0] = 0;
2205  versions[1] = 0;
2206  versions[2] = 0;
2207  int ret1 = core->servo_addr_r16(servo_id, 0x0801, &val1);
2208  int ret2 = core->servo_addr_r16(servo_id, 0x0802, &val2);
2209  int ret3 = core->servo_addr_r16(servo_id, 0x0803, &val3);
2210  if (ret1 == 0) { versions[0] = (unsigned char)val1; }
2211  else { code = ret1; }
2212  if (ret2 == 0) { versions[1] = (unsigned char)val2; }
2213  else { code = ret2; }
2214  if (ret3 == 0) { versions[2] = (unsigned char)val3; }
2215  else { code = ret3; }
2216  return code;
2217 }
2218 
2219 int XArmAPI::get_tgpio_version(unsigned char versions[3]) {
2220  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2221  float val1, val2, val3;
2222  int code;
2223  versions[0] = 0;
2224  versions[1] = 0;
2225  versions[2] = 0;
2226  int ret1 = core->tgpio_addr_r16(0x0801, &val1);
2227  int ret2 = core->tgpio_addr_r16(0x0802, &val2);
2228  int ret3 = core->tgpio_addr_r16(0x0803, &val3);
2229  if (ret1 == 0) { versions[0] = (unsigned char)val1; }
2230  else { code = ret1; }
2231  if (ret2 == 0) { versions[1] = (unsigned char)val2; }
2232  else { code = ret2; }
2233  if (ret3 == 0) { versions[2] = (unsigned char)val3; }
2234  else { code = ret3; }
2235  return code;
2236 }
2237 
2239  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2240  return core->reload_dynamics();
2241 }
2242 
2244  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2245  int wait_code = _wait_until_cmdnum_lt_max();
2246  if (wait_code != 0) return wait_code;
2247  return core->cnter_reset();
2248 }
2249 
2251  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2252  int wait_code = _wait_until_cmdnum_lt_max();
2253  if (wait_code != 0) return wait_code;
2254  return core->cnter_plus();
2255 }
2256 
2257 int XArmAPI::set_tgpio_digital_with_xyz(int ionum, int value, float xyz[3], float tol_r) {
2258  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2259  int wait_code = _wait_until_cmdnum_lt_max();
2260  if (wait_code != 0) return wait_code;
2261  return core->tgpio_position_set_digital(ionum, value, xyz, tol_r);
2262 }
2263 
2264 int XArmAPI::set_cgpio_digital_with_xyz(int ionum, int value, float xyz[3], float tol_r) {
2265  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2266  int wait_code = _wait_until_cmdnum_lt_max();
2267  if (wait_code != 0) return wait_code;
2268  return core->cgpio_position_set_digital(ionum, value, xyz, tol_r);
2269 }
2270 
2271 int XArmAPI::set_cgpio_analog_with_xyz(int ionum, float value, float xyz[3], float tol_r) {
2272  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2273  int wait_code = _wait_until_cmdnum_lt_max();
2274  if (wait_code != 0) return wait_code;
2275  return core->cgpio_position_set_analog(ionum, value, xyz, tol_r);
2276 }
2277 
2279  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2280  return core->config_io_stop_reset(1, int(on_off));
2281 }
2282 
2284  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2285  return core->config_io_stop_reset(0, int(on_off));
2286 }
2287 
2288 int XArmAPI::set_position_aa(fp32 pose[6], fp32 speed, fp32 acc, fp32 mvtime, bool is_tool_coord, bool relative, bool wait, fp32 timeout) {
2289  _check_is_pause();
2290  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2291  int wait_code = _wait_until_cmdnum_lt_max();
2292  if (wait_code != 0) return wait_code;
2293  last_used_tcp_speed = speed > 0 ? speed : last_used_tcp_speed;
2294  last_used_tcp_acc = acc > 0 ? acc : last_used_tcp_acc;
2295  fp32 mvpose[6];
2296  for (int i = 0; i < 6; i++) {
2297  mvpose[i] = (float)(default_is_radian || i < 3 ? pose[i] : pose[i] / RAD_DEGREE);
2298  }
2299  int ret = core->move_line_aa(mvpose, last_used_tcp_speed, last_used_tcp_acc, mvtime, (int)is_tool_coord, (int)relative);
2300  ret = _check_code(ret, true);
2301  if (wait && ret == 0) {
2302  ret = _wait_move(timeout);
2303  _sync();
2304  }
2305 
2306  return ret;
2307 }
2308 
2309 int XArmAPI::set_position_aa(fp32 pose[6], bool is_tool_coord, bool relative, bool wait, fp32 timeout) {
2310  return set_position_aa(pose, 0, 0, 0, is_tool_coord, relative, wait, timeout);
2311 }
2312 
2313 int XArmAPI::set_servo_cartesian_aa(fp32 pose[6], fp32 speed, fp32 acc, bool is_tool_coord, bool relative) {
2314  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2315  fp32 mvpose[6];
2316  for (int i = 0; i < 6; i++) {
2317  mvpose[i] = (float)(i < 3 || default_is_radian ? pose[i] : pose[i] / RAD_DEGREE);
2318  }
2319  return core->move_servo_cart_aa(mvpose, speed, acc, (int)is_tool_coord, (int)relative);
2320 }
2321 
2322 int XArmAPI::set_servo_cartesian_aa(fp32 pose[6], bool is_tool_coord, bool relative) {
2323  return set_servo_cartesian_aa(pose, 0, 0, is_tool_coord, relative);
2324 }
2325 
2326 int XArmAPI::get_pose_offset(float pose1[6], float pose2[6], float offset[6], int orient_type_in, int orient_type_out) {
2327  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2328  fp32 p1[6], p2[6];
2329  for (int i = 0; i < 6; i++) {
2330  p1[i] = (float)(default_is_radian || i < 3 ? pose1[i] : pose1[i] / RAD_DEGREE);
2331  p2[i] = (float)(default_is_radian || i < 3 ? pose2[i] : pose2[i] / RAD_DEGREE);
2332  }
2333  int ret = core->get_pose_offset(p1, p2, offset, orient_type_in, orient_type_out);
2334  for (int i = 0; i < 6; i++) {
2335  offset[i] = (float)(default_is_radian || i < 3 ? offset[i] : offset[i] * RAD_DEGREE);
2336  }
2337  return ret;
2338 }
2339 
2341  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2342  int ret = core->get_position_aa(pose);
2343  if (ret >= 0) {
2344  for (int i = 0; i < 6; i++) {
2345  pose[i] = (!default_is_radian && i > 2) ? (float)(pose[i] * RAD_DEGREE) : pose[i];
2346  }
2347  }
2348  return ret;
2349 }
2350 
2351 int XArmAPI::_check_modbus_code(int ret, unsigned char *rx_data) {
2352  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2353  if (ret == 0 || ret == API_CODE::ERR_CODE || ret == API_CODE::WAR_CODE) {
2354  if (rx_data != NULL && rx_data[0] != UXBUS_CONF::TGPIO_ID)
2355  return API_CODE::TGPIO_ID_ERR;
2356  if (ret != 0) {
2357  if (error_code != 19 && error_code != 28) {
2358  int err_warn[2] = { 0 };
2359  get_err_warn_code(err_warn);
2360  }
2361  return (error_code != 19 && error_code != 28) ? 0 : ret;
2362  }
2363  }
2364  return ret;
2365 }
2366 
2367 int XArmAPI::_get_modbus_baudrate(int *baud_inx) {
2368  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2369  float val;
2370  int ret = core->tgpio_addr_r16(SERVO3_RG::MODBUS_BAUDRATE & 0x0FFF, &val);
2371  *baud_inx = (int)val;
2372  if (ret == API_CODE::ERR_CODE || ret == API_CODE::WAR_CODE) {
2373  if (error_code != 19 && error_code != 28) {
2374  int err_warn[2] = { 0 };
2375  get_err_warn_code(err_warn);
2376  }
2377  ret = (error_code != 19 && error_code != 28) ? 0 : ret;
2378  }
2379  if (ret == 0 && *baud_inx >= 0 && *baud_inx < 13) modbus_baud_ = BAUDRATES[*baud_inx];
2380  return ret;
2381 }
2382 
2383 int XArmAPI::_checkset_modbus_baud(int baudrate, bool check) {
2384  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2385  if (check && modbus_baud_ == baudrate)
2386  return 0;
2387  int baud_inx = get_baud_inx(baudrate);
2388  if (baud_inx == -1) return API_CODE::MODBUS_BAUD_NOT_SUPPORT;
2389  int cur_baud_inx;
2390  int ret = _get_modbus_baudrate(&cur_baud_inx);
2391  if (ret == 0) {
2392  if (cur_baud_inx != baud_inx) {
2393  try {
2394  ignore_error_ = true;
2395  ignore_state_ = (state != 4 && state != 5) ? true : false;
2396  int state_ = state;
2397  // core->tgpio_addr_w16(SERVO3_RG::MODBUS_BAUDRATE, (float)baud_inx);
2398  core->tgpio_addr_w16(0x1a0b, (float)baud_inx);
2399  sleep_milliseconds(300);
2401  int err_warn[2] = { 0 };
2402  get_err_warn_code(err_warn);
2403  if (error_code == 19 || error_code == 28) {
2404  clean_error();
2405  if (ignore_state_) set_state(state_ >= 3 ? state_ : 0);
2406  sleep_milliseconds(1000);
2407  }
2408  }
2409  catch (...) {
2410  ignore_error_ = false;
2411  ignore_state_ = false;
2412  return API_CODE::API_EXCEPTION;
2413  }
2414  ignore_error_ = false;
2415  ignore_state_ = false;
2416  ret = _get_modbus_baudrate(&cur_baud_inx);
2417  }
2418  if (ret == 0 && cur_baud_inx < 13) modbus_baud_ = BAUDRATES[cur_baud_inx];
2419  }
2420  return modbus_baud_ == baudrate ? 0 : API_CODE::MODBUS_BAUD_NOT_CORRECT;
2421 }
2422 
2423 int XArmAPI::_robotiq_set(unsigned char *params, int length, unsigned char ret_data[6]) {
2424  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2426  unsigned char *send_data = new unsigned char[7 + length];
2427  send_data[0] = 0x09;
2428  send_data[1] = 0x10;
2429  send_data[2] = 0x03;
2430  send_data[3] = 0xE8;
2431  send_data[4] = 0x00;
2432  send_data[5] = 0x03;
2433  send_data[6] = (unsigned char)length;
2434  for (int i = 0; i < length; i++) { send_data[7+i] = params[i]; }
2435  int ret = getset_tgpio_modbus_data(send_data, length + 7, ret_data, 6);
2436  delete[] send_data;
2437  return ret;
2438 }
2439 int XArmAPI::_robotiq_get(unsigned char ret_data[9], unsigned char number_of_registers) {
2440  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2442  unsigned char *send_data = new unsigned char[6];
2443  send_data[0] = 0x09;
2444  send_data[1] = 0x03;
2445  send_data[2] = 0x07;
2446  send_data[3] = 0xD0;
2447  send_data[4] = 0x00;
2448  send_data[5] = number_of_registers;
2449  int ret = getset_tgpio_modbus_data(send_data, 6, ret_data, 3 + 2 * number_of_registers);
2450  delete[] send_data;
2451  if (ret == 0) {
2452  if (number_of_registers >= 0x01) {
2453  robotiq_status.gOBJ = (ret_data[3] & 0xC0) >> 6;
2454  robotiq_status.gSTA = (ret_data[3] & 0x30) >> 4;
2455  robotiq_status.gGTO = (ret_data[3] & 0x08) >> 3;
2456  robotiq_status.gACT = ret_data[3] & 0x01;
2457  }
2458  if (number_of_registers >= 0x02) {
2459  robotiq_status.kFLT = (ret_data[5] & 0xF0) >> 4;
2460  robotiq_status.gFLT = ret_data[5] & 0x0F;
2461  robotiq_status.gPR = ret_data[6];
2463  }
2464  if (number_of_registers >= 0x03) {
2465  robotiq_status.gPO = ret_data[7];
2466  robotiq_status.gCU = ret_data[8];
2467  }
2468 
2469  if (robotiq_status.gSTA == 3 && (robotiq_status.gFLT == 0 || robotiq_status.gFLT == 9)) {
2470  robotiq_is_activated_ = true;
2471  }
2472  else {
2473  robotiq_is_activated_ = false;
2474  }
2475  }
2476  return ret;
2477 }
2478 
2480  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2481  int failed_cnt = 0;
2482  long long expired = get_system_time() + (long long)(timeout * 1000);
2483  int code = API_CODE::WAIT_FINISH_TIMEOUT;
2484  unsigned char rx_data[9] = { 0 };
2485  while (timeout <= 0 || get_system_time() < expired) {
2486  int code2 = robotiq_get_status(rx_data);
2487  failed_cnt = code2 == 0 ? 0 : failed_cnt + 1;
2488  if (code2 == 0) {
2489  code = (robotiq_status.gFLT != 0 && !(robotiq_status.gFLT == 5 && robotiq_status.gSTA == 1)) ? API_CODE::END_EFFECTOR_HAS_FAULT : robotiq_status.gSTA == 3 ? 0 : code;
2490  }
2491  else {
2492  code = code2 == API_CODE::NOT_CONNECTED ? API_CODE::NOT_CONNECTED : failed_cnt > 10 ? API_CODE::CHECK_FAILED : code;
2493  }
2494  if (code != API_CODE::WAIT_FINISH_TIMEOUT) break;
2495  sleep_milliseconds(50);
2496  }
2497  return code;
2498 }
2499 
2500 int XArmAPI::_robotiq_wait_motion_completed(fp32 timeout, bool check_detected) {
2501  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2502  int failed_cnt = 0;
2503  long long expired = get_system_time() + (long long)(timeout * 1000);
2504  int code = API_CODE::WAIT_FINISH_TIMEOUT;
2505  unsigned char rx_data[9] = { 0 };
2506  while (timeout <= 0 || get_system_time() < expired) {
2507  int code2 = robotiq_get_status(rx_data);
2508  failed_cnt = code2 == 0 ? 0 : failed_cnt + 1;
2509  if (code2 == 0) {
2511  ((check_detected && (robotiq_status.gOBJ == 1 || robotiq_status.gOBJ == 2)) || (robotiq_status.gOBJ == 1 || robotiq_status.gOBJ == 2 || robotiq_status.gOBJ == 3)) ? 0 : code;
2512  }
2513  else {
2514  code = code2 == API_CODE::NOT_CONNECTED ? API_CODE::NOT_CONNECTED : failed_cnt > 10 ? API_CODE::CHECK_FAILED : code;
2515  }
2516  if (code != API_CODE::WAIT_FINISH_TIMEOUT) break;
2517  sleep_milliseconds(50);
2518  }
2520  return code;
2521 }
2522 
2523 int XArmAPI::robotiq_get_status(unsigned char ret_data[9], unsigned char number_of_registers) {
2524  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2525  return _robotiq_get(ret_data, number_of_registers);
2526 }
2527 
2528 int XArmAPI::robotiq_reset(unsigned char ret_data[6]) {
2529  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2530  unsigned char params[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
2531  unsigned char rx_data[6] = { 0 };
2532  int ret = _robotiq_set(params, 6, rx_data);
2533  if (ret_data != NULL) { memcpy(ret_data, rx_data, 6); }
2534  return ret;
2535 }
2536 
2537 int XArmAPI::robotiq_set_activate(bool wait, fp32 timeout, unsigned char ret_data[6]) {
2538  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2539  unsigned char params[6] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
2540  unsigned char rx_data[6] = { 0 };
2541  int ret = _robotiq_set(params, 6, rx_data);
2542  if (ret_data != NULL) { memcpy(ret_data, rx_data, 6); }
2543  if (wait && ret == 0) { ret = _robotiq_wait_activation_completed(timeout); }
2544  if (ret == 0) robotiq_is_activated_ = true;
2545  return ret;
2546 }
2547 
2548 int XArmAPI::robotiq_set_activate(bool wait, unsigned char ret_data[6]) {
2549  return robotiq_set_activate(wait, 3, ret_data);
2550 }
2551 int XArmAPI::robotiq_set_activate(unsigned char ret_data[6]) {
2552  return robotiq_set_activate(true, ret_data);
2553 }
2554 
2555 int XArmAPI::robotiq_set_position(unsigned char pos, unsigned char speed, unsigned char force, bool wait, fp32 timeout, unsigned char ret_data[6]) {
2556  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2557  unsigned char params[6] = { 0x09, 0x00, 0x00, pos, speed, force };
2558  unsigned char rx_data[6] = { 0 };
2559  bool has_error = error_code != 0;
2560  bool is_stop = state == 4 || state == 5;
2561  int code = _wait_move(NO_TIMEOUT);
2562  if (!(code == 0 || (is_stop && code == API_CODE::EMERGENCY_STOP) || (has_error && code == API_CODE::HAS_ERROR))) {
2563  return code;
2564  }
2565  int ret = _robotiq_set(params, 6, rx_data);
2566  if (ret_data != NULL) { memcpy(ret_data, rx_data, 6); }
2567  if (wait && ret == 0) { ret = _robotiq_wait_motion_completed(timeout); }
2568  return ret;
2569 }
2570 
2571 int XArmAPI::robotiq_set_position(unsigned char pos, bool wait, fp32 timeout, unsigned char ret_data[6]) {
2572  return robotiq_set_position(pos, 0xFF, 0xFF, wait, timeout, ret_data);
2573 }
2574 int XArmAPI::robotiq_set_position(unsigned char pos, bool wait, unsigned char ret_data[6]) {
2575  return robotiq_set_position(pos, wait, 5, ret_data);
2576 }
2577 
2578 int XArmAPI::robotiq_set_position(unsigned char pos, unsigned char ret_data[6]) {
2579  return robotiq_set_position(pos, true, ret_data);
2580 }
2581 
2582 int XArmAPI::robotiq_open(unsigned char speed, unsigned char force, bool wait, fp32 timeout, unsigned char ret_data[6]) {
2583  return robotiq_set_position(0x00, speed, force, wait, timeout, ret_data);
2584 }
2585 
2586 int XArmAPI::robotiq_open(bool wait, fp32 timeout, unsigned char ret_data[6]) {
2587  return robotiq_set_position(0x00, wait, timeout, ret_data);
2588 }
2589 
2590 int XArmAPI::robotiq_open(bool wait, unsigned char ret_data[6]) {
2591  return robotiq_open(wait, 5, ret_data);
2592 }
2593 
2594 int XArmAPI::robotiq_open(unsigned char ret_data[6]) {
2595  return robotiq_open(true, ret_data);
2596 }
2597 
2598 int XArmAPI::robotiq_close(unsigned char speed, unsigned char force, bool wait, fp32 timeout, unsigned char ret_data[6]) {
2599  return robotiq_set_position(0xFF, speed, force, wait, timeout, ret_data);
2600 }
2601 
2602 int XArmAPI::robotiq_close(bool wait, fp32 timeout, unsigned char ret_data[6]) {
2603  return robotiq_set_position(0xFF, wait, timeout, ret_data);
2604 }
2605 
2606 int XArmAPI::robotiq_close(bool wait, unsigned char ret_data[6]) {
2607  return robotiq_close(wait, 5, ret_data);
2608 }
2609 
2610 int XArmAPI::robotiq_close(unsigned char ret_data[6]) {
2611  return robotiq_close(true, ret_data);
2612 }
2613 
2614 int XArmAPI::_bio_gripper_send_modbus(unsigned char *send_data, int length, unsigned char *ret_data, int ret_length) {
2615  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2616  if (_checkset_modbus_baud(2000000) != 0) return API_CODE::MODBUS_BAUD_NOT_CORRECT;
2617  int ret = getset_tgpio_modbus_data(send_data, length, ret_data, ret_length);
2618  return ret;
2619 }
2620 
2621 int XArmAPI::_get_bio_gripper_register(unsigned char *ret_data, int address, int number_of_registers) {
2622  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2623  unsigned char params[6] = { 0x08, 0x03, (unsigned char)(address >> 8), (unsigned char)address, (unsigned char)(number_of_registers >> 8), (unsigned char)number_of_registers };
2624  return _bio_gripper_send_modbus(params, 6, ret_data, 3 + 2 * number_of_registers);
2625 }
2626 
2628  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2629  int failed_cnt = 0;
2630  long long expired = get_system_time() + (long long)(timeout * 1000);
2631  int code = API_CODE::WAIT_FINISH_TIMEOUT;
2632  int status = BIO_STATE::IS_MOTION;
2633  while (timeout <= 0 || get_system_time() < expired) {
2634  int code2 = get_bio_gripper_status(&status);
2635  failed_cnt = code2 == 0 ? 0 : failed_cnt + 1;
2636  if (code2 == 0) {
2637  code = (status & 0x03) == BIO_STATE::IS_MOTION ? code : (status & 0x03) == BIO_STATE::IS_FAULT ? API_CODE::END_EFFECTOR_HAS_FAULT : 0;
2638  }
2639  else {
2640  code = code2 == API_CODE::NOT_CONNECTED ? API_CODE::NOT_CONNECTED : failed_cnt > 10 ? API_CODE::CHECK_FAILED : code;
2641  }
2642  if (code != API_CODE::WAIT_FINISH_TIMEOUT) break;
2643  sleep_milliseconds(100);
2644  }
2646  return code;
2647 }
2648 
2650  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2651  int failed_cnt = 0;
2652  long long expired = get_system_time() + (long long)(timeout * 1000);
2653  int code = API_CODE::WAIT_FINISH_TIMEOUT;
2654  int status = BIO_STATE::IS_NOT_ENABLED;
2655  while (timeout <= 0 || get_system_time() < expired) {
2656  int code2 = get_bio_gripper_status(&status);
2657  failed_cnt = code2 == 0 ? 0 : failed_cnt + 1;
2658  if (code2 == 0) {
2659  code = bio_gripper_is_enabled_ ? 0 : code;
2660  }
2661  else {
2662  code = code2 == API_CODE::NOT_CONNECTED ? API_CODE::NOT_CONNECTED : failed_cnt > 10 ? API_CODE::CHECK_FAILED : code;
2663  }
2664  if (code != API_CODE::WAIT_FINISH_TIMEOUT) break;
2665  sleep_milliseconds(100);
2666  }
2667  return code;
2668 }
2669 
2670 int XArmAPI::set_bio_gripper_enable(bool enable, bool wait, fp32 timeout) {
2671  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2672  unsigned char params[6] = { 0x08, 0x06, 0x01, 0x00, 0x00, (unsigned char)enable };
2673  unsigned char rx_data[6] = { 0 };
2674  int ret = _bio_gripper_send_modbus(params, 6, rx_data, 6);
2675  if (ret == 0 && enable && wait) { ret = _bio_gripper_wait_enable_completed(timeout); }
2676  return ret;
2677 }
2678 
2680  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2681  unsigned short tmp = speed;
2682  unsigned char params[6] = { 0x08, 0x06, 0x03, 0x03, (unsigned char)(tmp >> 8), (unsigned char)tmp };
2683  unsigned char rx_data[6] = { 0 };
2684  int ret = _bio_gripper_send_modbus(params, 6, rx_data, 6);
2685  if (ret == 0) { bio_gripper_speed_ = speed; }
2686  return ret;
2687 }
2688 
2689 int XArmAPI::_set_bio_gripper_position(int pos, int speed, bool wait, fp32 timeout) {
2690  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2691  if (speed > 0 && speed != bio_gripper_speed_) { set_bio_gripper_speed(speed); }
2692  unsigned char params[11] = { 0x08, 0x10, 0x07, 0x00, 0x00, 0x02, 0x04 };
2693  params[7] = (unsigned char)(pos >> 24);
2694  params[8] = (unsigned char)(pos >> 16);
2695  params[9] = (unsigned char)(pos >> 8);
2696  params[10] = (unsigned char)(pos);
2697  unsigned char rx_data[6] = { 0 };
2698  bool has_error = error_code != 0;
2699  bool is_stop = state == 4 || state == 5;
2700  int code = _wait_move(NO_TIMEOUT);
2701  if (!(code == 0 || (is_stop && code == API_CODE::EMERGENCY_STOP) || (has_error && code == API_CODE::HAS_ERROR))) {
2702  return code;
2703  }
2704  int ret = _bio_gripper_send_modbus(params, 11, rx_data, 6);
2705  if (ret == 0 && wait) { ret = _bio_gripper_wait_motion_completed(timeout); }
2706  return ret;
2707 }
2708 
2709 int XArmAPI::_set_bio_gripper_position(int pos, bool wait, fp32 timeout) {
2710  return _set_bio_gripper_position(pos, 0, wait, timeout);
2711 }
2712 
2713 int XArmAPI::open_bio_gripper(int speed, bool wait, fp32 timeout) {
2714  return _set_bio_gripper_position(130, speed, wait, timeout);
2715 }
2716 
2717 int XArmAPI::open_bio_gripper(bool wait, fp32 timeout) {
2718  return open_bio_gripper(bio_gripper_speed_, wait, timeout);
2719 }
2720 
2721 int XArmAPI::close_bio_gripper(int speed, bool wait, fp32 timeout) {
2722  return _set_bio_gripper_position(50, speed, wait, timeout);
2723 }
2724 
2725 int XArmAPI::close_bio_gripper(bool wait, fp32 timeout) {
2726  return close_bio_gripper(bio_gripper_speed_, wait, timeout);
2727 }
2728 
2730  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2731  unsigned char rx_data[5] = { 0 };
2732  int ret = _get_bio_gripper_register(rx_data, 0x00);
2733  *status = (rx_data[3] << 8) + rx_data[4];
2734  if (ret == 0) {
2735  if ((*status & 0x03) == BIO_STATE::IS_FAULT) {
2736  int err;
2737  get_bio_gripper_error(&err);
2738  }
2740  bio_gripper_is_enabled_ = ((*status >> 2) & 0x03) == BIO_STATE::IS_ENABLED ? true : false;
2741  }
2742  return ret;
2743 }
2744 
2746  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2747  unsigned char rx_data[5] = { 0 };
2748  int ret = _get_bio_gripper_register(rx_data, 0x0F);
2749  *err = (rx_data[3] << 8) + rx_data[4];
2750  if (ret == 0) bio_gripper_error_code_ = *err;
2751  return ret;
2752 }
2753 
2755  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2756  unsigned char params[6] = { 0x08, 0x06, 0x00, 0x0F, 0x00, 0x00 };
2757  unsigned char rx_data[6] = { 0 };
2758  int ret = _bio_gripper_send_modbus(params, 6, rx_data, 6);
2759  int status;
2760  get_bio_gripper_status(&status);
2761  return ret;
2762 }
2763 
2765  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2766  return core->set_modbus_timeout(timeout);
2767 }
2768 
2770  return _checkset_modbus_baud(baud, false);
2771 }
2772 
2774  int cur_baud_inx;
2775  int ret = _get_modbus_baudrate(&cur_baud_inx);
2776  if (ret == 0 && cur_baud_inx < 13)
2777  modbus_baud_ = BAUDRATES[cur_baud_inx];
2778  *baud = modbus_baud_;
2779  return ret;
2780 }
2781 
2782 int XArmAPI::getset_tgpio_modbus_data(unsigned char *modbus_data, int modbus_length, unsigned char *ret_data, int ret_length) {
2783  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2784  unsigned char *rx_data = new unsigned char[ret_length + 1];
2785  int ret = core->tgpio_set_modbus(modbus_data, modbus_length, rx_data);
2786  ret = _check_modbus_code(ret, rx_data);
2787  memcpy(ret_data, rx_data + 1, ret_length);
2788  delete[] rx_data;
2789  return ret;
2790 }
2791 
2792 int XArmAPI::set_report_tau_or_i(int tau_or_i) {
2793  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2794  return core->set_report_tau_or_i(tau_or_i);
2795 }
2796 
2797 int XArmAPI::get_report_tau_or_i(int *tau_or_i) {
2798  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2799  return core->get_report_tau_or_i(tau_or_i);
2800 }
2801 
2803  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2804  return core->set_self_collision_detection((int)on);
2805 }
2806 
2807 int XArmAPI::set_collision_tool_model(int tool_type, int n, ...) {
2808  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2809  if (tool_type < COLLISION_TOOL_TYPE::USE_PRIMITIVES) {
2810  return core->set_collision_tool_model(tool_type);
2811  }
2812  assert(n > (tool_type == COLLISION_TOOL_TYPE::BOX ? 2 : tool_type == COLLISION_TOOL_TYPE::CYLINDER ? 1 : 0));
2813  fp32 *params = new fp32[n];
2814  va_list args;
2815  va_start(args, n);
2816  int inx = 0;
2817  while (inx < n)
2818  {
2819  params[inx] = (fp32)va_arg(args, double);
2820  inx++;
2821  }
2822  va_end(args);
2823  int ret = core->set_collision_tool_model(tool_type, n, params);
2824  delete[] params;
2825  return ret;
2826 }
2827 
2829  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2830  return core->set_simulation_robot((int)on);
2831 }
2832 
2833 int XArmAPI::vc_set_joint_velocity(fp32 speeds[7], bool is_sync) {
2834  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2835  fp32 jnt_v[7];
2836  for (int i = 0; i < 7; i++) {
2837  jnt_v[i] = (float)(default_is_radian ? speeds[i] : speeds[i] / RAD_DEGREE);
2838  }
2839  return core->vc_set_jointv(jnt_v, is_sync ? 1 : 0);
2840 }
2841 
2842 int XArmAPI::vc_set_cartesian_velocity(fp32 speeds[6], bool is_tool_coord) {
2843  if (!is_connected()) return API_CODE::NOT_CONNECTED;
2844  fp32 line_v[6];
2845  for (int i = 0; i < 6; i++) {
2846  line_v[i] = (float)((i < 3 || default_is_radian) ? speeds[i] : speeds[i] / RAD_DEGREE);
2847  }
2848  return core->vc_set_linev(line_v, is_tool_coord ? 1 : 0);
2849 }
2850 
int set_gravity_dir(float gravity_dir[3])
Definition: uxbus_cmd.cc:506
int set_mode(int mode)
Definition: xarm_api.cc:1066
void close_port(void)
Definition: ser.cc:141
int get_reduced_states(int *on, int xyz_list[6], float *tcp_speed, float *joint_speed, float jrange_rad[14]=NULL, int *fense_is_on=NULL, int *collision_rebound_is_on=NULL, int length=21)
Definition: uxbus_cmd.cc:256
int set_collis_sens(int value)
Definition: uxbus_cmd.cc:498
int register_mtable_mtbrake_changed_callback(void(*callback)(int mtable, int mtbrake))
Definition: xarm_api.cc:2068
bool has_warn(void)
Definition: xarm_api.cc:197
bool is_tcp_
Definition: xarm_api.h:1512
static const unsigned short SOFT_REBOOT
Definition: servo3_config.h:42
fp32 last_used_tcp_acc
Definition: xarm_api.h:111
int teach_sensitivity
Definition: xarm_api.h:94
int _check_code(int code, bool is_move_cmd=false)
Definition: xarm_api.cc:804
bool * motor_brake_states
Definition: xarm_api.h:88
int major_version_number_
Definition: xarm_api.h:1521
int is_joint_limit(fp32 angles[7], int *limit)
Definition: xarm_api.cc:1421
void sleep_milliseconds(unsigned long milliseconds)
Definition: uxbus_cmd.h:24
int gripper_modbus_set_pos(float pulse)
Definition: uxbus_cmd.cc:828
int _check_gripper_position(fp32 target_pos, fp32 timeout=10)
Definition: xarm_api.cc:1592
std::vector< std::string > split(const std::string &str, const std::string &pattern)
Definition: utils.h:15
int set_joint_maxacc(float maxacc)
Definition: uxbus_cmd.cc:484
fp32 * collision_model_params
Definition: xarm_api.h:143
fp32 rot_jerk
Definition: xarm_api.h:106
int get_tgpio_digital(int *io0_value, int *io1_value)
Definition: xarm_api.cc:1729
static int BAUDRATES[13]
Definition: xarm_api.cc:22
int collision_tool_type
Definition: xarm_api.h:142
int get_err_code(int *rx_data)
Definition: uxbus_cmd.cc:319
int load_trajectory(char *filename, float timeout=10)
Definition: xarm_api.cc:1939
void stop()
Definition: timer.h:149
static const int CHECK_FAILED
int get_version(unsigned char rx_data[40])
Definition: uxbus_cmd.cc:194
int gripper_modbus_clean_err(void)
Definition: uxbus_cmd.cc:855
static const int TCP_PORT_REPORT_RICH
Definition: xarm_config.h:21
int release_mtable_mtbrake_changed_callback(void(*callback)(int mtable, int mtbrake)=NULL)
Definition: xarm_api.cc:2104
std::vector< void(*)(int)> state_changed_callbacks_
Definition: xarm_api.h:1564
int mt_brake_
Definition: xarm_api.h:1527
unsigned char version[30]
Definition: xarm_api.h:101
bool robotiq_is_activated_
Definition: xarm_api.h:1546
int save_conf(void)
Definition: uxbus_cmd.cc:514
int tgpio_get_analog1(float *value)
Definition: uxbus_cmd.cc:720
int set_timeout(float timeout)
Definition: uxbus_cmd.cc:40
unsigned char gCU
Definition: xarm_api.h:47
static const int TRAJ_RW_FAILED
int set_bio_gripper_enable(bool enable, bool wait=true, fp32 timeout=3)
Definition: xarm_api.cc:2670
static const int SAVE_SUCCESS
int get_version(unsigned char version[40])
Definition: xarm_api.cc:950
unsigned char kFLT
Definition: xarm_api.h:43
int set_gripper_mode(int mode)
Definition: xarm_api.cc:1527
int master_id
Definition: xarm_api.h:97
void _sync(void)
Definition: xarm_api.cc:699
static const int EMERGENCY_STOP
int cgpio_get_analog2(float *value)
Definition: uxbus_cmd.cc:942
XArmAPI(const std::string &port="", bool is_radian=DEFAULT_IS_RADIAN, bool do_not_open=false, bool check_tcp_limit=true, bool check_joint_limit=true, bool check_cmdnum_limit=true, bool check_robot_sn=false, bool check_is_ready=true, bool check_is_pause=true, int max_callback_thread_count=-1, int max_cmdnum=256)
Definition: xarm_api.cc:41
int release_count_changed_callback(void(*callback)(int count)=NULL)
Definition: xarm_api.cc:2119
static const int SUCTION_CUP_TOUT
int register_report_location_callback(void(*callback)(const fp32 *pose, const fp32 *angles))
Definition: xarm_api.cc:2052
int move_jointb(float mvjoint[7], float mvvelo, float mvacc, float mvradii)
Definition: uxbus_cmd.cc:386
fp32 last_used_joint_speed
Definition: xarm_api.h:117
int get_cmdnum(int *cmdnum)
Definition: xarm_api.cc:978
#define SDK_VERSION
Definition: xarm_api.h:33
static const int IS_FAULT
bool state_is_ready
Definition: uxbus_cmd.h:215
int get_tgpio_version(unsigned char versions[3])
Definition: xarm_api.cc:2219
int clean_warn(void)
Definition: xarm_api.cc:1102
fp32 * last_used_angles
Definition: xarm_api.h:114
void _report_callback(callable_vector &&callbacks, arguments &&...args)
Definition: xarm_api.cc:210
int get_report_tau_or_i(int *tau_or_i)
Definition: xarm_api.cc:2797
int * cgpio_output_conf
Definition: xarm_api.h:151
int set_state(int state)
Definition: xarm_api.cc:1045
int clean_war(void)
Definition: uxbus_cmd.cc:337
int _robotiq_get(unsigned char ret_data[9], unsigned char number_of_registers=0x03)
Definition: xarm_api.cc:2439
static const int TGPIO_ID_ERR
static void report_thread_handle_(void *arg)
Definition: xarm_api.cc:694
SocketPort * stream_tcp_report_
Definition: xarm_api.h:1558
int register_mode_changed_callback(void(*callback)(int mode))
Definition: xarm_api.cc:2064
fp32 min_tcp_speed_
Definition: xarm_api.h:1529
int gripper_version_numbers_[3]
Definition: xarm_api.h:1550
int register_error_warn_changed_callback(void(*callback)(int err_code, int warn_code))
Definition: xarm_api.cc:2072
int register_connect_changed_callback(void(*callback)(bool connected, bool reported))
Definition: xarm_api.cc:2056
int _bio_gripper_wait_motion_completed(fp32 timeout=5)
Definition: xarm_api.cc:2627
int move_lineb(float mvpose[6], float mvvelo, float mvacc, float mvtime, float mvradii)
Definition: uxbus_cmd.cc:364
static const int END_EFFECTOR_NOT_ENABLED
static const int IDLE
static const int TGPIO_ID
int set_tcp_maxacc(float maxacc)
Definition: uxbus_cmd.cc:474
std::string port_
Definition: xarm_api.h:1498
int get_report_tau_or_i(int *rx_data)
Definition: uxbus_cmd.cc:1158
int release_mode_changed_callback(void(*callback)(int mode)=NULL)
Definition: xarm_api.cc:2100
int set_mode(int value)
Definition: uxbus_cmd.cc:347
int close_bio_gripper(int speed=0, bool wait=true, fp32 timeout=5)
Definition: xarm_api.cc:2721
int _checkset_modbus_baud(int baudrate, bool check=true)
Definition: xarm_api.cc:2383
int set_reduced_mode(bool on)
Definition: xarm_api.cc:1827
int motor_tid
Definition: xarm_api.h:99
int shutdown_system(int value=1)
Definition: xarm_api.cc:964
int axis
Definition: xarm_api.h:96
int set_world_offset(float pose_offset[6])
Definition: uxbus_cmd.cc:276
int config_io_stop_reset(int io_type, int val)
Definition: uxbus_cmd.cc:1148
int release_state_changed_callback(void(*callback)(int state)=NULL)
Definition: xarm_api.cc:2096
int _get_modbus_baudrate(int *baud_inx)
Definition: xarm_api.cc:2367
int set_reduced_jrange(float jrange_rad[14])
Definition: uxbus_cmd.cc:288
int get_pose_offset(float pose1[6], float pose2[6], float offset[6], int orient_type_in=0, int orient_type_out=0)
Definition: xarm_api.cc:2326
int release_report_location_callback(void(*callback)(const fp32 *pose, const fp32 *angles)=NULL)
Definition: xarm_api.cc:2088
int move_gohome(fp32 speed=0, fp32 acc=0, fp32 mvtime=0, bool wait=false, fp32 timeout=NO_TIMEOUT)
Definition: xarm_api.cc:1323
int * cgpio_input_digitals
Definition: xarm_api.h:146
void hex_to_nfp32(unsigned char *datahex, float *dataf, int n)
Definition: data_type.h:93
void _report_count_changed_callback(void)
Definition: xarm_api.cc:285
int set_position_aa(fp32 pose[6], fp32 speed=0, fp32 acc=0, fp32 mvtime=0, bool is_tool_coord=false, bool relative=false, bool wait=false, fp32 timeout=NO_TIMEOUT)
Definition: xarm_api.cc:2288
static const int ERR_CODE
int shutdown_system(int value)
Definition: uxbus_cmd.cc:206
fp32 * cgpio_output_anglogs
Definition: xarm_api.h:149
int get_fk(float angles[7], float pose[6])
Definition: uxbus_cmd.cc:530
bool is_sync_
Definition: xarm_api.h:1515
int load_traj(char filename[81])
Definition: uxbus_cmd.cc:229
int get_position(fp32 pose[6])
Definition: xarm_api.cc:997
int cgpio_state
Definition: xarm_api.h:144
int vc_set_linev(float line_v[6], int coord)
Definition: uxbus_cmd.cc:1189
int read_frame(unsigned char *data)
Definition: socket.cc:122
int reload_dynamics(void)
Definition: xarm_api.cc:2238
std::vector< void(*)(int)> mode_changed_callbacks_
Definition: xarm_api.h:1565
int get_cgpio_analog(int ionum, fp32 *value)
Definition: xarm_api.cc:1773
bool arm_type_is_1300_
Definition: xarm_api.h:1518
unsigned char gACT
Definition: xarm_api.h:42
int get_state(int *rx_data)
Definition: uxbus_cmd.cc:311
#define NO_TIMEOUT
Definition: xarm_api.h:32
int set_xyz_limits(int xyz_list[6])
Definition: uxbus_cmd.cc:272
int is_tcp_limit(float pose[6], int *value)
Definition: uxbus_cmd.cc:538
int mt_able_
Definition: xarm_api.h:1528
int bin8_to_32(unsigned char *a)
Definition: data_type.h:38
int is_simulation_robot
Definition: xarm_api.h:140
int release_cmdnum_changed_callback(void(*callback)(int cmdnum)=NULL)
Definition: xarm_api.cc:2112
int tgpio_get_digital(int *io1, int *io2)
Definition: uxbus_cmd.cc:695
int _robotiq_wait_activation_completed(fp32 timeout=3)
Definition: xarm_api.cc:2479
int minor_version_number_
Definition: xarm_api.h:1522
int reload_dynamics(void)
Definition: uxbus_cmd.cc:327
int set_reduced_jointspeed(float jspd_rad)
Definition: uxbus_cmd.cc:247
bool check_is_pause_
Definition: xarm_api.h:1504
int set_servo_cartesian_aa(fp32 pose[6], fp32 speed=0, fp32 acc=0, bool is_tool_coord=false, bool relative=false)
Definition: xarm_api.cc:2313
std::vector< void(*)(int, int)> error_warn_changed_callbacks_
Definition: xarm_api.h:1567
fp32 * joint_speed_limit
Definition: xarm_api.h:115
std::vector< void(*)(bool, bool)> connect_changed_callbacks_
Definition: xarm_api.h:1563
fp32 cmd_timeout_
Definition: xarm_api.h:1555
std::vector< void(*)(int)> cmdnum_changed_callbacks_
Definition: xarm_api.h:1568
int set_counter_reset(void)
Definition: xarm_api.cc:2243
int save_traj(char filename[81])
Definition: uxbus_cmd.cc:225
int set_collision_tool_model(int tool_type, int n=0,...)
Definition: xarm_api.cc:2807
int set_suction_cup(bool on, bool wait=false, float timeout=3, float delay_sec=0)
Definition: xarm_api.cc:2128
unsigned char * gpio_reset_config
Definition: xarm_api.h:130
int release_error_warn_changed_callback(void(*callback)(int err_code, int warn_code)=NULL)
Definition: xarm_api.cc:2108
int set_collision_tool_model(int tool_type, int n=0, float *argv=NULL)
Definition: uxbus_cmd.cc:1167
fp32 * gravity_direction
Definition: xarm_api.h:122
static const int TRAJ_PLAYBACK_TOUT
SocketPort * stream_tcp_
Definition: xarm_api.h:1557
void _report_mode_changed_callback(void)
Definition: xarm_api.cc:244
int connect(const std::string &port="")
Definition: xarm_api.cc:876
int get_joint_pose(float angles[7])
Definition: uxbus_cmd.cc:522
int save_conf(void)
Definition: xarm_api.cc:1453
int set_tool_position(fp32 pose[6], fp32 speed=0, fp32 acc=0, fp32 mvtime=0, bool wait=false, fp32 timeout=NO_TIMEOUT)
Definition: xarm_api.cc:1210
int get_tgpio_analog(int ionum, float *value)
Definition: xarm_api.cc:1747
int set_timeout(fp32 timeout)
Definition: xarm_api.cc:943
void _report_connect_changed_callback(void)
Definition: xarm_api.cc:225
int modbus_baud_
Definition: xarm_api.h:1542
static const int BOX
static const int NOT_CONNECTED
int clean_bio_gripper_error(void)
Definition: xarm_api.cc:2754
int get_err_warn_code(int err_warn[2])
Definition: xarm_api.cc:987
int set_gripper_position(fp32 pos, bool wait=false, fp32 timeout=10)
Definition: xarm_api.cc:1694
int collision_sensitivity
Definition: xarm_api.h:93
int move_circle(float pose1[6], float pose2[6], float mvvelo, float mvacc, float mvtime, float percent)
Definition: uxbus_cmd.cc:454
fp32 joint_jerk
Definition: xarm_api.h:105
int move_servo_cartesian(float mvpose[6], float mvvelo, float mvacc, float mvtime)
Definition: uxbus_cmd.cc:421
int is_joint_limit(float joint[7], int *value)
Definition: uxbus_cmd.cc:534
UxbusCmd * core
Definition: xarm_api.h:134
int set_pause_time(fp32 sltime)
Definition: xarm_api.cc:1107
static const int API_EXCEPTION
int set_cgpio_digital(int ionum, int value, float delay_sec=0)
Definition: xarm_api.cc:1784
long long max_report_interval_
Definition: xarm_api.h:1553
unsigned char gGTO
Definition: xarm_api.h:41
void _report_mtable_mtbrake_changed_callback(void)
Definition: xarm_api.cc:252
int set_position(fp32 pose[6], fp32 radius=-1, fp32 speed=0, fp32 acc=0, fp32 mvtime=0, bool wait=false, fp32 timeout=NO_TIMEOUT)
Definition: xarm_api.cc:1173
int set_cgpio_digital_input_function(int ionum, int fun)
Definition: xarm_api.cc:1810
static const int WAIT_FINISH_TIMEOUT
int device_type
Definition: xarm_api.h:95
static const int STATE_NOT_READY
int clean_error(void)
Definition: xarm_api.cc:1082
static const int HAS_ERROR
bool is_reported(void)
Definition: xarm_api.cc:205
int cgpio_get_state(int *state, int *digit_io, float *analog, int *input_conf, int *output_conf, int *input_conf2=NULL, int *output_conf2=NULL)
Definition: uxbus_cmd.cc:1007
bool _version_is_ge(int major=1, int minor=2, int revision=11)
Definition: xarm_api.cc:813
int register_count_changed_callback(void(*callback)(int count))
Definition: xarm_api.cc:2084
int playback_traj(int times, int spdx=1)
Definition: uxbus_cmd.cc:215
static const int IS_ENABLED
void dispatch(callable &&f, arguments &&...args)
Definition: timer.h:165
fp32 max_rot_acc
Definition: xarm_api.h:107
int _robotiq_wait_motion_completed(fp32 timeout=5, bool check_detected=false)
Definition: xarm_api.cc:2500
ThreadPool pool
Definition: xarm_api.h:1560
int set_collision_rebound(bool on)
Definition: xarm_api.cc:1882
long long sleep_finish_time_
Definition: xarm_api.h:1525
int get_cgpio_digital(int *digitals, int *digitals2=NULL)
Definition: xarm_api.cc:1758
int robotiq_open(unsigned char speed=0xFF, unsigned char force=0xFF, bool wait=true, fp32 timeout=5, unsigned char ret_data[6]=NULL)
Definition: xarm_api.cc:2582
int get_traj_rw_status(int *rx_data)
Definition: uxbus_cmd.cc:233
int cgpio_position_set_analog(int ionum, float value, float xyz[3], float tol_r)
Definition: uxbus_cmd.cc:1133
int set_tcp_maxacc(fp32 acc)
Definition: xarm_api.cc:1495
int xarm_gripper_error_code_
Definition: xarm_api.h:1547
std::vector< void(*)(const fp32 *)> temperature_changed_callbacks_
Definition: xarm_api.h:1569
int move_line_aa(float mvpose[6], float mvvelo, float mvacc, float mvtime, int mvcoord=0, int relative=0)
Definition: uxbus_cmd.cc:1059
int get_robot_sn(unsigned char rx_data[40])
Definition: uxbus_cmd.cc:198
int set_servo_cartesian(fp32 pose[6], fp32 speed=0, fp32 acc=0, fp32 mvtime=0, bool is_tool_coord=false)
Definition: xarm_api.cc:1290
void close_port(void)
Definition: socket.cc:136
int tgpio_addr_w16(int addr, float value)
Definition: uxbus_cmd.cc:638
static const int TCP_PORT_CONTROL
Definition: xarm_config.h:19
int get_cgpio_state(int *state, int *digit_io, fp32 *analog, int *input_conf, int *output_conf, int *input_conf2=NULL, int *output_conf2=NULL)
Definition: xarm_api.cc:1822
int servo_addr_r16(int id, int addr, float *value)
Definition: uxbus_cmd.cc:883
bool bio_gripper_is_enabled_
Definition: xarm_api.h:1545
int move_gohome(float mvvelo, float mvacc, float mvtime)
Definition: uxbus_cmd.cc:404
int get_ik(float pose[6], float angles[7])
Definition: uxbus_cmd.cc:526
fp32 p2p_msg_[5]
Definition: xarm_api.h:1539
int * cgpio_input_conf
Definition: xarm_api.h:150
int gripper_modbus_set_en(int value)
Definition: uxbus_cmd.cc:800
int _robotiq_set(unsigned char *params, int length, unsigned char ret_data[6])
Definition: xarm_api.cc:2423
int save_record_trajectory(char *filename, float timeout=10)
Definition: xarm_api.cc:1912
int tgpio_set_modbus(unsigned char *send_data, int length, unsigned char *recv_data)
Definition: uxbus_cmd.cc:760
int set_modbus_timeout(int value)
Definition: uxbus_cmd.cc:739
int _get_bio_gripper_register(unsigned char *ret_data, int address=0x00, int number_of_registers=1)
Definition: xarm_api.cc:2621
unsigned char gPR
Definition: xarm_api.h:45
void _report_temperature_changed_callback(void)
Definition: xarm_api.cc:277
int set_teach_sensitivity(int sensitivity)
Definition: xarm_api.cc:1436
fp32 min_joint_acc_
Definition: xarm_api.h:1535
int set_counter_increase(void)
Definition: xarm_api.cc:2250
int set_cgpio_digital_with_xyz(int ionum, int value, float xyz[3], float tol_r)
Definition: xarm_api.cc:2264
unsigned char gPO
Definition: xarm_api.h:46
int _release_event_callback(callable_vector &&callbacks, callable &&f)
Definition: xarm_api.cc:2038
fp32 realtime_tcp_speed
Definition: xarm_api.h:124
static const int SAVE_FAIL
int set_tgpio_digital(int ionum, int value, float delay_sec=0)
Definition: xarm_api.cc:1734
int sv3msg_[16]
Definition: xarm_api.h:1537
int robotiq_error_code_
Definition: xarm_api.h:1549
~XArmAPI(void)
Definition: xarm_api.cc:69
int cnter_plus(void)
Definition: uxbus_cmd.cc:284
int set_tcp_jerk(fp32 jerk)
Definition: xarm_api.cc:1488
int gripper_modbus_get_errcode(int *err)
Definition: uxbus_cmd.cc:847
int clean_conf(void)
Definition: xarm_api.cc:1448
void emergency_stop(void)
Definition: xarm_api.cc:1370
int set_simulation_robot(int on_off)
Definition: uxbus_cmd.cc:1178
int robotiq_close(unsigned char speed=0xFF, unsigned char force=0xFF, bool wait=true, fp32 timeout=5, unsigned char ret_data[6]=NULL)
Definition: xarm_api.cc:2598
int _get_gripper_status(int *status)
Definition: xarm_api.cc:1582
int get_position_aa(fp32 pose[6])
Definition: xarm_api.cc:2340
int _wait_move(fp32 timeout)
Definition: xarm_api.cc:1121
unsigned char gSTA
Definition: xarm_api.h:40
int bio_gripper_speed_
Definition: xarm_api.h:1543
int state
Definition: xarm_api.h:84
int config_cgpio_reset_when_stop(bool on_off)
Definition: xarm_api.cc:2283
int cgpio_set_infun(int ionum, int fun)
Definition: uxbus_cmd.cc:979
int is_ok(void)
Definition: socket.cc:118
void disconnect(void)
Definition: xarm_api.cc:930
int motion_enable(bool enable, int servo_id=8)
Definition: xarm_api.cc:1025
fp32 * currents
Definition: xarm_api.h:139
int get_position_aa(float pose[6])
Definition: uxbus_cmd.cc:1055
int cgpio_set_auxdigit(int ionum, int value)
Definition: uxbus_cmd.cc:948
int get_pose_offset(float pose1[6], float pose2[6], float offset[6], int orient_type_in=0, int orient_type_out=0)
Definition: uxbus_cmd.cc:1035
fp32 max_tcp_speed_
Definition: xarm_api.h:1530
int gripper_modbus_set_posspd(float speed)
Definition: uxbus_cmd.cc:839
int get_gripper_err_code(int *err)
Definition: xarm_api.cc:1557
int * cgpio_output_digitals
Definition: xarm_api.h:147
int get_bio_gripper_error(int *err)
Definition: xarm_api.cc:2745
fp32 * joint_acc_limit
Definition: xarm_api.h:116
int set_gripper_enable(bool enable)
Definition: xarm_api.cc:1516
int set_tcp_offset(float pose_offset[6])
Definition: uxbus_cmd.cc:489
fp32 max_joint_speed_
Definition: xarm_api.h:1534
void _recv_report_data(void)
Definition: xarm_api.cc:640
int get_robot_sn(unsigned char robot_sn[40])
Definition: xarm_api.cc:955
static const int LOAD_FAIL
int set_world_offset(float pose_offset[6])
Definition: xarm_api.cc:1887
unsigned char gFLT
Definition: xarm_api.h:44
int set_joint_jerk(fp32 jerk)
Definition: xarm_api.cc:1502
int config_tgpio_reset_when_stop(bool on_off)
Definition: xarm_api.cc:2278
void reset(bool wait=false, fp32 timeout=NO_TIMEOUT)
Definition: xarm_api.cc:1346
int is_tcp_limit(fp32 pose[6], int *limit)
Definition: xarm_api.cc:1412
std::vector< void(*)(int)> count_changed_callbacks_
Definition: xarm_api.h:1570
void _check_is_pause(void)
Definition: xarm_api.cc:785
int set_reduced_tcp_boundary(int boundary[6])
Definition: xarm_api.cc:1863
int set_fense_on(int on_off)
Definition: uxbus_cmd.cc:292
int _check_modbus_code(int ret, unsigned char *rx_data=NULL)
Definition: xarm_api.cc:2351
int robotiq_reset(unsigned char ret_data[6]=NULL)
Definition: xarm_api.cc:2528
int get_gripper_version(unsigned char versions[3])
Definition: xarm_api.cc:2169
int bio_gripper_error_code_
Definition: xarm_api.h:1548
bool ignore_state_
Definition: xarm_api.h:1517
int gripper_modbus_r16s(int addr, int len, unsigned char *rx_data)
Definition: uxbus_cmd.cc:789
int clean_gripper_error(void)
Definition: xarm_api.cc:1719
int gripper_modbus_set_mode(int value)
Definition: uxbus_cmd.cc:808
int * version_number
Definition: xarm_api.h:103
int warn_code
Definition: xarm_api.h:91
fp32 min_joint_speed_
Definition: xarm_api.h:1533
fp32 * last_used_position
Definition: xarm_api.h:120
static const int USE_PRIMITIVES
#define REPORT_BUF_SIZE
Definition: xarm_api.cc:18
int tgpio_get_analog2(float *value)
Definition: uxbus_cmd.cc:727
int get_reduced_mode(int *mode)
Definition: xarm_api.cc:1842
int set_collis_reb(int on_off)
Definition: uxbus_cmd.cc:297
std::thread report_thread_
Definition: xarm_api.h:1508
int get_tcp_pose(float pose[6])
Definition: uxbus_cmd.cc:518
void _check_version(void)
Definition: xarm_api.cc:704
long long get_system_time()
Definition: uxbus_cmd.h:32
int mode
Definition: xarm_api.h:85
int set_state(int value)
Definition: uxbus_cmd.cc:307
int set_cgpio_digital_output_function(int ionum, int fun)
Definition: xarm_api.cc:1816
int start_record_trajectory(void)
Definition: xarm_api.cc:1897
int robotiq_get_status(unsigned char ret_data[9], unsigned char number_of_registers=3)
Definition: xarm_api.cc:2523
bool control_box_type_is_1300_
Definition: xarm_api.h:1519
int cgpio_get_auxdigit(int *value)
Definition: uxbus_cmd.cc:933
fp32 * temperatures
Definition: xarm_api.h:128
fp32 * tcp_speed_limit
Definition: xarm_api.h:108
int move_line_tool(float mvpose[6], float mvvelo, float mvacc, float mvtime)
Definition: uxbus_cmd.cc:395
fp32 * realtime_joint_speeds
Definition: xarm_api.h:125
int sleep_instruction(float sltime)
Definition: uxbus_cmd.cc:449
fp32 max_tcp_acc_
Definition: xarm_api.h:1532
int register_state_changed_callback(void(*callback)(int state))
Definition: xarm_api.cc:2060
int clean_err(void)
Definition: uxbus_cmd.cc:332
int move_line(float mvpose[6], float mvvelo, float mvacc, float mvtime)
Definition: uxbus_cmd.cc:355
int clean_conf(void)
Definition: uxbus_cmd.cc:510
bool check_robot_sn_
Definition: xarm_api.h:1502
int set_cgpio_analog(int ionum, fp32 value)
Definition: xarm_api.cc:1797
fp32 * voltages
Definition: xarm_api.h:138
int set_gravity_direction(fp32 gravity_dir[3])
Definition: xarm_api.cc:1442
fp32 max_joint_acc_
Definition: xarm_api.h:1536
void _report_location_callback(void)
Definition: xarm_api.cc:217
static const int WAR_CODE
fp32 * tcp_load
Definition: xarm_api.h:92
static const int TRAJ_RW_TOUT
int set_servo_angle_j(fp32 angles[7], fp32 speed=0, fp32 acc=0, fp32 mvtime=0)
Definition: xarm_api.cc:1281
static const int SERIAL_BAUD
Definition: xarm_config.h:18
int set_servo_angle(fp32 angles[7], fp32 speed=0, fp32 acc=0, fp32 mvtime=0, bool wait=false, fp32 timeout=NO_TIMEOUT, fp32 radius=-1)
Definition: xarm_api.cc:1237
static const int ERR_CODE
bool check_cmdnum_limit_
Definition: xarm_api.h:1501
int get_bio_gripper_status(int *status)
Definition: xarm_api.cc:2729
void set_max_thread_count(int max_thread_count)
Definition: timer.h:160
int set_tgpio_modbus_timeout(int timeout)
Definition: xarm_api.cc:2764
int get_inverse_kinematics(fp32 pose[6], fp32 angles[7])
Definition: xarm_api.cc:1384
int slave_id
Definition: xarm_api.h:98
int set_gripper_speed(fp32 speed)
Definition: xarm_api.cc:1537
int set_bio_gripper_speed(int speed)
Definition: xarm_api.cc:2679
int get_cmdnum(int *rx_data)
Definition: uxbus_cmd.cc:315
int register_temperature_changed_callback(void(*callback)(const fp32 *temps))
Definition: xarm_api.cc:2080
fp32 * joints_torque
Definition: xarm_api.h:87
std::condition_variable cond_
Definition: xarm_api.h:1510
int get_gripper_position(fp32 *pos)
Definition: xarm_api.cc:1547
int get_state(int *state)
Definition: xarm_api.cc:969
bool _gripper_is_support_status(void)
Definition: xarm_api.cc:1572
long long last_report_time_
Definition: xarm_api.h:1552
int set_reduced_max_joint_speed(float speed)
Definition: xarm_api.cc:1837
bool ignore_error_
Definition: xarm_api.h:1516
int _check_gripper_status(fp32 timeout=10)
Definition: xarm_api.cc:1664
int tgpio_addr_r16(int addr, float *value)
Definition: uxbus_cmd.cc:651
fp32 last_used_joint_acc
Definition: xarm_api.h:118
int set_tcp_jerk(float jerk)
Definition: uxbus_cmd.cc:469
int set_collision_sensitivity(int sensitivity)
Definition: xarm_api.cc:1430
int register_cmdnum_changed_callback(void(*callback)(int cmdnum))
Definition: xarm_api.cc:2076
void _report_cmdnum_changed_callback(void)
Definition: xarm_api.cc:269
int get_forward_kinematics(fp32 angles[7], fp32 pose[6])
Definition: xarm_api.cc:1398
int is_collision_detection
Definition: xarm_api.h:141
int set_cgpio_analog_with_xyz(int ionum, float value, float xyz[3], float tol_r)
Definition: xarm_api.cc:2271
static const int WAR_CODE
int get_servo_version(unsigned char versions[3], int servo_id=1)
Definition: xarm_api.cc:2200
static const int LOAD_SUCCESS
static const int MODBUS_BAUD_NOT_CORRECT
int move_circle(fp32 pose1[6], fp32 pose2[6], fp32 percent, fp32 speed=0, fp32 acc=0, fp32 mvtime=0, bool wait=false, fp32 timeout=NO_TIMEOUT)
Definition: xarm_api.cc:1300
int set_fense_mode(bool on)
Definition: xarm_api.cc:1877
int set_reduced_joint_range(float jrange[14])
Definition: xarm_api.cc:1868
int cgpio_position_set_digital(int ionum, int value, float xyz[3], float tol_r)
Definition: uxbus_cmd.cc:1119
int set_servo_attach(int servo_id)
Definition: xarm_api.cc:1071
std::vector< void(*)(const fp32 *, const fp32 *)> report_location_callbacks_
Definition: xarm_api.h:1562
int revision_version_number_
Definition: xarm_api.h:1523
int get_suction_cup(int *val)
Definition: xarm_api.cc:2123
fp32 tcp_jerk
Definition: xarm_api.h:104
fp32 rot_msg_[2]
Definition: xarm_api.h:1540
static const int IS_MOTION
int set_record_traj(int value)
Definition: uxbus_cmd.cc:210
int move_servoj(float mvjoint[7], float mvvelo, float mvacc, float mvtime)
Definition: uxbus_cmd.cc:412
void _init(void)
Definition: xarm_api.cc:73
int open_bio_gripper(int speed=0, bool wait=true, fp32 timeout=5)
Definition: xarm_api.cc:2713
void commit(callable &&f, arguments &&...args)
Definition: timer.h:179
int cgpio_code
Definition: xarm_api.h:145
int cnter_reset(void)
Definition: uxbus_cmd.cc:280
int vc_set_joint_velocity(fp32 speeds[7], bool is_sync=true)
Definition: xarm_api.cc:2833
bool is_old_protocol_
Definition: xarm_api.h:1513
int tgpio_position_set_digital(int ionum, int value, float xyz[3], float tol_r)
Definition: uxbus_cmd.cc:1105
int _bio_gripper_send_modbus(unsigned char *send_data, int length, unsigned char *ret_data, int ret_length)
Definition: xarm_api.cc:2614
static const int CYLINDER
fp32 trs_msg_[5]
Definition: xarm_api.h:1538
int gripper_modbus_get_pos(float *pulse)
Definition: uxbus_cmd.cc:820
bool default_is_radian
Definition: xarm_api.h:132
int release_connect_changed_callback(void(*callback)(bool connected, bool reported)=NULL)
Definition: xarm_api.cc:2092
int set_reduced_mode(int on_off)
Definition: uxbus_cmd.cc:237
int set_teach_sens(int value)
Definition: uxbus_cmd.cc:502
std::mutex mutex_
Definition: xarm_api.h:1509
int _wait_until_cmdnum_lt_max(void)
Definition: xarm_api.cc:793
int max_cmdnum_
Definition: xarm_api.h:1506
int move_joint(float mvjoint[7], float mvvelo, float mvacc, float mvtime)
Definition: uxbus_cmd.cc:376
int playback_trajectory(int times=1, char *filename=NULL, bool wait=false, int double_speed=1)
Definition: xarm_api.cc:1966
int set_reduced_linespeed(float lspd_mm)
Definition: uxbus_cmd.cc:242
int get_reduced_mode(int *rx_data)
Definition: uxbus_cmd.cc:252
int robotiq_set_position(unsigned char pos, unsigned char speed=0xFF, unsigned char force=0xFF, bool wait=true, fp32 timeout=5, unsigned char ret_data[6]=NULL)
Definition: xarm_api.cc:2555
bool has_error(void)
Definition: xarm_api.cc:193
SerialPort * stream_ser_
Definition: xarm_api.h:1559
int cmd_num
Definition: xarm_api.h:86
int stop_record_trajectory(char *filename=NULL)
Definition: xarm_api.cc:1902
int _bio_gripper_wait_enable_completed(fp32 timeout=3)
Definition: xarm_api.cc:2649
int is_ok(void)
Definition: ser.cc:85
int release_temperature_changed_callback(void(*callback)(const fp32 *temps)=NULL)
Definition: xarm_api.cc:2116
int get_servo_angle(fp32 angles[7])
Definition: xarm_api.cc:1011
int set_self_collision_detection(int on_off)
Definition: uxbus_cmd.cc:1162
int count
Definition: xarm_api.h:129
int error_code
Definition: xarm_api.h:90
fp32 * tcp_acc_limit
Definition: xarm_api.h:109
int vc_set_jointv(float jnt_v[7], int jnt_sync)
Definition: uxbus_cmd.cc:1182
bool is_first_report_
Definition: xarm_api.h:1514
int set_tcp_offset(fp32 pose_offset[6])
Definition: xarm_api.cc:1458
unsigned char gOBJ
Definition: xarm_api.h:39
void _update(unsigned char *data)
Definition: xarm_api.cc:417
int get_tgpio_modbus_baudrate(int *baud)
Definition: xarm_api.cc:2773
int _register_event_callback(callable_vector &&callbacks, callable &&f)
Definition: xarm_api.cc:2028
static const unsigned short MODBUS_BAUDRATE
Definition: servo3_config.h:58
bool has_err_warn(void)
Definition: xarm_api.cc:189
int set_tcp_load(float mass, float load_offset[3])
Definition: uxbus_cmd.cc:493
void _report_state_changed_callback(void)
Definition: xarm_api.cc:235
int set_joint_jerk(float jerk)
Definition: uxbus_cmd.cc:479
float fp32
Definition: xarm_api.h:36
int move_servo_cart_aa(float mvpose[6], float mvvelo, float mvacc, int tool_coord=0, int relative=0)
Definition: uxbus_cmd.cc:1069
int set_report_tau_or_i(int tau_or_i=0)
Definition: xarm_api.cc:2792
bool callback_in_thread_
Definition: xarm_api.h:1505
fp32 min_tcp_acc_
Definition: xarm_api.h:1531
bool is_ready_
Definition: xarm_api.h:1511
int set_tgpio_modbus_baudrate(int baud)
Definition: xarm_api.cc:2769
int set_self_collision_detection(bool on)
Definition: xarm_api.cc:2802
int cgpio_set_outfun(int ionum, int fun)
Definition: uxbus_cmd.cc:983
void _report_error_warn_changed_callback(void)
Definition: xarm_api.cc:260
fp32 * position
Definition: xarm_api.h:119
bool is_connected(void)
Definition: xarm_api.cc:201
int motion_en(int id, int value)
Definition: uxbus_cmd.cc:302
int cgpio_set_analog2(float value)
Definition: uxbus_cmd.cc:974
fp32 * tcp_offset
Definition: xarm_api.h:121
int set_reduced_max_tcp_speed(float speed)
Definition: xarm_api.cc:1832
#define RAD_DEGREE
Definition: xarm_api.h:30
Definition: ser.h:17
int _set_bio_gripper_position(int pos, int speed=0, bool wait=true, fp32 timeout=5)
Definition: xarm_api.cc:2689
int cgpio_get_analog1(float *value)
Definition: uxbus_cmd.cc:936
int cgpio_delay_set_digital(int ionum, int value, float delay_sec)
Definition: uxbus_cmd.cc:1092
fp32 * angles
Definition: xarm_api.h:113
int vc_set_cartesian_velocity(fp32 speeds[6], bool is_tool_coord=false)
Definition: xarm_api.cc:2842
int set_joint_maxacc(fp32 acc)
Definition: xarm_api.cc:1509
static int get_baud_inx(int baud)
Definition: xarm_api.cc:24
int motor_fid
Definition: xarm_api.h:100
int set_report_tau_or_i(int tau_or_i)
Definition: uxbus_cmd.cc:1153
int getset_tgpio_modbus_data(unsigned char *modbus_data, int modbus_length, unsigned char *ret_data, int ret_length)
Definition: xarm_api.cc:2782
std::vector< void(*)(int, int)> mtable_mtbrake_changed_callbacks_
Definition: xarm_api.h:1566
struct RobotIqStatus robotiq_status
Definition: xarm_api.h:136
int set_servo_detach(int servo_id)
Definition: xarm_api.cc:1077
unsigned char sn[40]
Definition: xarm_api.h:102
static const int MODBUS_BAUD_NOT_SUPPORT
fp32 * world_offset
Definition: xarm_api.h:127
void _update_old(unsigned char *data)
Definition: xarm_api.cc:293
fp32 * cgpio_intput_anglogs
Definition: xarm_api.h:148
static const int END_EFFECTOR_HAS_FAULT
int robotiq_set_activate(bool wait=true, fp32 timeout=3, unsigned char ret_data[6]=NULL)
Definition: xarm_api.cc:2537
int get_reduced_states(int *on, int *xyz_list, float *tcp_speed, float *joint_speed, float jrange[14]=NULL, int *fense_is_on=NULL, int *collision_rebound_is_on=NULL)
Definition: xarm_api.cc:1847
static bool compare_version(int v1[3], int v2[3])
Definition: xarm_api.cc:29
void bin32_to_8(int a, unsigned char *b)
Definition: data_type.h:25
int set_tcp_load(fp32 weight, fp32 center_of_gravity[3])
Definition: xarm_api.cc:1469
static const int IS_NOT_ENABLED
static const int NOT_READY
int set_brake(int axis, int en)
Definition: uxbus_cmd.cc:342
bool gripper_is_enabled_
Definition: xarm_api.h:1544
int bin8_to_16(unsigned char *a)
Definition: data_type.h:42
int cgpio_set_analog1(float value)
Definition: uxbus_cmd.cc:969
int get_trajectory_rw_status(int *status)
Definition: xarm_api.cc:2022
fp32 last_used_tcp_speed
Definition: xarm_api.h:110
int tgpio_delay_set_digital(int ionum, int value, float delay_sec)
Definition: uxbus_cmd.cc:1079
int set_tgpio_digital_with_xyz(int ionum, int value, float xyz[3], float tol_r)
Definition: xarm_api.cc:2257
int tgpio_set_digital(int ionum, int value)
Definition: uxbus_cmd.cc:704
int set_simulation_robot(bool on)
Definition: xarm_api.cc:2828
bool * motor_enable_states
Definition: xarm_api.h:89


xarm_api
Author(s):
autogenerated on Sat May 8 2021 02:51:23