Menu

Offline CSV Template

brandon shrewsbury

In this example we are pulling data from a Vantage Vue Davis Weather Station. The data is exported as a csv file. Samples were stored at 1hz. We have broken the template across two files, the transport thread which acts just like the parse and publish class in the real time example, and a csv parser which posts event data at particular time intervals to create a correctly spaced rosbag.

Parse and Publish

#!/usr/bin/python
import threading
import rospy
import time
import Queue
from weather_logger.msg import weather

class Transport_thread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.__shutdown = False
        self.__pubWeather = rospy.Publisher('weather_data', weather)
        self.__queue = Queue.Queue()
        rospy.init_node('weatherLogger')



    def run(self):
        while self.__shutdown is not True:
            if not self.__queue.empty():
                msg = self.fill_msg(self.__queue.get_nowait())

                if not rospy.is_shutdown():
                    self.__pubWeather.publish(msg)#export to ROS

    def setData(self, data):
        self.__queue._put(data)

    def fill_msg(self,data):
        weatherMsg = weather()
        weatherMsg.date = data[0]
        weatherMsg.time = data[1]
        weatherMsg.temp_out = float(data[2])
        weatherMsg.hi_temp = float(data[3])
        weatherMsg.low_temp = float(data[4])
        weatherMsg.out_hum = int(data[5])
        weatherMsg.dew_pt = float(data[6])
        weatherMsg.wind_spd = float(data[7])
        weatherMsg.wind_dir = data[8]
        weatherMsg.wind_run = float(data[9])
        weatherMsg.hi_spd = float(data[10])
        weatherMsg.hi_dir = data[11]
        weatherMsg.wind_chill = float(data[12])
        weatherMsg.heat_index = float(data[13])
        weatherMsg.thw_index = float(data[14])
        weatherMsg.bar = float(data[15])
        weatherMsg.rain = float(data[16])
        weatherMsg.rain_rate = float(data[17])
        weatherMsg.head_dd = float(data[18])
        weatherMsg.cool_dd = float(data[19])
        weatherMsg.in_temp = float(data[20])
        weatherMsg.in_hum = int(data[21])
        weatherMsg.in_dew = float(data[22])
        weatherMsg.in_heat = float(data[23])
        weatherMsg.in_emc = float(data[24])
        weatherMsg.in_air_dens = float(data[25])
        weatherMsg.wind_samp = int(data[26])
        weatherMsg.wind_tx = int(data[27])
        weatherMsg.iss_recept = float(data[28])
        weatherMsg.arc_int32 = int(data[29])
        return weatherMsg;

    def shutdown(self):
        self.__shutdown = True

CSV Parser

#!/usr/bin/python
import sys
from time import sleep
from rosConnector import Transport_thread

def open_file():
    args = sys.argv[1:]
    if len(args) == 1:
        return open(args[0], "r") #Open file
    return None

def parse_data(myFile):
    rosSender = Transport_thread()
    rosSender.start()#start thread
    for aLine in myFile:
        data = aLine.split('\t') #assign/reassign thread values
        rosSender.setData(data) #send data to thread
        sleep(60) #sleep for 1 minute

    myFile.close() #close file
    rosSender.shutdown()

if __name__ == '__main__':
    myfile = open_file()
    if myfile is not None:
        parse_data(myfile)
    else:
        print "File not found. Exiting!"

Related

Wiki: Interfacing with Systems

MongoDB Logo MongoDB