forked from tanayrastogi/LineFollower_OpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduinoCom.py
79 lines (73 loc) · 2.12 KB
/
arduinoCom.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import serial
import array
import time
class Arduino:
def __init__(self):
print("Arduino object created")
#Initialize the serial communication with the device mentioned
def startCom(self, device):
try:
self.ser = serial.Serial(device ,9600, timeout=6)
print("Waiting")
time.sleep(5)
return 1
except:
print("Arduino device not available!")
return 0
def closeCom(self):
try:
self.ser.close()
print("serial port closed")
except:
print("Failed to close serial port")
#function to set value on Arduino
def set_value(self,lin_vel,ang_vel):
try:
self.ser.flushInput()
self.ser.write("setValue "+str(lin_vel)+","+str(ang_vel)+'\n')
y = self.ser.readline()
return y
except:
print("move_robot: Failed to write to arduino \n")
#function to get value from Arduino
def get_value(self,sub_cmd):
try:
self.ser.flushInput()
self.ser.write("get.value :"+sub_cmd+'\n')
x = self.ser.readline()
return x
except:
print("get_value: Faild to read from arduino \n")
return 0
#Handskae to check that serial device we are communicating is Arduino
def sayHello(self):
try:
self.ser.flushInput()
self.ser.write("Hi_Arduino\n")
pi = self.ser.readline().rstrip()
print pi
if pi == 'Hi_Raspberry':
return 1
else:
self.closeCom()
return 0
except:
print("sayHello: Failed to write and read from arduino")
return 0
#Connect to available serial devices and check
def connect(self):
if self.startCom('/dev/ttyACM0') == 1 and self.sayHello() == 1:
print("Connected to Arduino")
return 1
elif self.startCom('/dev/ttyACM1') == 1 and self.sayHello() == 1:
print("Connected to Arduino")
return 1
elif self.startCom('/dev/ttyACM2') == 1 and self.sayHello() == 1:
print("Connected to Arduino")
return 1
elif self.startCom('/dev/ttyACM3') == 1 and self.sayHello() == 1:
print("Connected to Arduino")
return 1
else:
print("Couldn't connect to Arduino!!!")
return 0