-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget.py
179 lines (132 loc) · 6.26 KB
/
target.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
import time
import math
import threading
import polar
import noise
from limits import mapToLimits, angleMod
from actions import *
from debug import DEBUG_TARGET
class TargetThread(threading.Thread):
def __init__(self, MotionThread, power, target_timeperiod = 0.1, move_timeperiod = 0.1, target_reached_radius = 0.1): #default 10hz target, 10hz move, 10cm target radius
threading.Thread.__init__(self)
self.name = "TargetThread"
self.MotionThread = MotionThread
self.power = power
self.move_timeperiod = move_timeperiod
self.target_timeperiod = target_timeperiod
self.target_reached_radius = target_reached_radius
self.path_lock = threading.Lock()
self.emergency_lock = threading.Lock()
self.path = []
self.target = None
self.polar_r = None
self.scaled_polar_r = None
self.polar_t = None
self.d_theta = None
self.emergency_stop = False
def clearPath(self):
with self.path_lock:
self.path = []
def setTarget(self, new_target):
self.clearPath()
self.addTarget(new_target)
def addTarget(self, new_target):
with self.path_lock:
self.path.append(new_target)
def setPath(self, new_path):
self.clearPath()
self.addPath(new_path)
def addPath(self, new_path):
with self.path_lock:
self.path.extend(new_path)
def calculatePolar(self):
robot_location = self.MotionThread.robot_location ##needs attention
self.polar_r = polar.getPolarR(robot_location, self.target)
self.polar_t = polar.getPolarT(robot_location, self.target)
self.d_theta = angleMod(self.polar_t - robot_location['yaw'])
self.scaled_polar_r = math.cos(math.radians(self.d_theta)) * self.polar_r
def processNextTarget(self):
self.target = None
with self.path_lock:
if (len(self.path) != 0):
self.target = self.path.pop(0) #return first item in array and remove it from array
if (self.target != None):
self.turnToTarget()
self.moveToTarget()
def setEmergencyStop(self, new_emergency_stop):
with self.emergency_lock:
self.emergency_stop = new_emergency_stop
def checkEmergencyStop(self):
with self.emergency_lock:
return self.emergency_stop
def checkIfTurnedToTarget(self):
turned = False
max_d_theta = self.target.get('max_d_theta', 180)
if (abs(self.d_theta) <= max_d_theta):
turned = True
noise.signalTarget(self.power)
return turned
def turnToTarget(self):
self.calculatePolar()
if ((self.checkEmergencyStop() == False) and (self.checkIfTurnedToTarget() == False)):
self.MotionThread.setAction(TURN_TO, self.polar_t)
time.sleep(self.move_timeperiod)
while ((self.checkEmergencyStop() == False) and (self.checkIfTurnedToTarget() == False)):
self.calculatePolar()
self.MotionThread.addAction(TURN_TO_CHANGE, self.polar_t)
def checkIfTargetReached(self):
reached = False
if (self.polar_r < self.target_reached_radius):
reached = True
noise.signalTarget(self.power)
return reached
def changeCurrentTarget(self, new_current_target):
self.target = new_current_target
def moveToTarget(self):
self.calculatePolar()
if ((self.checkEmergencyStop() == False) and (self.checkIfTargetReached() == False)):
self.MotionThread.setAction(MOVE_AND_TURN_TO, self.scaled_polar_r, self.polar_t)
time.sleep(self.move_timeperiod)
while ((self.checkEmergencyStop() == False) and (self.checkIfTargetReached() == False)):
self.calculatePolar()
self.MotionThread.addAction(MOVE_AND_TURN_TO_CHANGE, self.scaled_polar_r, self.polar_t)
time.sleep(self.move_timeperiod)
def run(self):
print "Starting " + self.name
while(True):
self.processNextTarget()
time.sleep(self.target_timeperiod)
print "Exiting " + self.name
# def run(self):
# print "Starting " + self.name
#
# wake_up_time = time.time() + self.time_period
#
# while (True):
# self.time_to_sleep = mapToLimits(wake_up_time - time.time(), self.time_period, 0)
# wake_up_time += self.time_period
# time.sleep(self.time_to_sleep)
# with self.lock:
#
# if (self.target == None):
# self.MotionThread.setAction(STILL)
#
# else:
# self.calculatePolar()
# self.MotionThread.setAction(MOVE_AND_TURN_TO, self.polar_r, self.polar_t)
#
# while (True):
# self.time_to_sleep = mapToLimits(wake_up_time - time.time(), self.time_period, 0)
# wake_up_time += self.time_period
# time.sleep(self.time_to_sleep)
# self.calculatePolar()
# self.MotionThread.setAction(MOVE_AND_TURN_TO_CHANGE, self.polar_r, self.polar_t)
#
#
# print "Exiting " + self.name
def debug(self):
if (DEBUG_TARGET == True):
print self.name
print "target = " + str(self.target)
print "len(path) = " + str(len(self.path))
print "polar_r = " + str(self.polar_r) + ", polar_t = " + str(self.polar_t) + ", scaled_polar_r = " + str(self.scaled_polar_r) + ", d_theta = " + str(self.d_theta)