-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy2.py
278 lines (224 loc) · 7.13 KB
/
py2.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
"""
@@@ warning @@@
U can control the local position of the drone
actually the problem is when u exit the controller
then from next time u run this then the local x, y pos are different
so u need to know if u run this again then u need to check
the local position of the drone and fix it near to the origin local position
"""
from __future__ import print_function
import threading
import roslib
import rospy
import mavros
import math
from mavros_msgs.msg import PositionTarget
from mavros_msgs.srv import SetMode, CommandBool
from geometry_msgs.msg import Twist
import sys, select, termios, tty
msg = """
--------------------------
"POSE CONTORLLER with DroNet"
--------------------------
offboard : q
arming : e
Start/Stop DroNet : t
+x +z(i)
^ w ^
| a s d < j | l >
| x v
@----> -y -z(k)
CTRL-C to quit------------
--------------------------
"""
moveBindings = {
'w':(1,0,0,0),
's':(-1,0,0,0),
'a':(0,1,0,0),
'd':(0,-1,0,0),
'x':(0,0,0,0),
'i':(0,0,1,0),
'k':(0,0,-1,0),
'j':(0,0,0,1), 'l':(0,0,0,-1),
'q':(0,0,0,0), 'e':(0,0,0,0),
't':(0,0,0,0),
}
def getKey(key_timeout):
tty.setraw(sys.stdin.fileno())
rlist, _, _ = select.select([sys.stdin], [], [], key_timeout)
if rlist:
key = sys.stdin.read(1)
else:
key = ''
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
return key
def arming(bool_):
"""
arming(0) >> disarming
arming(1) > arming
"""
arming = rospy.ServiceProxy('/mavros/cmd/arming', CommandBool)
arming(value=bool_)
def set_mode(mode):
"""
There are many modes u can set
set_mode("OFFBOARD") >> Offboard mode
MANUAL, ACRO, ALTCTL, POSCTL,
OFFBOARD, STABILIZED, RATTITUDE,
AUTO.MISSION ,AUTO.LOITER, AUTO.RTL,
AUTO.LAND, AUTO.RTGS, AUTO.READY,
AUTO.TAKEOFF
"""
set_mode = rospy.ServiceProxy('/mavros/set_mode', SetMode)
set_mode(0, mode)
mode = 0#0 stop, 1 forward, 2 backward, 3 left, 4 right, 5 Auto by DroNet
class PublishThread(threading.Thread):
def __init__(self, rate):
super(PublishThread, self).__init__()
self.publisher = rospy.Publisher('mavros/setpoint_raw/local', PositionTarget, queue_size=3)
self.x = 0.0
self.y = 0.0
self.z = 0.0
self.yaw = 0
self.condition = threading.Condition()
self.done = False
# Set timeout to None if rate is 0 (causes new_message to wait forever
# for new data to publish)
if rate != 0.0:
self.timeout = 1.0 / rate
else:
self.timeout = None
self.start()
def wait_for_subscribers(self):
i = 0
while not rospy.is_shutdown() and self.publisher.get_num_connections() == 0:
if i == 4:
print("Waiting for subscriber to connect to {}".format(self.publisher.name))
rospy.sleep(0.5)
i += 1
i = i % 5
if rospy.is_shutdown():
raise Exception("Got shutdown request before subscribers connected")
def update(self, x, y, z, yaw):
self.condition.acquire()
self.x = x
self.y = y
self.z = z
self.yaw = yaw
# Notify publish thread that we have a new message.
self.condition.notify()
self.condition.release()
def stop(self):
self.done = True
self.update(0, 0, 0, 0)
self.join()
def run(self):
pos = PositionTarget()
pos.coordinate_frame = 1
while not self.done:
self.condition.acquire()
# Wait for a new message or timeout.
self.condition.wait(self.timeout)
# Copy state into twist message.
if mode == 1:
pos_x = math.cos(-yaw)
pos_y = -math.sin(-yaw)
elif mode == 2:
pos_x = -math.cos(-yaw)
pos_y = math.sin(-yaw)
elif mode == 3:
pos_x = math.sin(-yaw)
pos_y = math.cos(-yaw)
elif mode == 4:
pos_x = -math.sin(-yaw)
pos_y = -math.cos(-yaw)
elif mode == 0:
pos_x = 0
pos_y = 0
pos.velocity.x = pos_x
pos.velocity.y = pos_y
pos.position.z = pos_z
pos.yaw = yaw
pos.type_mask = 3
self.condition.release()
# Publish.
self.publisher.publish(pos)
# Publish stop message when thread exits.
pos.velocity.x = 0
pos.velocity.y = 0
pos.position.z = 0
pos.yaw = 0
self.publisher.publish(pos)
if __name__=="__main__":
msg_cnt = 0
settings = termios.tcgetattr(sys.stdin)
rospy.init_node('teleop_setpoint_keyboard')
key_timeout = rospy.get_param("~key_timeout", 0.0)
if key_timeout == 0.0:
key_timeout = None
repeat = rospy.get_param("~repeat_rate", 30)
pub_thread = PublishThread(repeat)
pos_x = 0.0
pos_y = 0.0
pos_z = 0.0
yaw = 0.0
print(msg)
txt = ""
txt += 'x : ' + str(pos_x)
txt += ', y : ' + str(pos_y)
txt += ', z : ' + str(pos_z)
txt += ', deg : ' + str(yaw*180/3.1415)
print(txt, end="")
try:
pub_thread.wait_for_subscribers()
pub_thread.update(pos_x, pos_y, pos_z, yaw)
while(1):
key = getKey(key_timeout)
if key == 'q':
set_mode("OFFBOARD")
if key == 'e':
arming(1)
if key == 'm':
set_mode("MANUAL")
if key == 'w':
mode = 1
#pos_x = math.cos(-yaw)
#pos_y = -math.sin(-yaw)
if key == 'x':
mode = 2
#pos_x = -math.cos(-yaw)
#pos_y = math.sin(-yaw)
if key == 'a':
mode = 3
#pos_x = math.sin(-yaw)
#pos_y = math.cos(-yaw)
if key == 'd':
mode = 4
#pos_x = -math.sin(-yaw)
#pos_y = -math.cos(-yaw)
if key == 's':
mode = 0
#pos_x = 0
#pos_y = 0
if key == 't':
mode = 5
if key in moveBindings.keys():
#pos_x += moveBindings[key][0] * 0.1
#pos_y += moveBindings[key][1] * 0.1
pos_z += moveBindings[key][2] * 0.1
yaw += moveBindings[key][3] * 3.1415 / 20
if pos_z <= 0 : pos_z = 0
else:
# stopped.
if (key == '\x03'):
break
pub_thread.update(pos_x, pos_y, pos_z, yaw)
print(msg)
print('pos_z : ',pos_z)
print('yaw : ',yaw*180/3.1415)
except Exception as e:
print(e)
finally:
pub_thread.stop()
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)