Skip to content

Instantly share code, notes, and snippets.

@dlech
Created March 7, 2017 17:24
Show Gist options
  • Save dlech/37d4a4aca664c38458d85512390a521b to your computer and use it in GitHub Desktop.
Save dlech/37d4a4aca664c38458d85512390a521b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""Demo of useless machine using BrickPi3"""
import time
import ev3dev.brickpi as bp
if __name__ == '__main__':
# BrickPi3 cannot detect sensors, so we have to say which sensor is attached to each port
portS1 = bp.LegoPort('spi0.1:S1')
portS1.mode = 'ev3-uart'
portS1.set_device = 'lego-ev3-color'
# BrickPi3 cannot detect motors either, but they are setup already be default
# Initialize the sensors and motors
color_sensor = bp.ColorSensor('spi0.1:S1')
motor = bp.LargeMotor('spi0.1:MA')
motor.reset()
motor.run_direct(duty_cycle_sp=50)
time.sleep(0.5)
motor.stop(stop_action='brake')
motor.position = 0
time.sleep(3) # it takes some time for the sensor to start up
# Main program loop
while True:
if color_sensor.value() < 10:
time.sleep(0.5)
if color_sensor.value() > 10:
continue
motor.run_to_abs_pos(position_sp=-120, speed_sp=400)
# motor.wait_while('running')
time.sleep(0.5)
motor.run_to_abs_pos(position_sp=0, speed_sp=400)
# motor.wait_while('running')
time.sleep(0.5)
motor.stop(stop_action='brake')
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment