Write a simple state machine example with the Smach of Ros

Source: Internet
Author: User

Reference:

1. "Programming Robots with ROS--a practical INTRODUCTION to the ROBOT OPERATING SYSTEM" Morgan Quigley, Brian gerkey& William D. Smart book 13th chapter on Patrol

2.https://www.cnblogs.com/cv-pr/p/5155828.html

The recent use of Ros for task execution, and the deep understanding that the traditional method of implementation is extremely complex. For example, human face input work, including face detection, recognition, voice prompts, motion control, these sub-parts are basically through the subscription topic callback function processing, between the switch, such as face recognition, voice prompts between the switch, need to use the signal Volume control switch, when the multi-data fusion, logic is extremely chaotic, Also easily lead to redundant code, later maintenance is not easy to maintain. Deep understanding of how important a good implementation framework is. As his own experience, PR2 development, found that without a complete set of framework to perform tasks, will lead to code maintenance is particularly complicated, so the big boys to develop this set of Smach state machine function Pack. And the idea of state machine is conforming to the problem of multi-data fusion and multi-subtasks execution of robot. The advantage of the state machine is that it is simple to implement, easy to maintain, and easy to reconstruct task sequence. Recent implementations have proven that Smach is precisely tailored for mission execution.

So according to "programming Robots with ROS--a practical INTRODUCTION to the ROBOT OPERATING SYSTEM" in the 13th chapter on the patrol example, wrote a 3 states and 2 State machine with a status trigger condition, the state machine block diagram is as follows:

The Python implementation code is as follows:

1 #!/usr/bin/env python2 ImportRospy3  fromSmachImportState , StateMachine4  fromTimeImportSleep5 6 classOne (state):7   def __init__(self):8State.__init__(Self, outcomes=['Go',' Back' ])9     Ten   defExecute (Self, userdata): One     Print ' One' AConditions=input ("input cmd:") -Sleep (1) -     returnconditions the      - classBoth (state): -   def __init__(self): -State.__init__(Self, outcomes=['Go',' Back'  ]) +      -   defExecute (Self, userdata): +     Print ' Both' AConditions=input ("input cmd:") atSleep (1) -     returnconditions -      - classThree (state): -   def __init__(self): -State.__init__(Self, outcomes=['Go',' Back'  ]) in      -   defExecute (Self, userdata): to     Print 'three' +Conditions=input ("input cmd:") -Sleep (1) the     returnconditions *   $ if __name__=='__main__' :Panax NotoginsengSM = StateMachine (outcomes=['Go',' Back'  ]) - With SM: theStateMachine. Add' One', one (), transitions={'Go':' Both',' Back':'three'}) +StateMachine. Add' Both', both (), transitions={'Go':'three',' Back':' One'}) AStateMachine. Add'three', three (), transitions={'Go':' One',' Back':' Both'}) the      +Sm. Execute ()

The first is to create the state class One,two,three that we need to define by inheriting from the State class, as well as defining the trigger conditions for the status transitions, where two conditions ' go ', ' back ' are set.

then call Create An instance of the StateMachine class and joins the defined state class One,two,three to the instance, setting the transition relationship for the state.

Finally let the instance of the StateMachine class run up.

Running state machine at Terminal: $ python simple_fsm.py

Here is the result of the operation, everything and the design of the same, done!!

Write a simple state machine example with the Smach of Ros

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.