sim2rviz.py
Go to the documentation of this file.
1 import rospy
2 import pyproj
3 import utm
4 from geometry_msgs.msg import PoseStamped, QuaternionStamped, Quaternion, Pose, Twist, Vector3
5 from sensor_msgs.msg import NavSatFix
6 from nav_msgs.msg import Odometry
7 
8 # Initialize node
9 rospy.init_node('gps_attitude_to_pose')
10 
11 # Set up publishers and subscribers
12 pose_pub = rospy.Publisher('/drone_pose', PoseStamped, queue_size=1)
13 odom_pub = rospy.Publisher('/drone_odom', Odometry, queue_size=1)
14 attitude = Quaternion()
15 position = NavSatFix()
16 
17 # Variables to hold the origin of the local frame (first received GPS data)
18 origin_lat = None
19 origin_lon = None
20 origin_east = None
21 origin_north = None
22 origin_zone = None
23 origin_letter = None
24 
26  global attitude
27  attitude = msg.quaternion
28 
29 def gps_callback(msg):
30  global position, origin_lat, origin_lon, origin_east, origin_north, origin_zone, origin_letter
31  position = msg
32 
33  if origin_lat is None and origin_lon is None:
34  # Store the first received GPS data as the origin of our local frame
35  origin_lat = position.latitude
36  origin_lon = position.longitude
37  origin_east, origin_north, origin_zone, origin_letter = utm.from_latlon(origin_lat, origin_lon)
38 
39  else:
40  pose = PoseStamped()
41  pose.header = msg.header
42  pose.header.frame_id = "world"
43 
44  # Convert from LLA to UTM
45  east, north, _, _ = utm.from_latlon(position.latitude, position.longitude, force_zone_number=origin_zone, force_zone_letter=origin_letter)
46 
47  # Subtract the first position in UTM format
48  pose.pose.position.x = east - origin_east
49  pose.pose.position.y = north - origin_north
50  pose.pose.position.z = position.altitude
51 
52  # Orientation
53  pose.pose.orientation = attitude
54 
55  # Publishing the PoseStamped message
56  pose_pub.publish(pose)
57 
58  # Creating and publishing the Odometry message
59  odom_msg = Odometry()
60  odom_msg.header = pose.header
61  odom_msg.child_frame_id = "base_link"
62  odom_msg.pose.pose = pose.pose
63  odom_msg.twist.twist = Twist(Vector3(0, 0, 0), Vector3(0, 0, 0)) # Assuming velocities are zero
64  odom_pub.publish(odom_msg)
65 
66 attitude_sub = rospy.Subscriber('/sim/attitude', QuaternionStamped, attitude_callback)
67 gps_sub = rospy.Subscriber('/sim/gps_position', NavSatFix, gps_callback)
68 
69 rate = rospy.Rate(10) # 10 Hz
70 
71 while not rospy.is_shutdown():
72  rate.sleep()
sim2rviz.attitude_callback
def attitude_callback(msg)
Definition: sim2rviz.py:25
sim2rviz.gps_callback
def gps_callback(msg)
Definition: sim2rviz.py:29


inno_sim_interface
Author(s):
autogenerated on Wed Jan 3 2024 03:28:25