-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalbot_due.ino
71 lines (56 loc) · 1.94 KB
/
balbot_due.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
/*
* Balbot Due - my latest attempt at a balancing robot
* 12/2015
* Jamie Ahmed
*/
// Put all defines/constants/declarations in a header file for cleanliness
#include "balbot_due.h"
void setup() {
// Initialize serial port
Serial.begin(38400);
// Set ADC to 12-bit resolution
analogReadResolution(12);
// Initialize Pololu dual motor shield
motors.init();
// Initialize kalman filter struct
kalman_filter_init(&kalData);
// Load any stored parameters from flash
loadDataFromFlash();
// to keep me from having to calibrate after each sketch :-)
if(accelYOffset == 0) {
accelYOffset = 2021;
accelZOffset = 2555;
gyroYOffset = 1659;
// TODO: Put the rest of the PID values et al here.
}
}
void loop() {
float dt;
// 1KHz loop
if (micros() - oneKilohertz >= 1000) {
oneKilohertz = micros();
// Read and normalize our gyro and accel sensors
updateIMUData();
// Calculate dt (delta time) - how long since the last loop, in seconds
dt = (float)(micros() - last) / 1000000.0;
last = micros();
// Update kalman filter state with new gyro data
kalman_filter_state_update(gyroRadians(gyroY), &kalData, dt);
// Calculate the accelerometer angle using both Y & Z axes
float angleY = atan2(accelY, accelZ);
// Update kalman filter with accelrometer angle
kalman_filter_angle_update(angleY, &kalData);
}
// 10Hz loop
if (micros() - tenHertz >= 100000) {
dt = (float)(micros() - tenHertz) / 1000000.0;
tenHertz = micros();
// Get current speed readings from motor encoders
updateEncoderSpeeds(dt);
// Update steering & throttle inputs from RC receiver
readRC();
// Print serial stream data and process any incoming data
printSerialData();
readSerialData();
}
}