Examples
Contents
Examples#
Publish-Subscrib :#
This example show the implementation of the publish subscribe exmample (https://docs.ros.org/en/foxy/Tutorials/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html) using corail API. As you can see on the highlighted lines only some little class names and params changes are needed to switch from rclcpp to corail.
1/*
2 * Corail version 1.0, by Benoit Varillon and David Doose and Jean-Baptiste Chaudron and Charles Lesir-Cabaniols
3 * Copyright 2021 ISAE-Supaero, Université de Toulouse, France
4 *
5 * This file is part of the Corail project.
6 *
7 * Corail is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Corail is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Corail. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <iostream>
23
24#include "rclcpp/rclcpp.hpp"
25#include "std_msgs/msg/string.hpp"
26
27#include "corail_core/corail_core.hpp"
28
29using namespace std::chrono_literals;
30using namespace std::placeholders;
31
32
33class MinimalPublisher : public corail_core::RealTimeNode
34{
35 public:
36 MinimalPublisher() : RealTimeNode("minimal_publisher"), count_(0)
37 {
38 publisher_ = create_publisher<std_msgs::msg::String>("corail_example/topic",1);
39 timer_ = create_rt_timer("publisher",0,10,500ms,std::bind(&MinimalPublisher::timer_callback,this));
40 }
41 private:
42 void timer_callback()
43 {
44 auto msg = std_msgs::msg::String();
45 msg.data = "Hello, world! " + std::to_string(count_++);
46 RCLCPP_INFO(get_logger(),"Publishing: %s", msg.data.c_str());
47 publisher_->publish(msg);
48 }
49 corail_core::PeriodicTask::SharedPtr timer_;
50 rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
51 size_t count_;
52
53};
54
55class MinimalSubscriber : public corail_core::RealTimeNode
56{
57 public:
58 MinimalSubscriber() : RealTimeNode("minimal_subscriber")
59 {
60 sub = create_rt_subscription<std_msgs::msg::String>("subscription", 0, 20, 100ms, 500ms, "corail_example/topic", 1, std::bind(&MinimalSubscriber::topic_callback,this,_1));
61 }
62 private:
63 void topic_callback(std_msgs::msg::String::SharedPtr msg)
64 {
65 RCLCPP_INFO(get_logger(), "I heard: %s", msg->data.c_str());
66 }
67 corail_core::SubscriptionTask<std_msgs::msg::String>::SharedPtr sub;
68
69
70};
71
72int main(int argc, char * argv[])
73{
74 rclcpp::init(argc, argv);
75 corail_core::RealTimeExecutor exec("executor");
76 exec.add_rt_node(std::make_shared<MinimalPublisher>());
77 exec.add_rt_node(std::make_shared<MinimalSubscriber>());
78 exec.spin();
79 rclcpp::shutdown();
80}
Rclcpp and Corail :#
This example show that corail realtime nodes and classic rclcpp nodes can co-exist in the same corail RealTimeExecutor.
1/*
2 * Corail version 1.0, by Benoit Varillon and David Doose and Jean-Baptiste Chaudron and Charles Lesir-Cabaniols
3 * Copyright 2021 ISAE-Supaero, Université de Toulouse, France
4 *
5 * This file is part of the Corail project.
6 *
7 * Corail is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Corail is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Corail. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <iostream>
23
24#include "rclcpp/rclcpp.hpp"
25#include "std_msgs/msg/string.hpp"
26
27#include "corail_core/corail_core.hpp"
28
29using namespace std::chrono_literals;
30using namespace std::placeholders;
31
32
33class MinimalPublisher : public corail_core::RealTimeNode
34{
35 public:
36 MinimalPublisher() : RealTimeNode("minimal_publisher"), count_(0)
37 {
38 publisher_ = create_publisher<std_msgs::msg::String>("corail_example/topic",1);
39 timer_ = create_rt_timer("publisher",0,10,500ms,std::bind(&MinimalPublisher::timer_callback,this));
40 }
41 private:
42 void timer_callback()
43 {
44 auto msg = std_msgs::msg::String();
45 msg.data = "Hello, world! " + std::to_string(count_++);
46 RCLCPP_INFO(get_logger(),"Publishing: %s", msg.data.c_str());
47 publisher_->publish(msg);
48 }
49 corail_core::PeriodicTask::SharedPtr timer_;
50 rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
51 size_t count_;
52
53};
54
55class MinimalSubscriber : public rclcpp::Node
56{
57 public:
58 MinimalSubscriber() : Node("minimal_subscriber")
59 {
60 sub = create_subscription<std_msgs::msg::String>("corail_example/topic", 1, std::bind(&MinimalSubscriber::topic_callback,this,_1));
61 }
62 private:
63 void topic_callback(std_msgs::msg::String::SharedPtr msg)
64 {
65 RCLCPP_INFO(get_logger(), "I heard: %s", msg->data.c_str());
66 }
67 rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub;
68
69
70};
71
72int main(int argc, char * argv[])
73{
74 rclcpp::init(argc, argv);
75 corail_core::RealTimeExecutor exec("executor");
76 auto pub = std::make_shared<MinimalPublisher>();
77 auto sub = std::make_shared<MinimalSubscriber>();
78 exec.add_rt_node(pub);
79 exec.add_node(sub);
80 exec.spin();
81 rclcpp::shutdown();
82}
For more examples see the corail_examples package.
Corail is free software: you can redistribute it and/or modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.