Motors as sensors

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.

In the tutorial on motors it was explained that the motors on the robot have encoders that can read the rotation of the motor axel. This encoder also means that motors can be used as sensors to provide input to the robot.

Motor Measurement Functions

You can get measurements from the motors using the following Pybrick functions:

  • speed()→ int: deg/s → Gets the speed of the motor (deg/s).

  • [angle()→ int: deg](https://code.pybricks.com/static/docs/v2.7.0/pupdevices/motor.html#pybricks.pupdevices.Motor.angle) → Gets the rotation angle of the motor (deg).

  • load()→ int: mNm → Estimates the load that holds back the motor when it tries to move (mNm).

  • stalled()→ bool → Checks if the motor is currently stalled.

Pybricks also provides a configuration function:

Motor Measurement Example

We will use the code below to see these functions work:

  1. Create a new file called motor_inputs.py

  2. Type the code below into the file

  3. Predict what you think will happen.

  4. When running your code you will need to interact with the robot:

    • to get a speed reading of the wheel, press the left button

    • to get the wheel to return to it’s starting point, simultaneously press the left button and the right button.

    • to get the torque load and stall reading, press and hold the right button then gradually slow the left wheel with your hand until it stops.

 1# motor_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()
11left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
12
13left_motor.reset_angle(0)
14
15# --- MAIN LOOP
16while True:
17    pressed = hub.buttons.pressed()
18
19    if Button.LEFT in pressed and Button.RIGHT in pressed:
20        print("Angel:", left_motor.angle())
21        left_motor.run_target(1000, 0)
22        print("Angel:", left_motor.angle())
23    
24    elif Button.LEFT in pressed:
25        left_motor.run(300)
26        wait(250)
27        print("Speed:", left_motor.speed())
28        wait(500)
29        left_motor.stop()
30    
31    elif Button.RIGHT in pressed:
32        left_motor.run(300)
33        if left_motor.stalled():
34            print("Stalled")
35        else:
36            print("Torque load:", left_motor.load())
37    
38    else:
39        left_motor.stop()

Investigate

  • lines 3 - 7 → imports all the Pybricks command for use with your robot

  • line 10 → initialises the hub

  • line 11 &rarrl initialises the left motor (you will only use one)

  • line 13 → sets the current wheel posistion (angel) as angel 0 (the starting angle)

  • line 16 → starts infinite loop

  • line 17 → checks for pressed buttons and stores them in pressed

  • line 19 → checks if both the left and right buttons are currently pressed

  • line 20 → prints the current accumulated left motor angle

  • line 21 → send the left motor back to angle 0

  • line 22 → prints the current accumulated left motor angle

  • line 24 → checks if left button is pressed

  • line 25 → starts the left motor running at 300degs/s

  • line 26 → allows a quater of a second for the motor to get up to speed

  • line 27 → reads the left motor’s speed and prints it

  • line 28 → waits another half a second

  • line 29 → stops the left motor

  • line 31 → checks if the right button is pressed

  • line 32 → starts left motor running at 300deg/s

  • line 33 → checks if the left motor is stalled (prevented from moving)

  • line 34 → prints stalled

  • line 35 - 36 → if motor is not stalled then read and print the torque load that the left wheel is experiencing

  • line 38 - 39 → if no button is being pressed, stop the left motor

Modify

  • what happens if you move line 13 inside the main loop?

  • what happens if you comment out line 26?

  • what happens if you comment out line 25 and then physically turn the wheel when holding the left button?

  • what happens if you comment out line 32 and then physically turn the wheel when holding the right button?