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.
Merge pull request #9 from ss0g/main
joystick working
- Loading branch information
Showing
4 changed files
with
195 additions
and
6 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
54 changes: 54 additions & 0 deletions
54
src/main/java/com/spartronics4915/frc/commands/JoystickMotorCommand.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,54 @@ | ||
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 JoystickMotorCommand extends CommandBase | ||
{ | ||
private final JoystickMotor mJoystickMotor; | ||
private final Joystick mJoystick; | ||
private boolean mInverted; | ||
private boolean mSlow; | ||
|
||
public JoystickMotorCommand(JoystickMotor motor) | ||
{ | ||
mJoystickMotor = motor; | ||
mJoystick = motor.getJoystick(); | ||
mInverted = false; | ||
mSlow = false; | ||
addRequirements(mJoystickMotor); | ||
} | ||
|
||
@Override | ||
public void execute() | ||
{ | ||
double y = -1 * mJoystick.getY(); | ||
|
||
if (mSlow) | ||
{ | ||
y *= JoystickMotorConstants.kMotorSlowSpeed; | ||
} | ||
|
||
if (mInverted) | ||
{ | ||
y *= -1; | ||
} | ||
|
||
// copied from 2020-InfiniteRecharge, no idea how it works (magic) | ||
y = Math.copySign(Math.pow(Math.abs(y), JoystickMotorConstants.kJoystickResponseCurve), y); // apply response curve | ||
mJoystickMotor.set(applyDeadzone(y, 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; | ||
} | ||
} |