summaryrefslogtreecommitdiff
path: root/src/gyronardo.cpp
blob: 025bb7d767110d1ff6238378c513f6e2b55080ab (plain)
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
#include "Arduino.h"
#include "Wire.h"
#include "sensor.hpp"

Sensor sensor(0x68, &Wire);

/**
 * Stops code execution.
 */
void stop()
{
	while (true) {}
}

void setup()
{
	Serial.begin(9600);

	// Wait for Serial because the Arduino Leonardo is weird
	while (!Serial) {}

	while (!sensor.begin())
	{
		Serial.print(millis());
		Serial.println("Error: Could not connect to the sensor. Retrying after 2000 milliseconds...");
		delay(2000);
	}
	sensor.setAccelSensitivity(2); // 8g
	sensor.setGyroSensitivity(1);  // 500 degrees/s

	sensor.setThrottleEnabled(true);

	Serial.println("Automatically calibrating sensor...");
	bool cal_status = sensor.autoCalibrate();

	if (!cal_status)
	{
		Serial.print("Error: Automatic calibration timed out after ");
		Serial.print(CALIBRATION_TIMEOUT);
		Serial.println(" milliseconds");

		stop();
	}

	Serial.println("Finished calibrating");

	Serial.println("Starting...");
}

void loop()
{
	if (!sensor.is_calibrated)
	{
		return;
	}

	sensor.read();

	float pitch = sensor.getPitch();
	float roll = sensor.getRoll();

	Serial.print("Pitch: ");
	Serial.print(pitch, 3);
	Serial.print(" Roll: ");
	Serial.print(roll, 3);
	Serial.println();
}