Confusion with Falcon 500 encoder

We are trying to code a Falcon 500 to rotate a certain amount of times and then stop automatically. The code we have now currently works sometimes but doesn’t stop spinning.

Here is our current code for this:


We aren’t sure why this isn’t working. Any help would be appreciated.

Without seeing the rest of your code or knowing specifically what isn’t working, I can’t say for certain.

Having said that, there are VERY few valid reasons to use loops in FRC code (don’t at me, I know they exist), especially while loops. They often lead to loop overrun errors due to taking longer than 0.02ms to complete, which is the cycle time (50Hz) for the main TimedRobot loop in which all of your code runs.

4 Likes

I think you could print out the getSelectedSensorPosition before and in your loops and get some real insight. Your while loops coupled with your “else ifs” seem dubious to me.

Firstly, follow @Fletch1373’s advice regarding loops. Something of note is that the Falcon encoder is 2048 counts per revolution which means that if one rotation of the motor happens, the encoder will show 2048 as the position.

3 Likes

This is the full code.message.txt (9.1 KB)

@RoboticsGuy1 I think there is a fundamental misunderstanding about how the FRC development framework functions.

What you are currently using is a raw TimedRobot class. In this class the ~Init() methods will run once at the beginning of the different parts of a match. The ~Periodic() methods will execute “periodically” I think about every 20ms and any logic you do in those methods should try to limit the amount time you spend in your own code. In your case you could think of the teleopPeriodic() method as your loop.

This means you should be able to replace your while statements with if statements and you might be able to do something like what you are looking for.

With this said, I would make several suggestions.

  1. All CTRE motor controllers (including Falcons) include AWESOME logic to do much of what you are trying to do automatically. What I would investigate is using something called Motion Magic. It takes a bi to setup, but once you have it setup you just tell the motor what position you want to move to and it will do it all for you. For Information you want to check out CTRE’s documentation Motor Controller Closed Loop — Phoenix documentation . I know it probably seems like a lot, but it really isn’t too bad once you get the hang of it.
  2. I would strongly recommend against using a raw TimedRobot implementation. That type of implementation is meant for scenarios where you are managing everything. My recommendation would be to switch to a CommandBased approach. What Is “Command-Based” Programming? — FIRST Robotics Competition documentation. Again it may take a bit to get the handle on it, but it will be WAY easier to build on.

Hope that helps.

Appears there is a lot of great things with the Motion Magic and Closed loop motor controllers. Read the documentation and there is a significant learning curve on this topic.

Our team is using the Command-Based Programming and using code in the isFinished method to check sensor positions to determine when to end motion. One of the issues still working out is using the Forward or Reverse PIN on Falcon 500 to reset the sensor when the mechanism is as a certain location.

This may be true, but I honestly feel it is one of the biggest things someone can do to improve the performance of their robot. Finding ways to offload this type of logic is huge in my opinion. Maybe it isn’t something to figure out int he middle of a season though :man_shrugging:

Have you looked into it? What did you find the most confusing?

I think the only way to do this on the Falcon is to use a forward or reverse limit switch and run the following:
falcon.configClearPositionOnLimitR(true, timeoutMs);

I don’t think the Falcon supports the clear position on QuadIdx option like the TalonSRX.

What type of motion are you trying to do with what type of mechanism?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.