Member-only story
MicroPython Tutorial IX
Time to introduce some sensors into the mix. We want you to add a gyro sensor to the basic rover model, add it to the underside between the two motors on the axel.
Be-warned don’t plug your gyro into a running brick, turn it off. After connecting the gyro, don’t touch the brick after you turn it on until it is fully up. Do so and there is chance you’ll mess up the initialisation of gyro sensor and may not be able to get any meaningful readings from it.
Now add the code you need to bring the gyro online, you need to mention it in the imports and define which port it is connected to.
from pybricks.ev3devices import Motor, GyroSensorgyro = GyroSensor(Port.S2)
gyro.reset_angle(0)
Now incorporate it into the basic template we’ve been working on in these tutorials. Your code should end up looking like this.
speed = 250
angle = 360
jumpSpeed = 25
baseAngle = 0while True:
if Button.UP in brick.buttons():
speed = speed + jumpSpeed
print(gyro.angle(), " ", speed)
wait(500)
if Button.DOWN in brick.buttons():
speed = speed - jumpSpeed
print(gyro.angle(), " ", speed)
wait(500)
if Button.RIGHT in brick.buttons():
leftMotor.run_angle(speed,angle,Stop.COAST,False)…