AttributeError: type object 'State' has no attribute 'START'
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"]
Can you please update your question with a copy/paste of the full error?
Updated it
Is this a full copy of your code? I see
my_fsm.run()
but no definition of therun
methodThere is a pypi finite state machine here which you could just import. Also, you might like to rethink your placement of
rospy.spin()