2016-02-17 21:25:38 -04:00
|
|
|
#pragma once
|
2012-10-26 20:59:07 -03:00
|
|
|
|
2015-08-11 03:28:44 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Math/AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
|
|
|
|
#include <AP_Notify/AP_Notify.h> // Notify library
|
2017-01-03 05:56:57 -04:00
|
|
|
#include <SRV_Channel/SRV_Channel.h>
|
2015-08-11 03:28:44 -03:00
|
|
|
#include <Filter/Filter.h> // filter library
|
2012-10-26 20:59:07 -03:00
|
|
|
|
2015-09-28 23:58:52 -03:00
|
|
|
// offsets for motors in motor_out and _motor_filtered arrays
|
|
|
|
#define AP_MOTORS_MOT_1 0U
|
|
|
|
#define AP_MOTORS_MOT_2 1U
|
|
|
|
#define AP_MOTORS_MOT_3 2U
|
|
|
|
#define AP_MOTORS_MOT_4 3U
|
|
|
|
#define AP_MOTORS_MOT_5 4U
|
|
|
|
#define AP_MOTORS_MOT_6 5U
|
|
|
|
#define AP_MOTORS_MOT_7 6U
|
|
|
|
#define AP_MOTORS_MOT_8 7U
|
2017-05-13 22:15:49 -03:00
|
|
|
#define AP_MOTORS_MOT_9 8U
|
|
|
|
#define AP_MOTORS_MOT_10 9U
|
|
|
|
#define AP_MOTORS_MOT_11 10U
|
|
|
|
#define AP_MOTORS_MOT_12 11U
|
2012-10-26 20:59:07 -03:00
|
|
|
|
2017-05-13 22:15:49 -03:00
|
|
|
#define AP_MOTORS_MAX_NUM_MOTORS 12
|
2012-10-26 20:59:07 -03:00
|
|
|
|
|
|
|
// motor update rate
|
2013-05-14 04:56:55 -03:00
|
|
|
#define AP_MOTORS_SPEED_DEFAULT 490 // default output rate to the motors
|
2012-10-26 20:59:07 -03:00
|
|
|
|
|
|
|
/// @class AP_Motors
|
|
|
|
class AP_Motors {
|
|
|
|
public:
|
|
|
|
|
2016-12-14 01:46:42 -04:00
|
|
|
enum motor_frame_class {
|
|
|
|
MOTOR_FRAME_UNDEFINED = 0,
|
|
|
|
MOTOR_FRAME_QUAD = 1,
|
|
|
|
MOTOR_FRAME_HEXA = 2,
|
|
|
|
MOTOR_FRAME_OCTA = 3,
|
|
|
|
MOTOR_FRAME_OCTAQUAD = 4,
|
|
|
|
MOTOR_FRAME_Y6 = 5,
|
|
|
|
MOTOR_FRAME_HELI = 6,
|
|
|
|
MOTOR_FRAME_TRI = 7,
|
|
|
|
MOTOR_FRAME_SINGLE = 8,
|
2017-02-10 01:26:12 -04:00
|
|
|
MOTOR_FRAME_COAX = 9,
|
|
|
|
MOTOR_FRAME_TAILSITTER = 10,
|
2017-03-14 06:46:48 -03:00
|
|
|
MOTOR_FRAME_HELI_DUAL = 11,
|
2017-05-13 22:15:49 -03:00
|
|
|
MOTOR_FRAME_DODECAHEXA = 12,
|
2016-12-14 01:46:42 -04:00
|
|
|
};
|
|
|
|
enum motor_frame_type {
|
|
|
|
MOTOR_FRAME_TYPE_PLUS = 0,
|
|
|
|
MOTOR_FRAME_TYPE_X = 1,
|
|
|
|
MOTOR_FRAME_TYPE_V = 2,
|
|
|
|
MOTOR_FRAME_TYPE_H = 3,
|
|
|
|
MOTOR_FRAME_TYPE_VTAIL = 4,
|
|
|
|
MOTOR_FRAME_TYPE_ATAIL = 5,
|
2017-02-06 00:01:51 -04:00
|
|
|
MOTOR_FRAME_TYPE_Y6B = 10,
|
|
|
|
MOTOR_FRAME_TYPE_Y6F = 11 // for FireFlyY6
|
2016-12-14 01:46:42 -04:00
|
|
|
};
|
|
|
|
|
2012-10-26 20:59:07 -03:00
|
|
|
// Constructor
|
2015-05-14 22:00:46 -03:00
|
|
|
AP_Motors(uint16_t loop_rate, uint16_t speed_hz = AP_MOTORS_SPEED_DEFAULT);
|
2012-10-26 20:59:07 -03:00
|
|
|
|
2016-12-14 01:46:42 -04:00
|
|
|
// check initialisation succeeded
|
|
|
|
bool initialised_ok() const { return _flags.initialised_ok; }
|
2012-10-26 20:59:07 -03:00
|
|
|
|
|
|
|
// arm, disarm or check status status of motors
|
2016-07-01 02:35:29 -03:00
|
|
|
bool armed() const { return _flags.armed; }
|
2013-08-08 10:15:04 -03:00
|
|
|
void armed(bool arm);
|
2012-10-26 20:59:07 -03:00
|
|
|
|
2015-04-17 12:58:26 -03:00
|
|
|
// set motor interlock status
|
|
|
|
void set_interlock(bool set) { _flags.interlock = set;}
|
|
|
|
|
2015-05-01 00:16:32 -03:00
|
|
|
// get motor interlock status. true means motors run, false motors don't run
|
2016-07-01 02:35:29 -03:00
|
|
|
bool get_interlock() const { return _flags.interlock; }
|
2015-04-17 12:58:26 -03:00
|
|
|
|
2014-02-10 00:20:26 -04:00
|
|
|
// set_roll, set_pitch, set_yaw, set_throttle
|
2016-01-21 22:08:55 -04:00
|
|
|
void set_roll(float roll_in) { _roll_in = roll_in; }; // range -1 ~ +1
|
|
|
|
void set_pitch(float pitch_in) { _pitch_in = pitch_in; }; // range -1 ~ +1
|
|
|
|
void set_yaw(float yaw_in) { _yaw_in = yaw_in; }; // range -1 ~ +1
|
2016-03-22 23:28:18 -03:00
|
|
|
void set_throttle(float throttle_in) { _throttle_in = throttle_in; }; // range 0 ~ 1
|
2016-06-08 05:48:54 -03:00
|
|
|
void set_throttle_avg_max(float throttle_avg_max) { _throttle_avg_max = constrain_float(throttle_avg_max,0.0f,1.0f); }; // range 0 ~ 1
|
2016-01-21 01:58:55 -04:00
|
|
|
void set_throttle_filter_cutoff(float filt_hz) { _throttle_filter.set_cutoff_frequency(filt_hz); }
|
2016-01-01 18:20:32 -04:00
|
|
|
void set_forward(float forward_in) { _forward_in = forward_in; }; // range -1 ~ +1
|
|
|
|
void set_lateral(float lateral_in) { _lateral_in = lateral_in; }; // range -1 ~ +1
|
2016-01-21 01:58:55 -04:00
|
|
|
|
2015-01-24 00:57:32 -04:00
|
|
|
// accessors for roll, pitch, yaw and throttle inputs to motors
|
2016-01-21 22:08:55 -04:00
|
|
|
float get_roll() const { return _roll_in; }
|
|
|
|
float get_pitch() const { return _pitch_in; }
|
|
|
|
float get_yaw() const { return _yaw_in; }
|
2016-01-06 05:04:32 -04:00
|
|
|
float get_throttle() const { return constrain_float(_throttle_filter.get(),0.0f,1.0f); }
|
2016-01-01 18:20:32 -04:00
|
|
|
float get_throttle_bidirectional() const { return constrain_float(2*(_throttle_filter.get()-0.5f),-1.0f,1.0f); }
|
|
|
|
float get_forward() const { return _forward_in; }
|
|
|
|
float get_lateral() const { return _lateral_in; }
|
2016-03-22 10:57:26 -03:00
|
|
|
virtual float get_throttle_hover() const = 0;
|
2014-07-17 06:15:07 -03:00
|
|
|
|
2016-02-02 08:14:12 -04:00
|
|
|
// spool up states
|
|
|
|
enum spool_up_down_desired {
|
|
|
|
DESIRED_SHUT_DOWN = 0, // all motors stop
|
|
|
|
DESIRED_SPIN_WHEN_ARMED = 1, // all motors at spin when armed
|
2016-03-25 05:59:26 -03:00
|
|
|
DESIRED_THROTTLE_UNLIMITED = 2, // motors are no longer constrained by start up procedure
|
2016-02-02 08:14:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual void set_desired_spool_state(enum spool_up_down_desired spool) { _spool_desired = spool; };
|
|
|
|
|
2016-08-01 19:21:12 -03:00
|
|
|
enum spool_up_down_desired get_desired_spool_state(void) const { return _spool_desired; }
|
|
|
|
|
2015-07-13 01:39:57 -03:00
|
|
|
//
|
|
|
|
// voltage, current and air pressure compensation or limiting features - multicopters only
|
|
|
|
//
|
2015-02-23 02:35:10 -04:00
|
|
|
// set_voltage - set voltage to be used for output scaling
|
2015-07-13 01:39:57 -03:00
|
|
|
void set_voltage(float volts){ _batt_voltage = volts; }
|
2017-05-23 01:08:36 -03:00
|
|
|
void set_voltage_resting_estimate(float volts) { _batt_voltage_resting_estimate = volts; }
|
2015-02-23 02:35:10 -04:00
|
|
|
|
|
|
|
// set_current - set current to be used for output scaling
|
2015-07-13 01:39:57 -03:00
|
|
|
void set_current(float current){ _batt_current = current; }
|
2015-02-23 02:35:10 -04:00
|
|
|
|
2017-05-23 01:08:36 -03:00
|
|
|
// get and set battery resistance estimate
|
|
|
|
float get_batt_resistance() const { return _batt_resistance; }
|
|
|
|
void set_resistance(float resistance){ _batt_resistance = resistance; }
|
|
|
|
|
2015-04-28 15:19:50 -03:00
|
|
|
// set_density_ratio - sets air density as a proportion of sea level density
|
|
|
|
void set_air_density_ratio(float ratio) { _air_density_ratio = ratio; }
|
|
|
|
|
2013-07-21 01:58:24 -03:00
|
|
|
// structure for holding motor limit flags
|
|
|
|
struct AP_Motors_limit {
|
|
|
|
uint8_t roll_pitch : 1; // we have reached roll or pitch limit
|
|
|
|
uint8_t yaw : 1; // we have reached yaw limit
|
2013-07-25 12:45:59 -03:00
|
|
|
uint8_t throttle_lower : 1; // we have reached throttle's lower limit
|
|
|
|
uint8_t throttle_upper : 1; // we have reached throttle's upper limit
|
2013-07-21 01:58:24 -03:00
|
|
|
} limit;
|
|
|
|
|
2015-07-13 01:39:57 -03:00
|
|
|
//
|
|
|
|
// virtual functions that should be implemented by child classes
|
|
|
|
//
|
2012-10-26 20:59:07 -03:00
|
|
|
|
2016-12-14 01:34:15 -04:00
|
|
|
// set update rate to motors - a value in hertz
|
|
|
|
virtual void set_update_rate( uint16_t speed_hz ) { _speed_hz = speed_hz; }
|
|
|
|
|
2015-07-02 18:15:09 -03:00
|
|
|
// init
|
2016-12-14 01:46:42 -04:00
|
|
|
virtual void init(motor_frame_class frame_class, motor_frame_type frame_type) = 0;
|
|
|
|
|
|
|
|
// set frame class (i.e. quad, hexa, heli) and type (i.e. x, plus)
|
|
|
|
virtual void set_frame_class_and_type(motor_frame_class frame_class, motor_frame_type frame_type) = 0;
|
2012-10-26 20:59:07 -03:00
|
|
|
|
2015-07-02 18:15:09 -03:00
|
|
|
// enable - starts allowing signals to be sent to motors
|
|
|
|
virtual void enable() = 0;
|
2015-04-01 00:16:43 -03:00
|
|
|
|
2015-07-02 18:15:09 -03:00
|
|
|
// output - sends commands to the motors
|
|
|
|
virtual void output() = 0;
|
2015-02-23 05:09:08 -04:00
|
|
|
|
2015-07-02 18:15:09 -03:00
|
|
|
// output_min - sends minimum values out to the motors
|
|
|
|
virtual void output_min() = 0;
|
2013-05-26 23:21:31 -03:00
|
|
|
|
2015-07-02 18:15:09 -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) = 0;
|
2015-02-21 03:55:13 -04:00
|
|
|
|
2015-07-02 18:15:09 -03:00
|
|
|
// get_motor_mask - returns a bitmask of which outputs are being used for motors (1 means being used)
|
|
|
|
// this can be used to ensure other pwm outputs (i.e. for servos) do not conflict
|
|
|
|
virtual uint16_t get_motor_mask() = 0;
|
|
|
|
|
2016-02-03 07:55:55 -04:00
|
|
|
// pilot input in the -1 ~ +1 range for roll, pitch and yaw. 0~1 range for throttle
|
|
|
|
void set_radio_passthrough(float roll_input, float pitch_input, float throttle_input, float yaw_input);
|
|
|
|
|
2016-06-04 22:20:58 -03:00
|
|
|
// set loop rate. Used to support loop rate as a parameter
|
|
|
|
void set_loop_rate(uint16_t loop_rate) { _loop_rate = loop_rate; }
|
2016-08-29 02:37:44 -03:00
|
|
|
|
2017-06-16 08:23:37 -03:00
|
|
|
enum pwm_type { PWM_TYPE_NORMAL=0, PWM_TYPE_ONESHOT=1, PWM_TYPE_ONESHOT125=2, PWM_TYPE_BRUSHED=3 };
|
2016-08-29 02:37:44 -03:00
|
|
|
pwm_type get_pwm_type(void) const { return (pwm_type)_pwm_type.get(); }
|
2016-06-04 22:20:58 -03:00
|
|
|
|
2015-07-02 18:15:09 -03:00
|
|
|
protected:
|
|
|
|
// output functions that should be overloaded by child classes
|
|
|
|
virtual void output_armed_stabilizing()=0;
|
2016-01-04 01:56:54 -04:00
|
|
|
virtual void rc_write(uint8_t chan, uint16_t pwm);
|
2016-01-04 06:24:06 -04:00
|
|
|
virtual void rc_set_freq(uint32_t mask, uint16_t freq_hz);
|
|
|
|
virtual void rc_enable_ch(uint8_t chan);
|
2016-01-04 16:35:24 -04:00
|
|
|
virtual uint32_t rc_map_mask(uint32_t mask) const;
|
2016-04-21 04:32:25 -03:00
|
|
|
|
|
|
|
// add a motor to the motor map
|
|
|
|
void add_motor_num(int8_t motor_num);
|
2016-01-04 01:56:54 -04:00
|
|
|
|
2015-07-02 18:15:09 -03:00
|
|
|
// update the throttle input filter
|
2016-12-14 01:34:15 -04:00
|
|
|
virtual void update_throttle_filter() = 0;
|
2015-04-02 17:54:15 -03:00
|
|
|
|
2016-06-09 02:21:04 -03:00
|
|
|
// save parameters as part of disarming
|
|
|
|
virtual void save_params_on_disarm() {}
|
|
|
|
|
2016-02-02 08:22:53 -04:00
|
|
|
// convert input in -1 to +1 range to pwm output
|
2017-01-03 05:56:57 -04:00
|
|
|
int16_t calc_pwm_output_1to1(float input, const SRV_Channel *servo);
|
2016-02-02 08:22:53 -04:00
|
|
|
|
|
|
|
// convert input in 0 to +1 range to pwm output
|
2017-01-03 05:56:57 -04:00
|
|
|
int16_t calc_pwm_output_0to1(float input, const SRV_Channel *servo);
|
2016-02-02 08:22:53 -04:00
|
|
|
|
2013-09-12 10:27:44 -03:00
|
|
|
// flag bitmask
|
|
|
|
struct AP_Motors_flags {
|
2015-04-02 17:54:15 -03:00
|
|
|
uint8_t armed : 1; // 0 if disarmed, 1 if armed
|
2015-05-01 00:16:32 -03:00
|
|
|
uint8_t interlock : 1; // 1 if the motor interlock is enabled (i.e. motors run), 0 if disabled (motors don't run)
|
2016-12-14 01:46:42 -04:00
|
|
|
uint8_t initialised_ok : 1; // 1 if initialisation was successful
|
2013-09-12 10:27:44 -03:00
|
|
|
} _flags;
|
2013-07-16 00:45:40 -03:00
|
|
|
|
2013-09-12 10:27:44 -03:00
|
|
|
// internal variables
|
2016-03-22 09:34:14 -03:00
|
|
|
uint16_t _loop_rate; // rate in Hz at which output() function is called (normally 400hz)
|
2015-05-14 22:00:46 -03:00
|
|
|
uint16_t _speed_hz; // speed in hz to send updates to motors
|
2016-01-21 22:08:55 -04:00
|
|
|
float _roll_in; // desired roll control from attitude controllers, -1 ~ +1
|
|
|
|
float _pitch_in; // desired pitch control from attitude controller, -1 ~ +1
|
|
|
|
float _yaw_in; // desired yaw control from attitude controller, -1 ~ +1
|
2015-07-13 01:39:57 -03:00
|
|
|
float _throttle_in; // last throttle input from set_throttle caller
|
2016-01-01 18:20:32 -04:00
|
|
|
float _forward_in; // last forward input from set_forward caller
|
|
|
|
float _lateral_in; // last lateral input from set_lateral caller
|
2016-06-08 05:48:54 -03:00
|
|
|
float _throttle_avg_max; // last throttle input from set_throttle_avg_max
|
2015-07-13 01:39:57 -03:00
|
|
|
LowPassFilterFloat _throttle_filter; // throttle input filter
|
2016-02-02 08:14:12 -04:00
|
|
|
spool_up_down_desired _spool_desired; // desired spool state
|
2015-02-23 02:35:10 -04:00
|
|
|
|
2015-07-13 01:39:57 -03:00
|
|
|
// battery voltage, current and air pressure compensation variables
|
2015-02-23 02:35:10 -04:00
|
|
|
float _batt_voltage; // latest battery voltage reading
|
2017-05-23 01:08:36 -03:00
|
|
|
float _batt_voltage_resting_estimate; // estimated battery voltage with sag removed
|
2015-02-23 02:35:10 -04:00
|
|
|
float _batt_current; // latest battery current reading
|
2017-05-23 01:08:36 -03:00
|
|
|
float _batt_resistance; // latest battery resistance estimate in ohms
|
2015-04-28 15:19:50 -03:00
|
|
|
float _air_density_ratio; // air density / sea level density - decreases in altitude
|
2016-01-04 02:48:55 -04:00
|
|
|
|
|
|
|
// mapping to output channels
|
|
|
|
uint8_t _motor_map[AP_MOTORS_MAX_NUM_MOTORS];
|
|
|
|
uint16_t _motor_map_mask;
|
2016-04-22 00:24:04 -03:00
|
|
|
uint16_t _motor_fast_mask;
|
2016-02-03 07:55:55 -04:00
|
|
|
|
|
|
|
// pass through variables
|
|
|
|
float _roll_radio_passthrough = 0.0f; // roll input from pilot in -1 ~ +1 range. used for setup and providing servo feedback while landed
|
|
|
|
float _pitch_radio_passthrough = 0.0f; // pitch input from pilot in -1 ~ +1 range. used for setup and providing servo feedback while landed
|
|
|
|
float _throttle_radio_passthrough = 0.0f; // throttle/collective input from pilot in 0 ~ 1 range. used for setup and providing servo feedback while landed
|
|
|
|
float _yaw_radio_passthrough = 0.0f; // yaw input from pilot in -1 ~ +1 range. used for setup and providing servo feedback while landed
|
2016-04-13 03:33:20 -03:00
|
|
|
|
2016-04-13 18:48:54 -03:00
|
|
|
AP_Int8 _pwm_type; // PWM output type
|
2012-10-26 20:59:07 -03:00
|
|
|
};
|