Skip to content

Commit 7cda2ba

Browse files
authored
Merge pull request #82 from PhyXTGears-programming/feature/hatch-pid
Feature/hatch pid
2 parents bfa230a + 3bf9604 commit 7cda2ba

18 files changed

+454
-134
lines changed

src/main/cpp/Robot.cpp

+40-14
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ CreeperClimb* Robot::m_CreeperClimb;
1111
HatchMechanism* Robot::m_HatchMechanism;
1212

1313
// Initialize Commands - Intake
14-
GrabHatchFromDispenser* Robot::m_GrabHatchFromDispenser;
15-
ReleaseHatch* Robot::m_ReleaseHatch;
1614
RotateCargoForCargoShip* Robot::m_RotateCargoForCargoShip;
1715
RotateCargoForLevelOneRocket* Robot::m_RotateCargoForLevelOneRocket;
18-
RotateHatchForFloor* Robot::m_RotateHatchForFloor;
19-
RotateHatchForDispenser* Robot::m_RotateHatchForDispenser;
2016

2117
ShootCargoForCargoShip* Robot::m_ShootCargoForCargoShip;
2218
ShootCargoForLevelOneRocket* Robot::m_ShootCargoForLevelOneRocket;
@@ -35,6 +31,13 @@ ClimbStep* Robot::m_ClimbStep;
3531
DriveSandstormStepWithCargo* Robot::m_DriveSandstormStepWithCargo;
3632
DriveSandstormStepWithHatch* Robot::m_DriveSandstormStepWithHatch;
3733

34+
// Initialize Commands - Hatch
35+
LowerHatch* Robot::m_LowerHatch;
36+
RaiseHatch* Robot::m_RaiseHatch;
37+
38+
GrabHatchFromLoadingStation* Robot::m_GrabHatchFromLoadingStation;
39+
ReadyHatch* Robot::m_ReadyHatch;
40+
3841
// Initialize JSON reader
3942
wpi::json Robot::m_JsonConfig;
4043

@@ -65,12 +68,8 @@ Robot::Robot() {
6568
m_DriveSandstormStepWithHatch = new DriveSandstormStepWithHatch();
6669

6770
// Allocate and initialize commands - Intake
68-
m_GrabHatchFromDispenser = new GrabHatchFromDispenser();
69-
m_ReleaseHatch = new ReleaseHatch();
7071
m_RotateCargoForCargoShip = new RotateCargoForCargoShip();
7172
m_RotateCargoForLevelOneRocket = new RotateCargoForLevelOneRocket();
72-
m_RotateHatchForFloor = new RotateHatchForFloor();
73-
m_RotateHatchForDispenser = new RotateHatchForDispenser();
7473

7574
m_ShootCargoForCargoShip = new ShootCargoForCargoShip();
7675
m_ShootCargoForLevelOneRocket = new ShootCargoForLevelOneRocket();
@@ -84,6 +83,10 @@ Robot::Robot() {
8483
// Allocate and initialize commands -
8584
m_ReadyCreeperArm = new ReadyCreeperArm();
8685
m_ClimbStep = new ClimbStep();
86+
87+
// Allocate and initialize commands - Hatch
88+
m_GrabHatchFromLoadingStation = new GrabHatchFromLoadingStation();
89+
m_ReadyHatch = new ReadyHatch();
8790
}
8891

8992
void Robot::RobotInit() {
@@ -150,6 +153,7 @@ void Robot::DisabledInit() {
150153

151154
GetCreeperClimb().Disable();
152155
GetCargoIntake().Disable();
156+
GetHatchMechanism().Disable();
153157

154158
// Clear pending commands out of scheduler.
155159
// frc::Scheduler::GetInstance()->ResetAll();
@@ -165,6 +169,11 @@ void Robot::AutonomousInit() {
165169
GetDriveTrain().RunReset();
166170
GetCreeperClimb().RunReset();
167171
GetCargoIntake().RunReset();
172+
173+
// Some positions of hatch mechanism are compliant, so disable
174+
// PID/rotation instead of RunReset.
175+
GetHatchMechanism().StopRotation();
176+
168177
m_CanSandstormStepDrive = true;
169178

170179
m_OI.ClearButtonBuffer();
@@ -242,16 +251,33 @@ void Robot::CompetitionJoystickInput() {
242251
m_Bling.SetBling(m_Bling.CargoIntakePattern);
243252
}
244253

245-
if (console.GetHatchGrabReleased() || console.GetHatchReleaseReleased()) {
246-
std::cout << "Comp Joy Input: Stop Hatch Rotate" << std::endl;
247-
GetHatchMechanism().StopRotation();
248-
} else if (console.GetHatchGrabPressed()) {
254+
if (console.GetHatchGrabPressed()) {
249255
std::cout << "Comp Joy Input: Console: Hatch Grab Pressed" << std::endl;
250-
GetHatchMechanism().RaiseHatch();
256+
m_RaiseHatch
257+
->Until([]() { return Robot::m_OI.GetOperatorConsole().GetHatchGrabReleased(); })
258+
->Start();
251259
m_Bling.SetBling(m_Bling.HatchPattern);
252260
} else if (console.GetHatchReleasePressed()) {
253261
std::cout << "Comp Joy Input: Console: Hatch Release Pressed" << std::endl;
254-
GetHatchMechanism().LowerHatch();
262+
m_LowerHatch
263+
->Until([]() { return Robot::m_OI.GetOperatorConsole().GetHatchReleaseReleased(); })
264+
->Start();
265+
m_Bling.SetBling(m_Bling.HatchPattern);
266+
}
267+
268+
if (console.GetHatchTopPositionPressed()) {
269+
std::cout << "Comp Joy Input: Console: Hatch Top Position Pressed" << std::endl;
270+
m_GrabHatchFromLoadingStation->Start();
271+
m_Bling.SetBling(m_Bling.HatchPattern);
272+
} else if (console.GetHatchMidPositionPressed()) {
273+
std::cout << "Comp Joy Input: Console: Hatch Mid Position Pressed" << std::endl;
274+
m_ReadyHatch->Start();
275+
m_Bling.SetBling(m_Bling.HatchPattern);
276+
} else if (console.GetHatchLowerPressed()) {
277+
std::cout << "Comp Joy Input: Console: Hatch Lower Pressed" << std::endl;
278+
m_LowerHatch
279+
->Until([]() { return Robot::m_OI.GetOperatorConsole().GetHatchLowerReleased(); })
280+
->Start();
255281
m_Bling.SetBling(m_Bling.HatchPattern);
256282
}
257283

src/main/cpp/commands/GrabHatchFromDispenser.cpp

-44
This file was deleted.

src/main/cpp/commands/GrabHatchFromFloor.cpp

-44
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "commands/GrabHatchFromLoadingStation.h"
2+
#include "Robot.h"
3+
4+
#include <iostream>
5+
using std::cout;
6+
using std::endl;
7+
8+
/* GOAL:
9+
*
10+
* Lift hatch mechanism to top position and grip hatch when arm rises above
11+
* threshold.
12+
*
13+
* Command start:
14+
* - Lift hatch to top position with PID.
15+
*
16+
* Command execute:
17+
* - When top position exceeds "grip threshold", then grab hatch. This ensures
18+
* the hatch is fully seated inside gripper.
19+
*
20+
* Command finished when:
21+
* - Arm reaches top position.
22+
*
23+
* Command end:
24+
* - Leave PID enabled to hold position. Arm lifts to grab hatch and weight
25+
* of hatch should not pull arm down.
26+
*
27+
* Command interrupted:
28+
* - Stop rotation. Command was probably interrupted for a reason.
29+
*
30+
* Follow-up options:
31+
* - None.
32+
*/
33+
34+
GrabHatchFromLoadingStation::GrabHatchFromLoadingStation() {
35+
Requires(&Robot::GetHatchMechanism());
36+
}
37+
38+
void GrabHatchFromLoadingStation::Initialize() {
39+
// Start moving to top position.
40+
Robot::GetHatchMechanism().RotateToTopPosition();
41+
// Assume gripper is open.
42+
m_IsHatchGrabbed = false;
43+
}
44+
45+
void GrabHatchFromLoadingStation::Execute() {
46+
HatchMechanism &hatch = Robot::GetHatchMechanism();
47+
48+
if (!m_IsHatchGrabbed && hatch.IsAboveGripThreshold()) {
49+
// Only grab once.
50+
hatch.GrabHatch();
51+
m_IsHatchGrabbed = true;
52+
}
53+
}
54+
55+
bool GrabHatchFromLoadingStation::IsFinished() {
56+
return Robot::GetHatchMechanism().IsArmRotationDone();
57+
}
58+
59+
void GrabHatchFromLoadingStation::End() {}
60+
61+
void GrabHatchFromLoadingStation::Interrupted() {
62+
Robot::GetHatchMechanism().StopRotation();
63+
}

src/main/cpp/commands/LowerHatch.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
3+
/* Open Source Software - may be modified and shared by FRC teams. The code */
4+
/* must be accompanied by the FIRST BSD license file in the root directory of */
5+
/* the project. */
6+
/*----------------------------------------------------------------------------*/
7+
8+
#include "commands/LowerHatch.h"
9+
#include "Robot.h"
10+
11+
#include <iostream>
12+
13+
LowerHatch::LowerHatch() {
14+
Requires(&Robot::GetHatchMechanism());
15+
m_IsFinishedCallback = nullptr;
16+
}
17+
18+
// Called just before this Command runs the first time
19+
void LowerHatch::Initialize() {}
20+
21+
// Called repeatedly when this Command is scheduled to run
22+
void LowerHatch::Execute() {
23+
if (nullptr == m_IsFinishedCallback) {
24+
std::cerr << "LowerHatch command activated without IsFinished callback." << std::endl;
25+
return;
26+
}
27+
28+
HatchMechanism& hatch = Robot::GetHatchMechanism();
29+
30+
hatch.LowerHatch();
31+
hatch.ReleaseHatch();
32+
}
33+
34+
// Make this return true when this Command no longer needs to run execute()
35+
bool LowerHatch::IsFinished() {
36+
return nullptr == m_IsFinishedCallback || m_IsFinishedCallback();
37+
}
38+
39+
// Called once after isFinished returns true
40+
void LowerHatch::End() {
41+
Robot::GetHatchMechanism().StopRotation();
42+
}
43+
44+
// Called when another command which requires one or more of the same
45+
// subsystems is scheduled to run
46+
void LowerHatch::Interrupted() {
47+
Robot::GetHatchMechanism().StopRotation();
48+
}
49+
50+
LowerHatch* LowerHatch::Until(std::function<bool(void)> callback) {
51+
m_IsFinishedCallback = callback;
52+
return this;
53+
}

src/main/cpp/commands/RaiseHatch.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
3+
/* Open Source Software - may be modified and shared by FRC teams. The code */
4+
/* must be accompanied by the FIRST BSD license file in the root directory of */
5+
/* the project. */
6+
/*----------------------------------------------------------------------------*/
7+
8+
#include "commands/RaiseHatch.h"
9+
#include "Robot.h"
10+
11+
#include <iostream>
12+
13+
RaiseHatch::RaiseHatch() {
14+
Requires(&Robot::GetHatchMechanism());
15+
m_IsFinishedCallback = nullptr;
16+
}
17+
18+
// Called just before this Command runs the first time
19+
void RaiseHatch::Initialize() {}
20+
21+
// Called repeatedly when this Command is scheduled to run
22+
void RaiseHatch::Execute() {
23+
if (nullptr == m_IsFinishedCallback) {
24+
std::cerr << "RaiseHatch command activated without IsFinished callback." << std::endl;
25+
return;
26+
}
27+
28+
HatchMechanism& hatch = Robot::GetHatchMechanism();
29+
30+
hatch.RaiseHatch();
31+
hatch.GrabHatch();
32+
}
33+
34+
// Make this return true when this Command no longer needs to run execute()
35+
bool RaiseHatch::IsFinished() {
36+
return nullptr == m_IsFinishedCallback || m_IsFinishedCallback();
37+
}
38+
39+
// Called once after isFinished returns true
40+
void RaiseHatch::End() {
41+
Robot::GetHatchMechanism().StopRotation();
42+
}
43+
44+
// Called when another command which requires one or more of the same
45+
// subsystems is scheduled to run
46+
void RaiseHatch::Interrupted() {
47+
Robot::GetHatchMechanism().StopRotation();
48+
}
49+
50+
RaiseHatch* RaiseHatch::Until(std::function<bool(void)> callback) {
51+
m_IsFinishedCallback = callback;
52+
return this;
53+
}

0 commit comments

Comments
 (0)