-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_tilty.cpp
More file actions
446 lines (343 loc) · 10.8 KB
/
Copy pathmain_tilty.cpp
File metadata and controls
446 lines (343 loc) · 10.8 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <stdint.h> // atof()
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include "shell.h"
#include "usbcfg.h"
#include <r2p/Middleware.hpp>
#include <r2p/node/led.hpp>
#include <r2p/msg/motor.hpp>
#include <r2p/msg/imu.hpp>
#define USE_USB_SERIAL 1
#include <ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Float32.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <r2p_msgs/ImuRaw.h>
#include <r2p_msgs/PidParameters.h>
#include <r2p_msgs/Vector3_32.h>
#ifndef R2P_MODULE_NAME
#define R2P_MODULE_NAME "uDC"
#endif
static WORKING_AREA(wa_info, 1024);
static r2p::RTCANTransport rtcantra(RTCAND1);
RTCANConfig rtcan_config = { 1000000, 100, 60 };
r2p::Middleware r2p::Middleware::instance(R2P_MODULE_NAME, "BOOT_"R2P_MODULE_NAME);
r2p::Node vel_node("velpub", false);
r2p::Publisher<r2p::Velocity3Msg> vel_pub;
r2p::Node balcfg_node("balcfg", false);
r2p::Publisher<r2p::PIDCfgMsg> balcfg_pub;
r2p::Node velcfg_node("velcfg", false);
r2p::Publisher<r2p::PIDCfgMsg> velcfg_pub;
ros::NodeHandle nh;
/*
* Cycle USB connection on power up.
*/
void usb_lld_disconnect_bus(USBDriver *usbp) {
(void)usbp;
palClearPort(GPIOA, (1<<GPIOA_USB_DM) | (1<<GPIOA_USB_DP));
palSetPadMode(GPIOA, GPIOA_USB_DM, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(GPIOA, GPIOA_USB_DP, PAL_MODE_OUTPUT_PUSHPULL);
}
void usb_lld_connect_bus(USBDriver *usbp) {
(void)usbp;
palClearPort(GPIOA, (1<<GPIOA_USB_DM) | (1<<GPIOA_USB_DP));
palSetPadMode(GPIOA, GPIOA_USB_DM, PAL_MODE_ALTERNATE(14));
palSetPadMode(GPIOA, GPIOA_USB_DP, PAL_MODE_ALTERNATE(14));
}
/*===========================================================================*/
/* Command line related. */
/*===========================================================================*/
#define SHELL_WA_SIZE THD_WA_SIZE(2048)
#define TEST_WA_SIZE THD_WA_SIZE(256)
static void cmd_mem(BaseSequentialStream *chp, int argc, char *argv[]) {
size_t n, size;
(void) argv;
if (argc > 0) {
chprintf(chp, "Usage: mem\r\n");
return;
}
n = chHeapStatus(NULL, &size);
chprintf(chp, "core free memory : %u bytes\r\n", chCoreStatus());
chprintf(chp, "heap fragments : %u\r\n", n);
chprintf(chp, "heap free total : %u bytes\r\n", size);
}
static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[]) {
static const char *states[] = { THD_STATE_NAMES };
Thread *tp;
(void) argv;
if (argc > 0) {
chprintf(chp, "Usage: threads\r\n");
return;
}
chprintf(chp, " addr stack prio refs state time\r\n");
tp = chRegFirstThread();
do {
chprintf(chp, "%.8lx %.8lx %4lu %4lu %9s %lu\r\n", (uint32_t) tp, (uint32_t) tp->p_ctx.r13,
(uint32_t) tp->p_prio, (uint32_t)(tp->p_refs - 1), states[tp->p_state], (uint32_t) tp->p_time);
tp = chRegNextThread(tp);
} while (tp != NULL);
}
static void cmd_balcfg(BaseSequentialStream *chp, int argc, char *argv[]) {
r2p::PIDCfgMsg * msgp;
(void) argv;
if (argc != 3) {
chprintf(chp, "Usage: bcfg <k> <ti> <td>\r\n");
return;
}
balcfg_node.set_enabled(true);
if (balcfg_pub.alloc(msgp)) {
msgp->k = atof(argv[0]);
msgp->ti = atof(argv[1]);
msgp->td = atof(argv[2]);
balcfg_pub.publish(*msgp);
}
balcfg_node.set_enabled(false);
}
static void cmd_velcfg(BaseSequentialStream *chp, int argc, char *argv[]) {
r2p::PIDCfgMsg * msgp;
(void) argv;
if (argc != 3) {
chprintf(chp, "Usage: vcfg <k> <ti> <td>\r\n");
return;
}
velcfg_node.set_enabled(true);
if (velcfg_pub.alloc(msgp)) {
msgp->k = atof(argv[0]);
msgp->ti = atof(argv[1]);
msgp->td = atof(argv[2]);
velcfg_pub.publish(*msgp);
}
velcfg_node.set_enabled(false);
}
static const ShellCommand commands[] = { { "mem", cmd_mem }, { "threads", cmd_threads },
{ "bcfg", cmd_balcfg }, { "vcfg", cmd_velcfg }, { NULL, NULL } };
static const ShellConfig usb_shell_cfg = { (BaseSequentialStream *) &SDU1, commands };
static const ShellConfig serial_shell_cfg = { (BaseSequentialStream *) &SD3, commands };
/*
* R2P subscriber node.
*/
struct imu_data_t {
float roll;
float pitch;
float yaw;
} imu_data;
struct imu_raw_data_t {
int16_t acc_x;
int16_t acc_y;
int16_t acc_z;
int16_t gyro_x;
int16_t gyro_y;
int16_t gyro_z;
int16_t mag_x;
int16_t mag_y;
int16_t mag_z;
} imu_raw_data;
struct odometry_data_t {
float x;
float y;
float w;
} odometry_data;
bool imu_cb(const r2p::IMUMsg &msg) {
imu_data.roll = msg.roll;
imu_data.pitch = msg.pitch;
imu_data.yaw= msg.yaw;
return true;
}
bool imu_raw_cb(const r2p::IMURaw9 &msg) {
imu_raw_data.acc_x = msg.acc_x;
imu_raw_data.acc_y = msg.acc_y;
imu_raw_data.acc_z = msg.acc_z;
imu_raw_data.gyro_x = msg.gyro_x;
imu_raw_data.gyro_y = msg.gyro_y;
imu_raw_data.gyro_z = msg.gyro_z;
imu_raw_data.mag_x = msg.mag_x;
imu_raw_data.mag_y = msg.mag_y;
imu_raw_data.mag_z = msg.mag_z;
return true;
}
bool odometry_cb(const r2p::Velocity3Msg &msg) {
odometry_data.x = msg.x;
odometry_data.y = msg.y;
odometry_data.w= msg.w;
return true;
}
msg_t r2p_sub_node(void * arg) {
r2p::Node node("r2p_sub");
r2p::Subscriber<r2p::IMUMsg, 5> imu_sub(imu_cb);
r2p::Subscriber<r2p::IMURaw9, 5> imu_raw_sub(imu_raw_cb);
r2p::Subscriber<r2p::Velocity3Msg, 5> odometry_sub(odometry_cb);
(void) arg;
chRegSetThreadName("r2p_sub");
node.subscribe(imu_sub, "imu");
node.subscribe(imu_raw_sub, "imu_raw");
node.subscribe(odometry_sub, "odometry");
for (;;) {
node.spin(r2p::Time::ms(1000));
}
return CH_SUCCESS;
}
/*
* ROS rosserial publisher thread.
*/
float yaw = 0;
geometry_msgs::Vector3 odom_msg;
msg_t rosserial_pub_thread(void * arg) {
r2p_msgs::Vector3_32 odometry_msg;
r2p_msgs::Vector3_32 imu_msg;
r2p_msgs::ImuRaw imu_raw_msg;
ros::Publisher odometry_pub("odom", &odometry_msg);
ros::Publisher imu_pub("imu", &imu_msg);
ros::Publisher imu_raw_pub("imu_raw", &imu_raw_msg);
systime_t last_sample;
odom_msg.x = 0.0;
odom_msg.y = 0.0;
odom_msg.z = 0.0;
(void) arg;
chRegSetThreadName("rosserial_pub");
nh.initNode();
nh.advertise(odometry_pub);
nh.advertise(imu_pub);
nh.advertise(imu_raw_pub);
for (;;) {
last_sample = chTimeNow();
odometry_msg.x = odometry_data.x;
odometry_msg.y = odometry_data.y;
odometry_msg.z = odometry_data.w;
odometry_pub.publish(&odometry_msg);
imu_msg.x = imu_data.roll;
imu_msg.y = imu_data.pitch;
imu_msg.z = imu_data.yaw;
imu_pub.publish(&imu_msg);
imu_raw_msg.linear_acceleration.x = imu_raw_data.acc_x;
imu_raw_msg.linear_acceleration.y = imu_raw_data.acc_y;
imu_raw_msg.linear_acceleration.z = imu_raw_data.acc_z;
imu_raw_msg.angular_velocity.x = imu_raw_data.gyro_x;
imu_raw_msg.angular_velocity.y = imu_raw_data.gyro_y;
imu_raw_msg.angular_velocity.z = imu_raw_data.gyro_z;
imu_raw_msg.magnetic_field.x = imu_raw_data.mag_x;
imu_raw_msg.magnetic_field.y = imu_raw_data.mag_y;
imu_raw_msg.magnetic_field.z = imu_raw_data.mag_z;
imu_raw_pub.publish(&imu_raw_msg);
nh.spinOnce();
// Sleep for 50 milliseconds from last execution
chThdSleepUntil(last_sample + MS2ST(50));
}
return CH_SUCCESS;
}
/*
* ROS rosserial subscriber thread.
*/
void cmd_vel_cb( const geometry_msgs::Twist& cmd_vel_msg){
r2p::Velocity3Msg * msgp;
vel_node.set_enabled(true);
// Motor setpoints
if (vel_pub.alloc(msgp)) {
msgp->x = cmd_vel_msg.linear.x;
msgp->y = cmd_vel_msg.linear.y;
msgp->w = cmd_vel_msg.angular.z;
vel_pub.publish(*msgp);
}
vel_node.set_enabled(false);
}
void balcfg_cb( const r2p_msgs::PidParameters& PID_config_msg){
r2p::PIDCfgMsg * msgp;
balcfg_node.set_enabled(true);
if (balcfg_pub.alloc(msgp)) {
msgp->k = PID_config_msg.k;
msgp->ti = PID_config_msg.ti;
msgp->td = PID_config_msg.td;
balcfg_pub.publish(*msgp);
}
balcfg_node.set_enabled(false);
}
void velcfg_cb( const r2p_msgs::PidParameters& PID_config_msg){
r2p::PIDCfgMsg * msgp;
velcfg_node.set_enabled(true);
if (velcfg_pub.alloc(msgp)) {
msgp->k = PID_config_msg.k;
msgp->ti = PID_config_msg.ti;
msgp->td = PID_config_msg.td;
velcfg_pub.publish(*msgp);
}
velcfg_node.set_enabled(false);
}
msg_t rosserial_sub_thread(void * arg) {
ros::Subscriber<geometry_msgs::Twist> cmd_vel_sub("cmd_vel", &cmd_vel_cb );
ros::Subscriber<r2p_msgs::PidParameters> balcfg_sub("balcfg", balcfg_cb );
ros::Subscriber<r2p_msgs::PidParameters> velcfg_sub("velcfg", velcfg_cb );
(void) arg;
chRegSetThreadName("cmd_vel_sub");
nh.initNode();
nh.subscribe(cmd_vel_sub);
nh.subscribe(balcfg_sub);
nh.subscribe(velcfg_sub);
for (;;) {
nh.spinOnce();
chThdSleepMilliseconds(5);
}
return CH_SUCCESS;
}
/*
* Application entry point.
*/
extern "C" {
int main(void) {
// Thread *usb_shelltp = NULL;
// Thread *serial_shelltp = NULL;
halInit();
chSysInit();
/*
* Initializes a serial-over-USB CDC driver.
*/
sduObjectInit(&SDU1);
sduStart(&SDU1, &serusbcfg);
/*
* Activates the USB driver and then the USB bus pull-up on D+.
* Note, a delay is inserted in order to not have to disconnect the cable
* after a reset.
*/
usbDisconnectBus(serusbcfg.usbp);
chThdSleepMilliseconds(500);
usbStart(serusbcfg.usbp, &usbcfg);
usbConnectBus(serusbcfg.usbp);
/* Start the serial driver. */
sdStart(&SD3, NULL);
/*
* Shell manager initialization.
*/
// shellInit();
r2p::Middleware::instance.initialize(wa_info, sizeof(wa_info), r2p::Thread::LOWEST);
rtcantra.initialize(rtcan_config);
r2p::Middleware::instance.start();
r2p::ledsub_conf ledsub_conf = {"led"};
r2p::Thread::create_heap(NULL, THD_WA_SIZE(512), NORMALPRIO, r2p::ledsub_node, &ledsub_conf);
r2p::Thread::create_heap(NULL, THD_WA_SIZE(1024), NORMALPRIO, r2p_sub_node, NULL);
r2p::Thread::create_heap(NULL, THD_WA_SIZE(4096), NORMALPRIO, rosserial_pub_thread, NULL);
r2p::Thread::create_heap(NULL, THD_WA_SIZE(2048), NORMALPRIO, rosserial_sub_thread, NULL);
balcfg_node.advertise(balcfg_pub, "balcfg", r2p::Time::INFINITE);
velcfg_node.advertise(velcfg_pub, "velcfg", r2p::Time::INFINITE);
vel_node.advertise(vel_pub, "velocity", r2p::Time::INFINITE);
for (;;) {
/*
if (!usb_shelltp && (SDU1.config->usbp->state == USB_ACTIVE))
usb_shelltp = shellCreate(&usb_shell_cfg, SHELL_WA_SIZE, NORMALPRIO);
else if (chThdTerminated(usb_shelltp)) {
chThdRelease(usb_shelltp);
usb_shelltp = NULL;
}
*/
/*
if (!serial_shelltp)
serial_shelltp = shellCreate(&serial_shell_cfg, SHELL_WA_SIZE, NORMALPRIO);
else if (chThdTerminated(serial_shelltp)) {
chThdRelease(serial_shelltp);
serial_shelltp = NULL;
}
*/
r2p::Thread::sleep(r2p::Time::ms(500));
}
return CH_SUCCESS;
}
}