2013-05-29 20:51:34 -03:00
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
2012-04-02 05:26:37 -03:00
/// @file AP_MotorsHeli.h
/// @brief Motor control class for Traditional Heli
2012-10-26 20:59:07 -03:00
# ifndef __AP_MOTORS_HELI_H__
# define __AP_MOTORS_HELI_H__
2012-04-02 05:26:37 -03:00
# include <inttypes.h>
# include <AP_Common.h>
# include <AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
# include <RC_Channel.h> // RC Channel Library
2012-10-26 20:59:07 -03:00
# include "AP_Motors.h"
2012-06-02 04:12:00 -03:00
2013-11-04 00:13:54 -04:00
// maximum number of swashplate servos
# define AP_MOTORS_HELI_NUM_SWASHPLATE_SERVOS 3
2012-04-02 05:26:37 -03:00
2013-11-04 00:13:54 -04:00
// servo output rates
# define AP_MOTORS_HELI_SPEED_DEFAULT 125 // default servo update rate for helicopters
# define AP_MOTORS_HELI_SPEED_DIGITAL_SERVOS 125 // update rate for digital servos
# define AP_MOTORS_HELI_SPEED_ANALOG_SERVOS 125 // update rate for analog servos
2012-04-02 05:26:37 -03:00
2013-07-22 16:23:16 -03:00
// TradHeli Aux Function Output Channels
# define AP_MOTORS_HELI_AUX CH_7
# define AP_MOTORS_HELI_RSC CH_8
2013-11-04 00:13:54 -04:00
// servo position defaults
# define AP_MOTORS_HELI_SERVO1_POS -60
# define AP_MOTORS_HELI_SERVO2_POS 60
# define AP_MOTORS_HELI_SERVO3_POS 180
2012-04-02 05:26:37 -03:00
2013-11-04 00:13:54 -04:00
// swash type definitions
# define AP_MOTORS_HELI_SWASH_CCPM 0
# define AP_MOTORS_HELI_SWASH_H1 1
2012-05-30 22:50:25 -03:00
2013-11-04 00:13:54 -04:00
// default swash min and max angles and positions
2014-01-09 18:21:06 -04:00
# define AP_MOTORS_HELI_SWASH_ROLL_MAX 2500
# define AP_MOTORS_HELI_SWASH_PITCH_MAX 2500
2013-11-04 00:13:54 -04:00
# define AP_MOTORS_HELI_COLLECTIVE_MIN 1250
# define AP_MOTORS_HELI_COLLECTIVE_MAX 1750
# define AP_MOTORS_HELI_COLLECTIVE_MID 1500
2012-08-21 17:54:01 -03:00
2013-11-06 08:39:10 -04:00
// swash min and max position while in stabilize mode (as a number from 0 ~ 100)
2013-11-04 07:53:27 -04:00
# define AP_MOTORS_HELI_MANUAL_COLLECTIVE_MIN 0
# define AP_MOTORS_HELI_MANUAL_COLLECTIVE_MAX 100
2013-11-04 00:13:54 -04:00
2013-11-06 08:39:10 -04:00
// swash min while landed or landing (as a number from 0 ~ 1000
# define AP_MOTORS_HELI_LAND_COLLECTIVE_MIN 0
2013-07-22 16:23:16 -03:00
// tail types
# define AP_MOTORS_HELI_TAILTYPE_SERVO 0
# define AP_MOTORS_HELI_TAILTYPE_SERVO_EXTGYRO 1
# define AP_MOTORS_HELI_TAILTYPE_DIRECTDRIVE_VARPITCH 2
# define AP_MOTORS_HELI_TAILTYPE_DIRECTDRIVE_FIXEDPITCH 3
2013-11-04 00:13:54 -04:00
// default external gyro gain (ch7 out)
2013-11-08 04:32:50 -04:00
# define AP_MOTORS_HELI_EXT_GYRO_GAIN 350
2013-07-22 16:23:16 -03:00
// minimum outputs for direct drive motors
2014-01-09 18:21:06 -04:00
# define AP_MOTOR_HELI_DDTAIL_DEFAULT 500
2013-07-22 16:23:16 -03:00
2013-11-04 00:13:54 -04:00
2013-11-04 07:53:27 -04:00
// main rotor speed control types (ch8 out)
2013-11-07 03:07:53 -04:00
# define AP_MOTORS_HELI_RSC_MODE_NONE 0 // main rotor ESC is directly connected to receiver, pilot controls ESC speed through transmitter directly
# define AP_MOTORS_HELI_RSC_MODE_CH8_PASSTHROUGH 1 // main rotor ESC is connected to RC8 (out), pilot desired rotor speed provided by CH8 input
# define AP_MOTORS_HELI_RSC_MODE_SETPOINT 2 // main rotor ESC is connected to RC8 (out), desired speed is held in RSC_SETPOINT parameter
2013-11-04 00:13:54 -04:00
2013-11-07 03:07:53 -04:00
// default main rotor speed (ch8 out) as a number from 0 ~ 1000
# define AP_MOTORS_HELI_RSC_SETPOINT 500
// default main rotor ramp up time in seconds
2013-11-09 00:38:33 -04:00
# define AP_MOTORS_HELI_RSC_RAMP_TIME 1 // 1 second to ramp output to main rotor ESC to full power (most people use exterrnal govenors so we can ramp up quickly)
# define AP_MOTORS_HELI_RSC_RUNUP_TIME 10 // 10 seconds for rotor to reach full speed
2013-11-07 03:07:53 -04:00
# define AP_MOTORS_HELI_TAIL_RAMP_INCREMENT 5 // 5 is 2 seconds for direct drive tail rotor to reach to full speed (5 = (2sec*100hz)/1000)
2013-11-04 00:13:54 -04:00
// motor run-up time default in 100th of seconds
# define AP_MOTORS_HELI_MOTOR_RUNUP_TIME 500 // 500 = 5 seconds
// flybar types
# define AP_MOTORS_HELI_NOFLYBAR 0
# define AP_MOTORS_HELI_FLYBAR 1
2012-05-30 22:50:25 -03:00
class AP_HeliControls ;
2012-08-21 23:19:52 -03:00
/// @class AP_MotorsHeli
2012-04-02 05:26:37 -03:00
class AP_MotorsHeli : public AP_Motors {
2012-08-21 23:19:52 -03:00
public :
/// Constructor
2014-02-10 00:22:38 -04:00
AP_MotorsHeli ( RC_Channel & rc_roll ,
RC_Channel & rc_pitch ,
RC_Channel & rc_throttle ,
RC_Channel & rc_yaw ,
RC_Channel & servo_aux ,
RC_Channel & servo_rotor ,
RC_Channel & swash_servo_1 ,
RC_Channel & swash_servo_2 ,
RC_Channel & swash_servo_3 ,
RC_Channel & yaw_servo ,
2012-08-21 23:19:52 -03:00
uint16_t speed_hz = AP_MOTORS_HELI_SPEED_DEFAULT ) :
2013-01-02 00:27:58 -04:00
AP_Motors ( rc_roll , rc_pitch , rc_throttle , rc_yaw , speed_hz ) ,
2013-11-07 03:07:53 -04:00
_servo_aux ( servo_aux ) ,
_servo_rsc ( servo_rotor ) ,
2012-08-21 23:19:52 -03:00
_servo_1 ( swash_servo_1 ) ,
_servo_2 ( swash_servo_2 ) ,
_servo_3 ( swash_servo_3 ) ,
_servo_4 ( yaw_servo ) ,
2013-11-04 00:13:54 -04:00
_roll_scaler ( 1 ) ,
_pitch_scaler ( 1 ) ,
_collective_scalar ( 1 ) ,
2013-11-04 07:53:27 -04:00
_collective_out ( 0 ) ,
_collective_mid_pwm ( 0 ) ,
2013-11-08 04:29:54 -04:00
_rotor_desired ( 0 ) ,
2013-11-07 03:07:53 -04:00
_rotor_out ( 0 ) ,
_rsc_ramp_increment ( 0.0f ) ,
2013-11-09 00:38:33 -04:00
_rsc_runup_increment ( 0.0f ) ,
_rotor_speed_estimate ( 0.0f ) ,
2014-05-11 20:18:16 -03:00
_tail_direct_drive_out ( 0 ) ,
2014-05-11 20:35:26 -03:00
_dt ( 0.01f ) ,
_delta_phase_angle ( 0 )
2012-08-21 23:19:52 -03:00
{
2012-12-12 17:52:23 -04:00
AP_Param : : setup_object_defaults ( this , var_info ) ;
2012-08-21 23:19:52 -03:00
2013-11-04 07:53:27 -04:00
// initialise flags
_heliflags . swash_initialised = 0 ;
_heliflags . landing_collective = 0 ;
_heliflags . motor_runup_complete = 0 ;
} ;
2012-08-21 23:19:52 -03:00
// init
void Init ( ) ;
2012-09-13 09:31:13 -03:00
// set update rate to motors - a value in hertz
2012-08-21 23:19:52 -03:00
// you must have setup_motors before calling this
void set_update_rate ( uint16_t speed_hz ) ;
// enable - starts allowing signals to be sent to motors
void enable ( ) ;
// output_min - sends minimum values out to the motors
void output_min ( ) ;
2014-04-28 04:29:49 -03:00
// output_test - spin a motor at the pwm value specified
// motor_seq is the motor's sequence number from 1 to the number of motors on the frame
// pwm value is an actual pwm value that will be output, normally in the range of 1000 ~ 2000
virtual void output_test ( uint8_t motor_seq , int16_t pwm ) ;
2012-08-21 23:19:52 -03:00
2013-11-04 07:53:27 -04:00
//
// heli specific methods
//
// allow_arming - returns true if main rotor is spinning and it is ok to arm
2014-01-30 03:30:04 -04:00
bool allow_arming ( ) const ;
2013-11-04 07:53:27 -04:00
2013-07-22 16:23:16 -03:00
// _tail_type - returns the tail type (servo, servo with ext gyro, direct drive var pitch, direct drive fixed pitch)
2014-01-30 03:30:04 -04:00
int16_t tail_type ( ) const { return _tail_type ; }
2013-11-04 07:53:27 -04:00
2013-11-07 03:07:53 -04:00
// ext_gyro_gain - gets and sets external gyro gain as a pwm (1000~2000)
2014-01-30 03:30:04 -04:00
int16_t ext_gyro_gain ( ) const { return _ext_gyro_gain ; }
2013-11-07 03:07:53 -04:00
void ext_gyro_gain ( int16_t pwm ) { _ext_gyro_gain = pwm ; }
2013-11-04 07:53:27 -04:00
// has_flybar - returns true if we have a mechical flybar
2014-01-30 03:30:04 -04:00
bool has_flybar ( ) const { return _flybar_mode ; }
2013-11-04 07:53:27 -04:00
// get_collective_mid - returns collective mid position as a number from 0 ~ 1000
2014-01-30 03:30:04 -04:00
int16_t get_collective_mid ( ) const { return _collective_mid ; }
2013-11-04 07:53:27 -04:00
// get_collective_out - returns collective position from last output as a number from 0 ~ 1000
2014-01-30 03:30:04 -04:00
int16_t get_collective_out ( ) const { return _collective_out ; }
2013-11-04 07:53:27 -04:00
// set_collective_for_landing - limits collective from going too low if we know we are landed
void set_collective_for_landing ( bool landing ) { _heliflags . landing_collective = landing ; }
2013-11-08 04:29:54 -04:00
// get_rsc_mode - gets the rotor speed control method (AP_MOTORS_HELI_RSC_MODE_NONE, AP_MOTORS_HELI_RSC_MODE_CH8_PASSTHROUGH or AP_MOTORS_HELI_RSC_MODE_SETPOINT)
2014-01-30 03:30:04 -04:00
uint8_t get_rsc_mode ( ) const { return _rsc_mode ; }
2013-11-08 04:29:54 -04:00
// get_rsc_setpoint - gets contents of _rsc_setpoint parameter (0~1000)
2014-01-30 03:30:04 -04:00
int16_t get_rsc_setpoint ( ) const { return _rsc_setpoint ; }
2013-11-08 04:29:54 -04:00
// set_desired_rotor_speed - sets target rotor speed as a number from 0 ~ 1000
void set_desired_rotor_speed ( int16_t desired_speed ) ;
2013-11-04 07:53:27 -04:00
// return true if the main rotor is up to speed
2014-01-30 03:30:04 -04:00
bool motor_runup_complete ( ) const ;
2012-08-21 23:19:52 -03:00
2013-11-07 03:07:53 -04:00
// recalc_scalers - recalculates various scalers used. Should be called at about 1hz to allow users to see effect of changing parameters
void recalc_scalers ( ) ;
2013-11-10 08:05:33 -04:00
// get_phase_angle - returns phase angle
2014-01-30 03:30:04 -04:00
int16_t get_phase_angle ( ) const { return _phase_angle ; }
2013-11-10 08:05:33 -04:00
2012-08-21 23:19:52 -03:00
// var_info for holding Parameter information
static const struct AP_Param : : GroupInfo var_info [ ] ;
2014-05-11 20:18:16 -03:00
2014-05-11 20:35:26 -03:00
// set_dt for setting main loop rate time
2014-05-11 20:18:16 -03:00
void set_dt ( float dt ) { _dt = dt ; }
2014-05-11 20:35:26 -03:00
2014-05-11 20:50:03 -03:00
// set_delta_phase_angle for setting variable phase angle compensation and force
// recalculation of collective factors
void set_delta_phase_angle ( int16_t angle ) ;
2012-04-02 05:26:37 -03:00
2012-05-30 22:50:25 -03:00
protected :
2012-08-21 23:19:52 -03:00
2013-11-04 07:53:27 -04:00
// output - sends commands to the motors
void output_armed ( ) ;
void output_disarmed ( ) ;
private :
2012-08-21 23:19:52 -03:00
// heli_move_swash - moves swash plate to attitude of parameters passed in
2012-12-08 16:12:39 -04:00
void move_swash ( int16_t roll_out , int16_t pitch_out , int16_t coll_in , int16_t yaw_out ) ;
2012-08-21 23:19:52 -03:00
// reset_swash - free up swash for maximum movements. Used for set-up
void reset_swash ( ) ;
2013-11-04 07:53:27 -04:00
// init_swash - initialise the swash plate
void init_swash ( ) ;
2012-08-21 23:19:52 -03:00
2013-11-04 07:53:27 -04:00
// calculate_roll_pitch_collective_factors - calculate factors based on swash type and servo position
void calculate_roll_pitch_collective_factors ( ) ;
2013-11-07 03:07:53 -04:00
// rsc_control - main function to update values to send to main rotor and tail rotor ESCs
2013-11-08 04:29:54 -04:00
void rsc_control ( ) ;
2013-11-07 03:07:53 -04:00
// rotor_ramp - ramps rotor towards target. result put rotor_out and sent to ESC
void rotor_ramp ( int16_t rotor_target ) ;
// tail_ramp - ramps tail motor towards target. Only used for direct drive variable pitch tails
// results put into _tail_direct_drive_out and sent to ESC
void tail_ramp ( int16_t tail_target ) ;
// return true if the tail rotor is up to speed
bool tail_rotor_runup_complete ( ) ;
// write_rsc - outputs pwm onto output rsc channel (ch8). servo_out parameter is of the range 0 ~ 1000
void write_rsc ( int16_t servo_out ) ;
// write_aux - outputs pwm onto output aux channel (ch7). servo_out parameter is of the range 0 ~ 1000
void write_aux ( int16_t servo_out ) ;
2012-08-21 23:19:52 -03:00
2013-11-04 07:53:27 -04:00
// external objects we depend upon
2014-02-10 00:22:38 -04:00
RC_Channel & _servo_aux ; // output to ext gyro gain and tail direct drive esc (ch7)
RC_Channel & _servo_rsc ; // output to main rotor esc (ch8)
RC_Channel & _servo_1 ; // swash plate servo #1
RC_Channel & _servo_2 ; // swash plate servo #2
RC_Channel & _servo_3 ; // swash plate servo #3
RC_Channel & _servo_4 ; // tail servo
2013-11-04 07:53:27 -04:00
// flags bitmask
struct heliflags_type {
uint8_t swash_initialised : 1 ; // true if swash has been initialised
uint8_t landing_collective : 1 ; // true if collective is setup for landing which has much higher minimum
uint8_t motor_runup_complete : 1 ; // true if the rotors have had enough time to wind up
} _heliflags ;
// parameters
AP_Int16 _servo1_pos ; // Angular location of swash servo #1
AP_Int16 _servo2_pos ; // Angular location of swash servo #2
AP_Int16 _servo3_pos ; // Angular location of swash servo #3
AP_Int16 _roll_max ; // Maximum roll angle of the swash plate in centi-degrees
AP_Int16 _pitch_max ; // Maximum pitch angle of the swash plate in centi-degrees
AP_Int16 _collective_min ; // Lowest possible servo position for the swashplate
AP_Int16 _collective_max ; // Highest possible servo position for the swashplate
AP_Int16 _collective_mid ; // Swash servo position corresponding to zero collective pitch (or zero lift for Assymetrical blades)
2013-07-22 16:23:16 -03:00
AP_Int16 _tail_type ; // Tail type used: Servo, Servo with external gyro, direct drive variable pitch or direct drive fixed pitch
2013-11-04 07:53:27 -04:00
AP_Int8 _swash_type ; // Swash Type Setting - either 3-servo CCPM or H1 Mechanical Mixing
2013-11-07 03:07:53 -04:00
AP_Int16 _ext_gyro_gain ; // PWM sent to external gyro on ch7 when tail type is Servo w/ ExtGyro
2013-11-04 07:53:27 -04:00
AP_Int8 _servo_manual ; // Pass radio inputs directly to servos during set-up through mission planner
AP_Int16 _phase_angle ; // Phase angle correction for rotor head. If pitching the swash forward induces a roll, this can be correct the problem
AP_Int16 _collective_yaw_effect ; // Feed-forward compensation to automatically add rudder input when collective pitch is increased. Can be positive or negative depending on mechanics.
2013-11-07 03:07:53 -04:00
AP_Int16 _rsc_setpoint ; // rotor speed when RSC mode is set to is enabledv
AP_Int8 _rsc_mode ; // Which main rotor ESC control mode is active
2013-11-09 00:38:33 -04:00
AP_Int8 _rsc_ramp_time ; // Time in seconds for the output to the main rotor's ESC to reach full speed
AP_Int8 _rsc_runup_time ; // Time in seconds for the main rotor to reach full speed. Must be longer than _rsc_ramp_time
2013-11-04 07:53:27 -04:00
AP_Int8 _flybar_mode ; // Flybar present or not. Affects attitude controller used during ACRO flight mode
2013-11-06 08:39:10 -04:00
AP_Int16 _land_collective_min ; // Minimum collective when landed or landing
2013-11-07 03:07:53 -04:00
AP_Int16 _direct_drive_tailspeed ; // Direct Drive VarPitch Tail ESC speed (0 ~ 1000)
2013-11-04 07:53:27 -04:00
// internal variables
float _rollFactor [ AP_MOTORS_HELI_NUM_SWASHPLATE_SERVOS ] ;
float _pitchFactor [ AP_MOTORS_HELI_NUM_SWASHPLATE_SERVOS ] ;
float _collectiveFactor [ AP_MOTORS_HELI_NUM_SWASHPLATE_SERVOS ] ;
float _roll_scaler ; // scaler to convert roll input from radio (i.e. -4500 ~ 4500) to max roll range
float _pitch_scaler ; // scaler to convert pitch input from radio (i.e. -4500 ~ 4500) to max pitch range
float _collective_scalar ; // collective scalar to convert pwm form (i.e. 0 ~ 1000) passed in to actual servo range (i.e 1250~1750 would be 500)
float _collective_scalar_manual ; // collective scalar to reduce the range of the collective movement while collective is being controlled manually (i.e. directly by the pilot)
int16_t _collective_out ; // actual collective pitch value. Required by the main code for calculating cruise throttle
int16_t _collective_mid_pwm ; // collective mid parameter value converted to pwm form (i.e. 0 ~ 1000)
2013-11-08 04:29:54 -04:00
int16_t _rotor_desired ; // latest desired rotor speed from pilot
float _rotor_out ; // latest output sent to the main rotor or an estimate of the rotors actual speed (whichever is higher) (0 ~ 1000)
2013-11-07 03:07:53 -04:00
float _rsc_ramp_increment ; // the amount we can increase the rotor output during each 100hz iteration
2013-11-09 00:38:33 -04:00
float _rsc_runup_increment ; // the amount we can increase the rotor's estimated speed during each 100hz iteration
float _rotor_speed_estimate ; // estimated speed of the main rotor (0~1000)
2013-11-07 03:07:53 -04:00
int16_t _tail_direct_drive_out ; // current ramped speed of output on ch7 when using direct drive variable pitch tail type
2014-05-11 20:18:16 -03:00
float _dt ; // main loop time
2014-05-11 20:35:26 -03:00
int16_t _delta_phase_angle ; // phase angle dynamic compensation
2012-08-21 23:19:52 -03:00
} ;
2012-04-02 05:26:37 -03:00
2012-08-06 22:02:14 -03:00
# endif // AP_MOTORSHELI