Hi there
I’ve been struggling for a while to recieve data from the /odom topic. When I subscribe I get a ton of data, and what I really want is a specific data. How do I retrieve this data?
Also how do I find the files with Odometry so I can learn how more about the topic?
Best, Rasmus MT
Hi @r.malmvig ,
Welcome to the Community!
Do a rostopic info /odom and check if the messages are of type nav_msgs/Odometry.
If they are, then do rosmsg show nav_msgs/Odometry.
If they look like following:
std_msgs/Header header
uint32 seq
time stamp
string frame_id
string child_frame_id
geometry_msgs/PoseWithCovariance pose
geometry_msgs/Pose pose
geometry_msgs/Point position
float64 x
float64 y
float64 z
geometry_msgs/Quaternion orientation
float64 x
float64 y
float64 z
float64 w
float64[36] covariance
geometry_msgs/TwistWithCovariance twist
geometry_msgs/Twist twist
geometry_msgs/Vector3 linear
float64 x
float64 y
float64 z
geometry_msgs/Vector3 angular
float64 x
float64 y
float64 z
float64[36] covariance
If you store /odom message as odom_msg.
You can retrieve poses as odom_msg.pose.pose.position.x, quaternion as odom_msg.pose.pose.orientation.x, instantaneous linear velocity as odom_msg.twist.twist.linear.x and instantaneous angular velocity as odom_msg.twist.twist.angular.x.
If you are using python it is odom_msg.pose.pose.position.x
If you are using c++ it is odom_msg->pose.pose.position.x
Hope I helped. Let me know if I did.
Regards,
Girish
2 Likes
Hi Girish,
Thanks a lot for your reply. Somehow I don’t manage to make it happen. This is my code. Where do I need to implement?
import rclpy
# import the ROS2 python libraries
from rclpy.node import Node
# import the Twist module from geometry_msgs interface
from geometry_msgs.msg import Twist
# import the LaserScan module from sensor_msgs interface
from sensor_msgs.msg import LaserScan
from nav_msgs.msg import Odometry
# import Quality of Service library, to set the correct profile and reliability in order to read sensor data.
from rclpy.qos import ReliabilityPolicy, QoSProfile
class TopicsQuizNode(Node):
def __init__(self):
# Initialize the publisher
super().__init__('topics_quiz_node')
self.publisher_ = self.create_publisher(Twist, 'cmd_vel', 10)
self.subscriber1 = self.create_subscription(
LaserScan,
'/scan',
self.listener_callback1,
QoSProfile(depth=10, reliability=ReliabilityPolicy.BEST_EFFORT))
self.subscriber2 = self.create_subscription(
Odometry,
'/odom',
self.listener_callback2,
QoSProfile(depth=10, reliability=ReliabilityPolicy.BEST_EFFORT))
self.laser_forward = 0
self.odom_data = 0
timer_period = 0.5
self.cmd = Twist()
self.timer = self.create_timer(timer_period, self.timer_callback)
def listener_callback1(self, msg1):
self.laser_forward = msg1.ranges[359]
def listener_callback2(self, msg2):
self.odom_data = msg2
def timer_callback(self):
self.cmd.linear.x = 0.5
self.get_logger().info('I receive: "%s"' %
str(self.odom_data))
# Publish the message to the topic
self.publisher_.publish(self.cmd)
# Display the message on the console
self.get_logger().info('Publishing: "%s"' % self.cmd)
def main(args=None):
# initialize the ROS communication
rclpy.init(args=args)
# declare the node constructor
topics_quiz_node = TopicsQuizNode()
# pause the program execution, waits for a request to kill the node (ctrl+c)
rclpy.spin(topics_quiz_node)
# Explicity destroy the node
topics_quiz_node.destroy_node()
# shutdown the ROS communication
rclpy.shutdown()
if __name__ == '__main__':
main()
@r.malmvig ,
Sure…
I see from your code that the /odom callback is using listener_callback2 (also use a distinct name please! make it odom_callback or similar name).
Anyways, inside your listener_callback2(self, msg2) replace code like this:
def listener_callback2(self, msg2):
position = msg2.pose.pose.position
orientation = msg2.pose.pose.orientation
(posx, posy, posz) = (position.x, position.y, position.z)
(qx, qy, qz, qw) = (orientation.x, orientation.y, orientation.z, orientation.w)
# similarly for twist message if you need
return None
Regards,
Girish
PS: You never tried to do what I said! Please take time to explore and try out or else learning becomes difficult.
1 Like
This topic was automatically closed after 22 hours. New replies are no longer allowed.