Issues with using the motion magic control method

Our team is trying to use the motion magic control mode in the talon srx library, but after setting up the motors, and tuning the PID, the robot doesn’t move when we use that control method.

I’ve looked at CTRE’s software reference manual plus their example code, and even other team’s code on github (even our c++ code from years prior), and I cant figure out what were not doing correctly.

We know that the motors and motor controllers that we are using work, and we also know that the encoders on the robot work.

Heres a link to the code: https://github.com/jeffrypig23/ChinaRobotPathFollower/blob/master/src/main/java/frc/robot/Motor.java#L21

Any advice as to what were doing wrong is appreciated.

  1. Start here and work your way to the bottom…
    https://phoenix-documentation.readthedocs.io/en/latest/ch16_ClosedLoop.html#sensor-preparation

  2. Don’t just bring pieces of the example code. Open the actual example verbatim, then tweak.
    They work out-of-box for you to use.
    Once you have the mecanism working, then integrate into your app.
    https://github.com/CrossTheRoadElec/Phoenix-Examples-Languages/blob/master/Java/MotionMagic/src/main/java/frc/robot/Robot.java

  3. As an example, I don’t see any calls to configFactoryDefault() in your snippet. But don’t just read this post, then add the call. Start with the test example first, bring-up the mechanism, then integrate.

  4. If a Talon/Victor/CANifier/Pigeon/PCM/PDP is misbehaving, take a self-test while the behavior is occurring. This will tell you everything you need to diagnose it.

Make sense?

3 Likes

What is the purpose of the line:

this.target = this.ticks_per_inch / position;

It looks like your motionmagic code is using ticks as the position target, and your position argument is coming in the form of inches.

Is that correct? If so, what is the desired result of that division? A position of 10 inches for example would result in something like 1000 ticks of motion-ish, which according to your calculations would only be about an inch worth of ticks.

Unless I’m missing something in your code that’s just not resolving in my chimpanzee brain.

What is the purpose of the line:

this.target = this.ticks_per_inch / position;

It looks like your motionmagic code is using ticks as the position target, and your position argument is coming in the form of inches.

Is that correct? If so, what is the desired result of that division? A position of 10 inches for example would result in something like 1000 ticks of motion-ish, which according to your calculations would only be about an inch worth of ticks.

Unless I’m missing something in your code that’s just not resolving in my chimpanzee brain.

Sorry I took so long to reply.

this.target = this.ticks_per_inch / position;

converts the argument from inches into ticks.

I checked the end result, and given the wheel diameter, and how we geared the drive train, it results in about 4000 ticks.

Start here and work your way to the bottom…
https://phoenix-documentation.readthedocs.io/en/latest/ch16_ClosedLoop.html#sensor-preparation

So to be sure that I’m correctly utilizing the encoder/sendor, what does it mean to “Complete the sensor bring up procedure”?

The Bring Up Sensors section of the left navigation bar on their site.

What position argument results in 4000 ticks? It looked like position arguments were in inches, but the very argument name ticks per inch divided by inches makes it seem like that should be multiplication not division.

I looked into it. the position is loaded from a file. Which had the change in distance loaded in meters (oops). More specifically, it was 0.5 meters. So putting that into the equation:

this.ticks_per_inch / position

yeilds

(353.25 * 3.141592653589793) / 0.5

which is 2219.535209761189

So I did the math completely wrong, yet somehow got a decent result.

Anyways…

I thought this would be useful information: using the print statement at the bottom I was able to check what the program was getting for the target, current position, and motor power (in percentage). Here’s what one of the outputs looks like:

Current position: 0
Target position: 2220.0
Power: 0

So from what I can tell, the target position is being set correctly?