Ask Your Question
0

How can I publish Position in rviz ?

asked 2021-05-14 12:59:14 -0500

Latout92 gravatar image

Hello, I am new in ROS and I have a little question.

I want to publish a starting position and goal position on rviz but I didn't succeed. I don't know where is the error in my code and/or if it possible or not because I have seen no information about it on the internet.

This in ROS2 Foxy and I use the turtlebot3.

Thank you for your help, I let you my code here :

import rclpy
from rclpy.node import Node
from std_msgs.msg import String
from geometry_msgs.msg import PoseWithCovarianceStamped, PoseStamped


class MinimalPublisher(Node):

def __init__(self):
    super().__init__('minimal_publisher')
    self.publisher_ = self.create_publisher(PoseWithCovarianceStamped , '/initialpose', 10)
    self.pose_callback()
    self.publisher_ = self.create_publisher(PoseStamped , '/goal_pose', 10)
    self.goal_callback()

def pose_callback(self):
    self.msg = PoseWithCovarianceStamped()
    self.msg.header.frame_id = "/base_link"
    self.msg.pose.pose.position.x = 0.7
    self.msg.pose.pose.position.y = 0.5
    self.msg.pose.pose.position.z = 0.1
    self.publisher_.publish(self.msg)

def goal_callback(self):
    self.goal = PoseStamped()
    # self.msg.header.frame_id = "/base_link"
    self.goal.pose.position.x = 0.7
    self.goal.pose.position.y = 0.5
    self.goal.pose.position.z = 0.1
    self.publisher_.publish(self.goal)
    print("done")


def main(args=None):
    rclpy.init(args=args)
    minimal_publisher = MinimalPublisher()
    rclpy.spin(minimal_publisher)
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
     main()
edit retag flag offensive close merge delete

Comments

"but I didn't succeed" — please provide more details. What all have you tried and what are your findings?

abhishek47kashyap gravatar image abhishek47kashyap  ( 2021-05-15 03:33:57 -0500 )edit

First I launched rviz and i also do in a shell "ros2 topic echo /initialpose". By doing that i can see on the rviz if the position is well published and with echo I can also if something is published on the topic.

The result is : - I can see that I publish well on the topic /initialpose - Nothing happened in rviz interface

Furthermore for setting the goal position, when I echo goal pose I hear nothing so maybe my syntaxis is wrong ?

Latout92 gravatar image Latout92  ( 2021-05-15 04:25:02 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2021-05-15 04:51:21 -0500

abhishek47kashyap gravatar image

The problem has nothing to do with rviz but rather these lines:

self.publisher_ = self.create_publisher(PoseWithCovarianceStamped , '/initialpose', 10)
self.pose_callback()
self.publisher_ = self.create_publisher(PoseStamped , '/goal_pose', 10)
self.goal_callback()

Please refer to ROS2 Writing a simple publisher and subscriber (Python) to better understand how to create publishers and subscribers.

In your code, the same publisher object self.publisher_ is publishing on two topics: /initialpose and /goal_pose.

Also, a callback function has to be tied to a subscriber. For every message that arrives on the topic, that callback function gets executed. In your code, the methods pose_callback() and goal_callback() are not really callbacks but just methods that get executed only once: minimal_publisher = MinimalPublisher().

edit flag offensive delete link more

Comments

1

Okay I see so what do you suggest me to publish the initial pose and goal pose in rviz ? Continue with callback functions or not ?

Furthermore, in the tutorial you sent me, in the section 2.1. I don't understand this line in the 4th code : self.timer = self.create_timer(timer_period, self.timer_callback). self.create_timer() is a specific function syntaxis to create a timer ? So if I want to publish position I might do this ? :

def __init__(self):
       super().__init__('minimal_publisher')
       self.publisher_ = self.create_publisher(pose, '/initialpose', 10)
       timer_period =  0.5 # seconds
       self.pose = self.create_pose(timer_period, self.pose_callback)

Right ? Finally timer_period is to define the rate of publishing ? If it is, How can I neutralise it to publish only one time ?

Sorry for all tose questions but I am not really good at ROS cause I just started some days ago. Thank you for your help.

Latout92 gravatar image Latout92  ( 2021-05-15 12:23:25 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-05-14 12:59:14 -0500

Seen: 43 times

Last updated: May 15