-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavgoal3
67 lines (49 loc) · 1.88 KB
/
navgoal3
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
#!/usr/bin/env python
import math
import rospy
import tf2_ros
import tf2_geometry_msgs
from tf.transformations import euler_from_quaternion
from geometry_msgs.msg import PoseStamped
from crazyflie_driver.msg import Position
# Current goal (global state)
goal = None
def goal_callback(msg):
global goal
# RViz's "2D Nav Goal" publishes z=0, so add some altitude if needed.
if msg.pose.position.z == 0.0:
msg.pose.position.z = 0.4
rospy.loginfo('New goal set:\n%s', msg)
goal = msg
def publish_cmd(goal):
# Need to tell TF that the goal was just generated
goal.header.stamp = rospy.Time.now()
if not tf_buf.can_transform(goal.header.frame_id, 'cf1/odom', goal.header.stamp):
rospy.logwarn_throttle(5.0, 'No transform from %s to cf1/odom' % goal.header.frame_id)
return
goal_odom = tf_buf.transform(goal, 'cf1/odom')
cmd = Position()
cmd.header.stamp = rospy.Time.now()
cmd.header.frame_id = goal_odom.header.frame_id
cmd.x = goal_odom.pose.position.x
cmd.y = goal_odom.pose.position.y
cmd.z = goal_odom.pose.position.z
roll, pitch, yaw = euler_from_quaternion((goal_odom.pose.orientation.x,
goal_odom.pose.orientation.y,
goal_odom.pose.orientation.z,
goal_odom.pose.orientation.w))
cmd.yaw = math.degrees(yaw)
pub_cmd.publish(cmd)
rospy.init_node('navgoal3')
sub_goal = rospy.Subscriber('/move_base_simple/goal', PoseStamped, goal_callback)
pub_cmd = rospy.Publisher('/cf1/cmd_position', Position, queue_size=2)
tf_buf = tf2_ros.Buffer()
tf_lstn = tf2_ros.TransformListener(tf_buf)
def main():
rate = rospy.Rate(10) # Hz
while not rospy.is_shutdown():
if goal:
publish_cmd(goal)
rate.sleep()
if __name__ == '__main__':
main()