micro-ROS / micro_ros_platformio

micro-ROS library for Platform.IO
Apache License 2.0
225 stars 80 forks source link

Esp32 micro ros transport wifi #85

Closed Ben01233210 closed 2 years ago

Ben01233210 commented 2 years ago

Issue template

I am trying to connect my esp32 over wifi with the micro ros agent. It runs in a docker container. My machine is a Ubuntu 22.04.

Steps to reproduce the issue

My code is working with serial transport, i tried also with the example code form adruino and platform io adapted for wifi, nothing woked.

But here my code for the wifi:

IPAddress agent_ip(192, 168, 1, 100);
size_t agent_port = 8888;

char ssid[] = "ssid";
char psk[] = "pswd";
set_microros_wifi_transports(ssid, psk, agent_ip, agent_port);

My platform.ini:

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
board_microros_transport = wifi
monitor_filters = 
    direct
    esp32_exception_decoder
lib_deps = 
    waspinator/AccelStepper@^1.61
    https://github.com/micro-ROS/micro_ros_platformio

Micro ros agent in docker (it runs): docker run -it --rm -v /dev:/dev -v /dev/shm:/dev/shm --privileged --net=host microros/micro-ros-agent:humble udp4 --port 8888 -v6

Expected behavior

working (connecting to agent) :)

Actual behavior

No output on docker. I tried with serial print the program runs and I cloud print all the wifi credentials

Additional information

Esp is in wifi range and i tried some time before with esp idf and it worked also. Thanks for help and super cool project thanks for making it possible!

pablogs9 commented 2 years ago

Share code for replicating this issue.

Which transport are you using? Does the board connect to wifi with your setup without micro-ROS?

Ben01233210 commented 2 years ago

I am trying to use wifi as transport for micro ros but not getting it to work, with serial transport it worked.

Before I used micro ros with platform io, i used esp idf. There the wifi connection worked with micro ros and also the agent worked so there were no problems.

I don´t know how i should test the wifi connection without using micro ros. Thanks for the response!

#include <Arduino.h>

#include <micro_ros_platformio.h>

#include <rcl/rcl.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>

#include <std_msgs/msg/int32.h>

IPAddress agent_ip(192, 168, 178, 88);
size_t agent_port = 8888;

char ssid[] = "ssid";
char psk[] = "pwsd";  // change to correct ssid and pswd in my code

rcl_subscription_t subscriber;
std_msgs__msg__Int32 msg;

rclc_executor_t exe;
rclc_support_t support;
rcl_allocator_t allocator;
rcl_node_t node;

void spin_motor(const void *msgin)
{
    const std_msgs__msg__Int32 *msg = (const std_msgs__msg__Int32 *)msgin;
    digitalWrite(GPIO_NUM_17, HIGH);
    delay(msg->data * 100);
    digitalWrite(GPIO_NUM_17, LOW);
}

void setup()
{
    set_microros_wifi_transports(ssid, psk, agent_ip, agent_port);
    pinMode(GPIO_NUM_17, OUTPUT);
    digitalWrite(GPIO_NUM_17, HIGH);

    digitalWrite(GPIO_NUM_17, HIGH);
    delay(GPIO_NUM_17 * 100);
    digitalWrite(GPIO_NUM_17, LOW);

    rcl_allocator_t allocator = rcl_get_default_allocator();

    rclc_support_init(&support, 0, NULL, &allocator);

    // Init default node
    rclc_node_init_default(&node, "motor_driver", "", &support);

    rclc_subscription_init_default(
        &subscriber, &node,
        ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "motor_pos");

    rclc_executor_init(&exe, &support.context, 1, &allocator);

    rcl_ret_t rc = rclc_executor_add_subscription(
        &exe, &subscriber, &msg,
        &spin_motor, ON_NEW_DATA);
}

void loop()
{
    rclc_executor_spin_some(&exe, RCL_MS_TO_NS(100));
}
pablogs9 commented 2 years ago
Ben01233210 commented 2 years ago

Hi, I tried ruing wifi without micro ros, it worked. The Code I copied from the Internet :) It gets an Ip

I am sorry I don't now what you mean with your second point. But i am using the esp32dev board and just the normal wifi not wifi_nina and I did not create the colcon.meta file. I don't know if its helpful but i would share also the installation of micro ros in platform io.

The code for testing wifi:

#include <Arduino.h>
#include "WiFi.h"

// WiFi credentials.
const char* WIFI_SSID = "wifi-ssid";
const char* WIFI_PASS = "wifi-password";

void setup()
{
    Serial.begin(9600);
    // Giving it a little time because the serial monitor doesn't
    // immediately attach. Want the firmware that's running to
    // appear on each upload.
    delay(2000);

    Serial.println();
    Serial.println("Running Firmware.");

    // Connect to Wifi.
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(WIFI_SSID);

    // Set WiFi to station mode and disconnect from an AP if it was previously connected
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(100);

    WiFi.begin(WIFI_SSID, WIFI_PASS);
    Serial.println("Connecting...");

    while (WiFi.status() != WL_CONNECTED) {
      // Check to see if connecting failed.
      // This is due to incorrect credentials
      if (WiFi.status() == WL_CONNECT_FAILED) {
        Serial.println("Failed to connect to WIFI. Please verify credentials: ");
        Serial.println();
        Serial.print("SSID: ");
        Serial.println(WIFI_SSID);
        Serial.print("Password: ");
        Serial.println(WIFI_PASS);
        Serial.println();
      }
      delay(5000);
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    Serial.println("Hello World, I'm connected to the internets!!");
}

void loop()
{
    delay(5000);
    Serial.println("Hello World");
}

So here the installation of the micro ros lib:

Library Manager: Installing git+https://github.com/micro-ROS/micro_ros_platformio
git version 2.34.1
Cloning into '/home/neb/.platformio/.cache/tmp/pkg-installing-brxhhstz'...
remote: Enumerating objects: 67, done.
remote: Counting objects: 100% (67/67), done.
remote: Compressing objects: 100% (52/52), done.
remote: Total 67 (delta 10), reused 45 (delta 4), pack-reused 0
Receiving objects: 100% (67/67), 125.47 KiB | 2.46 MiB/s, done.
Resolving deltas: 100% (10/10), done.
Library Manager: micro_ros_platformio@0.0.1+sha.22cf9b6 has been installed!
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (5.2.0) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: 
 - framework-arduinoespressif32 @ 3.20005.220925 (2.0.5) 
 - tool-esptoolpy @ 1.40201.0 (4.2.1) 
 - tool-mkfatfs @ 2.0.1 
 - tool-mklittlefs @ 1.203.210628 (2.3) 
 - tool-mkspiffs @ 2.230.0 (2.30) 
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Installing pyyaml with pip at PlatformIO environment
/home/neb/.platformio/penv/bin/python -m pip install pyyaml
Requirement already satisfied: pyyaml in /home/neb/.platformio/penv/lib/python3.10/site-packages (6.0)
Installing markupsafe==2.0.1 with pip at PlatformIO environment
/home/neb/.platformio/penv/bin/python -m pip install markupsafe==2.0.1
Requirement already satisfied: markupsafe==2.0.1 in /home/neb/.platformio/penv/lib/python3.10/site-packages (2.0.1)
Configuring esp32dev with transport wifi
Downloading micro-ROS dev dependencies
         - Downloaded ament_cmake
         - Downloaded ament_lint
         - Downloaded ament_package
         - Downloaded googletest
         - Downloaded ament_cmake_ros
         - Downloaded ament_index
Building micro-ROS dev dependencies
Downloading micro-ROS library
         - Downloaded microcdr
         - Downloaded microxrcedds_client
         - Downloaded rcl_action
         - Downloaded rcl_yaml_param_parser (ignored)
         - Downloaded rcl
         - Downloaded rcl_lifecycle
         - Downloaded rclc_examples (ignored)
         - Downloaded rclc_lifecycle
         - Downloaded rclc
         - Downloaded rclc_parameter
         - Downloaded micro_ros_utilities
         - Downloaded rcutils
         - Downloaded micro_ros_msgs
         - Downloaded rmw_microxrcedds
         - Downloaded rosidl_typesupport_c
         - Downloaded rosidl_typesupport_cpp (ignored)
         - Downloaded rosidl_typesupport_microxrcedds_test_msg
         - Downloaded rosidl_typesupport_microxrcedds_c_tests
         - Downloaded rosidl_typesupport_microxrcedds_cpp_tests
         - Downloaded rosidl_typesupport_microxrcedds_cpp (ignored)
         - Downloaded rosidl_typesupport_microxrcedds_c
         - Downloaded rosidl_parser
         - Downloaded rosidl_adapter
         - Downloaded rosidl_generator_c
         - Downloaded rosidl_generator_cpp (ignored)
         - Downloaded rosidl_typesupport_introspection_tests
         - Downloaded rosidl_typesupport_introspection_c
         - Downloaded rosidl_typesupport_interface
         - Downloaded rosidl_runtime_c
         - Downloaded rosidl_cli
         - Downloaded rosidl_cmake
         - Downloaded rosidl_typesupport_introspection_cpp (ignored)
         - Downloaded rosidl_runtime_cpp (ignored)
         - Downloaded rmw_implementation_cmake
         - Downloaded rmw
         - Downloaded action_msgs
         - Downloaded rcl_interfaces
         - Downloaded composition_interfaces
         - Downloaded lifecycle_msgs
         - Downloaded rosgraph_msgs
         - Downloaded statistics_msgs
         - Downloaded test_msgs
         - Downloaded builtin_interfaces
         - Downloaded rosidl_default_generators
         - Downloaded rosidl_default_runtime
         - Downloaded unique_identifier_msgs
         - Downloaded common_interfaces
         - Downloaded nav_msgs
         - Downloaded actionlib_msgs
         - Downloaded std_srvs
         - Downloaded sensor_msgs_py
         - Downloaded sensor_msgs
         - Downloaded diagnostic_msgs
         - Downloaded std_msgs
         - Downloaded stereo_msgs
         - Downloaded shape_msgs
         - Downloaded trajectory_msgs
         - Downloaded visualization_msgs
         - Downloaded geometry_msgs
         - Downloaded test_interface_files
         - Downloaded rmw_implementation
         - Downloaded test_rmw_implementation
         - Downloaded rcl_logging_spdlog (ignored)
         - Downloaded rcl_logging_noop
         - Downloaded rcl_logging_interface
         - Downloaded tracetools_test
         - Downloaded test_tracetools
         - Downloaded tracetools_read
         - Downloaded ros2trace
         - Downloaded tracetools
         - Downloaded test_tracetools_launch
         - Downloaded tracetools_launch
         - Downloaded tracetools_trace
         - Extra packages folder not found, skipping...
Building micro-ROS library
Found 34 compatible libraries
Scanning dependencies...
Dependency Graph
|-- AccelStepper @ 1.61.0
|-- micro_ros_platformio @ 0.0.1+sha.22cf9b6
|   |-- WiFi @ 2.0.0
Building in release mode

Thank you very much for the effort!

pablogs9 commented 2 years ago

I am sorry I don't now what you mean with your second point.

In general, I just need instructions for replicating your issue. Share here your project so we can build and reproduce your problem.

Ben01233210 commented 2 years ago

Hi, sorry for replying so late. I switched my location and also the wifi. I used the same code like before but now it works. Probably it was something with the wifi. The only difference I know of is that I now saved it in the default location of platfrom io and not in a costume one like I did before. In about a week I am back in the other location. I`ll see if I can set it up then properly. But thank you very much!