1

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.

MisterWeary
  • 605
  • 1
  • 9
  • 16

2 Answers2

3

ev3.speaker.say(text) synthesizes speech as it goes. This is fun, but it is very slow. This is especially noticeable in a control loop like yours.

I'd recommend using ev3.speaker.beep() instead. You could even select the frequency based on the reflection value so you can "hear" what the sensor "sees".

  • Thank you for your reply. It hasn't stopped the lag between sensor and motor though. I feel like the motor is needing to accelerate to the speed I've given it and then decelerate from that speed before it responds. Is this is a thing? Is there a way to remove all accel/decel and just immediately perform the next command? The old NXT bricks worked that way?!? – MisterWeary Jan 13 '21 at 22:59
2

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.

MisterWeary
  • 605
  • 1
  • 9
  • 16