Skip to content

Using Encoders to Control Motor Position

12sliu edited this page Dec 20, 2023 · 3 revisions

🛞 Using Encoders to Control Motor Position

Encoders are sensors that get the position of a motor in "counts"; there are a certain number of counts per second for each motor that can be found on the REV website and have to be adjusted for gear ratios.

Here is a page for how to code motors using encoders. It is useful for driving drivetrains certain distances (although should not be used by itself here as it does not take into account the robot slipping) and for moving arms to certain heights.)

Bulk Reads

Loop times are how long it takes for the robot to complete a loop in software. Usually, below 50 ms is a good loop time. One should not worry about loop times specifically being an issue unless you are using swerve in FTC. However, decreasing loop times in general is still good practice.

A much better explanation of bulk reads can be found in ConceptMotorBulkRead in the samples of the SDK, and you should absolutely refer to that.

TLDR: Manually clearing the cache is the most efficient way of reads. At init(), go through all of your LynxModules (representation of Expansion Hub or Control Hub in code) and set bulk caching mode to be MANUAL. At loop(), clear the bulk cache for every LynxModule before reading from motors.

However, one should still only read from motors once every loop, save it somewhere, and then pass it around as needed, as that is guaranteed to be the fastest.

Component Classes and Bulk Reads

Read only if in 2023-2024 or interested in software architecture

In the 2023 to 2024 season, we are using component classes.

Having one giant bloated OpMode with all of the functionality of the robot is bad programming practice. Disadvantages:

  • It makes code harder to debug, as to test one function of the robot, all the other functions will get in the way.
  • Increases the chances of merge conflicts.
  • Harder to read and less organised (If a programmer needs to find the CV code, they shouldn’t have to look for it in the arm code.)

We decided to have the functionality of each component of the robot in a separate class. (e.g. drivetrain in MecanumDrivetrain.java, arm in ArmComponent.java). These components have getters (methods that return a field) for velocity and position of their systems, which used to directly read from the motor.

Now, a read() method of every component is manually called in every loop before using getters. read() simply reads from the motors and then saves it in a private field. The getters now return the value in those private fields.

It is imperative that read() is called every loop, or the getters will return bad values!

Clone this wiki locally