0

This is my code to read the LDS sensor. LDS sensor is used to estimate the distance from robot to walls. I can print the data(estimate distance) in the terminal but I cant write it in the csv file automatically. I would like to write about 1000 data into csv file by using python code enter image description here.

#! /usr/bin/env python

import rospy
import csv
from sensor_msgs.msg import LaserScan

def callback(msg):
    d = msg.ranges [90]
    print d

rospy.init_node("read")
sub = rospy.Subscriber('/scan', LaserScan, callback)
rospy.spin()

f= open("\\testread.csv", "w")
c=csv.writer(f)
for x in range(1000):
    c.writerow(d)

f.close()
martineau
  • 119,623
  • 25
  • 170
  • 301
AmirulJ
  • 118
  • 1
  • 11
  • You have a [scope](https://stackoverflow.com/q/291978/10077) problem with `d`. But please post your traceback as text in the question, not as an image. – Fred Larson Aug 04 '21 at 15:54
  • 1
    [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – martineau Aug 04 '21 at 15:55
  • It looks like you're using Python 2. If so please tag your question accordingly. – martineau Aug 04 '21 at 15:57

2 Answers2

1

Outputting a topic formatted as csv is built into rostopic echo, the -p flag rostopic_echo. You can redirect the terminal output to a file instead of the terminal with >

So to save /scan to csv you could just run the following in a terminal:

rostopic echo -p /scan > scan.csv

That page also mentions that the -b flag can be used to echo a specific topic from a rosbag. Not sure if you are aware of rosbag which is purpose built for recording ros data. That way you could also record other relevant data at the same time and use other ros tools to analyze your data like plotjuggler for example.

MRFalcon
  • 121
  • 4
  • Good points; if he's not pre-processing the data before writing it, there's no need to write a node, and if it's being written to csv, something will process it later anyway, so no need to write a node. – JWCS Aug 06 '21 at 14:46
  • Thank you so much. I've run this code in the terminal and it works. it created another csv file, but when I do like this rostopic echo -p /scan > \\scan.csv the terminal said the permission is denied. May I know why? – AmirulJ Aug 08 '21 at 15:59
  • I guess you are using windows. Not seeing the same on my system. What ever operating system you are using the \\ is interpreted as path. Not sure what exactly the operating system will try to do if you give that as a path, nothing good apparently. You can specify some specific location with absolute path like `... > ~/my/data/scan.csv` or a relative path from where you are currently in the terminal such as `... > ../scan.csv`. Applies to Ubuntu at least. Not familiar with how windows shell handles all of this. – MRFalcon Aug 09 '21 at 07:13
  • 1
    @JWCS, thanks for the reply, theoretically some simple processing could be done with existing ros tools as well by applying transform from topic_tools to the topic http://wiki.ros.org/topic_tools/transform. Depending on the application and what is necessary it could quickly be more sensible to write a node to do more complex things though. – MRFalcon Aug 09 '21 at 07:33
0

In a more classic ROS style, do all your setup in the "main" function, have global variables for either the returned values of callbacks or for initialized parameters, and do the processing for all your received msgs in your callbacks or timers.

#! /usr/bin/env python
# lds_to_csv_node.py

import rospy
import csv
from sensor_msgs.msg import LaserScan

# Global vars
csv_writer = None

def scan_callback(msg):
    d = msg.ranges [90]
    if csv_writer is not None:
        csv_writer.writerow(d)

def main():
    rospy.init_node("lds_to_csv_node")
    sub = rospy.Subscriber('/scan', LaserScan, scan_callback)
    fh = open("\\testread.csv", "wb")
    csv_writer = csv.writer(fh) # More constant params
    rospy.spin()
    fh.close()
    csv_writer = None

if __name__ == '__main__':
    main()
JWCS
  • 1,120
  • 1
  • 7
  • 17
  • Thank you so much. I've try this coding and have no error, but my csv file is still empty after executing. May I know why? – AmirulJ Aug 08 '21 at 15:41
  • If you add a print statement to the scan_callback, and nothing is printed, then perhaps the scan callback isn't receiving data, your main() initialization is messed up? If you try to echo the topic and nothing is printed, maybe your source or topic is messed up? If data isn't getting into the node, then it's a problem more likely with the other side – JWCS Aug 11 '21 at 16:28