This repository has been archived by the owner on Mar 18, 2020. It is now read-only.
forked from LeoRover/core2_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
344 lines (290 loc) · 9.41 KB
/
main.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "hFramework.h"
#include "hCloudClient.h"
#include "ros.h"
#include "geometry_msgs/Twist.h"
#include "std_msgs/Int16.h"
#include "std_msgs/Bool.h"
#include "std_msgs/Empty.h"
#include "std_msgs/Float32.h"
#include "std_msgs/UInt16MultiArray.h"
#include "sensor_msgs/JointState.h"
#include "diff_controller.h"
#include "params.h"
#include "utils.h"
using namespace hFramework;
ros::NodeHandle nh;
std_msgs::Float32 battery;
ros::Publisher *battery_pub;
bool publish_battery = false;
geometry_msgs::Twist odom;
ros::Publisher *odom_pub;
bool publish_odom = false;
sensor_msgs::JointState joint_states;
ros::Publisher *joint_states_pub;
bool publish_joint = false;
ros::Subscriber<geometry_msgs::Twist> *twist_sub;
ros::Subscriber<std_msgs::Bool> *relay1_sub;
ros::Subscriber<std_msgs::Bool> *relay2_sub;
ros::Subscriber<std_msgs::Bool> *relay3_sub;
ros::Subscriber<std_msgs::Bool> *relay4_sub;
DiffController *dc;
ServoWrapper servo1(1, hServo.servo1);
ServoWrapper servo2(2, hServo.servo2);
ServoWrapper servo3(3, hServo.servo3);
ServoWrapper servo4(4, hServo.servo4);
ServoWrapper servo5(5, hServo.servo5);
ServoWrapper servo6(6, hServo.servo6);
void cmdVelCallback(const geometry_msgs::Twist& msg)
{
dc->setSpeed(msg.linear.x, msg.angular.z);
#ifdef DEBUG
Serial.printf("[cmdVelCallback] linear: %f angular %f\r\n", msg.linear.x, msg.angular.z);
#endif
}
void relay1Callback(const std_msgs::Bool& i)
{
if (i.data==true) hSens1.pin1.write(1);
else hSens1.pin1.write(0);;
//hSens1.pin1.toggle();
#ifdef DEBUG
Serial.printf("[relay1Callback] relay status: %d\r\n", i.data);
#endif
}
void relay2Callback(const std_msgs::Bool& i)
{
if (i.data==true) hSens1.pin2.write(1);
else hSens1.pin2.write(0);;
//hSens1.pin2.toggle();
#ifdef DEBUG
Serial.printf("[relay2Callback] relay status: %d\r\n", i.data);
#endif
}
void relay3Callback(const std_msgs::Bool& i)
{
if (i.data==true) hSens1.pin3.write(1);
else hSens1.pin3.write(0);;
//hSens1.pin3.toggle();
#ifdef DEBUG
Serial.printf("[relay3Callback] relay status: %d\r\n", i.data);
#endif
}
void relay4Callback(const std_msgs::Bool& i)
{
if (i.data==true) hSens1.pin4.write(1);
else hSens1.pin4.write(0);;
//hSens1.pin4.toggle();
#ifdef DEBUG
Serial.printf("[relay4Callback] relay status: %d\r\n", i.data);
#endif
}
void initROS()
{
battery_pub = new ros::Publisher("/battery", &battery);
odom_pub = new ros::Publisher("/odom", &odom);
joint_states_pub = new ros::Publisher("/joint_states", &joint_states);
twist_sub = new ros::Subscriber<geometry_msgs::Twist>("/cmd_vel", &cmdVelCallback);
relay1_sub = new ros::Subscriber<std_msgs::Bool>("/relay1", &relay1Callback);
relay2_sub = new ros::Subscriber<std_msgs::Bool>("/relay2", &relay2Callback);
relay3_sub = new ros::Subscriber<std_msgs::Bool>("/relay3", &relay3Callback);
relay4_sub = new ros::Subscriber<std_msgs::Bool>("/relay4", &relay4Callback);
ros::Subscriber<std_msgs::Int16, ServoWrapper> *servo1_angle_sub =
new ros::Subscriber<std_msgs::Int16, ServoWrapper>("/servo1/angle", &ServoWrapper::angleCallback, &servo1);
ros::Subscriber<std_msgs::Int16, ServoWrapper> *servo2_angle_sub =
new ros::Subscriber<std_msgs::Int16, ServoWrapper>("/servo2/angle", &ServoWrapper::angleCallback, &servo2);
ros::Subscriber<std_msgs::Int16, ServoWrapper> *servo3_angle_sub =
new ros::Subscriber<std_msgs::Int16, ServoWrapper>("/servo3/angle", &ServoWrapper::angleCallback, &servo3);
ros::Subscriber<std_msgs::Int16, ServoWrapper> *servo4_angle_sub =
new ros::Subscriber<std_msgs::Int16, ServoWrapper>("/servo4/angle", &ServoWrapper::angleCallback, &servo4);
ros::Subscriber<std_msgs::Int16, ServoWrapper> *servo5_angle_sub =
new ros::Subscriber<std_msgs::Int16, ServoWrapper>("/servo5/angle", &ServoWrapper::angleCallback, &servo5);
ros::Subscriber<std_msgs::Int16, ServoWrapper> *servo6_angle_sub =
new ros::Subscriber<std_msgs::Int16, ServoWrapper>("/servo6/angle", &ServoWrapper::angleCallback, &servo6);
ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper> *servo1_pwm_sub =
new ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper>("/servo1/pwm", &ServoWrapper::pwmCallback, &servo1);
ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper> *servo2_pwm_sub =
new ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper>("/servo2/pwm", &ServoWrapper::pwmCallback, &servo2);
ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper> *servo3_pwm_sub =
new ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper>("/servo3/pwm", &ServoWrapper::pwmCallback, &servo3);
ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper> *servo4_pwm_sub =
new ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper>("/servo4/pwm", &ServoWrapper::pwmCallback, &servo4);
ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper> *servo5_pwm_sub =
new ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper>("/servo5/pwm", &ServoWrapper::pwmCallback, &servo5);
ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper> *servo6_pwm_sub =
new ros::Subscriber<std_msgs::UInt16MultiArray, ServoWrapper>("/servo6/pwm", &ServoWrapper::pwmCallback, &servo6);
nh.advertise(*battery_pub);
nh.advertise(*odom_pub);
nh.advertise(*joint_states_pub);
nh.subscribe(*relay1_sub);
nh.subscribe(*relay2_sub);
nh.subscribe(*relay3_sub);
nh.subscribe(*relay4_sub);
nh.subscribe(*twist_sub);
nh.subscribe(*servo1_angle_sub);
nh.subscribe(*servo2_angle_sub);
nh.subscribe(*servo3_angle_sub);
nh.subscribe(*servo4_angle_sub);
nh.subscribe(*servo5_angle_sub);
nh.subscribe(*servo6_angle_sub);
nh.subscribe(*servo1_pwm_sub);
nh.subscribe(*servo2_pwm_sub);
nh.subscribe(*servo3_pwm_sub);
nh.subscribe(*servo4_pwm_sub);
nh.subscribe(*servo5_pwm_sub);
nh.subscribe(*servo6_pwm_sub);
}
void setupServos()
{
hServo.enablePower();
hServo.setPeriod(SERVO_PERIOD);
switch(SERVO_VOLTAGE) {
case VOLTAGE_5V:
hServo.setVoltage5V();
break;
case VOLTAGE_6V:
hServo.setVoltage6V();
break;
case VOLTAGE_7V4:
hServo.setVoltage7V4();
break;
case VOLTAGE_8V6:
hServo.setVoltage8V6();
}
hServo.servo1.calibrate(SERVO_1_ANGLE_MIN, SERVO_1_WIDTH_MIN,
SERVO_1_ANGLE_MAX, SERVO_1_WIDTH_MAX);
hServo.servo2.calibrate(SERVO_2_ANGLE_MIN, SERVO_2_WIDTH_MIN,
SERVO_2_ANGLE_MAX, SERVO_2_WIDTH_MAX);
hServo.servo3.calibrate(SERVO_3_ANGLE_MIN, SERVO_3_WIDTH_MIN,
SERVO_3_ANGLE_MAX, SERVO_3_WIDTH_MAX);
hServo.servo4.calibrate(SERVO_4_ANGLE_MIN, SERVO_4_WIDTH_MIN,
SERVO_4_ANGLE_MAX, SERVO_4_WIDTH_MAX);
hServo.servo5.calibrate(SERVO_5_ANGLE_MIN, SERVO_5_WIDTH_MIN,
SERVO_5_ANGLE_MAX, SERVO_5_WIDTH_MAX);
hServo.servo6.calibrate(SERVO_6_ANGLE_MIN, SERVO_6_WIDTH_MIN,
SERVO_6_ANGLE_MAX, SERVO_6_WIDTH_MAX);
}
void setupJoints()
{
joint_states.header.frame_id = "base_link";
joint_states.name = new char*[4] {
"wheel_FL_joint", "wheel_RL_joint",
"wheel_FR_joint", "wheel_RR_joint"
};
joint_states.position = new float[4];
joint_states.velocity = new float[4];
joint_states.effort = new float[4];
joint_states.name_length = 4;
joint_states.position_length = 4;
joint_states.velocity_length = 4;
joint_states.effort_length = 4;
}
void batteryLoop()
{
uint32_t t = sys.getRefTime();
long dt = 1000;
while(true)
{
if (!publish_battery)
{
battery.data = sys.getSupplyVoltage();
publish_battery = true;
}
sys.delaySync(t, dt);
}
}
void odomLoop()
{
uint32_t t = sys.getRefTime();
long dt = 50;
while(true)
{
if (!publish_odom)
{
std::vector<float> odo = dc->getOdom();
odom.linear.x = odo[0];
odom.angular.z = odo[1];
publish_odom = true;
}
sys.delaySync(t, dt);
}
}
void jointStatesLoop()
{
uint32_t t = sys.getRefTime();
long dt = 50;
while(true)
{
if (!publish_joint)
{
std::vector<float> pos = dc->getWheelPositions();
std::vector<float> vel = dc->getWheelVelocities();
std::vector<float> eff = dc->getWheelEfforts();
joint_states.header.stamp = nh.now();
joint_states.position[0] = pos[0];
joint_states.position[1] = pos[1];
joint_states.position[2] = pos[2];
joint_states.position[3] = pos[3];
joint_states.velocity[0] = vel[0];
joint_states.velocity[1] = vel[1];
joint_states.velocity[2] = vel[2];
joint_states.velocity[3] = vel[3];
joint_states.effort[0] = eff[0];
joint_states.effort[1] = eff[1];
joint_states.effort[2] = eff[2];
joint_states.effort[3] = eff[3];
publish_joint = true;
}
sys.delaySync(t, dt);
}
}
void LEDLoop()
{
uint32_t t = sys.getRefTime();
long dt = 250;
while(true)
{
if(!nh.connected())
LED.toggle();
else
LED.write(true);
sys.delaySync(t, dt);
}
}
void hMain()
{
uint32_t t = sys.getRefTime();
platform.begin(&RPi);
nh.getHardware()->initWithDevice(&platform.LocalSerial);
//nh.getHardware()->initWithDevice(&RPi);
nh.initNode();
dc = new DiffController(INPUT_TIMEOUT);
dc->start();
setupServos();
setupJoints();
initROS();
LED.setOut();
hSens1.pin1.setOut();
hSens1.pin2.setOut();
hSens1.pin3.setOut();
hSens1.pin4.setOut();
sys.taskCreate(&LEDLoop);
sys.taskCreate(&batteryLoop);
sys.taskCreate(&odomLoop);
sys.taskCreate(&jointStatesLoop);
while (true)
{
nh.spinOnce();
if (publish_battery){
battery_pub->publish(&battery);
publish_battery = false;
}
if (publish_odom){
odom_pub->publish(&odom);
publish_odom = false;
}
if (publish_joint){
joint_states_pub->publish(&joint_states);
publish_joint = false;
}
sys.delaySync(t, 10);
}
}