-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidterm.py
executable file
·345 lines (269 loc) · 12.1 KB
/
midterm.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#! /usr/bin/python
# Import the libraries
import rospy
from mavros_msgs.msg import State
from geometry_msgs.msg import PoseStamped, Point, Quaternion, TwistStamped
import std_msgs.msg
import math
import numpy as np
from tf.transformations import quaternion_from_euler
from mavros_msgs.srv import CommandBool, SetMode, SetModeRequest, CommandBoolRequest, CommandTOL
radius = 8
omega = 0.5
# Define the class
class mapRock:
def __init__(self):
self.curr_drone_pose = PoseStamped()
self.current_state = State()
self.vel_pose = TwistStamped()
self.des_pose = PoseStamped()
self.isReadyToFly = False
self.rate = rospy.Rate(5)
self.orientation = quaternion_from_euler(0, 0, 0)
self.distThreshold = 0.5
drone_state = rospy.Subscriber("/mavros/state", State, callback=self.state_cb)
self.pose_pub = rospy.Publisher("/mavros/setpoint_position/local", PoseStamped, queue_size=10)
self.vel_pub = rospy.Publisher("/mavros/setpoint_velocity/cmd_vel", TwistStamped, queue_size=10)
self.arming_cl = rospy.ServiceProxy("/mavros/cmd/arming", CommandBool)
self.local_pose_sub = rospy.Subscriber("/mavros/local_position/pose", PoseStamped, callback=self.poseCallback)
self.offboardCheck = False
self.pose = PoseStamped()
self.pose.pose.position.x = 0
self.pose.pose.position.y = 0
self.pose.pose.position.z = 5
self.probe = np.array([40.5, 3.8, 15])
self.rock = np.array([60.2, -7, 20.6]) #[58.20, -14.5, 19.5, #2]
self.rover = np.array([12.6, -65.0, -3.0])
self.reachAltitude = False
self.reachedLocation = False
self.orbitRock = True
def state_cb(self, msg):
self.current_state = msg
def poseCallback(self, msg):
self.curr_drone_pose = msg
def drone_state_cb(self, msg):
print('[INFO] Drone Mode : ', msg.mode)
if msg.mode == 'OFFBOARD':
self.isReadyToFly = True
print('[INFO] Ready to Fly!')
def set_Offboard(self):
print('Setting mode')
try:
while not rospy.is_shutdown() and not self.offboardCheck:
for i in range(15):
if (rospy.is_shutdown()):
break
self.pose_pub.publish(self.pose)
self.rate.sleep()
set_mode = rospy.ServiceProxy("/mavros/set_mode", SetMode)
mode = set_mode(custom_mode="OFFBOARD")
self.offboardCheck = True
self.isReadyToFly = True
self.reachAltitude = True
rospy.loginfo(mode)
print(mode)
except rospy.ServiceException as e:
rospy.loginfo('SetMode is not Offboard')
def arm_drone(self):
rospy.wait_for_service('/mavros/cmd/arming')
try:
armService = rospy.ServiceProxy("mavros/cmd/arming", CommandBool)
armService(True)
except rospy.ServiceException as e:
print("Service arm call failed: %s" % e)
def disArm_drone(self):
print('[INFO] Trying to DisArm!')
rospy.wait_for_service('/mavros/cmd/arming')
try:
arming_cl = rospy.ServiceProxy('mavros/cmd/arming', CommandBool)
response = arming_cl(value=False)
rospy.loginfo(response)
except rospy.ServiceException as e:
print('[ALERT] DisArming failed : ', e)
'''
def offboard(self):
# Wait for Flight Controller connection
print('[INFO] Enabling offboard!')
arm_cmd = CommandBoolRequest()
arm_cmd.value = True
if (self.arming_cl.call(arm_cmd).success == True):
rospy.loginfo("Vehicle armed")
# Send a few setpoints before starting
for i in range(30):
if (rospy.is_shutdown()):
break
self.pose_pub.publish(self.pose)
self.rate.sleep()
offb_set_mode = SetModeRequest()
offb_set_mode.custom_mode = 'OFFBOARD'
if (self.set_mode_client.call(offb_set_mode).mode_sent == True):
self.offboardCheck = True
rospy.loginfo("OFFBOARD enabled")
last_req = rospy.Time.now()
while not rospy.is_shutdown() and not self.offboardCheck:
if (self.current_state.mode != "OFFBOARD" and (rospy.Time.now() - last_req) > rospy.Duration(5.0)):
if (self.set_mode_client.call(offb_set_mode).mode_sent == True):
self.offboardCheck = True
rospy.loginfo("OFFBOARD enabled")
break
last_req = rospy.Time.now()
else:
if (not self.current_state.armed and (rospy.Time.now() - last_req) > rospy.Duration(5.0)):
if (self.arming_cl.call(arm_cmd).success == True):
rospy.loginfo("Vehicle armed")
last_req = rospy.Time.now()
'''
'''
def setmode_offb(self):
rospy.wait_for_service("/mavros/cmd/arming")
arming_client = rospy.ServiceProxy("/mavros/cmd/arming", CommandBool)
rospy.wait_for_service("/mavros/set_mode")
set_mode_client = rospy.ServiceProxy("/mavros/set_mode", SetMode)
arm_cmd = CommandBoolRequest()
arm_cmd.value = True
if arming_client.call(arm_cmd).success == True:
rospy.loginfo("Vehicle armed")
for i in range(100):
if rospy.is_shutdown():
break
self.local_pose_pub.publish(self.pose)
self.rate.sleep()
offb_set_mode = SetModeRequest()
offb_set_mode.custom_mode = "OFFBOARD"
if set_mode_client.call(offb_set_mode).mode_sent == True:
self.offboardCheck = True
rospy.loginfo("OFFBOARD enabled")
last_req = rospy.Time.now()
while not rospy.is_shutdown() and not self.offboardCheck:
if self.current_state.mode != "OFFBOARD" and (rospy.Time.now() - last_req) > rospy.Duration(5.0):
if set_mode_client.call(offb_set_mode).mode_sent == True:
self.offboardCheck = True
rospy.loginfo("OFFBOARD enabled")
break
last_req = rospy.Time.now()
else:
if not self.current_state.armed and (rospy.Time.now() - last_req) > rospy.Duration(5.0):
if arming_client.call(arm_cmd).success == True:
rospy.loginfo("Vehicle armed")
last_req = rospy.Time.now()
'''
def takeOff(self):
print('[INFO] Taking-off the Ground!')
self.des_pose.pose.position.x = self.curr_drone_pose.pose.position.x
self.des_pose.pose.position.y = self.curr_drone_pose.pose.position.y
self.des_pose.pose.position.z = float(self.altitude)
des = self.des_pose.pose.position
print('[INFO] des : ',des)
try:
while not rospy.is_shutdown():
self.isReadyToFly = True
if self.isReadyToFly:
curr = self.curr_drone_pose.pose.position
dist = math.sqrt(
(curr.x - des.x)**2 + (curr.y - des.y)**2 + (curr.z - des.z)**2)
if dist > self.distThreshold:
self.pose_pub.publish(self.des_pose)
self.rate.sleep()
except rospy.ServiceException as e:
print('[ALERT] Not able to Take-Off!', e)
def setStabilizeMode(self):
rospy.wait_for_service('/mavros/set_mode')
try:
flightModeService = rospy.ServiceProxy('/mavros/set_mode', SetMode)
isModeChanged = flightModeService(custom_mode='OFFBOARD') # return true or false
except rospy.ServiceException as e:
print("service set_mode call failed: %s. GUIDED Mode could not be set. Check that GPS is enabled" % e)
def setTakeoffMode(self):
rospy.wait_for_service('/mavros/cmd/takeoff')
try:
takeoffService = rospy.ServiceProxy('/mavros/cmd/takeoff', CommandTOL)
takeoffService(altitude=5, latitude=0, longitude=0, min_pitch=0, yaw=0)
except rospy.ServiceException as e:
print("Service takeoff call failed: %s" % e)
def land(self):
print('[INFO] Trying to Land on site!')
rospy.wait_for_service('/mavros/cmd/land')
try:
while not rospy.is_shutdown():
land_cl = rospy.ServiceProxy('/mavros/cmd/land', CommandTOL)
response = land_cl(altitude=10, latitude=0, longitude=0, min_pitch=0, yaw=0)
rospy.loginfo(response)
except rospy.ServiceException as e:
print('[ALERT] Not able to Land!', e)
def got_to_location(self, location):
print('[INFO] Heading to new location')
self.reachedLocation = False
self.des_pose.pose.position.x = location[0]
self.des_pose.pose.position.y = location[1]
self.des_pose.pose.position.z = location[2]
self.des_pose.pose.orientation.x = self.orientation[0]
self.des_pose.pose.orientation.y = self.orientation[1]
self.des_pose.pose.orientation.z = self.orientation[2]
self.des_pose.pose.orientation.w = self.orientation[3]
des = self.des_pose.pose.position
try:
while not rospy.is_shutdown():
if self.isReadyToFly:
curr = self.curr_drone_pose.pose.position
dist = math.sqrt(
(curr.x - des.x)**2 + (curr.y - des.y)**2 + (curr.z - des.z)**2)
if dist > self.distThreshold:
self.pose_pub.publish(self.des_pose)
else:
print('[INFO] Reached Location!')
self.reachedLocation = True
break
except rospy.ServiceException as e:
print('[ALERT] Not able to Fly to Location!', e)
'''
def revolve_rock(self):
rospy.loginfo('Revolving around the rock initiated!')
pass
'''
def revolve_rock(self):
rospy.loginfo("Orbitting Rock")
self.t0 = rospy.get_time()
while not rospy.is_shutdown() and self.orbitRock:
t = rospy.get_time() - self.t0
if t > 15:
self.orbitRock = False
break
'''
self.pose.pose.position.x = self.rock[0] + (radius * math.cos(omega * t))
self.pose.pose.position.y = self.rock[1] + (radius * math.sin(omega * t))
self.pose.pose.position.z = self.rock[2]
'''
self.vel_pose.twist.linear.x = (-5 * math.cos(omega * t))
self.vel_pose.twist.linear.y = (-5 * math.sin(omega * t))
self.vel_pose.twist.linear.z = 0
heading = math.atan2(self.curr_drone_pose.pose.position.y - 0,
self.curr_drone_pose.pose.position.x - 0)
self.vel_pose.twist.angular.z = 0.49 #self.orientation[2]
self.vel_pose.header.stamp = rospy.Time.now()
self.vel_pub.publish(self.vel_pose)
self.rate.sleep()
def activate_slam(self):
pass
def main(self):
print('[INFO] Started!')
while not rospy.is_shutdown() and not self.current_state.connected:
self.rate.sleep()
self.arm_drone()
if not self.offboardCheck:
self.set_Offboard()
if self.reachAltitude:
self.got_to_location(self.probe)
if self.reachedLocation:
self.orientation = quaternion_from_euler(0, 0, -3.14/2)
self.got_to_location(self.rock)
if self.reachedLocation:
self.revolve_rock()
if self.reachedLocation:
self.got_to_location(self.rover)
self.disArm_drone()
print('[INFO] Mission Accomplished')
if __name__ == '__main__':
rospy.init_node('offboard')
obj = mapRock()
obj.main()
rospy.spin()