Saturday, May 2, 2026
HomeRoboticsExploring ROS2 utilizing wheeled Robotic – #3 – Transferring the robotic

Exploring ROS2 utilizing wheeled Robotic – #3 – Transferring the robotic

[ad_1]

By Marco Arruda

On this submit you’ll learn to publish to a ROS2 subject utilizing ROS2 C++. As much as the top of the video, we’re shifting the robotic Dolly robotic, simulated utilizing Gazebo 11.

You’ll be taught:

  • Tips on how to create a node with ROS2 and C++
  • Tips on how to public to a subject with ROS2 and C++

1 – Setup setting – Launch simulation

Earlier than the rest, be sure you have the rosject from the earlier submit, you’ll be able to copy it from right here.

Launch the simulation in a single webshell and in a distinct tab, checkout the subjects we have now accessible. It’s essential to get one thing much like the picture beneath:

2 – Create a subject writer

Create a brand new file to container the writer node: moving_robot.cpp and paste the next content material:

#embody <chrono>
#embody <useful>
#embody <reminiscence>

#embody "rclcpp/rclcpp.hpp"
#embody "geometry_msgs/msg/twist.hpp"

utilizing namespace std::chrono_literals;

/* This instance creates a subclass of Node and makes use of std::bind() to register a
 * member perform as a callback from the timer. */

class MovingRobot : public rclcpp::Node {
public:
  MovingRobot() : Node("moving_robot"), count_(0) {
    publisher_ =
        this->create_publisher("/dolly/cmd_vel", 10);
    timer_ = this->create_wall_timer(
        500ms, std::bind(&MovingRobot::timer_callback, this));
  }

personal:
  void timer_callback() {
    auto message = geometry_msgs::msg::Twist();
    message.linear.x = 0.5;
    message.angular.z = 0.3;
    RCLCPP_INFO(this->get_logger(), "Publishing: '%f.2' and %f.2",
                message.linear.x, message.angular.z);
    publisher_->publish(message);
  }
  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Writer::SharedPtr publisher_;
  size_t count_;
};

int foremost(int argc, char *argv[]) {
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared());
  rclcpp::shutdown();
  return 0;
}QoS (High quality of Service)

Much like the subscriber it’s created a category that inherits Node. A publisher_ is setup and in addition a callback, though this time is just not a callback that receives messages, however a timer_callback known as in a frequency outlined by the timer_ variable. This callback is used to publish messages to the robotic.

The create_publisher technique wants two arguments:

  • subject identify
  • QoS (High quality of Service) – That is the coverage of knowledge saved within the queue. You may make use of various middlewares and even use some supplied by default. We’re simply establishing a queue of 10. By default, it retains the final 10 messages despatched to the subject.

The message printed have to be created utilizing the category imported:

message = geometry_msgs::msg::Twist();

We make sure the callback strategies on the subscribers facet will all the time acknowledge the message. That is the best way it needs to be printed through the use of the writer technique publish.

3 – Compile and run the node

So as to compile we have to alter some issues within the ~/ros2_ws/src/my_package/CMakeLists.txt. So add the next to the file:

  • Add the geometry_msgs dependency
  • Append the executable moving_robot
  • Add set up instruction for moving_robot
find_package(geometry_msgs REQUIRED)
...
# shifting robotic
add_executable(moving_robot src/moving_robot.cpp)
ament_target_dependencies(moving_robot rclcpp geometry_msgs)
...
set up(TARGETS
  moving_robot
  reading_laser
  DESTINATION lib/${PROJECT_NAME}/
)

We will run the node like beneath:

supply ~/ros2_ws/set up/setup.bash
ros2 run my_package

Associated programs & additional hyperlinks:

The submit Exploring ROS2 utilizing wheeled Robotic – #3 – Transferring the Robotic
appeared first on The Assemble.


The Assemble Weblog

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments