SMACC2
st_state_2.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15namespace sm_ferrari
16{
17using namespace std::chrono_literals;
18
19// STATE DECLARATION
21{
22 using SmaccState::SmaccState;
23
24 // DECLARE CUSTOM OBJECT TAGS
25 struct TIMEOUT : ABORT{};
26 struct NEXT : SUCCESS{};
27 struct PREVIOUS : ABORT{};
28
29 // TRANSITION TABLE
30 typedef mpl::list<
31
32 // Transition<EvTimer<CbTimerCountdownOnce, OrTimer>, StState3, TIMEOUT>,
33 // Transition<EvAllGo<SrAllEventsGo>, StState3>,
34 // Keyboard events
35 Transition<EvKeyPressP<CbDefaultKeyboardBehavior, OrKeyboard>, StState1, PREVIOUS>,
36 Transition<EvKeyPressN<CbDefaultKeyboardBehavior, OrKeyboard>, StState3, NEXT>,
37 Transition<EvMyBehavior<CbMySubscriberBehavior, OrSubscriber>, StState3, NEXT>,
38 Transition<EvTrue<EgConditionalGenerator, StState2>, StState3, NEXT>
39
41
42 static int k;
43
44 // STATE FUNCTIONS
45 static void staticConfigure()
46 {
47 // configure_orthogonal<OrTimer, CbTimerCountdownOnce>(10);
48 // configure_orthogonal<OrSubscriber, CbWatchdogSubscriberBehavior>();
49 configure_orthogonal<OrSubscriber, CbMySubscriberBehavior>();
50 // configure_orthogonal<OrUpdatablePublisher, CbDefaultPublishLoop>();
51 configure_orthogonal<OrKeyboard, CbDefaultKeyboardBehavior>();
52
53 // Create State Reactor
54 // auto sbAll = static_createStateReactor<SrAllEventsGo>();
55 // sbAll->addInputEvent<EvKeyPressA<CbDefaultKeyboardBehavior, OrKeyboard>>();
56 // sbAll->addInputEvent<EvKeyPressB<CbDefaultKeyboardBehavior, OrKeyboard>>();
57 // sbAll->addInputEvent<EvKeyPressC<CbDefaultKeyboardBehavior, OrKeyboard>>();
58 // sbAll->setOutputEvent<EvAllGo<SrAllEventsGo>>();
59
60 static_createEventGenerator<EgConditionalGenerator>(ConditionalGeneratorMode::ON_UPDATE);
61 }
62
64 {
65 k = 0;
66
67 auto eg = this->getEventGenerator<EgConditionalGenerator>();
68 eg->setPredicateFunction([=] { return this->eventGeneratorPredicate(this); });
69 }
70
71 bool eventGeneratorPredicate(ISmaccState * st)
72 {
73 auto res = k > 300;
74 RCLCPP_INFO(st->getLogger(), "[State2] checking k: %d > 300 == %d", k, res);
75 rclcpp::sleep_for(10ms);
76 // rclcpp::Duration(10ms).sleep();
77 return res;
78 }
79
80 void update() override
81 {
82 k++;
83 RCLCPP_INFO(getLogger(), "[State2] internally updating k: %d", k);
84 }
85
86 void onEntry() { RCLCPP_INFO(getLogger(), "On Entry!"); }
87
88 void onExit() { RCLCPP_INFO(getLogger(), "On Exit!"); }
89};
90
91int StState2::k = 0;
92} // namespace sm_ferrari
rclcpp::Logger getLogger()
Definition: smacc_state.hpp:36
void update() override
Definition: st_state_2.hpp:80
bool eventGeneratorPredicate(ISmaccState *st)
Definition: st_state_2.hpp:71
static void staticConfigure()
Definition: st_state_2.hpp:45
mpl::list< Transition< EvKeyPressP< CbDefaultKeyboardBehavior, OrKeyboard >, StState1, PREVIOUS >, Transition< EvKeyPressN< CbDefaultKeyboardBehavior, OrKeyboard >, StState3, NEXT >, Transition< EvMyBehavior< CbMySubscriberBehavior, OrSubscriber >, StState3, NEXT >, Transition< EvTrue< EgConditionalGenerator, StState2 >, StState3, NEXT > > reactions
Definition: st_state_2.hpp:40