Recommended Posts

I'm busy with automating a LEGO railway crossing and have therefore written the code below. This code works fine, but motor D starts only when motor A is finished. Are there possibilities to start both motors at the same time?

 

#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
                                 InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile


# Initialize the EV3 Brick.
ev3             = EV3Brick()
sensor_1        = UltrasonicSensor(Port.S1)
motor_A         = Motor(Port.A)
motor_D         = Motor(Port.D)

# parameters
speed           = 6*165*10  # [deg/s]
rotation_angle  = 24*90     # gear ratio 24:1
dis_track_1     = 65        # [mm]
t1              = 10*1000   # [sec]

while True:
    if sensor_1.distance() < dis_track_1:
        # close railway crossing
        ev3.light.on(Color.RED)
        motor_A.run_angle(speed=speed, rotation_angle=-rotation_angle, then=Stop.HOLD, wait=True)
        motor_D.run_angle(speed=speed, rotation_angle=-rotation_angle, then=Stop.HOLD, wait=True)

        # wait 10 seconds
        wait(t1) 

        # open railway crossing
        motor_A.run_angle(speed=speed, rotation_angle=rotation_angle, then=Stop.HOLD, wait=True)
        motor_D.run_angle(speed=speed, rotation_angle=rotation_angle, then=Stop.HOLD, wait=True)
        ev3.light.on(Color.GREEN)

 

Share this post


Link to post
Share on other sites

Hi

at a fist glance: have a look at the "wait=true", what does the help function say?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.