HamsterPythonExample1

From cogniteam
Revision as of 09:01, 24 March 2019 by Ariy (talk | contribs) (Created page with "=== Python Example 1 === * Receive laser scan from robot to decide if free <syntaxhighlight lang="python" line='line'> #!/usr/bin/env python import rospy from sensor_msgs.msg...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Python Example 1

  • Receive laser scan from robot to decide if free

<syntaxhighlight lang="python" line='line'>

  1. !/usr/bin/env python

import rospy from sensor_msgs.msg import Imu

def is_free(ranges ,start_index, end_index ,min_distance): #sub array from angle to angle s_ranges = ranges[start_index:end_index] #filter the array b_ranges = filter(lambda x: x <= min_distance, s_ranges) return len(b_ranges) == 0


def callback(msg): if is_free(msg.ranges,160,200,0.2): print "FREE" else: print "BLOCKED"

rospy.init_node('scan_values')

  1. Put your Hamster number instead of agent<number>
  2. The agent number is printed on the Hamster cover

sub = rospy.Subscriber('/agent14/scan', LaserScan, callback)


rospy.spin()

</syntaxhighlight>

To execute save this example in example1.py and type in the terminal

<syntaxhighlight lang="bash" line='line'>

$ python example1.py

</syntaxhighlight>

Ex. 1