-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompass.cpp
130 lines (95 loc) · 2.76 KB
/
Compass.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
#include "Compass.hpp"
extern unsigned long flatTimer;
extern Pilot m;
extern Led led;
Compass::Compass(){
//TODO
}
void Compass::init(){
DEBUG_SERIAL.println("Compass begin");
while(!bno.begin()){
DEBUG_SERIAL.println("Compass initialisation error!");
delay(100);
}
DEBUG_SERIAL.println("Compass read StartHeading");
if (EEPROM.read(EEPROM_HEADING_SIGN) == 0) {
_startHeading = EEPROM.read(EEPROM_HEADING);
} else {
_startHeading = -EEPROM.read(EEPROM_HEADING);
}
if(_startHeading == 0){
buzzerTone(800);
}
DEBUG_SERIAL.println("Compass restore Offsets");
restoreOffsets();
}
void Compass::restoreOffsets(){
adafruit_bno055_offsets_t calibrationData;
DEBUG_SERIAL.println("load offsets");
EEPROM.get(EEPROM_COMPASS_OFFSET, calibrationData);
DEBUG_SERIAL.println("loaded offset");
delay(50);
bno.setSensorOffsets(calibrationData);
delay(10);
DEBUG_SERIAL.println("changed offset");
}
void Compass::update(){
debugln("Start");
bno.getEvent(&_event);
_heading = (((int)_event.orientation.x - _startHeading + 720) % 360) - 180;
_pitch = _event.orientation.y;
_roll = _event.orientation.z;
if(abs(_pitch) < FLAT_ANGLE){
flatTimer = millis();
}
debugln("Stop");
}
void Compass::setStartHeading(){
_startHeading = 0;
update();
_startHeading = _heading;
EEPROM.write(EEPROM_HEADING_SIGN, _startHeading < 0); // speichere Vorzeichen
EEPROM.write(EEPROM_HEADING, abs(_startHeading)); // speichere Winkel
}
void Compass::setStartHeading(int angle){
_startHeading = angle;
}
void Compass::calibrate(){
m.brake(true);
sensors_event_t event;
digitalWrite(LED_BACK_LEFT, HIGH);
digitalWrite(LED_BACK_RIGHT, HIGH);
while(input.button_compass){
input.update();
}
while (!bno.isFullyCalibrated() && !input.button_compass){
analogWrite(BUZZER, 127 * (millis() <= buzzerStopTimer)); // buzzer anschalten bzw. wieder ausschalten
input.update();
bno.getEvent(&event);
led.showCalibration();
led.led();
led.heartbeat();
delay(50);
}
led.showCalibration();
led.led();
led.heartbeat();
adafruit_bno055_offsets_t newCalib;
if(bno.getSensorOffsets(newCalib))
EEPROM.put(EEPROM_COMPASS_OFFSET, newCalib);
delay(200);
digitalWrite(LED_BACK_LEFT, LOW);
digitalWrite(LED_BACK_RIGHT, LOW);
}
void Compass::getCalibration(byte *system, byte *gyro, byte *accel, byte *mag){
bno.getCalibration(system, gyro, accel, mag);
}
int Compass::getHeading(){
return _heading;
}
int Compass::getPitch(){
return _pitch;
}
int Compass::getRoll(){
return _roll;
}