简体   繁体   中英

Is there any way to get roscore string using python script in raspberry pi?

I'm using Raspberry Pi for LaserScanner(Motor + Rangefinder/Hokuyo).

For the case of Rangefinder, I open 3 command window,

#1: roscore

#2: rosrun urg_node urg_node _serial_port:=/dev/ttyACM0

#3: sh /home/pi/hokuyo/lidarDemo.sh /home/pi/Desktop/Demo/Data lidar.txt

where lidarDemo.sh:

if [$# -ne 2 ] 

then 
  echo '2 inputs'
  exit 1
fi

rostopic echo /scan|tee $1/$2

Then lidar.txt is saved until I close the terminal#3.

lidar.txt:

header:

seq: 16828

stamp:

secs: 1594386688

nsecs: 380816175

frame_id: "laser"

angle_min: -2.35619449615

angle_max: 2.35619449615

angle_increment: 0.00436332309619

time_increment: 1.73611151695e-05

scan_time: 0.0250000003725

range_min: 0.0230000000447

range_max: 60.0

ranges: [3.888000011444092, 3.888000011444092, 3.888000011444092, ...

intensities: [2673.0, 2663.0, 2652.0, 2673.0...


header:

seq: 16828

stamp:

...

Now, Can I get those strings of lidar.txt by using python script in real-time?

For example, test.py:

while(True):
    str = getRoscore()
    print(str)

Assuming you want to get realtime lidar data in python; you can write a python script which has a subscriber to /scan topic where you can process your data in real-time something similar to this,

    import rospy
    from sensor_msgs.msg import LaserScan


    lidar_message = None

    def lidar_callback(data):
        rospy.loginfo(rospy.get_caller_id() + " %s", data)
        lidar_message = data
        ##Execute necessary processing here..

    if __name__ == '__main__':
        rospy.init_node('lidar_node', anonymous=True)
        rospy.Subscriber("/scan", LaserScan, lidar_callback)
        
        ##Execute rest of the logic here...

        rospy.spin()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM