-
Notifications
You must be signed in to change notification settings - Fork 2
/
robotController.py
223 lines (185 loc) · 8.41 KB
/
robotController.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import piconzero as pz, time # get the servo module
import math
from init import * # hardware configuration
import hcsr04 # get the ultrasonic module
ultrasonicInitialized = False;
channels=[rotChannel,tiltChannel,liftChannel,gripChannel]
limMax=[rotMax, tiltMax, liftMax, gripMax]
limMin=[rotMin, tiltMin, liftMin, gripMin]
positions=[rotInit, tiltInit, liftInit, gripInit] # Position variable that the program operates on
speed=speedInit
#=====================================================================
# Initializes the robot arm to it's start position
def init():
print "Initialization started"
pz.init() # load servo operation library
# Set output mode to Servo
pz.setOutputConfig(rotChannel, 2)
pz.setOutputConfig(tiltChannel, 2)
pz.setOutputConfig(liftChannel, 2)
pz.setOutputConfig(gripChannel, 2)
# Sets the robot to the initial position.
# Possible issue: rapid movement from the previous operation stop point.
for i in channels:
_safeSetAngle(i, positions[i])
time.sleep(.5) # Sleep to reduce violent motions if arm is way off start target
return 0
#=====================================================================
#=====================================================================
# Initializes the ultrasonic module
def initUltrasonic():
hcsr04.init()
global ultrasonicInitialized
ultrasonicInitialized = True;
#=====================================================================
#=====================================================================
# Prepares robot arm for turning off
def shutdown():
resetPosition()
pz.cleanup()
if ultrasonicInitialized:
hcsr04.cleanup()
#=====================================================================
#=====================================================================
# Resets robot arm to default location
def resetPosition():
_smoothMotion(rotChannel, rotInit)
_smoothMotion(tiltChannel, tiltInit)
_smoothMotion(liftChannel, liftInit)
_smoothMotion(gripChannel, gripInit)
#=====================================================================
#=========================================================================================
# Determines range to the nearest object, taking the mean value of several measurements
def texasRanger():
i = 0 # measurement counter
imax = 5 # total measurement number
distances = []
while i < imax:
distances.append(hcsr04.getDistance())
time.sleep(0.05) # Sleep needed to allow pings from previous measurements to dissipate
i += 1
distances.sort()
return distances[2]
#=========================================================================================
#=====================================================================
# A function to make roboArm operation movement smoother, please use for all servo operations
# Will return False if angle exceeds maximum range or True when the servo has been moved
def _smoothMotion(channel,stop):
if not _canSetAngle(channel, stop):
print "Operational range exceeded"
return False
while positions[channel] != stop:
diff = stop - positions[channel]
positions[channel] += math.copysign(min(1, abs(diff)), diff)
_safeSetAngle(channel, int(positions[channel]))
time.sleep(speed)
return True
#=====================================================================
#=====================================================================
# Set servo with limit check
def _safeSetAngle(channel, angle):
if _canSetAngle(channel, angle):
pz.setOutput(channel, angle)
return True
return False
#=====================================================================
#=====================================================================
# Returns true if angle is valid for channel
def _canSetAngle(channel, angle):
if angle > limMax[channel] or angle < limMin[channel]:
return False
return True
#=====================================================================
#=====================================================================
# Increase/decrease rotation number of degrees
def rotate(offset):
return _smoothMotion(rotChannel, positions[rotChannel] + offset)
#=====================================================================
#=====================================================================
# Increase/decrease tilt number of degrees
def tilt(offset):
return _smoothMotion(tiltChannel, positions[tiltChannel] + offset)
#=====================================================================
#=====================================================================
# Increase/decrease lift number of degrees
def lift(offset):
return _smoothMotion(liftChannel, positions[liftChannel] + offset)
#=====================================================================
#=====================================================================
# Increase/decrease grip number of degrees
def grip(offset):
return _smoothMotion(gripChannel, positions[gripChannel] + offset)
#=====================================================================
#=====================================================================
# Sets rotation to number of degrees
def setRotation(angle):
return _smoothMotion(rotChannel, angle)
#=====================================================================
#=====================================================================
# Sets tilt to number of degrees
def setTilt(angle):
return _smoothMotion(tiltChannel, angle)
#=====================================================================
#=====================================================================
# Sets lift to number of degrees
def setLift(angle):
return _smoothMotion(liftChannel, angle)
#=====================================================================
#=====================================================================
# Sets grip to number of degrees
def setGrip(angle):
return _smoothMotion(gripChannel, angle)
#=====================================================================
#=====================================================================
# Sets all to number of degrees
def setAll(rotation, tilt, lift, grip):
targetValues = [rotation, tilt, lift, grip]
diffs = []
for i in channels:
if not _canSetAngle(i, targetValues[i]):
print "Operational range exceeded"
return False
else:
diffs.append(abs(targetValues[i] - positions[i]))
# Number of iterations set so that max change per iteration is
# 1 degree, to give uniform servo speed.
deltas = []
iterations = int(max(diffs))
if iterations == 0: # No change needed
return True
for i in channels:
deltas.append((targetValues[i] - positions[i]) / iterations)
for x in range(0, iterations):
for i in channels:
if positions[i] != targetValues[i]:
positions[i] += deltas[i]
_safeSetAngle(i, int(positions[i]))
time.sleep(speed)
return True
#=====================================================================
#=====================================================================
# Gets rotation to number of degrees
def getRotation():
return positions[rotChannel]
#=====================================================================
#=====================================================================
# Gets tilt to number of degrees
def getTilt():
return positions[tiltChannel]
#=====================================================================
#=====================================================================
# Gets lift to number of degrees
def getLift():
return positions[liftChannel]
#=====================================================================
#=====================================================================
# Gets grip to number of degrees
def getGrip():
return positions[gripChannel]
#=====================================================================
#=====================================================================
# Sets servo speed
def setSpeed(newSpeed):
global speed
speed = newSpeed
#=====================================================================