15 #define DEBUG_DETAIL 0 16 #define PRINT_HEX_DATA(hex, len, ...) \ 18 if (DEBUG_MODE && DEBUG_DETAIL) { \ 19 printf(__VA_ARGS__); \ 20 for (int i = 0; i < len; ++i) { \ 21 printf("%02x ", hex[i]); \ 29 fprintf(stderr,
"[xarm_driver] Ctrl-C caught, exit process...\n");
53 ROS_INFO(
"try to reconnect to report socket");
58 ROS_INFO(
"reconnect to report socket success");
63 ROS_ERROR(
"reconnect to report socket failed");
82 unsigned char rx_data[1024];
83 unsigned char prev_data[1280];
84 unsigned char ret_data[1024 * 2];
87 int db_packet_cnt = 0;
88 int db_success_pkt_cnt = 0;
89 int db_discard_pkt_cnt = 0;
90 int db_failed_pkt_cnt = 0;
91 bool prev_pkt_is_not_empty =
false;
93 ROS_INFO(
"start to handle the tcp report");
101 if (ret != 0)
continue;
105 if (num < 4 && size <= 0)
continue;
110 if (num + offset < size) {
112 ROS_INFO(
"[READ:%d][PACKET:%d][SUCCESS:%d][DISCARD:%d][FAILED:%d] The data packet length is insufficient, waiting for the next packet splicing. num=%d, offset=%d, length=%d\n",
113 db_read_cnt, db_packet_cnt, db_success_pkt_cnt, db_discard_pkt_cnt, db_failed_pkt_cnt, num, offset, num + offset);
114 memcpy(ret_data + offset + 4, rx_data + 4, num);
119 memcpy(ret_data + offset + 4, rx_data + 4, size - offset);
121 int offset2 = size - offset;
122 while (num - offset2 >= size) {
123 db_discard_pkt_cnt += 1;
126 ROS_INFO(
"[READ:%d][PACKET:%d][SUCCESS:%d][DISCARD:%d][FAILED:%d] Data packet stick to packets, the previous data packet will be discarded. num=%d, offset=%d\n",
127 db_read_cnt, db_packet_cnt, db_success_pkt_cnt, db_discard_pkt_cnt, db_failed_pkt_cnt, num, offset);
128 PRINT_HEX_DATA(ret_data, size + 4,
"[%d] Discard Packet: ", db_packet_cnt);
129 memcpy(ret_data + 4, rx_data + 4 + offset2, size);
134 if (size_of_data != size) {
135 db_failed_pkt_cnt += 2;
136 ROS_WARN(
"[READ:%d][PACKET:%d][SUCCESS:%d][DISCARD:%d][FAILED:%d] Packet abnormal. num=%d, offset=%d, size=%d, length=%d\n",
137 db_read_cnt, db_packet_cnt, db_success_pkt_cnt, db_discard_pkt_cnt, db_failed_pkt_cnt, num, offset, size, size_of_data);
138 PRINT_HEX_DATA(ret_data, size + 4,
"[%d] Abnormal Packet: ", db_packet_cnt);
142 prev_pkt_is_not_empty =
false;
146 ROS_ERROR(
"packet abnormal, reconnect failed\n");
150 if (prev_pkt_is_not_empty) {
151 PRINT_HEX_DATA(prev_data, size + 4,
"[%d] Normal Packet: ", db_packet_cnt);
154 memcpy(prev_data, ret_data, size + 4);
157 offset = num - offset2;
160 ROS_INFO(
"[READ:%d][PACKET:%d][SUCCESS:%d][DISCARD:%d][FAILED:%d] Data packets are redundant and will be left for the next packet splicing process. num=%d, offset=%d\n",
161 db_read_cnt, db_packet_cnt, db_success_pkt_cnt, db_discard_pkt_cnt, db_failed_pkt_cnt, num, offset);
162 memcpy(ret_data + 4, rx_data + 4 + offset2, offset);
164 if (!prev_pkt_is_not_empty) {
165 prev_pkt_is_not_empty =
true;
174 db_success_pkt_cnt++;
181 ROS_INFO(
"[READ:%d][PACKET:%d][SUCCESS:%d][DISCARD:%d][FAILED:%d]", db_read_cnt, db_packet_cnt, db_success_pkt_cnt, db_discard_pkt_cnt, db_failed_pkt_cnt);
191 js_msg.header.frame_id =
"real-time data";
226 rm_msg.angle.resize(joint_num_);
234 sprintf(str,
"%0.3f", d);
235 sscanf(str,
"%lf", &r);
238 for(i = 0; i < 6; i++)
261 for (
int i = 0; i < 16; ++i) {
270 ROS_WARN(
"[DEBUG] real_data.flush_data failed, ret = %d\n", ret);
278 delete [] prev_angle;
279 ROS_ERROR(
"xArm Report Connection Failed! Please Shut Down (Ctrl-C) and Retry ...");
307 int main(
int argc,
char **argv)
309 ros::init(argc, argv,
"xarm_driver_node");
317 std::string robot_ip =
"192.168.1.121";
320 ROS_ERROR(
"No param named 'xarm_robot_ip'");
321 ROS_ERROR(
"Use default robot ip = 192.168.1.121");
325 n.
getParam(
"xarm_robot_ip", robot_ip);
328 char server_ip[20]={0};
329 strcpy(server_ip,robot_ip.c_str());
void closeReportSocket(void)
int main(int argc, char **argv)
unsigned char cgpio_input_conf[16]
unsigned char cgpio_output_conf[16]
std::vector< std::string > joint_name_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int flush_data(unsigned char *rx_data)
void XARMDriverInit(ros::NodeHandle &root_nh, char *server_ip)
static void * thread_proc(void *arg)
int get_frame(unsigned char *data)
int bin8_to_32(unsigned char *a)
float cgpio_output_analogs[2]
void pub_joint_state(sensor_msgs::JointState &js_msg)
int cgpio_output_digitals[2]
void exit_sig_handler(int signum)
#define PRINT_HEX_DATA(hex, len,...)
XarmRTConnection(ros::NodeHandle &root_nh, char *server_ip, xarm_api::XARMDriver &drv)
xarm_msgs::CIOState cio_msg
void pub_cgpio_state(xarm_msgs::CIOState &cio_msg)
int cgpio_input_digitals[2]
sensor_msgs::JointState js_msg
static constexpr const double GET_FRAME_RATE_HZ
bool isConnectionOK(void)
XArmReportData * report_data_ptr
bool getParam(const std::string &key, std::string &s) const
void pub_robot_msg(xarm_msgs::RobotMsg &rm_msg)
float cgpio_input_analogs[2]
bool hasParam(const std::string &key) const
xarm_msgs::RobotMsg rm_msg
xarm_api::XARMDriver xarm_driver
ROSCPP_DECL void waitForShutdown()
void bin32_to_8(int a, unsigned char *b)
bool reConnectReportSocket(char *server_ip)