micro-ROS / micro_ros_arduino

micro-ROS library for Arduino
Apache License 2.0
437 stars 113 forks source link

esp32 publishing rate limited at 2 Hz #1810

Closed amangan closed 2 months ago

amangan commented 2 months ago

I am not able to increase the frequency more than 2 Hz (seems like a similar issue to this one). It publishes two quickly data points then pauses

Description:

Steps to reproduce the issue

Install micro_ros_arduino humble try to decrease timer_timeout in publisher example (it is able to increase & is printing the expected values when connecting to an encoder) echo output when running ros2 topic hz /micro_ros_arduino_node_publisher getting the output below

Even when trying to use rclc_publisher_init_best_effort, I am seeing the same output. Same thing when getting rid of the delay in the loop()

Screenshot from 2024-07-25 16-22-06

Any ideas on things to try or know of common causes?

(Edit - attaching code below)

`#include

include

include <rcl/rcl.h>

include <rcl/error_handling.h>

include <rclc/rclc.h>

include <rclc/executor.h>

include <std_msgs/msg/int32.h>

rcl_publisher_t publisher; std_msgsmsgInt32 msg; rclc_executor_t executor; rclc_support_t support; rcl_allocator_t allocator; rcl_node_t node; rcl_timer_t timer;

define LED_PIN 13

define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}}

define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}

void error_loop(){ while(1){ digitalWrite(LED_PIN, !digitalRead(LED_PIN)); 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++; } }

void setup() { set_microros_transports();

pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, HIGH);

delay(2000);

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_arduino_node", "", &support));

// create publisher RCCHECK(rclc_publisher_init_default( &publisher, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "micro_ros_arduino_node_publisher"));

// create timer, const unsigned int timer_timeout = 100; RCCHECK(rclc_timer_init_default( &timer, &support, RCL_MS_TO_NS(timer_timeout), timer_callback));

// create executor RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator)); RCCHECK(rclc_executor_add_timer(&executor, &timer));

msg.data = 0; }

void loop() { delay(100); RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100))); }`

hippo5329 commented 2 months ago

This example is expected to run slowly for demonstration purpose. It is not for real usage. You may try remove the delay(100) in the loop.

void loop() { // delay(100); RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100))); }

amangan commented 2 months ago

Yeah I mention above that the same output occurs when getting rid of the delay in the loop() and when using rclc_publisher_init_best_effort

hippo5329 commented 2 months ago

Please follow my wiki, https://github.com/hippo5329/micro_ros_arduino_examples_platformio/wiki.

Remove the delay. I can run to 200Hz without issue. The max rate is about 250Hz with my i7 laptop.

amangan commented 2 months ago

@hippo5329 just followed your wiki (but for humble) and everything seems to be working - thank you!