-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositionContol.py
executable file
·270 lines (219 loc) · 8.33 KB
/
positionContol.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
#!/usr/bin/env python
# ROS python API
import rospy
# 3D point & Stamped Pose msgs
from geometry_msgs.msg import Point, PoseStamped
# import all mavros messages and services
from mavros_msgs.msg import *
from mavros_msgs.srv import *
from std_msgs.msg import Int32MultiArray
# oepncv
import cv2
#darkflow
from darkflow.net.build import TFNet
import time
# Flight modes class
# Flight modes are activated using ROS services
class fcuModes:
def __init__(self):
pass
def setTakeoff(self):
rospy.wait_for_service('mavros/cmd/takeoff')
try:
takeoffService = rospy.ServiceProxy('mavros/cmd/takeoff', mavros_msgs.srv.CommandTOL)
takeoffService(altitude = 3)
except rospy.ServiceException as e:
print ("Service takeoff call failed: %s",e)
def setArm(self):
rospy.wait_for_service('mavros/cmd/arming')
try:
armService = rospy.ServiceProxy('mavros/cmd/arming', mavros_msgs.srv.CommandBool)
armService(True)
except rospy.ServiceException as e:
print ("Service arming call failed: %s",e)
def setDisarm(self):
rospy.wait_for_service('mavros/cmd/arming')
try:
armService = rospy.ServiceProxy('mavros/cmd/arming', mavros_msgs.srv.CommandBool)
armService(False)
except rospy.ServiceException as e:
print ("Service disarming call failed: %s",e)
def setStabilizedMode(self):
rospy.wait_for_service('mavros/set_mode')
try:
flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
flightModeService(custom_mode='STABILIZED')
except rospy.ServiceException as e:
print ("service set_mode call failed: %s. Stabilized Mode could not be set.",e)
def setOffboardMode(self):
rospy.wait_for_service('mavros/set_mode')
try:
flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
flightModeService(custom_mode='OFFBOARD')
except rospy.ServiceException as e:
print ("service set_mode call failed: %s. Offboard Mode could not be set.",e)
def setAltitudeMode(self):
rospy.wait_for_service('mavros/set_mode')
try:
flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
flightModeService(custom_mode='ALTCTL')
except rospy.ServiceException as e:
print ("service set_mode call failed: %s. Altitude Mode could not be set.",e)
def setPositionMode(self):
rospy.wait_for_service('mavros/set_mode')
try:
flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
flightModeService(custom_mode='POSCTL')
except rospy.ServiceException as e:
print ("service set_mode call failed: %s. Position Mode could not be set.",e)
def setAutoLandMode(self):
rospy.wait_for_service('mavros/set_mode')
try:
flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
flightModeService(custom_mode='AUTO.LAND')
except rospy.ServiceException as e:
print ("service set_mode call failed: %s. Autoland Mode could not be set.",e)
class Controller:
# initialization method
def __init__(self):
# Drone state
self.state = State()
# Instantiate a setpoints message
self.sp = PositionTarget()
# set the flag to use position setpoints and yaw angle
self.sp.type_mask = int('010111111000', 2)
# LOCAL_NED
self.sp.coordinate_frame = 1
# We will fly at a fixed altitude for now
# Altitude setpoint, [meters]
self.ALT_SP = 3.0
# update the setpoint message with the required altitude
self.sp.position.z = self.ALT_SP
# Step size for position update
self.STEP_SIZE = 2.0
# Fence. We will assume a square fence for now
self.FENCE_LIMIT = 5.0
# A Message for the current local position of the drone
self.local_pos = Point(0.0, 0.0, 3.0)
# initial values for setpoints
self.sp.position.x = 0.0
self.sp.position.y = 0.0
# speed of the drone is set using MPC_XY_CRUISE parameter in MAVLink
# using QGroundControl. By default it is 5 m/s.
# Callbacks
## local position callback
def posCb(self, msg):
self.local_pos.x = msg.pose.position.x
self.local_pos.y = msg.pose.position.y
self.local_pos.z = msg.pose.position.z
## Drone State callback
def stateCb(self, msg):
self.state = msg
## Update setpoint message
def updateSp(self):
self.sp.position.x = self.local_pos.x
self.sp.position.y = self.local_pos.y
def x_dir(self):
self.sp.position.x = self.local_pos.x + 5
self.sp.position.y = self.local_pos.y
def neg_x_dir(self):
self.sp.position.x = self.local_pos.x - 5
self.sp.position.y = self.local_pos.y
def y_dir(self):
self.sp.position.x = self.local_pos.x
self.sp.position.y = self.local_pos.y + 5
def neg_y_dir(self):
self.sp.position.x = self.local_pos.x
self.sp.position.y = self.local_pos.y - 5
# Main function
def main():
# initiate node
rospy.init_node('webcam_node')
# flight mode object
modes = fcuModes()
# controller object
cnt = Controller()
# ROS loop rate
rate = rospy.Rate(20.0)
# Subscribe to drone state
rospy.Subscriber('mavros/state', State, cnt.stateCb)
# Subscribe to drone's local position
rospy.Subscriber('mavros/local_position/pose', PoseStamped, cnt.posCb)
# Setpoint publisher
sp_pub = rospy.Publisher('mavros/setpoint_raw/local', PositionTarget, queue_size=1)
# Make sure the drone is armed
while not cnt.state.armed:
modes.setArm()
rate.sleep()
# set in takeoff mode and takeoff to default altitude (3 m)
# modes.setTakeoff()
# rate.sleep()
# We need to send few setpoint messages, then activate OFFBOARD mode, to take effect
k=0
while k<10:
sp_pub.publish(cnt.sp)
rate.sleep()
k = k + 1
# activate OFFBOARD mode
modes.setOffboardMode()
# load the head detector model and the weights
options = {
'model': '/home/xi/catkin_ws/src/headdetector/src/cfg/yolov2-tiny-1c.cfg',
'load': 71875,
'threshold': 0.35,
'labels': '/home/xi/catkin_ws/src/headdetector/src/labels.txt',
'backup': '/home/xi/catkin_ws/src/headdetector/src/ckpt'
}
tfnet = TFNet(options)
# define the frame properties
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# make prediction on real time stream
#while True:
i = 0
c1 = 0
c2 = 0
# ROS main loop
while not rospy.is_shutdown():
#cnt.updateSp()
stime = time.time()
ret, frame = capture.read()
if ret:
results = tfnet.return_predict(frame)
if results:
# read the predicted bbox
tl=(results[0]['topleft']['x'],results[0]['topleft']['y'])
br=(results[0]['bottomright']['x'],results[0]['bottomright']['y'])
label = results[0]['label']
c1 = round((tl[0] + br[0]) / 2 - 640)
c2 = round((tl[1] + br[1]) / 2 - 360)
print('center postion:',c1,c2)
# insert bbox and label on the detected region
text = '{}'.format(label)
frame = cv2.rectangle(frame, tl, br, (255,0,0), 5)
frame = cv2.putText(
frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
cv2.imshow('frame', frame)
#print('FPS {:.1f}'.format(1 / (time.time() - stime)))
if cv2.waitKey(1) & 0xFF == ord('q'):
# break
capture.release()
cv2.destroyAllWindows()
exit()
if c1 <=100 and c1 >=-100:
cnt.updateSp()
print('hovering')
elif c1 < -100:
cnt.x_dir()
print('fw_right')
elif c1 > 100:
cnt.neg_x_dir()
print('fw_left')
sp_pub.publish(cnt.sp)
rate.sleep()
if __name__ == '__main__':
try:
main()
except rospy.ROSInterruptException:
pass