generated from Spartronics4915/Template
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
179 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/spartronics4915/frc/commands/JoystickMotorCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.spartronics4915.frc.commands; | ||
|
||
import edu.wpi.first.wpilibj2.command.CommandBase; | ||
import edu.wpi.first.wpilibj.Joystick; | ||
|
||
import com.spartronics4915.frc.Constants; | ||
import com.spartronics4915.frc.subsystems.JoystickMotor; | ||
|
||
public class JoystickMotorCommands | ||
{ | ||
private final JoystickMotor mMotor; | ||
private final Joystick mJoystick; | ||
private boolean mInverted; | ||
private boolean mSlow; | ||
|
||
public JoystickMotorCommands(JoystickMotor motor) | ||
{ | ||
mMotor = motor; | ||
mJoystick = motor.getJoystick(); | ||
mInverted = false; | ||
mSlow = false; | ||
} | ||
|
||
public class TeleOpCommand extends CommandBase | ||
{ | ||
public TeleOpCommand() | ||
{ | ||
addRequirements(mMotor); | ||
} | ||
|
||
@Override | ||
public void execute() | ||
{ | ||
double y = -1 * mJoystick.getY(); | ||
|
||
if (mSlow) | ||
{ | ||
y *= Constants.JoystickMotorConstants.kMotorSlowSpeed; | ||
} | ||
|
||
if (mInverted) | ||
{ | ||
y *= -1; | ||
} | ||
|
||
// copied from 2020-InfiniteRecharge, no idea how it works (magic) | ||
y = Math.copySign(Math.pow(Math.abs(y), 5.0/3.0), y); // apply response curve | ||
mMotor.set(applyDeadzone(y, Constants.JoystickMotorConstants.kJoystickDeadzone)); | ||
} | ||
|
||
// also copied from ir2020 | ||
private double applyDeadzone(double val, double deadzone) | ||
{ | ||
if (Math.abs(val) < deadzone) | ||
{ | ||
return 0.0; | ||
} | ||
return val; | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/spartronics4915/frc/subsystems/JoystickMotor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.spartronics4915.frc.subsystems; | ||
|
||
import com.spartronics4915.frc.Constants; | ||
import com.spartronics4915.lib.subsystems.SpartronicsSubsystem; | ||
|
||
import edu.wpi.first.wpilibj.Joystick; | ||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
|
||
import com.revrobotics.CANError; | ||
import com.revrobotics.CANSparkMax; | ||
import com.revrobotics.CANSparkMax.IdleMode; | ||
import com.revrobotics.CANSparkMaxLowLevel.MotorType; | ||
|
||
public class JoystickMotor extends SpartronicsSubsystem | ||
{ | ||
private static JoystickMotor sInstance = null; | ||
private static CANSparkMax mMotor; | ||
private static Joystick mJoystick; | ||
|
||
public JoystickMotor() | ||
{ | ||
boolean success = true; | ||
try | ||
{ | ||
mMotor = new CANSparkMax(Constants.kTestMotorId, MotorType.kBrushless); | ||
mJoystick = new Joystick(Constants.OI.kJoystickId); | ||
} | ||
catch (Exception exception) | ||
{ | ||
logException("Could not construct hardware: ", exception); | ||
success = false; | ||
} | ||
logInitialized(success); | ||
} | ||
|
||
public static JoystickMotor getInstance() | ||
{ | ||
if (sInstance == null) { | ||
sInstance = new JoystickMotor(); | ||
} | ||
return sInstance; | ||
} | ||
|
||
public void set(double speed) | ||
{ | ||
SmartDashboard.putString("Motor","Setting Speed"); | ||
mMotor.set(speed); | ||
SmartDashboard.putNumber("Speed",mMotor.get()); | ||
} | ||
|
||
public double getSpeed() | ||
{ | ||
SmartDashboard.putString("Motor","Getting Speed"); | ||
return mMotor.get(); | ||
} | ||
|
||
public Joystick getJoystick() | ||
{ | ||
return mJoystick; | ||
} | ||
} |