Ask Your Question

Problem while Running SMACH program

asked 2016-02-15 04:31:23 -0500

Niranjan gravatar image

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

#!/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'
            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():

    # 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(), 
        smach.StateMachine.add('BAR', Bar(), 

    # Execute SMACH plan
    outcome = sm.execute()

if __name__ == '__main__':

But when I execute it I am getting this error

Traceback (most recent call last):
  File "", 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....

edit retag flag offensive close merge delete



In a terminal, type python and then try: from smach import State Does that result in an error too?

spmaniato gravatar image spmaniato  ( 2016-02-15 14:15:17 -0500 )edit

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..

Niranjan gravatar image Niranjan  ( 2016-02-15 22:47:46 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2016-02-22 01:46:42 -0500

Niranjan gravatar image

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

edit flag offensive delete link more

answered 2020-06-04 10:42:11 -0500

pepelepew71 gravatar image

updated 2020-06-05 09:58:57 -0500

I have the same problem and from smach import State can not solve my problem. So, I print the smach package path.

import smach


I found that catkin_make create a redundant smach package in my workspace devel directory. Just delete it and solved.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-02-15 04:31:23 -0500

Seen: 602 times

Last updated: Jun 05 '20