Skip to content

Commit

Permalink
increase min required time for updating stats.engaged_time and slight…
Browse files Browse the repository at this point in the history
…ly clean up
  • Loading branch information
PonomarevDA committed Sep 28, 2024
1 parent d6f4e09 commit 910aa03
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
31 changes: 12 additions & 19 deletions Src/modules/dronecan/arming/arming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,29 @@
*/

#include "arming.hpp"
#include <storage.h>
#include "can_driver.h"
#include "params.hpp"
#include "common/algorithms.hpp"
#include <params.hpp>
#include <logger.hpp>

DronecanLogger logger = DronecanLogger("ArmingModule");


REGISTER_MODULE(ArmingModule)

void ArmingModule::init() {
health = Status::OK;
prev_eng_time = paramsGetIntegerValue(IntParamsIndexes::PARAM_STATS_ENG_TIME);
mode = Module::Mode::STANDBY;
}

void ArmingModule::spin_once() {
global_mode = ModuleManager::get_global_mode();
auto global_mode = ModuleManager::get_global_mode();
auto crnt_time_ms = HAL_GetTick();
if (global_mode == Mode::ENGAGED && !is_armed) {
is_armed = true;
arm_start_time = crnt_time_ms;
return;
}
if (is_armed && global_mode != Mode::ENGAGED && arm_start_time + 1000 < crnt_time_ms) {

uint32_t elapsed_time_ms = crnt_time_ms - arm_start_time;
if (is_armed && global_mode != Mode::ENGAGED) {
is_armed = false;
auto cur_eng_time = (crnt_time_ms - arm_start_time) / 1000;
prev_eng_time = cur_eng_time + prev_eng_time;
paramsSetIntegerValue(IntParamsIndexes::PARAM_STATS_ENG_TIME, int(prev_eng_time));
paramsSave();
if (elapsed_time_ms > PERIOD_OF_INSENSITIVITY_MS) {
uint32_t prev_eng_time = paramsGetIntegerValue(IntParamsIndexes::PARAM_STATS_ENG_TIME);
auto new_eng_time = prev_eng_time + elapsed_time_ms / 1000;
paramsSetIntegerValue(IntParamsIndexes::PARAM_STATS_ENG_TIME, int(new_eng_time));
paramsSave();
logger.log_info("Engaged time has been udpated");
}
}
}
10 changes: 6 additions & 4 deletions Src/modules/dronecan/arming/arming.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define SRC_MODULES_ARMING_HPP_

#include "module.hpp"
#include "subscriber.hpp"
#include "common/logging.hpp"

#ifdef __cplusplus
extern "C" {
Expand All @@ -17,16 +17,18 @@ extern "C" {
class ArmingModule : public Module {
public:
ArmingModule() : Module(2, Protocol::DRONECAN) {}
void init() override;

protected:
void spin_once() override;

private:
Mode global_mode;
bool is_armed = {false};
uint32_t arm_start_time = 0;
uint32_t prev_eng_time = 0;

static inline Logging logger{"ARM"};

// Do not update persistent parameters if the node engaged less than this period of time
static constexpr uint32_t PERIOD_OF_INSENSITIVITY_MS{3000};
};

#ifdef __cplusplus
Expand Down

0 comments on commit 910aa03

Please sign in to comment.