Jump to content

Recommended Posts

Posted

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)

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...