-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialTest3Backup.py
More file actions
95 lines (79 loc) · 2.48 KB
/
serialTest3Backup.py
File metadata and controls
95 lines (79 loc) · 2.48 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from time import sleep
from pySerialTransfer import pySerialTransfer as txfer
import numpy as np
import re
import RPi.GPIO as GPIO
port = 'serial0'
hwResetPin = 26 # GPIO pin 26 not physical pin 26
def sendMessage(message):
for index in range(len(message)):
link.txBuff[index] = message[index]
link.send(len(message))
def ping():
alive = ['A','L','I','V','E','?']
sendMessage(alive)
def getResponse(): # Returns false if no message is available
if link.available():
if link.status >= 0:
response = ''
for index in range(link.bytesRead):
char = chr(link.rxBuff[index])
response += char
return response.rstrip('\x00') # Fix shit, strings be damned
else:
print('Error: {}'.format(link.status))
return 'False'
else:
return 'False'
def init():
GPIO.setmode(GPIO.BCM) # Setup GPIO for Arduino HW reset
GPIO.setup(hwResetPin, GPIO.OUT)
global link
link = txfer.SerialTransfer(port)
link.open()
sleep(5) # Allow time for arduino to reset
def negotiateConnection(): # Attempts to establish a connection with the arduino
attempts = 1
while True:
print('Attempting Connection: ' + str(attempts))
ping()
response = getResponse()
if response == 'ALIVE':
break
else:
if attempts >= 10:
print('Arduino not found after 10 connection attempts')
attempts = 0
resetArduino()
negotiateConnection()
return None
sleep(0.5)
attempts += 1
print('Connection established')
def getImage():
takeImage = ['T','A','K','E','I','M','A','G','E']
sendMessage(takeImage)
sleep(0.1)
return getResponse()
def resetArduino():
print('Reseting Arduino')
GPIO.output(hwResetPin, GPIO.HIGH)
sleep(1) # 0.25s maybe ok?
GPIO.output(hwResetPin, GPIO.LOW)
sleep(5) # Allow time for Arduino to boot
if __name__ == '__main__':
try:
print('Starting Program')
init()
negotiateConnection()
while True:
image = getImage()
if image and image != 'False':
print(image)
else:
print('No Image Found, Attempting to Renogotiate Connection')
negotiateConnection()
sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
link.close()