smach 상태 정의하기
Smach 상태 만들기
class Foo(smach.State):
def __init__(self, outcomes=['outcome1', 'outcome2']):
# Your state initialization goes here
def execute(self, userdata):
# Your state execution goes here
if xxxx:
return 'outcome1'
else:
return 'outcome2'
생성자 생성자 메서드에서 state 클래스를 초기화한다. 생성자에서 블록킹이 일어나지 않도록 주의해야한다. 만약 다른 시스템이 켜지길 기다려야 한다면 다른 쓰레드에서 처리해야한다.
execute execute 메소드에서 실제 동작이 이뤄진다. 원하는 코드를 실행할 수 있다. 블록킹도 가능하다. 이 메서드에서 값을 반환하면 현재 state가 완료된다.
outcome
state가 완료되면 outcome을 반환한다. 각 state는 연관된 여러 outcome을 가진다. outcome은 state가 어떻게 끝나야 하는지 묘사하는 유저 정의 문자열이다. 가능한 outcomes 집합은 예시로 succeeded
, failed
, awesome
정도가 있다. 다음 state로의 전환은 이전 state의 outcomes에 기반해 정해진다.
상태 머신에 상태 추가
상태 머신(state machine)은 여러 state를 보유할 수 있는 컨테이너다. state를 추가할 때 상태간의 전환(transition)을 지정한다.
# 상태머신 컨테이너에서 가능한 outcome들
sm = smach.StateMachine(outcomes=['outcome4','outcome5'])
with sm:
# 상태머신에 FOO라는 상태를 등록. state는 대문자 표기가 원칙
# 만약 outcome이 outcome1이면 BAR state로 전환.
smach.StateMachine.add('FOO', Foo(),
transitions={'outcome1':'BAR',
'outcome2':'outcome4'})
smach.StateMachine.add('BAR', Bar(),
transitions={'outcome2':'FOO'})
Leave a comment