Publishers and Subscriber Activity
Introduction
Basics
Create a node that accomplishes the following:
- Publishes on
turtle1/cmd_vel
and causes the turtle to move forward in the x direction - Subscribes to
turtle1/pose
and, when the turtle's x coordinate is greater less than 2 or greater than 7 reverses the turtle direction
Advanced
- Make the x span where the turtle turns around customizable via parameters called
xmin
andxmax
. - Run two copies of your node and the turtlesim in two separate namespaces with two separate
xmin
andxmax
values.
Sample Code
import rclpy from rclpy.node import Node from geometry_msgs.msg import Twist, Vector3 class TurtleAct(Node): """ Move the turtle back and forth """ def __init__(self): super().__init__("turtleact") self.timer = self.create_timer(1, self.timer_callback) # Notice that we are using a relative name "cmd_vel" here self.pub = self.create_publisher(Twist, "cmd_vel", 10) def timer_callback(self): """ This timer callback is called at a fixed frequency """ self.get_logger().info("Timer!") self.pub.publish(Twist(linear=Vector3(x=1.0, y=1.0, z=1.0), angular=Vector3(x=2.0, y=2.0, z=2.0))) def turtleact_node(args=None): """ The node's entry point """ rclpy.init(args=args) node = TurtleAct() rclpy.spin(node) rclpy.shutdown()
Testing
- Run the
turtlesim_node
and your node and observe whether the expected behavior occurs
Reference
- ROS API (and references therein).