Skip to content

Reading from the sensor with python

ldemk edited this page Apr 9, 2019 · 1 revision
  1. Create a package

  2. Create a launch file there

<launch>
    <!-- My Package launch file -->
    <node pkg="steering" type="avoid_wall_publisher.py" name="steering_publisher"  output="screen">
    </node>
</launch>
  1. Create an avoid_wall_publisher.py file
#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
# import sensor_msgs
from sensor_msgs.msg import LaserScan

def callback(msg): 
    print (len(msg.ranges))
    print (msg.ranges[-1], msg.ranges[0], msg.ranges[len(msg.ranges)//2])


rospy.init_node('steering_wall')
# rate = rospy.Rate(2)               # We create a Rate object of 2Hz
sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)
rospy.spin()

while not rospy.is_shutdown():     # Endless loop until Ctrl + C
    # rospy.spin()
  callback(msg)
  rate.sleep()
Clone this wiki locally