/
kinectProcessor.py
176 lines (133 loc) · 4.99 KB
/
kinectProcessor.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
from freenect import sync_get_depth as get_depth, sync_get_video as get_video
import struct
import freenect
import Create2_TetheredDrive as robotLib
import numpy as np
import cv
import sys, glob # for listing serial ports
from Tkinter import *
import tkMessageBox
import tkSimpleDialog
try:
import serial
except ImportError:
# tkMessageBox.showerror('Import error', 'Please install pyserial.')
print("'Import error: Please install pyserial.")
raise
connection = None
VELOCITYCHANGE = 200
ROTATIONCHANGE = 300
distanceThreshold = 100
whiteThreshold = 255
class TetheredApp(Tk):
def __init__(self):
Tk.__init__(self)
# sendCommandASCII takes a string of whitespace-separated, ASCII-encoded base 10 values to send
def sendCommandASCII(self, command):
cmd = ""
for v in command.split():
cmd += chr(int(v))
self.sendCommandRaw(cmd)
def callMovementCommand(self,rVel,lVel):
cmd = struct.pack(">Bhh", 145, rVel, lVel) #command for robot full stop
self.sendCommandRaw(cmd)
self.callbackKeyLastDriveCommand = cmd
#TODO add a method that automatically adjusts tilt of kinect
def doloop(self):
global depth, rgb
while True:
(depth, _), (rgb, _) = get_depth(), get_video()
d3 = np.dstack((depth, depth, depth)).astype(np.uint8)
redAvg = 0
greenAvg = 0
blueAvg = 0
distanceAvg = 0
for i in range(len(d3)):
redAvg += d3[0][i][0]
greenAvg += d3[0][i][1]
blueAvg += d3[0][i][2]
redAvg = redAvg / len(d3)
greenAvg = greenAvg / len(d3)
blueAvg = blueAvg / len(d3)
distanceAvg = (redAvg + blueAvg + greenAvg) / 3
cv.ShowImage('both', cv.fromarray(np.array(d3[::2, ::2, ::-1])))
self.sendCommandASCII('128') # sets mode to passive
self.sendCommandASCII('131') # sets mode to safe
if(distanceAvg >= 255 or distanceAvg <= 10):
self.sendCommandASCII('140 3 1 64 16 141 3') #beep
self.callMovementCommand(0,-100)
elif (distanceAvg > distanceThreshold):
self.sendCommandASCII('140 3 1 64 16 141 3') #beep
self.callMovementCommand(100,100)
else:
self.callMovementCommand(0,0) #stop
self.callMovementCommand(0,100) #Right turn
cv.WaitKey(10)
def getSerialPorts(self):
"""Lists serial ports
From http://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of available serial ports
"""
if sys.platform.startswith('win'):
ports = ['COM' + str(i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this is to exclude your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def connect(self):
global connection
if connection is not None:
print "Already connected"
return
try:
ports = self.getSerialPorts()
print(ports)
port = ports[0]
except EnvironmentError:
print "ENVIRONMENT ERROR!!"
if port is not None:
print "Trying " + str(port) + "... "
try:
connection = serial.Serial(port, baudrate=115200, timeout=1)
print "Connected!"
except:
print "Failed."
# sendCommandRaw takes a string interpreted as a byte array
def sendCommandRaw(self, command):
global connection
try:
if connection is not None:
connection.write(command)
# else:
# # tkMessageBox.showerror('Not connected!', 'Not connected to a robot!')
# print ""
except serial.SerialException:
print "Lost connection"
# tkMessageBox.showinfo('Uh-oh', "Lost connection to the robot!")
connection = None
print ' '.join([ str(ord(c)) for c in command ])
self.text.insert(END, ' '.join([ str(ord(c)) for c in command ]))
self.text.insert(END, '\n')
self.text.see(END)
np.set_printoptions(threshold='nan')
distanceThreshold = 100
if __name__ == "__main__":
robot = TetheredApp()
robot.connect()
robot.doloop()