ice.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2022 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 "ice.hpp"
20 #include <std_msgs/UInt8.h>
21 #include <mavros_msgs/ESCStatusItem.h>
22 
23 static const constexpr double WORKING_RPM = 4000.0;
24 static const constexpr double STARTING_RPM = 500.0;
25 static const constexpr double PERIOD_1 = 1500.0; // falling rpm period
26 static const constexpr double PERIOD_2 = 1500.0; // starting period
27 static const constexpr double PERIOD_3 = 2000.0; // waiting period
28 static const constexpr double PERIOD_23 = PERIOD_2 + PERIOD_3; // starting + waiting period
29 
30 IceStatusSensor::IceStatusSensor(ros::NodeHandle* nh, const char* topic, double period) : BaseSensor(nh, period){
31  std::string base_name = topic;
32  auto rpm_name = base_name + "_rpm";
33  auto status_name = base_name + "_status";
34  publisher_ = node_handler_->advertise<mavros_msgs::ESCStatusItem>(rpm_name.c_str(), 5);
35  _status_publisher = node_handler_->advertise<std_msgs::UInt8>(status_name.c_str(), 5);
36 }
37 bool IceStatusSensor::publish(double rpm) {
38  auto crntTimeSec = ros::Time::now().toSec();
39  if(_isEnabled && (nextPubTimeSec_ < crntTimeSec)){
40  estimate_state(rpm);
41 
42  std_msgs::UInt8 state_msg;
43  state_msg.data = _state;
44  _status_publisher.publish(state_msg);
45 
46  mavros_msgs::ESCStatusItem rpm_msg;
47  rpm_msg.rpm = static_cast<int32_t>(rpm);
48  publisher_.publish(rpm_msg);
49 
50  nextPubTimeSec_ = crntTimeSec + PERIOD;
51  }
52  return true;
53 }
55  if (_stallTsMs == 0) {
57  } else {
59  }
60 }
61 
63  auto crntTimeSec = ros::Time::now().toSec();
64  if (rpm < 1.0) {
65  _state = 0;
66  } else if (_state == 0) {
67  _state = 1;
69  } else if (_startTsSec + 3.0 < crntTimeSec) {
70  _state = 2;
71  }
72  _rpm = rpm;
73 }
74 
76  auto crntTimeMs = ros::Time::now().toSec() * 1000;
77  auto timeElapsedMs = crntTimeMs - _stallTsMs;
78  if (timeElapsedMs < PERIOD_1) {
79  _state = 2;
80  _rpm = WORKING_RPM * (PERIOD_1 - timeElapsedMs) / PERIOD_1;
81  } else {
82  double timeSinceRestartMs = timeElapsedMs - PERIOD_1;
83  if (fmod(timeSinceRestartMs, PERIOD_23) < PERIOD_2) {
84  _state = 1;
86  } else {
87  _state = 2;
88  _rpm = 0.0;
89  }
90  }
91 }
92 
94  _stallTsMs = ros::Time::now().toSec() * 1000;
95 }
96 
98  _stallTsMs = 0;
99 }
double nextPubTimeSec_
Definition: sensor_base.hpp:37
bool publish(double rpm)
Definition: ice.cpp:37
IceStatusSensor(ros::NodeHandle *nh, const char *topic, double period)
Definition: ice.cpp:30
static const constexpr double PERIOD_2
Definition: ice.cpp:26
void emulate_normal_mode(double rpm)
Definition: ice.cpp:62
void start_stall_emulation()
Definition: ice.cpp:93
double _rpm
Definition: ice.hpp:37
double _stallTsMs
Definition: ice.hpp:39
static const constexpr double PERIOD_23
Definition: ice.cpp:28
uint8_t _state
Definition: ice.hpp:38
ros::NodeHandle * node_handler_
Definition: sensor_base.hpp:33
const double PERIOD
Definition: sensor_base.hpp:35
static const constexpr double STARTING_RPM
Definition: ice.cpp:24
void estimate_state(double rpm)
Definition: ice.cpp:54
void publish(const boost::shared_ptr< M > &message) const
static const constexpr double PERIOD_3
Definition: ice.cpp:27
double _startTsSec
Definition: ice.hpp:40
ros::Publisher _status_publisher
Definition: ice.hpp:35
void emulate_stall_mode()
Definition: ice.cpp:75
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool _isEnabled
Definition: sensor_base.hpp:34
static const constexpr double WORKING_RPM
Definition: ice.cpp:23
static Time now()
static const constexpr double PERIOD_1
Definition: ice.cpp:25
void stop_stall_emulation()
Definition: ice.cpp:97
ros::Publisher publisher_
Definition: sensor_base.hpp:36


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