The CANTalon speed controllers are very nice and offer a lot more features than most of the other speed controllers. So, there are at least two ways to approach this problem:
Ignore the enhanced features and wire the encoder to your roboRIO and use a standard PIDController.
Use the enhanced features of the CANTalons (onboard PID controller and wire your encoder directly to the "leader" of each side).
If you want to ignore the enhanced feature, and run your CANTalons in percent vbus mode:
- Construct a CANTalon as the "leader" for the left side of your drive train and change its control mode to percent vbus (or one of the other control modes that the CANTalon does not use it's own PIDController on).
- For each additional motor on the left side, change its control mode to follower and set it to follow the "leader" (mimic the leader's output).
- Connect your left side encoder cables directly to the roboRIO and create a standard Encoder object to manage them.
- Construct a PIDController that uses the "leader" CANTalon as the output and the Encoder as the input.
- When you want to move a specific distance, set the target and start your PIDController.
- When you want to get out of "postion mode" make sure you stop your PIDController (otherwise it will fight your driver).
If you want to use the enhanced features, you can run your CANTalons in position mode:
- Construct a CANTalon as the leader for the left side of your drive train and change its control mode to position and set the P, I and D values (there may be some additional set up here).
- For each additional motor on the left side, change its control mode to follower and set it to follow the "leader" (mimic the leader's output).
- Connect your left side encoder cables directly to the "leader" CANTalon (not the roboRIO).
- Set the position you want to move to on the "leader" and the CANTalons should do the rest.
- When you want to get out of "postion mode", you will need to change the control mode of the "leader" CANTalon (otherwise it will fight your driver).
Regardless of the approach you take, it will probably be useful to create a separate class to encapsulate your logic as the left side and right side will be almost identical.