ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Variable is not updating in Ros service

asked 2021-06-09 16:40:36 -0500

Rida1 gravatar image

updated 2021-06-12 19:00:13 -0500

jayess gravatar image

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()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-06-13 10:36:26 -0500

James NT gravatar image

updated 2021-06-13 10:43:15 -0500

When you are calling your methods say, this one:

def placed_obj(req):
    if req.transition == 1:
        currentstatus = "at-loc-1"
    elif req.transition == 0:
        currentstatus = "START"
    else:
        rospy.loginfo("Invalid command")
    exit

You are actually creating a new variable called 'currentstatus' within that method and that only lasts for the life of that method. Try extending the scope of the external variable 'currentstatus' by using the global keyword. Like this:

def placed_obj(req):
    global current_status
    if req.transition == 1:
        currentstatus = "at-loc-1"
    elif req.transition == 0:
        currentstatus = "START"
    else:
        rospy.loginfo("Invalid command")
    exit

Doing this will allow you to modify a variable outside of the current scope.
You will need to do this in all the methods where you attempt to modify 'currentstatus'

There are other issues in your code such as the 'exit' oddity but your core question, I believe will be resolved by the use of the global keyword.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-06-09 16:40:36 -0500

Seen: 115 times

Last updated: Jun 13 '21