Robotics StackExchange | Archived questions

Node for Publishing csv file and node to subscribe

Dear community. I hope you all doing well. I am developing a big project using ROS and Raspberry pi. I have one Raspberry pi as a master and two raspberry pi as slaves. So in the Raspi Master, I upload a CSV file containing something like this

So, the first node must publish the data of the CSV file. If the "Material" is "Hola" then publish "Cebo" with TOPIC1 in otherwise if the "Material" is "Hola2" then publish "Cebo2" with TOPIC2.

The second node must subscribe from the previous node and publish for rosserial Arduino.

My problem now is when I publish the CSV file, the subscriber(which is subscribing only to TOPIC1) receives the second line and not the first line. I do not know why. Please take a look at my code. Btw, after read the CSV the publisher node must stop publishing.

Main_Node.py

#!/usr/bin/env python
import serial
import rospy
import csv
import os
import rospkg
from std_msgs.msg import String
from time import sleep

rospack = rospkg.RosPack()
rospack.get_path('haive_project')

def Assignment_of_task():
      with open(rospack.get_path('haive_project')+'/script/test_2.csv','r') as csv_file:
             csv_reader = csv.DictReader(csv_file)
             for line in csv_reader:
                  print(line['Machine'])
                  print(line)
                  if line['Material'] == 'Hola':
                     command = line['command']
                     pub.publish(command)
                     print(command)
                     rate.sleep()

if __name__ == '__main__':
     rospy.init_node('Selector_node', anonymous=True)
     pub = rospy.Publisher('Assigning_task', String, queue_size=10)
     rate = rospy.Rate(10)
     try:
          Assignment_of_task()
          print("Done")
     except rospy.ROSInterruptException:
         pass

Node_2.py

#!/usr/bin/env python
import serial
import rospy
from std_msgs.msg import String
from time import sleep

class Haive_1001(object):
  def __init__(self):
      self.sub = rospy.Subscriber("Assigning_task", String, self.callback)
      self.haive1001 = String()


  def callback(self, coordinates):

      self.haive1001 = coordinates
      print(self.haive1001)
      pub.publish(self.haive1001)
      rate.sleep()

if __name__ == "__main__":
    rospy.init_node('Haive_1001_node', anonymous=True)
    pub = rospy.Publisher('data_for_Arduino_DUE', String, queue_size=10)
    rate = rospy.Rate(10) # 10hz
    haive_class = Haive_1001()
    rospy.spin()

Asked by subarashi on 2020-06-24 05:24:10 UTC

Comments

Are you sure that your subscriber is correctly subscribed to the topic before the publisher sends the first message? Is the first message being published in the topic? I have faced some problems with synchronization in both sides, either the subscriber not yet subscribed to the topic when you start sending messages, either the topic is not correctly initialized when the publisher starts sending messages. I would check this, and also I would try to play with queue_size. Try to set it to one.

Asked by Bernat Gaston on 2020-07-27 08:58:54 UTC

Answers