Motor¶
Run at a fix Duty cycle¶
In this example the motor runs at a duty-cycle from -100% to +100%.
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Button
from pybricks.tools import print, wait
motor = Motor(Port.B)
cycle = 50
while True:
bts = brick.buttons()
if Button.LEFT in bts:
cycle = max(-100, cycle-10)
elif Button.RIGHT in bts:
cycle = min(100, cycle+10)
elif Button.CENTER in bts:
break
motor.dc(cycle)
print(cycle, motor.speed(), motor.angle())
wait(100)
Run at a fix speed¶
In this mode the motor uses feedback action to keep the speed constant.
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Button
from pybricks.tools import print, wait
motor = Motor(Port.B)
speed = 100
while True:
bts = brick.buttons()
if Button.LEFT in bts:
speed = max(-1000, speed-100)
elif Button.RIGHT in bts:
speed = min(1000, speed+100)
elif Button.CENTER in bts:
break
motor.run(speed)
print(speed, motor.speed(), motor.angle())
wait(100)
Run for a specified time¶
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Button, Stop
from pybricks.tools import print, wait
motor = Motor(Port.B)
while True:
bts = brick.buttons()
if Button.RIGHT in bts:
motor.run_time(200, 3000, Stop.COAST, False)
elif Button.CENTER in bts:
break
print(motor.speed(), motor.angle())
wait(100)
Run for a specified angle¶
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Button, Stop
from pybricks.tools import print, wait
motor = Motor(Port.B)
while True:
bts = brick.buttons()
if Button.RIGHT in bts:
motor.run_angle(200, 500, Stop.COAST, False)
elif Button.CENTER in bts:
break
print(motor.speed(), motor.angle())
wait(100)
Track an angle¶
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Button, Stop
from pybricks.tools import print, wait, StopWatch
import math
motor = Motor(Port.B)
watch = StopWatch()
amplitude = 90
while True:
bts = brick.buttons()
t = watch.time()/1000
angle = math.sin(t) * amplitude
motor.track_target(angle)
if Button.CENTER in bts:
break