Skip to content

Commit

Permalink
removed air density as a parameter. Using ideal gas law
Browse files Browse the repository at this point in the history
  • Loading branch information
JMoore5353 committed Dec 17, 2024
1 parent 0ee7dde commit 4173d59
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 11 deletions.
1 change: 0 additions & 1 deletion include/mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ class Mixer : public ParamListenerInterface

// Define parameters for the mixer
float R_; // Motor resistance
float rho_; // Air density
float K_V_; // Motor back-emf constant
float K_Q_; // Motor torque constant
float i_0_; // Motor no-load current
Expand Down
2 changes: 0 additions & 2 deletions include/param.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ enum : uint16_t
PARAM_BAUD_RATE = 0,
PARAM_SERIAL_DEVICE,

PARAM_AIR_DENSITY,

PARAM_NUM_MOTORS,
PARAM_MOTOR_RESISTANCE,
PARAM_MOTOR_KV,
Expand Down
3 changes: 3 additions & 0 deletions include/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class Sensors : public ParamListenerInterface
Sensors(ROSflight & rosflight);

inline const Data & data() const { return data_; }
inline float rho() { return rho_; }
void get_filtered_IMU(turbomath::Vector & accel, turbomath::Vector & gyro, uint64_t & stamp_us);

// function declarations
Expand Down Expand Up @@ -227,6 +228,8 @@ class Sensors : public ParamListenerInterface
float accel_[3] = {0, 0, 0};
float gyro_[3] = {0, 0, 0};

float rho_ = 1.225;

bool calibrating_acc_flag_ = false;
bool calibrating_gyro_flag_ = false;
uint8_t next_sensor_to_update_ = BAROMETER;
Expand Down
4 changes: 1 addition & 3 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void Controller::calculate_max_thrust()
{
float R = RF_.params_.get_param_float(PARAM_MOTOR_RESISTANCE);
float D = RF_.params_.get_param_float(PARAM_PROP_DIAMETER);
float rho = RF_.params_.get_param_float(PARAM_AIR_DENSITY);
float rho = RF_.sensors_.rho();
float CQ = RF_.params_.get_param_float(PARAM_PROP_CQ);
float CT = RF_.params_.get_param_float(PARAM_PROP_CT);
float KV = RF_.params_.get_param_float(PARAM_MOTOR_KV);
Expand Down Expand Up @@ -210,8 +210,6 @@ void Controller::param_change_callback(uint16_t param_id)
case PARAM_PID_YAW_RATE_D:
case PARAM_MOTOR_RESISTANCE:
case PARAM_PROP_DIAMETER:
case PARAM_AIR_DENSITY:
case PARAM_PROP_CQ:
case PARAM_PROP_CT:
case PARAM_MOTOR_KV:
case PARAM_NO_LOAD_CURRENT:
Expand Down
6 changes: 3 additions & 3 deletions src/mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ void Mixer::param_change_callback(uint16_t param_id)
init_mixing();
break;
case PARAM_MOTOR_RESISTANCE:
case PARAM_AIR_DENSITY:
case PARAM_MOTOR_KV:
case PARAM_NO_LOAD_CURRENT:
case PARAM_PROP_DIAMETER:
Expand Down Expand Up @@ -325,7 +324,6 @@ void Mixer::init_mixing()
void Mixer::update_parameters()
{
R_ = RF_.params_.get_param_float(PARAM_MOTOR_RESISTANCE);
rho_ = RF_.params_.get_param_float(PARAM_AIR_DENSITY);
K_V_ = RF_.params_.get_param_float(PARAM_MOTOR_KV);
K_Q_ = K_V_;
i_0_ = RF_.params_.get_param_float(PARAM_NO_LOAD_CURRENT);
Expand Down Expand Up @@ -610,6 +608,8 @@ float Mixer::mix_multirotor_with_motor_parameters(Controller::Output commands)
// Mix the inputs
float max_output = 1.0;

float rho = RF_.sensors_.rho();

for (uint8_t i = 0; i < NUM_MIXER_OUTPUTS; i++) {
if ((*mixer_to_use_.output_type)[i] == M) {
// Matrix multiply to mix outputs for Motor type
Expand All @@ -625,7 +625,7 @@ float Mixer::mix_multirotor_with_motor_parameters(Controller::Output commands)

// Ch. 4, setting equation for torque produced by a propeller equal to Eq. 4.19
// Note that we assume constant advance ratio, leading to constant torque and thrust constants.
float V_in = rho_ * pow(D_, 5.0) / (4.0 * pow(M_PI, 2.0)) * omega_squared * C_Q_ * R_ / K_Q_
float V_in = rho * pow(D_, 5.0) / (4.0 * pow(M_PI, 2.0)) * omega_squared * C_Q_ * R_ / K_Q_
+ R_ * i_0_ + K_V_ * sqrt(omega_squared);

// Convert desired V_in setting to a throttle setting
Expand Down
2 changes: 0 additions & 2 deletions src/param.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ void Params::set_defaults(void)
init_param_int(PARAM_BAUD_RATE, "BAUD_RATE", 921600); // Baud rate of MAVlink communication with companion computer | 9600 | 921600
init_param_int(PARAM_SERIAL_DEVICE, "SERIAL_DEVICE", 0); // Serial Port (for supported devices) | 0 | 3

init_param_float(PARAM_AIR_DENSITY, "AIR_DENSITY", 1.225f); // Density of the air (kg/m^3) | 0 | 1000.0

init_param_int(PARAM_NUM_MOTORS, "NUM_MOTORS", 4); // Number of vertical-facing motors on the vehicle | 1 | 8
init_param_float(PARAM_MOTOR_RESISTANCE, "MOTOR_RESISTANCE", 0.042f); // Electrical resistance of the motor windings (ohms) | 0 | 1000.0
init_param_float(PARAM_MOTOR_KV, "MOTOR_KV", 0.01706f); // Back emf constant of the motor in SI units (V/rad/s) | 0 | 1000.0
Expand Down
2 changes: 2 additions & 0 deletions src/sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ got_flags Sensors::run()
got.baro = true;
rf_.board_.baro_read(&data_.baro_pressure, &data_.baro_temperature);
correct_baro();

rho_ = (0.0289644 * data_.baro_pressure) / (8.31432 * data_.baro_temperature);
}
}

Expand Down

5 comments on commit 4173d59

@avtoku
Copy link
Contributor

@avtoku avtoku commented on 4173d59 Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The baro sensor temperature measures the sensor chip's internal temperature and not the outside air temperature.
Rather than rho_ = (0.0289644 * data_.baro_pressure) / (8.31432 * data_.baro_temperature);
Assume we are in a standard atmosphere:
rho_ = 1.225 * pow( data_.baro_pressure/101325.0, 0.809736894596450 );

@JMoore5353
Copy link
Contributor Author

@JMoore5353 JMoore5353 commented on 4173d59 Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks. I was going to ask about that. Do you have a reference for that equation? $\rho = \rho_{STP} * (P / P_0)^{x}$ --> I'm not sure where the exponent comes from.

@avtoku
Copy link
Contributor

@avtoku avtoku commented on 4173d59 Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exponent comes from the hydrostatic model of the atmosphere. In terms of those things, the exponent = 1-RGamma/g, where R = 287.052874 J/kg-K, Gamma = 6.5e-3 K/m, and g = 9.80665 m/s^2
1-R
Gamma/g = 0.809736894_760188_. The last few digits are different from the one in my equation above because of rounding.

@avtoku
Copy link
Contributor

@avtoku avtoku commented on 4173d59 Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a reference. for the pressure vs. altitude: http://mae-nas.eng.usu.edu/MAE_6530_Web/New_Course/Section4/[Section4.2](http://mae-nas.eng.usu.edu/MAE_6530_Web/New_Course/Section4/Section4.2.pdf).pdf With a little more algebra and you get the relationship for rho vs. pressure.

@JMoore5353
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Please sign in to comment.