v0.0.2
This commit is contained in:
1 parent
28db27de8e
commit
8537ba9033
1 file changed
+108
-103
@@ -1,104 +1,109 @@
|
|||||||
################################################################################
|
################################################################################
|
||||||
# #
|
# #
|
||||||
# pybricks script for 4532-1 - Manual Level Crossing #
|
# pybricks script for 4532-1 - Manual Level Crossing #
|
||||||
# #
|
# #
|
||||||
# This script enables the LEGO® 4532-1 - Manual Level Crossing to be used with #
|
# This script enables the LEGO® 4532-1 - Manual Level Crossing to be used with #
|
||||||
# the LEGO Powerd UP system . #
|
# the LEGO Powerd UP system . #
|
||||||
# #
|
# #
|
||||||
# Configuration of the Technic Hub: #
|
# Configuration of the Technic Hub: #
|
||||||
# Port A: 45303-1 - WeDo 2.0 Medium Motor #
|
# Port A: 45303-1 - WeDo 2.0 Medium Motor #
|
||||||
# Port B: 45303-1 - WeDo 2.0 Medium Motor (not yet implemented) #
|
# Port B: #
|
||||||
# Port C: 88007-1 - Color & Distance Sensor #
|
# Port C: 88007-1 - Color & Distance Sensor #
|
||||||
# Port D: 88007-1 - Color & Distance Sensor (not yet implemented) #
|
# Port D: 88007-1 - Color & Distance Sensor (not yet implemented) #
|
||||||
# #
|
# #
|
||||||
################################################################################
|
################################################################################
|
||||||
# #
|
# #
|
||||||
# Changelog #
|
# Changelog #
|
||||||
# #
|
# #
|
||||||
################################################################################
|
################################################################################
|
||||||
# v0.0.1 05-02-2023 #
|
# v0.0.2 06-02-2023 #
|
||||||
# Add comments and header. #
|
# changed barrier_wait_up to barrier_wait_after #
|
||||||
# v0.0.0 04-02-2023 #
|
# Changed barrier_wait to barrier_wait_up #
|
||||||
# First version. #
|
# changed barrier_motor_1 to barrier_motor #
|
||||||
################################################################################
|
# added barrier_wait_down #
|
||||||
|
# v0.0.1 05-02-2023 #
|
||||||
from pybricks.hubs import TechnicHub
|
# Add comments and header. #
|
||||||
from pybricks.pupdevices import Motor, DCMotor, ColorDistanceSensor
|
# v0.0.0 04-02-2023 #
|
||||||
from pybricks.parameters import Color, Port
|
# First version. #
|
||||||
from pybricks.tools import wait
|
################################################################################
|
||||||
|
|
||||||
# variables to set
|
from pybricks.hubs import TechnicHub
|
||||||
barrier_duty_cycle = 27 # speed of the motor (0-100).
|
from pybricks.pupdevices import Motor, DCMotor, ColorDistanceSensor
|
||||||
barrier_wait = 575 # Time the barrier needs to go up or down in ms.
|
from pybricks.parameters import Color, Port
|
||||||
barrier_wait_up = 2000 # Time the barries waits to go up after last detection.
|
from pybricks.tools import wait
|
||||||
|
|
||||||
# Initialize the hub.
|
# variables to set
|
||||||
hub = TechnicHub()
|
barrier_duty_cycle = 30 # speed of the motor (0-100).
|
||||||
|
barrier_wait_up = 1000 # Time the barrier needs to go up in ms.
|
||||||
# Initialize a the motors
|
barrier_wait_down = 600 # Time the barrier needs to go down in ms.
|
||||||
barrier_motor_1 = DCMotor(Port.A)
|
barrier_wait_after = 2000 # Time the barrier waits to go up after last detection.
|
||||||
#barrier_motor_2 = DCMotor(Port.B)
|
|
||||||
# Initialize the sensors.
|
# Initialize the hub.
|
||||||
distance_sensor_1 = ColorDistanceSensor(Port.C)
|
hub = TechnicHub()
|
||||||
#distance_sensor_2 = ColorDistanceSensor(Port.D)
|
|
||||||
|
# Initialize a the motor
|
||||||
# Make the barrier go down to calibrate the position
|
barrier_motor = DCMotor(Port.A)
|
||||||
barrier_motor_1.dc(barrier_duty_cycle)
|
# Initialize the sensors.
|
||||||
# Wait for the amount of time set with "barrier_wait" plus an extra amount for calibration.
|
distance_sensor_1 = ColorDistanceSensor(Port.C)
|
||||||
wait(barrier_wait+100)
|
#distance_sensor_2 = ColorDistanceSensor(Port.D)
|
||||||
# Stop the motor.
|
|
||||||
barrier_motor_1.stop()
|
# Make the barrier go down to calibrate the position
|
||||||
|
barrier_motor.dc(-barrier_duty_cycle)
|
||||||
# Wait a few milliseconds for the next step.
|
# Wait for the amount of time set with "barrier_wait_down" plus an extra amount for calibration.
|
||||||
wait(10)
|
wait(barrier_wait_down+100)
|
||||||
|
# Stop the motor.
|
||||||
# Variable initialisation for further use.
|
barrier_motor.stop()
|
||||||
wait_cycles=0 # number of cycles through the while loop before opening the barrier
|
|
||||||
barrier_position = 1 # Current barrier position (1=down, 0=up)
|
# Wait a few milliseconds for the next step.
|
||||||
|
wait(10)
|
||||||
# Start program loop
|
|
||||||
while True:
|
# Variable initialisation for further use.
|
||||||
# Check to see if there is a train.
|
wait_cycles=0 # number of cycles through the while loop before opening the barrier
|
||||||
if distance_sensor_1.distance() <= 40:
|
barrier_position = 1 # Current barrier position (1=down, 0=up)
|
||||||
# Change light color on hub to reflect the current status.
|
|
||||||
hub.light.on(Color.RED)
|
# Start program loop
|
||||||
# Only send the barrier down if it's currently up.
|
while True:
|
||||||
if barrier_position == 0:
|
# Check to see if there is a train.
|
||||||
# Send barrier down.
|
if distance_sensor_1.distance() <= 40:
|
||||||
barrier_motor_1.dc(barrier_duty_cycle)
|
# Change light color on hub to reflect the current status.
|
||||||
# wait for the barrier to move to its position.
|
hub.light.on(Color.RED)
|
||||||
wait(barrier_wait)
|
# Only send the barrier down if it's currently up.
|
||||||
# Stop the barrier from moving after the wait time.
|
if barrier_position == 0:
|
||||||
barrier_motor_1.stop()
|
# Send barrier down.
|
||||||
# Set the variable to the up position
|
barrier_motor.dc(-barrier_duty_cycle)
|
||||||
barrier_position = 1
|
# wait for the barrier to move to its position.
|
||||||
# As long as there is a train detected keep program within this loop.
|
wait(barrier_wait_down)
|
||||||
while distance_sensor_1.distance() <= 40:
|
# Stop the barrier from moving after the wait time.
|
||||||
wait (10)
|
barrier_motor.stop()
|
||||||
# Set the number of while cycles to wait before sending the barrier up after last detection.
|
# Set the variable to the up position
|
||||||
wait_cycles = barrier_wait_up
|
barrier_position = 1
|
||||||
# Check if the barrier needs to stay down
|
# As long as there is a train detected keep program within this loop.
|
||||||
elif wait_cycles > 0:
|
while distance_sensor_1.distance() <= 40:
|
||||||
# Change light color on hub to reflect the current status.
|
wait (10)
|
||||||
hub.light.on(Color.BLUE)
|
# Set the number of while cycles to wait before sending the barrier up after last detection.
|
||||||
# Change the number of cycles left before sending the barrier up.
|
wait_cycles = barrier_wait_after
|
||||||
wait_cycles = wait_cycles-1
|
# Check if the barrier needs to stay down
|
||||||
# If there is no train and there is no need to keep the barrier down send it up.
|
elif wait_cycles > 0:
|
||||||
else:
|
# Change light color on hub to reflect the current status.
|
||||||
# Change light color on hub to reflect the current status.
|
hub.light.on(Color.BLUE)
|
||||||
hub.light.on(Color.GREEN)
|
# Change the number of cycles left before sending the barrier up.
|
||||||
# Only send the barrier up if it's currently down.
|
wait_cycles = wait_cycles-1
|
||||||
if barrier_position == 1:
|
# If there is no train and there is no need to keep the barrier down send it up.
|
||||||
# Send barrier down.
|
else:
|
||||||
barrier_motor_1.dc(-barrier_duty_cycle)
|
# Change light color on hub to reflect the current status.
|
||||||
# wait for the barrier to move to its position.
|
hub.light.on(Color.GREEN)
|
||||||
wait(barrier_wait)
|
# Only send the barrier up if it's currently down.
|
||||||
# Stop the barrier from moving after the wait time.
|
if barrier_position == 1:
|
||||||
barrier_motor_1.stop()
|
# Send barrier down.
|
||||||
# Set the variable to the up position
|
barrier_motor.dc(barrier_duty_cycle)
|
||||||
barrier_position = 0
|
# wait for the barrier to move to its position.
|
||||||
# As long as there is no train detected keep program within this loop.
|
wait(barrier_wait_up)
|
||||||
while distance_sensor_1.distance() > 40:
|
# Stop the barrier from moving after the wait time.
|
||||||
wait (10)
|
barrier_motor.stop()
|
||||||
# wait for 1ms before continuing.
|
# Set the variable to the up position
|
||||||
|
barrier_position = 0
|
||||||
|
# As long as there is no train detected keep program within this loop.
|
||||||
|
while distance_sensor_1.distance() > 40:
|
||||||
|
wait (10)
|
||||||
|
# wait for 1ms before continuing.
|
||||||
wait(1)
|
wait(1)
|
||||||
Reference in new issue
Block a user