-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.java
144 lines (122 loc) · 5.09 KB
/
Robot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/* @author Ian Fernandes and Novice Programmers */
/*----------------------------------------------------------------------------*/
package frc.robot;
//Import necessary classes to run robot hardware commands
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.PWMVictorSPX;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.cameraserver.CameraServer;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the TimedRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class Robot extends TimedRobot {
//Define left side of differential drive as a SpeedControllerGroup object
Spark leftFront = new Spark(5);
Spark leftRear = new Spark(1);
SpeedControllerGroup left = new SpeedControllerGroup(leftFront, leftRear);
//Define right side of differential drive as a SpeedControllerGroup object
Spark rightFront = new Spark(2);
Spark rightRear = new Spark(3);
SpeedControllerGroup right = new SpeedControllerGroup(rightFront, rightRear);
//Construct speed controller to operate rotational-action hatch motor
PWMVictorSPX hatchMotor = new PWMVictorSPX(4);
//Construct speed controller to operate cargo motor
PWMVictorSPX cargoIntake = new PWMVictorSPX(0);
//Construct DifferentialDrive object
private final DifferentialDrive differentialDrive
= new DifferentialDrive(left, right);
//Construct two joysticks, one for driving, one for "arm" operations
private final Joystick driveStick = new Joystick(0);
private final Joystick armStick = new Joystick(1);
//Construct boolean for button to toggle high speed/low speed for differential drive.
private boolean highSpeed = true;
//Constants for rotational-action hatch motor and cargo motor
private final double ROTATIONAL_HATCH_MOTOR_POWER = .75;
private final double CARGO_MOTOR_POWER = .3;
private final Timer autonomousTimer = new Timer();
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
@Override
public void robotInit() {
//Start camera streams to driver station.
CameraServer.getInstance().startAutomaticCapture();
CameraServer.getInstance().startAutomaticCapture();
//Optional
hatchMotor.setSafetyEnabled(true);
cargoIntake.setSafetyEnabled(true);
}
/**
* This function is run once each time the robot enters autonomous mode.
*/
@Override
public void autonomousInit() {
//Set up autonomous timer.
autonomousTimer.reset();
autonomousTimer.start();
}
/**
* This function is called periodically during autonomous.
*/
@Override
public void autonomousPeriodic() {
if(driveStick.getRawButton(11))
highSpeed = !highSpeed;
//Operate differential drive with arcade drive function using the driveStick's Y and X values respectively.
differentialDrive.arcadeDrive((highSpeed?(-driveStick.getY()*.75):(-driveStick.getY()/4.)), driveStick.getX()*.75);
//If the trigger on armStick is pushed, move motor forward.
hatchMotor.set(armStick.getTrigger()?ROTATIONAL_HATCH_MOTOR_POWER:0);
//Operate linear hatch with deadband of 0.5 and instantaneous speed
if(armStick.getY()<-0.5)
cargoIntake.set(CARGO_MOTOR_POWER);
else if(armStick.getY()>0.5)
cargoIntake.set(-CARGO_MOTOR_POWER);
else
cargoIntake.set(0);
}
/**
* This function is called once each time the robot enters teleoperated mode.
*/
@Override
public void teleopInit() {
}
/**
* This function is called periodically during teleoperated mode.
*/
@Override
public void teleopPeriodic() {
if(driveStick.getRawButton(11))
highSpeed = !highSpeed;
//Operate differential drive with arcade drive function using the driveStick's Y and X values respectively.
differentialDrive.arcadeDrive((highSpeed?(-driveStick.getY()*.75):(-driveStick.getY()/4.)), driveStick.getX()*.75);
//If the trigger on armStick is pushed, move motor forward.
hatchMotor.set(armStick.getTrigger()?ROTATIONAL_HATCH_MOTOR_POWER:0);
//Operate cargo mechanism
if(armStick.getY()<-0.5)
cargoIntake.set(CARGO_MOTOR_POWER);
else if(armStick.getY()>0.5)
cargoIntake.set(-CARGO_MOTOR_POWER);
else
cargoIntake.set(0);
}
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic() {
}
}