Skip to content

Commit 43b097b

Browse files
committed
AR_Motors: added MOT_REV_DELAY
adds an optional delay when reversing motors for ESCs that need it
1 parent acf8ac9 commit 43b097b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

libraries/AR_Motors/AP_MotorsUGV.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ const AP_Param::GroupInfo AP_MotorsUGV::var_info[] = {
118118
// @User: Advanced
119119
AP_GROUPINFO("THST_ASYM", 14, AP_MotorsUGV, _thrust_asymmetry, 1.0f),
120120

121+
// @Param: REV_DELAY
122+
// @DisplayName: Motor reversal delay
123+
// @Description: For reversible motors that need a delay before they can reverse. When greater than zero the throttle will go to zero for this amount of time before outputting the new throttle.
124+
// @Units: s
125+
// @User: Standard
126+
AP_GROUPINFO("REV_DELAY", 15, AP_MotorsUGV, _reverse_delay, 0),
127+
121128
AP_GROUPEND
122129
};
123130

@@ -1028,6 +1035,23 @@ void AP_MotorsUGV::output_throttle(SRV_Channel::Function function, float throttl
10281035
}
10291036
#endif // AP_RELAY_ENABLED
10301037

1038+
if (_reverse_delay > 0) {
1039+
switch (function) {
1040+
case SRV_Channel::k_throttle:
1041+
rev_delay_throttle.output(function, throttle, _reverse_delay);
1042+
return;
1043+
case SRV_Channel::k_throttleLeft:
1044+
rev_delay_throttleLeft.output(function, throttle * 10, _reverse_delay);
1045+
return;
1046+
case SRV_Channel::k_throttleRight:
1047+
rev_delay_throttleRight.output(function, throttle * 10, _reverse_delay);
1048+
return;
1049+
default:
1050+
// fall through to other non-delayed outputs
1051+
break;
1052+
}
1053+
}
1054+
10311055
// output to servo channel
10321056
switch (function) {
10331057
case SRV_Channel::k_throttle:
@@ -1172,6 +1196,25 @@ bool AP_MotorsUGV::is_digital_pwm_type() const
11721196
return false;
11731197
}
11741198

1199+
/*
1200+
handle delay on reversal for a throttle
1201+
*/
1202+
void AP_MotorsUGV::ReverseThrottle::output(SRV_Channel::Function function, float throttle, float delay)
1203+
{
1204+
const uint32_t now_ms = AP_HAL::millis();
1205+
if (is_zero(throttle)) {
1206+
// pass through, no change, don't update the last throttle
1207+
} else if (throttle * last_throttle < 0 &&
1208+
now_ms - last_output_ms < delay*1000) {
1209+
// sign change, add pause
1210+
throttle = 0;
1211+
} else {
1212+
last_output_ms = now_ms;
1213+
last_throttle = throttle;
1214+
}
1215+
SRV_Channels::set_output_scaled(function, throttle);
1216+
}
1217+
11751218
namespace AP {
11761219
AP_MotorsUGV *motors_ugv()
11771220
{

libraries/AR_Motors/AP_MotorsUGV.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ class AP_MotorsUGV {
209209
AP_Float _vector_angle_max; // angle between steering's middle position and maximum position when using vectored thrust. zero to disable vectored thrust
210210
AP_Float _speed_scale_base; // speed above which steering is scaled down when using regular steering/throttle vehicles. zero to disable speed scaling
211211
AP_Float _steering_throttle_mix; // Steering vs Throttle priorisation. Higher numbers prioritise steering, lower numbers prioritise throttle. Only valid for Skid Steering vehicles
212+
AP_Float _reverse_delay; // delay in seconds when reversing motor
212213

213214
// internal variables
214215
float _steering; // requested steering as a value from -4500 to +4500
@@ -231,6 +232,17 @@ class AP_MotorsUGV {
231232
float _lateral_factor[AP_MOTORS_NUM_MOTORS_MAX];
232233
uint8_t _motors_num;
233234

235+
/*
236+
3 reversal handling structures, for k_throttle, k_throttleLeft and k_throttleRight
237+
*/
238+
struct ReverseThrottle {
239+
float last_throttle;
240+
uint32_t last_output_ms;
241+
242+
// output with delay for reversal
243+
void output(SRV_Channel::Function function, float throttle, float delay);
244+
} rev_delay_throttle, rev_delay_throttleLeft, rev_delay_throttleRight;
245+
234246
static AP_MotorsUGV *_singleton;
235247
};
236248

0 commit comments

Comments
 (0)