|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +#Jesus Tordesillas Torres, December 2019 |
| 4 | + |
| 5 | +#This files plots in gazebo with the position and orientation of the drone according to the desired position and acceleration specified in the goal topic |
| 6 | + |
| 7 | +import roslib |
| 8 | +import rospy |
| 9 | +import math |
| 10 | +from snapstack_msgs.msg import Goal, State |
| 11 | +from geometry_msgs.msg import Pose |
| 12 | +from gazebo_msgs.msg import ModelState |
| 13 | +import numpy as np |
| 14 | +from numpy import linalg as LA |
| 15 | + |
| 16 | +from tf.transformations import quaternion_from_euler, euler_from_quaternion, quaternion_about_axis, quaternion_multiply |
| 17 | + |
| 18 | +from visualization_msgs.msg import Marker |
| 19 | + |
| 20 | +import tf |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +class FakeSim: |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + self.state=State() |
| 28 | + |
| 29 | + self.state.pos.x = rospy.get_param('~x', 0.0); |
| 30 | + self.state.pos.y = rospy.get_param('~y', 0.0); |
| 31 | + self.state.pos.z = rospy.get_param('~z', 0.0); |
| 32 | + yaw = rospy.get_param('~yaw', 0.0); |
| 33 | + |
| 34 | + pitch=0.0; |
| 35 | + roll=0.0; |
| 36 | + quat = quaternion_from_euler(yaw, pitch, roll, 'rzyx') |
| 37 | + |
| 38 | + self.state.quat.x = quat[0] |
| 39 | + self.state.quat.y = quat[1] |
| 40 | + self.state.quat.z = quat[2] |
| 41 | + self.state.quat.w = quat[3] |
| 42 | + |
| 43 | + self.pubGazeboState = rospy.Publisher('/gazebo/set_model_state', ModelState, queue_size=1) |
| 44 | + self.pubMarkerDrone = rospy.Publisher('marker', Marker, queue_size=1, latch=True) |
| 45 | + self.pubState = rospy.Publisher('state', State, queue_size=1, latch=True) |
| 46 | + self.timer = rospy.Timer(rospy.Duration(0.01), self.pubTF) |
| 47 | + name = rospy.get_namespace() |
| 48 | + self.name = name[1:-1] |
| 49 | + |
| 50 | + rospy.sleep(1.0) |
| 51 | + |
| 52 | + self.state.header.frame_id="world" |
| 53 | + self.pubState.publish(self.state) |
| 54 | + |
| 55 | + pose=Pose() |
| 56 | + pose.position.x=self.state.pos.x; |
| 57 | + pose.position.y=self.state.pos.y; |
| 58 | + pose.position.z=self.state.pos.z; |
| 59 | + pose.orientation.x = quat[0] |
| 60 | + pose.orientation.y = quat[1] |
| 61 | + pose.orientation.z = quat[2] |
| 62 | + pose.orientation.w = quat[3] |
| 63 | + |
| 64 | + self.pubMarkerDrone.publish(self.getDroneMarker(pose)); |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + def goalCB(self, data): |
| 70 | + |
| 71 | + |
| 72 | + state = State() |
| 73 | + gazebo_state = ModelState() |
| 74 | + gazebo_state.model_name = self.name |
| 75 | + axis_z=[0,0,1] |
| 76 | + |
| 77 | + accel=[data.a.x, data.a.y, data.a.z + 9.81]; |
| 78 | + |
| 79 | + gazebo_state.pose.position.x = data.p.x |
| 80 | + gazebo_state.pose.position.y = data.p.y |
| 81 | + gazebo_state.pose.position.z = data.p.z |
| 82 | + |
| 83 | + |
| 84 | + drone_quaternion_with_yaw=[]; |
| 85 | + |
| 86 | + if(LA.norm(accel)>0.000001 and LA.norm(np.cross(accel, axis_z))>0.0000001): |
| 87 | + norm_accel=LA.norm(accel) |
| 88 | + accel=accel/norm_accel |
| 89 | + axis=np.cross(accel, axis_z); |
| 90 | + |
| 91 | + dot=np.dot(accel,axis_z) |
| 92 | + angle=math.acos(dot) |
| 93 | + drone_quaternion = quaternion_about_axis(-angle, axis)# Quaternion(axis=axis, angle=-angle) |
| 94 | + |
| 95 | + #Use the yaw from goal |
| 96 | + drone_quaternion_with_yaw=quaternion_multiply(drone_quaternion,quaternion_from_euler(data.yaw, 0.0, 0.0, 'rzyx')) |
| 97 | + |
| 98 | + else: #Take only the yaw angle |
| 99 | + drone_quaternion_with_yaw = quaternion_from_euler(data.yaw, 0.0, 0.0, 'rzyx') |
| 100 | + |
| 101 | + gazebo_state.pose.orientation.x = drone_quaternion_with_yaw[0] |
| 102 | + gazebo_state.pose.orientation.y = drone_quaternion_with_yaw[1] |
| 103 | + gazebo_state.pose.orientation.z = drone_quaternion_with_yaw[2] |
| 104 | + gazebo_state.pose.orientation.w = drone_quaternion_with_yaw[3] |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + #self.gazebo_state.twist = data.twist |
| 109 | + |
| 110 | + ## HACK TO NOT USE GAZEBO |
| 111 | + gazebo_state.reference_frame = "world" |
| 112 | + self.pubGazeboState.publish(gazebo_state) |
| 113 | + ## END OF HACK TO NOT USE GAZEBO |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + self.state.header.frame_id="world" |
| 118 | + self.state.pos=data.p |
| 119 | + self.state.vel=data.v |
| 120 | + self.state.quat=gazebo_state.pose.orientation |
| 121 | + self.pubState.publish(self.state) |
| 122 | + # print("State after:") |
| 123 | + # print(self.state.quat) |
| 124 | + |
| 125 | + self.pubMarkerDrone.publish(self.getDroneMarker(gazebo_state.pose)); |
| 126 | + |
| 127 | + def pubTF(self, timer): |
| 128 | + br = tf.TransformBroadcaster() |
| 129 | + br.sendTransform((self.state.pos.x, self.state.pos.y, self.state.pos.z), |
| 130 | + (self.state.quat.x, self.state.quat.y, self.state.quat.z, self.state.quat.w), |
| 131 | + rospy.Time.now(), |
| 132 | + self.name, |
| 133 | + "vicon") |
| 134 | + |
| 135 | + def getDroneMarker(self, pose): |
| 136 | + marker=Marker(); |
| 137 | + marker.id=1; |
| 138 | + marker.ns="mesh_"+self.name; |
| 139 | + marker.header.frame_id="world" |
| 140 | + marker.type=marker.MESH_RESOURCE; |
| 141 | + marker.action=marker.ADD; |
| 142 | + |
| 143 | + marker.pose=pose |
| 144 | + marker.lifetime = rospy.Duration.from_sec(0.0); |
| 145 | + marker.mesh_use_embedded_materials=True |
| 146 | + marker.mesh_resource="package://acl_sim/meshes/quadrotor/quadrotor.dae" |
| 147 | + marker.scale.x=1.0; |
| 148 | + marker.scale.y=1.0; |
| 149 | + marker.scale.z=1.0; |
| 150 | + return marker |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +def startNode(): |
| 157 | + c = FakeSim() |
| 158 | + rospy.Subscriber("goal", Goal, c.goalCB) |
| 159 | + |
| 160 | + rospy.spin() |
| 161 | + |
| 162 | +if __name__ == '__main__': |
| 163 | + |
| 164 | + ns = rospy.get_namespace() |
| 165 | + try: |
| 166 | + rospy.init_node('relay') |
| 167 | + if str(ns) == '/': |
| 168 | + rospy.logfatal("Need to specify namespace as vehicle name.") |
| 169 | + rospy.logfatal("This is tyipcally accomplished in a launch file.") |
| 170 | + rospy.logfatal("Command line: ROS_NAMESPACE=mQ01 $ rosrun quad_control joy.py") |
| 171 | + else: |
| 172 | + print "Starting perfect tracker node for: " + ns |
| 173 | + startNode() |
| 174 | + except rospy.ROSInterruptException: |
| 175 | + pass |
0 commit comments