back to Index
This class defines a python class for driving the zumocrawler.
In [1]:
from python.mbedrpc import *
import threading
class Motor:
def __init__(self, a1, a2):
self.a1=a1
self.a2=a2
def cmd(self, speed):
if speed >=0:
self.a1.write(speed)
self.a2.write(0)
else:
self.a1.write(0)
self.a2.write(-speed)
class Robot:
def __init__(self, dev='/dev/ttyACM0'):
self.mbed=SerialRPC(dev, 115200)
a1=PwmOut(self.mbed, p21)
a2=PwmOut(self.mbed, p22)
b1=PwmOut(self.mbed, p23)
b2=PwmOut(self.mbed, p24)
self.m_right = Motor(a1, a2)
self.m_left = Motor(b1, b2)
self.enabled=True
self.last_left=0
self.last_right=0
self.sensors=[]
for i in (p20,p19,p18,p17,p16,p15):
self.sensors.append(AnalogIn(self.mbed, i))
self.rlock=threading.Lock()
def enable(self):
# self.rlock.acquire()
self.enabled=True
self._cmd(self.last_left, self.last_right)
# self.rlock.release()
def disable(self):
# self.rlock.acquire()
self.enabled=False
self._cmd(self.last_left, self.last_right)
# self.rlock.release()
def drive(self, left, right):
# self.rlock.acquire()
self._cmd(left, right)
# self.rlock.release()
def cmd(self, left, right):
# self.rlock.acquire()
self._cmd(left, right)
# self.rlock.release()
def _cmd(self, left, right):
self.last_left=left
self.last_right=right
if self.enabled:
self.m_left.cmd(-left)
self.m_right.cmd(right)
else:
self.m_left.cmd(0)
self.m_right.cmd(0)
def read_sensors(self):
""" returns an array of the line sensor reflectance values
"""
def read(sensor): return sensor.read()
return map(read, self.sensors)
In [31]:
#NBINCLUDE_STOP
In [2]:
r = Robot()
In [3]:
r.cmd(1,0)
In [4]:
r.cmd(0,1)
In [5]:
r.cmd(0,0)
In [8]:
help(r.read_sensors)
In [21]:
r.read_sensors()
Out[21]:
In [41]:
for sensor in r.sensors:
print sensor.read()
In [41]:
r.enable()
In [8]:
r.disable()
In [1]:
%%file zrobot.py
from python.mbedrpc import *
import threading
class Motor:
def __init__(self, a1, a2):
self.a1=a1
self.a2=a2
def cmd(self, speed):
if speed >=0:
self.a1.write(speed)
self.a2.write(0)
else:
self.a1.write(0)
self.a2.write(-speed)
class Robot:
def __init__(self, dev='/dev/ttyACM0'):
self.mbed=SerialRPC(dev, 115200)
a1=PwmOut(self.mbed, p21)
a2=PwmOut(self.mbed, p22)
b1=PwmOut(self.mbed, p23)
b2=PwmOut(self.mbed, p24)
self.m_right = Motor(a1, a2)
self.m_left = Motor(b1, b2)
self.enabled=True
self.last_left=0
self.last_right=0
self.sensors=[]
for i in (p20,p19,p18,p17,p16,p15):
self.sensors.append(AnalogIn(self.mbed, i))
self.rlock=threading.Lock()
def enable(self):
# self.rlock.acquire()
self.enabled=True
self._cmd(self.last_left, self.last_right)
# self.rlock.release()
def disable(self):
# self.rlock.acquire()
self.enabled=False
self._cmd(self.last_left, self.last_right)
# self.rlock.release()
def drive(self, left, right):
# self.rlock.acquire()
self._cmd(left, right)
# self.rlock.release()
def cmd(self, left, right):
# self.rlock.acquire()
self._cmd(left, right)
# self.rlock.release()
def _cmd(self, left, right):
self.last_left=left
self.last_right=right
if self.enabled:
self.m_left.cmd(-left)
self.m_right.cmd(right)
else:
self.m_left.cmd(0)
self.m_right.cmd(0)
def read_sensors(self):
def read(sensor): return sensor.read()
return map(read, self.sensors)
In [ ]: