diff --git a/libraries/SITL/SIM_Frame.cpp b/libraries/SITL/SIM_Frame.cpp index 083544d14c..bf38da1302 100644 --- a/libraries/SITL/SIM_Frame.cpp +++ b/libraries/SITL/SIM_Frame.cpp @@ -243,3 +243,18 @@ void Frame::calculate_forces(const Aircraft &aircraft, aircraft.rand_normal(0, 1)) * accel_noise * noise_scale; } + +// calculate current and voltage +void Frame::current_and_voltage(const struct sitl_input &input, float &voltage, float ¤t) +{ + voltage = 0; + current = 0; + for (uint8_t i=0; ibatt_voltage - motor_speed * 0.7; +} diff --git a/libraries/SITL/SIM_Motor.h b/libraries/SITL/SIM_Motor.h index 8bbf451a3a..47e629ee2f 100644 --- a/libraries/SITL/SIM_Motor.h +++ b/libraries/SITL/SIM_Motor.h @@ -76,5 +76,9 @@ public: Vector3f &body_thrust); // Z is down uint16_t update_servo(uint16_t demand, uint64_t time_usec, float &last_value); + + // calculate current and voltage + void current_and_voltage(const struct sitl_input &input, float &voltage, float ¤t, uint8_t motor_offset); }; + } diff --git a/libraries/SITL/SIM_Multicopter.cpp b/libraries/SITL/SIM_Multicopter.cpp index 875c8e08f4..877aa83216 100644 --- a/libraries/SITL/SIM_Multicopter.cpp +++ b/libraries/SITL/SIM_Multicopter.cpp @@ -63,6 +63,9 @@ void MultiCopter::update(const struct sitl_input &input) calculate_forces(input, rot_accel, accel_body); + // estimate voltage and current + frame->current_and_voltage(input, battery_voltage, battery_current); + update_dynamics(rot_accel); update_external_payload(input); diff --git a/libraries/SITL/SIM_QuadPlane.cpp b/libraries/SITL/SIM_QuadPlane.cpp index e6ae2d9a82..c3b4fd1998 100644 --- a/libraries/SITL/SIM_QuadPlane.cpp +++ b/libraries/SITL/SIM_QuadPlane.cpp @@ -109,6 +109,19 @@ void QuadPlane::update(const struct sitl_input &input) frame->calculate_forces(*this, input, quad_rot_accel, quad_accel_body); + // estimate voltage and current + frame->current_and_voltage(input, battery_voltage, battery_current); + + float throttle; + if (reverse_thrust) { + throttle = filtered_servo_angle(input, 2); + } else { + throttle = filtered_servo_range(input, 2); + } + // assume 20A at full fwd throttle + throttle = fabsf(throttle); + battery_current += 20 * throttle; + rot_accel += quad_rot_accel; accel_body += quad_accel_body;