ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I am going to post some code that I think can help you with your problem. If this does not align with what you want just let me know and I will update the answer. I wrote some comments in the code so you can understand what is doing. Here it is:
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
from geometry_msgs.msg import Twist
operating_mode = 0
vel_x = 0.0
vel_z = 0.0
# Callback for the operation mode, is just an integer that can be check with an if statement.
def mode_callback(mode):
global operating_mode
operating_mode = mode.data
# Callback for the vel command sent to move the platform.
def cmd_vel_callback(twist_cmd):
global vel_x, vel_z
vel_x = twist_cmd.linear.x # Extract linear component from Twist.
vel_z = twist_cmd.angular.z # Extract angular component from Twist.
# Main loop for ROS node.
def spin():
rospy.init_node('mode_operator') # Init the ROS node.
rate = rospy.Rate(10) # Rate at which the node is going to run.
rospy.Subscriber("mode", Int32, mode_callback) # Subscribe to the mode flag topic.
rospy.Subscriber("cmd_twist", Twist, cmd_vel_callback) # Subscribe to the cmd_vel topic to read commands.
# While ros is ok just continue working...
while not rospy.is_shutdown():
# Here you can implement and if statement that checks the operating_mode flag, like:
# if operating_mode == 0:
# mode is Manual
# elif operating_mode == 1:
# mode is Auto
# ...
# You have also here access to your cmd variables: vel_x, vel_z.
rospy.loginfo("Operating mode is: " + str(operating_mode) \
+ " and cmd_vel command is vel_x: " + str(vel_x) + " vel_z: " + str(vel_z))
rate.sleep() # Let the loop sleep to accomplish the rate wanted.
if __name__ == '__main__':
try:
spin()
except rospy.ROSInterruptException:
pass
Hope it can help you.
Regards.