Node for Publishing csv file and node to subscribe

asked 2020-06-24 05:24:10 -0500

subarashi gravatar image

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

  • No;Material;Task
  • 1;Hola;Cebo
  • 2;Hola;Cebo52
  • 3;Hola;Cebo25
  • 4;Hola2;Cebo2
  • 5;Hola;Cebo53
  • 6;Hola;Cebo54

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()
edit retag flag offensive close merge delete