Skip to content

Commit

Permalink
Adding powermeter interface to DcSupplySimulator
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Lukas <sebastian.lukas@pionix.de>
  • Loading branch information
SebaLukas committed Jul 3, 2024
1 parent 80a477f commit c277302
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 45 deletions.
2 changes: 1 addition & 1 deletion config/config-sil-dc-sae-v2g.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ active_modules:
- module_id: imd
implementation_id: main
powersupply_dc:
module: JsDCSupplySimulator
module: DCSupplySimulator
yeti_driver:
module: JsYetiSimulator
config_module:
Expand Down
2 changes: 1 addition & 1 deletion config/config-sil-dc-sae-v2h.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ active_modules:
- module_id: imd
implementation_id: main
powersupply_dc:
module: JsDCSupplySimulator
module: DCSupplySimulator
yeti_driver:
module: JsYetiSimulator
config_module:
Expand Down
2 changes: 1 addition & 1 deletion config/config-sil-dc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ active_modules:
- module_id: imd
implementation_id: main
powersupply_dc:
module: JsDCSupplySimulator
module: DCSupplySimulator
yeti_driver:
module: JsYetiSimulator
config_module:
Expand Down
2 changes: 1 addition & 1 deletion config/config-sil-two-evse-dc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ active_modules:
slac_1:
module: JsSlacSimulator
powersupply_dc:
module: JsDCSupplySimulator
module: DCSupplySimulator
imd:
module: IMDSimulator
config_implementation:
Expand Down
1 change: 1 addition & 0 deletions modules/simulation/DCSupplySimulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ target_compile_features(${MODULE_NAME} PUBLIC cxx_std_17)
target_sources(${MODULE_NAME}
PRIVATE
"main/power_supply_DCImpl.cpp"
"powermeter/powermeterImpl.cpp"
)

# ev@c55432ab-152c-45a9-9d2e-7281d50c69c3:v1
Expand Down
6 changes: 4 additions & 2 deletions modules/simulation/DCSupplySimulator/DCSupplySimulator.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 chargebyte GmbH
// Copyright (C) 2023 Contributors to EVerest
// Copyright chargebyte GmbH
// Copyright Pionix GmbH and Contributors to EVerest

#include "DCSupplySimulator.hpp"

namespace module {

void DCSupplySimulator::init() {
invoke_init(*p_main);
invoke_init(*p_powermeter);
}

void DCSupplySimulator::ready() {
invoke_ready(*p_main);
invoke_ready(*p_powermeter);
}

} // namespace module
8 changes: 6 additions & 2 deletions modules/simulation/DCSupplySimulator/DCSupplySimulator.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright chargebyte GmbH and Contributors to EVerest
// Copyright Pionix GmbH and Contributors to EVerest
#ifndef DCSUPPLY_SIMULATOR_HPP
#define DCSUPPLY_SIMULATOR_HPP

Expand All @@ -12,6 +13,7 @@

// headers for provided interface implementations
#include <generated/interfaces/power_supply_DC/Implementation.hpp>
#include <generated/interfaces/powermeter/Implementation.hpp>

// ev@4bf81b14-a215-475c-a1d3-0a484ae48918:v1
// insert your custom include headers here
Expand All @@ -24,10 +26,12 @@ struct Conf {};
class DCSupplySimulator : public Everest::ModuleBase {
public:
DCSupplySimulator() = delete;
DCSupplySimulator(const ModuleInfo& info, std::unique_ptr<power_supply_DCImplBase> p_main, Conf& config) :
ModuleBase(info), p_main(std::move(p_main)), config(config){};
DCSupplySimulator(const ModuleInfo& info, std::unique_ptr<power_supply_DCImplBase> p_main,
std::unique_ptr<powermeterImplBase> p_powermeter, Conf& config) :
ModuleBase(info), p_main(std::move(p_main)), p_powermeter(std::move(p_powermeter)), config(config){};

const std::unique_ptr<power_supply_DCImplBase> p_main;
const std::unique_ptr<powermeterImplBase> p_powermeter;
const Conf& config;

// ev@1fce4c5e-0ab8-41bb-90f7-14277703d2ac:v1
Expand Down
100 changes: 63 additions & 37 deletions modules/simulation/DCSupplySimulator/main/power_supply_DCImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 chargebyte GmbH
// Copyright (C) 2023 Contributors to EVerest
// Copyright chargebyte GmbH
// Copyright Pionix GmbH and Contributors to EVerest

#include <chrono>
#include <mutex>
Expand All @@ -11,10 +11,12 @@ namespace module {
namespace main {

void power_supply_DCImpl::init() {
this->connector_voltage = 0.0;
this->connector_current = 0.0;
connector_voltage = 0.0;
connector_current = 0.0;
energy_import_total = 0.0;
energy_export_total = 0.0;

this->power_supply_thread_handle = std::thread(&power_supply_DCImpl::power_supply_worker, this);
power_supply_thread_handle = std::thread(&power_supply_DCImpl::power_supply_worker, this);
}

static auto get_capabilities_from_config(const Conf& config) {
Expand All @@ -40,35 +42,35 @@ static auto get_capabilities_from_config(const Conf& config) {
}

void power_supply_DCImpl::ready() {
publish_capabilities(get_capabilities_from_config(this->config));
publish_capabilities(get_capabilities_from_config(config));
}

void power_supply_DCImpl::handle_setMode(types::power_supply_DC::Mode& value) {
this->mode = value;
mode = value;

std::scoped_lock access_lock(this->power_supply_values_mutex);
std::scoped_lock access_lock(power_supply_values_mutex);
if ((value == types::power_supply_DC::Mode::Off) || (value == types::power_supply_DC::Mode::Fault)) {
this->connector_voltage = 0.0;
this->connector_current = 0.0;
connector_voltage = 0.0;
connector_current = 0.0;
} else if (value == types::power_supply_DC::Mode::Export) {
this->connector_voltage = this->settings_connector_export_voltage;
this->connector_current = this->settings_connector_max_export_current;
connector_voltage = settings_connector_export_voltage;
connector_current = settings_connector_max_export_current;
} else if (value == types::power_supply_DC::Mode::Import) {
this->connector_voltage = this->settings_connector_import_voltage;
this->connector_current = this->settings_connector_max_import_current;
connector_voltage = settings_connector_import_voltage;
connector_current = settings_connector_max_import_current;
}

mod->p_main->publish_mode(value);
}

void power_supply_DCImpl::clampVoltageCurrent(double& voltage, double& current) {
voltage = voltage < this->config.min_voltage ? this->config.min_voltage
: voltage > this->config.max_voltage ? this->config.max_voltage
: voltage;
voltage = voltage < config.min_voltage ? config.min_voltage
: voltage > config.max_voltage ? config.max_voltage
: voltage;

current = current < this->config.min_current ? this->config.min_current
: current > this->config.max_current ? this->config.max_current
: current;
current = current < config.min_current ? config.min_current
: current > config.max_current ? config.max_current
: current;
}

void power_supply_DCImpl::handle_setExportVoltageCurrent(double& voltage, double& current) {
Expand All @@ -77,13 +79,13 @@ void power_supply_DCImpl::handle_setExportVoltageCurrent(double& voltage, double

clampVoltageCurrent(temp_voltage, temp_current);

std::scoped_lock access_lock(this->power_supply_values_mutex);
this->settings_connector_export_voltage = temp_voltage;
this->settings_connector_max_export_current = temp_current;
std::scoped_lock access_lock(power_supply_values_mutex);
settings_connector_export_voltage = temp_voltage;
settings_connector_max_export_current = temp_current;

if (this->mode == types::power_supply_DC::Mode::Export) {
this->connector_voltage = this->settings_connector_export_voltage;
this->connector_current = this->settings_connector_max_export_current;
if (mode == types::power_supply_DC::Mode::Export) {
connector_voltage = settings_connector_export_voltage;
connector_current = settings_connector_max_export_current;
}
}

Expand All @@ -93,32 +95,56 @@ void power_supply_DCImpl::handle_setImportVoltageCurrent(double& voltage, double

clampVoltageCurrent(temp_voltage, temp_current);

std::scoped_lock access_lock(this->power_supply_values_mutex);
this->settings_connector_import_voltage = temp_voltage;
this->settings_connector_max_import_current = temp_current;
std::scoped_lock access_lock(power_supply_values_mutex);
settings_connector_import_voltage = temp_voltage;
settings_connector_max_import_current = temp_current;

if (this->mode == types::power_supply_DC::Mode::Import) {
this->connector_voltage = this->settings_connector_import_voltage;
this->connector_current = -this->settings_connector_max_import_current;
if (mode == types::power_supply_DC::Mode::Import) {
connector_voltage = settings_connector_import_voltage;
connector_current = -settings_connector_max_import_current;
}
}

types::powermeter::Powermeter power_supply_DCImpl::power_meter_external() {
types::powermeter::Powermeter powermeter;

powermeter.timestamp = Everest::Date::to_rfc3339(date::utc_clock::now());
powermeter.meter_id = "DC_POWERMETER";

if (connector_current > 0) {
energy_import_total += (connector_voltage * connector_current * 0.5) / 3600;
}
if (connector_current < 0) {
energy_export_total += (connector_voltage * -connector_current * 0.5) / 3600;
}

powermeter.energy_Wh_import = {static_cast<float>(energy_import_total)};
powermeter.energy_Wh_export = {static_cast<float>(energy_export_total)};

powermeter.power_W = {static_cast<float>(connector_current * connector_voltage)};
powermeter.current_A = {static_cast<float>(connector_current)};
powermeter.voltage_V = {static_cast<float>(connector_voltage)};

return powermeter;
}

void power_supply_DCImpl::power_supply_worker(void) {
types::power_supply_DC::VoltageCurrent voltage_current;

while (true) {
if (this->power_supply_thread_handle.shouldExit()) {
if (power_supply_thread_handle.shouldExit()) {
break;
}

// set interval for publishing
std::this_thread::sleep_for(std::chrono::milliseconds(LOOP_SLEEP_MS));

std::scoped_lock access_lock(this->power_supply_values_mutex);
voltage_current.voltage_V = static_cast<float>(this->connector_voltage);
voltage_current.current_A = static_cast<float>(this->connector_current);
std::scoped_lock access_lock(power_supply_values_mutex);
voltage_current.voltage_V = static_cast<float>(connector_voltage);
voltage_current.current_A = static_cast<float>(connector_current);

this->mod->p_main->publish_voltage_current(voltage_current);
mod->p_main->publish_voltage_current(voltage_current);
mod->p_powermeter->publish_powermeter(power_meter_external());
}
}
} // namespace main
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright chargebyte GmbH and Contributors to EVerest
// Copyright Pionix GmbH and Contributors to EVerest
#ifndef MAIN_POWER_SUPPLY_DC_IMPL_HPP
#define MAIN_POWER_SUPPLY_DC_IMPL_HPP

Expand Down Expand Up @@ -66,8 +67,11 @@ class power_supply_DCImpl : public power_supply_DCImplBase {
types::power_supply_DC::Mode mode;
double connector_voltage;
double connector_current;
double energy_import_total;

Check notice on line 70 in modules/simulation/DCSupplySimulator/main/power_supply_DCImpl.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/simulation/DCSupplySimulator/main/power_supply_DCImpl.hpp#L70

class member 'power_supply_DCImpl::energy_import_total' is never used.
double energy_export_total;

Check notice on line 71 in modules/simulation/DCSupplySimulator/main/power_supply_DCImpl.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/simulation/DCSupplySimulator/main/power_supply_DCImpl.hpp#L71

class member 'power_supply_DCImpl::energy_export_total' is never used.
std::mutex power_supply_values_mutex;
Everest::Thread power_supply_thread_handle;
types::powermeter::Powermeter power_meter_external();
void power_supply_worker(void);

static constexpr int LOOP_SLEEP_MS{500};
Expand Down
4 changes: 4 additions & 0 deletions modules/simulation/DCSupplySimulator/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ provides:
description: Max supported current
type: number
default: 200.0
powermeter:
interface: powermeter
description: Power meter interface for simulation
metadata:
license: https://opensource.org/licenses/Apache-2.0
authors:
- Cornelius Claussen (Pionix GmbH)
- Fabian Hartung (chargebyte GmbH)
- Mohannad Oraby (chargebyte GmbH)
- Sebastian Lukas (Pionix GmbH)
28 changes: 28 additions & 0 deletions modules/simulation/DCSupplySimulator/powermeter/powermeterImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest

#include "powermeterImpl.hpp"

namespace module {
namespace powermeter {

void powermeterImpl::init() {
}

void powermeterImpl::ready() {
}

types::powermeter::TransactionStartResponse
powermeterImpl::handle_start_transaction(types::powermeter::TransactionReq& value) {
return {types::powermeter::TransactionRequestStatus::OK};
}

types::powermeter::TransactionStopResponse powermeterImpl::handle_stop_transaction(std::string& transaction_id) {
return {types::powermeter::TransactionRequestStatus::NOT_SUPPORTED,
{},
{},
"DcSupplySimulator does not support stop transaction request."};
}

} // namespace powermeter
} // namespace module
63 changes: 63 additions & 0 deletions modules/simulation/DCSupplySimulator/powermeter/powermeterImpl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest
#ifndef POWERMETER_POWERMETER_IMPL_HPP
#define POWERMETER_POWERMETER_IMPL_HPP

//
// AUTO GENERATED - MARKED REGIONS WILL BE KEPT
// template version 3
//

#include <generated/interfaces/powermeter/Implementation.hpp>

#include "../DCSupplySimulator.hpp"

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

namespace module {
namespace powermeter {

struct Conf {};

class powermeterImpl : public powermeterImplBase {
public:
powermeterImpl() = delete;
powermeterImpl(Everest::ModuleAdapter* ev, const Everest::PtrContainer<DCSupplySimulator>& mod, Conf& config) :
powermeterImplBase(ev, "powermeter"), mod(mod), config(config){};

// ev@8ea32d28-373f-4c90-ae5e-b4fcc74e2a61:v1
// insert your public definitions here
// ev@8ea32d28-373f-4c90-ae5e-b4fcc74e2a61:v1

protected:
// command handler functions (virtual)
virtual types::powermeter::TransactionStartResponse
handle_start_transaction(types::powermeter::TransactionReq& value) override;
virtual types::powermeter::TransactionStopResponse handle_stop_transaction(std::string& transaction_id) override;

// ev@d2d1847a-7b88-41dd-ad07-92785f06f5c4:v1
// insert your protected definitions here
// ev@d2d1847a-7b88-41dd-ad07-92785f06f5c4:v1

private:
const Everest::PtrContainer<DCSupplySimulator>& mod;
const Conf& config;

virtual void init() override;
virtual void ready() override;

// ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1
// insert your private definitions here
// ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1
};

// ev@3d7da0ad-02c2-493d-9920-0bbbd56b9876:v1
// insert other definitions here
// ev@3d7da0ad-02c2-493d-9920-0bbbd56b9876:v1

} // namespace powermeter
} // namespace module

#endif // POWERMETER_POWERMETER_IMPL_HPP

0 comments on commit c277302

Please sign in to comment.