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

publishing sensory data out of sync

asked 2022-05-09 21:45:54 -0500

huybeme gravatar image

Hi all,

I am working on a project with the ROBOTIS turtlebot3 burger and am trying to include a temperature sensor with its build.

I have uploaded some arduino code into the OpenCR1.0 to read sensory data for serial communication. This data is being read on my raspberry pi with ubuntu server 20.04. I am also using ROS2 foxy as well.

#define PORT4_SIG1          73
#define PORT4_SIG2          74
#define PORT4_ADC           75
#define OLLO_SLEEP          46

void setup() {
  Serial.begin(115200);
  pinMode(OLLO_SLEEP, OUTPUT);
  digitalWrite(OLLO_SLEEP, HIGH);
  pinMode(PORT4_ADC, INPUT_ANALOG);
  pinMode(PORT4_SIG1, OUTPUT);
  pinMode(PORT4_SIG2, OUTPUT);
}
void loop() {
  uint32_t analogValue;
  analogValue = analogRead(PORT4_ADC);
  word long vvalue = (4095 - analogValue) * 10000 / analogValue;
  Serial.println(float(vvalue / 1000.0));
}

Then I have a python script to confirm I can read its value and print into the terminal.

#!/usr/bin/env python3
import serial
import time
if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyACM0', 115200, timeout=1)
    ser.reset_input_buffer()
    while True:
        line = ser.readline().decode('utf-8').rstrip()
        print(line)

I can see the temperature changing properly as I grip onto the sensor probe and let go. Now I want to implement this with ROS2. Here is my attempt in reading in the serial data and publishing it:

#!/usr.bin/env python3
import rclpy
from rclpy.node import Node
from std_msgs.msg import Float32
import serial

class tps_node(Node):
    def __init__(self):
        super().__init__("tps_node")

        self.ser = serial.Serial('/dev/ttyACM0', 115200, timeout=1)
        self.ser.reset_input_buffer()

        self.tps_publisher = self.create_publisher(
            Float32, "tps_node", 10
        )
        self.pub_timer = self.create_timer(0.1, self.callback_temp_publisher)

        self.get_logger().info("TPS10 node started")

    def callback_temp_publisher(self):

        if self.ser.in_waiting > 0:
            line = self.ser.readline().decode('utf-8').rstrip()
            try:
                line = float(line)
            except:
                return
            temp = Float32()
            temp.data = line
            self.tps_publisher.publish(temp)
            print(line)

def main(args=None):
    rclpy.init(args=args)
    node = tps_node()
    rclpy.spin(node)
    rclpy.shutdown()


if __name__ == "__main__":
    main()

My issue is that I think my timer is not in sync with the serial communication. I get temperature outputs but it does not increase with me holding onto the sensor probe. I think the output data is too far back in the stack and cannot keep up with the communication. So my question is, what is the best way to get sensory data from my OpenCR1.0 (an arduino board) to my pi so I can publish it in as near real time as possible or to get the latest value to get the most up to date value.

Please let me know if I have left out any detail and need to provide more information.

Thanks in advance!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2022-05-10 14:57:30 -0500

Mike Scheutzow gravatar image

On the ros side, your approach to reading the serial port is not good. All the data being sent is being buffered, so currently you are reading the oldest line from that buffer. If the arduino is sending lines faster than 10Hz, the read side will never catch up and your readings will be increasing out of date.

edit flag offensive delete link more

Comments

Thanks for your input all, I am new to arduino and ROS so bear with me. I did manage to get acceptable readings by making my pi send a message to trigger the arduino to send the sensor values. This should get me through my project. Before I close out this thread, could I get your input on a few additional question.

I have read on a few threads that generally the maximum baud rate is 115200 for the serial communication, does this control the rate of output? It seems like my output from the arduino is about a second even when I lower my timer rate for publishing. I was hoping to understand how to control it making it faster. I do understand that I could delay to make it slower although it might be hacky.

huybeme gravatar image huybeme  ( 2022-05-11 19:27:24 -0500 )edit

This web site is organized as one question / one answer. Please create a new question. In the description for your new question you can include a url back to this question if you think it will provide helpful context. But it's good if you make your new question as standalone as you can.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-05-12 06:38:58 -0500 )edit
2

answered 2022-05-10 04:14:03 -0500

dinesh gravatar image

My suggestion is not to write the coe which communicates with arudino from raspberry from scracts for it will make the task more complicated. You can search and use already available arduino serial packags from github to do this and modify them according to your requirement if needed. For example here is one arduino serial communication library = Here

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-05-09 21:45:54 -0500

Seen: 172 times

Last updated: May 10 '22