-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpro_lecture_1_ex4_solution_gyro.ino
159 lines (134 loc) · 4.57 KB
/
rpro_lecture_1_ex4_solution_gyro.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
/*
* read girscope data
* Carlos Gomez Cubero
*
* The gyroscope is calibrated at the beginning "turnSensorSetup();"
* The calibration initialize the X, Y and Z axis of the girscopoe so place it
* in a flat surface.
*
* When the yellow LED is off (few instants after start) you can press the A button to
* reset the rotation on the Z axis to zero and start measuring from there. The rotation
* is sent thru the serial port.
*
*
* Most of the code is extracted from pololu zumo32U4 example RotationResist
* https://github.com/pololu/zumo-32u4-arduino-library/blob/master/examples/RotationResist/RotationResist.ino
*/
#include <Wire.h>
#include <Zumo32U4.h>
Zumo32U4LCD lcd;
Zumo32U4IMU imu;
Zumo32U4ButtonA buttonA;
/* turnAngle is a 32-bit unsigned integer representing the amount
the robot has turned since the last time turnSensorReset was
called. This is computed solely using the Z axis of the gyro, so
it could be inaccurate if the robot is rotated about the X or Y
axes.
Our convention is that a value of 0x20000000 represents a 45
degree counter-clockwise rotation. This means that a uint32_t
can represent any angle between 0 degrees and 360 degrees. If
you cast it to a signed 32-bit integer by writing
(int32_t)turnAngle, that integer can represent any angle between
-180 degrees and 180 degrees. */
uint32_t turnAngle = 0;
// turnRate is the current angular rate of the gyro, in units of
// 0.07 degrees per second.
int16_t turnRate;
// This is the average reading obtained from the gyro's Z axis
// during calibration.
int16_t gyroOffset;
// This variable helps us keep track of how much time has passed
// between readings of the gyro.
uint16_t gyroLastUpdate = 0;
void setup() {
Serial.begin(9600);
turnSensorSetup();
delay(500);
turnSensorReset();
lcd.clear();
}
int32_t getTurnAngleInDegrees(){
turnSensorUpdate();
return (((int32_t)turnAngle >> 16) * 360) >> 16;
}
void loop() {
int32_t turnDegrees = getTurnAngleInDegrees();
Serial.println("Degree: " + (String)turnDegrees);
}
/* This should be called in setup() to enable and calibrate the
gyro. It uses the LCD, yellow LED, and button A. While the LCD
is displaying "Gyro cal", you should be careful to hold the robot
still.
The digital zero-rate level of the gyro can be as high as
25 degrees per second, and this calibration helps us correct for
that. */
void turnSensorSetup()
{
Wire.begin();
imu.init();
imu.enableDefault();
imu.configureForTurnSensing();
lcd.clear();
lcd.print(F("Gyro cal"));
// Turn on the yellow LED in case the LCD is not available.
ledYellow(1);
// Delay to give the user time to remove their finger.
delay(500);
// Calibrate the gyro.
int32_t total = 0;
for (uint16_t i = 0; i < 1024; i++)
{
// Wait for new data to be available, then read it.
while(!imu.gyroDataReady()) {}
imu.readGyro();
// Add the Z axis reading to the total.
total += imu.g.z;
}
ledYellow(0);
gyroOffset = total / 1024;
// Display the angle (in degrees from -180 to 180) until the
// user presses A.
lcd.clear();
turnSensorReset();
while (!buttonA.getSingleDebouncedRelease())
{
turnSensorUpdate();
lcd.gotoXY(0, 0);
// do some math and pointer magic to turn angle in seconds to angle in degrees
lcd.print((((int32_t)turnAngle >> 16) * 360) >> 16);
lcd.print(F(" "));
}
lcd.clear();
}
// This should be called to set the starting point for measuring
// a turn. After calling this, turnAngle will be 0.
void turnSensorReset()
{
gyroLastUpdate = micros();
turnAngle = 0;
}
// Read the gyro and update the angle. This should be called as
// frequently as possible while using the gyro to do turns.
void turnSensorUpdate()
{
// Read the measurements from the gyro.
imu.readGyro();
turnRate = imu.g.z - gyroOffset;
// Figure out how much time has passed since the last update (dt)
uint16_t m = micros();
uint16_t dt = m - gyroLastUpdate;
gyroLastUpdate = m;
// Multiply dt by turnRate in order to get an estimation of how
// much the robot has turned since the last update.
// (angular change = angular velocity * time)
int32_t d = (int32_t)turnRate * dt;
// The units of d are gyro digits times microseconds. We need
// to convert those to the units of turnAngle, where 2^29 units
// represents 45 degrees. The conversion from gyro digits to
// degrees per second (dps) is determined by the sensitivity of
// the gyro: 0.07 degrees per second per digit.
//
// (0.07 dps/digit) * (1/1000000 s/us) * (2^29/45 unit/degree)
// = 14680064/17578125 unit/(digit*us)
turnAngle += (int64_t)d * 14680064 / 17578125;
}