Skip to content

Commit

Permalink
Working servo testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JonahEMorgan committed Jan 19, 2025
1 parent bacb161 commit e0b49b3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public static final class ControllerConstants {
public static final double kTriggerDeadzone = .05;
}

public static final class ServoConstants {
public static final int kMaxRotation = 270;
}

public static final class DriveConstants {
// CAN IDs (updated)
public static final int kFrontRightDrivePort = 10;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import edu.wpi.first.wpilibj2.command.button.CommandPS4Controller;
import frc.robot.Constants.ControllerConstants;
import frc.robot.subsystems.DriveSubsystem;
import frc.robot.subsystems.ServoSubsystem;

public class Robot extends TimedRobot {
private Command m_autonomousCommand;
private final DriveSubsystem m_driveSubsystem = new DriveSubsystem();
private final CommandPS4Controller m_driverController = new CommandPS4Controller(
ControllerConstants.kDriverControllerPort);
private final PowerDistribution m_pdh = new PowerDistribution();
private final ServoSubsystem m_servoSubsystem = new ServoSubsystem();

public Robot() {
SmartDashboard.putData(m_pdh);
Expand All @@ -43,6 +45,10 @@ public void bindDriveControls() {
() -> -m_driverController.getLeftX(),
() -> m_driverController.getR2Axis() - m_driverController.getL2Axis(),
m_driverController.getHID()::getSquareButton));
m_driverController.L1().onTrue(m_servoSubsystem.rotateCommand(0.0));
m_driverController.L2().onTrue(m_servoSubsystem.rotateCommand(null));
m_driverController.R1().onTrue(m_servoSubsystem.rotateCommand(270.0));
m_driverController.R2().onTrue(m_servoSubsystem.rotateCommand(200.0));
}

@Override
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/frc/robot/Subsystems/ServoSubsystem.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package frc.robot.Subsystems;
package frc.robot.subsystems;

import static frc.robot.Constants.ServoConstants.*;

import edu.wpi.first.wpilibj.Servo;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;

public class ServoSubsystem extends SubsystemBase {
Expand All @@ -12,20 +15,28 @@ public class ServoSubsystem extends SubsystemBase {
public ServoSubsystem() {
m_motor = new Servo(0);
time = new Timer();
m_motor.setBoundsMicroseconds(2500, 0, 1500, 0, 500);
}

public void init() {
m_motor.setZeroLatch();
}

public void run() {
// TODO Auto-generated method stub
super.periodic();

time.start();
@Override
public void periodic() {
SmartDashboard.putNumber("Time", time.get());
SmartDashboard.putNumber("Position", m_motor.getPosition());
}

m_motor.set(1.0);
/**
* Rotates the servo. Does not wait for it to finish.
*
* @param angle The angle from 0 in the left to 270 in the right.
* @return The command.
*/
public Command rotateCommand(Double angle) {
if (angle == null)
return runOnce(() -> m_motor.set(.5));
return runOnce(() -> m_motor.set(angle / kMaxRotation));
}
}

0 comments on commit e0b49b3

Please sign in to comment.