Skip to content

Commit

Permalink
is_running added to state
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzs13 committed Nov 1, 2024
1 parent e40d254 commit 91a50d6
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 14 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,11 @@ int main(int argc, char *argv[]) {
std::initializer_list<std::string>{"outcome4"});

// cancel state machine on ROS 2 shutdown
rclcpp::on_shutdown([sm]() { sm->cancel_state(); });
rclcpp::on_shutdown([sm]() {
if (sm->is_running()) {
sm->cancel_state();
}
});

// add states
sm->add_state("FOO", std::make_shared<FooState>(),
Expand Down Expand Up @@ -895,7 +899,11 @@ int main(int argc, char *argv[]) {
std::initializer_list<std::string>{"outcome4"});

// cancel state machine on ROS 2 shutdown
rclcpp::on_shutdown([sm]() { sm->cancel_state(); });
rclcpp::on_shutdown([sm]() {
if (sm->is_running()) {
sm->cancel_state();
}
});

// add states
sm->add_state("SETTING_INTS",
Expand Down Expand Up @@ -1053,7 +1061,11 @@ int main(int argc, char *argv[]) {
std::initializer_list<std::string>{"outcome4"});

// cancel state machine on ROS 2 shutdown
rclcpp::on_shutdown([sm]() { sm->cancel_state(); });
rclcpp::on_shutdown([sm]() {
if (sm->is_running()) {
sm->cancel_state();
}
});

// add states
sm->add_state("CALLING_FIBONACCI", std::make_shared<FibonacciState>(),
Expand Down Expand Up @@ -1177,7 +1189,11 @@ int main(int argc, char *argv[]) {
std::initializer_list<std::string>{"outcome4"});

// cancel state machine on ROS 2 shutdown
rclcpp::on_shutdown([sm]() { sm->cancel_state(); });
rclcpp::on_shutdown([sm]() {
if (sm->is_running()) {
sm->cancel_state();
}
});

// add states
sm->add_state("PRINTING_ODOM", std::make_shared<PrintOdometryState>(5),
Expand Down
2 changes: 2 additions & 0 deletions yasmin/include/yasmin/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class State {

private:
std::atomic_bool canceled{false};
std::atomic_bool running{false};

public:
State(std::set<std::string> outcomes);
Expand All @@ -53,6 +54,7 @@ class State {
this->canceled.store(true);
};
bool is_canceled() const;
bool is_running() const;

std::set<std::string> const &get_outcomes();

Expand Down
4 changes: 4 additions & 0 deletions yasmin/src/yasmin/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ State::operator()(std::shared_ptr<blackboard::Blackboard> blackboard) {
YASMIN_LOG_DEBUG("Executing state '%s'", this->to_string().c_str());

this->canceled.store(false);
this->running.store(true);
std::string outcome = this->execute(blackboard);

if (std::find(this->outcomes.begin(), this->outcomes.end(), outcome) ==
Expand Down Expand Up @@ -59,9 +60,12 @@ State::operator()(std::shared_ptr<blackboard::Blackboard> blackboard) {
"'. The possible outcomes are: " + outcomes_string.c_str());
}

this->running.store(false);
return outcome;
}

bool State::is_canceled() const { return this->canceled.load(); }

bool State::is_running() const { return this->running.load(); }

std::set<std::string> const &State::get_outcomes() { return this->outcomes; }
1 change: 0 additions & 1 deletion yasmin/tests/python/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def execute(self, blackboard):
class TestState(unittest.TestCase):

def setUp(self):

self.state = FooState()

def test_state_call(self):
Expand Down
6 changes: 6 additions & 0 deletions yasmin/yasmin/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class State(ABC):

def __init__(self, outcomes: Set[str]) -> None:
self._outcomes = set()
self._running = False
self._canceled = False

if outcomes:
Expand All @@ -38,6 +39,7 @@ def __call__(self, blackboard: Blackboard = None) -> str:
yasmin.YASMIN_LOG_DEBUG(f"Executing state '{self}'")

self._canceled = False
self._running = True

if blackboard is None:
blackboard = Blackboard()
Expand All @@ -49,6 +51,7 @@ def __call__(self, blackboard: Blackboard = None) -> str:
f"Outcome '{outcome}' does not belong to the outcomes of the state '{self}'. The possible outcomes are: {self._outcomes}"
)

self._running = False
return outcome

@abstractmethod
Expand All @@ -65,5 +68,8 @@ def cancel_state(self) -> None:
def is_canceled(self) -> bool:
return self._canceled

def is_running(self) -> bool:
return self._running

def get_outcomes(self) -> Set[str]:
return self._outcomes
6 changes: 5 additions & 1 deletion yasmin_demos/src/action_client_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ int main(int argc, char *argv[]) {
std::initializer_list<std::string>{"outcome4"});

// cancel state machine on ROS 2 shutdown
rclcpp::on_shutdown([sm]() { sm->cancel_state(); });
rclcpp::on_shutdown([sm]() {
if (sm->is_running()) {
sm->cancel_state();
}
});

// add states
sm->add_state("CALLING_FIBONACCI", std::make_shared<FibonacciState>(),
Expand Down
6 changes: 5 additions & 1 deletion yasmin_demos/src/monitor_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ int main(int argc, char *argv[]) {
std::initializer_list<std::string>{"outcome4"});

// cancel state machine on ROS 2 shutdown
rclcpp::on_shutdown([sm]() { sm->cancel_state(); });
rclcpp::on_shutdown([sm]() {
if (sm->is_running()) {
sm->cancel_state();
}
});

// add states
sm->add_state("PRINTING_ODOM", std::make_shared<PrintOdometryState>(5),
Expand Down
6 changes: 5 additions & 1 deletion yasmin_demos/src/service_client_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ int main(int argc, char *argv[]) {
std::initializer_list<std::string>{"outcome4"});

// cancel state machine on ROS 2 shutdown
rclcpp::on_shutdown([sm]() { sm->cancel_state(); });
rclcpp::on_shutdown([sm]() {
if (sm->is_running()) {
sm->cancel_state();
}
});

// add states
sm->add_state("SETTING_INTS",
Expand Down
6 changes: 5 additions & 1 deletion yasmin_demos/src/yasmin_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ int main(int argc, char *argv[]) {
std::initializer_list<std::string>{"outcome4"});

// cancel state machine on ROS 2 shutdown
rclcpp::on_shutdown([sm]() { sm->cancel_state(); });
rclcpp::on_shutdown([sm]() {
if (sm->is_running()) {
sm->cancel_state();
}
});

// add states
sm->add_state("FOO", std::make_shared<FooState>(),
Expand Down
3 changes: 2 additions & 1 deletion yasmin_demos/yasmin_demos/action_client_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def main():
outcome = sm(blackboard)
yasmin.YASMIN_LOG_INFO(outcome)
except KeyboardInterrupt:
sm.cancel_state()
if sm.is_running():
sm.cancel_state()

# shutdown ROS 2
if rclpy.ok():
Expand Down
3 changes: 2 additions & 1 deletion yasmin_demos/yasmin_demos/monitor_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def main():
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except KeyboardInterrupt:
sm.cancel_state()
if sm.is_running():
sm.cancel_state()

# shutdown ROS 2
if rclpy.ok():
Expand Down
3 changes: 2 additions & 1 deletion yasmin_demos/yasmin_demos/nav_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def main():

# shutdown ROS 2
if rclpy.ok():
rclpy.shutdown()
if sm.is_running():
rclpy.shutdown()


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion yasmin_demos/yasmin_demos/service_client_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def main():
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except KeyboardInterrupt:
sm.cancel_state()
if sm.is_running():
sm.cancel_state()

# shutdown ROS 2
if rclpy.ok():
Expand Down
3 changes: 2 additions & 1 deletion yasmin_demos/yasmin_demos/yasmin_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def main():
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except KeyboardInterrupt:
sm.cancel_state()
if sm.is_running():
sm.cancel_state()

# shutdown ROS 2
if rclpy.ok():
Expand Down

0 comments on commit 91a50d6

Please sign in to comment.