logger.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 RaccoonLab.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
17  */
18 
19 #include "logger.hpp"
20 
21 #include <Eigen/Geometry>
22 
23 #include "main.hpp"
24 #include "cs_converter.hpp"
25 
26 static const std::string COLOR_RED = "\033[1;31m";
27 static const std::string COLOR_GREEN = "\033[1;32m";
28 static const std::string COLOR_BOLD = "\033[1;29m";
29 static const std::string COLOR_TAIL = "\033[0m";
30 
31 static const float ROS_PUB_PERIOD_SEC = 0.05f;
32 
33 void StateLogger::init(double clockScale, double dt_secs) {
34  _clockScale = clockScale;
35  _dt_secs = dt_secs;
36 }
37 
38 void StateLogger::createStringStream(std::stringstream& logStream,
39  const Eigen::Vector3d& pose,
40  double dynamicsCounter,
41  double rosPubCounter,
42  double periodSec) {
43  auto& actuators = _actuators._actuators;
44  auto& maxDelayUsec = _actuators.maxDelayUsec_;
45  auto& actuatorsMsgCounter = _actuators.actuatorsMsgCounter_;
46  const auto& armed = _actuators.armed_;
47 
48  std::string arm_str = armed ? COLOR_GREEN + "[Armed]" + COLOR_TAIL : "[Disarmed]";
49  logStream << arm_str << ", ";
50 
51  logStream << _info.dynamicsName.c_str() << ". ";
52 
53  double dynamicsCompleteness = (double)dynamicsCounter * _dt_secs / (_clockScale * periodSec);
54  std::string dyn_str = "dyn=" + std::to_string(dynamicsCompleteness);
55  colorize(logStream, dynamicsCompleteness >= 0.9, dyn_str);
56  logStream << ", ";
57 
58  double rosPubCompleteness = (double)rosPubCounter * (double)ROS_PUB_PERIOD_SEC / (_clockScale * periodSec);
59  std::string ros_pub_str = "ros_pub=" + std::to_string(rosPubCompleteness);
60  colorize(logStream, rosPubCompleteness >= 0.9, ros_pub_str);
61  logStream << ", ";
62 
63  std::string actuator_str = "setpoint=" + std::to_string(actuatorsMsgCounter);
64  bool is_actuator_ok = actuatorsMsgCounter > 100 && maxDelayUsec < 20000 && maxDelayUsec != 0;
65  colorize(logStream, is_actuator_ok, actuator_str);
66  logStream << " msg/sec.\n";
67  actuatorsMsgCounter = 0;
68  maxDelayUsec = 0;
69 
70  addBold(logStream, "mc");
71  logStream << std::setprecision(2) << std::fixed << " ["
72  << actuators[0] << ", "
73  << actuators[1] << ", "
74  << actuators[2] << ", "
75  << actuators[3] << "] ";
76 
78  addBold(logStream, "fw rpy");
79  logStream << " [" << actuators[4] << ", "
80  << actuators[5] << ", "
81  << actuators[6] << "]";
82  addBold(logStream, " throttle");
83  logStream << " [" << actuators[7] << "] ";
84  }
85 
86  auto enuPosition = (_info.notation == DynamicsNotation_t::PX4_NED_FRD) ? Converter::nedToEnu(pose) : pose;
87  addBold(logStream, "enu pose");
88  logStream << std::setprecision(1) << std::fixed << " ["
89  << enuPosition[0] << ", "
90  << enuPosition[1] << ", "
91  << enuPosition[2] << "].";
92 }
93 
94 void StateLogger::colorize(std::stringstream& logStream, bool is_ok, const std::string& newData) {
95  if(!is_ok){
96  logStream << COLOR_RED << newData << COLOR_TAIL;
97  }else{
98  logStream << newData;
99  }
100 }
101 
102 void StateLogger::addBold(std::stringstream& logStream, const char* newData) {
103  logStream << COLOR_BOLD << newData << COLOR_TAIL;
104 }
std::string dynamicsName
Definition: dynamics.hpp:38
Actuators & _actuators
Definition: logger.hpp:40
uint64_t actuatorsMsgCounter_
Definition: actuators.hpp:38
Eigen::Vector3d nedToEnu(Eigen::Vector3d ned)
static const std::string COLOR_TAIL
Definition: logger.cpp:29
double _clockScale
Definition: logger.hpp:44
static const std::string COLOR_BOLD
Definition: logger.cpp:28
DynamicsInfo & _info
Definition: logger.hpp:42
DynamicsNotation_t notation
Definition: dynamics.hpp:44
static const std::string COLOR_GREEN
Definition: logger.cpp:27
bool armed_
Definition: actuators.hpp:44
std::vector< double > _actuators
Definition: actuators.hpp:34
double _dt_secs
Definition: logger.hpp:45
static void colorize(std::stringstream &logStream, bool is_ok, const std::string &newData)
Definition: logger.cpp:94
static const float ROS_PUB_PERIOD_SEC
Definition: logger.cpp:31
void init(double clockScale, double dt_secs)
Definition: logger.cpp:33
void createStringStream(std::stringstream &logStream, const Eigen::Vector3d &pose, double dynamicsCounter, double rosPubCounter, double periodSec)
Definition: logger.cpp:38
static void addBold(std::stringstream &logStream, const char *newData)
Definition: logger.cpp:102
static const std::string COLOR_RED
Definition: logger.cpp:26
uint64_t maxDelayUsec_
Definition: actuators.hpp:37
VehicleType vehicleType
Definition: dynamics.hpp:41


inno_vtol_dynamics
Author(s): Roman Fedorenko, Dmitry Ponomarev, Ezra Tal, Winter Guerra
autogenerated on Sat Jul 1 2023 02:13:44