Last active
June 30, 2023 17:08
-
-
Save pleasehelpme798/3020ef5c4b3440f1e717788c330d9994 to your computer and use it in GitHub Desktop.
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
// 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; | |
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | |
import edu.wpi.first.wpilibj.Encoder; | |
import edu.wpi.first.wpilibj.XboxController; | |
import edu.wpi.first.wpilibj.TimedRobot; | |
import edu.wpi.first.wpilibj.drive.DifferentialDrive; | |
import edu.wpi.first.wpilibj.motorcontrol.MotorController; | |
import edu.wpi.first.wpilibj.motorcontrol.PWMSparkMax; | |
import edu.wpi.first.cameraserver.CameraServer; | |
import edu.wpi.first.cscore.UsbCamera; | |
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup; | |
import edu.wpi.first.math.controller.PIDController; | |
public class Robot extends TimedRobot { | |
//Differential Drive | |
private DifferentialDrive m_myRobot; | |
//Declaring The port that the xbox controller will use in smart dashboard | |
private XboxController cplusplus = new XboxController(0); | |
//This is setting up the camera to read from usb port zero | |
UsbCamera Cameranumber1 = CameraServer.startAutomaticCapture(0); | |
//Motor names and their respective pwm ports | |
private final MotorController front_leftMotor = new PWMSparkMax(2); | |
private final MotorController front_rightMotor = new PWMSparkMax(0); | |
private final MotorController rear_leftMotor = new PWMSparkMax(3); | |
private final MotorController rear_rightMotor = new PWMSparkMax(1); | |
//Allows multiple motors to be controlled with the differential drive | |
MotorControllerGroup LeftMotors = new MotorControllerGroup(front_leftMotor,rear_leftMotor); | |
MotorControllerGroup RightMotors = new MotorControllerGroup(front_rightMotor,rear_rightMotor); | |
//Encoder Names | |
Encoder encoder1, encoder2, encoder3, encoder4; | |
//PID controller values | |
PIDController pidleft; | |
PIDController pidright; | |
double kpleft = 0; | |
double kileft = 0; | |
double kdleft = 0; | |
double kpright = 0; | |
double kiright = 0; | |
double kdright = 0; | |
//Values for encoders | |
private static final double cpr = 360; //this is representative of the resolution of the encoders | |
private static final double whd = 8; //this variable is representative of the 8 inch wheel diameter | |
@Override | |
public void robotInit() { | |
/* We need to invert one side of the drivetrain so that positive voltages | |
result in both sides moving forward*/ | |
front_rightMotor.setInverted(true); | |
rear_rightMotor.setInverted(true); | |
//Creating a differntial drive | |
m_myRobot = new DifferentialDrive(LeftMotors, RightMotors); | |
//Declaring the ports that the encoders are plugged into | |
encoder1 = new Encoder(0,1); | |
encoder2 = new Encoder(2,3); | |
encoder3 = new Encoder(4,5); | |
encoder4 = new Encoder(6,7); | |
//Setting the distance per pulse on encoders | |
encoder1.setDistancePerPulse(Math.PI*whd/cpr); | |
encoder2.setDistancePerPulse(Math.PI*whd/cpr); | |
encoder3.setDistancePerPulse(Math.PI*whd/cpr); | |
encoder4.setDistancePerPulse(Math.PI*whd/cpr); | |
//Declaring the names of the PID controllers | |
pidleft = new PIDController(kpleft, kileft, kdleft); | |
pidright = new PIDController(kpright, kiright, kdright); | |
} | |
@Override | |
public void teleopPeriodic() { | |
//This is a type of differential drive that controls each side of the robot independently | |
m_myRobot.tankDrive((pidleft.calculate(encoder1.getRate(), cplusplus.getRawAxis(1)/2)), (pidright.calculate(encoder2.getDistance(), cplusplus.getRawAxis(5)/2))); | |
//Displaying the encoder values to Smart dashboard | |
SmartDashboard.putNumber("Encoder1", encoder1.getRate()); | |
SmartDashboard.putNumber("Encoder2", encoder2.getRate()); | |
SmartDashboard.putNumber("Encoder3", encoder3.getRate()); | |
SmartDashboard.putNumber("Encoder4", encoder4.getRate()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment