-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
219 lines (165 loc) · 4.39 KB
/
Copy pathmain.cpp
File metadata and controls
219 lines (165 loc) · 4.39 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
#include <cstdio>
#include "ch.h"
#include "hal.h"
#include <r2p/Middleware.hpp>
#include <r2p/msg/motor.hpp>
#include <led/nodes/led.hpp>
static WORKING_AREA(wa_info, 1024);
static r2p::RTCANTransport rtcantra(RTCAND1);
RTCANConfig rtcan_config = { 1000000, 100, 60 };
r2p::Middleware r2p::Middleware::instance(MODULE_NAME, "BOOT_"MODULE_NAME);
uint16_t in_values[12] = {0};
uint16_t out_values[12] = {0};
static const uint16_t failsafe_values[12] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
systime_t last_update = 0;
bool failsafe = true;
int idx = 0;
static void icuwidthcb(ICUDriver *icup) {
const icucnt_t width = icuGetWidth(icup);
chSysLockFromIsr();
if ((width > 2500) || (width < 500)) {
// sync pulse or glitch
idx = 0;
chSysUnlockFromIsr();
return;
}
in_values[idx++] = width;
if (idx >= 8) {
idx = 0;
}
chSysUnlockFromIsr();
}
static void icuperiodcb(ICUDriver *icup) {
}
static ICUConfig icucfg = {
ICU_INPUT_ACTIVE_HIGH,
1000000, /* 1 MHz ICU clock frequency. */
icuwidthcb,
icuperiodcb,
NULL,
ICU_CHANNEL_2,
0
};
/*
* RC input node.
*/
struct rcin_node_conf {
const char * name;
const char * topic;
};
msg_t rcin_node(void * arg) {
rcin_node_conf * conf = (rcin_node_conf *) arg;
r2p::Node node(conf->name);
r2p::Publisher<r2p::ServoMsg> servo_pub;
r2p::ServoMsg * msgp;
(void) arg;
chRegSetThreadName(conf->name);
node.advertise(servo_pub, conf->topic);
icuStart(&ICUD4, &icucfg);
icuEnable(&ICUD4);
while (!chThdShouldTerminate()) {
if (servo_pub.alloc(msgp)) {
for (int i = 0; i < 8; i++) {
msgp->pulse[i] = in_values[i];
}
servo_pub.publish(msgp);
}
chThdSleepMilliseconds(20);
}
return CH_SUCCESS;
}
/*
* PWM cyclic callback.
*/
static void pwmcb(PWMDriver *pwmp) {
const uint16_t * values;
(void) pwmp;
chSysLockFromIsr()
if (failsafe) {
values = failsafe_values;
} else {
values = out_values;
}
if (pwmp == &PWMD1) {
pwm_lld_enable_channel(pwmp, 0, values[6]);
pwm_lld_enable_channel(pwmp, 1, values[7]);
pwm_lld_enable_channel(pwmp, 2, values[5]);
pwm_lld_enable_channel(pwmp, 3, values[4]);
}
if (pwmp == &PWMD2) {
pwm_lld_enable_channel(pwmp, 0, values[8]);
pwm_lld_enable_channel(pwmp, 1, values[9]);
pwm_lld_enable_channel(pwmp, 2, values[10]);
pwm_lld_enable_channel(pwmp, 3, values[11]);
}
if (pwmp == &PWMD3) {
pwm_lld_enable_channel(pwmp, 0, values[0]);
pwm_lld_enable_channel(pwmp, 1, values[1]);
pwm_lld_enable_channel(pwmp, 2, values[2]);
pwm_lld_enable_channel(pwmp, 3, values[3]);
}
chSysUnlockFromIsr();
}
/*
* PWM configuration.
*/
static PWMConfig pwmcfg = { 1000000, /* 1 MHz PWM clock frequency. */
20000, /* 50 Hz frequency. */
pwmcb, { { PWM_OUTPUT_ACTIVE_HIGH, NULL }, { PWM_OUTPUT_ACTIVE_HIGH, NULL }, { PWM_OUTPUT_ACTIVE_HIGH, NULL }, {
PWM_OUTPUT_ACTIVE_HIGH, NULL } }, 0, 0};
/*
* RC output node.
*/
struct rcout_node_conf {
const char * name;
const char * topic;
};
bool servo_cb(const r2p::ServoMsg &msg) {
for (int i = 0; i < 8; i++) {
out_values[i] = msg.pulse[i];
}
last_update = chTimeNow();
return true;
}
msg_t rcout_node(void * arg) {
rcout_node_conf * conf = (rcout_node_conf *) arg;
r2p::Node node(conf->name);
r2p::Subscriber<r2p::ServoMsg, 5> servo_sub(servo_cb);
(void) arg;
chRegSetThreadName(conf->name);
node.subscribe(servo_sub, conf->topic);
pwmStart(&PWMD1, &pwmcfg);
pwmStart(&PWMD2, &pwmcfg);
pwmStart(&PWMD3, &pwmcfg);
while (!chThdShouldTerminate()) {
node.spin(r2p::Time::ms(1000));
}
return CH_SUCCESS;
}
/*
* Application entry point.
*/
extern "C" {
int main(void) {
halInit();
chSysInit();
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);
rcin_node_conf rcin_conf = {"rcin_node", "rcin"};
r2p::Thread::create_heap(NULL, THD_WA_SIZE(2048), NORMALPRIO + 1, rcin_node, &rcin_conf);
rcout_node_conf rcout_conf = {"rcout_node", "rcout"};
r2p::Thread::create_heap(NULL, THD_WA_SIZE(2048), NORMALPRIO + 1, rcout_node, &rcout_conf);
for (;;) {
if (chTimeNow() - last_update > 500) {
failsafe = true;
} else {
failsafe = false;
}
r2p::Thread::sleep(r2p::Time::ms(100));
}
return CH_SUCCESS;
}
}