Rosserial arduino publish topic in different void function
Hello Dear Community.
I am trying to publish data from the Arduino but it seems it's not working well because I can't see the message publishing. I guess the arduino publish fast and when I run rosrun rosserial_python serial_node.py /dev/ttyACM0
takes time to connect.
I need to publish a topic from another void function different than void loop.
Here is my code in Arduino
#define USE_USBCON
#include <ros.h>
#include <std_msgs/UInt8.h>
#define STATUS_PROCESSING 0x31
uint8_t CURRENT_STATUS;
ros::NodeHandle nh;
std_msgs::UInt8 str_msg;
ros::Publisher chatter("chatter", &str_msg);
void setup(){
nh.initNode();
nh.advertise(chatter);
SERIAL_INIT(9600);
Serial1.println("INIT\tOK");
}
void loop(){
nh.spinOnce();
delay(3000);
}
void SERIAL_INIT(int t){
Serial1.begin(t);
CURRENT_STATUS = STATUS_PROCESSING;
str_msg.data = CURRENT_STATUS;
delay(3000);
chatter.publish( &str_msg );
nh.spinOnce();
delay(1000);
}
and this is my python subscriber:
import rospy
from std_msgs.msg import UInt8
from time import sleep
def callback(data):
rospy.loginfo("I heard: %s", data.data)
def listener():
rospy.Subscriber("chatter", UInt8, callback)
rospy.spin()
if __name__ == '__main__':
rospy.init_node('listener', anonymous=True)
listener()
Running the code, nothing happens... I need to see for one time that SERIAL INIT is done. I hope someone helps me. It seems that is publishing but when I run rosrun rosserial_python serial_node.py /dev/ttyACM0 it takes time to run son when is running the Arduino already published... So How can I fix it? Thank you so much.
Not clear when you say nothing happens are you referring to not seeing a message in ros, or not seeing anything from the Serial1 port on the mega, or both.
I would tend to turn on the built - in led to provide an indication of whether the function has completed, rather than adding the complication of the additional serial port.
I don't have a mega so can't try your code as is, but when I removed the Serial1 stuff and tried your code on an Uno, I got messages appearing in rostopic echo /chatter if I called SERIAL_INIT from loop(), but not from within setup()
Thanks for answering me. I've solved that issue moving the publisher to the void loop... but when it is in other void functions, It seems is not publishing... Do you know a good way to publish a message from other void functions?
Is this what you mean by publish in another function, this works as expected on an uno, rostopic echo /chatter
Thank you just a few minutes I've tried and works... Thank you. Is there anyway to test the connection between Ros and Arduino? For instance test connection between Ros in raspberry with Ros Arduino?
there are many things to try to troubleshoot things, if you have a specific problem probably best to ask a new question to keep stuff organised and easier to search, as this is a different issue to the original question