-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition_control_demo.py
100 lines (76 loc) · 3.44 KB
/
position_control_demo.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
"""
testing offboard positon control with a simple takeoff script
"""
import rospy
from mavros_msgs.msg import State
from geometry_msgs.msg import PoseStamped, Point, Quaternion
import math
import numpy
class OffbPosCtl:
curr_pose = PoseStamped()
waypointIndex = 0
distThreshold = 0.4
sim_ctr = 1
des_pose = PoseStamped()
isReadyToFly = False
# locations = numpy.matrix([[0, 0, 2, 0, 0, 0],
# [8, 8, 2, 0, 0, 0],
# [0, 0, 2, 0, 0, 0]
# ])
locations = numpy.matrix([[2, 0, 1, 0, 0, -0.48717451, -0.87330464],
[0, 2, 1, 0, 0, 0, 1],
[-2, 0, 1, 0., 0., 0.99902148, -0.04422762],
[0, -2, 1, 0, 0, 0, 0],
])
def __init__(self):
rospy.init_node('offboard_test', anonymous=True)
pose_pub = rospy.Publisher('/mavros/setpoint_position/local', PoseStamped, queue_size=10)
mocap_sub = rospy.Subscriber('/mavros/local_position/pose', PoseStamped, callback=self.mocap_cb)
state_sub = rospy.Subscriber('/mavros/state', State, callback=self.state_cb)
rate = rospy.Rate(10) # Hz
rate.sleep()
self.des_pose = self.copy_pose(self.curr_pose)
shape = self.locations.shape
while not rospy.is_shutdown():
print self.sim_ctr, shape[0], self.waypointIndex
if self.waypointIndex is shape[0]:
self.waypointIndex = 0
self.sim_ctr += 1
if self.isReadyToFly:
des_x = self.locations[self.waypointIndex, 0]
des_y = self.locations[self.waypointIndex, 1]
des_z = self.locations[self.waypointIndex, 2]
self.des_pose.pose.position.x = des_x
self.des_pose.pose.position.y = des_y
self.des_pose.pose.position.z = des_z
self.des_pose.pose.orientation.x = self.locations[self.waypointIndex, 3]
self.des_pose.pose.orientation.y = self.locations[self.waypointIndex, 4]
self.des_pose.pose.orientation.z = self.locations[self.waypointIndex, 5]
self.des_pose.pose.orientation.w = self.locations[self.waypointIndex, 6]
curr_x = self.curr_pose.pose.position.x
curr_y = self.curr_pose.pose.position.y
curr_z = self.curr_pose.pose.position.z
dist = math.sqrt((curr_x - des_x)*(curr_x - des_x) + (curr_y - des_y)*(curr_y - des_y) + (curr_z - des_z)*(curr_z - des_z))
if dist < self.distThreshold:
self.waypointIndex += 1
# print dist, curr_x, curr_y, curr_z, self.waypointIndex
pose_pub.publish(self.des_pose)
rate.sleep()
def copy_pose(self, pose):
pt = pose.pose.position
quat = pose.pose.orientation
copied_pose = PoseStamped()
copied_pose.header.frame_id = pose.header.frame_id
copied_pose.pose.position = Point(pt.x, pt.y, pt.z)
copied_pose.pose.orientation = Quaternion(quat.x, quat.y, quat.z, quat.w)
return copied_pose
def mocap_cb(self, msg):
# print msg
self.curr_pose = msg
def state_cb(self,msg):
print msg.mode
if(msg.mode=='OFFBOARD'):
self.isReadyToFly = True
print "readyToFly"
if __name__ == "__main__":
OffbPosCtl()