First Lego League Challenge Team
drive() then wait(milliseconds), then stop()
robot.drive(150, 0) # drive at 150mm/sec, in straight line (because turn_rate=0)
wait(1000) # wait one second
robot.stop() # not required if program ends after wait command
—>try it out (copy code and paste it under Python tab)
call drive() many times inside while loop using distance() or angle():
robot.reset()
# keep driving until either robot has travelled 100cm or turned 120degrees
while robot.distance() < 1000 and robot.angle() < 120:
robot.drive(150,30) # drive for 150mm/s, while turning left at 30deg/sec
wait(10) # wait for a short time or do something else
robot.stop()
—>try it out (copy code and paste it under Python tab)
Runs the motor at a constant speed, no stopping. If it is the only command, make sure you use a wait command, otherwise the program will end before run command can execute:
motorC.run(200)
wait(500) # run the lift motor command for half a second
motorC.stop()
—>try it out (use Tow Truck robot)
Drive Straight Using Gyro - Forward: (Proportional algorithm)
gyro_sensor = GyroSensor(Port.S3) # Assumes gyro is connected to port 3
distance = 1000 # millimetres
robotSpeed = 150 # mm/sec
robot.reset()
gyro_sensor.reset_angle(0)
PROPORTIONAL_GAIN = 1.1
while robot.distance() < distance:
angle_correction = -1 * PROPORTIONAL_GAIN * gyro_sensor.angle()
robot.drive(robotSpeed, angle_correction)
wait(10)
robot.stop()
—>try it out (use Single Sensor Line Robot and select Gyro Challenges from Worlds under the Simulator tab)
Drive Straight Using Gyro - Forward or Backwards: (Proportional algorithm)
gyro_sensor = GyroSensor(Port.S3) # Assumes gyro is connected to port 3
distance = -1000 # millimetres
robotSpeed = 150 # mm/sec
robot.reset()
gyro_sensor.reset_angle(0)
PROPORTIONAL_GAIN = 1.1
if distance < 0: # move backwards
while robot.distance() > distance:
reverseSpeed = -1 * robotSpeed
angle_correction = -1 * PROPORTIONAL_GAIN * gyro_sensor.angle()
robot.drive(reverseSpeed, angle_correction)
wait(10)
elif distance > 0: # move forwards
while robot.distance() < distance:
angle_correction = -1 * PROPORTIONAL_GAIN * gyro_sensor.angle()
robot.drive(robotSpeed, angle_correction)
wait(10)
robot.stop()
—>try it out (use Single Sensor Line Robot and select Gyro Challenges from Worlds under the Simulator tab)
Spin Turn Using Gyro - Right Turn Only
gyro_sensor = GyroSensor(Port.S3)
angle = 90 # degrees
speed = 150 # mm/s
gyro_sensor.reset_angle(0)
while gyro_sensor.angle() < angle:
motorA.run(speed=speed)
motorB.run(speed=(-1 * speed))
wait(10)
motorA.brake()
motorB.brake()
—>try it out (copy code and paste in under Python tab)
Spin Turn Using Gyro - Either Direction
gyro_sensor = GyroSensor(Port.S3)
angle = -90 # degrees
speed = 150 # mm/s
gyro_sensor.reset_angle(0)
if angle < 0:
while gyro_sensor.angle() > angle:
motorA.run(speed=(-1 * speed))
motorB.run(speed=speed)
wait(10)
elif angle > 0:
while gyro_sensor.angle() < angle:
motorA.run(speed=speed)
motorB.run(speed=(-1 * speed))
wait(10)
else:
print("Error: no angle chosen")
motorA.brake()
motorB.brake()
—>try it out (copy code and paste in under Python tab)
Notes:
color_sensor_in1 = ColorSensor(Port.S1)
while True:
if color_sensor_in1.reflection() > 95: # white
robot.drive(drive_speed=75, turn_rate=-60)
else:
if color_sensor_in1.reflection() < 10: # black
robot.drive(drive_speed=75, turn_rate=60)
else: #straight
robot.drive(drive_speed=75, turn_rate=0)
—>try it out (use Single Sensor Line Robot and select Line Following Challenges from Worlds, under the Simulator tab)
color_sensor_in1 = ColorSensor(Port.S1)
BLACK = 10
WHITE = 95
threshold = (BLACK + WHITE) / 2
PROPORTIONAL_GAIN = 1.5
while True:
deviation = color_sensor_in1.reflection() - threshold
angle_correction = PROPORTIONAL_GAIN * deviation
robot.drive(drive_speed=100, turn_rate=angle_correction)
wait(10)
—>try it out (use Single Sensor Line Robot and select Line Following Challenges from Worlds, under the Simulator tab)