-
Notifications
You must be signed in to change notification settings - Fork 1
/
Robot.java
285 lines (246 loc) · 6.64 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 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. */
/*----------------------------------------------------------------------------*/
package frc.robot;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;
import com.analog.adis16448.frc.ADIS16448_IMU;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.GenericHID.Hand;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
import edu.wpi.first.wpilibj.Encoder;
/**
* 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 build.gradle file in the
* project.
*/
public class Robot extends TimedRobot {
private Command m_autonomousCommand;
private RobotContainer m_robotContainer;
XboxController StickB; // 底盤控制
XboxController StickS; // 投射器
WPI_VictorSPX LFMotor; // 左前
WPI_VictorSPX RFMotor; // 右前
WPI_VictorSPX LBMotor; // 左後
WPI_VictorSPX RBMotor; // 右後
WPI_VictorSPX ArmMotor;
WPI_VictorSPX HMotorA;
WPI_VictorSPX HMotorB;
WPI_VictorSPX LSMotor; // 左投
WPI_VictorSPX RSMotor; // 右投
WPI_VictorSPX XMotor; // 左右
CANSparkMax BMotor; // 上下
SpeedControllerGroup RMotors; // 右側馬達群組
SpeedControllerGroup LMotors; // 左側馬達群組
SpeedControllerGroup HangMotors; // 爬升馬達群組
double LSpeed; // 左
double RSpeed; // 右
double Hang;
double InB;
double OutB;
int SparkId=5;
int Arm;
int Encord;
int auto;
/**
* This function is run when the robot is first started up and should be used
* for any initialization code.
*/
@Override
public void robotInit()
{
// Instantiate our RobotContainer. This will perform all our button bindings,
// and put our
// autonomous chooser on the dashboard.
m_robotContainer = new RobotContainer();
StickB = new XboxController(0);
StickS = new XboxController(1);
RFMotor = new WPI_VictorSPX(4);
RBMotor = new WPI_VictorSPX(3);
LFMotor = new WPI_VictorSPX(2);
LBMotor = new WPI_VictorSPX(1);
BMotor = new CANSparkMax(5,MotorType.kBrushless);
HMotorA = new WPI_VictorSPX(7);
HMotorB = new WPI_VictorSPX(8);
ArmMotor = new WPI_VictorSPX(6);
RMotors = new SpeedControllerGroup(RFMotor, RBMotor);
LMotors = new SpeedControllerGroup(LFMotor, LBMotor);
HangMotors = new SpeedControllerGroup(HMotorA, HMotorB);
}
Timer Autotimer = new Timer();
Timer ArmTimer = new Timer();
Encoder Encoder = new Encoder(2,4);
ADIS16448_IMU imu = new ADIS16448_IMU();
/**
* This function is called every robot packet, no matter the mode. Use this for
* items like diagnostics that you want ran during disabled, autonomous,
* teleoperated and test.
*
* <p>
* This runs after the mode specific periodic functions, but before LiveWindow
* and SmartDashboard integrated updating.
*/
@Override
public void robotPeriodic()
{
// CommandScheduler.getInstance().run();
}
/**
* This function is called once each time the robot enters Disabled mode.
*/
@Override
public void disabledInit()
{
}
@Override
public void disabledPeriodic()
{
}
/**
* This autonomous runs the autonomous command selected by your
* {@link RobotContainer} class.
*/
@Override
public void autonomousInit()
{
m_autonomousCommand = m_robotContainer.getAutonomousCommand();
auto = 0;
Autotimer.reset();
}
@Override
public void autonomousPeriodic()
{
switch(Encord)
{
case 0:
if(Encoder.get()>=5)
{
RMotors.set(0);
LMotors.set(0);
Encord++;
}
if(imu.getGyroAngleX()==0)
{
RMotors.set(0.3);
LMotors.set(-0.3);
}
else if(imu.getGyroAngleX() > 0 && imu.getGyroAngleX()<180)
{
RMotors.set(0.1);
LMotors.set(-0.3);
}
else if(imu.getGyroAngleX() < 360 && imu.getGyroAngleX()>180)
{
RMotors.set(0.3);
LMotors.set(-0.1);
}
case 1:
break;
}
switch(auto)
{
case 0:
if(Autotimer.get()==0)
{
Autotimer.start();
}
if(Autotimer.get()==0.5)
{
ArmMotor.set(0);
BMotor.set(-0.2);
auto++;
}
else
{
ArmMotor.set(0.2);
}
case 1:
break;
}
}
@Override
public void teleopInit()
{
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
if (m_autonomousCommand != null)
{
m_autonomousCommand.cancel();
}
}
/**
* This function is called periodically during operator control.
*/
@Override
public void teleopPeriodic()
{
RSpeed = -StickB.getY(Hand.kRight);
LSpeed = StickB.getY(Hand.kLeft);
RSpeed = RSpeed*0.7;
LSpeed = LSpeed*0.7;
RMotors.set(RSpeed);
LMotors.set(LSpeed);
//------------------------------------
if(StickB.getBumper(Hand.kLeft))
{
ArmMotor.set(-0.2);
}
else if(StickB.getBumper(Hand.kRight))
{
ArmMotor.set(0.2);
}
else
{
ArmMotor.set(0);
}
if(StickB.getYButton())
{
BMotor.set(0.02);
}
else if(!StickB.getYButton())
{
BMotor.set(0);
}
if(StickB.getBButton())
{
HangMotors.set(0.5);
}
else if(!StickB.getBButton() )
{
HangMotors.set(0);
}
if(StickB.getAButton())
{
HangMotors.set(-0.5);
}
else if(!StickB.getAButton())
{
HangMotors.set(0);
}
}
@Override
public void testInit()
{
// Cancels all running commands at the start of test mode.
CommandScheduler.getInstance().cancelAll();
}
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic()
{
}
}