From 17b129bfc38135eb2bdc70fe99df4031987a32d0 Mon Sep 17 00:00:00 2001 From: Andy Blight Date: Mon, 18 Mar 2024 14:55:19 +0000 Subject: [PATCH 1/6] Added example to show Wi-Fi and a subscriber. Added an example that shows how to use micro-ROS with a publisher and a subscriber with Wi-Fi on an ESP32. --- examples/micro-ros_subscriber_wifi/.gitignore | 6 + .../micro-ros_subscriber_wifi/platformio.ini | 12 ++ .../micro-ros_subscriber_wifi/src/Main.cpp | 138 ++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 examples/micro-ros_subscriber_wifi/.gitignore create mode 100644 examples/micro-ros_subscriber_wifi/platformio.ini create mode 100644 examples/micro-ros_subscriber_wifi/src/Main.cpp diff --git a/examples/micro-ros_subscriber_wifi/.gitignore b/examples/micro-ros_subscriber_wifi/.gitignore new file mode 100644 index 00000000..beaaf8a9 --- /dev/null +++ b/examples/micro-ros_subscriber_wifi/.gitignore @@ -0,0 +1,6 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +.vscode/extensions.json diff --git a/examples/micro-ros_subscriber_wifi/platformio.ini b/examples/micro-ros_subscriber_wifi/platformio.ini new file mode 100644 index 00000000..dcfadb29 --- /dev/null +++ b/examples/micro-ros_subscriber_wifi/platformio.ini @@ -0,0 +1,12 @@ +; This example shows micro-ROS can be used with Wi-Fi transport on the +; ESP32-CAM board. + +[env:esp32cam] +platform = espressif32 +board = esp32cam +framework = arduino +board_microros_distro = rolling +board_microros_transport = wifi +monitor_speed = 115200 +lib_deps = + https://github.com/micro-ROS/micro_ros_platformio diff --git a/examples/micro-ros_subscriber_wifi/src/Main.cpp b/examples/micro-ros_subscriber_wifi/src/Main.cpp new file mode 100644 index 00000000..c857daa6 --- /dev/null +++ b/examples/micro-ros_subscriber_wifi/src/Main.cpp @@ -0,0 +1,138 @@ +/* This example shows micro-ROS can be used with Wi-Fi transport on the + ESP32-CAM board. + + NOTE: There are two FIXMES in the code where you need to replace the + existing code with your Wi-Fi credentials and IP address of the agent. + + To test the ROS 2 communication, you can use the following commands: + 1. Subscribe to the topic /micro_ros_platformio_node_publisher: + ros2 topic echo /micro_ros_platformio_node_publisher std_msgs/msg/Int32 + 2. Publish a message to the topic /cmd_vel: + ros2 topic pub -1 /cmd_vel geometry_msgs/msg/Twist "{linear:{x: 1.0}, angular:{z: -1.0}}" +*/ + +#include +#include +#include +#include +#include +#include +#include + +// FIXME: Replace with your Wi-Fi credentials. +#define SSID "your ssid" +#define SSID_PW "your password" + +// Publisher +rcl_publisher_t publisher; +std_msgs__msg__Int32 msg; + +// Subscriber +static const char *k_twist = "cmd_vel"; +static rcl_subscription_t subscriber_twist; +static geometry_msgs__msg__Twist twist_msg; + +// Node, executor and timer variables. +rclc_executor_t executor; +rclc_support_t support; +rcl_allocator_t allocator; +rcl_node_t node; +rcl_timer_t timer; + +#define RCCHECK(fn) \ + { \ + rcl_ret_t temp_rc = fn; \ + if ((temp_rc != RCL_RET_OK)) { \ + printf("Failed status on line %d: %d. Message: %s, Aborting.\n", \ + __LINE__, (int)temp_rc, rcl_get_error_string().str); \ + error_loop(temp_rc); \ + } \ + } + +#define RCSOFTCHECK(fn) \ + { \ + rcl_ret_t temp_rc = fn; \ + if ((temp_rc != RCL_RET_OK)) { \ + printf("Failed status on line %d: %d. Continuing.\n", __LINE__, \ + (int)temp_rc); \ + } \ + } + +// Error handle loop +void error_loop(rcl_ret_t rc) { + Serial.println("Error loop"); + while (1) { + delay(100); + } +} + +void timer_callback(rcl_timer_t *timer, int64_t last_call_time) { + RCLC_UNUSED(last_call_time); + if (timer != NULL) { + RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL)); + msg.data++; + Serial.println("Published message"); + } +} + +void twist_callback(const void *msg_in) { + const geometry_msgs__msg__Twist *msg = + (const geometry_msgs__msg__Twist *)msg_in; + Serial.println("Received twist message"); + Serial.print("Linear: "); + Serial.print(msg->linear.x); + Serial.print(", "); + Serial.print("Angular: "); + Serial.print(msg->angular.z); + Serial.println(); +} + +void setup() { + // Configure serial transport + Serial.begin(115200); + Serial.println("Started"); + + char ssid[] = SSID; + char ssid_pw[] = SSID_PW; + // FIXME: Replace with your Wi-Fi credentials. + IPAddress agent_ip(192, 168, 0, 2); + const uint16_t k_agent_port = 8888; + set_microros_wifi_transports(ssid, ssid_pw, agent_ip, k_agent_port); + delay(2000); + Serial.println("Connected"); + + allocator = rcl_get_default_allocator(); + + // create init_options + RCCHECK(rclc_support_init(&support, 0, NULL, &allocator)); + + // create node + RCCHECK( + rclc_node_init_default(&node, "micro_ros_platformio_node", "", &support)); + + // create publisher + RCCHECK(rclc_publisher_init_default( + &publisher, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), + "micro_ros_platformio_node_publisher")); + + // create timer. + const unsigned int timer_timeout = 1000; + RCCHECK(rclc_timer_init_default(&timer, &support, RCL_MS_TO_NS(timer_timeout), + timer_callback)); + + // Create subscriber. + RCCHECK(rclc_subscription_init_best_effort( + &subscriber_twist, &node, + ROSIDL_GET_MSG_TYPE_SUPPORT(geometry_msgs, msg, Twist), k_twist)); + + // create executor + RCCHECK(rclc_executor_init(&executor, &support.context, 2, &allocator)); + RCCHECK(rclc_executor_add_timer(&executor, &timer)); + RCCHECK(rclc_executor_add_subscription( + &executor, &subscriber_twist, &twist_msg, &twist_callback, ON_NEW_DATA)); +} + +void loop() { + delay(100); + RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100))); +} From c89e30029d8b171c714431c69a474e0f797bcb8b Mon Sep 17 00:00:00 2001 From: Andy Blight Date: Mon, 18 Mar 2024 15:19:49 +0000 Subject: [PATCH 2/6] Renamed main.cpp --- examples/micro-ros_subscriber_wifi/src/{Main.cpp => main.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/micro-ros_subscriber_wifi/src/{Main.cpp => main.cpp} (100%) diff --git a/examples/micro-ros_subscriber_wifi/src/Main.cpp b/examples/micro-ros_subscriber_wifi/src/main.cpp similarity index 100% rename from examples/micro-ros_subscriber_wifi/src/Main.cpp rename to examples/micro-ros_subscriber_wifi/src/main.cpp From 1f51ff68b2bf76616ec367195d45d6e8f22f0b4d Mon Sep 17 00:00:00 2001 From: Andy Blight Date: Mon, 18 Mar 2024 16:10:30 +0000 Subject: [PATCH 3/6] Added CI jobs for examples --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d9cbcc34..4672c8d1 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,3 +28,25 @@ jobs: export PATH=$PATH:~/.platformio/penv/bin cd repo/ci pio run -e ${{ matrix.platform }} + + micro_ros_platformio_examples: + runs-on: ubuntu-22.04 + container: ubuntu:22.04 + steps: + - uses: actions/checkout@v3 + with: + path: repo + - name: Install environment + uses: ./repo/.github/actions/platformio-env + - name: Build 1 + shell: bash + run: | + export PATH=$PATH:~/.platformio/penv/bin + cd repo/examples/micro-ros_publisher + pio run -e teensy41 + - name: Build 2 + shell: bash + run: | + export PATH=$PATH:~/.platformio/penv/bin + cd repo/examples/micro-ros_subscriber_wifi + pio run -e esp32cam From 62c28c1e868f0427e190edd066aeb7510e6b71c8 Mon Sep 17 00:00:00 2001 From: Andy Blight Date: Mon, 18 Mar 2024 16:24:52 +0000 Subject: [PATCH 4/6] Removed env for publisher example --- .github/workflows/ci.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4672c8d1..ba772849 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: uses: ./repo/.github/actions/platformio-env - name: Build shell: bash - run: | + run: export PATH=$PATH:~/.platformio/penv/bin cd repo/ci pio run -e ${{ matrix.platform }} @@ -38,15 +38,15 @@ jobs: path: repo - name: Install environment uses: ./repo/.github/actions/platformio-env - - name: Build 1 + - name: micro-ros_publisher shell: bash - run: | + run: export PATH=$PATH:~/.platformio/penv/bin cd repo/examples/micro-ros_publisher - pio run -e teensy41 - - name: Build 2 + pio run + - name: micro-ros_subscriber_wifi shell: bash - run: | + run: export PATH=$PATH:~/.platformio/penv/bin cd repo/examples/micro-ros_subscriber_wifi pio run -e esp32cam From 5634e9c296c3e27aade0b1ec595b7ed76dacc95a Mon Sep 17 00:00:00 2001 From: Andy Blight Date: Tue, 19 Mar 2024 11:00:04 +0000 Subject: [PATCH 5/6] Restored missing vertical bar symbols. --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ba772849..4893732e 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: uses: ./repo/.github/actions/platformio-env - name: Build shell: bash - run: + run: | export PATH=$PATH:~/.platformio/penv/bin cd repo/ci pio run -e ${{ matrix.platform }} @@ -40,13 +40,13 @@ jobs: uses: ./repo/.github/actions/platformio-env - name: micro-ros_publisher shell: bash - run: + run: | export PATH=$PATH:~/.platformio/penv/bin cd repo/examples/micro-ros_publisher pio run - name: micro-ros_subscriber_wifi shell: bash - run: + run: | export PATH=$PATH:~/.platformio/penv/bin cd repo/examples/micro-ros_subscriber_wifi pio run -e esp32cam From 67ccfffab0ce90fa21b9ca488cbab7575159c0e1 Mon Sep 17 00:00:00 2001 From: Andy Blight Date: Tue, 19 Mar 2024 11:29:26 +0000 Subject: [PATCH 6/6] Fixed publisher example. The publisher example code was failing due to the first 3 lines of the platformio.ini file being commented out. I can now build that publisher example on my PC, so hopefully this time it will build both examples. --- .github/workflows/ci.yml | 2 +- examples/micro-ros_publisher/platformio.ini | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4893732e..843672c7 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,7 +43,7 @@ jobs: run: | export PATH=$PATH:~/.platformio/penv/bin cd repo/examples/micro-ros_publisher - pio run + pio run -e teensy41 - name: micro-ros_subscriber_wifi shell: bash run: | diff --git a/examples/micro-ros_publisher/platformio.ini b/examples/micro-ros_publisher/platformio.ini index 71c5e8ea..a1647490 100644 --- a/examples/micro-ros_publisher/platformio.ini +++ b/examples/micro-ros_publisher/platformio.ini @@ -1,6 +1,6 @@ -; [env:teensy41] -; platform = teensy -; board = teensy41 +[env:teensy41] +platform = teensy +board = teensy41 framework = arduino board_microros_transport = serial lib_deps =