I'm using the code below in an attempt to read laser data and determine which side of my robot is closest to a wall. The laser data is successfully being printed in the left and right callbacks but I try to assign both values to global variables and use those varibles in a third function. However, the varibales DO NOT print in my run() function:
#!/usr/bin/env python
import rospy
import math
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
from tf import transformations
from sensor_msgs.msg import LaserScan
subL_value = ''
subR_value = 'test'
def callbackLeft(msg) :
subL_value = min(msg.ranges[0:49])
def callbackRight(msg) :
global subR_value
subR_value = min(msg.ranges[0:49])
def run() :
# rospy.Subscriber('/scan_left', LaserScan, callbackLeft)
rospy.Subscriber('/scan_right', LaserScan, callbackRight)
# print('\tLeft: '),
# print(subL_value)
print('\tRight: '),
global subR_value
print(subR_value)
if __name__ == "__main__":
rospy.init_node('wall_detector')
run()
rospy.spin()
In the code above, I am only using the subR_value, Initiating it to a value of test, resetting it in the callbackRight subscriber callback function and attemoting to read the new value in the run() function.
However, running the script, I have 2 problems:
- The printed value is test and hence is no overwritten as expected
- The script does not loop only outputting a single Right: test output
I found this post which is a similar problem and also this one but I seem to be satisfying the necessary global variable labelling.
Am I missing something ?