Variable is not updating in Ros service
Hello,
I have made this simple status updating ros service code . The problem is that i don't want the variable "currentstatus" return to it's old value ( i.e currentstatus = "START") once it's changed (because this way, it again starts from beginning and disturbs the sequence). The problem maybe is that everytime the if main loop starts, the currentstatus goes back to its value "START" But i don't know how to manage this.
import rospy
from std_msgs.msg import String
from ros_training_4.srv import *
def handle_trigger_transition(req):
rospy.loginfo("Processing request...")
update = 1
if currentstatus == "START":
START(req)
elif currentstatus == "at-loc-1":
at_loc_1(req)
elif currentstatus == "at-loc-2":
at_loc_2(req)
elif currentstatus == "grasped-obj":
grasped_obj(req)
elif currentstatus == "placed-obj":
placed_obj(req)
else:
rospy.loginfo("Status not found")
rospy.spin()
def START(req):
if req.transition == 1:
rospy.loginfo("move-1")
currentstatus = "at-loc-1"
elif req.transition == 2:
rospy.loginfo("move-2")
currentstatus = "at-loc-2"
elif req.transition == 0:
rospy.loginfo("Reset")
currentstatus = "START"
else:
rospy.loginfo("Invalid command")
pub.publish(currentstatus)
rospy.spin()
def at_loc_1(req):
if req.transition == 2:
rospy.loginfo("move-2")
currentstatus = "at-loc-2"
elif req.transition == 0:
rospy.loginfo("Reset")
currentstatus = "START"
else:
rospy.loginfo("Invalid command")
exit
def at_loc_2(req):
if req.transition == 3:
rospy.loginfo("grasp")
currentstatus = "grasped-obj"
elif req.transition == 0:
rospy.loginfo("Reset")
currentstatus = "START"
else:
rospy.loginfo("Invalid command")
exit
def grasped_obj(req):
if req.transition == 4:
rospy.loginfo("place")
currentstatus = "placed-obj"
elif req.transition == 0:
rospy.loginfo("Reset")
currentstatus = "START"
else:
rospy.loginfo("Invalid command")
def placed_obj(req):
if req.transition == 1:
currentstatus = "at-loc-1"
elif req.transition == 0:
currentstatus = "START"
else:
rospy.loginfo("Invalid command")
exit
def TriggerTransitionServer():
rate = rospy.Rate(2)
s = rospy.Service('transition', MyService, handle_trigger_transition)
while not rospy.is_shutdown():
pub.publish(currentstatus)
rate.sleep()
rospy.spin()
)
if __name__ == '__main__':
if not('update' in locals()):
currentstatus = "START"
rospy.init_node('fsm')
pub = rospy.Publisher('state', String, queue_size=10)
TriggerTransitionServer()