-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathswiftpro_read_node.cpp
More file actions
executable file
·137 lines (117 loc) · 3.03 KB
/
swiftpro_read_node.cpp
File metadata and controls
executable file
·137 lines (117 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Software License Agreement (BSD License)
* Copyright (c) 2017, UFactory, Inc.
* All rights reserved.
* Author: Roger Cui <roger@ufactory.cc>
* David Long <xiaokun.long@ufactory.cc>
*/
#include <ros/ros.h>
#include <serial/serial.h>
#include <std_msgs/String.h>
#include <string>
#include <swiftpro/SwiftproState.h>
serial::Serial _serial; // serial object
float position[4] = {0.0}; // 3 cartesian coordinates: x, y, z(mm) and 1 angle(degree)
char strdata[2048]; // global variables for handling string
void handlestr()
{
char *pch = strtok(strdata, " ");
float value[8];
int index = 0;
while (pch != NULL && index < 5)
{
value[index] = atof(pch+1);
pch = strtok(NULL, " ");
index++;
}
position[0] = value[1];
position[1] = value[2];
position[2] = value[3];
position[3] = value[4];
}
void handlechar(char c)
{
static int index = 0;
switch(c)
{
case '\r':
break;
case '\n':
strdata[index] = '\0';
handlestr();
index = 0;
break;
default:
strdata[index++] = c;
break;
}
}
/*
* Node name:
* swiftpro_read_node
*
* Topic publish: (rate = 20Hz, queue size = 1)
* position_read_topic
*/
int main(int argc, char** argv)
{
ros::init(argc, argv, "swiftpro_read_node");
ros::NodeHandle nh("~");
swiftpro::SwiftproState swiftpro_state;
std_msgs::String result;
std::string param;
std::string serport;
ros::Publisher pub = nh.advertise<swiftpro::SwiftproState>("SwiftproState_topic", 1);
ros::Rate loop_rate(20);
if (nh.getParam("port",param)) {
serport.assign(param.c_str());
} else {
serport="/dev/ttyUSB0";
}
try
{
_serial.setPort(serport);
_serial.setBaudrate(115200);
serial::Timeout to = serial::Timeout::simpleTimeout(1000);
_serial.setTimeout(to);
_serial.open();
ROS_INFO_STREAM("Port has been open successfully");
}
catch (serial::IOException& e)
{
ROS_ERROR_STREAM("Unable to open port");
return -1;
}
if (_serial.isOpen())
{
ros::Duration(3.0).sleep(); // wait 3s
_serial.write("M2019\r\n"); // detach
ros::Duration(0.5).sleep(); // wait 0.5s
_serial.write("M2120 V0.05\r\n"); // report position per 0.05s
ROS_INFO_STREAM("Start to report data");
}
while (ros::ok()) // publish positionesian coordinates
{
if (_serial.available())
{
result.data = _serial.read(_serial.available());
// ROS_INFO_STREAM("Read:" << result.data);
for (int i = 0; i < result.data.length(); i++)
handlechar(result.data.c_str()[i]);
swiftpro_state.pump = 0;
swiftpro_state.gripper = 0;
swiftpro_state.swiftpro_status = 0;
swiftpro_state.motor_angle1 = 0.0;
swiftpro_state.motor_angle2 = 0.0;
swiftpro_state.motor_angle3 = 0.0;
swiftpro_state.motor_angle4 = position[3];
swiftpro_state.x = position[0];
swiftpro_state.y = position[1];
swiftpro_state.z = position[2];
pub.publish(swiftpro_state);
ROS_INFO("position: %.2f %.2f %.2f %.2f", position[0], position[1], position[2], position[3]);
}
ros::spinOnce();
loop_rate.sleep();
}
}