Struggling with Motion Magic with TalonSRX

So up until this year, our team has either used Victor SPs or used TalonSRXs for no more than passing them into a Differential Drive arcade drive. I spent the summer and most of the pre-season reading up on the talons and everything they can do and was super excited to jump into using them to their full ability (rip auton :stuck_out_tongue:). I have struggled all of this week, however, to wrap my head around motion magic. Our end goal is to use motion magic to drive forward using the mag encoders we’ve attached to the talons. We’ve double and triple checked all of the encoders and talons to make sure they are working as expected. I have read and re-read the Phoenix docs and even created a plain project with the motion magic example code, but I cannot seem to get it to work. As it is, my code does not move the robot. If I change where I call driveToDistance() to initialize() to only run once, the drive train starts and just keeps moving without stopping. I mainly have a couple of questions:

  1. Should this code (assuming all phases are correct) drive forward 10 rotations and then stop?
  2. If not, is motion magic supposed to stop the drive train on its own? Or am I supposed to be checking getErrorDerivative() == 0 to stop the drive train (as it is right now, the command never ends…I figure I’ll have to use it to end that, but do I have to tell the drive train to stop with that too?).
  3. Is set() for motion magic supposed to be called continuously or only once? The way I’m thinking of it is I tell it what it needs to go to and then it should just go to that…therefore it should only be called once. In the example code, however, it is called continuously.
  4. (And final, sorry for the ultra long post) What are the counts that I pass into set() in respect to? By putting in 4096, am I saying go 4096 counts forward, or go forward or backward until I get to 4096 counts. Or, I’m thinking of this as I’m writing this, does that depend on whether I have the feedback device set relative vs. positive?

I’d really love to understand this more I realize it is well documented, but I am not fully understanding the documentation :stuck_out_tongue:

  1. . No, you never return True from your command isFinished method, so it won’t ever stop.

  2. Yes, assuming you have properly tuned the kp, ki, kd parameters, your profile should get you to the target position.

  3. . You only need to call it once.

  4. . Set is target position in units expected by the Talon, in my team’s case it’s encoder ticks.

Check out our DriveMM command in our github to see a working (ish) example of driving straight for a set distance using MM.

I would also recommend having the Shuffleboard window that shows the Scheduler up just to verify that when the wheels stop turning that your Command is actually finishing. We actually dealt with some false positive results when we were pid tuning because it appeared the commands were finishing when they weren’t.

We are trying MotionMagic for the first time this year as well, it’s been way easier than traditional PID has been for us the last few years so I’m a big fan but it’s not without it’s nuances either.

Awesome thank you! I formatted everything a bit more and I believe I understand a bit better now. I do have one other question though…this should be obvious but I just want to double check. If the parameters are not correctly tuned and it overshoots, will it go backwards to try to correct itself? I’d assume so, but I don’t remember anything specifying it. Thanks again!

If your kP value is too high, you will overshoot your target and oscillate back eventually.

If P is too high, the system will oscillate trying to correct itself.

I would recommend using P and D only. Using I brings in the issue of the time dimension since I is an accumulated error (the integral) over time. When tuning, start with P, raise it and as soon as the system starts to oscillate, start raising D until the system stops oscillating. D (the derivative) limits the rate of change in the error but D by itself can’t get the error to 0. Your goal in tuning is to get the system critically damped. Start at low speeds.

Check out this guide on tuning using Talons: on the resources page of our website: which has a video series on motor control. The Grapher utility plots real time telemetry from the robot, including data read directly from the Talons.

1 Like