-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathswiftpro_write_node.cpp
More file actions
executable file
·221 lines (191 loc) · 5.47 KB
/
swiftpro_write_node.cpp
File metadata and controls
executable file
·221 lines (191 loc) · 5.47 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Software License Agreement (BSD License)
* Copyright (c) 2017, UFactory, Inc.
* All rights reserved.
* Author: Roger Cui <roger@ufactory.cc>
*/
#include <string>
#include <ros/ros.h>
#include <serial/serial.h>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>
#include <swiftpro/SwiftproState.h>
#include <swiftpro/status.h>
#include <swiftpro/position.h>
#include <swiftpro/angle4th.h>
serial::Serial _serial; // serial object
swiftpro::SwiftproState pos;
/*
* Description: callback when receive data from position_write_topic
* Inputs: msg(float) 3 cartesian coordinates: x, y, z(mm)
* Outputs: Gcode send gcode to control swift pro
*/
void position_write_callback(const swiftpro::position& msg)
{
std::string Gcode = "";
std_msgs::String result;
char x[10];
char y[10];
char z[10];
pos.x = msg.x;
pos.y = msg.y;
pos.z = msg.z;
sprintf(x, "%.2f", msg.x);
sprintf(y, "%.2f", msg.y);
sprintf(z, "%.2f", msg.z);
Gcode = (std::string)"G0 X" + x + " Y" + y + " Z" + z + " F10000" + "\r\n";
ROS_INFO("%s", Gcode.c_str());
_serial.write(Gcode.c_str());
result.data = _serial.read(_serial.available());
}
/*
* Description: callback when receive data from angle4th_topic
* Inputs: msg(float) angle of 4th motor(degree)
* Outputs: Gcode send gcode to control swift pro
*/
void angle4th_callback(const swiftpro::angle4th& msg)
{
std::string Gcode = "";
std_msgs::String result;
char m4[10];
pos.motor_angle4 = msg.angle4th;
sprintf(m4, "%.2f", msg.angle4th);
Gcode = (std::string)"G2202 N3 V" + m4 + "\r\n";
ROS_INFO("%s", Gcode.c_str());
_serial.write(Gcode.c_str());
result.data = _serial.read(_serial.available());
}
/*
* Description: callback when receive data from swiftpro_status_topic
* Inputs: msg(uint8) status of gripper: attach if 1; detach if 0
* Outputs: Gcode send gcode to control swift pro
*/
void swiftpro_status_callback(const swiftpro::status& msg)
{
std::string Gcode = "";
std_msgs::String result;
if (msg.status == 1)
Gcode = (std::string)"M17\r\n"; // attach
else if (msg.status == 0)
Gcode = (std::string)"M2019\r\n";
else
{
ROS_INFO("Error:Wrong swiftpro status input");
return;
}
pos.swiftpro_status = msg.status;
ROS_INFO("%s", Gcode.c_str());
_serial.write(Gcode.c_str());
result.data = _serial.read(_serial.available());
}
/*
* Description: callback when receive data from gripper_topic
* Inputs: msg(uint8) status of gripper: work if 1; otherwise 0
* Outputs: Gcode send gcode to control swift pro
*/
void gripper_callback(const swiftpro::status& msg)
{
std::string Gcode = "";
std_msgs::String result;
if (msg.status == 1)
Gcode = (std::string)"M2232 V1" + "\r\n";
else if (msg.status == 0)
Gcode = (std::string)"M2232 V0" + "\r\n";
else
{
ROS_INFO("Error:Wrong gripper status input");
return;
}
pos.gripper = msg.status;
ROS_INFO("%s", Gcode.c_str());
_serial.write(Gcode.c_str());
result.data = _serial.read(_serial.available());
}
/*
* Description: callback when receive data from pump_topic
* Inputs: msg(uint8) status of pump: work if 1; otherwise 0
* Outputs: Gcode send gcode to control swift pro
*/
void pump_callback(const swiftpro::status& msg)
{
std::string Gcode = "";
std_msgs::String result;
if (msg.status == 1)
Gcode = (std::string)"M2231 V1" + "\r\n";
else if (msg.status == 0)
Gcode = (std::string)"M2231 V0" + "\r\n";
else
{
ROS_INFO("Error:Wrong pump status input");
return;
}
pos.pump = msg.status;
ROS_INFO("%s", Gcode.c_str());
_serial.write(Gcode.c_str());
result.data = _serial.read(_serial.available());
}
/*
* Node name:
* swiftpro_write_node
*
* Topic publish: (rate = 20Hz, queue size = 1)
* swiftpro_state_topic
*
* Topic subscribe: (queue size = 1)
* position_write_topic
* swiftpro_status_topic
* angle4th_topic
* gripper_topic
* pump_topic
*/
int main(int argc, char** argv)
{
ros::init(argc, argv, "swiftpro_write_node");
ros::NodeHandle nh("~");
std::string param;
std::string serport;
swiftpro::SwiftproState swiftpro_state;
ros::Subscriber sub1 = nh.subscribe("position_write_topic", 1, position_write_callback);
ros::Subscriber sub2 = nh.subscribe("swiftpro_status_topic", 1, swiftpro_status_callback);
ros::Subscriber sub3 = nh.subscribe("angle4th_topic", 1, angle4th_callback);
ros::Subscriber sub4 = nh.subscribe("gripper_topic", 1, gripper_callback);
ros::Subscriber sub5 = nh.subscribe("pump_topic", 1, pump_callback);
ros::Publisher pub = nh.advertise<swiftpro::SwiftproState>("SwiftproState_topic", 1);
ros::Rate loop_rate(20);
ROS_INFO("Got parameter: %s", param.c_str());
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.5).sleep(); // wait 3.5s
_serial.write("M2120 V0\r\n"); // stop report position
ros::Duration(0.1).sleep(); // wait 0.1s
_serial.write("M17\r\n"); // attach
ros::Duration(0.1).sleep(); // wait 0.1s
ROS_INFO_STREAM("Attach and wait for commands");
}
while (ros::ok())
{
pub.publish(pos);
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}