-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvelocity_control_boat.py
95 lines (71 loc) · 3.02 KB
/
velocity_control_boat.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
"""
Jnaneshwar Das
Offboard velocity control with MAVROS for following position setpoints on Robo-boat-o repeatedly
"""
import rospy
from mavros_msgs.msg import State
from geometry_msgs.msg import PoseStamped, Point, Quaternion, Twist
import math
import numpy
class OffbPosCtl:
curr_pose = PoseStamped()
waypointIndex = 0
sim_ctr = 1
des_vel = Twist()
isReadyToFly = False
WIDTH = 20
THR_FACTOR = 2
distThreshold = 1
locations = numpy.matrix([[WIDTH, 0, 0, 0, 0, -0.48717451, -0.87330464],
[0, WIDTH, 1, 0, 0, 0, 1],
[-WIDTH, 0, 1, 0., 0., 0.99902148, -0.04422762],
[0, -WIDTH, 1, 0, 0, 0, 0],
])
def __init__(self):
rospy.init_node('offboard_velocity_test', anonymous=True)
vel_pub = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel_unstamped', Twist, 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()
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]
self.des_x = des_x
self.des_y = des_y
curr_x = self.curr_pose.pose.position.x
curr_y = self.curr_pose.pose.position.y
dist = math.sqrt((curr_x - des_x)*(curr_x - des_x) + (curr_y - des_y)*(curr_y - des_y))
norm = math.sqrt((curr_x - des_x)*(curr_x - des_x) + (curr_y - des_y)*(curr_y - des_y))
norm_throttled = norm*self.THR_FACTOR
self.des_vel.linear.x = (des_x - curr_x)/norm_throttled
self.des_vel.linear.y = (des_y - curr_y)/norm_throttled
if dist < self.distThreshold:
self.waypointIndex += 1
print(dist, curr_x, curr_y, self.waypointIndex)
vel_pub.publish(self.des_vel)
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()