rosseial arduino multiarray callback can not run

Environment

ROS DISTRO: Noetic
Arduino Board: Arduino Mega2560

Problem Description

I tried to use the rosserial library for serial communication. I tested sending some data types, such as std_msgs/Int32 and std_msgs/Int16, and the communication worked fine, with the callback functions being triggered normally. However, when I tried to send data of the types std_msgs/Int16MultiArray and std_msgs/Int32MultiArray, the callback functions of the subscriber node on the Arduino did not get triggered.

Code

The following code block is the content of the file int32MultiArrayTest.cpp.This program runs on ROS Noetic.

#include <ros/ros.h>
#include <std_msgs/Int32MultiArray.h>

void sub_cb(const std_msgs::Int32MultiArray::ConstPtr &msg) {
  ROS_INFO("Result: %d %d %d %d", msg->data[0], msg->data[1], msg->data[2],
           msg->data[3]);
  //   ROS_INFO("Steps: %d", msg->data);
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "/int32_node");
  ros::NodeHandle nh;
  ros::Publisher pub =
      nh.advertise<std_msgs::Int32MultiArray>("/int32_topic", 5);
  ros::Subscriber sub =
      nh.subscribe<std_msgs::Int32MultiArray>("/arduino_add", 5, sub_cb);
  ros::Rate loop_rate(1);
  std_msgs::Int32MultiArray msg;
  msg.data = {1, 2, 3, 4};
  msg.layout.dim.push_back(std_msgs::MultiArrayDimension());
  msg.layout.dim[0].size = 4;
  msg.layout.dim[0].label="Steps";
  msg.layout.dim[0].stride=1;
  while (ros::ok()) {
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
  }
  return 0;
}

The following code block is the content of the file run_int32test.launch.

<launch>
    <node pkg="rosserial_arduino" type="serial_node.py" name="serial_interface" respawn="true"
        output="screen">
        <param name="port" value="/dev/ttyACM0" />
        <param name="baud" value="9600" />
    </node>
    <node pkg="scara_control" type="int32test" name="test_int32_arduino" respawn="true"
        output="screen">
    </node>
</launch>

The following code block is the content of the file rosSerialTest.ino.It runs on the Arduino Mega2560.

#include <Arduino.h>
#include <ros.h>
#include <std_msgs/Int32MultiArray.h>

#define LED_PIN 9

std_msgs::Int32MultiArray sum_msg;

ros::NodeHandle nh;

ros::Publisher pub("/arduino_add", &sum_msg);

long steps[4] = {0, 0, 0, 0};

void messageCb(const std_msgs::Int32MultiArray &msg) {
  digitalWrite(LED_PIN, HIGH);
  for (size_t i = 0; i < 4; i++)
    steps[i] = msg.data[i];
}

ros::Subscriber<std_msgs::Int32MultiArray> sub("/int32_topic", &messageCb);

void setup() {
  pinMode(LED_PIN, LOW);
  digitalWrite(LED_PIN, LOW);
  nh.getHardware()->setBaud(9600);
  nh.initNode();
  nh.advertise(pub);
  nh.subscribe(sub);
  nh.spinOnce();
  delay(1);
}

void loop() {
  sum_msg.data = steps;
  sum_msg.data_length = 4;
  pub.publish(&sum_msg);
  nh.spinOnce();
  delay(1);
}

What I Have Tried

  1. I tried running the above code and got the following result.
[INFO][1716035508.688977485]: Result: 0 0 0 0
[INFO][1716035508.688985089]: Result: 0 0 0 0
[INFO][1716035508.689395577]: Result: 0 0 0 0
[INFO][1716035508.689496259]: Result: 0 0 0 0
  1. I tried changing the data type in the above code to std_msgs/Int16MultiArray, compiled and ran it, and got the following result.
[INFO][1716035508.688977485]: Result: 0 0 0 0
[INFO][1716035508.688985089]: Result: 0 0 0 0
[INFO][1716035508.689395577]: Result: 0 0 0 0
[INFO][1716035508.689496259]: Result: 0 0 0 0

However, after waiting for a while (at least 2 minutes), the callback function of the subscriber node on the Arduino was triggered, and I got the following result.

[INFO][1716035508.688977485]: Result: 1 2 3 4
[INFO][1716035508.688985089]: Result: 1 2 3 4
[INFO][1716035508.689395577]: Result: 1 2 3 4
[INFO][1716035508.689496259]: Result: 1 2 3 4

Thank You in Advance

Thank you all for your help! Any suggestions are greatly appreciated. I will make sure to update this post with the solution once I have resolved the issue, so others can benefit from it as well.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật