0
$\begingroup$

Setup:

  • Arduino Portenta H7 & Arduino Nano RP2040 Connect
  • Host PC running ROS2 Foxy and micro ros agent

Problem: I get this error from time to time and randomly

Subscriber error : Deserialization of data failed -> function deserialize_change

ERROR

when I try to read the data published by the board (either one) one ros2 topics. I get the data that I want, ultrasonic sensor readings, but sometimes I get this error. It seems that the data published has some corrupt info, eventhough I have filtered out in my code any other characters that are not ASCII characters.

Any fix for this ?

Code:

#include <micro_ros_arduino.h>

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

#include <std_msgs/msg/string.h>
#include <cctype>

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

int triggerPins[] = {2, 4, 6};  
int echoPins[] = {3, 5, 7};     

unsigned long durations[3];

#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));
  }
}

bool isASCII(char c) {return (c >= 32 && c <= 126);}
bool isAlNum(char c) {return (c >= '0' && c <= '9') || (c>= 'A' && c <= 'Z') || (c >= 'a' && c<= 'z');}

void removeSpecialCharacters(char* str) {
  for (int i =0; str[i] != '\0'; ++i){
    if (!isASCII(str[i]) && isAlNum(str[i])){
      // If the character is not alphanumeric or a space, remove it
      for (int j = i; str[j] != '\0'; ++j){
        str[j] = str[j + 1];
      }
      --i; 
    }
  }
}

void setup() {
  set_microros_wifi_transports("SSID", "PASS", "192.168.X.XX", 8888);

  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, String),
    "micro_ros_arduino_node_publisher"));

  // create timer,
  const unsigned int timer_timeout = 700;
  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));
  
  for (int i = 0; i < 3; ++i) {
    pinMode(triggerPins[i], OUTPUT);
    pinMode(echoPins[i], INPUT_PULLDOWN);
  }
  
  msg.data.data = " ";
}

void loop() {
  delay(100);
  RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
  char terminal[100] = " ";
  
  for (int i = 0; i < 3; ++i) {
    
    digitalWrite(triggerPins[i], LOW);
    delayMicroseconds(10); 
    digitalWrite(triggerPins[i], HIGH);
    delayMicroseconds(10); 
    digitalWrite(triggerPins[i], LOW);
    durations[i] = pulseIn(echoPins[i], HIGH, 40000UL);

    double distanceCm = (double)durations[i] / 2.0 * 0.0343;

    char durationString[10] = " ";

    char direction[10] = " ";
    if (i==0){strcat(direction, "Front");}
    else if (i==1){strcat(direction, "Angle");}
    else {strcat(direction, "Side");}
    
    char sensorData[50]; // Is up above
    sprintf(sensorData, "Distance %s is %s cm\n", direction, durationString);

    strcat(terminal, sensorData);
  }
  removeSpecialCharacters(terminal);
  msg.data.data = terminal;
}
$\endgroup$

1 Answer 1

-1
$\begingroup$

Pass the data in your callback function

$\endgroup$
2
  • 1
    $\begingroup$ Welcome to Robotics Shanmugam S. Thanks for your answer but we are looking for comprehensive answers that provide some explanation and context. Very short answers cannot do this, so please edit your answer to explain why it is right, ideally with citations. Answers that don't include explanations may be removed. See How to Answer for more info. $\endgroup$
    – Chuck
    Commented Apr 11 at 14:53
  • 1
    $\begingroup$ OP states the error is Subscriber error : Deserialization of data failed -> function deserialize_change, how does your comment to pass the data in a callback address the deserialization issue? $\endgroup$
    – Chuck
    Commented Apr 11 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.