ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Ok, so I figured out what code was triggering the issue. The thing is that I was trying to measure the time it takes to publish a message to ROS from Arduino. So the code I had was something like this:

ros::NodeHandle nh;
LogMsgType log_msg;  // where LogMsgType is some/any ROS message type
ros::Publisher logger("logger", &log_msg);

void motorInterruptLeft() {
  // literary just an empty function
}

void setup() {
  attachInterrupt(1, motorInterruptLeft, CHANGE);
  nh.initNode();
  nh.advertise(logger);
}

void loop() {
  nh.spinOnce();

  long curTime = millis();
  static long lastTrigger = 0;
  if (curTime - lastTrigger > 100) {
    lastTrigger = curTime;
    static long time = 0;
    logger.publish( &log_msg ); // doing some publishing every 100ms
    time = millis();  // !!!!!! THIS CALL CAUSES ROSSERIAL TO LOOSE CONNECTION
  }
}

I'm not sure exactly how it works internally, but apparently there is some sort of clash between ROS publisher communicating over Serial, interruptions being triggered (I mean 'motorInterruptLeft') and reading the current time with 'millis()' call. It probably has something to do with 'millis()' disabling the interrupts in order to read consistent view of the current time. However when I read the time before calling 'logger.publish()' the problem doesn't reproduce, so it's definitely something related to serial communication being performed.

I'm new to microcontroller development. Would be nice to hear comments from professionals.