-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_triskar.cpp
More file actions
332 lines (253 loc) · 8.46 KB
/
Copy pathmain_triskar.cpp
File metadata and controls
332 lines (253 loc) · 8.46 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
#include <stdlib.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>
#ifndef R2P_MODULE_NAME
#define R2P_MODULE_NAME "USB"
#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("speedpub", false);
r2p::Publisher<r2p::Speed3Msg> vel_pub;
bool speed_first_time = true;
r2p::Node pidcfg_node("pidcfg", false);
r2p::Publisher<r2p::PIDCfgMsg> pidcfg_pub;
bool pidcfg_first_time = true;
BaseSequentialStream * serialp;
bool stream_enc = false;
/*
* DP resistor control is not possible on the STM32F3-Discovery, using stubs
* for the connection macros.
*/
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));
}
/*===========================================================================*/
/* Kinematics. */
/*===========================================================================*/
template<typename T> static inline T clamp(T min, T value, T max) {
return (value < min) ? min : ((value > max) ? max : value);
}
/*
* //_______________________\\
* // x \\
* \ 2 ^ 1 /
* \ | /
* \ | /
* \ y<----@ /
* \ z /
* \ /
* \ /
* \ 3 /
* \___/
* =====
*
* Body frame velocity to wheel angular velocity:
* R * dth1 = cos(60°) * dx - cos(30°) * dy - L * dgamma
* R * dth2 = cos(60°) * dx + cos(30°) * dy - L * dgamma
* R * dth3 = -dx - L * dgamma
*
* Name mapping and units:
* dx = strafe [m/s]
* dy = forward [m/s]
* dgamma = angular [rad/s]
* dth{1,2,3} [rad/s]
*/
// Robot parameters
#define _L 0.160f // Wheel distance [m]
#define _R 0.035f // Wheel radius [m]
#define _MAX_DTH 52.0f // Maximum wheel angular speed [rad/s]
#define _m1_R (-1.0f / _R)
#define _mL_R (-_L / _R)
#define _C60_R (0.500000000f / _R) // cos(60°) / R
#define _C30_R (0.866025404f / _R) // cos(30°) / R
#define _TICKS 64.0f
#define _RATIO 29.0f
#define _PI 3.14159265359f
#define R2T(r) (_TICKS * _RATIO)/(r * 2 * _PI)
#define T2R(t) (t / R2T)
#define M2T(m) (m * _TICKS * _RATIO)/(2 * _PI * _R)
#define T2M(t) (t / _M2TICK)
/*===========================================================================*/
/* Command line related. */
/*===========================================================================*/
#define SHELL_WA_SIZE THD_WA_SIZE(2048)
static void cmd_run(BaseSequentialStream *chp, int argc, char *argv[]) {
r2p::Speed3Msg * msgp;
(void) argv;
if (argc != 3) {
chprintf(chp, "Usage: run <forward> <strafe> <angular>\r\n");
return;
}
vel_node.set_enabled(true);
if (speed_first_time) {
vel_node.advertise(vel_pub, "speed3", r2p::Time::INFINITE);
speed_first_time = false;
}
float x = atof(argv[0]);
float y = atof(argv[1]);
float w = atof(argv[2]);
// Wheel angular speeds
const float dthz123 = _mL_R * w;
const float dx12 = _C60_R * y;
const float dy12 = _C30_R * x;
float dth1 = dx12 - dy12 + dthz123;
float dth2 = dx12 + dy12 + dthz123;
float dth3 = _m1_R * y + dthz123;
// Motor setpoints
if (vel_pub.alloc(msgp)) {
msgp->value[0] = (int16_t) clamp(-_MAX_DTH, dth1, _MAX_DTH);
msgp->value[1] = (int16_t) clamp(-_MAX_DTH, dth2, _MAX_DTH);
msgp->value[2] = (int16_t) clamp(-_MAX_DTH, dth3, _MAX_DTH);
vel_pub.publish(*msgp);
}
chprintf(chp, "SETPOINT: %f %f %f\r\n", dth1, dth2, dth3);
vel_node.set_enabled(false);
}
static void cmd_stop(BaseSequentialStream *chp, int argc, char *argv[]) {
r2p::Speed3Msg * msgp;
(void) argv;
if (argc > 0) {
chprintf(chp, "Usage: stop\r\n");
return;
}
vel_node.set_enabled(true);
if (speed_first_time) {
vel_node.advertise(vel_pub, "speed3", r2p::Time::INFINITE);
speed_first_time = false;
}
// Stop motors
if (vel_pub.alloc(msgp)) {
msgp->value[0] = 0;
msgp->value[1] = 0;
msgp->value[2] = 0;
vel_pub.publish(*msgp);
}
vel_node.set_enabled(false);
}
static void cmd_enc(BaseSequentialStream *chp, int argc, char *argv[]) {
(void) argv;
if (argc > 0) {
chprintf(chp, "Usage: e\r\n");
return;
}
serialp = chp;
stream_enc = !stream_enc;
}
static void cmd_pidcfg(BaseSequentialStream *chp, int argc, char *argv[]) {
r2p::PIDCfgMsg * msgp;
(void) argv;
if (argc != 3) {
chprintf(chp, "Usage: pidcfg <k> <ti> <td>\r\n");
return;
}
pidcfg_node.set_enabled(true);
if (pidcfg_first_time) {
pidcfg_node.advertise(pidcfg_pub, "pidcfg", r2p::Time::INFINITE);
pidcfg_first_time = false;
}
if (pidcfg_pub.alloc(msgp)) {
msgp->k = atof(argv[0]);
msgp->ti = atof(argv[1]);
msgp->td = atof(argv[2]);
pidcfg_pub.publish(*msgp);
}
pidcfg_node.set_enabled(false);
}
static const ShellCommand commands[] = { { "r", cmd_run }, { "s", cmd_stop }, { "e", cmd_enc }, { "pidcfg", cmd_pidcfg}, { NULL, NULL } };
static const ShellConfig usb_shell_cfg = { (BaseSequentialStream *) &SDU1, commands };
static const ShellConfig serial_shell_cfg = { (BaseSequentialStream *) &SD3, commands };
/*
* Encoder subscriber node.
*/
msg_t encoder_sub_node(void * arg) {
r2p::Node node("enc1_sub");
r2p::Subscriber<r2p::EncoderMsg, 5> enc_sub;
r2p::EncoderMsg * msgp;
(void) arg;
chRegSetThreadName("enc_sub");
node.subscribe(enc_sub, "encoder1");
for (;;) {
node.spin(r2p::Time::ms(1000));
if (enc_sub.fetch(msgp)) {
if (stream_enc) {
chprintf((BaseSequentialStream*) serialp, "%f\r\n", msgp->delta * 50); // delta_rad to rad/s
}
enc_sub.release(*msgp);
} else {
r2p::Thread::sleep(r2p::Time::ms(1));
}
}
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::ledpub_conf ledpub_conf = { "led", 1 };
r2p::Thread::create_heap(NULL, THD_WA_SIZE(512), NORMALPRIO, r2p::ledpub_node, &ledpub_conf);
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, encoder_sub_node, NULL);
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); /* Recovers memory of the previous shell. */
usb_shelltp = NULL; /* Triggers spawning of a new shell. */
}
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;
}
}