0
$\begingroup$

Rosanswers logo

Hi,
I am trying out the tutorials on SMACH in ros-indigo.
I am trying to run this following SMACH program state_machine_simple.py

#!/usr/bin/env python

import roslib; roslib.load_manifest('smach_tutorials')
import rospy
import smach
import smach_ros

# define state Foo
class Foo(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['outcome1','outcome2'])
        self.counter = 0

    def execute(self, userdata):
        rospy.loginfo('Executing state FOO')
        if self.counter < 3:
            self.counter += 1
            return 'outcome1'
        else:
            return 'outcome2'


# define state Bar
class Bar(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['outcome2'])

    def execute(self, userdata):
        rospy.loginfo('Executing state BAR')
        return 'outcome2'
        
# main
def main():
    rospy.init_node('smach_example_state_machine')

    # Create a SMACH state machine
    sm = smach.StateMachine(outcomes=['outcome4', 'outcome5'])

    # Open the container
    with sm:
        # Add states to the container
        smach.StateMachine.add('FOO', Foo(), 
                               transitions={'outcome1':'BAR', 
                                            'outcome2':'outcome4'})
        smach.StateMachine.add('BAR', Bar(), 
                               transitions={'outcome2':'FOO'})

    # Execute SMACH plan
    outcome = sm.execute()


if __name__ == '__main__':
    main()

But when I execute it I am getting this error

Traceback (most recent call last):
  File "state_machine_simple.py", line 9, in <module>
    class Foo(smach.State):
AttributeError: 'module' object has no attribute 'State'

I don't know how to solve this error... kindly help me out....


Originally posted by Niranjan on ROS Answers with karma: 51 on 2016-02-15

Post score: 0


Original comments

Comment by spmaniato on 2016-02-15:
In a terminal, type python and then try: from smach import State Does that result in an error too?

Comment by Niranjan on 2016-02-15:
Hi, I tried that its not showing error.... So I tried including from smach import State statement in the program... now its working fine.. thank you..

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Adding from smach import State statement in the program solves this problem


Originally posted by Niranjan with karma: 51 on 2016-02-22

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.