robot-2023/src/main/java/frc/robot/subsystems/BasePilotable.java
2023-04-11 17:23:38 -04:00

84 lines
2.9 KiB
Java

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot.subsystems;
import com.kauailabs.navx.frc.AHRS;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.PneumaticsModuleType;
import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import edu.wpi.first.wpilibj.shuffleboard.ShuffleboardTab;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
public class BasePilotable extends SubsystemBase {
final CANSparkMax avantdroit = new CANSparkMax(Constants.avantdroit, MotorType.kBrushless);
final CANSparkMax avantgauche = new CANSparkMax(Constants.avantgauche, MotorType.kBrushless);
final CANSparkMax arrieredroit = new CANSparkMax(Constants.arrieredroit, MotorType.kBrushless);
final CANSparkMax arrieregauche = new CANSparkMax(Constants.arrieregauche, MotorType.kBrushless);
final MotorControllerGroup droit = new MotorControllerGroup(avantdroit, arrieredroit);
final MotorControllerGroup gauche = new MotorControllerGroup(avantgauche, arrieregauche);
final DifferentialDrive drive = new DifferentialDrive(gauche, droit);
//piston
private DoubleSolenoid brake = new DoubleSolenoid(PneumaticsModuleType.CTREPCM,Constants.brakeouvre, Constants.brakeferme);
//gyro
ShuffleboardTab teb = Shuffleboard.getTab("teb");
double pitchoffset = 0;
private AHRS gyroscope = new AHRS();
public double getpitch() {
return -gyroscope.getPitch() + pitchoffset;
}
public void drive(double xSpeed, double zRotation){
drive.arcadeDrive(xSpeed, zRotation);
}
public void drive(double xSpeed, double zRotation, boolean square){
drive.arcadeDrive(xSpeed, zRotation, square);
}
public double distance(){
return (-avantdroit.getEncoder().getPosition()
+avantgauche.getEncoder().getPosition()
-arrieredroit.getEncoder().getPosition()
+arrieregauche.getEncoder().getPosition()) / 4;
}
public void Reset() {
avantdroit.getEncoder().setPosition(0);
avantgauche.getEncoder().setPosition(0);
arrieredroit.getEncoder().setPosition(0);
arrieregauche.getEncoder().setPosition(0);
}
public void resetGyro(){
pitchoffset = gyroscope.getPitch();
}
public void BrakeOuvre(){
brake.set(Value.kForward);
}
public void BrakeFerme(){
brake.set(Value.kReverse);
}
/** Creates a new BasePilotable. */
public BasePilotable() {
droit.setInverted(true);
teb.add(drive);
teb.addDouble("distancerobot",this::distance);
teb.addDouble("angle gyro", this::getpitch);
}
@Override
public void periodic() {
}
}