Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

python - A question about cross-module and callback function

I wrote a state flow program module with python In the test program I have two modules

fsm.py contains the achievement of the state machine and call.py is the script which call the fsm.py module

Brief

when I put some specific code in __main __, the program can run normally

error info

I implemented the callback function by creating a new process

Detail

error code

import fsm

def func_camera_conv():
    print( 'camera_conv done' )
def func_encoder_controll():
    print( 'func_encoder_controll done' )
def func_camera_controll():
    print( 'func_camera_controll done' )
def func_encoder_conv():
    print( 'func_encoder_conv done' )
def func_chassis_move():
    print( 'func_chassis_move done' )
def func_chassis_stop():
    print( 'func_chassis_stop done' )
def func_chassis_rough():
    print( 'func_chassis_rough done' )
def func_arm_start():
    print( 'func_arm_start done' )
def func_arm_done():
    print( 'func_arm_done done' )
def func_mission_done():
    print( 'func_mission_done done' )
def func_mission_start():
    print( 'func_mission_start done' )
def func_rough_done():
    print( 'func_rough_done done' )

def CameraConv():
    print( 'entry: CAMERA_CONV' )
def CameraDiv():
    print( 'entry: CAMERA_DIV' )
def EncoderConv():
    print( 'entry: ENCODER_CONV' )
def EncoderDiv():
    print( 'entry: ENCODER_DIV' )
def ChassisRough():
    print( 'entry: CHASSIS_ROUGH' )
def QueueCheck():
    print( 'entry: QUEUE_CHECK' )
def ChassisPause():
    print( 'entry: CHASSIS_PAUSE' )
def ChassisWaitArm():
    print( 'entry: CHASSIS_WAIT_ARM' )
def ArmReady():
    print( 'entry: ARM_READY' )
def ArmBusy():
    print( 'entry: ARM_BUSY' )
def TaskInit():
    print( 'entry: TASK_INIT' )
def GetMission():
    print( 'entry: GET_MISSION' )
def PublishMission():
    print( 'entry: PUBLISH_MISSION' )
def MissionDone():
    print( 'entry: MISSION_DONE' )


fsm.fsm.new_state( fsm.fsm.ROOT_STATE_NAME, 'ARM' )
fsm.fsm.new_state( fsm.fsm.ROOT_STATE_NAME, 'TASK_CONTROLL' )
fsm.fsm.new_state( fsm.fsm.ROOT_STATE_NAME, 'CHASSIS' )

fsm.fsm.new_state( 'ARM', 'ARM_READY', ArmReady )
fsm.fsm.new_state( 'ARM', 'ARM_BUSY', ArmBusy )

fsm.fsm.new_state( 'TASK_CONTROLL', 'PUBLISH_MISSION', PublishMission )
fsm.fsm.new_state( 'TASK_CONTROLL', 'TASK_INIT', TaskInit )
fsm.fsm.new_state( 'TASK_CONTROLL', 'GET_MISSION', GetMission )
fsm.fsm.new_state( 'TASK_CONTROLL', 'MISSION_DONE', MissionDone ) 

fsm.fsm.new_state( 'CHASSIS', 'CHASSIS_ROUGH', ChassisRough )
fsm.fsm.new_state( 'CHASSIS', 'CHASSIS_MOVE' )
fsm.fsm.new_state( 'CHASSIS', 'CHASSIS_STOP' )

fsm.fsm.new_state( 'CHASSIS_MOVE', 'CAMERA_CONTROLL' )
fsm.fsm.new_state( 'CHASSIS_MOVE', 'ENCODER_CONTROLL' )

fsm.fsm.new_state( 'ENCODER_CONTROLL', 'ENCODER_CONV', EncoderConv )
fsm.fsm.new_state( 'ENCODER_CONTROLL', 'ENCODER_DIV', EncoderDiv )

fsm.fsm.new_state( 'CHASSIS_STOP', 'CHASSIS_PAUSE', ChassisPause )
fsm.fsm.new_state( 'CHASSIS_STOP', 'QUEUE_CHECK', QueueCheck )
fsm.fsm.new_state( 'CHASSIS_STOP', 'CHASSIS_WAIT_ARM', ChassisWaitArm )

fsm.fsm.new_state( 'CAMERA_CONTROLL', 'CAMERA_CONV', CameraConv )
fsm.fsm.new_state( 'CAMERA_CONTROLL', 'CAMERA_DIV', CameraDiv )

fsm.fsm.set_state( 'ARM', 'ARM_READY' )
fsm.fsm.set_state( 'TASK_CONTROLL', 'TASK_INIT' )
fsm.fsm.set_state( 'CHASSIS', 'CHASSIS_STOP' )
fsm.fsm.set_state( 'CHASSIS_MOVE', 'ENCODER_CONTROLL' )
fsm.fsm.set_state( 'CHASSIS_STOP', 'QUEUE_CHECK' )
fsm.fsm.set_state( 'CAMERA_CONTROLL', 'CAMERA_DIV' )
fsm.fsm.set_state( 'ENCODER_CONTROLL', 'ENCODER_DIV' )

fsm.fsm.add_event( 'CAMERA_DIV', 'camera_conv', 'CAMERA_CONV', func_camera_conv )
fsm.fsm.add_event( 'CAMERA_CONTROLL', 'encoder_controll', 'ENCODER_CONTROLL', func_encoder_controll )
fsm.fsm.add_event( 'ENCODER_CONTROLL', 'camera_controll', 'CAMERA_CONTROLL', func_camera_controll )
fsm.fsm.add_event( 'ENCODER_DIV', 'encoder_conv', 'ENCODER_CONV', func_encoder_conv )
fsm.fsm.add_event( 'CHASSIS_STOP', 'chassis_move', 'CHASSIS_MOVE', func_chassis_move )

fsm.fsm.add_event( 'CHASSIS_MOVE', 'chassis_stop', 'CHASSIS_STOP', func_chassis_stop )
fsm.fsm.add_event( 'CHASSIS_ROUGH', 'encoder_conv', 'CHASSIS_STOP', func_encoder_conv )
fsm.fsm.add_event( 'CHASSIS_STOP', 'chassis_rough', 'CHASSIS_ROUGH', func_chassis_rough )
fsm.fsm.add_event( 'QUEUE_CHECK', 'arm_start', 'CHASSIS_WAIT_ARM', func_arm_start )
fsm.fsm.add_event( 'CHASSIS_WAIT_ARM', 'arm_done', 'CHASSIS_PAUSE', func_arm_done )

fsm.fsm.add_event( 'ARM_READY', 'arm_start', 'ARM_BUSY', func_arm_start )
fsm.fsm.add_event( 'ARM_BUSY', 'arm_done', 'ARM_READY', func_arm_done)
fsm.fsm.add_event( 'TASK_INIT', 'arm_done', 'GET_MISSION', func_arm_done )
fsm.fsm.add_event( 'GET_MISSION', 'mission_start', 'PUBLISH_MISSION', func_mission_start )
fsm.fsm.add_event( 'PUBLISH_MISSION', 'arm_done', 'PUBLISH_MISSION', func_arm_done )

fsm.fsm.add_event( 'PUBLISH_MISSION', 'mission_done', 'MISSION_DONE', func_mission_done )

fsm.fsm.start()

correct code

this file can run normally with __main __

import fsm

def func_camera_conv():
    print( 'camera_conv done' )
def func_encoder_controll():
    print( 'func_encoder_controll done' )
def func_camera_controll():
    print( 'func_camera_controll done' )
def func_encoder_conv():
    print( 'func_encoder_conv done' )
def func_chassis_move():
    print( 'func_chassis_move done' )
def func_chassis_stop():
    print( 'func_chassis_stop done' )
def func_chassis_rough():
    print( 'func_chassis_rough done' )
def func_arm_start():
    print( 'func_arm_start done' )
def func_arm_done():
    print( 'func_arm_done done' )
def func_mission_done():
    print( 'func_mission_done done' )
def func_mission_start():
    print( 'func_mission_start done' )
def func_rough_done():
    print( 'func_rough_done done' )

def CameraConv():
    print( 'entry: CAMERA_CONV' )
def CameraDiv():
    print( 'entry: CAMERA_DIV' )
def EncoderConv():
    print( 'entry: ENCODER_CONV' )
def EncoderDiv():
    print( 'entry: ENCODER_DIV' )
def ChassisRough():
    print( 'entry: CHASSIS_ROUGH' )
def QueueCheck():
    print( 'entry: QUEUE_CHECK' )
def ChassisPause():
    print( 'entry: CHASSIS_PAUSE' )
def ChassisWaitArm():
    print( 'entry: CHASSIS_WAIT_ARM' )
def ArmReady():
    print( 'entry: ARM_READY' )
def ArmBusy():
    print( 'entry: ARM_BUSY' )
def TaskInit():
    print( 'entry: TASK_INIT' )
def GetMission():
    print( 'entry: GET_MISSION' )
def PublishMission():
    print( 'entry: PUBLISH_MISSION' )
def MissionDone():
    print( 'entry: MISSION_DONE' )


if _name__ == '__main__':
    fsm.fsm.new_state( fsm.fsm.ROOT_STATE_NAME, 'ARM' )
    fsm.fsm.new_state( fsm.fsm.ROOT_STATE_NAME, 'TASK_CONTROLL' )
    fsm.fsm.new_state( fsm.fsm.ROOT_STATE_NAME, 'CHASSIS' )

    fsm.fsm.new_state( 'ARM', 'ARM_READY', ArmReady )
    fsm.fsm.new_state( 'ARM', 'ARM_BUSY', ArmBusy )

    fsm.fsm.new_state( 'TASK_CONTROLL', 'PUBLISH_MISSION', PublishMission )
    fsm.fsm.new_state( 'TASK_CONTROLL', 'TASK_INIT', TaskInit )
    fsm.fsm.new_state( 'TASK_CONTROLL', 'GET_MISSION', GetMission )
    fsm.fsm.new_state( 'TASK_CONTROLL', 'MISSION_DONE', MissionDone ) 

    fsm.fsm.new_state( 'CHASSIS', 'CHASSIS_ROUGH', ChassisRough )
    fsm.fsm.new_state( 'CHASSIS', 'CHASSIS_MOVE' )
    fsm.fsm.new_state( 'CHASSIS', 'CHASSIS_STOP' )

    fsm.fsm.new_state( 'CHASSIS_MOVE', 'CAMERA_CONTROLL' )
    fsm.fsm.new_state( 'CHASSIS_MOVE', 'ENCODER_CONTROLL' )

    fsm.fsm.new_state( 'ENCODER_CONTROLL', 'ENCODER_CONV', EncoderConv )
    fsm.fsm.new_state( 'ENCODER_CONTROLL', 'ENCODER_DIV', EncoderDiv )

    fsm.fsm.new_state( 'CHASSIS_STOP', 'CHASSIS_PAUSE', ChassisPause )
    fsm.fsm.new_state( 'CHASSIS_STOP', 'QUEUE_CHECK', QueueCheck )
    fsm.fsm.new_state( 'CHASSIS_STOP', 'CHASSIS_WAIT_ARM', ChassisWaitArm )

    fsm.fsm.new_state( 'CAMERA_CONTROLL', 'CAMERA_CONV', CameraConv )
    fsm.fsm.new_state( 'CAMERA_CONTROLL', 'CAMERA_DIV', CameraDiv )

    fsm.fsm.set_state( 'ARM', 'ARM_READY' )
    fsm.fsm.set_state( 'TASK_CONTROLL', 'TASK_INIT' )
    fsm.fsm.set_state( 'CHASSIS', 'CHASSIS_STOP' )
    fsm.fsm.set_state( 'CHASSIS_MOVE', 'ENCODER_CONTROLL' )
    fsm.fsm.set_state( 'CHASSIS_STOP', 'QUEUE_CHECK' )
    fsm.fsm.set_state( 'CAMERA_CONTROLL', 'CAMERA_DIV' )
    fsm.fsm.set_state( 'ENCODER_CONTROLL', 'ENCODER_DIV' )

    fsm.fsm.add_event( 'CAMERA_DIV', 'camera_conv', 'CAMERA_CONV', func_camera_conv )
    fsm.fsm.add_event( 'CAMERA_CONTROLL', 'encoder_controll', 'ENCODER_CONTROLL', func_encoder_controll )
    fsm.fsm.add_event( 'ENCODER_CONTROLL', 'camera_controll', 'CAMERA_CONTROLL', func_camera_controll )
    fsm.fsm.add_event( 'ENCODER_DIV', 'encoder_conv', 'ENCODER_CONV', func_encoder_conv )
    fsm.fsm.add_event( 'CHASSIS_STOP', 'chassis_move', 'CHASSIS_MOVE', func_chassis_move )

    fsm.fsm.add_event( 'CHASSIS_MOVE', 'chassis_stop', 'CHASSIS_STOP', func_chassis_stop )
    fsm.fsm.add_event( 'CHASSIS_ROUGH', 'encoder_conv', 'CHASSIS_STOP', func_encoder_conv )
    fsm.fsm.add_event( 'CHASSIS_STOP', 'chassis_rough', 'CHASSIS_ROUGH', func_chassis_rough )
    fsm.fsm.add_event( 'QUEUE_CHECK', 'arm_start', 'CHASSIS_WAIT_ARM', func_arm_start )
    fsm.fsm.add_event( 'CHASSIS_WAIT_ARM', 'arm_done', 'CHASSIS_PAUSE', func_arm_done )

    fsm.fsm.add_event( 'ARM_READY', 'arm_start', 'ARM_BUSY', func_arm_start )
    fsm.fsm.add_event( 'ARM_BUSY', 'arm_done', 'ARM_READY', func_arm_done)
    fsm.fsm.add_event( 'TASK_INIT', 'arm_done', 'GET_MISSION', func_arm_done )
    fsm.fsm.add_event( 'GET_MISSION', 'mission_start', 'PUBLISH_MISSION', func_mission_start )
    fsm.fsm.add_event( 'PUBLISH_MISSION', 'arm_done', 'PUBLISH_MISSION', func_arm_done )

    fsm.fsm.add_event( 'PUBLISH_MISSION', 'mission_done', 'MISSION_DONE', func_mission_done )

    fsm.fsm.start()

question from:https://stackoverflow.com/questions/65650293/a-question-about-cross-module-and-callback-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...