123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- #include <AP_HAL/AP_HAL.h>
- #include <AP_Math/AP_Math.h>
- #include "AP_MotorsTailsitter.h"
- #include <GCS_MAVLink/GCS.h>
- extern const AP_HAL::HAL& hal;
- #define SERVO_OUTPUT_RANGE 4500
- void AP_MotorsTailsitter::init(motor_frame_class frame_class, motor_frame_type frame_type)
- {
-
- uint8_t chan;
-
- SRV_Channels::set_aux_channel_default(SRV_Channel::k_throttleRight, CH_1);
- if (SRV_Channels::find_channel(SRV_Channel::k_throttleRight, chan)) {
- motor_enabled[chan] = true;
- }
-
- SRV_Channels::set_aux_channel_default(SRV_Channel::k_throttleLeft, CH_2);
- if (SRV_Channels::find_channel(SRV_Channel::k_throttleLeft, chan)) {
- motor_enabled[chan] = true;
- }
-
- SRV_Channels::set_aux_channel_default(SRV_Channel::k_tiltMotorRight, CH_3);
- SRV_Channels::set_angle(SRV_Channel::k_tiltMotorRight, SERVO_OUTPUT_RANGE);
-
- SRV_Channels::set_aux_channel_default(SRV_Channel::k_tiltMotorLeft, CH_4);
- SRV_Channels::set_angle(SRV_Channel::k_tiltMotorLeft, SERVO_OUTPUT_RANGE);
-
- _flags.initialised_ok = (frame_class == MOTOR_FRAME_TAILSITTER);
- }
- AP_MotorsTailsitter::AP_MotorsTailsitter(uint16_t loop_rate, uint16_t speed_hz) :
- AP_MotorsMulticopter(loop_rate, speed_hz)
- {
- set_update_rate(speed_hz);
- }
- void AP_MotorsTailsitter::set_update_rate(uint16_t speed_hz)
- {
-
- _speed_hz = speed_hz;
- SRV_Channels::set_rc_frequency(SRV_Channel::k_throttleLeft, speed_hz);
- SRV_Channels::set_rc_frequency(SRV_Channel::k_throttleRight, speed_hz);
- }
- void AP_MotorsTailsitter::output_to_motors()
- {
- if (!_flags.initialised_ok) {
- return;
- }
- switch (_spool_state) {
- case SpoolState::SHUT_DOWN:
- SRV_Channels::set_output_pwm(SRV_Channel::k_throttleLeft, get_pwm_output_min());
- SRV_Channels::set_output_pwm(SRV_Channel::k_throttleRight, get_pwm_output_min());
- break;
- case SpoolState::GROUND_IDLE:
- set_actuator_with_slew(_actuator[1], actuator_spin_up_to_ground_idle());
- SRV_Channels::set_output_pwm(SRV_Channel::k_throttleLeft, output_to_pwm(actuator_spin_up_to_ground_idle()));
- SRV_Channels::set_output_pwm(SRV_Channel::k_throttleRight, output_to_pwm(actuator_spin_up_to_ground_idle()));
- break;
- case SpoolState::SPOOLING_UP:
- case SpoolState::THROTTLE_UNLIMITED:
- case SpoolState::SPOOLING_DOWN:
- SRV_Channels::set_output_pwm(SRV_Channel::k_throttleLeft, output_to_pwm(thrust_to_actuator(_thrust_left)));
- SRV_Channels::set_output_pwm(SRV_Channel::k_throttleRight, output_to_pwm(thrust_to_actuator(_thrust_right)));
- break;
- }
-
- SRV_Channels::set_output_scaled(SRV_Channel::k_tiltMotorLeft, _tilt_left*SERVO_OUTPUT_RANGE);
- SRV_Channels::set_output_scaled(SRV_Channel::k_tiltMotorRight, _tilt_right*SERVO_OUTPUT_RANGE);
- }
- uint16_t AP_MotorsTailsitter::get_motor_mask()
- {
- uint32_t motor_mask = 0;
- uint8_t chan;
- if (SRV_Channels::find_channel(SRV_Channel::k_throttleLeft, chan)) {
- motor_mask |= 1U << chan;
- }
- if (SRV_Channels::find_channel(SRV_Channel::k_throttleRight, chan)) {
- motor_mask |= 1U << chan;
- }
-
- motor_mask |= AP_MotorsMulticopter::get_motor_mask();
- return motor_mask;
- }
- void AP_MotorsTailsitter::output_armed_stabilizing()
- {
- float roll_thrust;
- float pitch_thrust;
- float yaw_thrust;
- float throttle_thrust;
- float thrust_max;
- float thr_adj = 0.0f;
-
- const float compensation_gain = get_compensation_gain();
- roll_thrust = (_roll_in + _roll_in_ff) * compensation_gain;
- pitch_thrust = (_pitch_in + _pitch_in_ff) * compensation_gain;
- yaw_thrust = (_yaw_in + _yaw_in_ff) * compensation_gain;
- throttle_thrust = get_throttle() * compensation_gain;
-
- if (throttle_thrust <= 0.0f) {
- throttle_thrust = 0.0f;
- limit.throttle_lower = true;
- }
- if (throttle_thrust >= _throttle_thrust_max) {
- throttle_thrust = _throttle_thrust_max;
- limit.throttle_upper = true;
- }
-
- _thrust_left = throttle_thrust + roll_thrust * 0.5f;
- _thrust_right = throttle_thrust - roll_thrust * 0.5f;
-
- thrust_max = MAX(_thrust_right,_thrust_left);
- if (thrust_max > 1.0f) {
- thr_adj = 1.0f - thrust_max;
- limit.throttle_upper = true;
- limit.roll = true;
- limit.pitch = true;
- }
-
- _thrust_left = constrain_float(_thrust_left + thr_adj, 0.0f, 1.0f);
- _thrust_right = constrain_float(_thrust_right + thr_adj, 0.0f, 1.0f);
- _throttle = throttle_thrust + thr_adj;
-
- _tilt_left = pitch_thrust - yaw_thrust;
- _tilt_right = pitch_thrust + yaw_thrust;
- }
- void AP_MotorsTailsitter::output_test_seq(uint8_t motor_seq, int16_t pwm)
- {
-
- if (!armed()) {
- return;
- }
-
- switch (motor_seq) {
- case 1:
-
- SRV_Channels::set_output_pwm(SRV_Channel::k_throttleRight, pwm);
- break;
- case 2:
-
- SRV_Channels::set_output_pwm(SRV_Channel::k_tiltMotorRight, pwm);
- break;
- case 3:
-
- SRV_Channels::set_output_pwm(SRV_Channel::k_throttleLeft, pwm);
- break;
- case 4:
-
- SRV_Channels::set_output_pwm(SRV_Channel::k_tiltMotorLeft, pwm);
- break;
- default:
-
- break;
- }
- }
|