123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #include "Sub.h"
- bool Sub::acro_init()
- {
-
- pos_control.set_alt_target(0);
-
-
- set_neutral_controls();
- return true;
- }
- void Sub::acro_run()
- {
- float target_roll, target_pitch, target_yaw;
-
- if (!motors.armed()) {
- motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
- attitude_control.set_throttle_out(0,true,g.throttle_filt);
- attitude_control.relax_attitude_controllers();
- return;
- }
- motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
-
- get_pilot_desired_angle_rates(channel_roll->get_control_in(), channel_pitch->get_control_in(), channel_yaw->get_control_in(), target_roll, target_pitch, target_yaw);
-
- attitude_control.input_rate_bf_roll_pitch_yaw(target_roll, target_pitch, target_yaw);
-
- attitude_control.set_throttle_out(channel_throttle->norm_input(), false, g.throttle_filt);
-
-
- motors.set_forward(channel_forward->norm_input());
- motors.set_lateral(channel_lateral->norm_input());
- }
- void Sub::get_pilot_desired_angle_rates(int16_t roll_in, int16_t pitch_in, int16_t yaw_in, float &roll_out, float &pitch_out, float &yaw_out)
- {
- float rate_limit;
- Vector3f rate_ef_level, rate_bf_level, rate_bf_request;
-
- float total_in = norm(pitch_in, roll_in);
- if (total_in > ROLL_PITCH_INPUT_MAX) {
- float ratio = (float)ROLL_PITCH_INPUT_MAX / total_in;
- roll_in *= ratio;
- pitch_in *= ratio;
- }
-
- if (g.acro_expo <= 0) {
- rate_bf_request.x = roll_in * g.acro_rp_p;
- rate_bf_request.y = pitch_in * g.acro_rp_p;
- } else {
-
- float rp_in, rp_in3, rp_out;
-
- if (g.acro_expo > 1.0f) {
- g.acro_expo = 1.0f;
- }
-
- rp_in = float(roll_in)/ROLL_PITCH_INPUT_MAX;
- rp_in3 = rp_in*rp_in*rp_in;
- rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
- rate_bf_request.x = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
-
- rp_in = float(pitch_in)/ROLL_PITCH_INPUT_MAX;
- rp_in3 = rp_in*rp_in*rp_in;
- rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
- rate_bf_request.y = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
- }
-
- rate_bf_request.z = yaw_in * g.acro_yaw_p;
-
- if (g.acro_trainer != ACRO_TRAINER_DISABLED) {
-
- int32_t roll_angle = wrap_180_cd(ahrs.roll_sensor);
- rate_ef_level.x = -constrain_int32(roll_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_roll;
-
- int32_t pitch_angle = wrap_180_cd(ahrs.pitch_sensor);
- rate_ef_level.y = -constrain_int32(pitch_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_pitch;
-
- rate_ef_level.z = 0;
-
- if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
- if (roll_angle > aparm.angle_max) {
- rate_ef_level.x -= g.acro_balance_roll*(roll_angle-aparm.angle_max);
- } else if (roll_angle < -aparm.angle_max) {
- rate_ef_level.x -= g.acro_balance_roll*(roll_angle+aparm.angle_max);
- }
- if (pitch_angle > aparm.angle_max) {
- rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle-aparm.angle_max);
- } else if (pitch_angle < -aparm.angle_max) {
- rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle+aparm.angle_max);
- }
- }
-
- attitude_control.euler_rate_to_ang_vel(attitude_control.get_att_target_euler_cd()*radians(0.01f), rate_ef_level, rate_bf_level);
-
- if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
- rate_bf_request.x += rate_bf_level.x;
- rate_bf_request.y += rate_bf_level.y;
- rate_bf_request.z += rate_bf_level.z;
- } else {
- float acro_level_mix = constrain_float(1-MAX(MAX(abs(roll_in), abs(pitch_in)), abs(yaw_in))/4500.0, 0, 1)*ahrs.cos_pitch();
-
- rate_bf_level = rate_bf_level*acro_level_mix;
-
- rate_limit = fabsf(fabsf(rate_bf_request.x)-fabsf(rate_bf_level.x));
- rate_bf_request.x += rate_bf_level.x;
- rate_bf_request.x = constrain_float(rate_bf_request.x, -rate_limit, rate_limit);
-
- rate_limit = fabsf(fabsf(rate_bf_request.y)-fabsf(rate_bf_level.y));
- rate_bf_request.y += rate_bf_level.y;
- rate_bf_request.y = constrain_float(rate_bf_request.y, -rate_limit, rate_limit);
-
- rate_limit = fabsf(fabsf(rate_bf_request.z)-fabsf(rate_bf_level.z));
- rate_bf_request.z += rate_bf_level.z;
- rate_bf_request.z = constrain_float(rate_bf_request.z, -rate_limit, rate_limit);
- }
- }
-
- roll_out = rate_bf_request.x;
- pitch_out = rate_bf_request.y;
- yaw_out = rate_bf_request.z;
- }
|