-
Notifications
You must be signed in to change notification settings - Fork 1
/
cabot-arduino-ace.ino
262 lines (232 loc) · 7.06 KB
/
cabot-arduino-ace.ino
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
/*******************************************************************************
* Copyright (c) 2020, 2023 Carnegie Mellon University and Miraikan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*******************************************************************************/
#ifdef ESP32
#undef ESP32
#include "CaBotHandle.hpp"
#define ESP32
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
#else
#include "CaBotHandle.hpp"
#endif
#include <arduino-timer.h>
#include "Arduino.h"
#include "uart_com.h"
#include "BarometerReader.hpp"
#include "ButtonsReader.hpp"
#include "Heartbeat.h"
#include "IMUReader.hpp"
#include "WiFiReader.hpp"
#include "TouchReader.hpp"
#include "VibratorController.hpp"
cabot::Handle ch;
uart_com uart(ch);
Timer < 10 > timer;
// configurations
#define BAUDRATE (115200UL)
#define HEARTBEAT_DELAY (20)
#ifdef ESP32
// TODO: need to reconfigure
#define BTN1_PIN (13) // up
#define BTN2_PIN (14) // down
#define BTN3_PIN (15) // left
#define BTN4_PIN (16) // right
#define VIB1_PIN (19) //front
#define VIB2_PIN (20) //back //not using
#define VIB3_PIN (18) //left
#define VIB4_PIN (17) //right
#else
#define BTN1_PIN (2) // up
#define BTN2_PIN (3) // down
#define BTN3_PIN (4) // left
#define BTN4_PIN (5) // right
#define VIB1_PIN (11) //front
#define VIB2_PIN (6) //back //not using
#define VIB3_PIN (10) //left
#define VIB4_PIN (9) //right
#endif
#define TOUCH_BASELINE (128)
#define TOUCH_THRESHOLD_DEFAULT (64)
#define RELEASE_THRESHOLD_DEFAULT (24)
#define TIMEOUT_DEFAULT (500)
uart_com urt_cm(ch);
// sensors
BarometerReader bmpReader(ch);
ButtonsReader buttonsReader(ch, urt_cm);
IMUReader imuReader(ch);
WiFiReader wifiReader(ch);
TouchReader touchReader(ch, urt_cm);
// controllers
VibratorController vibratorController(ch, urt_cm);
Heartbeat heartbeat(LED_BUILTIN, HEARTBEAT_DELAY);
void setup()
{
// set baud rate
ch.setBaudRate(BAUDRATE);
urt_cm.begin(19200);
// connect to rosserial
ch.init();
ch.loginfo("Connected");
while (!ch.connected()) {
ch.spinOnce();
}
int run_imu_calibration = 0;
ch.getParam("run_imu_calibration", &run_imu_calibration, 1, TIMEOUT_DEFAULT);
if (run_imu_calibration != 0) {
imuReader.calibration();
timer.every(
100, [] (void *) {
imuReader.update();
imuReader.update_calibration();
return true;
});
ch.loginfo("Calibration Mode started");
return;
}
int calibration_params[22];
uint8_t * offsets = NULL;
if (ch.getParam(
"calibration_params", calibration_params, 22,
TIMEOUT_DEFAULT))
{
offsets = (uint8_t *)malloc(sizeof(uint8_t) * 22);
char default_values[128];
int n = 0;
n = snprintf(default_values+n, sizeof(default_values), "Using [");
for (int i = 0; i < 22; i++) {
offsets[i] = calibration_params[i] & 0xFF;
n += snprintf(default_values+n, sizeof(default_values)-n, "%d", offsets[i]);
if (i < 21) {
n += snprintf(default_values+n, sizeof(default_values)-n, ",");
}
}
n += snprintf(default_values+n, sizeof(default_values)-n, "] for calibration_params");
ch.loginfo(default_values);
} else {
ch.logwarn("clibration_params is needed to use IMU (BNO055) correctly.");
ch.logwarn("You can run calibration by setting _run_imu_calibration:=1");
ch.logwarn("You can check calibration value with /calibration topic.");
ch.logwarn(
"First 22 byte is calibration data, following 4 byte is calibration "
"status for");
ch.logwarn(
"System, Gyro, Accel, Magnet, 0 (not configured) <-> 3 (configured)");
ch.logwarn("Specify like calibration_params:=[0, 0, 0, 0 ...]");
ch.logwarn("Visit the following link to check how to calibrate sensoe");
ch.logwarn(
"https://learn.adafruit.com/"
"adafruit-bno055-absolute-orientation-sensor/device-calibration");
}
ch.loginfo("setting up WiFi");
wifiReader.init(
[] (char * buf) {
//ch.loginfo(buf);//TODO*
});
int touch_params[3];
int touch_baseline;
int touch_threshold;
int release_threshold;
if (!ch.getParam("touch_params", touch_params, 3, TIMEOUT_DEFAULT)) {
ch.logwarn(
"Please use touch_params:=[baseline,touch,release] format to set touch "
"params");
touch_baseline = TOUCH_BASELINE;
touch_threshold = TOUCH_THRESHOLD_DEFAULT;
release_threshold = RELEASE_THRESHOLD_DEFAULT;
ch.logwarn(
" touched if the raw value is less than touch_params[0] - "
"touch_params[1]");
ch.logwarn(
" released if the raw value is higher than touch_params[0] - "
"touch_params[2]");
} else {
touch_baseline = touch_params[0];
touch_threshold = touch_params[1];
release_threshold = touch_params[2];
}
char default_values[48];
snprintf(
default_values, 48, "Using [%d, %d, %d] for touch_params",
touch_baseline, touch_threshold, release_threshold);
ch.loginfo(default_values);
// initialize
ch.loginfo("starting uart com");
urt_cm.init();
urt_cm.start();
ch.loginfo("setting up BMP280");
bmpReader.init();
ch.loginfo("setting up Buttons");
buttonsReader.init();
ch.loginfo("setting up BNO055");
imuReader.init(offsets);
ch.loginfo("setting up MPR121");
touchReader.init(touch_baseline, touch_threshold, release_threshold);
ch.loginfo("setting up vibrations");
vibratorController.init();
ch.loginfo("setting up heartbeat");
heartbeat.init();
// wait sensors ready
delay(100);
// set timers
timer.every(
1000, [] (void *) {
ch.sync();
return true;
});
timer.every(
500, [] (void *) {
bmpReader.update();
urt_cm.publish();
touchReader.diag_pub();
return true;
});
timer.every(
20, [] (void *) {
//heartbeat.update();
buttonsReader.update();
touchReader.update();
return true;
});
timer.every(
10, [] (void *) {
imuReader.update();
vibratorController.update();
return true;
});
timer.every(
20, [] (void *) {
wifiReader.update();
return true;
});
ch.loginfo("Arduino is ready");
}
void loop()
{
timer.tick < void > ();
urt_cm.update();
ch.spinOnce();
}
void restart()
{
ESP.restart();
}