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

AttributeError: type object 'State' has no attribute 'START'

asked 2021-06-12 16:48:18 -0500

Rida1 gravatar image

updated 2021-06-13 16:47:48 -0500

jayess gravatar image

Hello,

I am trying to create a simple finite state machine.

import rospy
# from std_msgs.msg import String
from ros_training_4.srv import MyService
from ros_training_4.msg import State

class MyFsm:
    def __init__(self):
        self.pub = rospy.Publisher('state', State, queue_size=10)
        self.server = rospy.Service('transition', MyService, self.handle_trigger_transition)
        #self.duration = rospy.get_param("Duration")
        self.timer = rospy.Timer(rospy.Duration(0.5), self.my_callback(self))
        self.state = State.START
        # TODO: make parameter

    def my_callback(self, event):
        #self.pub.publish(self.state)
        rospy.spin()

    def handle_trigger_transition(self, req):
        rospy.loginfo("Processing request...")
        if self.state == State.START:
            Start()
        elif self.state == State.AT_LOC_1:
            at_loc_1()
        elif self.state == State.AT_LOC_2:
            at_loc_2()
        elif self.state == State.GRASPED_OBJ:
            grasped_obj()
        elif self.state == State.PLACED_OBJ:
            placed_obj()

        else:
             rospy.loginfo("Status not found")

    def Start(self, req):

        if req.transition == 1:
            rospy.loginfo("move_1")
            self.state = State.AT_LOC_1

        elif req.transition == 2:
            rospy.loginfo("move-2")
            self.state = State.AT_LOC_2

        elif req.transition == 0:
            rospy.loginfo("Reset")
            self.state = State.START

        else:
            rospy.loginfo("Invalid command")

    def at_loc_1(self, req):
        if req.transition == 2:
            rospy.loginfo("move-2")
            self.state = State.AT_LOC_2
        elif req.transition == 0:
            rospy.loginfo("Reset")
            self.state = State.START
        else:
            rospy.loginfo("Invalid command")


    def at_loc_2(self, req):
        if req.transition == 3:
            rospy.loginfo("grasp")
            self.state = State.GRASPED_OBJ
        elif req.transition == 0:
            rospy.loginfo("Reset")
            self.state = State.START
        else:
            rospy.loginfo("Invalid command")


    def grasped_obj(self, req):
        if req.transition == 4:
            rospy.loginfo("Place")
            self.state = State.PLACED_OBJ
        elif req.transition == 0:
            rospy.loginfo("Reset")
            self.state = State.START
        else:
            rospy.loginfo("Invalid command")

    def placed_obj(self, req):
        if req.transition == 1:
            self.state = State.AT_LOC_1
        elif req.transition == 0:
            self.state = State.START
        else:
            rospy.loginfo("Invalid command")


if __name__ == '__main__':

    rospy.init_node('fsm')
    #my_fsm = MyFsm()
    #my_fsm.run()
    MyFsm()

My State.msg file looks like this

uint16 state
uint16 INVALID=0
uint16 START=1
uint16 AT_LOC_1=2
uint16 AT_LOC_2=3
uint16 GRASPED_OBJ=4
uint16 PLACED_OBJ=5

At running this code i get the mentioned error

[ERROR] [1623611350.368994]: Error processing request: 'MyFsm' object has no attribute 'state'
['Traceback (most recent call last):\n', '  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros_service.py", line 632, in _handle_request\n    response = convert_return_to_response(self.handler(request), self.response_class)\n', '  File "/home/rida/catkin_ws/src/ros_training_4/nodes/TriggerTransition.py", line 124, in handle_trigger_transition\n    if self.state == State.START:\n', "AttributeError: 'MyFsm' object has no attribute 'state'\n"]
edit retag flag offensive close merge delete

Comments

Can you please update your question with a copy/paste of the full error?

jayess gravatar image jayess  ( 2021-06-12 17:32:45 -0500 )edit

Updated it

Rida1 gravatar image Rida1  ( 2021-06-12 17:36:28 -0500 )edit

Is this a full copy of your code? I see my_fsm.run() but no definition of the run method

jayess gravatar image jayess  ( 2021-06-12 18:43:59 -0500 )edit

There is a pypi finite state machine here which you could just import. Also, you might like to rethink your placement of rospy.spin()

James NT gravatar image James NT  ( 2021-06-13 18:54:13 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2021-06-13 16:04:09 -0500

Rida1 gravatar image

updated 2021-06-13 16:46:55 -0500

jayess gravatar image

I have resolved the issue.. I just changed the sequence of the code.

Previously:

class MyFsm: 
    def __init__(self): 
        self.pub = rospy.Publisher('state', State, queue_size=10) 
        self.server = rospy.Service('transition', MyService, self.handle_trigger_transition) 
        #self.duration = rospy.get_param("Duration") 
        self.timer = rospy.Timer(rospy.Duration(0.5), self.my_callback(self)) 
        self.state = State.START

Now:

class MyFsm:
    def __init__(self):
        self.pub = rospy.Publisher('state', State, queue_size=10)
        self.state = State.START
        self.server = rospy.Service('transition', MyService, self.handle_trigger_transition)
        self.timer = rospy.Timer(rospy.Duration(0.5), self.my_callback)

It solved the issue..!

edit flag offensive delete link more
0

answered 2021-06-12 17:46:37 -0500

jayess gravatar image

In your handle_trigger_transition method, you have

if state == START.State:

but the variable state is not defined, you probably want to use self.state or req.state instead. You also use START.State instead of State.START and STATE.AT_LOC_2 instead of State.AT_LOC_2

There's a few times that State is written as STATE.

Also, it appears that in a few instances you're trying to access methods without using self . You should use self.<instance-method>() instead.

edit flag offensive delete link more

Comments

Hey thanks.. actually that was mistakenly altered. I have updated my original code. I'm still having the attribute error... I think it can't import the module ros_training_4.msg. What can i do? I have checked everything. Cmake.. Package.xml etc

Rida1 gravatar image Rida1  ( 2021-06-13 14:07:25 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-06-12 16:48:18 -0500

Seen: 1,002 times

Last updated: Jun 13 '21