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.2 Challenge – Navigating to a specified location

The WRO_2018_Regular_Junior challenge background has several coloured areas marked on it at (350, 580), (1180, 960) and (2000, 580).

You should not spend more than 30–45 minutes on this challenge.

From the starting location of the robot at (1180, 150, 90), see if you can write a program that drives the robot using dead reckoning – that is, using just the motor position and the gyro angle values – to drive the robot to one of those locations. Then see if you can get it to drive to one of the other locations.

The background coordinates give locations in millimetres relative to a fixed origin.

Once you have got your program to work reasonably reliably, try adding some noise to the motors using the Wheel noise slider in the simulator. Does this have any effect on the performance of your control program?

You may find it helpful to do some sums to calculate how far the robot has to travel. Make some notes on that here.

# Maybe try out some sums here?
%%sim_magic_preloaded -p -x -1180 -y 150 -a 90 -b WRO_2018_Regular_Junior

# YOUR CODE HERE

Comment on how well your robot performed the task here. What strategy did you use to come up with your solution?

Describe what effect, if any, adding noise to the motors does to the performance of the robot in completing this dead-reckoning task.

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.