Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions modules/Simulation/OVMSimulator/OVMSimulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ struct Conf {};
class OVMSimulator : public Everest::ModuleBase {
public:
OVMSimulator() = delete;
OVMSimulator(const ModuleInfo& info, std::unique_ptr<over_voltage_monitorImplBase> p_main, Conf& config) :
ModuleBase(info), p_main(std::move(p_main)), config(config){};
OVMSimulator(const ModuleInfo& info, Everest::MqttProvider& mqtt_provider,
std::unique_ptr<over_voltage_monitorImplBase> p_main, Conf& config) :
ModuleBase(info), mqtt(mqtt_provider), p_main(std::move(p_main)), config(config){};

Everest::MqttProvider& mqtt;
const std::unique_ptr<over_voltage_monitorImplBase> p_main;
const Conf& config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ namespace module {
namespace main {

void over_voltage_monitorImpl::init() {
over_voltage_monitoring_active = false;
over_voltage_monitorImpl_thread_handle =
std::thread(&over_voltage_monitorImpl::over_voltage_monitorImpl_worker, this);
const std::string OVER_VOLTAGE_TOPIC = "everest_api/" + this->mod->info.id + "/cmd/set_over_voltage_measurement_V";
EVLOG_info << "Can control the over voltage measurement values over the topic: " << OVER_VOLTAGE_TOPIC
<< " via MQTT";
this->mod->mqtt.subscribe(OVER_VOLTAGE_TOPIC, [this](const std::string& over_voltage_measurement_V) {
try {
EVLOG_info << "Setting simulated over voltage measurement to " << over_voltage_measurement_V
<< " V via MQTT";
voltage_measurement_V = std::stof(over_voltage_measurement_V);
} catch (const std::invalid_argument& e) {
EVLOG_error << "Failed to set over voltage measurement via MQTT, invalid value: "
<< over_voltage_measurement_V;
}
});
}

void over_voltage_monitorImpl::ready() {
Expand All @@ -21,6 +37,7 @@ void over_voltage_monitorImpl::handle_set_limits(double& emergency_over_voltage_
void over_voltage_monitorImpl::handle_start() {
EVLOG_info << "Over voltage monitoring: starting with " << emergency_threshold << " (emergency) and "
<< error_threshold << "(error)";
over_voltage_monitoring_active = true;
if (config.simulate_error_shutdown) {
std::thread([this]() {
std::this_thread::sleep_for(std::chrono::seconds(config.simulate_error_delay));
Expand All @@ -42,8 +59,23 @@ void over_voltage_monitorImpl::handle_start() {
}
}

void over_voltage_monitorImpl::over_voltage_monitorImpl_worker() {
while (true) {
if (this->over_voltage_monitorImpl_thread_handle.shouldExit()) {
break;
}

if (this->over_voltage_monitoring_active == true) {
this->mod->p_main->publish_voltage_measurement_V(this->voltage_measurement_V);
}

std::this_thread::sleep_for(std::chrono::milliseconds(this->LOOP_SLEEP_MS));
}
}

void over_voltage_monitorImpl::handle_stop() {
EVLOG_info << "Over voltage monitoring: stopped.";
over_voltage_monitoring_active = false;
}

void over_voltage_monitorImpl::handle_reset_over_voltage_error() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

// ev@75ac1216-19eb-4182-a85c-820f1fc2c091:v1
// insert your custom include headers here
#include <atomic>

Check warning on line 17 in modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp#L17

Include file: <atomic> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utils/thread.hpp>

Check warning on line 18 in modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp#L18

Include file: <utils/thread.hpp> not found. Please note: Cppcheck does not need standard library headers to get proper results.
// ev@75ac1216-19eb-4182-a85c-820f1fc2c091:v1

namespace module {
Expand Down Expand Up @@ -57,6 +59,12 @@
// insert your private definitions here
double error_threshold{0.};
double emergency_threshold{0.};

static constexpr int LOOP_SLEEP_MS{20};

Check warning on line 63 in modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp#L63

class member 'over_voltage_monitorImpl::LOOP_SLEEP_MS' is never used.
std::atomic<bool> over_voltage_monitoring_active;
float voltage_measurement_V{0.0f};

Check warning on line 65 in modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/Simulation/OVMSimulator/main/over_voltage_monitorImpl.hpp#L65

class member 'over_voltage_monitorImpl::voltage_measurement_V' is never used.
Everest::Thread over_voltage_monitorImpl_thread_handle;
void over_voltage_monitorImpl_worker(void);
// ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1
};

Expand Down
1 change: 1 addition & 0 deletions modules/Simulation/OVMSimulator/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ provides:
description: Simulate over voltage error N seconds after start of charging
type: integer
default: 5
enable_external_mqtt: true
metadata:
license: https://opensource.org/licenses/Apache-2.0
authors:
Expand Down