Chapter 2 – Sensor Fusion and Coding Structures
2.1 – Gyroscopes
When you made your remote control, Zumi turned left or right using the number of degrees that you gave as a parameter. But how does Zumi know how many degrees she is turning? Zumi is equipped with an MPU, or motion processing unit, and has two very important sensors that are necessary for driving straight and making accurate turns: the gyroscope and accelerometer. This module is about the gyroscope.
Step 1: Gyroscope
When you think of a gyroscope, you may be thinking of a mounted, spinning wheel that stays balanced in the same orientation, no matter which direction you turn or flip the gyroscope. This can be explained by conservation of momentum. Similarly, in electronics, gyroscopes are tiny chips that use motion to detect changes in orientation. In robotics, this device measures rotation speed. Gyroscopes are important because we need rotation speed to calculate how many degrees Zumi has turned in a given amount of time.
Step 2: Import libraries
Import the required libraries by running the following code:
from zumi.zumi import Zumi
from zumi.util.screen import Screen
import time
zumi = Zumi()
screen = Screen()
Step 3: Axes
There is more than one axis that you can use to measure rotational speed. The axis you will be most concerned with is yaw, or measuring turns to the left and right. You can also measure if Zumi is tilting forward and backward or tilting left and right. These three axes are called roll, pitch, and yaw.
There are three codes below reading all three axes: X, Y, and Z. Run each one and check Zumi’s screen to see how the angles are affected. You can match X, Y, and Z with roll, pitch, and yaw. Make sure you start the code with Zumi flat on the ground before picking her up.
X-angle
zumi.reset_gyro()
for i in range(0,50):
current_angle = int(zumi.read_x_angle())
message = " X-Angle reading "
message = message + str(current_angle)
screen.draw_text(message)
time.sleep(0.05)
print("Done")
screen.draw_text_center("Done")
Y-angle
zumi.reset_gyro()
for i in range(0,50):
current_angle = int(zumi.read_y_angle())
message = " Y-Angle reading "
message = message + str(current_angle)
screen.draw_text(message)
time.sleep(0.05)
print("Done")
screen.draw_text_center("Done")
Z-angle
zumi.reset_gyro()
for i in range(0,50):
current_angle = int(zumi.read_z_angle())
message = " Z-Angle reading "
message = message + str(current_angle)
screen.draw_text(message)
time.sleep(0.05)
print("Done")
screen.draw_text_center("Done")
Based on the data, you can figure out which directions correspond to X, Y, and Z.
For the purposes of driving, the Z-axis or yaw of Zumi’s gyroscope is the most important as it measures the rotation or turn of Zumi. To get the angle of rotation, you can call the zumi.read_z_angle()
function, which will return the number of degrees turned from the initial starting point. This angle measurement will be crucial in later programs that involve controlling Zumi’s movements based on rotation.
Step 4: Reset Gyroscope
Before we start, let’s learn how to reset Zumi’s gyroscope. This will set all angles for roll, pitch, and yaw back to zero. You can use the reset_gyro() function to do this. Here’s an example code snippet:
zumi.reset_gyro()
for i in range(100):
z_angle = int(zumi.read_z_angle()) # <-- This function reads the angle
message = " Z Angle reading "
message = message + str(z_angle)
screen.draw_text(message)
time.sleep(0.1)
Step 5: Absolute Angles
In the previous lesson, you learned how to use relative angles to make shapes. However, this is not a good way to keep track of Zumi’s heading. Instead, we can use absolute angles, which are based on a fixed reference point (like North).
When you call the Zumi object or run reset_gyro()
, Zumi will reset all gyroscope values to zero. Think of this action as resetting your “North”.
Step 6: zumi.turn()
To use absolute angles, we will use the zumi.turn() function. Here’s how it works:
- 0 degrees is North,
- 90 or -270 degrees is West,
- 180 or -180 degrees is South, and
- 270 or -90 degrees is East.
Use positive values to turn left and negative values to turn right.
Step 7: Challenge – Squares with Heading
Problem
Now that you know how to use absolute angles, let’s write some code to drive Zumi in a square using turn() function. Here are the directions:
- Go North for 1 block
- Go West for 1 block
- Go South for 1 block
- Go East for 1 block
Solution
from zumi.zumi import Zumi
zumi = Zumi()
zumi.turn(0) # set the initial heading to North
zumi.forward() # move forward one block
zumi.turn(90) # turn left and face West
zumi.forward() # move forward one block
zumi.turn(90) # turn left and face South
zumi.forward() # move forward one block
zumi.turn(90) # turn left and face East
zumi.forward() # move forward one block
Step 8: Review
In this module, we have learned about using the gyroscope and absolute angles to control Zumi’s movements with more precision. We have also learned about resetting the gyroscope and using the turn() function to set Zumi’s heading to specific angles. With this knowledge, we were able to program Zumi to drive in a square pattern using the absolute heading like using a compass.
Demo Video
Questions :