LCOV - code coverage report
Current view: top level - AP_WheelEncoder - AP_WheelRateControl.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 11 34 32.4 %
Date: 2026-09-09 11:02:08 Functions: 3 5 60.0 %

          Line data    Source code
       1             : #include <AP_WheelEncoder/AP_WheelRateControl.h>
       2             : 
       3             : extern const AP_HAL::HAL& hal;
       4             : 
       5             : const AP_Param::GroupInfo AP_WheelRateControl::var_info[] = {
       6             :     // @Param: _ENABLE
       7             :     // @DisplayName: Wheel rate control enable/disable
       8             :     // @Description: Enable or disable wheel rate control
       9             :     // @Values: 0:Disabled,1:Enabled
      10             :     // @User: Standard
      11             :     AP_GROUPINFO_FLAGS("_ENABLE", 1, AP_WheelRateControl, _enabled, 0, AP_PARAM_FLAG_ENABLE),
      12             : 
      13             :     // @Param: _RATE_MAX
      14             :     // @DisplayName: Wheel max rotation rate
      15             :     // @Description: Wheel max rotation rate
      16             :     // @Units: rad/s
      17             :     // @Range: 0 200
      18             :     // @User: Standard
      19             :     AP_GROUPINFO("_RATE_MAX", 2, AP_WheelRateControl, _rate_max, AP_WHEEL_RATE_MAX_DEFAULT),
      20             : 
      21             :     // @Param: _RATE_FF
      22             :     // @DisplayName: Wheel rate control feed forward gain
      23             :     // @Description: Wheel rate control feed forward gain.  Desired rate (in radians/sec) is multiplied by this constant and output to output (in the range -1 to +1)
      24             :     // @Range: 0.100 2.000
      25             :     // @User: Standard
      26             : 
      27             :     // @Param: _RATE_P
      28             :     // @DisplayName: Wheel rate control P gain
      29             :     // @Description: Wheel rate control P gain.  Converts rate error (in radians/sec) to output (in the range -1 to +1)
      30             :     // @Range: 0.100 2.000
      31             :     // @User: Standard
      32             : 
      33             :     // @Param: _RATE_I
      34             :     // @DisplayName: Wheel rate control I gain
      35             :     // @Description: Wheel rate control I gain.  Corrects long term error between the desired rate (in rad/s) and actual
      36             :     // @Range: 0.000 2.000
      37             :     // @User: Standard
      38             : 
      39             :     // @Param: _RATE_IMAX
      40             :     // @DisplayName: Wheel rate control I gain maximum
      41             :     // @Description: Wheel rate control I gain maximum.  Constrains the output (range -1 to +1) that the I term will generate
      42             :     // @Range: 0.000 1.000
      43             :     // @User: Standard
      44             : 
      45             :     // @Param: _RATE_D
      46             :     // @DisplayName: Wheel rate control D gain
      47             :     // @Description: Wheel rate control D gain.  Compensates for short-term change in desired rate vs actual
      48             :     // @Range: 0.000 0.400
      49             :     // @User: Standard
      50             : 
      51             :     // @Param: _RATE_FILT
      52             :     // @DisplayName: Wheel rate control filter frequency
      53             :     // @Description: Wheel rate control input filter.  Lower values reduce noise but add delay.
      54             :     // @Range: 1.000 100.000
      55             :     // @Units: Hz
      56             :     // @User: Standard
      57             : 
      58             :     // @Param: _RATE_FLTT
      59             :     // @DisplayName: Wheel rate control target frequency in Hz
      60             :     // @Description: Wheel rate control target frequency in Hz
      61             :     // @Range: 1 50
      62             :     // @Increment: 1
      63             :     // @Units: Hz
      64             :     // @User: Standard
      65             : 
      66             :     // @Param: _RATE_FLTE
      67             :     // @DisplayName: Wheel rate control error frequency in Hz
      68             :     // @Description: Wheel rate control error frequency in Hz
      69             :     // @Range: 1 50
      70             :     // @Increment: 1
      71             :     // @Units: Hz
      72             :     // @User: Standard
      73             : 
      74             :     // @Param: _RATE_FLTD
      75             :     // @DisplayName: Wheel rate control derivative frequency in Hz
      76             :     // @Description: Wheel rate control derivative frequency in Hz
      77             :     // @Range: 1 50
      78             :     // @Increment: 1
      79             :     // @Units: Hz
      80             :     // @User: Standard
      81             : 
      82             :     // @Param: _RATE_SMAX
      83             :     // @DisplayName: Wheel rate slew rate limit
      84             :     // @Description: Sets an upper limit on the slew rate produced by the combined P and D gains. If the amplitude of the control action produced by the rate feedback exceeds this value, then the D+P gain is reduced to respect the limit. This limits the amplitude of high frequency oscillations caused by an excessive gain. The limit should be set to no more than 25% of the actuators maximum slew rate to allow for load effects. Note: The gain will not be reduced to less than 10% of the nominal value. A value of zero will disable this feature.
      85             :     // @Range: 0 200
      86             :     // @Increment: 0.5
      87             :     // @User: Advanced
      88             : 
      89             :     // @Param: _RATE_PDMX
      90             :     // @DisplayName: Wheel rate control PD sum maximum
      91             :     // @Description: Wheel rate control PD sum maximum.  The maximum/minimum value that the sum of the P and D term can output
      92             :     // @Range: 0.000 1.000
      93             : 
      94             :     // @Param: _RATE_D_FF
      95             :     // @DisplayName: Wheel rate Derivative FeedForward Gain
      96             :     // @Description: FF D Gain which produces an output that is proportional to the rate of change of the error
      97             :     // @Range: 0.000 0.400
      98             :     // @Increment: 0.001
      99             :     // @User: Advanced
     100             : 
     101             :     // @Param: _RATE_NTF
     102             :     // @DisplayName: Wheel rate Target notch filter index
     103             :     // @Description: Wheel rate Target notch filter index, zero disables
     104             :     // @Range: 0 8
     105             :     // @User: Advanced
     106             : 
     107             :     // @Param: _RATE_NEF
     108             :     // @DisplayName: Wheel rate Error notch filter index
     109             :     // @Description: Wheel rate Error notch filter index, zero disables
     110             :     // @Range: 0 8
     111             :     // @User: Advanced
     112             : 
     113             :     AP_SUBGROUPINFO(_rate_pid0, "_RATE_", 3, AP_WheelRateControl, AC_PID),
     114             : 
     115             :     // @Param: 2_RATE_FF
     116             :     // @DisplayName: Wheel rate control feed forward gain
     117             :     // @Description: Wheel rate control feed forward gain.  Desired rate (in radians/sec) is multiplied by this constant and output to output (in the range -1 to +1)
     118             :     // @Range: 0.100 2.000
     119             :     // @User: Standard
     120             : 
     121             :     // @Param: 2_RATE_P
     122             :     // @DisplayName: Wheel rate control P gain
     123             :     // @Description: Wheel rate control P gain.  Converts rate error (in radians/sec) to output (in the range -1 to +1)
     124             :     // @Range: 0.100 2.000
     125             :     // @User: Standard
     126             : 
     127             :     // @Param: 2_RATE_I
     128             :     // @DisplayName: Wheel rate control I gain
     129             :     // @Description: Wheel rate control I gain.  Corrects long term error between the desired rate (in rad/s) and actual
     130             :     // @Range: 0.000 2.000
     131             :     // @User: Standard
     132             : 
     133             :     // @Param: 2_RATE_IMAX
     134             :     // @DisplayName: Wheel rate control I gain maximum
     135             :     // @Description: Wheel rate control I gain maximum.  Constrains the output (range -1 to +1) that the I term will generate
     136             :     // @Range: 0.000 1.000
     137             :     // @User: Standard
     138             : 
     139             :     // @Param: 2_RATE_D
     140             :     // @DisplayName: Wheel rate control D gain
     141             :     // @Description: Wheel rate control D gain.  Compensates for short-term change in desired rate vs actual
     142             :     // @Range: 0.000 0.400
     143             :     // @User: Standard
     144             : 
     145             :     // @Param: 2_RATE_FILT
     146             :     // @DisplayName: Wheel rate control filter frequency
     147             :     // @Description: Wheel rate control input filter.  Lower values reduce noise but add delay.
     148             :     // @Range: 1.000 100.000
     149             :     // @Units: Hz
     150             :     // @User: Standard
     151             : 
     152             :     // @Param: 2_RATE_FLTT
     153             :     // @DisplayName: Wheel rate control target frequency in Hz
     154             :     // @Description: Wheel rate control target frequency in Hz
     155             :     // @Range: 1 50
     156             :     // @Increment: 1
     157             :     // @Units: Hz
     158             :     // @User: Standard
     159             : 
     160             :     // @Param: 2_RATE_FLTE
     161             :     // @DisplayName: Wheel rate control error frequency in Hz
     162             :     // @Description: Wheel rate control error frequency in Hz
     163             :     // @Range: 1 50
     164             :     // @Increment: 1
     165             :     // @Units: Hz
     166             :     // @User: Standard
     167             : 
     168             :     // @Param: 2_RATE_FLTD
     169             :     // @DisplayName: Wheel rate control derivative frequency in Hz
     170             :     // @Description: Wheel rate control derivative frequency in Hz
     171             :     // @Range: 1 50
     172             :     // @Increment: 1
     173             :     // @Units: Hz
     174             :     // @User: Standard
     175             : 
     176             :     // @Param: 2_RATE_SMAX
     177             :     // @DisplayName: Wheel rate slew rate limit
     178             :     // @Description: Sets an upper limit on the slew rate produced by the combined P and D gains. If the amplitude of the control action produced by the rate feedback exceeds this value, then the D+P gain is reduced to respect the limit. This limits the amplitude of high frequency oscillations caused by an excessive gain. The limit should be set to no more than 25% of the actuators maximum slew rate to allow for load effects. Note: The gain will not be reduced to less than 10% of the nominal value. A value of zero will disable this feature.
     179             :     // @Range: 0 200
     180             :     // @Increment: 0.5
     181             :     // @User: Advanced
     182             : 
     183             :     // @Param: 2_RATE_PDMX
     184             :     // @DisplayName: Wheel rate control PD sum maximum
     185             :     // @Description: Wheel rate control PD sum maximum.  The maximum/minimum value that the sum of the P and D term can output
     186             :     // @Range: 0.000 1.000
     187             : 
     188             :     // @Param: 2_RATE_D_FF
     189             :     // @DisplayName: Wheel rate Derivative FeedForward Gain
     190             :     // @Description: FF D Gain which produces an output that is proportional to the rate of change of the target
     191             :     // @Range: 0.000 0.400
     192             :     // @Increment: 0.001
     193             :     // @User: Advanced
     194             : 
     195             :     // @Param: 2_RATE_NTF
     196             :     // @DisplayName: Wheel rate Target notch filter index
     197             :     // @Description: Wheel rate Target notch filter index, zero disables
     198             :     // @Range: 0 8
     199             :     // @User: Advanced
     200             : 
     201             :     // @Param: 2_RATE_NEF
     202             :     // @DisplayName: Wheel rate Error notch filter index
     203             :     // @Description: Wheel rate Error notch filter index, zero disables
     204             :     // @Range: 0 8
     205             :     // @User: Advanced
     206             : 
     207             :     AP_SUBGROUPINFO(_rate_pid1, "2_RATE_", 4, AP_WheelRateControl, AC_PID),
     208             : 
     209             :     AP_GROUPEND
     210             : };
     211             : 
     212         285 : AP_WheelRateControl::AP_WheelRateControl(const AP_WheelEncoder &wheel_encoder_ref) :
     213         285 :         _wheel_encoder(wheel_encoder_ref)
     214             : {
     215         285 :     AP_Param::setup_object_defaults(this, var_info);
     216         285 : }
     217             : 
     218             : // returns true if a wheel encoder and rate control PID are available for this instance
     219      694202 : bool AP_WheelRateControl::enabled(uint8_t instance)
     220             : {
     221             :     // sanity check instance
     222      694202 :     if ((instance > 1) || (_enabled == 0)) {
     223      694202 :         return false;
     224             :     }
     225             :     // wheel encoder enabled
     226           0 :     return _wheel_encoder.enabled(instance);
     227             : }
     228             : 
     229             : // get throttle output in the range -100 to +100 given a desired rate expressed as a percentage of the rate_max (-100 to +100)
     230             : // instance can be 0 or 1
     231           0 : float AP_WheelRateControl::get_rate_controlled_throttle(uint8_t instance, float desired_rate_pct, float dt)
     232             : {
     233           0 :     if (!enabled(instance)) {
     234           0 :         return 0;
     235             :     }
     236             : 
     237             :     // determine which PID instance to use
     238           0 :     AC_PID& rate_pid = (instance == 0) ? _rate_pid0 : _rate_pid1;
     239             : 
     240             :     // check for timeout
     241           0 :     uint32_t now = AP_HAL::millis();
     242           0 :     if (now - _last_update_ms > AP_WHEEL_RATE_CONTROL_TIMEOUT_MS) {
     243           0 :         rate_pid.reset_filter();
     244           0 :         rate_pid.reset_I();
     245           0 :         _limit[instance].lower = false;
     246           0 :         _limit[instance].upper = false;
     247             :     }
     248           0 :     _last_update_ms = now;
     249             : 
     250             :     // convert desired rate as a percentage to radians/sec
     251           0 :     float desired_rate = desired_rate_pct * 0.01f * get_rate_max_rads();
     252             : 
     253             :     // get actual rate from wheel encoder
     254           0 :     float actual_rate = _wheel_encoder.get_rate(instance);
     255             : 
     256             :     // constrain and set limit flags
     257           0 :     float output = rate_pid.update_all(desired_rate, actual_rate, dt, (_limit[instance].lower || _limit[instance].upper));
     258           0 :     output += rate_pid.get_ff();
     259             : 
     260             :     // set limits for next iteration
     261           0 :     _limit[instance].upper = output >= 100.0f;
     262           0 :     _limit[instance].lower = output <= -100.0f;
     263             : 
     264           0 :     return output;
     265             : }
     266             : 
     267             : // get pid objects for reporting
     268           0 : AC_PID& AP_WheelRateControl::get_pid(uint8_t instance)
     269             : {
     270           0 :     if (instance == 0) {
     271           0 :         return _rate_pid0;
     272             :     } else {
     273           0 :         return _rate_pid1;
     274             :     }
     275             : }
     276             : 
     277       12526 : void AP_WheelRateControl::set_notch_sample_rate(float sample_rate)
     278             : {
     279             : #if AP_FILTER_ENABLED
     280       12526 :     _rate_pid0.set_notch_sample_rate(sample_rate);
     281       12526 :     _rate_pid1.set_notch_sample_rate(sample_rate);
     282             : #endif
     283       12526 : }

Generated by: LCOV version 1.14