mros2 on MBed example reported at ROS Japan UG #44 LT. [Event] https://rosjp.connpass.com/event/222141/ [Slide] https://www.slideshare.net/smorita1/mros2-on-mbed

Dependencies:   mbed-mros2

Please refer to the getting started.

https://github.com/mROS-base/mros2-mbed

Committer:
smoritaemb
Date:
Sun Mar 13 21:41:01 2022 +0900
Branch:
test_v0.3.2
Revision:
4:2030aed3c888
Parent:
1:c01b621ed377
Copied app.c of the echoreply_string as main.cpp as application code.
And added templates.hpp needed by the new mros2 framework.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
smoritaemb 4:2030aed3c888 1 /* mros2 example
smoritaemb 0:8630970819e4 2 * Copyright (c) 2021 smorita_emb
smoritaemb 0:8630970819e4 3 *
smoritaemb 0:8630970819e4 4 * Licensed under the Apache License, Version 2.0 (the "License");
smoritaemb 0:8630970819e4 5 * you may not use this file except in compliance with the License.
smoritaemb 0:8630970819e4 6 * You may obtain a copy of the License at
smoritaemb 0:8630970819e4 7 *
smoritaemb 0:8630970819e4 8 * http://www.apache.org/licenses/LICENSE-2.0
smoritaemb 0:8630970819e4 9 *
smoritaemb 0:8630970819e4 10 * Unless required by applicable law or agreed to in writing, software
smoritaemb 0:8630970819e4 11 * distributed under the License is distributed on an "AS IS" BASIS,
smoritaemb 0:8630970819e4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
smoritaemb 0:8630970819e4 13 * See the License for the specific language governing permissions and
smoritaemb 0:8630970819e4 14 * limitations under the License.
smoritaemb 0:8630970819e4 15 */
smoritaemb 0:8630970819e4 16
smoritaemb 0:8630970819e4 17 #include "mbed.h"
smoritaemb 0:8630970819e4 18 #include "mros2.h"
smoritaemb 0:8630970819e4 19 #include "std_msgs/msg/string.hpp"
smoritaemb 0:8630970819e4 20 #include "EthernetInterface.h"
smoritaemb 0:8630970819e4 21
smoritaemb 4:2030aed3c888 22 #define IP_ADDRESS ("192.168.11.2") /* IP address */
smoritaemb 4:2030aed3c888 23 #define SUBNET_MASK ("255.255.255.0") /* Subnet mask */
smoritaemb 4:2030aed3c888 24 #define DEFAULT_GATEWAY ("192.168.11.1") /* Default gateway */
smoritaemb 4:2030aed3c888 25
smoritaemb 4:2030aed3c888 26
smoritaemb 0:8630970819e4 27 mros2::Subscriber sub;
smoritaemb 0:8630970819e4 28 mros2::Publisher pub;
smoritaemb 0:8630970819e4 29
smoritaemb 0:8630970819e4 30 void userCallback(std_msgs::msg::String *msg)
smoritaemb 0:8630970819e4 31 {
smoritaemb 0:8630970819e4 32 printf("subscribed msg: '%s'\r\n", msg->data.c_str());
smoritaemb 0:8630970819e4 33 printf("publishing msg: '%s'\r\n", msg->data.c_str());
smoritaemb 0:8630970819e4 34 pub.publish(*msg);
smoritaemb 0:8630970819e4 35 }
smoritaemb 0:8630970819e4 36
smoritaemb 0:8630970819e4 37 int main() {
smoritaemb 4:2030aed3c888 38 EthernetInterface network;
smoritaemb 4:2030aed3c888 39 network.set_dhcp(false);
smoritaemb 4:2030aed3c888 40 network.set_network(IP_ADDRESS, SUBNET_MASK, DEFAULT_GATEWAY);
smoritaemb 4:2030aed3c888 41 nsapi_size_or_error_t result = network.connect();
smoritaemb 0:8630970819e4 42
smoritaemb 4:2030aed3c888 43 printf("mbed mros2 start!\r\n");
smoritaemb 4:2030aed3c888 44 printf("app name: echoreply_string\r\n");
smoritaemb 4:2030aed3c888 45 mros2::init(0, NULL);
smoritaemb 4:2030aed3c888 46 MROS2_DEBUG("mROS 2 initialization is completed\r\n");
smoritaemb 0:8630970819e4 47
smoritaemb 4:2030aed3c888 48 mros2::Node node = mros2::Node::create_node("mros2_node");
smoritaemb 4:2030aed3c888 49 pub = node.create_publisher<std_msgs::msg::String>("to_linux", 10);
smoritaemb 4:2030aed3c888 50 sub = node.create_subscription<std_msgs::msg::String>("to_stm", 10, userCallback);
smoritaemb 0:8630970819e4 51
smoritaemb 4:2030aed3c888 52 MROS2_INFO("ready to pub/sub message\r\n");
smoritaemb 0:8630970819e4 53
smoritaemb 4:2030aed3c888 54 mros2::spin();
smoritaemb 4:2030aed3c888 55 return 0;
smoritaemb 4:2030aed3c888 56 }