Difference between revisions of "HamsterPython"

From cogniteam
Jump to: navigation, search
Line 20: Line 20:
  
 
def is_free(ranges ,start_index, end_index ,min_distance):
 
def is_free(ranges ,start_index, end_index ,min_distance):
 +
#sub array from angle to angle
 
s_ranges = ranges[start_index:end_index]
 
s_ranges = ranges[start_index:end_index]
b_ranges = [i for i in s_ranges if i <= min_distance]
+
#filter the array
if len(b_ranges) > 1 :print "BLOCKED"
+
b_ranges = filter(lambda x: x <= min_distance, s_ranges)
 +
if len(b_ranges) > 1 : print "BLOCKED"
 
else : print "FREE"
 
else : print "FREE"
  
Line 34: Line 36:
 
#The agent number is printed on the Hamster cover  
 
#The agent number is printed on the Hamster cover  
 
sub = rospy.Subscriber('/agent14/scan', LaserScan, callback)
 
sub = rospy.Subscriber('/agent14/scan', LaserScan, callback)
 +
 +
 
rospy.spin()
 
rospy.spin()
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 13:54, 19 March 2019

Python setup

  • Note you are on the HamsterNet

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

$ export ROS_IP=<your IP>

</syntaxhighlight>

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

$ export ROS_MASTER_URI=http://10.0.2.<HAMSTER AGENT Number>:11311

</syntaxhighlight>


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 LaserScan

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) if len(b_ranges) > 1 : print "BLOCKED" else : print "FREE"


def callback(msg): is_free(msg.ranges,160,200,0.2)

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>