pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.68k stars 530 forks source link

Passing data - passing a string leads to error #433

Closed mat-l closed 4 years ago

mat-l commented 4 years ago

Dear all,

similar to the example Passing-data I have copied the code lines to my project.

Instead of temps or pressures I would like to hand over a string which is afterwards published to the ROS (Robot Operating System). After creating the statemachine object "sm" and performing the transition "starting" with the given data which should be passed, in this case the variable is "rospubmessage" and the value is a string "test", the pyhon script is throwing an error:

sm.starting(rospubmessage="test") Traceback (most recent call last): File "/usr/lib/python3.6/code.py", line 91, in runcode exec(code, self.locals) File "", line 1, in File "/home/mat/.local/lib/python3.6/site-packages/transitions/extensions/locking.py", line 90, in trigger return _super(LockedEvent, self).trigger(model, *args, kwargs) File "/home/mat/.local/lib/python3.6/site-packages/transitions/core.py", line 390, in trigger return self.machine._process(func) File "/home/mat/.local/lib/python3.6/site-packages/transitions/core.py", line 1109, in _process return trigger() File "/home/mat/.local/lib/python3.6/site-packages/transitions/core.py", line 408, in _trigger return self._process(event_data) File "/home/mat/.local/lib/python3.6/site-packages/transitions/core.py", line 417, in _process if trans.execute(event_data): File "/home/mat/.local/lib/python3.6/site-packages/transitions/core.py", line 269, in execute self._change_state(event_data) File "/home/mat/.local/lib/python3.6/site-packages/transitions/core.py", line 279, in _change_state event_data.machine.get_state(self.dest).enter(event_data) File "/home/mat/.local/lib/python3.6/site-packages/transitions/core.py", line 121, in enter event_data.machine.callbacks(self.on_enter, event_data) File "/home/mat/.local/lib/python3.6/site-packages/transitions/extensions/locking.py", line 176, in _locked_method return func(*args, *kwargs) File "/home/mat/.local/lib/python3.6/site-packages/transitions/core.py", line 1044, in callbacks self.callback(func, event_data) File "/home/mat/.local/lib/python3.6/site-packages/transitions/extensions/locking.py", line 176, in _locked_method return func(args, kwargs) File "/home/mat/.local/lib/python3.6/site-packages/transitions/core.py", line 1065, in callback func(*event_data.args, **event_data.kwargs) TypeError: rospublisher() got an unexpected keyword argument 'rospubmessage'

The code looks as follows:

class StatemachineClass(object):
    def __init__(self): self.set_environment()
    def set_environment(self, rospubmessage=str("1"), rossubmessage=str("2")):
        self.rospubmessage = rospubmessage
        self.rossubmessage = rossubmessage

    def print_rospubmessage(self): print("Current rospubmessage is: %s" % self.rospubmessage)
    def print_rossubmessage(self): print("Current rospubmessage is: %s" % self.rossubmessage)

    def rospublisher(self):
        print("the message is: " + str(self.rospubmessage))

## The states
states = [
    State(name='initial'),
    State(name='startup', on_enter=['rospublisher'],
    State('operation', on_enter=['rossubscriber']),
    State(name='shutdown', on_exit=['say_goodbye2'])
]

sm = StatemachineClass()
machine = Machine(sm, states, initial='initial')

machine.add_transition('starting', 'initial', 'startup', before='set_environment')
machine.add_transition('operating', 'startup', 'operation')
machine.add_transition('stopping', 'operation', 'initial')

Could you please help me with the error? Best regards matl

mat-l commented 4 years ago

Found a solution with respect to the documentation. :)