Chapter 2 – Sensor Fusion and Coding Structures
2.2 – Gyroscope and Accelerometer
Welcome to the Gyroscope Lesson! In this lesson, you will learn how Zumi, a robot car, knows how many degrees she is turning. Zumi is equipped with an MPU (motion processing unit) and has two important sensors for driving straight and making accurate turns: the gyroscope and accelerometer. In this lesson, we will focus on the gyroscope.
Step 1: Understanding the Gyroscope
A gyroscope is a device that measures rotation speed. Unlike the spinning wheel gyroscope you may be familiar with, electronic gyroscopes are tiny chips that use motion to detect changes in orientation. In robotics, we use gyroscopes to calculate how many degrees Zumi has turned in a given amount of time.
Step 2: Understanding Axes
There are three axes of rotation: roll (X), pitch (Y), and yaw (Z).
In this lesson, we will focus on the Z axis, which measures turns to the left and right. In code, we call these axes X, Y, and Z. You can use the following code to measure the angles of each axis:
X-Axis
Y-Axis
Z-Axis
NOTE: Notice that when you turn Zumi to the left, the angles are positive. When you turn Zumi to the right, angles are negative.
Step 3: Understanding the Relationship between Gyroscope and Driving
Now that you know how to measure the angles of each axis, let’s focus on the Z axis, which is the most important one for driving and making accurate turns. To see how Zumi keeps track of her angle when driving, try the following code:
This code will show you the starting angle of 0 and the finishing angle around 90 degrees. It may not be perfect, but it will give you an idea of how Zumi keeps track of her angle when driving.
Step 4: Adding Sound to Gyroscope
You can use the gyroscope to create interesting projects, such as Zumi making sounds when she is facing a certain direction. Here’s how to set it up:
Step 5: Adding if Statements
Problem
To play four different notes depending on which quarter of the circle Zumi is facing, we need to write if statements for four cases:
* If 0 degrees < angle AND angle < 90 degrees (First quarter)
* Else if 90 degrees < angle AND angle < 180 degrees (Second quarter)
* Else if 180 degrees < angle AND angle < 270 degrees (Third quarter)
* Else if 270 degrees < angle AND angle < 360 degrees (Fourth quarter)
To set up these if statements, use the following code:
Since you will need to compare the same angle reading multiple times, create a variable at the top of the loop that will save the reading, like this:
Finally, match the code to the outline written above and add four unique sounds!
Solution
NOTE: Notice, once again, that if you turn Zumi to the right, the values read by the gyroscope are negative. Therefore, the sounds will not play. If you turn it left more than a full 360°, the gyroscope will read angles larger than 360, and the buzzers will not play any notes. Look at the console for the printed values as you turn Zumi to figure out which notes should or should not be playing!
Step 6: Calibrating the Gyroscope
If the gyro seems to be giving inaccurate results, you may need to recalibrate it. To do this, run the following block while Zumi is on a flat surface and not moving:
Step 7: Resetting the Gyroscope
The gyro will always reset to 0 when you run a Blockly program, but sometimes you need to reset the gyro in the middle of your program. To reset the gyro, use the following block:
Step 8: Review
Congratulations, you have completed the Gyroscope Lesson! You now understand how gyroscopes and accelerometers work and how they are used in robotics.
Demo Video