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:
Sat Oct 09 05:40:59 2021 +0000
Revision:
0:8630970819e4
Child:
1:c01b621ed377
mros2 on Mbed sample

Who changed what in which revision?

UserRevisionLine numberNew contents of line
smoritaemb 0:8630970819e4 1 /* mros2 on Mbed 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 "mbed-trace/mbed_trace.h"
smoritaemb 0:8630970819e4 19 #include "mros2.h"
smoritaemb 0:8630970819e4 20 #include "std_msgs/msg/string.hpp"
smoritaemb 0:8630970819e4 21 #include "EthernetInterface.h"
smoritaemb 0:8630970819e4 22
smoritaemb 0:8630970819e4 23 mros2::Subscriber sub;
smoritaemb 0:8630970819e4 24 mros2::Publisher pub;
smoritaemb 0:8630970819e4 25
smoritaemb 0:8630970819e4 26 void userCallback(std_msgs::msg::String *msg)
smoritaemb 0:8630970819e4 27 {
smoritaemb 0:8630970819e4 28 printf("subscribed msg: '%s'\r\n", msg->data.c_str());
smoritaemb 0:8630970819e4 29 printf("publishing msg: '%s'\r\n", msg->data.c_str());
smoritaemb 0:8630970819e4 30 pub.publish(*msg);
smoritaemb 0:8630970819e4 31 }
smoritaemb 0:8630970819e4 32
smoritaemb 0:8630970819e4 33 DigitalOut led1(LED1);
smoritaemb 0:8630970819e4 34 #define IP_ADDRESS ("192.168.11.2") /* IP address, if you want to change*/
smoritaemb 0:8630970819e4 35 #define SUBNET_MASK ("255.255.255.0") /* Subnet mask */
smoritaemb 0:8630970819e4 36 #define DEFAULT_GATEWAY ("192.168.11.1") /* Default gateway */
smoritaemb 0:8630970819e4 37 int main() {
smoritaemb 0:8630970819e4 38 EthernetInterface network;
smoritaemb 0:8630970819e4 39 network.set_dhcp(false);
smoritaemb 0:8630970819e4 40 network.set_network(IP_ADDRESS, SUBNET_MASK, DEFAULT_GATEWAY);
smoritaemb 0:8630970819e4 41 nsapi_size_or_error_t result = network.connect();
smoritaemb 0:8630970819e4 42
smoritaemb 0:8630970819e4 43 printf("mbed mros2 start!\r\n");
smoritaemb 0:8630970819e4 44 mros2::init(0, NULL);
smoritaemb 0:8630970819e4 45 printf("mROS 2 initialization is completed\r\n");
smoritaemb 0:8630970819e4 46
smoritaemb 0:8630970819e4 47 mros2::Node node = mros2::Node::create_node("mros2_node");
smoritaemb 0:8630970819e4 48 pub = node.create_publisher<std_msgs::msg::String>("to_linux", 10);
smoritaemb 0:8630970819e4 49 sub = node.create_subscription("to_stm", 10, userCallback);
smoritaemb 0:8630970819e4 50 std_msgs::msg::String msg;
smoritaemb 0:8630970819e4 51
smoritaemb 0:8630970819e4 52 printf("ready to pub/sub message\r\n");
smoritaemb 0:8630970819e4 53 mros2::spin();
smoritaemb 0:8630970819e4 54
smoritaemb 0:8630970819e4 55 return 0;
smoritaemb 0:8630970819e4 56 }