mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-01-08 17:08:28 -04:00
daec4ea10e
Further to refactor of RC_Channel class which included adding get_xx set_xx methods, change reads and writes to the public members to calls to get and set functionsss old public member(int16_t) get function -> int16_t set function (int16_t) (expression where c is an object of type RC_Channel) c.radio_in c.get_radio_in() c.set_radio_in(v) c.control_in c.get_control_in() c.set_control_in(v) c.servo_out c.get_servo_out() c.set_servo_out(v) c.pwm_out c.get_pwm_out() // use existing c.radio_out c.get_radio_out() c.set_radio_out(v) c.radio_max c.get_radio_max() c.set_radio_max(v) c.radio_min c.get_radio_min() c.set_radio_min(v) c.radio_trim c.get_radio_trim() c.set_radio_trim(v); c.min_max_configured() // return true if min and max are configured Because data members of RC_Channels are now private and so cannot be written directly some overloads are provided in the Plane classes to provide the old functionality new overload Plane::stick_mix_channel(RC_Channel *channel) which forwards to the previously existing void stick_mix_channel(RC_Channel *channel, int16_t &servo_out); new overload Plane::channel_output_mixer(Rc_Channel* , RC_Channel*)const which forwards to (uint8_t mixing_type, int16_t & chan1, int16_t & chan2)const; Rename functions RC_Channel_aux::set_radio_trim(Aux_servo_function_t function) to RC_Channel_aux::set_trim_to_radio_in_for(Aux_servo_function_t function) RC_Channel_aux::set_servo_out(Aux_servo_function_t function, int16_t value) to RC_Channel_aux::set_servo_out_for(Aux_servo_function_t function, int16_t value) Rationale: RC_Channel is a complicated class, which combines several functionalities dealing with stick inputs in pwm and logical units, logical and actual actuator outputs, unit conversion etc, etc The intent of this PR is to clarify existing use of the class. At the basic level it should now be possible to grep all places where private variable is set by searching for the set_xx function. (The wider purpose is to provide a more generic and logically simpler method of output mixing. This is a small step)
168 lines
6.1 KiB
C++
168 lines
6.1 KiB
C++
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
/*
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include "AP_MotorsHeli_RSC.h"
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
// init_servo - servo initialization on start-up
|
|
void AP_MotorsHeli_RSC::init_servo()
|
|
{
|
|
// setup RSC on specified channel by default
|
|
RC_Channel_aux::set_aux_channel_default(_aux_fn, _default_channel);
|
|
}
|
|
|
|
// set_power_output_range
|
|
void AP_MotorsHeli_RSC::set_power_output_range(uint16_t power_low, uint16_t power_high)
|
|
{
|
|
_power_output_low = power_low;
|
|
_power_output_high = power_high;
|
|
_power_output_range = _power_output_high - _power_output_low;
|
|
}
|
|
|
|
// output - update value to send to ESC/Servo
|
|
void AP_MotorsHeli_RSC::output(RotorControlState state)
|
|
{
|
|
switch (state){
|
|
case ROTOR_CONTROL_STOP:
|
|
// set rotor ramp to decrease speed to zero, this happens instantly inside update_rotor_ramp()
|
|
update_rotor_ramp(0.0f);
|
|
|
|
// control output forced to zero
|
|
_control_output = 0.0f;
|
|
break;
|
|
|
|
case ROTOR_CONTROL_IDLE:
|
|
// set rotor ramp to decrease speed to zero
|
|
update_rotor_ramp(0.0f);
|
|
|
|
// set rotor control speed to idle speed parameter, this happens instantly and ignore ramping
|
|
_control_output = _idle_output;
|
|
break;
|
|
|
|
case ROTOR_CONTROL_ACTIVE:
|
|
// set main rotor ramp to increase to full speed
|
|
update_rotor_ramp(1.0f);
|
|
|
|
if ((_control_mode == ROTOR_CONTROL_MODE_SPEED_PASSTHROUGH) || (_control_mode == ROTOR_CONTROL_MODE_SPEED_SETPOINT)) {
|
|
// set control rotor speed to ramp slewed value between idle and desired speed
|
|
_control_output = _idle_output + (_rotor_ramp_output * (_desired_speed - _idle_output));
|
|
} else if (_control_mode == ROTOR_CONTROL_MODE_OPEN_LOOP_POWER_OUTPUT) {
|
|
// throttle output depending on estimated power demand. Output is ramped up from idle speed during rotor runup.
|
|
_control_output = _idle_output + (_rotor_ramp_output * ((_power_output_low - _idle_output) + (_power_output_range * _load_feedforward)));
|
|
}
|
|
break;
|
|
}
|
|
|
|
// update rotor speed run-up estimate
|
|
update_rotor_runup();
|
|
|
|
// output to rsc servo
|
|
write_rsc(_control_output);
|
|
}
|
|
|
|
// update_rotor_ramp - slews rotor output scalar between 0 and 1, outputs float scalar to _rotor_ramp_output
|
|
void AP_MotorsHeli_RSC::update_rotor_ramp(float rotor_ramp_input)
|
|
{
|
|
// sanity check ramp time
|
|
if (_ramp_time <= 0) {
|
|
_ramp_time = 1;
|
|
}
|
|
|
|
// ramp output upwards towards target
|
|
if (_rotor_ramp_output < rotor_ramp_input) {
|
|
// allow control output to jump to estimated speed
|
|
if (_rotor_ramp_output < _rotor_runup_output) {
|
|
_rotor_ramp_output = _rotor_runup_output;
|
|
}
|
|
// ramp up slowly to target
|
|
_rotor_ramp_output += (1.0f / (_ramp_time * _loop_rate));
|
|
if (_rotor_ramp_output > rotor_ramp_input) {
|
|
_rotor_ramp_output = rotor_ramp_input;
|
|
}
|
|
}else{
|
|
// ramping down happens instantly
|
|
_rotor_ramp_output = rotor_ramp_input;
|
|
}
|
|
}
|
|
|
|
// update_rotor_runup - function to slew rotor runup scalar, outputs float scalar to _rotor_runup_ouptut
|
|
void AP_MotorsHeli_RSC::update_rotor_runup()
|
|
{
|
|
// sanity check runup time
|
|
if (_runup_time < _ramp_time) {
|
|
_runup_time = _ramp_time;
|
|
}
|
|
if (_runup_time <= 0 ) {
|
|
_runup_time = 1;
|
|
}
|
|
|
|
// ramp speed estimate towards control out
|
|
float runup_increment = 1.0f / (_runup_time * _loop_rate);
|
|
if (_rotor_runup_output < _rotor_ramp_output) {
|
|
_rotor_runup_output += runup_increment;
|
|
if (_rotor_runup_output > _rotor_ramp_output) {
|
|
_rotor_runup_output = _rotor_ramp_output;
|
|
}
|
|
}else{
|
|
_rotor_runup_output -= runup_increment;
|
|
if (_rotor_runup_output < _rotor_ramp_output) {
|
|
_rotor_runup_output = _rotor_ramp_output;
|
|
}
|
|
}
|
|
|
|
// update run-up complete flag
|
|
|
|
// if control mode is disabled, then run-up complete always returns true
|
|
if ( _control_mode == ROTOR_CONTROL_MODE_DISABLED ){
|
|
_runup_complete = true;
|
|
return;
|
|
}
|
|
|
|
// if rotor ramp and runup are both at full speed, then run-up has been completed
|
|
if (!_runup_complete && (_rotor_ramp_output >= 1.0f) && (_rotor_runup_output >= 1.0f)) {
|
|
_runup_complete = true;
|
|
}
|
|
// if rotor speed is less than critical speed, then run-up is not complete
|
|
// this will prevent the case where the target rotor speed is less than critical speed
|
|
if (_runup_complete && (get_rotor_speed() <= _critical_speed)) {
|
|
_runup_complete = false;
|
|
}
|
|
}
|
|
|
|
// get_rotor_speed - gets rotor speed either as an estimate, or (ToDO) a measured value
|
|
float AP_MotorsHeli_RSC::get_rotor_speed() const
|
|
{
|
|
// if no actual measured rotor speed is available, estimate speed based on rotor runup scalar.
|
|
return _rotor_runup_output;
|
|
}
|
|
|
|
// write_rsc - outputs pwm onto output rsc channel
|
|
// servo_out parameter is of the range 0 ~ 1
|
|
void AP_MotorsHeli_RSC::write_rsc(float servo_out)
|
|
{
|
|
if (_control_mode == ROTOR_CONTROL_MODE_DISABLED){
|
|
// do not do servo output to avoid conflicting with other output on the channel
|
|
// ToDo: We should probably use RC_Channel_Aux to avoid this problem
|
|
return;
|
|
} else {
|
|
RC_Channel_aux::set_servo_out_for(RC_Channel_aux::k_heli_rsc, servo_out * 1000.0f);
|
|
}
|
|
}
|