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
- 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
- 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.