-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadc.cpp
296 lines (258 loc) · 8.97 KB
/
adc.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
// ----------------------------------------------------------------------------
#include <fstream>
#include <pigpio.h>
#include "status.h"
#include "config.h"
#include "GY87.h"
#include "altFilter.h"
#include "ESCController.h"
#include "pid.h"
#include "debug.h"
// GPIOs
#define CAMERA_STATION_OUT 18
// #define PARACHUTE_OUT // No parachute yet
#define GY87_INTERRUPT_GPIO 4
#define HCSR04_TRIG 20
#define HCSR04_ECHO 16
#define HCSR04_VCC 21
// Global variables to be shared in threads
statusContainer status;
configuration config;
ESCController escControl;
GY87 gy87;
altFilter baroFilter;
altFilter sonarFilter;
PIDSystem pids;
// For HC-SR04+
uint32_t start = 0;
bool gotEcho = false;
int hcsr04pDelay = 0; // Due to limitations, update HC-SR04+ only at 10Hz
bool PIDEnabled = false;
// ----------------------------------------------------------------------------
// Callbacks
// Handling MPU6050 data ready interrupt at 100Hz
void gy87InterruptUpdater(int gpio, int level, uint32_t tick)
{
if (level==1) {
gy87.updateMPU();
//Atomic value assign
for (int i=0;i<3;i++) {
status.attitude[i] = gy87.ypr[i]; // rad
}
status.heading = gy87.mh; // rad
status.gyroscope[0] = (float)gy87.gyro.x/32768*1000*M_PI/180; // rad/s
status.gyroscope[1] = (float)gy87.gyro.y/32768*1000*M_PI/180;
status.gyroscope[2] = (float)gy87.gyro.z/32768*1000*M_PI/180;
status.gravity[0] = gy87.gravity.x;
status.gravity[1] = gy87.gravity.y;
status.gravity[2] = gy87.gravity.z;
status.accRelative[0] = (float)gy87.aaReal.x/8192*config.g; // m/s^2
status.accRelative[1] = (float)gy87.aaReal.y/8192*config.g;
status.accRelative[2] = (float)gy87.aaReal.z/8192*config.g;
status.accAbsolute[0] = (float)gy87.aaWorld.x/8192*config.g;
status.accAbsolute[1] = (float)gy87.aaWorld.y/8192*config.g;
status.accAbsolute[2] = (float)gy87.aaWorld.z/8192*config.g;
}
}
// HC-SR04+
// Handle echo
void getEcho (int gpio, int level, uint32_t tick)
{
if (level == 1) {
start = tick;
} else {
status.sonarAltitude = ((tick-start) * 1e-6 * status.sonicVelocity/2) * status.gravity[2];
gotEcho = true;
}
}
// Updater thread, works at 100Hz, Clock 0
void updater()
{
// -------------------------------------------------------------------------
// Sensors
// BMP180 barometer
gy87.updateBMP(config.seaLevelPressure);
status.temperature = gy87.temperature; // C
status.pressure = gy87.pressure; // Pa
status.baroAltitude = gy87.baroAltitude; // m
status.sonicVelocity = gy87.sonicVelocity; // m/s
// baroFilter
baroFilter.updateAltFilter(gy87.baroAltitude, status.accAbsolute[2], config.dt);
status.baroFilterAltitude = baroFilter.filterAltitude;
status.baroFilterVelocityZ = baroFilter.filterVelocityZ;
// HC-SR04+ trigger at 10Hz
if (hcsr04pDelay == 0) {
status.sonar = gotEcho;
gotEcho = false;
gpioTrigger(HCSR04_TRIG, 40, 1);
}
hcsr04pDelay += 1;
hcsr04pDelay = hcsr04pDelay % 10;
// sonarFilter, only when sonar is avaliable.
if (status.sonar) {
sonarFilter.updateAltFilter(status.sonarAltitude, status.accAbsolute[2], config.dt);
if (sonarFilter.filterAltitude < 0) {
// Not possible to < 0
status.sonarFilterAltitude = 0;
} else {
status.sonarFilterAltitude = sonarFilter.filterAltitude;
}
status.sonarFilterVelocityZ = sonarFilter.filterVelocityZ;
}
// -------------------------------------------------------------------------
// PID system
if (PIDEnabled) {
pids.update(status, config.dt);
escControl.YPRT(pids.yprt);
}
}
// ----------------------------------------------------------------------------
// Routines
// Initialize all sensors and motors
void systemInitialize()
{
info("Loading configuration file...");
std::ifstream configFile("config.txt");
configFile >> config;
configFile.close();
info("Initializing system...");
I2Cdev::initialize();
gpioInitialise();
// Initialize ESC controller
info("Initializing ESC controller...");
escControl.initialize(config.controlled_esc, config.ESCFrequency, config.ESCOutMin, config.ESCOutMax);
// Centering camera station
info("Centering camera station...");
gpioServo(CAMERA_STATION_OUT, 1500);
}
void initialize()
{
// Power sonar up
gpioWrite(HCSR04_VCC, 1);
// Start GY87
info("Initializing GY87 10-DOF IMU...");
gy87.initialize();
gy87.setOffset(config.gy87Offset);
info("Starting GY87 data gathering...");
gpioSetAlertFunc(GY87_INTERRUPT_GPIO, gy87InterruptUpdater);
gy87.startDMP();
// Wait 10s
info("Waiting for DMP data to stablize...");
gpioSleep(PI_TIME_RELATIVE, 10, 0);
// Initialization of altitude filter and starting conditions when GY87's data is stable
info("Recording starting yaw and magnetic heading...");
for (int i=0;i<3;i++) {
status.startAttitude[i] = status.attitude[i];
}
status.startHeading = status.heading;
info("Initializing altitude filters...");
// Run twice to get temperature and altitude
gy87.updateBMP(config.seaLevelPressure);
gy87.updateBMP(config.seaLevelPressure);
baroFilter.initialize(gy87.baroAltitude, config.baroFilterConfig);
sonarFilter.initialize(0, config.sonarFilterConfig);
info("Initializing PID system...");
pids.initialize(config.ratePIDSystemConfig, config.attitudePIDSystemConfig, config.ZPIDSystemConfig);
pids.setAttitudeTargets(status.startAttitude);
//pids.setVzTarget(0.028, ALTITUDE_BARO);
pids.setAltitudeTarget(0, ALTITUDE_SONAR);
info("Starting updater thread...");
gpioSetMode(HCSR04_ECHO, 0);
gpioSetMode(HCSR04_TRIG, 1);
gpioSetAlertFunc(HCSR04_ECHO, getEcho);
gpioSetTimerFunc(0, 10, updater);
// Wait for 2 seconds
gpioSleep(PI_TIME_RELATIVE, 2, 0);
info("Getting starting altitude from altitude filter reading...");
status.startAltitude = status.baroFilterAltitude;
info("Altitude = "+to_string(status.startAltitude));
}
void cleanup()
{
info("Cleaning up...");
gy87.reset();
gpioTerminate();
}
// ----------------------------------------------------------------------------
// Temporary
using namespace std;
void statusDisplayer()
{
//cout << "Y: " << status.attitude[0] << "\t";
//cout << "P: " << status.attitude[1] << "\t";
//cout << "R: " << status.attitude[2] << "\t";
cout << "ZA: " << status.accAbsolute[2] << "\t";
//cout << "GZ: " << status.gravity[2] << "\t";
//cout << "Baro: " << status.baroAltitude << "\t";
//cout << "Sonar: " << status.sonarAltitude << "\t";
cout << "BA: " << status.baroAltitude << "\t";
cout << "BF: " << status.baroFilterAltitude << "\t";
cout << "BV: " << status.baroFilterVelocityZ << "\t";
cout << "SA: " << status.sonarAltitude << "\t";
cout << "SF: " << status.sonarFilterAltitude << "\t";
cout << "SV: " << status.sonarFilterVelocityZ << "\t";
//cout << "E0: " << gpioGetPWMdutycycle(config.controlled_esc[0]) << "\t";
//cout << "E1: " << gpioGetPWMdutycycle(config.controlled_esc[1]) << "\t";
//cout << "E2: " << gpioGetPWMdutycycle(config.controlled_esc[2]) << "\t";
//cout << "E3: " << gpioGetPWMdutycycle(config.controlled_esc[3]) << "\t";
cout << endl;
}
// Main program
int main(int argc, char** argv)
{
systemInitialize();
gpioSetTimerFunc(3, 200, statusDisplayer);
string command;
while (true) {
cin >> command;
if (command == "arm") {
escControl.arming();
continue;
}
if (command == "start") {
escControl.startMotor();
PIDEnabled = true;
continue;
}
if (command == "init") {
initialize();
continue;
}
if (command == "stop") {
PIDEnabled = false;
gpioDelay(100000);
escControl.stopMotor();
continue;
}
if (command == "quit") {
break;
}
if (command[0] == 'c') {
gpioServo(CAMERA_STATION_OUT, std::stoi(command.substr(1)));
continue;
}
if (command[0] == 'y') {
pids.setAttitudeTarget(0, std::stof(command.substr(1)));
continue;
}
if (command[0] == 'p') {
pids.setAttitudeTarget(1, std::stof(command.substr(1)));
continue;
}
if (command[0] == 'r') {
pids.setAttitudeTarget(2, std::stof(command.substr(1)));
continue;
}
if (command[0] == 'a') {
pids.setAltitudeTarget(std::stof(command.substr(1)), ALTITUDE_BARO);
continue;
}
if (command[0] == 'n') {
pids.setAltitudeTarget(std::stof(command.substr(1)), ALTITUDE_SONAR);
continue;
}
err("Unknow command: "+command);
}
cleanup();
return 0;
}