-
Notifications
You must be signed in to change notification settings - Fork 7
/
roomba.py
56 lines (46 loc) · 1.71 KB
/
roomba.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import serial, os
class Roomba():
def __init__(self):
self.arduinoIR = ArduinoIR()
def turn(self, onOrOff):
if onOrOff == 'on':
self.turnOn()
if onOrOff == 'off':
self.turnOff()
def turnOn(self):
self.arduinoIR.executeArduinoIRCommand('turn on')
def turnOff(self):
self.arduinoIR.executeArduinoIRCommand('turn off')
def clean(self):
self.arduinoIR.executeArduinoIRCommand('clean')
def goHome(self):
self.arduinoIR.executeArduinoIRCommand('gohome')
class ArduinoIR():
def listSerialPorts(self):
if os.name == 'posix':
self.available = []
try:
self.s = serial.Serial('/dev/ttyACM0', 9600)
self.available.append('/dev/ttyACM0')
self.s.close()
except serial.SerialException:
pass
if len(self.available) == 0:
raise Exception
return self.available
def executeArduinoIRCommand(self, command):
try:
self.serialPortsArray = []
self.serialPortsArray = self.listSerialPorts()
except:
print "Is the arduino plugged in?"
if len(self.serialPortsArray) == 1:
self.ser = serial.Serial(self.serialPortsArray[0],
timeout=5) # open first serial port
self.ser.write(command) # write a string
self.response = self.ser.readline()
if self.response.endswith("is an unknown command"):
print "The Arduino didn't understand our command"
raise Exception
print(self.response),
self.ser.close() # close port