-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathcar_simulation.cpp
More file actions
456 lines (398 loc) · 16.3 KB
/
Copy pathcar_simulation.cpp
File metadata and controls
456 lines (398 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest
#include "car_simulation.hpp"
#include "constants.hpp"
#include <everest/logging.hpp>
constexpr double MS_FACTOR = (1.0 / 60.0 / 60.0 / 1000.0);
void CarSimulation::state_machine() {
using types::ev_board_support::EvCpState;
const auto state_has_changed = sim_data.state != sim_data.last_state;
sim_data.last_state = sim_data.state;
switch (sim_data.state) {
case SimState::UNPLUGGED:
if (state_has_changed) {
r_ev_board_support->call_set_cp_state(EvCpState::A);
r_ev_board_support->call_allow_power_on(false);
// Wait for physical plugin (ev BSP sees state A on CP and not Disconnected)
sim_data.slac_state = "UNMATCHED";
if (!r_ev.empty()) {
r_ev[0]->call_stop_charging();
}
}
break;
case SimState::PLUGGED_IN:
if (state_has_changed) {
r_ev_board_support->call_set_cp_state(EvCpState::B);
r_ev_board_support->call_allow_power_on(false);
simulate_soc();
}
break;
case SimState::CHARGING_REGULATED:
if (state_has_changed || sim_data.pwm_duty_cycle != sim_data.last_pwm_duty_cycle) {
sim_data.last_pwm_duty_cycle = sim_data.pwm_duty_cycle;
// do not draw power if EVSE paused by stopping PWM
if (sim_data.pwm_duty_cycle > 7.0 && sim_data.pwm_duty_cycle < 97.0) {
r_ev_board_support->call_set_cp_state(EvCpState::C);
r_ev_board_support->call_allow_power_on(true);
} else {
r_ev_board_support->call_set_cp_state(EvCpState::B);
r_ev_board_support->call_allow_power_on(false);
}
}
break;
case SimState::CHARGING_FIXED:
// Todo(sl): What to do here
if (state_has_changed) {
// Also draw power if EVSE stopped PWM - this is a break the rules simulator->mode to test the charging
// implementation!
r_ev_board_support->call_set_cp_state(EvCpState::C);
r_ev_board_support->call_allow_power_on(true);
}
break;
case SimState::ERROR_E:
if (state_has_changed) {
r_ev_board_support->call_set_cp_state(EvCpState::E);
r_ev_board_support->call_allow_power_on(false);
}
break;
case SimState::DIODE_FAIL:
if (state_has_changed) {
r_ev_board_support->call_diode_fail(true);
r_ev_board_support->call_allow_power_on(false);
}
break;
case SimState::ISO_POWER_READY:
if (state_has_changed) {
r_ev_board_support->call_set_cp_state(EvCpState::C);
}
break;
case SimState::ISO_CHARGING_REGULATED:
if (state_has_changed) {
r_ev_board_support->call_set_cp_state(EvCpState::C);
r_ev_board_support->call_allow_power_on(true);
}
break;
case SimState::BCB_TOGGLE:
if (sim_data.bcb_toggle_C) {
r_ev_board_support->call_set_cp_state(EvCpState::C);
sim_data.bcb_toggle_C = false;
} else {
r_ev_board_support->call_set_cp_state(EvCpState::B);
sim_data.bcb_toggle_C = true;
++sim_data.bcb_toggles;
}
break;
default:
sim_data.state = SimState::UNPLUGGED;
break;
}
if (not state_has_changed and
(sim_data.state == SimState::CHARGING_REGULATED or sim_data.state == SimState::CHARGING_FIXED or
sim_data.state == SimState::ISO_CHARGING_REGULATED)) {
simulate_soc();
}
timepoint_last_update = std::chrono::steady_clock::now();
};
void CarSimulation::simulate_soc() {
const double ms =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - timepoint_last_update)
.count();
const double factor = MS_FACTOR * ms;
double power = 0.0;
types::evse_manager::EVInfo ev_info;
switch (charge_mode) {
case ChargeMode::None:
// nothing to do
break;
case ChargeMode::AC:
power = charge_current_a * config.ac_nominal_voltage;
ev_info.target_current = charge_current_a;
ev_info.target_voltage = 0;
break;
case ChargeMode::ACThreePhase:
power = charge_current_a * config.ac_nominal_voltage * 3.0;
ev_info.target_current = charge_current_a;
ev_info.target_voltage = 0;
break;
case ChargeMode::DC:
power = config.dc_target_current * config.dc_target_voltage;
ev_info.target_current = config.dc_target_current;
ev_info.target_voltage = config.dc_target_voltage;
break;
}
if (sim_data.battery_charge_wh > sim_data.battery_capacity_wh) {
sim_data.battery_charge_wh = sim_data.battery_capacity_wh;
} else {
sim_data.battery_charge_wh += power * factor;
}
ev_info.soc = (sim_data.battery_charge_wh / sim_data.battery_capacity_wh) * 100.0;
if (ev_info.soc > 100.0) {
ev_info.soc = 100.0;
}
ev_info.battery_capacity = sim_data.battery_capacity_wh;
ev_info.battery_full_soc = 100;
p_ev_manager->publish_ev_info(ev_info);
}
bool CarSimulation::sleep(const CmdArguments& arguments, size_t loop_interval_ms) {
if (not sim_data.sleep_ticks_left.has_value()) {
const auto sleep_time = std::stold(arguments[0]);
const auto sleep_time_ms = sleep_time * 1000;
sim_data.sleep_ticks_left = static_cast<long long>(sleep_time_ms / loop_interval_ms) + 1;
}
auto& sleep_ticks_left = sim_data.sleep_ticks_left.value();
sleep_ticks_left -= 1;
if (not(sleep_ticks_left > 0)) {
sim_data.sleep_ticks_left.reset();
return true;
} else {
return false;
}
}
bool CarSimulation::iec_wait_pwr_ready(const CmdArguments& arguments) {
sim_data.state = SimState::PLUGGED_IN;
return (sim_data.pwm_duty_cycle > 7.0f && sim_data.pwm_duty_cycle < 97.0f);
}
bool CarSimulation::iso_wait_pwm_is_running(const CmdArguments& arguments) {
sim_data.state = SimState::PLUGGED_IN;
return (sim_data.pwm_duty_cycle > 4.0f && sim_data.pwm_duty_cycle < 97.0f);
}
bool CarSimulation::draw_power_regulated(const CmdArguments& arguments) {
charge_current_a = std::stod(arguments[0]);
r_ev_board_support->call_set_ac_max_current(charge_current_a);
if (arguments[1] == constants::THREE_PHASES) {
r_ev_board_support->call_set_three_phases(true);
charge_mode = ChargeMode::ACThreePhase;
} else {
r_ev_board_support->call_set_three_phases(false);
charge_mode = ChargeMode::AC;
}
sim_data.state = SimState::CHARGING_REGULATED;
return true;
}
bool CarSimulation::draw_power_fixed(const CmdArguments& arguments) {
charge_current_a = std::stod(arguments[0]);
r_ev_board_support->call_set_ac_max_current(charge_current_a);
if (arguments[1] == constants::THREE_PHASES) {
r_ev_board_support->call_set_three_phases(true);
charge_mode = ChargeMode::ACThreePhase;
} else {
r_ev_board_support->call_set_three_phases(false);
charge_mode = ChargeMode::AC;
}
sim_data.state = SimState::CHARGING_FIXED;
return true;
}
bool CarSimulation::pause(const CmdArguments& arguments) {
sim_data.state = SimState::PLUGGED_IN;
return true;
}
bool CarSimulation::unplug(const CmdArguments& arguments) {
sim_data.state = SimState::UNPLUGGED;
charge_mode = ChargeMode::None;
return true;
}
bool CarSimulation::error_e(const CmdArguments& arguments) {
sim_data.state = SimState::ERROR_E;
return true;
}
bool CarSimulation::diode_fail(const CmdArguments& arguments) {
sim_data.state = SimState::DIODE_FAIL;
return true;
}
bool CarSimulation::rcd_current(const CmdArguments& arguments) {
sim_data.rcd_current_ma = std::stof(arguments[0]);
r_ev_board_support->call_set_rcd_error(sim_data.rcd_current_ma);
return true;
}
bool CarSimulation::iso_wait_slac_matched(const CmdArguments& arguments) {
sim_data.state = SimState::PLUGGED_IN;
if (sim_data.slac_state == "UNMATCHED") {
EVLOG_debug << "Slac UNMATCHED";
if (!r_slac.empty()) {
EVLOG_debug << "Slac trigger matching";
r_slac[0]->call_reset();
r_slac[0]->call_trigger_matching();
sim_data.slac_state = "TRIGGERED";
}
}
if (sim_data.slac_state == "MATCHED") {
EVLOG_debug << "Slac Matched";
return true;
}
return false;
}
bool CarSimulation::iso_wait_pwr_ready(const CmdArguments& arguments) {
if (sim_data.iso_pwr_ready) {
sim_data.state = SimState::ISO_POWER_READY;
return true;
}
return false;
}
bool CarSimulation::iso_dc_power_on(const CmdArguments& arguments) {
sim_data.state = SimState::ISO_POWER_READY;
if (sim_data.dc_power_on) {
sim_data.state = SimState::ISO_CHARGING_REGULATED;
r_ev_board_support->call_allow_power_on(true);
charge_mode = ChargeMode::DC;
return true;
}
if (sim_data.iso_charger_paused) {
const auto cmds =
std::array<std::string, 2>{"pause;iso_wait_v2g_session_stopped;sleep 2;iso_wait_pwm_is_running;",
"iso_wait_pwr_ready;iso_wait_for_stop 36000"};
EVLOG_info << "Charger wants to pause the session";
r_ev_board_support->call_allow_power_on(false);
// NOTE(sl): Change when the Energymode has more then 2 values
const std::string energy_mode = (sim_data.energy_mode == EnergyMode::AC) ? "AC" : "DC";
const std::string iso_start_v2g_session = "iso_start_v2g_session " + energy_mode + ";";
auto& modify_session_cmds = sim_data.modify_charging_session_cmds.emplace();
modify_session_cmds = cmds[0];
modify_session_cmds += iso_start_v2g_session;
modify_session_cmds += cmds[1];
sim_data.iso_pwr_ready = false;
sim_data.sleep_ticks_left.reset();
sim_data.iso_charger_paused = false;
// NOTE(sl): return false, otherwise the simulation will end too early before the session cmds can be adjusted
}
return false;
}
bool CarSimulation::iso_start_v2g_session(const CmdArguments& arguments, bool three_phases) {
const auto& energy_mode = arguments[0];
const auto& payment_option = arguments[1];
const auto& departure_time = std::stoi(arguments[2]);
const auto& e_amount = std::stoi(arguments[3]);
const auto selected_payment_option = [payment_option,
this](bool auto_payment_option) -> types::iso15118::SelectedPaymentOption {
if (auto_payment_option) {
return types::iso15118::SelectedPaymentOption{};
}
auto selected_payment_option =
types::iso15118::SelectedPaymentOption{std::nullopt, config.force_payment_option};
if (payment_option == "externalpayment") {
selected_payment_option.payment_option = types::iso15118::PaymentOption::ExternalPayment;
} else if (payment_option == "contract") {
selected_payment_option.payment_option = types::iso15118::PaymentOption::Contract;
} else {
EVLOG_warning << "Go back to auto payment because payment_option was not recognized";
selected_payment_option.enforce_payment_option.reset();
}
return selected_payment_option;
}(payment_option == "auto");
if (energy_mode == constants::AC) {
sim_data.energy_mode = EnergyMode::AC;
if (not three_phases) {
r_ev[0]->call_start_charging(types::iso15118::EnergyTransferMode::AC_single_phase_core,
selected_payment_option, departure_time, e_amount);
charge_mode = ChargeMode::AC;
} else {
r_ev[0]->call_start_charging(types::iso15118::EnergyTransferMode::AC_three_phase_core,
selected_payment_option, departure_time, e_amount);
charge_mode = ChargeMode::ACThreePhase;
}
} else if (energy_mode == constants::DC) {
r_ev[0]->call_start_charging(types::iso15118::EnergyTransferMode::DC_extended, selected_payment_option,
departure_time, e_amount);
sim_data.energy_mode = EnergyMode::DC;
charge_mode = ChargeMode::DC;
} else {
return false;
}
return true;
}
bool CarSimulation::iso_draw_power_regulated(const CmdArguments& arguments) {
charge_current_a = std::stod(arguments[0]);
r_ev_board_support->call_set_ac_max_current(charge_current_a);
if (arguments[1] == constants::THREE_PHASES) {
r_ev_board_support->call_set_three_phases(true);
charge_mode = ChargeMode::ACThreePhase;
} else {
r_ev_board_support->call_set_three_phases(false);
charge_mode = ChargeMode::AC;
}
sim_data.state = SimState::ISO_CHARGING_REGULATED;
return true;
}
bool CarSimulation::iso_stop_charging(const CmdArguments& arguments) {
r_ev[0]->call_stop_charging();
r_ev_board_support->call_allow_power_on(false);
sim_data.state = SimState::PLUGGED_IN;
charge_mode = ChargeMode::None;
return true;
}
bool CarSimulation::iso_wait_for_stop(const CmdArguments& arguments, size_t loop_interval_ms) {
if (not sim_data.sleep_ticks_left.has_value()) {
const auto sleep_time_ms = std::stold(arguments[0]) * 1000;
sim_data.sleep_ticks_left = static_cast<long long>(sleep_time_ms / loop_interval_ms) + 1;
}
auto& sleep_ticks_left = sim_data.sleep_ticks_left.value();
sleep_ticks_left -= 1;
if (not(sleep_ticks_left > 0)) {
r_ev[0]->call_stop_charging();
r_ev_board_support->call_allow_power_on(false);
sim_data.state = SimState::PLUGGED_IN;
sim_data.sleep_ticks_left.reset();
return true;
}
if (sim_data.iso_stopped) {
EVLOG_info << "POWER OFF iso stopped";
r_ev_board_support->call_allow_power_on(false);
sim_data.state = SimState::PLUGGED_IN;
sim_data.sleep_ticks_left.reset();
return true;
}
if (sim_data.iso_charger_paused) {
const auto cmds =
std::array<std::string, 2>{"pause;iso_wait_v2g_session_stopped;sleep 2;iso_wait_pwm_is_running;",
"iso_wait_pwr_ready;iso_wait_for_stop 36000"};
EVLOG_info << "Charger wants to pause the session";
r_ev_board_support->call_allow_power_on(false);
// NOTE(sl): Change when the Energymode has more then 2 values
const std::string energy_mode = (sim_data.energy_mode == EnergyMode::AC) ? "AC" : "DC";
const std::string iso_start_v2g_session = "iso_start_v2g_session " + energy_mode + ";";
auto& modify_session_cmds = sim_data.modify_charging_session_cmds.emplace();
modify_session_cmds = cmds[0];
modify_session_cmds += iso_start_v2g_session;
modify_session_cmds += cmds[1];
sim_data.iso_pwr_ready = false;
sim_data.sleep_ticks_left.reset();
sim_data.iso_charger_paused = false;
// NOTE(sl): return false, otherwise the simulation will end too early before the session cmds can be adjusted
return false;
}
return false;
}
bool CarSimulation::iso_wait_v2g_session_stopped(const CmdArguments& arguments) {
if (sim_data.v2g_finished) {
sim_data.v2g_finished = false;
return true;
}
return false;
}
bool CarSimulation::iso_pause_charging(const CmdArguments& arguments) {
r_ev[0]->call_pause_charging();
sim_data.state = SimState::PLUGGED_IN;
sim_data.iso_pwr_ready = false;
return true;
}
bool CarSimulation::iso_wait_for_resume(const CmdArguments& arguments) {
return false;
}
bool CarSimulation::iso_start_bcb_toggle(const CmdArguments& arguments) {
sim_data.v2g_finished = false;
sim_data.state = SimState::BCB_TOGGLE;
if (sim_data.bcb_toggles >= std::stoul(arguments[0]) || sim_data.bcb_toggles == 3) {
sim_data.bcb_toggles = 0;
sim_data.state = SimState::PLUGGED_IN;
return true;
}
return false;
}
bool CarSimulation::wait_for_real_plugin(const CmdArguments& arguments) {
using types::board_support_common::Event;
if (sim_data.actual_bsp_event == Event::A) {
EVLOG_info << "Real plugin detected";
sim_data.state = SimState::PLUGGED_IN;
return true;
}
return false;
}