Skip to content

Commit

Permalink
AP_Motors: Add current limiting to 6DOF motors for Sub
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxxzer committed Apr 29, 2018
1 parent c2eee2d commit e5bd6e2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
43 changes: 43 additions & 0 deletions libraries/AP_Motors/AP_Motors6DOF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* AP_Motors6DOF.cpp - ArduSub motors library
*/

#include <AP_BattMonitor/AP_BattMonitor.h>
#include <AP_HAL/AP_HAL.h>
#include "AP_Motors6DOF.h"

Expand Down Expand Up @@ -266,6 +267,11 @@ void AP_Motors6DOF::output_to_motors()
}
}

float AP_Motors6DOF::get_current_limit_max_throttle()
{
return 1.0f;
}

// output_armed - sends commands to the motors
// includes new scaling stability patch
// TODO pull code that is common to output_armed_not_stabilizing into helper functions
Expand Down Expand Up @@ -338,6 +344,43 @@ void AP_Motors6DOF::output_armed_stabilizing()
}
}
}

const AP_BattMonitor &battery = AP::battery();

// Current limiting
if (_batt_current_max <= 0.0f || !battery.has_current()) {
return;
}

float _batt_current = battery.current_amps();

float _batt_current_delta = _batt_current - _batt_current_last;

float loop_interval = 1.0f/_loop_rate;

float _current_change_rate = _batt_current_delta / loop_interval;

float predicted_current = _batt_current + (_current_change_rate * loop_interval * 5);

float batt_current_ratio = _batt_current/_batt_current_max;

float predicted_current_ratio = predicted_current/_batt_current_max;
_batt_current_last = _batt_current;

if (predicted_current > _batt_current_max * 1.5f) {
batt_current_ratio = 2.5f;
} else if (_batt_current < _batt_current_max && predicted_current > _batt_current_max) {
batt_current_ratio = predicted_current_ratio;
}
_output_limited += (loop_interval/(loop_interval+_batt_current_time_constant)) * (1 - batt_current_ratio);

_output_limited = constrain_float(_output_limited, 0.0f, 1.0f);

for (uint8_t i = 0; i < AP_MOTORS_MAX_NUM_MOTORS; i++) {
if (motor_enabled[i]) {
_thrust_rpyt_out[i] *= _output_limited;
}
}
}

// output_armed - sends commands to the motors
Expand Down
6 changes: 6 additions & 0 deletions libraries/AP_Motors/AP_Motors6DOF.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class AP_Motors6DOF : public AP_MotorsMatrix {
static const struct AP_Param::GroupInfo var_info[];

protected:
// return current_limit as a number from 0 ~ 1 in the range throttle_min to throttle_max
float get_current_limit_max_throttle() override;

//Override MotorsMatrix method
void add_motor_raw_6dof(int8_t motor_num, float roll_fac, float pitch_fac, float yaw_fac, float climb_fac, float forward_fac, float lat_fac, uint8_t testing_order);
Expand All @@ -60,4 +62,8 @@ class AP_Motors6DOF : public AP_MotorsMatrix {
float _throttle_factor[AP_MOTORS_MAX_NUM_MOTORS]; // each motors contribution to throttle (climb/descent)
float _forward_factor[AP_MOTORS_MAX_NUM_MOTORS]; // each motors contribution to forward/backward
float _lateral_factor[AP_MOTORS_MAX_NUM_MOTORS]; // each motors contribution to lateral (left/right)

// current limiting
float _output_limited = 1.0f;
float _batt_current_last = 0.0f;
};
2 changes: 1 addition & 1 deletion libraries/AP_Motors/AP_MotorsMulticopter.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class AP_MotorsMulticopter : public AP_Motors {
virtual void update_throttle_filter();

// return current_limit as a number from 0 ~ 1 in the range throttle_min to throttle_max
float get_current_limit_max_throttle();
virtual float get_current_limit_max_throttle();

// apply_thrust_curve_and_volt_scaling - returns throttle in the range 0 ~ 1
float apply_thrust_curve_and_volt_scaling(float thrust) const;
Expand Down

0 comments on commit e5bd6e2

Please sign in to comment.