2017-07-06 00:07:12 -03:00
/*
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 <AP_HAL/AP_HAL.h>
2018-05-03 05:54:13 -03:00
# include <AP_Math/AP_Math.h>
2017-07-06 00:07:12 -03:00
# include "SRV_Channel/SRV_Channel.h"
# include "AP_MotorsUGV.h"
# include "Rover.h"
extern const AP_HAL : : HAL & hal ;
// parameters for the motor class
const AP_Param : : GroupInfo AP_MotorsUGV : : var_info [ ] = {
// @Param: PWM_TYPE
2017-09-21 01:48:22 -03:00
// @DisplayName: Motor Output PWM type
2017-07-06 00:23:42 -03:00
// @Description: This selects the output PWM type as regular PWM, OneShot, Brushed motor support using PWM (duty cycle) with separated direction signal, Brushed motor support with separate throttle and direction PWM (duty cyle)
2017-08-21 21:24:34 -03:00
// @Values: 0:Normal,1:OneShot,2:OneShot125,3:BrushedWithRelay,4:BrushedBiPolar
2017-07-06 00:07:12 -03:00
// @User: Advanced
// @RebootRequired: True
AP_GROUPINFO ( " PWM_TYPE " , 1 , AP_MotorsUGV , _pwm_type , PWM_TYPE_NORMAL ) ,
// @Param: PWM_FREQ
2017-09-21 01:48:22 -03:00
// @DisplayName: Motor Output PWM freq for brushed motors
// @Description: Motor Output PWM freq for brushed motors
2017-07-06 00:07:12 -03:00
// @Units: kHz
// @Range: 1 20
// @Increment: 1
// @User: Advanced
// @RebootRequired: True
AP_GROUPINFO ( " PWM_FREQ " , 2 , AP_MotorsUGV , _pwm_freq , 16 ) ,
// @Param: SAFE_DISARM
// @DisplayName: Motor PWM output disabled when disarmed
// @Description: Disables motor PWM output when disarmed
// @Values: 0:PWM enabled while disarmed, 1:PWM disabled while disarmed
// @User: Advanced
AP_GROUPINFO ( " SAFE_DISARM " , 3 , AP_MotorsUGV , _disarm_disable_pwm , 0 ) ,
2017-08-15 22:31:49 -03:00
// @Param: THR_MIN
// @DisplayName: Throttle minimum
2018-03-26 04:25:43 -03:00
// @Description: Throttle minimum percentage the autopilot will apply. This is useful for handling a deadzone around low throttle and for preventing internal combustion motors cutting out during missions.
2017-08-15 22:31:49 -03:00
// @Units: %
// @Range: 0 20
// @Increment: 1
// @User: Standard
AP_GROUPINFO ( " THR_MIN " , 5 , AP_MotorsUGV , _throttle_min , 0 ) ,
// @Param: THR_MAX
// @DisplayName: Throttle maximum
// @Description: Throttle maximum percentage the autopilot will apply. This can be used to prevent overheating an ESC or motor on an electric rover
// @Units: %
// @Range: 30 100
// @Increment: 1
// @User: Standard
AP_GROUPINFO ( " THR_MAX " , 6 , AP_MotorsUGV , _throttle_max , 100 ) ,
2017-11-27 02:18:04 -04:00
// @Param: SLEWRATE
// @DisplayName: Throttle slew rate
// @Description: Throttle slew rate as a percentage of total range per second. A value of 100 allows the motor to change over its full range in one second. A value of zero disables the limit. Note some NiMH powered rovers require a lower setting of 40 to reduce current demand to avoid brownouts.
// @Units: %/s
// @Range: 0 1000
// @Increment: 1
// @User: Standard
AP_GROUPINFO ( " SLEWRATE " , 8 , AP_MotorsUGV , _slew_rate , 100 ) ,
2017-12-01 09:27:30 -04:00
// @Param: THST_EXPO
// @DisplayName: Thrust Curve Expo
// @Description: Thrust curve exponent (-1 to +1 with 0 being linear)
// @Range: -1.0 1.0
// @User: Advanced
AP_GROUPINFO ( " THST_EXPO " , 9 , AP_MotorsUGV , _thrust_curve_expo , 0.0f ) ,
2018-04-09 09:09:49 -03:00
// @Param: VEC_THR_BASE
// @DisplayName: Vector thrust throttle base
// @Description: Throttle level above which steering is scaled down when using vector thrust. zero to disable vectored thrust
// @Units: %
// @Range: 0 100
// @User: Advanced
AP_GROUPINFO ( " VEC_THR_BASE " , 10 , AP_MotorsUGV , _vector_throttle_base , 0.0f ) ,
2017-07-06 00:07:12 -03:00
AP_GROUPEND
} ;
2017-07-06 00:23:42 -03:00
AP_MotorsUGV : : AP_MotorsUGV ( AP_ServoRelayEvents & relayEvents ) :
_relayEvents ( relayEvents )
2017-07-06 00:07:12 -03:00
{
AP_Param : : setup_object_defaults ( this , var_info ) ;
}
void AP_MotorsUGV : : init ( )
{
2017-07-06 07:01:53 -03:00
// setup servo ouput
setup_servo_output ( ) ;
2017-07-06 00:07:12 -03:00
// setup pwm type
setup_pwm_type ( ) ;
// set safety output
setup_safety_output ( ) ;
}
2017-08-15 22:00:40 -03:00
// setup output in case of main CPU failure
void AP_MotorsUGV : : setup_safety_output ( )
{
2017-08-21 21:24:34 -03:00
if ( _pwm_type = = PWM_TYPE_BRUSHED_WITH_RELAY ) {
2017-08-15 22:00:40 -03:00
// set trim to min to set duty cycle range (0 - 100%) to servo range
2017-08-24 08:09:32 -03:00
SRV_Channels : : set_trim_to_min_for ( SRV_Channel : : k_throttle ) ;
2017-08-15 22:00:40 -03:00
SRV_Channels : : set_trim_to_min_for ( SRV_Channel : : k_throttleLeft ) ;
SRV_Channels : : set_trim_to_min_for ( SRV_Channel : : k_throttleRight ) ;
}
if ( _disarm_disable_pwm ) {
// throttle channels output zero pwm (i.e. no signal)
SRV_Channels : : set_safety_limit ( SRV_Channel : : k_throttle , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
SRV_Channels : : set_safety_limit ( SRV_Channel : : k_throttleLeft , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
SRV_Channels : : set_safety_limit ( SRV_Channel : : k_throttleRight , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
} else {
// throttle channels output trim values (because rovers will go backwards if set to MIN)
SRV_Channels : : set_safety_limit ( SRV_Channel : : k_throttle , SRV_Channel : : SRV_CHANNEL_LIMIT_TRIM ) ;
SRV_Channels : : set_safety_limit ( SRV_Channel : : k_throttleLeft , SRV_Channel : : SRV_CHANNEL_LIMIT_TRIM ) ;
SRV_Channels : : set_safety_limit ( SRV_Channel : : k_throttleRight , SRV_Channel : : SRV_CHANNEL_LIMIT_TRIM ) ;
}
// stop sending pwm if main CPU fails
SRV_Channels : : set_failsafe_limit ( SRV_Channel : : k_throttle , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
SRV_Channels : : set_failsafe_limit ( SRV_Channel : : k_throttleLeft , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
SRV_Channels : : set_failsafe_limit ( SRV_Channel : : k_throttleRight , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
}
// setup servo output ranges
void AP_MotorsUGV : : setup_servo_output ( )
{
// k_steering are limited to -45;45 degree
SRV_Channels : : set_angle ( SRV_Channel : : k_steering , SERVO_MAX ) ;
// k_throttle are in power percent so -100 ... 100
SRV_Channels : : set_angle ( SRV_Channel : : k_throttle , 100 ) ;
// skid steering left/right throttle as -1000 to 1000 values
SRV_Channels : : set_angle ( SRV_Channel : : k_throttleLeft , 1000 ) ;
SRV_Channels : : set_angle ( SRV_Channel : : k_throttleRight , 1000 ) ;
2018-05-08 01:23:22 -03:00
// k_motor1, k_motor2 and k_motor3 are in power percent so -100 ... 100
SRV_Channels : : set_angle ( SRV_Channel : : k_motor1 , 100 ) ;
SRV_Channels : : set_angle ( SRV_Channel : : k_motor2 , 100 ) ;
SRV_Channels : : set_angle ( SRV_Channel : : k_motor3 , 100 ) ;
2017-08-15 22:00:40 -03:00
}
2017-08-15 22:31:49 -03:00
// set steering as a value from -4500 to +4500
2018-05-03 05:54:13 -03:00
// apply_scaling should be set to false for manual modes where
// no scaling by speed or angle should be performed
void AP_MotorsUGV : : set_steering ( float steering , bool apply_scaling )
2017-08-15 22:31:49 -03:00
{
_steering = constrain_float ( steering , - 4500.0f , 4500.0f ) ;
2018-05-03 05:54:13 -03:00
_scale_steering = apply_scaling ;
2017-08-15 22:31:49 -03:00
}
// set throttle as a value from -100 to 100
void AP_MotorsUGV : : set_throttle ( float throttle )
{
2018-05-02 05:26:32 -03:00
// only allow setting throttle if armed
if ( ! hal . util - > get_soft_armed ( ) ) {
return ;
}
2017-08-15 22:31:49 -03:00
2018-05-04 22:10:59 -03:00
// check throttle is between -_throttle_max and +_throttle_max
2017-08-15 22:31:49 -03:00
_throttle = constrain_float ( throttle , - _throttle_max , _throttle_max ) ;
}
2018-05-06 05:08:51 -03:00
// get slew limited throttle
// used by manual mode to avoid bad steering behaviour during transitions from forward to reverse
// same as private slew_limit_throttle method (see below) but does not update throttle state
float AP_MotorsUGV : : get_slew_limited_throttle ( float throttle , float dt ) const
{
if ( _slew_rate < = 0 ) {
return throttle ;
}
const float throttle_change_max = MAX ( 1.0f , ( float ) _slew_rate * dt ) ;
return constrain_float ( throttle , _throttle_prev - throttle_change_max , _throttle_prev + throttle_change_max ) ;
}
2017-07-06 00:07:12 -03:00
/*
work out if skid steering is available
*/
bool AP_MotorsUGV : : have_skid_steering ( ) const
{
if ( SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleLeft ) & &
SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleRight ) ) {
return true ;
}
return false ;
}
2018-05-07 04:30:44 -03:00
// returns true if omni rover
bool AP_MotorsUGV : : is_omni_rover ( ) const
{
if ( SRV_Channels : : function_assigned ( SRV_Channel : : k_motor1 ) & &
SRV_Channels : : function_assigned ( SRV_Channel : : k_motor2 ) & &
SRV_Channels : : function_assigned ( SRV_Channel : : k_motor3 ) ) {
return true ;
}
return false ;
}
2018-05-03 05:54:13 -03:00
void AP_MotorsUGV : : output ( bool armed , float ground_speed , float dt )
2017-07-06 00:07:12 -03:00
{
// soft-armed overrides passed in armed status
if ( ! hal . util - > get_soft_armed ( ) ) {
armed = false ;
2018-05-02 05:26:32 -03:00
_throttle = 0.0f ;
2017-07-06 00:07:12 -03:00
}
2018-05-02 05:19:12 -03:00
// sanity check parameters
sanity_check_parameters ( ) ;
2017-08-09 04:20:44 -03:00
// clear and set limits based on input (limit flags may be set again by output_regular or output_skid_steering methods)
set_limits_from_input ( armed , _steering , _throttle ) ;
2017-11-21 20:43:30 -04:00
// slew limit throttle
slew_limit_throttle ( dt ) ;
2017-07-06 00:07:12 -03:00
// output for regular steering/throttle style frames
2018-05-03 05:54:13 -03:00
output_regular ( armed , ground_speed , _steering , _throttle ) ;
2017-07-06 00:07:12 -03:00
2018-05-07 04:30:44 -03:00
// output for omni style frames
2018-05-08 01:23:22 -03:00
output_omni ( armed , _steering , _throttle ) ;
2018-05-07 04:30:44 -03:00
2017-07-06 00:07:12 -03:00
// output for skid steering style frames
2017-08-09 08:31:36 -03:00
output_skid_steering ( armed , _steering , _throttle ) ;
2017-07-06 00:07:12 -03:00
// send values to the PWM timers for output
SRV_Channels : : calc_pwm ( ) ;
2017-11-02 23:35:14 -03:00
SRV_Channels : : cork ( ) ;
2017-07-06 00:07:12 -03:00
SRV_Channels : : output_ch_all ( ) ;
2017-11-02 23:35:14 -03:00
SRV_Channels : : push ( ) ;
2017-07-06 00:07:12 -03:00
}
2017-12-01 09:09:13 -04:00
// test steering or throttle output as a percentage of the total (range -100 to +100)
// used in response to DO_MOTOR_TEST mavlink command
bool AP_MotorsUGV : : output_test_pct ( motor_test_order motor_seq , float pct )
{
// check if the motor_seq is valid
if ( motor_seq > MOTOR_TEST_THROTTLE_RIGHT ) {
return false ;
}
pct = constrain_float ( pct , - 100.0f , 100.0f ) ;
switch ( motor_seq ) {
case MOTOR_TEST_THROTTLE : {
if ( ! SRV_Channels : : function_assigned ( SRV_Channel : : k_throttle ) ) {
return false ;
}
output_throttle ( SRV_Channel : : k_throttle , pct ) ;
break ;
}
case MOTOR_TEST_STEERING : {
if ( ! SRV_Channels : : function_assigned ( SRV_Channel : : k_steering ) ) {
return false ;
}
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_steering , pct * 45.0f ) ;
break ;
}
case MOTOR_TEST_THROTTLE_LEFT : {
if ( ! SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleLeft ) ) {
return false ;
}
output_throttle ( SRV_Channel : : k_throttleLeft , pct ) ;
break ;
}
case MOTOR_TEST_THROTTLE_RIGHT : {
if ( ! SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleRight ) ) {
return false ;
}
output_throttle ( SRV_Channel : : k_throttleRight , pct ) ;
break ;
}
default :
return false ;
}
SRV_Channels : : calc_pwm ( ) ;
SRV_Channels : : cork ( ) ;
SRV_Channels : : output_ch_all ( ) ;
SRV_Channels : : push ( ) ;
return true ;
}
// test steering or throttle output using a pwm value
bool AP_MotorsUGV : : output_test_pwm ( motor_test_order motor_seq , float pwm )
{
// check if the motor_seq is valid
if ( motor_seq > MOTOR_TEST_THROTTLE_RIGHT ) {
return false ;
}
switch ( motor_seq ) {
case MOTOR_TEST_THROTTLE : {
if ( ! SRV_Channels : : function_assigned ( SRV_Channel : : k_throttle ) ) {
return false ;
}
SRV_Channels : : set_output_pwm ( SRV_Channel : : k_throttle , pwm ) ;
break ;
}
case MOTOR_TEST_STEERING : {
if ( ! SRV_Channels : : function_assigned ( SRV_Channel : : k_steering ) ) {
return false ;
}
SRV_Channels : : set_output_pwm ( SRV_Channel : : k_steering , pwm ) ;
break ;
}
case MOTOR_TEST_THROTTLE_LEFT : {
if ( ! SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleLeft ) ) {
return false ;
}
SRV_Channels : : set_output_pwm ( SRV_Channel : : k_throttleLeft , pwm ) ;
break ;
}
case MOTOR_TEST_THROTTLE_RIGHT : {
if ( ! SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleRight ) ) {
return false ;
}
SRV_Channels : : set_output_pwm ( SRV_Channel : : k_throttleRight , pwm ) ;
break ;
}
default :
return false ;
}
SRV_Channels : : calc_pwm ( ) ;
SRV_Channels : : cork ( ) ;
SRV_Channels : : output_ch_all ( ) ;
SRV_Channels : : push ( ) ;
return true ;
}
2017-12-09 02:54:22 -04:00
// returns true if checks pass, false if they fail. report should be true to send text messages to GCS
bool AP_MotorsUGV : : pre_arm_check ( bool report ) const
{
// check if both regular and skid steering functions have been defined
if ( SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleLeft ) & &
SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleRight ) & &
SRV_Channels : : function_assigned ( SRV_Channel : : k_throttle ) & &
SRV_Channels : : function_assigned ( SRV_Channel : : k_steering ) ) {
if ( report ) {
gcs ( ) . send_text ( MAV_SEVERITY_CRITICAL , " PreArm: regular AND skid steering configured " ) ;
}
return false ;
}
// check if only one of skid-steering output has been configured
if ( SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleLeft ) ! = SRV_Channels : : function_assigned ( SRV_Channel : : k_throttleRight ) ) {
if ( report ) {
gcs ( ) . send_text ( MAV_SEVERITY_CRITICAL , " PreArm: check skid steering config " ) ;
}
return false ;
}
// check if only one of throttle or steering outputs has been configured
if ( SRV_Channels : : function_assigned ( SRV_Channel : : k_throttle ) ! = SRV_Channels : : function_assigned ( SRV_Channel : : k_steering ) ) {
if ( report ) {
gcs ( ) . send_text ( MAV_SEVERITY_CRITICAL , " PreArm: check steering and throttle config " ) ;
}
return false ;
}
2018-05-07 04:30:44 -03:00
// check if only one of the omni rover outputs has been configured
if ( ( SRV_Channels : : function_assigned ( SRV_Channel : : k_motor1 ) ) ! = ( SRV_Channels : : function_assigned ( SRV_Channel : : k_motor2 ) ) | |
( SRV_Channels : : function_assigned ( SRV_Channel : : k_motor1 ) ) ! = ( SRV_Channels : : function_assigned ( SRV_Channel : : k_motor3 ) ) | |
( SRV_Channels : : function_assigned ( SRV_Channel : : k_motor2 ) ) ! = ( SRV_Channels : : function_assigned ( SRV_Channel : : k_motor3 ) ) ) {
if ( report ) {
gcs ( ) . send_text ( MAV_SEVERITY_CRITICAL , " PreArm: check motor 1, motor2 and motor3 config " ) ;
}
}
2017-12-09 02:54:22 -04:00
return true ;
}
2018-05-02 05:19:12 -03:00
// sanity check parameters
void AP_MotorsUGV : : sanity_check_parameters ( )
{
_throttle_min = constrain_int16 ( _throttle_min , 0 , 20 ) ;
_throttle_max = constrain_int16 ( _throttle_max , 30 , 100 ) ;
_vector_throttle_base = constrain_float ( _vector_throttle_base , 0.0f , 100.0f ) ;
}
2017-12-01 09:09:13 -04:00
// setup pwm output type
void AP_MotorsUGV : : setup_pwm_type ( )
{
switch ( _pwm_type ) {
case PWM_TYPE_ONESHOT :
2018-03-14 03:04:37 -03:00
hal . rcout - > set_output_mode ( 0xFFFF , AP_HAL : : RCOutput : : MODE_PWM_ONESHOT ) ;
2017-12-01 09:09:13 -04:00
break ;
2018-04-03 05:10:28 -03:00
case PWM_TYPE_ONESHOT125 :
hal . rcout - > set_output_mode ( 0xFFFF , AP_HAL : : RCOutput : : MODE_PWM_ONESHOT125 ) ;
break ;
2017-12-01 09:09:13 -04:00
case PWM_TYPE_BRUSHED_WITH_RELAY :
case PWM_TYPE_BRUSHED_BIPOLAR :
2018-03-14 03:04:37 -03:00
hal . rcout - > set_output_mode ( 0xFFFF , AP_HAL : : RCOutput : : MODE_PWM_BRUSHED ) ;
2017-12-01 09:09:13 -04:00
/*
* Group 0 : channels 0 1
* Group 1 : channels 4 5 6 7
* Group 2 : channels 2 3
*/
// TODO : See if we can seperate frequency between groups
hal . rcout - > set_freq ( ( 1UL < < 0 ) , static_cast < uint16_t > ( _pwm_freq * 1000 ) ) ; // Steering group
hal . rcout - > set_freq ( ( 1UL < < 2 ) , static_cast < uint16_t > ( _pwm_freq * 1000 ) ) ; // Throttle group
break ;
default :
// do nothing
break ;
}
}
2017-07-06 00:07:12 -03:00
// output to regular steering and throttle channels
2018-05-03 05:54:13 -03:00
void AP_MotorsUGV : : output_regular ( bool armed , float ground_speed , float steering , float throttle )
2017-07-06 00:07:12 -03:00
{
// output to throttle channels
if ( armed ) {
2018-05-03 05:54:13 -03:00
if ( _scale_steering ) {
// vectored thrust handling
if ( have_vectored_thrust ( ) ) {
if ( fabsf ( throttle ) > _vector_throttle_base ) {
// scale steering down linearly as throttle increases above _vector_throttle_base
steering * = constrain_float ( _vector_throttle_base / fabsf ( throttle ) , 0.0f , 1.0f ) ;
}
} else {
// scale steering down as speed increase above 1m/s
if ( fabsf ( ground_speed ) > 1.0f ) {
steering * = ( 1.0f / fabsf ( ground_speed ) ) ;
} else {
// regular steering rover at low speed so set limits to stop I-term build-up in controllers
if ( ! have_skid_steering ( ) ) {
limit . steer_left = true ;
limit . steer_right = true ;
}
}
// reverse steering output if backing up
if ( is_negative ( ground_speed ) ) {
steering * = - 1.0f ;
}
}
} else {
// reverse steering direction when backing up
if ( is_negative ( throttle ) ) {
steering * = - 1.0f ;
}
2018-04-09 09:09:49 -03:00
}
2017-08-24 08:09:32 -03:00
output_throttle ( SRV_Channel : : k_throttle , throttle ) ;
2017-07-06 00:07:12 -03:00
} else {
// handle disarmed case
if ( _disarm_disable_pwm ) {
SRV_Channels : : set_output_limit ( SRV_Channel : : k_throttle , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
} else {
SRV_Channels : : set_output_limit ( SRV_Channel : : k_throttle , SRV_Channel : : SRV_CHANNEL_LIMIT_TRIM ) ;
}
}
2018-04-09 09:09:49 -03:00
// always allow steering to move
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_steering , steering ) ;
2017-07-06 00:07:12 -03:00
}
2018-05-07 04:30:44 -03:00
// output for omni style frames
void AP_MotorsUGV : : output_omni ( bool armed , float steering , float throttle )
{
2018-05-08 01:23:22 -03:00
if ( ! is_omni_rover ( ) ) {
2018-05-07 04:30:44 -03:00
return ;
}
if ( armed ) {
2018-05-08 23:30:37 -03:00
// scale throttle and steering to a 1000 to 2000 range for motor throttle calculations
2018-05-08 01:23:22 -03:00
const float scaled_throttle = ( throttle - ( 100 ) ) * ( 2000 - 1000 ) / ( - 100 - ( 100 ) ) + 1000 ;
const float scaled_steering = ( steering - ( - 4500 ) ) * ( 2000 - 1000 ) / ( 4500 - ( - 4500 ) ) + 1000 ;
2018-05-08 23:30:37 -03:00
// calculate desired vehicle speed and direction
// 1500 is a place-holder value for lateral movement input
2018-05-08 01:23:22 -03:00
const float magnitude = safe_sqrt ( ( scaled_throttle * scaled_throttle ) + ( 1500 * 1500 ) ) ;
const float theta = atan2f ( scaled_throttle , 1500 ) ;
2018-05-08 23:30:37 -03:00
2018-05-03 05:54:13 -03:00
// calculate X and Y vectors using the following the equations: vx = cos(?) * magnitude and vy = sin(?) * magnitude
2018-05-08 01:23:22 -03:00
const float Vx = - ( cosf ( theta ) * magnitude ) ;
const float Vy = - ( sinf ( theta ) * magnitude ) ;
2018-05-08 23:30:37 -03:00
// calculate output throttle for each motor and scale it back to a -100 to 100 range
2018-05-03 05:54:13 -03:00
// calculations are done using the following equations: MOTOR1 = <20> vx, MOTOR2 = 0.5 * v <20> v(3/2) * vy, MOTOR 3 = 0.5 * vx + v(3/2) * vy
2018-05-08 23:30:37 -03:00
// safe_sqrt((3)/2) used because the motors are 120 degrees apart in the frame, this setup is mandatory
2018-05-08 01:23:22 -03:00
const int16_t motor_1 = ( ( ( - Vx ) + scaled_steering ) - ( 2500 ) ) * ( 100 - ( - 100 ) ) / ( 3500 - ( 2500 ) ) + ( - 100 ) ;
const int16_t motor_2 = ( ( ( ( 0.5 * Vx ) - ( ( safe_sqrt ( 3 ) / 2 ) * Vy ) ) + scaled_steering ) - ( 1121 ) ) * ( 100 - ( - 100 ) ) / ( 2973 - ( 1121 ) ) + ( - 100 ) ;
const int16_t motor_3 = ( ( ( ( 0.5 * Vx ) + ( ( safe_sqrt ( 3 ) / 2 ) * Vy ) ) + scaled_steering ) - ( - 1468 ) ) * ( 100 - ( - 100 ) ) / ( 383 - ( - 1468 ) ) + ( - 100 ) ;
2018-05-08 23:30:37 -03:00
// send pwm value to each motor
2018-05-08 01:23:22 -03:00
output_throttle ( SRV_Channel : : k_motor1 , motor_1 ) ;
output_throttle ( SRV_Channel : : k_motor2 , motor_2 ) ;
output_throttle ( SRV_Channel : : k_motor3 , motor_3 ) ;
2018-05-07 04:30:44 -03:00
} else {
// handle disarmed case
if ( _disarm_disable_pwm ) {
SRV_Channels : : set_output_limit ( SRV_Channel : : k_motor1 , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
SRV_Channels : : set_output_limit ( SRV_Channel : : k_motor2 , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
SRV_Channels : : set_output_limit ( SRV_Channel : : k_motor3 , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
} else {
SRV_Channels : : set_output_limit ( SRV_Channel : : k_motor1 , SRV_Channel : : SRV_CHANNEL_LIMIT_TRIM ) ;
SRV_Channels : : set_output_limit ( SRV_Channel : : k_motor2 , SRV_Channel : : SRV_CHANNEL_LIMIT_TRIM ) ;
SRV_Channels : : set_output_limit ( SRV_Channel : : k_motor3 , SRV_Channel : : SRV_CHANNEL_LIMIT_TRIM ) ;
}
}
}
2017-07-06 00:07:12 -03:00
// output to skid steering channels
void AP_MotorsUGV : : output_skid_steering ( bool armed , float steering , float throttle )
{
2017-08-09 04:20:44 -03:00
if ( ! have_skid_steering ( ) ) {
return ;
}
2017-07-06 00:07:12 -03:00
// handle simpler disarmed case
if ( ! armed ) {
if ( _disarm_disable_pwm ) {
SRV_Channels : : set_output_limit ( SRV_Channel : : k_throttleLeft , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
SRV_Channels : : set_output_limit ( SRV_Channel : : k_throttleRight , SRV_Channel : : SRV_CHANNEL_LIMIT_ZERO_PWM ) ;
} else {
SRV_Channels : : set_output_limit ( SRV_Channel : : k_throttleLeft , SRV_Channel : : SRV_CHANNEL_LIMIT_TRIM ) ;
SRV_Channels : : set_output_limit ( SRV_Channel : : k_throttleRight , SRV_Channel : : SRV_CHANNEL_LIMIT_TRIM ) ;
}
return ;
}
// skid steering mixer
float steering_scaled = steering / 4500.0f ; // steering scaled -1 to +1
float throttle_scaled = throttle / 100.0f ; // throttle scaled -1 to +1
// apply constraints
steering_scaled = constrain_float ( steering_scaled , - 1.0f , 1.0f ) ;
throttle_scaled = constrain_float ( throttle_scaled , - 1.0f , 1.0f ) ;
// check for saturation and scale back throttle and steering proportionally
const float saturation_value = fabsf ( steering_scaled ) + fabsf ( throttle_scaled ) ;
if ( saturation_value > 1.0f ) {
steering_scaled = steering_scaled / saturation_value ;
throttle_scaled = throttle_scaled / saturation_value ;
}
2017-11-27 05:16:31 -04:00
// add in throttle and steering
2018-05-03 05:54:13 -03:00
const float motor_left = throttle_scaled + steering_scaled ;
const float motor_right = throttle_scaled - steering_scaled ;
2017-07-12 05:31:56 -03:00
// send pwm value to each motor
output_throttle ( SRV_Channel : : k_throttleLeft , 100.0f * motor_left ) ;
output_throttle ( SRV_Channel : : k_throttleRight , 100.0f * motor_right ) ;
}
// output throttle value to main throttle channel, left throttle or right throttle. throttle should be scaled from -100 to 100
void AP_MotorsUGV : : output_throttle ( SRV_Channel : : Aux_servo_function_t function , float throttle )
{
// sanity check servo function
2018-05-08 01:23:22 -03:00
if ( function ! = SRV_Channel : : k_throttle & & function ! = SRV_Channel : : k_throttleLeft & & function ! = SRV_Channel : : k_throttleRight & & function ! = SRV_Channel : : k_motor1 & & function ! = SRV_Channel : : k_motor2 & & function ! = SRV_Channel : : k_motor3 ) {
2017-07-12 05:31:56 -03:00
return ;
}
2017-12-01 09:27:30 -04:00
// constrain and scale output
throttle = get_scaled_throttle ( throttle ) ;
2017-07-12 05:31:56 -03:00
// set relay if necessary
2017-08-21 21:24:34 -03:00
if ( _pwm_type = = PWM_TYPE_BRUSHED_WITH_RELAY ) {
2017-08-07 08:12:44 -03:00
// find the output channel, if not found return
const SRV_Channel * out_chan = SRV_Channels : : get_channel_for ( function ) ;
if ( out_chan = = nullptr ) {
return ;
}
2017-08-07 08:12:44 -03:00
const int8_t reverse_multiplier = out_chan - > get_reversed ( ) ? - 1 : 1 ;
bool relay_high = is_negative ( reverse_multiplier * throttle ) ;
2017-07-12 05:31:56 -03:00
switch ( function ) {
case SRV_Channel : : k_throttle :
case SRV_Channel : : k_throttleLeft :
2018-05-08 01:23:22 -03:00
case SRV_Channel : : k_motor1 :
2017-08-07 08:12:44 -03:00
_relayEvents . do_set_relay ( 0 , relay_high ) ;
2017-07-12 05:31:56 -03:00
break ;
case SRV_Channel : : k_throttleRight :
2018-05-08 01:23:22 -03:00
case SRV_Channel : : k_motor2 :
2017-08-07 08:12:44 -03:00
_relayEvents . do_set_relay ( 1 , relay_high ) ;
2017-07-12 05:31:56 -03:00
break ;
2018-05-08 01:23:22 -03:00
case SRV_Channel : : k_motor3 :
_relayEvents . do_set_relay ( 2 , relay_high ) ;
break ;
2017-07-12 05:31:56 -03:00
default :
// do nothing
break ;
}
2017-08-07 08:12:44 -03:00
// invert the output to always have positive value calculated by calc_pwm
throttle = reverse_multiplier * fabsf ( throttle ) ;
2017-07-12 05:31:56 -03:00
}
// output to servo channel
switch ( function ) {
case SRV_Channel : : k_throttle :
2018-05-08 01:23:22 -03:00
case SRV_Channel : : k_motor1 :
case SRV_Channel : : k_motor2 :
case SRV_Channel : : k_motor3 :
2017-07-12 05:31:56 -03:00
SRV_Channels : : set_output_scaled ( function , throttle ) ;
break ;
case SRV_Channel : : k_throttleLeft :
case SRV_Channel : : k_throttleRight :
2017-08-24 08:09:32 -03:00
SRV_Channels : : set_output_scaled ( function , throttle * 10.0f ) ;
2017-07-12 05:31:56 -03:00
break ;
default :
// do nothing
break ;
2017-07-06 00:23:42 -03:00
}
2017-07-06 00:07:12 -03:00
}
// slew limit throttle for one iteration
void AP_MotorsUGV : : slew_limit_throttle ( float dt )
{
2018-05-06 05:08:51 -03:00
const float throttle_orig = _throttle ;
_throttle = get_slew_limited_throttle ( _throttle , dt ) ;
if ( throttle_orig > _throttle ) {
limit . throttle_upper = true ;
} else if ( throttle_orig < _throttle ) {
limit . throttle_lower = true ;
2017-07-06 00:07:12 -03:00
}
2017-11-21 20:43:30 -04:00
_throttle_prev = _throttle ;
2017-07-06 00:07:12 -03:00
}
2017-08-09 04:20:44 -03:00
// set limits based on steering and throttle input
void AP_MotorsUGV : : set_limits_from_input ( bool armed , float steering , float throttle )
{
// set limits based on inputs
limit . steer_left = ! armed | | ( steering < = - 4500.0f ) ;
limit . steer_right = ! armed | | ( steering > = 4500.0f ) ;
2017-08-15 22:31:49 -03:00
limit . throttle_lower = ! armed | | ( throttle < = - _throttle_max ) ;
limit . throttle_upper = ! armed | | ( throttle > = _throttle_max ) ;
2017-08-09 04:20:44 -03:00
}
2017-12-01 09:27:30 -04:00
2018-03-26 04:25:43 -03:00
// scale a throttle using the _throttle_min and _thrust_curve_expo parameters. throttle should be in the range -100 to +100
2017-12-01 09:27:30 -04:00
float AP_MotorsUGV : : get_scaled_throttle ( float throttle ) const
{
2018-03-26 04:25:43 -03:00
// exit immediately if throttle is zero
if ( is_zero ( throttle ) ) {
return throttle ;
}
// scale using throttle_min
if ( _throttle_min > 0 ) {
if ( is_negative ( throttle ) ) {
throttle = - _throttle_min + ( throttle * ( ( 100.0f - _throttle_min ) / 100.0f ) ) ;
} else {
throttle = _throttle_min + ( throttle * ( ( 100.0f - _throttle_min ) / 100.0f ) ) ;
}
}
// skip further scaling if thrust curve disabled or invalid
2017-12-01 09:27:30 -04:00
if ( is_zero ( _thrust_curve_expo ) | | ( _thrust_curve_expo > 1.0f ) | | ( _thrust_curve_expo < - 1.0f ) ) {
return throttle ;
}
// calculate scaler
const float sign = ( throttle < 0.0f ) ? - 1.0f : 1.0f ;
const float throttle_pct = constrain_float ( throttle , - 100.0f , 100.0f ) / 100.0f ;
return 100.0f * sign * ( ( _thrust_curve_expo - 1.0f ) + safe_sqrt ( ( 1.0f - _thrust_curve_expo ) * ( 1.0f - _thrust_curve_expo ) + 4.0f * _thrust_curve_expo * fabsf ( throttle_pct ) ) ) / ( 2.0f * _thrust_curve_expo ) ;
}