Skip to content

Commit 24d074a

Browse files
authored
Merge pull request #468 from rosflight/jacob/use-current-battery-in-mixer
Use battery voltage as max voltage in mixer
2 parents 75a8a7a + fe5144c commit 24d074a

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

include/sensors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class Sensors : public ParamListenerInterface
122122
void correct_mag(void);
123123
void correct_baro(void);
124124
void correct_diff_pressure(void);
125+
void lpf_battery(void);
125126
void update_battery_monitor_multipliers(void);
126127

127128
// IMU calibration
@@ -151,6 +152,8 @@ class Sensors : public ParamListenerInterface
151152
// Battery Monitor
152153
float battery_voltage_alpha_{0.995};
153154
float battery_current_alpha_{0.995};
155+
float previous_battery_voltage_{0.0};
156+
float previous_battery_current_{0.0};
154157
};
155158

156159
} // namespace rosflight_firmware

src/controller.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ void Controller::calculate_max_thrust()
8787
float KQ = KV;
8888
int num_motors = RF_.params_.get_param_int(PARAM_NUM_MOTORS);
8989

90+
// Note: As voltage drops during flight, this max voltage stays constant.
91+
// This means that a constant throttle setting will result in a constant
92+
// output thrust command (if converting from throttle to thrust).
9093
float V_max = RF_.params_.get_param_float(PARAM_VOLT_MAX);
9194
float a = R * rho * pow(D, 5.0) * CQ / (4 * pow(M_PI, 2.0) * KQ);
9295
float b = KV;

src/mixer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ void Mixer::param_change_callback(uint16_t param_id)
121121
case PARAM_PROP_CT: C_T_ = RF_.params_.get_param_float(PARAM_PROP_CT); break;
122122
case PARAM_PROP_CQ: C_Q_ = RF_.params_.get_param_float(PARAM_PROP_CQ); break;
123123
case PARAM_NUM_MOTORS: num_motors_ = RF_.params_.get_param_int(PARAM_NUM_MOTORS); break;
124-
case PARAM_VOLT_MAX: V_max_ = RF_.params_.get_param_float(PARAM_VOLT_MAX); break;
125124
case PARAM_MOTOR_PWM_SEND_RATE:
126125
init_PWM();
127126
break;
@@ -265,7 +264,6 @@ void Mixer::update_parameters()
265264
C_T_ = RF_.params_.get_param_float(PARAM_PROP_CT);
266265
C_Q_ = RF_.params_.get_param_float(PARAM_PROP_CQ);
267266
num_motors_ = RF_.params_.get_param_int(PARAM_NUM_MOTORS);
268-
V_max_ = RF_.params_.get_param_float(PARAM_VOLT_MAX);
269267
}
270268

271269
Mixer::mixer_t Mixer::invert_mixer(const mixer_t* mixer_to_invert)
@@ -479,7 +477,8 @@ float Mixer::mix_multirotor_with_motor_parameters(Controller::Output commands)
479477
+ R_ * i_0_ + K_V_ * sqrt(omega_squared);
480478

481479
// Convert desired V_in setting to a throttle setting
482-
outputs_[i] = V_in / V_max_;
480+
BatteryStruct * batt = RF_.sensors_.get_battery();
481+
outputs_[i] = V_in / batt->voltage;
483482

484483
// Save off the largest control output (for motors) if it is greater than 1.0 for future scaling
485484
if (abs(outputs_[i]) > max_output) { max_output = abs(outputs_[i]); }

src/sensors.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ const float Sensors::DIFF_PRESSURE_MAX_CALIBRATION_VARIANCE = 100.0; // standard
5252

5353
Sensors::Sensors(ROSflight & rosflight)
5454
: rf_(rosflight)
55-
{}
55+
{
56+
previous_battery_voltage_ = rf_.params_.get_param_float(PARAM_VOLT_MAX);
57+
}
5658

5759
void Sensors::init()
5860
{
@@ -169,7 +171,9 @@ got_flags Sensors::run()
169171
got.sonar = rf_.board_.sonar_read(&sonar_);
170172

171173
// BATTERY_MONITOR:
172-
got.battery = rf_.board_.battery_read(&battery_);
174+
if ((got.battery = rf_.board_.battery_read(&battery_))) {
175+
lpf_battery();
176+
}
173177

174178
return got;
175179
}
@@ -477,6 +481,14 @@ void Sensors::correct_diff_pressure()
477481
* sqrt((fabs(diff_pressure_.pressure) / (0.5 * 1.225)));
478482
}
479483

484+
void Sensors::lpf_battery()
485+
{
486+
battery_.voltage = battery_.voltage * (1.0 - battery_voltage_alpha_) + previous_battery_voltage_ * battery_voltage_alpha_;
487+
battery_.current = battery_.current * (1.0 - battery_current_alpha_) + previous_battery_current_ * battery_current_alpha_;
488+
previous_battery_voltage_ = battery_.voltage;
489+
previous_battery_current_ = battery_.current;
490+
}
491+
480492
void Sensors::update_battery_monitor_multipliers()
481493
{
482494
float voltage_multiplier = this->rf_.params_.get_param_float(PARAM_BATTERY_VOLTAGE_MULTIPLIER);

0 commit comments

Comments
 (0)