4 Keeping track of direction – which way are we heading?
Contents
4 Keeping track of direction – which way are we heading?¶
As well as keeping track of how much the wheels have turned, and estimating location on that basis, we can also use the robot’s gyroscope – often referred to as a ‘gyro’ – sensor to tell us which direction it is facing.
In the following activities, you will see how the gyroscope and position sensors can be used to keep track of where the robot has been, as well as helping it get to where it needs to go.
So let’s get the simulated loaded in the normal way and then find out where we’re heading next…
from nbev3devsim.load_nbev3devwidget import roboSim, eds
%load_ext nbev3devsim
4.1 Activity – Detecting orientation¶
The following program defines a simple edge follower that allows the robot to navigate its way around the shape described in the Two_shapes background, logging the gyro sensor as it does so.
Show the chart, enable the gyro trace, and download and run the program. Purely by observation of the chart view of the gyro data, do you think you would be able to determine the shape corresponding to the path followed by the robot?
Stop the downloaded program executing either from the Simulator controls or the simulator keyboard shortcut (S
).
%%sim_magic_preloaded -c -b Two_shapes -x 400 -y 700 -a -90
colorRight = ColorSensor(INPUT_3)
gyro = GyroSensor(INPUT_4)
while True:
# Get the gyro value
print('Gyro: '+str(gyro.angle))
intensity_right = colorRight.reflected_light_intensity_pc
if intensity_right > 70:
left_motor_speed = SpeedPercent(0)
right_motor_speed = SpeedPercent(20)
else:
left_motor_speed = SpeedPercent(20)
right_motor_speed = SpeedPercent(0)
tank_drive.on(left_motor_speed, right_motor_speed)
Add your observations about the gyro data trace as the robot follows the boundary of the provided shape. To what extent can you use the data to identify the shape of the route taken by the robot? How might you identify the path more exactly?
#### Example observations
Click on the arrow in the sidebar or run this cell to reveal some example observations.
The gyro sensor values follow a stepped trace in the chart, dropping by 90 or so every time the robot turns a corner, corresponding to a right-angled turn anticlockwise. The values oscillate as the robot proceeds, wiggling as it follows the edge of the line. The width (as measured along the x-axis) of each step is roughly the same, so the robot is describing a square.
I also noticed that the angle count is not a direction: it seems to be an accumulated count of degrees turned in a particular direction. If the robot were to turn the other way then I would expect the count to go down. I even did a little experiment to check that.
%%sim_magic_preloaded -c
gyro = GyroSensor(INPUT_4)
say('Turn one way')
tank_drive.on(SpeedPercent(20), SpeedPercent(0))
while gyro.angle < 90:
print('Gyro: '+str(gyro.angle))
say('and the other')
# Turn the other way
tank_drive.on(SpeedPercent(0), SpeedPercent(20))
while gyro.angle > 0:
print('Gyro: '+str(gyro.angle))
say('all done')
4.3 Summary¶
In this notebook and the previous one, you have seen how the motor position
tachometer can be used to record how far each motor has turned, and the gyroscope angle
value to keep track of the accumulated directional turns, in degrees, it has turned. In each case, turning one way increases the count, whereas turning the other way decreases it.
Tacho counts and gyro angles are very useful for providing an indicative feel for how a robot has travelled, but they may not be particularly accurate. It is worth making the point again that for many data traces, it is the trends and differences that often tell us much of what we need to know rather than the actual values.
This completes the practical activities for this week. It’s been quite a short week, so use any extra time to work through the practical-related activities in the TMA.