|
| 1 | +// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <chrono> |
| 16 | +#include <cstdio> |
| 17 | +#include <complex_msgs/msg/nested_msg_test.hpp> |
| 18 | +#include "rclcpp/rclcpp.hpp" |
| 19 | +using namespace std::chrono_literals; |
| 20 | + |
| 21 | +class complex_msg_publisher_cpp_node : public rclcpp::Node |
| 22 | +{ |
| 23 | +public: |
| 24 | + complex_msg_publisher_cpp_node() : Node("complex_msg_publisher_cpp") |
| 25 | + { |
| 26 | + publisher_ = this->create_publisher<complex_msgs::msg::NestedMsgTest>("complex_msgs_msg_NestedMsgTest"); |
| 27 | + timer_ = this->create_wall_timer( |
| 28 | + 500ms, std::bind(&complex_msg_publisher_cpp_node::timer_callback, this)); |
| 29 | + } |
| 30 | + |
| 31 | +private: |
| 32 | + void timer_callback() |
| 33 | + { |
| 34 | + msg.data1 = (bool)(num & 0x01); |
| 35 | + msg.data2 = (uint8_t)num; |
| 36 | + msg.data3 = (signed char)num; |
| 37 | + msg.data4 = (float)num; |
| 38 | + msg.data5 = (double)num; |
| 39 | + msg.data6 = (int8_t)num; |
| 40 | + msg.data7 = (uint8_t)num; |
| 41 | + msg.data8 = (int16_t)num; |
| 42 | + msg.data9 = (uint16_t)num; |
| 43 | + msg.data10 = (int32_t)num; |
| 44 | + msg.data11 = (uint32_t)num; |
| 45 | + msg.data12 = (int64_t)num; |
| 46 | + msg.data13 = (uint64_t)num; |
| 47 | + msg.data14.data1 = "Msg A - " + std::to_string(num); |
| 48 | + msg.data14.data2 = "Msg B - " + std::to_string(num); |
| 49 | + msg.data14.data3 = "Msg C - " + std::to_string(num); |
| 50 | + msg.data14.data4 = "Msg D - " + std::to_string(num); |
| 51 | + num++; |
| 52 | + |
| 53 | + publisher_->publish(msg); |
| 54 | + |
| 55 | + std::cout << "I send:" << std::endl; |
| 56 | + std::cout << "\tBool: " << std::to_string(msg.data1) << std::endl; |
| 57 | + std::cout << "\tuint8_t: " << std::to_string(msg.data2) << std::endl; |
| 58 | + std::cout << "\tsigned char: " << std::to_string(msg.data3) << std::endl; |
| 59 | + std::cout << "\tfloat: " << std::to_string(msg.data4) << std::endl; |
| 60 | + std::cout << "\tdouble: " << std::to_string(msg.data5) << std::endl; |
| 61 | + std::cout << "\tint8_t: " << std::to_string(msg.data6) << std::endl; |
| 62 | + std::cout << "\tuint8_t: " << std::to_string(msg.data7) << std::endl; |
| 63 | + std::cout << "\tint16_t: " << std::to_string(msg.data8) << std::endl; |
| 64 | + std::cout << "\tuint16_t: " << std::to_string(msg.data9) << std::endl; |
| 65 | + std::cout << "\tint32_t: " << std::to_string(msg.data10) << std::endl; |
| 66 | + std::cout << "\tuint32_t: " << std::to_string(msg.data11) << std::endl; |
| 67 | + std::cout << "\tint64_t: " << std::to_string(msg.data12) << std::endl; |
| 68 | + std::cout << "\tuint64_t: " << std::to_string(msg.data13) << std::endl; |
| 69 | + |
| 70 | + std::cout << "\tstring 1: " << msg.data14.data1 << std::endl; |
| 71 | + std::cout << "\tstring 2: " << msg.data14.data2 << std::endl; |
| 72 | + std::cout << "\tstring 3: " << msg.data14.data3 << std::endl; |
| 73 | + std::cout << "\tstring 4: " << msg.data14.data4 << std::endl; |
| 74 | + std::cout << "\n" << std::endl; |
| 75 | + } |
| 76 | + |
| 77 | + rclcpp::Publisher<complex_msgs::msg::NestedMsgTest>::SharedPtr publisher_; |
| 78 | + rclcpp::TimerBase::SharedPtr timer_; |
| 79 | + complex_msgs::msg::NestedMsgTest msg; |
| 80 | + size_t num = 0; |
| 81 | +}; |
| 82 | + |
| 83 | + |
| 84 | +int main(int argc, char * argv[]) |
| 85 | +{ |
| 86 | + rclcpp::init(argc, argv); |
| 87 | + rclcpp::spin(std::make_shared<complex_msg_publisher_cpp_node>()); |
| 88 | + rclcpp::shutdown(); |
| 89 | + return 0; |
| 90 | +} |
0 commit comments