Closed slyandsmart closed 6 months ago
I think i found a solution by myself how work within the python-statemachine in combination with ros2.
import rclpy
from rclpy.node import Node
from rclpy.executors import MultiThreadedExecutor
from std_msgs.msg import String
from statemachine import StateMachine, State
class MyStateMachine(StateMachine):
state_a = State(initial=True)
state_b = State()
state_c = State()
trigger = state_a.to(state_b,cond='condition_success') |state_a.to(state_c,cond='condition_success2') | state_b.to(state_c) | state_c.to(state_a)
class StateMachineNode(Node):
def __init__(self):
super().__init__('state_machine_node')
self.myfsm = MyStateMachine(self)
self.subscription = self.create_subscription(
String,
'state_transition',
self.state_transition_callback,
10)
def condition_success(self):
return self.new_state == 'StateA'
def condition_success2(self):
return self.new_state == 'StateB'
def state_transition_callback(self, msg):
self.new_state = msg.data
if self.new_state in ['StateA', 'StateB', 'StateC']:
self.myfsm.send('trigger')
self.get_logger().info(f'Transitioning trigger got it {self.new_state}')
def on_enter_state_a(self):
self.get_logger().info('Executing function for StateA')
def on_enter_state_b(self):
self.get_logger().info('Executing function for StateB')
def on_enter_state_c(self):
self.get_logger().info('Executing function for StateC')
def main(args=None):
rclpy.init(args=args)
node = StateMachineNode()
#fsm = MyStateMachine(node)
executor = MultiThreadedExecutor()
executor.add_node(node)
try:
node.get_logger().info('State machine node running')
executor.spin()
except KeyboardInterrupt:
node.get_logger().info('Shutting down')
rclpy.shutdown()
if __name__ == '__main__':
main()
This implementation allows me to use the trigger form a ros2 topic within the state machine. So my full functionality can be in the ros2 node.
Nice @slyandsmart . After reading your solution the fix was to "send" to the state machine the event name, not the name of the target state. Right? In your case, the event name is trigger
.
Changing this line;
self.my_fsm.send(new_state)
By this one:
self.myfsm.send('trigger')
Also, I think that you'll better served if the code surrounding the state machine don't know anything about the internal states.
So if you could refactor the event loop to receive the performed actions instead the desired state, your design will improve:
Something like this:
def state_transition_callback(self, msg):
event = msg.data
if event in self.myfsm.allowed_events:
self.myfsm.send(event)
self.get_logger().info(f'Transitioning trigger got it {event}. Current state is {self.myfsm.current_state.id}')
As you've resolved the problem, I'll close the issue for now. Feel free to reopen it or open another one.
Description
I want to use the state-machine to control the robots state in a simple pic and place example. But i tried to do a minimal example just to switch between states by subscribing a ros2 topic with an string message.
I get an error while transision. statemachine.exceptions.TransitionNotAllowed: Can't StateA when in StateA.
I would expect to get an log output in on of the states. It should transition. while i subscibe the message.