Bionic-Bison-5535 / Bionic-Bison-2023-Competition-Robot

This is FRC team 5535's robot code for the 2023 competition.
1 stars 0 forks source link

CANSparkMax vs Talons - Robot adaptability #9

Closed WesleyMcGinn closed 1 year ago

WesleyMcGinn commented 1 year ago

Introduction

On Saturday, February 18th, our team had a plentiful dose of motor errors and major concerns for the CANSparkMaxs. Last year, we had several major Talon SRX issues at the Lakeview competition. In the midst of competition, if one motor type stops working, we may want to immediately change over to the other and diagnose the problem later. Because of this, I suggest that we incorporate functionality for CANSparkMaxs and Talons into our robot code.

Proposal

Similar to what is done in Weswerve.java, line 17, we could use a boolean statement at the beginning of each system class that reads true if we are using Talons and false if we are using CANSparkMaxs. Although this would add more code to the robot that may not be absolutely necessary, I think that it is worth the backup plan that it would provide us with.

Differences In Code Between CANSparkMaxs and Talons

CANSparkMax Code Talon SRX Code
Import 1 import com.revrobotics.CANSparkMax; import com.ctre.phoenix.motorcontrol.can.TalonSRX;
Import 2 import com.revrobotics.CANSparkMaxLowLevel.MotorType; import com.ctre.phoenix.motorcontrol.ControlMode;
Declaration public CANSparkMax myMotor; public TalonSRX myMotor;
Initialization myMotor = new CANSparkMax(<CAN_ID>, MotorType.kBrushless); myMotor = new TalonSRX(<CAN_ID>);
Set Speed myMotor.set(<SPEED>); myMotor.set(ControlMode.PercentOutput, <SPEED>);

Note: Replace <CAN_ID> with the CAN ID of the motor and replace <SPEED> with a value from -1 to 1.

WesleyMcGinn commented 1 year ago

Something else to keep in mind is the different references to encoder values from Talons and CANSparkMaxs:

CANSparkMax Encoder Code Talon SRX Code
Import import com.revrobotics.RelativeEncoder; No Encoder Import Needed
Initialization myEncoder = myMotor.getEncoder(); No Encoder Initialization Needed
Set Value myEncoder.setPosition(<ticks>); myMotor.setSelectedSensorPosition(<ticks>);
Get Value myEncoder.getPosition() myMotor.getSelectedSensorPosition()

Additionally, NEOs and Talons spin in opposite directions. The following function may be needed, which works the same for both NEOs and Talons: myMotor.setInverted(true);

WesleyMcGinn commented 1 year ago

This issue was solved with the robot-motor-adaptability branch.

WesleyMcGinn commented 1 year ago

The new Motor.java class can be used as follows:

Action: Code for Action:
Import import frc.robot.systems.Motor;
Declaration public Motor myMotor;
Initialization myMotor = new Motor(<CAN_ID>, <IS_TALON?>, <DIRECTION>);
Set Speed myMotor.set(<SPEED>);
Get Encoder Value myMotor.getEnc()
Set Encoder Value myMotor.setEnc(<ENCODER_TICKS>);
Set Motor Position myMotor.goTo(<ENCODER_TICKS_FROM_START>);
Update* myMotor.update();

*This function must be called periodically if you used the .goTo function. It will update the speed of the motor based on how far it is from the desired encoder value.

WesleyMcGinn commented 1 year ago

ERROR!

The motor function myMotor.goTo(<ENCODER_TICKS_FROM_START>); was just found to have an issue: The motor will adjust to go to its original location, but will not go to any other position.

This will be worked on immediately.

WesleyMcGinn commented 1 year ago

Fixed Issue

This issue was resolved by the last commit. The motors should now automatically adjust to reach their set positions when the update() function is called.