diff --git a/libraries/SITL/examples/EvaluateBatteryModel/EvaluateBatteryModel.cpp b/libraries/SITL/examples/EvaluateBatteryModel/EvaluateBatteryModel.cpp new file mode 100644 index 0000000000..1e131f758a --- /dev/null +++ b/libraries/SITL/examples/EvaluateBatteryModel/EvaluateBatteryModel.cpp @@ -0,0 +1,87 @@ +#include + +#include +#include + +#include + +/* run with: + ./waf configure --board linux + ./waf build --targets examples/EvaluateBatteryModel + ./build/linux/examples/EvaluateBatteryModel Tools/autotest/models/Callisto.json 50 +*/ + +void setup(); +void loop(); + +const AP_HAL::HAL& hal = AP_HAL::get_HAL(); + +// helper to give access to protected functions +class Frame_helper : public SITL::Frame { +public: + using SITL::Frame::Frame; + + void public_load_frame_params(const char *model_json) { + load_frame_params(model_json); + } + + float get_capacity() { return model.battCapacityAh; } + float get_max_volt() { return model.maxVoltage; } + float get_resistance() { return model.refBatRes; } + +}; + +Frame_helper frame {nullptr, 0, nullptr}; +SITL::Battery battery; + +void setup(void) +{ + uint8_t argc; + char * const *argv; + + hal.util->commandline_arguments(argc, argv); + + if (argc <= 2) { + ::printf("pass .json model definition path and current draw\n"); + exit(0); + } + + // parse JSON vehicle definition + frame.public_load_frame_params(argv[1]); + + // parse current draw + const float current = strtof(argv[2], NULL); + + const float amp_hour_capacity = frame.get_capacity(); + const float resistance = frame.get_resistance(); + const float max_voltage = frame.get_max_volt(); + const float min_voltage = max_voltage * 0.7; + + ::printf("Simulating %0.2fv, %0.2f ah battery with resistance of %f\n", max_voltage, amp_hour_capacity, resistance); + ::printf("Voltage from %0.2f to %0.2f with constant current draw of %0.2f\n", max_voltage, min_voltage, current); + + // setup battery model + battery.setup(amp_hour_capacity, resistance, max_voltage); + battery.init_voltage(max_voltage); + + uint64_t time = 0; + hal.scheduler->stop_clock(time); + const double time_step = 0.05; + + ::printf("time, voltage\n"); + while (battery.get_voltage() >= min_voltage) { + battery.set_current(current); + + ::printf("%0.2f, %0.2f\n", time * 1.0e-6, battery.get_voltage()); + + time += time_step*1e6; + hal.scheduler->stop_clock(time); + } +} + +void loop(void) +{ + exit(0); +} + +AP_HAL_MAIN(); diff --git a/libraries/SITL/examples/EvaluateBatteryModel/wscript b/libraries/SITL/examples/EvaluateBatteryModel/wscript new file mode 100644 index 0000000000..3b6de3be64 --- /dev/null +++ b/libraries/SITL/examples/EvaluateBatteryModel/wscript @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# encoding: utf-8 + +def build(bld): + + if bld.env.BOARD != 'linux': + return + + source = bld.path.ant_glob('*.cpp') + source.append('../../../../libraries/SITL/SIM_Battery.cpp') + source.append('../../../../libraries/SITL/SIM_Frame.cpp') + + bld.env.DEFINES.append("USE_PICOJSON=1") + + bld.ap_program( + use='ap', + program_groups=['examples'], + source=source, + ) +