Search code examples
micropythonlego-mindstormslego-mindstorms-ev3pybricks-micropython

How do I get immediate response between motors and sensors?


I am new to pybricks and have found very little documentation to help answer my own query. I have written what I thought would be a simple program to spin my robot on the spot until the UltrasonicSensor sees something. It will then push forwards. If it is pushed backwards and sees a black line, it should try and swing out of the way.

The following code "works", but it's response to the Ultrasonic and Light sensors is significantly delayed:

#!/usr/bin/env pybricks-micropython

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor, ColorSensor, UltrasonicSensor
from pybricks.parameters import Port
from pybricks.tools import wait

ev3 = EV3Brick()
eyes = UltrasonicSensor(Port.S2)
left_motor = Motor(Port.B)
right_motor = Motor(Port.A)
right_light = ColorSensor(Port.S1)
left_light = ColorSensor(Port.S4)

while True:

    if right_light.reflection() < 50:
        ev3.speaker.say('black')
        left_motor.run(500)
        right_motor.run(-100)
        wait(2000)
        left_motor.run(500)
        right_motor.run(500)
        wait(1000)
    if eyes.distance() > 200:
        left_motor.run(500)
        right_motor.run(-500)
    else:
        left_motor.run(-500)
        right_motor.run(-500)

I can see in the (limited) documentation that you can apparently change motor settings but I can't find direction on how to do this (or even if it would be useful). Any help would be appreciated.


Solution

  • So the problem was that I used the "run" command to move the motors. Run appears to have an acceleration and deceleration component.

    I used "dc" instead of "run" and the motors instantly respond to the sensor data now.