forked from Telos4/RoboRally
added robot class and example movement sequence
This commit is contained in:
parent
0b6f61932f
commit
157c61ad87
|
@ -4,6 +4,9 @@ import time
|
||||||
class Motor:
|
class Motor:
|
||||||
def __init__(self, enable, dir1, dir2):
|
def __init__(self, enable, dir1, dir2):
|
||||||
self.enable_pin = machine.Pin(enable, machine.Pin.OUT)
|
self.enable_pin = machine.Pin(enable, machine.Pin.OUT)
|
||||||
|
self.enable_pwm = machine.PWM(self.enable_pin)
|
||||||
|
self.enable_pwm.freq(500)
|
||||||
|
|
||||||
self.dir1_pin = machine.Pin(dir1, machine.Pin.OUT)
|
self.dir1_pin = machine.Pin(dir1, machine.Pin.OUT)
|
||||||
self.dir2_pin = machine.Pin(dir2, machine.Pin.OUT)
|
self.dir2_pin = machine.Pin(dir2, machine.Pin.OUT)
|
||||||
|
|
||||||
|
@ -11,25 +14,64 @@ class Motor:
|
||||||
self.reverse()
|
self.reverse()
|
||||||
|
|
||||||
def reverse(self):
|
def reverse(self):
|
||||||
print("reversing direction")
|
|
||||||
self.direction = not self.direction
|
self.direction = not self.direction
|
||||||
self.dir1_pin.value(self.direction)
|
self.dir1_pin.value(self.direction)
|
||||||
self.dir2_pin.value(not self.direction)
|
self.dir2_pin.value(not self.direction)
|
||||||
|
|
||||||
def spin(self,seconds):
|
def control(self, value):
|
||||||
self.enable_pin.value(1)
|
if value > 0.0: # forward
|
||||||
|
if not self.direction: # switch direction if necessary
|
||||||
|
self.reverse()
|
||||||
|
else: # backward
|
||||||
|
if self.direction: # switch direction if necessary
|
||||||
|
self.reverse()
|
||||||
|
# apply value as pwm signal
|
||||||
|
self.enable_pwm.duty(int(abs(value)*1023))
|
||||||
|
|
||||||
|
|
||||||
|
class Robot:
|
||||||
|
def __init__(self):
|
||||||
|
print("setting up motors")
|
||||||
|
self.m1 = Motor(4, 16, 5)
|
||||||
|
self.m2 = Motor(14, 0, 2)
|
||||||
|
print("setup complete")
|
||||||
|
|
||||||
|
def forward(self, seconds):
|
||||||
|
print("Onward, to glory!")
|
||||||
|
self.m1.control(1.0)
|
||||||
|
self.m2.control(1.0)
|
||||||
time.sleep(seconds)
|
time.sleep(seconds)
|
||||||
self.enable_pin.value(0)
|
self.m1.control(0.0)
|
||||||
|
self.m2.control(0.0)
|
||||||
|
|
||||||
print("setting up pins")
|
def backward(self, seconds):
|
||||||
m1 = Motor(4, 16, 5)
|
print("Better retreat...")
|
||||||
m2 = Motor(14, 0, 2)
|
self.m1.control(-1.0)
|
||||||
print("setup complete")
|
self.m2.control(-1.0)
|
||||||
|
time.sleep(seconds)
|
||||||
|
self.m1.control(0.0)
|
||||||
|
self.m2.control(0.0)
|
||||||
|
|
||||||
print("Motor test")
|
def spin_right(self, seconds):
|
||||||
print("Motor 1:")
|
print("You spin my head right round, right round..")
|
||||||
m1.spin(0.1)
|
self.m1.control(0.8)
|
||||||
print("Motor 2:")
|
self.m2.control(-0.8)
|
||||||
m2.spin(0.1)
|
time.sleep(seconds)
|
||||||
|
self.m1.control(0.0)
|
||||||
|
self.m2.control(0.0)
|
||||||
|
|
||||||
print("done!")
|
def spin_left(self, seconds):
|
||||||
|
print("spinning left")
|
||||||
|
self.m1.control(-0.8)
|
||||||
|
self.m2.control(0.8)
|
||||||
|
time.sleep(seconds)
|
||||||
|
self.m1.control(0.0)
|
||||||
|
self.m2.control(0.0)
|
||||||
|
|
||||||
|
def sequence(self):
|
||||||
|
self.spin_left(0.3)
|
||||||
|
self.spin_right(0.3)
|
||||||
|
self.forward(0.5)
|
||||||
|
self.backward(0.5)
|
||||||
|
|
||||||
|
wall_e = Robot()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user