SMACC2
st_state_1.hpp
Go to the documentation of this file.
1// Copyright 2021 MyName/MyCompany Inc.
2// Copyright 2021 RobosoftAI Inc. (template)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include "rclcpp/rclcpp.hpp"
19#include "smacc2/smacc.hpp"
20
21namespace $sm_name$
22{
23// SMACC2 clases
26
27// STATE MACHINE SHARED VARIABLES (used in this state)
28extern unsigned int _counter_;
29extern rclcpp::Time _start_time_;
30
31extern unsigned int _sum_of_iterations_;
32extern double _sum_of_elapsed_time_;
33
34// State constants
35constexpr unsigned int ITERATIONS_CHECK = 1000;
36
37// STATE DECLARATION
38struct State1 : smacc2::SmaccState<State1, $SmName$>
39{
40 using SmaccState::SmaccState;
41
42 // TRANSITION TABLE
43 typedef boost::mpl::list<
44
46
47 >
49
50 // STATE FUNCTIONS
51 static void staticConfigure() {}
52
54
55 void onEntry()
56 {
57 // NOTE: counter is updated in 'State2'
59 {
60 rclcpp::Duration elapsed = getNode()->now() - _start_time_;
61 double frequency_Hz = ITERATIONS_CHECK / elapsed.seconds();
63 _sum_of_elapsed_time_ += elapsed.seconds();
64 double global_frequency_Hz = _sum_of_iterations_ / _sum_of_elapsed_time_;
65
66 // Using fatal to override all logging restrictions.
67 RCLCPP_FATAL(
68 getLogger(), "Executed %u iterations in %lf seconds: %lf Hz. Longtime frequency: %lf Hz",
69 ITERATIONS_CHECK, elapsed.seconds(), frequency_Hz, global_frequency_Hz);
70
71 _counter_ = 1;
72 _start_time_ = getNode()->now();
73 }
74
75 this->postEvent<EvStateRequestFinish<State1>>();
76 }
77
78 void onExit() {}
79};
80} // namespace $sm_name$
rclcpp::Logger getLogger()
Definition: smacc_state.hpp:36
rclcpp::Node::SharedPtr & getNode()
Definition: smacc_state.hpp:34
static unsigned int _counter_
Definition: sm_name.hpp:30
double _sum_of_elapsed_time_
Definition: sm_name.hpp:34
rclcpp::Time _start_time_
Definition: sm_name.hpp:31
constexpr unsigned int ITERATIONS_CHECK
Definition: st_state_1.hpp:35
unsigned int _sum_of_iterations_
Definition: sm_name.hpp:33
boost::mpl::list< Transition< EvStateRequestFinish< State1 >, State2 > > reactions
Definition: st_state_1.hpp:48
static void staticConfigure()
Definition: st_state_1.hpp:51
void runtimeConfigure()
Definition: st_state_1.hpp:53