From c4bde8fb80ca168018567cb706f196d44428dc6c Mon Sep 17 00:00:00 2001 From: Wire Jansen Date: Tue, 16 Dec 2025 20:34:51 +0100 Subject: [PATCH] Add command for setting SOC to the car simulator Signed-off-by: Wire Jansen --- modules/EV/EvManager/main/car_simulation.hpp | 7 +++++++ modules/EV/EvManager/main/car_simulatorImpl.cpp | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/modules/EV/EvManager/main/car_simulation.hpp b/modules/EV/EvManager/main/car_simulation.hpp index 6dff1ee580..bd633d4e6c 100644 --- a/modules/EV/EvManager/main/car_simulation.hpp +++ b/modules/EV/EvManager/main/car_simulation.hpp @@ -35,6 +35,13 @@ class CarSimulation { sim_data.battery_charge_wh = config.dc_energy_capacity * (soc / 100.0); } + void set_soc(double soc) { + if (soc < 0 || soc > 100) { + throw std::out_of_range("SoC value " + std::to_string(soc) + " is out of range (0-100)"); + } + sim_data.battery_charge_wh = config.dc_energy_capacity * (soc / 100.0); + } + const SimState& get_state() const { return sim_data.state; } diff --git a/modules/EV/EvManager/main/car_simulatorImpl.cpp b/modules/EV/EvManager/main/car_simulatorImpl.cpp index b9e24ee166..d15d541d01 100644 --- a/modules/EV/EvManager/main/car_simulatorImpl.cpp +++ b/modules/EV/EvManager/main/car_simulatorImpl.cpp @@ -189,6 +189,20 @@ void car_simulatorImpl::register_all_commands() { EVLOG_error << "plugin command called but \"plugin_commands\" config key not set"; return true; }); + command_registry->register_command("set_soc", 1, [this](const CmdArguments& arguments) { + try { + double soc = std::stod(arguments.at(0)); + this->car_simulation->set_soc(soc); + } catch (const std::invalid_argument& e) { + EVLOG_error << "set_soc command called with invalid argument: " << arguments.at(0); + return true; + } catch (const std::out_of_range& e) { + EVLOG_error << "set_soc command called with out of range argument: " << arguments.at(0); + return true; + } + + return true; + }); if (!mod->r_slac.empty()) { command_registry->register_command("iso_wait_slac_matched", 0, [this](const CmdArguments& arguments) {