SMACC2
st_state_1.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
15//
16// Author: Denis Štogl (template)
17//
18
19#pragma once
20
21#include "rclcpp/rclcpp.hpp"
22#include "smacc2/smacc.hpp"
23
25{
26// SMACC2 clases
29
30// STATE MACHINE SHARED VARIABLES (used in this state)
31extern unsigned int _counter_;
32extern std::shared_ptr<rclcpp::Node> _node_;
33extern rclcpp::Time _start_time_;
34
35extern unsigned int _sum_of_iterations_;
36extern double _sum_of_elapsed_time_;
37
38// State constants
39constexpr unsigned int ITERATIONS_CHECK = 100;
40
41// STATE DECLARATION
42struct State1 : smacc2::SmaccState<State1, SmCoretestTransitionSpeed1>
43{
44 using SmaccState::SmaccState;
45
46 // TRANSITION TABLE
47 typedef boost::mpl::list<
48
50
51 >
53
54 // STATE FUNCTIONS
55 static void staticConfigure() {}
56
58
59 void onEntry()
60 {
61 // NOTE: counter is updated in 'State2'
63 {
64 rclcpp::Duration elapsed = _node_->now() - _start_time_;
65 double frequency_Hz = ITERATIONS_CHECK / elapsed.seconds();
67 _sum_of_elapsed_time_ += elapsed.seconds();
68 double global_frequency_Hz = _sum_of_iterations_ / _sum_of_elapsed_time_;
69
70 // Using fatal to override all logging restrictions.
71 RCLCPP_FATAL(
72 _node_->get_logger(),
73 "Executed %u iterations in %lf seconds: %lf Hz. Longtime frequency: %lf Hz",
74 ITERATIONS_CHECK, elapsed.seconds(), frequency_Hz, global_frequency_Hz);
75
76 _counter_ = 1;
77 _start_time_ = _node_->now();
78 }
79
80 this->postEvent<EvStateRequestFinish<State1>>();
81 }
82
83 void onExit() {}
84};
85} // namespace sm_coretest_transition_speed_1
std::shared_ptr< rclcpp::Node > _node_
constexpr unsigned int ITERATIONS_CHECK
Definition: st_state_1.hpp:39
boost::mpl::list< Transition< EvStateRequestFinish< State1 >, State2 > > reactions
Definition: st_state_1.hpp:52