Skip to content

Commit 3447334

Browse files
authored
Merge pull request #91 from PhyXTGears-programming/develop
Develop
2 parents 6c03918 + 5b2cd60 commit 3447334

13 files changed

+261
-113
lines changed

src/main/cpp/Robot.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ RaiseHatch* Robot::m_RaiseHatch;
4444

4545
GrabHatchFromLoadingStation* Robot::m_GrabHatchFromLoadingStation;
4646
ReadyHatch* Robot::m_ReadyHatch;
47+
ReadyTakeHatch* Robot::m_ReadyTakeHatch;
4748

4849
// Initialize JSON reader
4950
wpi::json Robot::m_JsonConfig;
@@ -95,6 +96,7 @@ Robot::Robot() {
9596
// Allocate and initialize commands - Hatch
9697
m_GrabHatchFromLoadingStation = new GrabHatchFromLoadingStation();
9798
m_ReadyHatch = new ReadyHatch();
99+
m_ReadyTakeHatch = new ReadyTakeHatch();
98100
m_RaiseHatch = new RaiseHatch();
99101
m_LowerHatch = new LowerHatch();
100102
}
@@ -122,7 +124,9 @@ void Robot::RobotPeriodic() {
122124
frc::SmartDashboard::PutNumber("climb stage", m_ClimbStep->GetSegment());
123125
frc::SmartDashboard::PutBoolean("climb ready", GetCreeperClimb().IsArmAtPosition("arm-ready"));
124126
frc::SmartDashboard::PutBoolean("climb done", GetCreeperClimb().IsArmAtPosition("arm-climb"));
125-
frc::SmartDashboard::PutNumber("hatch anlge", GetHatchMechanism().GetArmPosition());
127+
frc::SmartDashboard::PutNumber("hatch angle", GetHatchMechanism().GetArmPosition());
128+
129+
frc::SmartDashboard::PutString("drive mode", GetDriveTrain().GetIdleModeText());
126130

127131
bool manualControl = abs(m_OI.GetOperatorConsole().GetThrottle()) >= 0.75;
128132
bool cargoControl = m_OI.GetOperatorConsole().GetThrottle() >= 0.75;
@@ -195,8 +199,6 @@ void Robot::AutonomousInit() {
195199
GetHatchMechanism().StopRotation();
196200
GetHatchMechanism().HoldPID();
197201

198-
m_CanSandstormStepDrive = true;
199-
200202
m_OI.ClearButtonBuffer();
201203
}
202204

@@ -205,8 +207,6 @@ void Robot::AutonomousPeriodic() {
205207
}
206208

207209
void Robot::TeleopInit() {
208-
m_CanSandstormStepDrive = false;
209-
210210
m_OI.ClearButtonBuffer();
211211
}
212212

@@ -230,10 +230,9 @@ void Robot::TestPeriodic() {}
230230

231231
void Robot::CompetitionJoystickInput() {
232232
// DRIVER CONTROLS
233-
if (m_OI.GetDriverJoystick().GetBButton() && m_CanSandstormStepDrive) {
233+
if (m_OI.GetDriverJoystick().GetBButtonPressed()) {
234234
std::cout << "Comp Joy Input: Driver: B Button Down" << std::endl;
235-
m_DriveSandstormStepWithCargo->Start();
236-
m_CanSandstormStepDrive = false;
235+
GetDriveTrain().ToggleIdleMode();
237236
}
238237

239238
if (m_OI.GetDriverJoystick().GetAButtonPressed()) {
@@ -340,10 +339,8 @@ void Robot::CompetitionJoystickInput() {
340339
m_ReadyHatch->Start();
341340
m_Bling.SetBling(m_Bling.HatchPattern);
342341
} else if (console.GetHatchLowerPressed()) {
343-
std::cout << "Comp Joy Input: Console: Hatch Lower Pressed" << std::endl;
344-
m_LowerHatch
345-
->Until([]() { return Robot::m_OI.GetOperatorConsole().GetHatchLowerReleased(); })
346-
->Start();
342+
std::cout << "Comp Joy Input: Console: Hatch Bottom Position Pressed" << std::endl;
343+
m_ReadyTakeHatch->Start();
347344
m_Bling.SetBling(m_Bling.HatchPattern);
348345
}
349346

@@ -406,6 +403,7 @@ void Robot::CompetitionJoystickInput() {
406403
m_Bling.SetBling(m_Bling.ClimbReady);
407404
} else if (console.GetCreeperClimbEnabled()) {
408405
std::cout << "Comp Joy Input: Console: Climb Sequence Pressed" << std::endl;
406+
GetDriveTrain().SetIdleMode(IdleMode::kBrake);
409407
m_ClimbStep->Start();
410408
m_Bling.SetBling(m_Bling.Climb);
411409
}

src/main/cpp/commands/ClimbStep.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ void ClimbStep::Execute() {
111111
case Segment::RaiseCylinder:
112112
Robot::GetCreeperClimb().RotateArmToPosition("home");
113113
Robot::GetCreeperClimb().PistonRetract();
114-
m_StopCylinderDelay.Start();
115-
m_Segment = Segment::StopCylinder;
114+
m_Segment = Segment::End;
116115
break;
117116
case Segment::StopCylinder:
118117
if (m_StopCylinderDelay.IsDone()) {
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "commands/ReadyTakeHatch.h"
2+
#include "Robot.h"
3+
4+
#include <iostream>
5+
using std::cout;
6+
using std::endl;
7+
8+
/* GOAL:
9+
*
10+
* Lift/lower hatch mechanism to bottom position and release hatch grip.
11+
*
12+
* Command start:
13+
* - Lift/lower hatch to bottom position with PID.
14+
* - Release hatch grip.
15+
*
16+
* Command execute:
17+
*
18+
* Command finished when:
19+
* - Arm reaches bottom position.
20+
*
21+
* Command end:
22+
* - Leave PID enabled to hold position. Arm lifts/lowers to setpoint with
23+
* hatch and weight of hatch should not pull arm down.
24+
*
25+
* Command interrupted:
26+
* - Stop rotation. Command was probably interrupted for a reason.
27+
*
28+
* Follow-up options:
29+
* - None.
30+
*/
31+
32+
ReadyTakeHatch::ReadyTakeHatch() {
33+
Requires(&Robot::GetHatchMechanism());
34+
}
35+
36+
void ReadyTakeHatch::Initialize() {
37+
HatchMechanism &hatch = Robot::GetHatchMechanism();
38+
39+
// Start moving to bottom position.
40+
hatch.RotateToBottomPosition();
41+
hatch.ReleaseHatch();
42+
}
43+
44+
void ReadyTakeHatch::Execute() {}
45+
46+
bool ReadyTakeHatch::IsFinished() {
47+
return Robot::GetHatchMechanism().IsArmRotationDone();
48+
}
49+
50+
void ReadyTakeHatch::End() {}
51+
52+
void ReadyTakeHatch::Interrupted() {
53+
Robot::GetHatchMechanism().StopRotation();
54+
}

src/main/cpp/subsystems/DriveTrain.cpp

+65-68
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,31 @@
55
#include <frc/smartdashboard/SendableBuilder.h>
66

77
#include <cmath>
8+
#include <iostream>
9+
#include <iomanip>
810

911
DriveTrain::DriveTrain(wpi::json &jsonConfig) : frc::Subsystem("DriveTrain") {
1012
# ifndef PROTOBOT
11-
// Set up TalonSRXs.
12-
m_MotorRightFront.ConfigFactoryDefault();
13-
m_MotorRightBack.ConfigFactoryDefault();
14-
m_MotorLeftFront.ConfigFactoryDefault();
15-
m_MotorLeftBack.ConfigFactoryDefault();
16-
17-
int contLimit = 40;
18-
int peakLimit = 50;
19-
int peakTime = 1800;
20-
21-
m_MotorRightFront.EnableCurrentLimit(true);
22-
m_MotorRightFront.ConfigContinuousCurrentLimit(contLimit, 10);
23-
m_MotorRightFront.ConfigPeakCurrentLimit(peakLimit, 10);
24-
m_MotorRightFront.ConfigPeakCurrentDuration(peakTime, 10);
25-
26-
m_MotorRightBack.EnableCurrentLimit(true);
27-
m_MotorRightBack.ConfigContinuousCurrentLimit(contLimit, 10);
28-
m_MotorRightBack.ConfigPeakCurrentLimit(peakLimit, 10);
29-
m_MotorRightBack.ConfigPeakCurrentDuration(peakTime, 10);
30-
31-
m_MotorLeftFront.EnableCurrentLimit(true);
32-
m_MotorLeftFront.ConfigContinuousCurrentLimit(contLimit, 10);
33-
m_MotorLeftFront.ConfigPeakCurrentLimit(peakLimit, 10);
34-
m_MotorLeftFront.ConfigPeakCurrentDuration(peakTime, 10);
35-
36-
m_MotorLeftBack.EnableCurrentLimit(true);
37-
m_MotorLeftBack.ConfigContinuousCurrentLimit(contLimit, 10);
38-
m_MotorLeftBack.ConfigPeakCurrentLimit(peakLimit, 10);
39-
m_MotorLeftBack.ConfigPeakCurrentDuration(peakTime, 10);
40-
41-
// The documentation says to do this, so that both sides get the proper values.
42-
// See https://phoenix-documentation.readthedocs.io/en/latest/ch15_WPIDrive.html?highlight=wpi_talon
43-
m_MotorRightFront.SetInverted(false);
44-
m_MotorRightBack.SetInverted(false);
45-
m_MotorLeftFront.SetInverted(true);
46-
m_MotorLeftBack.SetInverted(true);
13+
// m_MotorRightFront.SetSmartCurrentLimit(45);
14+
// m_MotorRightFront.SetSecondaryCurrentLimit(60);
4715

16+
// m_MotorRightBack.SetSmartCurrentLimit(45);
17+
// m_MotorRightBack.SetSecondaryCurrentLimit(60);
18+
19+
// m_MotorLeftFront.SetSmartCurrentLimit(45);
20+
// m_MotorLeftFront.SetSecondaryCurrentLimit(60);
21+
22+
// m_MotorLeftBack.SetSmartCurrentLimit(45);
23+
// m_MotorLeftBack.SetSecondaryCurrentLimit(60);
24+
25+
m_MotorRight1.SetInverted(false);
26+
m_MotorRight2.SetInverted(false);
27+
m_MotorRight3.SetInverted(false);
28+
m_MotorLeft1.SetInverted(true);
29+
m_MotorLeft2.SetInverted(true);
30+
m_MotorLeft3.SetInverted(true);
31+
32+
SetIdleMode(m_IdleMode);
4833
# endif
4934

5035
m_EncoderLeft.SetDistancePerPulse(kEncoderDistPerPulse);
@@ -54,7 +39,7 @@ DriveTrain::DriveTrain(wpi::json &jsonConfig) : frc::Subsystem("DriveTrain") {
5439
Subsystem::AddChild("Left Drive PID", &m_LeftPID);
5540
Subsystem::AddChild("Right Drive PID", &m_RightPID);
5641
#else
57-
m_MaxAcceleration = jsonConfig["drive"]["max-acceleration"];
42+
m_MinAcceleration = jsonConfig["drive"]["min-acceleration"];
5843

5944
m_MaxNormalSpeed = jsonConfig["drive"]["max-normal-speed"];
6045
m_SandstormStepSpeed = jsonConfig["drive"]["sandstorm-step-speed"];
@@ -88,11 +73,11 @@ void DriveTrain::Drive(frc::XboxController& driver) {
8873
// Get left stick axes values.
8974
double hidX = -driver.GetX(frc::XboxController::kRightHand);
9075
double hidY = driver.GetY(frc::XboxController::kLeftHand);
91-
double sprintFactor = 0.75;
76+
double sprintFactor = 0.65;
9277
sprintFactor += driver.GetTriggerAxis(frc::XboxController::kRightHand) * 0.25;
93-
sprintFactor -= driver.GetTriggerAxis(frc::XboxController::kLeftHand) * 0.25;
78+
sprintFactor -= driver.GetTriggerAxis(frc::XboxController::kLeftHand) * 0.20;
9479

95-
double turnFactor = 1 - (driver.GetTriggerAxis(frc::XboxController::kLeftHand) * 0.3); // THIS
80+
double turnFactor = 0.6 - (driver.GetTriggerAxis(frc::XboxController::kLeftHand) * 0.15); // THIS
9681

9782
if (ENABLE_DRIVETRAIN_CONTROL) {
9883
ArcadeDrive(hidY * sprintFactor, hidX * turnFactor, true);
@@ -162,27 +147,7 @@ void DriveTrain::ArcadeDrive(double xSpeed, double zRotation, bool squareInputs)
162147
double currentLeft = m_LeftMotors.Get() / m_maxOutput;
163148
double currentRight = m_RightMotors.Get() / m_maxOutput;
164149

165-
double maxLeftAccel = m_MaxAcceleration, maxRightAccel = m_MaxAcceleration;
166-
167-
/* *
168-
// Descrease the max velocity step size for the slower drive side so
169-
// turning takes effect sooner.
170-
//
171-
// WARNING: Doesn't really work from initial testing. Robot drives more straight.
172-
if (0.01 < std::abs(desiredLeft) - std::abs(desiredRight)) {
173-
// Left speed is faster than right.
174-
maxLeftAccel = m_MaxAcceleration;
175-
maxRightAccel = m_MaxAcceleration * desiredRight / desiredLeft;
176-
} else if (0.01 < std::abs(desiredRight) - std::abs(desiredLeft)) {
177-
// Right speed is faster than left.
178-
maxLeftAccel = m_MaxAcceleration * desiredLeft / desiredRight;
179-
maxRightAccel = m_MaxAcceleration;
180-
} else {
181-
// Both speeds are effectively same.
182-
maxLeftAccel = m_MaxAcceleration;
183-
maxRightAccel = m_MaxAcceleration;
184-
}
185-
/* */
150+
double minLeftAccel = m_MinAcceleration, minRightAccel = m_MinAcceleration;
186151

187152
double timeDelta = m_TimeDelta.Split();
188153

@@ -194,17 +159,17 @@ void DriveTrain::ArcadeDrive(double xSpeed, double zRotation, bool squareInputs)
194159
// v_{i+1} is the next velocity to send to the motors,
195160
// a is the max acceleration, and
196161
// dt is the time delta in seconds since the velocity step.
197-
double allowedLeft = ComputeNextOutputDelta(
162+
double allowedLeft = ComputeNextOutput(
198163
currentLeft,
199164
desiredLeft,
200-
maxLeftAccel,
165+
minLeftAccel,
201166
timeDelta
202167
);
203168

204-
double allowedRight = ComputeNextOutputDelta(
169+
double allowedRight = ComputeNextOutput(
205170
currentRight,
206171
desiredRight,
207-
maxRightAccel,
172+
minRightAccel,
208173
timeDelta
209174
);
210175

@@ -225,6 +190,38 @@ void DriveTrain::ArcadeDrive(double xSpeed, double zRotation, bool squareInputs)
225190
Feed();
226191
}
227192

193+
void DriveTrain::SetIdleMode(rev::CANSparkMax::IdleMode mode) {
194+
m_IdleMode = mode;
195+
196+
m_MotorRight1.SetIdleMode(mode);
197+
m_MotorRight2.SetIdleMode(mode);
198+
m_MotorRight3.SetIdleMode(mode);
199+
200+
m_MotorLeft1.SetIdleMode(mode);
201+
m_MotorLeft2.SetIdleMode(mode);
202+
m_MotorLeft3.SetIdleMode(mode);
203+
}
204+
205+
void DriveTrain::ToggleIdleMode() {
206+
if (IdleMode::kCoast == m_IdleMode) {
207+
SetIdleMode(IdleMode::kBrake);
208+
} else {
209+
SetIdleMode(IdleMode::kCoast);
210+
}
211+
}
212+
213+
IdleMode DriveTrain::GetIdleMode() {
214+
return m_IdleMode;
215+
}
216+
217+
wpi::StringRef DriveTrain::GetIdleModeText() {
218+
if (IdleMode::kCoast == m_IdleMode) {
219+
return "Coast";
220+
} else {
221+
return "Brake";
222+
}
223+
}
224+
228225
void DriveTrain::StopMotor() {
229226
#ifdef USE_DRIVETRAIN_PID
230227
m_LeftPID.Disable();
@@ -301,7 +298,7 @@ void DriveTrain::UseDukesSpeedLimit() {
301298
#endif
302299
}
303300

304-
double DriveTrain::ComputeNextOutputDelta(double iVel, double fVel, double maxAccel, double timeDelta) {
305-
double deltaVel = fVel - iVel;
306-
return iVel + std::copysign(std::min(std::abs(deltaVel), maxAccel * timeDelta), deltaVel);
301+
double DriveTrain::ComputeNextOutput(double iVel, double fVel, double minAccel, double timeDelta) {
302+
double deltaVel = (fVel - iVel) * 0.4;
303+
return iVel + std::copysign(std::min(std::fabs(deltaVel), minAccel), deltaVel);
307304
}

src/main/cpp/subsystems/HatchMechanism.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ HatchMechanism::HatchMechanism(wpi::json &jsonConfig) : Subsystem("HatchMechanis
2424

2525
m_Config.MidPosition = jsonConfig["hatch"]["rotation"]["mid-position"];
2626
m_Config.TopPosition = jsonConfig["hatch"]["rotation"]["top-position"];
27+
m_Config.BottomPosition = jsonConfig["hatch"]["rotation"]["bottom-position"];
2728
m_Config.GripThreshold = jsonConfig["hatch"]["rotation"]["grip-threshold"];
2829

2930
AddChild("Hatch Arm Position", &m_ArmPosition);
@@ -43,6 +44,11 @@ void HatchMechanism::LowerHatch () {
4344
SetRotateSpeed(HatchGrabberSpeed);
4445
}
4546

47+
void HatchMechanism::RotateToBottomPosition() {
48+
m_ArmPID.SetSetpoint(m_Config.BottomPosition);
49+
m_ArmPID.Enable();
50+
}
51+
4652
void HatchMechanism::RotateToMidPosition() {
4753
m_ArmPID.SetSetpoint(m_Config.MidPosition);
4854
m_ArmPID.Enable();

0 commit comments

Comments
 (0)