Skip to content

Instantly share code, notes, and snippets.

@yabberyabber
Last active May 27, 2017 22:43
Show Gist options
  • Save yabberyabber/c032e9f3014a1af193ca0a489d785103 to your computer and use it in GitHub Desktop.
Save yabberyabber/c032e9f3014a1af193ca0a489d785103 to your computer and use it in GitHub Desktop.

Foldback Current Limit for CANTalon

The Algorithm

The CanTalon library provides one method for limiting current limiting: SetCurrentLimit. It kind of sucks. Meh

We need a more advanced method of Current limiting. Specifically, we need a temporary peak current limit that will fold back to a lower, safe, current limit when the motors are under heavy use.

There shall be 4 configurable parameters:

  • Peak current limit (the highest allowable current under any circumstances)
  • Peak current duration (the longest amount of time the talons is allowed to supply peak current)
  • Foldback current limit (a safe allowable current that the talon could supply for dayz without damaging the motor)
  • Foldback current duration (how long the fold-back in current lasts before we yankthe current back up again)

Here's some extra terms to aid discussion:

  • active current limit (the current limit that the Talon is currently configured at talon.SetCurrentLimit())
  • active current draw (the amount of current the Talon is currently supplying - talon.GetOututCurrent())

Under normal operating curcumstances, the Talon has an active current limit equal to Peak current limit. If the current draw on the motor is creater than Foldback current limit for more than Peak current duration, then set the active current limit is reduced to foldback current limit. After foldback current duration, the active current limit is raised back to peak current limit.

Implementation Overview

Create a class that is a subclass of both CANTalon and CoopTask. Add the following methods:

  • A new constructor that takes in the CAN Id of the Talon as well as an optional instance of TaskMgr
  • ConfigureFoldbackCurrentLimit(double peakCurrent, double peakCurrentDuration, double foldbackCurrent, double foldbackCurrentDuration)
  • TaskPrePeriodic - this overrides the TaskPrePeriodic function
  • SetCurrentLimit - this overrides the CANTalon function

The bulk of this logic is going to go in TaskPrePeriodic. ConfigureFoldbackCurrentLimit and SetCurrentLimit should each be no more than 5 lines.

Here's a state diagram: States: LegacyMode - a single static current limit is set just like in the regular CANTalon object -> PeakCurrentMode (if someone calls ConfigureFoldbackCurrentLimit) PeakCurrentMode - foldback current is enabled and we are allowing peak current -> LegacyMode (if someone calls SetCurrentLimit) -> FoldbackCurrentMode (if we've been drawing more than Foldback current limit for Peak current duration FoldbackCurrentMode - foldback current is enabled and we are allowing only foldback current -> LegacyMode (if someone calls SetCurrentLimit) -> PeakCurrentMode (if we've been in FoldbackCurrentMode for more than Foldback current duration)

CAN packates are not free so we need to be conservative. Only call CANTalon.SetCurrentLimit when the current limit actually changes. Also, only call CANTalon.GetOutputCurrent when we are in PeakCurrentMode.

Implementation Details

(to be filled in)

Testing

This has to be unit tested before we put it on a robot. I'm in the process of making a mock CANTalon class for testing purposes. Testing this extension is blocked on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment