Drive Base as Sensor¶
Pybrick Documentation
To explore all Pybricks’ features check the Pybricks documentaion. This can also be seen in the right-hand panel of the Pybricks IDE.
Since the drive base is made up of motors, the encoders can also be used to measure what the robot has done.
Drive Base Measurement Functions¶
There are three functions that can extract measurements from the drive base:
distance()→ int: mm
→ Gets the estimated driven distance (mm).angle()→ int: deg
→ Gets the estimated rotation angle (deg) of the drive base.state()→ Tuple[int, int, int, int]
→ Gets the state of the robot. The return tuple is(distance, speed, angle, turn_rate)
There is also one configuration function:
reset()
→ Resets the estimated driven distance and angle to0
.
Drive Base Measurement Example¶
Now use the code below to explore these functions:
Create a new file called
drive_base_inputs.py
Type the code below into the file
Predict what you think will happen.
When running your code you will need to interact with the robot:
to get an arc movement reading, press the left button
to get a straight line distance reading, press the right button
to get a turn degrees reading, press the Bluetooth button
1# drive_base_inputs.py
2
3from pybricks.hubs import PrimeHub
4from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
5from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
6from pybricks.robotics import DriveBase
7from pybricks.tools import wait, StopWatch
8
9# --- SETUP
10hub = PrimeHub()
11
12left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
13right_motor = Motor(Port.B, Direction.CLOCKWISE)
14my_robot = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=80)
15
16my_robot.reset()
17
18# --- MAIN LOOP
19while True:
20 pressed = hub.buttons.pressed()
21
22 if Button.LEFT in pressed:
23 distance, speed, angle, rate_of_turn = my_robot.state()
24 print("Start - distance:", distance, "speed:", speed, "angle:", angle, "rate of turn:", rate_of_turn)
25 my_robot.curve(100, 180)
26 distance, speed, angle, rate_of_turn = my_robot.state()
27 print("Start - distance:", distance, "speed:", speed, "angle:", angle, "rate of turn:", rate_of_turn)
28
29 elif Button.RIGHT in pressed:
30 distance = my_robot.distance()
31 print("Start distance:", distance)
32 my_robot.straight(500)
33 distance = my_robot.distance()
34 print("End distance:", distance)
35
36 elif Button.BLUETOOTH in pressed:
37 angle = my_robot.angle()
38 print("Start angle:", angle)
39 my_robot.turn(-90)
40 angle = my_robot.angle()
41 print("End angle:", angle)
42
43 else:
44 my_robot.stop()
Investigate
lines 3 - 7 → imports all the Pybricks command for use with your robot
line 10 → initialises the hub
lines 12 - 14 → initialises the drive base
line 16 → sets the robot’s distance and angle counters to
0
line 19 → create the main loop
line 20 → checks for pressed buttons and stores them in
pressed
line 22 → checks if the left button was pressed
line 23 → get the robot’s state tuple and stores the values in
distance
,speed
,angle
, andrate_of_turn
line 24 → prints the robot state values stored in
distance
,speed
,angle
, andrate_of_turn
line 25 → drives the robot in an arc
line 26 → get the robot’s state tuple and stores the values in
distance
,speed
,angle
, andrate_of_turn
line 27 → prints the robot state values stored in
distance
,speed
,angle
, andrate_of_turn
line 29 → checks if the right button is pressed
line 30 → reads the current robot distance count and stores it in
distance
line 31 → prints th current robot distance count
line 32 → moves the robot in a straight line
line 33 → reads the current robot distance count and stores it in
distance
line 34 → prints th current robot distance count
line 36 → checks if the Bluetooth button is pressed
line 37 → reads the current robot angle count and stores it in
angle
line 38 → prints th current robot angle count
line 39 → turns the robot
line 40 → reads the current robot angle count and stores it in
angle
line 41 → prints th current robot angle count
line 43 - 44 → if not button is pressed stop the drive base
Modify
what happens if you move line 16 to inside the main loop?
can you use
my_robot.state()
to measure a straight move by the robot?