我用python写了一段代码来做一些操作,这些操作必须是并发的,这就是为什么我把FSM画成两个并发的FSM的原因。在python中使用Fysom时,我想到了两个FSM的定义。但在某种程度上,我在想如何进行过渡;我在这里有点困惑,这就是为什么我请求您的支持。如果有人能帮忙,我将不胜感激。这是我定义fsms的方式,请告诉我这在Fysom中是否没有意义
fsm1 = fysom({
'initial':'Idle',
'events':[
{'name':'sc','src':'Idle','dst':'percent30'},
{'name':'sc','src':'percent30','dst':'percent60'},
{'name':'sc','src':'percent60','dst':'percent90'},
{'name':'sc','src':'percent90','dst':'Idle'}
],
'callbacks':{
'onIdle':onIdle,
'onRead':onpercent30,
'onTurnon':onpercent60,
'onDisplay':onpercent90
}
})
fsm2 = fysom({
'initial':'SR',
'events':[
{'name':'Right','src':'SR','dst':'SL'},
{'name':'Left','src':'SL','dst':'SR'}
],
'callbacks':{
'onIdle':onSR,
'onRead':onSL
}
}) 没有添加从fsm1到fsm2的转换,这是我感到困惑的部分,我想问一下,如果我想的是完全不可能的,我必须将两者合并在一起。
发布于 2015-06-05 04:04:38
如果你使用线程,它就能工作。
from fysom import Fysom
import threading
from termcolor import colored
import time
def onIdle(e):
print 'Fan is off'
def ons30(e):
print 'Fan is running at 30% speed'
def ons60(e):
print 'Fan is running at 60% speed'
def ons90(e):
print 'Fan is running at 90% speed'
def onSwingingRight(e):
if fsm1.current != "Idle":
print 'Fan is swinging right'
def onSwingingLeft(e):
if fsm1.current != "Idle":
print 'Fan is swinging left'
def onnoswinging(e):
if fsm1.current != "Idle":
print 'Fan is not swinging'
def onswinging(e):
if fsm1.current != "Idle":
print 'Fan is swinging'
def onMeasure(e):
print colored(' Measuring Temperature', 'red')
def onMedian(e):
print colored(' Finding the Median', 'red')
def onLCD(e):
print colored(' Updating the LCD', 'red')
fsm1 = Fysom(
{'initial': 'Idle','events':
[
{'name': 'sc', 'src': 'Idle', 'dst': 's30'},
{'name': 'sc', 'src': 's30', 'dst': 's60'},
{'name': 'sc', 'src': 's60', 'dst': 's90'},
{'name': 'sc', 'src': 's90', 'dst': 'Idle'}
],
'callbacks': {
'onIdle': onIdle,
'ons30': ons30,
'ons60': ons60,
'ons90': ons90}
})
fsm2 = Fysom(
{'initial': 'SwingingRight','events':
[
{'name': 'right', 'src': 'SwingingRight', 'dst': 'SwingingLeft'},
{'name': 'left', 'src': 'SwingingLeft', 'dst': 'SwingingRight'}
],
'callbacks': {
'onSwingingRight': onSwingingRight,
'onSwingingLeft': onSwingingLeft
}
})
fsm3 = Fysom(
{'initial': 'noswinging','events':
[
{'name': 'swing', 'src': 'noswinging', 'dst': 'swinging'},
{'name': 'swing', 'src': 'swinging', 'dst': 'noswinging'}
],
'callbacks': {
'onnoswinging': onnoswinging,
'onswinging': onswinging
}
})
fsm4 = Fysom(
{'initial': 'Measure','events':
[
{'name': 'timeup', 'src': 'Measure', 'dst': 'Median'},
{'name': 'l', 'src': 'Median', 'dst': 'LCD'},
{'name': 'l', 'src': 'LCD', 'dst': 'Measure'}
],
'callbacks': {
'onMeasure': onMeasure,
'onMedian': onMedian,
'onLCD':onLCD
}
})
def controlThreadLoop():
while True:
input = raw_input(">>>>> \n")
if input == 'sc':
fsm1.sc()
elif input == 'swing':
fsm3.swing()
elif input == 'left':
fsm2.left()
elif input =='right':
fsm2.right()
def monitoringThreadLoop():
while True:
time.sleep(5.0)
fsm4.timeup()
fsm4.l()
fsm4.l()
controlThread = threading.Thread(target=controlThreadLoop)
monitoringThread = threading.Thread(target=monitoringThreadLoop)
controlThread.start();
monitoringThread.start();https://stackoverflow.com/questions/30650821
复制相似问题