Skip to content

Commit

Permalink
Adding run thread, fix: sim restarts again
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 May 8, 2024
1 parent 841c177 commit eddd637
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
4 changes: 1 addition & 3 deletions modules/EvManager/main/CarSimulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <everest/logging.hpp>

void CarSimulation::stateMachine() {
void CarSimulation::state_machine() {
using types::ev_board_support::EvCpState;

const auto stateHasChanged = sim_data.state != sim_data.lastState;
Expand All @@ -21,8 +21,6 @@ void CarSimulation::stateMachine() {
r_ev_board_support->call_allow_power_on(false);
// Wait for physical plugin (ev BSP sees state A on CP and not Disconnected)

// If we have auto_exec configured, restart simulation when it was unplugged
EVLOG_info << "Unplug detected, restarting simulation.";
sim_data.slacState = "UNMATCHED";
r_ev[0]->call_stop_charging();
}
Expand Down
6 changes: 5 additions & 1 deletion modules/EvManager/main/CarSimulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class CarSimulation {
return sim_data.state;
}

void set_state(SimState state) {
sim_data.state = state;
}

void setBspEvent(types::board_support_common::Event event) {
sim_data.actualBspEvent = event;
}
Expand Down Expand Up @@ -68,7 +72,7 @@ class CarSimulation {
sim_data.dc_power_on = dcPowerOn;
}

void stateMachine();
void state_machine();
bool sleep(const CmdArguments&, size_t);
bool iec_wait_pwr_ready(const CmdArguments&);
bool iso_wait_pwm_is_running(const CmdArguments& arguments);
Expand Down
3 changes: 2 additions & 1 deletion modules/EvManager/main/SimulationData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ enum class SimState {
ISO_POWER_READY,
ISO_CHARGING_REGULATED,
BCB_TOGGLE,
UNDEFINED,
};

struct SimulationData {

SimulationData() = default;

SimState state{SimState::UNPLUGGED};
SimState lastState{SimState::UNPLUGGED};
SimState lastState{SimState::UNDEFINED};

Check notice on line 34 in modules/EvManager/main/SimulationData.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/EvManager/main/SimulationData.hpp#L34

struct member 'SimulationData::lastState' is never used.
std::string slacState{"UNMATCHED"};

Check notice on line 35 in modules/EvManager/main/SimulationData.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/EvManager/main/SimulationData.hpp#L35

struct member 'SimulationData::slacState' is never used.
std::optional<size_t> sleepTicksLeft{};

Expand Down
72 changes: 39 additions & 33 deletions modules/EvManager/main/car_simulatorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
namespace module::main {

void car_simulatorImpl::init() {
enabled = false;
loopIntervalMs = DEFAULT_LOOP_INTERVAL_MS;
registerAllCommands();
subscribeToExternalMQTT();
subscribeToVariablesOnInit();

std::thread(&car_simulatorImpl::run, this).detach();
}

void car_simulatorImpl::ready() {
Expand Down Expand Up @@ -43,13 +44,7 @@ void car_simulatorImpl::handle_enable(bool& value) {

callEVBoardSupportFunctions();

if (value) {
enabled = true;
simulationThread = std::thread{&car_simulatorImpl::handleSimulationLoop, this};
} else {
enabled = false;
simulationThread.join();
}
enabled = value;

mod->r_ev_board_support->call_enable(value);
publish_enabled(value);
Expand All @@ -65,7 +60,7 @@ void car_simulatorImpl::handle_executeChargingSession(std::string& value) {

updateCommandQueue(value);

std::lock_guard<std::mutex> lock{carSimulationMutex};
const std::lock_guard<std::mutex> lock{carSimulationMutex};
if (!commandQueue.empty()) {
executionActive = true;
}
Expand All @@ -81,27 +76,32 @@ void car_simulatorImpl::handle_modifyChargingSession(std::string& value) {

updateCommandQueue(value);

std::lock_guard<std::mutex> lock{carSimulationMutex};
const std::lock_guard<std::mutex> lock{carSimulationMutex};
if (!commandQueue.empty()) {
executionActive = true;
}
}

void car_simulatorImpl::handleSimulationLoop() {
while (enabled) {
if (executionActive) {
runSimulationLoop();
std::this_thread::sleep_for(std::chrono::milliseconds(loopIntervalMs));
}
}
EVLOG_debug << "Finished simulation.";
void car_simulatorImpl::run() {
while (true) {
if (enabled == true && executionActive == true) {

resetCarSimulationDefaults();
const auto finished = run_simulation_loop();

// If we have auto_exec_infinite configured, restart simulation when it is done
if (mod->config.auto_exec && mod->config.auto_exec_infinite) {
auto valueCopy = mod->config.auto_exec_commands;
handle_executeChargingSession(valueCopy);
if (finished) {
EVLOG_info << "Finished simulation.";
this->executionActive = false;

resetCarSimulationDefaults();

// If we have auto_exec_infinite configured, restart simulation when it is done
if (mod->config.auto_exec && mod->config.auto_exec_infinite) {
auto value_copy = mod->config.auto_exec_commands;
handle_executeChargingSession(value_copy);
}
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(loopIntervalMs));
}
}

Expand Down Expand Up @@ -178,28 +178,33 @@ void car_simulatorImpl::registerAllCommands() {
}
}

void car_simulatorImpl::runSimulationLoop() {
bool car_simulatorImpl::run_simulation_loop() {
// Execute sim commands until a command blocks, or we are finished
std::lock_guard<std::mutex> lock{carSimulationMutex};
const std::lock_guard<std::mutex> lock{carSimulationMutex};
while (executionActive && !commandQueue.empty()) {
auto& currentCommand = commandQueue.front();

auto commandBlocked = false;
auto command_blocked = false;

try {
commandBlocked = !currentCommand.execute();
command_blocked = !currentCommand.execute();
} catch (const std::exception& e) {
EVLOG_error << e.what();
}

if (!commandBlocked) {
if (!command_blocked) {
commandQueue.pop();
} else {
break; // command blocked, wait for timer to run this function again
}
}

car_simulation->stateMachine();
car_simulation->state_machine();

if (commandQueue.empty()) {
return true;
}
return false;
}

bool car_simulatorImpl::checkCanExecute() {
Expand All @@ -217,13 +222,14 @@ bool car_simulatorImpl::checkCanExecute() {

void car_simulatorImpl::subscribeToVariablesOnInit() {
// subscribe bsp_event
std::lock_guard<std::mutex> lock{carSimulationMutex};
const std::lock_guard<std::mutex> lock{carSimulationMutex};
using types::board_support_common::BspEvent;
mod->r_ev_board_support->subscribe_bsp_event([this](const auto& bsp_event) {
car_simulation->setBspEvent(bsp_event.event);
if (bsp_event.event == types::board_support_common::Event::Disconnected &&
car_simulation->getState() == SimState::UNPLUGGED) {
car_simulation->getState() != SimState::UNPLUGGED) {
executionActive = false;
car_simulation->set_state(SimState::UNPLUGGED);
}
});

Expand Down Expand Up @@ -299,12 +305,12 @@ void car_simulatorImpl::subscribeToExternalMQTT() {
});
}
void car_simulatorImpl::resetCarSimulationDefaults() {
std::lock_guard<std::mutex> lock{carSimulationMutex};
const std::lock_guard<std::mutex> lock{carSimulationMutex};
car_simulation->reset();
}

void car_simulatorImpl::updateCommandQueue(std::string& value) {
std::lock_guard<std::mutex> lock{carSimulationMutex};
const std::lock_guard<std::mutex> lock{carSimulationMutex};
commandQueue = SimulationCommand::parseSimCommands(value, *command_registry);
}
} // namespace module::main
7 changes: 3 additions & 4 deletions modules/EvManager/main/car_simulatorImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class car_simulatorImpl : public car_simulatorImplBase {

// ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1

void run();
void handle_modifyChargingSession(std::string& value);
bool checkCanExecute();
void handleSimulationLoop();
void runSimulationLoop();
bool run_simulation_loop();
void registerAllCommands();
void subscribeToVariablesOnInit();
void setupEVParameters();
Expand All @@ -68,11 +68,10 @@ class car_simulatorImpl : public car_simulatorImplBase {
std::mutex carSimulationMutex;
std::unique_ptr<CarSimulation> car_simulation;

bool enabled;
bool enabled{false};

Check notice on line 71 in modules/EvManager/main/car_simulatorImpl.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/EvManager/main/car_simulatorImpl.hpp#L71

class member 'car_simulatorImpl::enabled' is never used.
std::atomic<bool> executionActive{false};
size_t loopIntervalMs{};

Check notice on line 73 in modules/EvManager/main/car_simulatorImpl.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/EvManager/main/car_simulatorImpl.hpp#L73

class member 'car_simulatorImpl::loopIntervalMs' is never used.

std::thread simulationThread;
std::queue<SimulationCommand> commandQueue;

Check notice on line 75 in modules/EvManager/main/car_simulatorImpl.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/EvManager/main/car_simulatorImpl.hpp#L75

class member 'car_simulatorImpl::commandQueue' is never used.

// ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1
Expand Down

0 comments on commit eddd637

Please sign in to comment.