code structure with several callbacks
Hello,
I am working on a small 4WD rover. I use ultrasonic sensors, and a RPlidar for autonomous navigation and a joy for manual teleoperation. My aim is quite simple, but i am asking some questions about the right python script architecture.
The main idea is to have a loop which check joy inputs, and update both sonar and laser data Joy input decide the current mode ('AUTO' or 'MANUAL')
Thus I write this code :
#!/usr/bin/env python
import rospy
import time
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Joy
from rospy_tutorials.msg import Floats
from sensor_msgs.msg import LaserScan
## MODE
def auto_mode():
print( sonar_array[0] + sonar_array[1])
print( laser_array[0] + laser_array[1])
# detect obstacle
# compute path
## CALLBACKS
def callback_joy(data):
if data.buttons[0]<0.5 :
switchMode = 'AUTO'
else:
switchMode = 'MANUAL'
twist.linear.x = 4*data.axes[1]
twist.angular.z = 4*data.axes[0]
def callback_sonar(data):
sonar_array = data.data
#some data filtering and processing
def callback_laser(scan):
laser_array = scan.ranges
#some data filtering and processing
if __name__ == '__main__':
# initialisation
print("initialization ")
pub = rospy.Publisher('/cmd_vel',Twist,queue_size=5)
twist = Twist()
rospy.init_node('viva_node', anonymous=True)
rospy.Subscriber('joy', Joy, callback_joy)
rospy.Subscriber('sonar', Floats, callback_sonar)
rospy.Subscriber('/scan', LaserScan, callback_laser)
# loop
print('loop')
while True:
print('spin')
rospy.spin()
print 'Current Mode is : ', switch_mode
if switch_mode == 'AUTO':
auto_mode()
But, when i run, this script nothing append except :
pi@rasberrypi:~ $ rosrun perso_package viva_node_rev3.py
initialization
loop
spin
Then script get stuck and nothing append. I don't know why, any idea would are welcome.
Matt
A suggestion: use the teleop_twist_joy package instead of converting raw
Joy
msgs to Twists yourself.I need to switch between manual and auto mode from my joystick, so I am oblige to import raw Joy msgs. But maybe there is an other way