micro-ROS / micro-ROS/micro_ros_arduino
micro-ros-publisher keeps publishing the same value over and over even though I update the message every callback.
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 576
- Forks
- 146
- PR merge metrics
- No merged PRs in 30d
Description
I am using micro ros to send imu data from a esp32 to my laptop running ros(on a virtual machine). The topic shows up, and so does the data except that it just outputs the same values over and over, even If I move the imu, and its completely different from the values I get if I just display the values on serial monitor. Can anyone tell me what going wrong?
Here's the full code
```
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
rcl_publisher_t publisher;
sensor_msgs__msg__Imu Message;
std_msgs__msg__Header hdr;
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)){}}
Adafruit_MPU6050 mpu;
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, &Message, NULL));
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Message.header = hdr;
Message.linear_acceleration.x = double(a.acceleration.x);
Message.linear_acceleration.y = double(a.acceleration.y);
Message.linear_acceleration.z = double(a.acceleration.z);
Message.angular_velocity.x = double(g.gyro.x);
Message.angular_velocity.y = double(g.gyro.y);
Message.angular_velocity.z = double(g.gyro.z);
}
}
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(sensor_msgs, msg, Imu),
"imu/data"));
// create timer,
const unsigned int timer_timeout = 1000;
RCCHECK(rclc_timer_init_default(
&timer,
&support,
RCL_MS_TO_NS(timer_timeout),
timer_callback));
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// create executor
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
RCCHECK(rclc_executor_add_timer(&executor, &timer));
Message.linear_acceleration.x = double(0);
Message.linear_acceleration.y = double(0);
Message.linear_acceleration.z = double(0);
Message.angular_velocity.x = double(0);
Message.angular_velocity.y = double(0);
Message.angular_velocity.z = double(0);
}
void loop() {
delay(100);
RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Message.header = hdr;
Message.linear_acceleration.x = double(a.acceleration.x);
Message.linear_acceleration.y = double(a.acceleration.y);
Message.linear_acceleration.z = double(a.acceleration.z);
Message.angular_velocity.x = double(g.gyro.x);
Message.angular_velocity.y = double(g.gyro.y);
Message.angular_velocity.z = double(g.gyro.z);
}
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing the message update and publish calls in timer_callback and loop in the supplied sketch. Reproduce the issue with the ESP32 and IMU, then verify that the published imu/data values reflect the latest sensor readings rather than an earlier message state.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- embedded-iot, robotics
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100