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_YELLOW = "\033[1;33m";
29 static const std::string COLOR_BOLD = "\033[1;29m";
30 static const std::string COLOR_TAIL = "\033[0m";
31 
32 static const float ROS_PUB_PERIOD_SEC = 0.05f;
33 
34 void StateLogger::init(double clockScale, double dt_secs) {
35  _clockScale = clockScale;
36  _dt_secs = dt_secs;
37 }
38 
39 void StateLogger::createStringStream(std::stringstream& logStream,
40  const Eigen::Vector3d& pose,
41  double dynamicsCounter,
42  double rosPubCounter,
43  double periodSec) {
44  uint64_t actuatorsMsgCounter;
45  uint64_t actuatorsMaxDelayUsec;
46  _actuators.retriveStats(&actuatorsMsgCounter, &actuatorsMaxDelayUsec);
47  const auto arming_status = _actuators.getArmingStatus();
48  const auto& actuators = _actuators.actuators;
49  auto actuatorsSize = _actuators.actuatorsSize;
50 
51  std::string arm_str;
52  if (arming_status == ArmingStatus::ARMED) {
53  arm_str = COLOR_GREEN + "[Armed]" + COLOR_TAIL;
54  } else if (arming_status == ArmingStatus::DISARMED) {
55  arm_str = "[Disarmed]";
56  } else {
57  arm_str = COLOR_YELLOW + "[No ARM status]" + COLOR_TAIL;
58  }
59  logStream << arm_str << ", ";
60 
61  logStream << _info.dynamicsName.c_str() << ". ";
62 
63  double dynamicsCompleteness = (double)dynamicsCounter * _dt_secs / (_clockScale * periodSec);
64  uint8_t dynamicsPercent = 100 * dynamicsCompleteness;
65  std::string dyn_str = "dyn=" + std::to_string(dynamicsPercent) + "%";
66  addErrColor(logStream, dynamicsPercent >= 95, dyn_str);
67  logStream << ", ";
68 
69  double rosPubCompleteness = (double)rosPubCounter * (double)ROS_PUB_PERIOD_SEC / (_clockScale * periodSec);
70  uint8_t rosPubPercent = 100 * rosPubCompleteness;
71  std::string ros_pub_str = "ros_pub=" + std::to_string(rosPubPercent) + "%";
72  addErrColor(logStream, rosPubPercent >= 99, ros_pub_str);
73  logStream << ", ";
74 
75  std::string setpoint_name;
76  if (actuatorsSize > 1) {
77  setpoint_name = "Vector" + std::to_string(actuatorsSize);
78  } else if (actuatorsSize == 1) {
79  setpoint_name = "Scalar";
80  } else {
81  setpoint_name = "Setpoint";
82  }
83  std::string actuator_str = setpoint_name + "=" + std::to_string(actuatorsMsgCounter) +\
84  " hz (" + std::to_string(actuatorsMaxDelayUsec / 1000) + " ms)";
85  if (actuatorsMsgCounter < 150 || actuatorsMaxDelayUsec > 30000 || actuatorsMaxDelayUsec == 0) {
86  addErrColor(logStream, false, actuator_str);
87  } else if (actuatorsMsgCounter < 175 || actuatorsMaxDelayUsec > 15000) {
88  addWarnColor(logStream, actuator_str);
89  } else {
90  logStream << actuator_str;
91  }
92  logStream << ".\n";
93 
94  addBold(logStream, "mc");
96  logStream << std::setprecision(2) << std::fixed << " ["
97  << actuators[0] << ", "
98  << actuators[1] << ", "
99  << actuators[2] << ", "
100  << actuators[3] << "] ";
101  } else {
102  logStream << std::setprecision(2) << std::fixed << " ["
103  << actuators[0] << ", "
104  << actuators[1] << ", "
105  << actuators[2] << ", "
106  << actuators[3] << ", "
107  << actuators[4] << ", "
108  << actuators[5] << ", "
109  << actuators[6] << ", "
110  << actuators[7] << "] ";
111  }
112 
114  addBold(logStream, "fw rpy");
115  logStream << " [" << actuators[5] << ", "
116  << actuators[6] << ", "
117  << actuators[7] << "]";
118  addBold(logStream, " throttle");
119  logStream << " [" << actuators[4] << "] ";
121  addBold(logStream, "fw rpy");
122  logStream << " [" << actuators[9] << ", "
123  << actuators[10] << ", "
124  << actuators[11] << "]";
125  addBold(logStream, " throttle");
126  logStream << " [" << actuators[8] << "] ";
127  }
128 
129  auto enuPosition = (_info.notation == DynamicsNotation_t::PX4_NED_FRD) ? Converter::nedToEnu(pose) : pose;
130  addBold(logStream, "enu pose");
131  logStream << std::setprecision(1) << std::fixed << " ["
132  << enuPosition[0] << ", "
133  << enuPosition[1] << ", "
134  << enuPosition[2] << "].";
135 }
136 
137 void StateLogger::addErrColor(std::stringstream& logStream, bool is_ok, const std::string& newData) {
138  if(!is_ok){
139  logStream << COLOR_RED << newData << COLOR_TAIL;
140  }else{
141  logStream << newData;
142  }
143 }
144 
145 void StateLogger::addWarnColor(std::stringstream& logStream, const std::string& newData) {
146  logStream << COLOR_YELLOW << newData << COLOR_TAIL;
147 }
148 
149 void StateLogger::addBold(std::stringstream& logStream, const char* newData) {
150  logStream << COLOR_BOLD << newData << COLOR_TAIL;
151 }
Actuators::getArmingStatus
ArmingStatus getArmingStatus()
Definition: actuators.cpp:34
ArmingStatus::DISARMED
@ DISARMED
ArmingStatus::ARMED
@ ARMED
COLOR_BOLD
static const std::string COLOR_BOLD
Definition: logger.cpp:29
StateLogger::addBold
static void addBold(std::stringstream &logStream, const char *newData)
Definition: logger.cpp:149
DynamicsInfo::loggingType
LoggingType loggingType
Definition: dynamics.hpp:43
DynamicsInfo::notation
DynamicsNotation_t notation
Definition: dynamics.hpp:46
StateLogger::_clockScale
double _clockScale
Definition: logger.hpp:45
StateLogger::addErrColor
static void addErrColor(std::stringstream &logStream, bool is_ok, const std::string &newData)
Definition: logger.cpp:137
StateLogger::_dt_secs
double _dt_secs
Definition: logger.hpp:46
main.hpp
StateLogger::createStringStream
void createStringStream(std::stringstream &logStream, const Eigen::Vector3d &pose, double dynamicsCounter, double rosPubCounter, double periodSec)
Definition: logger.cpp:39
StateLogger::_actuators
Actuators & _actuators
Definition: logger.hpp:41
StateLogger::init
void init(double clockScale, double dt_secs)
Definition: logger.cpp:34
DynamicsNotation_t::PX4_NED_FRD
@ PX4_NED_FRD
COLOR_TAIL
static const std::string COLOR_TAIL
Definition: logger.cpp:30
COLOR_GREEN
static const std::string COLOR_GREEN
Definition: logger.cpp:27
Actuators::retriveStats
void retriveStats(uint64_t *msg_counter, uint64_t *max_delay_us)
Definition: actuators.cpp:26
Actuators::actuatorsSize
uint8_t actuatorsSize
Definition: actuators.hpp:40
Actuators::actuators
std::vector< double > actuators
Definition: actuators.hpp:39
StateLogger::_info
DynamicsInfo & _info
Definition: logger.hpp:43
LoggingType::STANDARD_VTOL
@ STANDARD_VTOL
ROS_PUB_PERIOD_SEC
static const float ROS_PUB_PERIOD_SEC
Definition: logger.cpp:32
logger.hpp
LoggingType::VTOL_8_MOTORS
@ VTOL_8_MOTORS
StateLogger::addWarnColor
static void addWarnColor(std::stringstream &logStream, const std::string &newData)
Definition: logger.cpp:145
COLOR_RED
static const std::string COLOR_RED
Definition: logger.cpp:26
COLOR_YELLOW
static const std::string COLOR_YELLOW
Definition: logger.cpp:28
DynamicsInfo::dynamicsName
std::string dynamicsName
Definition: dynamics.hpp:40
Converter::nedToEnu
Eigen::Vector3d nedToEnu(Eigen::Vector3d ned)
Definition: cs_converter.cpp:50
cs_converter.hpp


inno_vtol_dynamics
Author(s): Roman Fedorenko, Dmitry Ponomarev, Ezra Tal, Winter Guerra
autogenerated on Mon Dec 9 2024 03:13:35