Skip to content

Commit f2be338

Browse files
authored
Merge pull request #89 from PhyXTGears-programming/competition/indy-robot-inv
Competition/indy robot inv
2 parents 1572526 + ee90bcc commit f2be338

File tree

10 files changed

+102
-44
lines changed

10 files changed

+102
-44
lines changed

src/main/cpp/Robot.cpp

+5-5
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
}
@@ -228,7 +230,7 @@ void Robot::TestPeriodic() {}
228230

229231
void Robot::CompetitionJoystickInput() {
230232
// DRIVER CONTROLS
231-
if (m_OI.GetDriverJoystick().GetBButton()) {
233+
if (m_OI.GetDriverJoystick().GetBButtonPressed()) {
232234
std::cout << "Comp Joy Input: Driver: B Button Down" << std::endl;
233235
GetDriveTrain().ToggleIdleMode();
234236
}
@@ -337,10 +339,8 @@ void Robot::CompetitionJoystickInput() {
337339
m_ReadyHatch->Start();
338340
m_Bling.SetBling(m_Bling.HatchPattern);
339341
} else if (console.GetHatchLowerPressed()) {
340-
std::cout << "Comp Joy Input: Console: Hatch Lower Pressed" << std::endl;
341-
m_LowerHatch
342-
->Until([]() { return Robot::m_OI.GetOperatorConsole().GetHatchLowerReleased(); })
343-
->Start();
342+
std::cout << "Comp Joy Input: Console: Hatch Bottom Position Pressed" << std::endl;
343+
m_ReadyTakeHatch->Start();
344344
m_Bling.SetBling(m_Bling.HatchPattern);
345345
}
346346

+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

+7-27
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ DriveTrain::DriveTrain(wpi::json &jsonConfig) : frc::Subsystem("DriveTrain") {
3939
Subsystem::AddChild("Left Drive PID", &m_LeftPID);
4040
Subsystem::AddChild("Right Drive PID", &m_RightPID);
4141
#else
42-
m_MaxAcceleration = jsonConfig["drive"]["max-acceleration"];
42+
m_MinAcceleration = jsonConfig["drive"]["min-acceleration"];
4343

4444
m_MaxNormalSpeed = jsonConfig["drive"]["max-normal-speed"];
4545
m_SandstormStepSpeed = jsonConfig["drive"]["sandstorm-step-speed"];
@@ -147,27 +147,7 @@ void DriveTrain::ArcadeDrive(double xSpeed, double zRotation, bool squareInputs)
147147
double currentLeft = m_LeftMotors.Get() / m_maxOutput;
148148
double currentRight = m_RightMotors.Get() / m_maxOutput;
149149

150-
double maxLeftAccel = m_MaxAcceleration, maxRightAccel = m_MaxAcceleration;
151-
152-
/* *
153-
// Descrease the max velocity step size for the slower drive side so
154-
// turning takes effect sooner.
155-
//
156-
// WARNING: Doesn't really work from initial testing. Robot drives more straight.
157-
if (0.01 < std::abs(desiredLeft) - std::abs(desiredRight)) {
158-
// Left speed is faster than right.
159-
maxLeftAccel = m_MaxAcceleration;
160-
maxRightAccel = m_MaxAcceleration * desiredRight / desiredLeft;
161-
} else if (0.01 < std::abs(desiredRight) - std::abs(desiredLeft)) {
162-
// Right speed is faster than left.
163-
maxLeftAccel = m_MaxAcceleration * desiredLeft / desiredRight;
164-
maxRightAccel = m_MaxAcceleration;
165-
} else {
166-
// Both speeds are effectively same.
167-
maxLeftAccel = m_MaxAcceleration;
168-
maxRightAccel = m_MaxAcceleration;
169-
}
170-
/* */
150+
double minLeftAccel = m_MinAcceleration, minRightAccel = m_MinAcceleration;
171151

172152
double timeDelta = m_TimeDelta.Split();
173153

@@ -182,14 +162,14 @@ void DriveTrain::ArcadeDrive(double xSpeed, double zRotation, bool squareInputs)
182162
double allowedLeft = ComputeNextOutput(
183163
currentLeft,
184164
desiredLeft,
185-
maxLeftAccel,
165+
minLeftAccel,
186166
timeDelta
187167
);
188168

189169
double allowedRight = ComputeNextOutput(
190170
currentRight,
191171
desiredRight,
192-
maxRightAccel,
172+
minRightAccel,
193173
timeDelta
194174
);
195175

@@ -318,7 +298,7 @@ void DriveTrain::UseDukesSpeedLimit() {
318298
#endif
319299
}
320300

321-
double DriveTrain::ComputeNextOutput(double iVel, double fVel, double maxAccel, double timeDelta) {
322-
double deltaVel = fVel - iVel;
323-
return iVel + deltaVel / 2.0;
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);
324304
}

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();

src/main/deploy/compbot-config.json

+11-10
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
"rotation": {
2929
"zero-point": 0,
3030
"angles": {
31-
"home": 0.73,
32-
"cargo-ship-shoot": 0.73,
33-
"rocket-1-shoot": 0.41,
34-
"rocket-2-shoot": 0.73,
35-
"cargo-floor-pickup": 0.02,
36-
"hatch-dispenser-pickup": 0.73,
37-
"hatch-floor-pickup": 0.73
31+
"home": 0.65,
32+
"cargo-ship-shoot": 0.65,
33+
"rocket-1-shoot": 0.33,
34+
"rocket-2-shoot": 0.65,
35+
"cargo-floor-pickup": 0.00,
36+
"hatch-dispenser-pickup": 0.65,
37+
"hatch-floor-pickup": 0.65
3838
}
3939
},
4040
"roller-speed": {
@@ -45,7 +45,7 @@
4545
}
4646
},
4747
"drive": {
48-
"max-acceleration": 12.0,
48+
"min-acceleration": 1.0,
4949
"max-normal-speed": 1,
5050
"sandstorm-step-speed": 0.45,
5151
"turn-factor": 1
@@ -60,7 +60,7 @@
6060
"zero-point": 0,
6161
"angles": {
6262
"home": 80,
63-
"arm-ready": 161,
63+
"arm-ready": 163,
6464
"arm-climb": 230
6565
}
6666
}
@@ -75,7 +75,8 @@
7575
"rotation": {
7676
"grip-threshold": 0.95,
7777
"top-position": 0.69,
78-
"mid-position": 1.50
78+
"mid-position": 1.50,
79+
"bottom-position": 2.40
7980
}
8081
}
8182
}

src/main/include/Bling.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Bling {
2222

2323
private:
2424
const double m_PwmMap [SizeValue] = {
25-
[Default] = -0.91, // Rainbow Forest Palette
25+
[Default] = -0.99, // Rainbow Forest Palette
2626
[CargoIntakePattern] = 0.65, // Orange
2727
[CargoShootShipPattern] = -0.39, // Color Waves, Lava Palette
2828
[CargoShootRocketPattern] = -0.57, // Large Fire

src/main/include/Robot.h

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "commands/RaiseHatch.h"
3434
#include "commands/ReadyCreeperArm.h"
3535
#include "commands/ReadyHatch.h"
36+
#include "commands/ReadyTakeHatch.h"
3637
#include "commands/RotateCargoForCargoShip.h"
3738
#include "commands/RotateCargoForLevelOneRocket.h"
3839
#include "commands/ShootCargoForCargoShip.h"
@@ -88,6 +89,7 @@ class Robot : public frc::TimedRobot {
8889

8990
static GrabHatchFromLoadingStation* m_GrabHatchFromLoadingStation;
9091
static ReadyHatch* m_ReadyHatch;
92+
static ReadyTakeHatch* m_ReadyTakeHatch;
9193

9294
// JSON Reader for Config (this should probably be moved later)
9395
static wpi::json m_JsonConfig;
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <frc/commands/Command.h>
4+
5+
class ReadyTakeHatch : public frc::Command {
6+
public:
7+
ReadyTakeHatch();
8+
void Initialize() override;
9+
void Execute() override;
10+
bool IsFinished() override;
11+
void End() override;
12+
void Interrupted() override;
13+
};

src/main/include/subsystems/DriveTrain.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class DriveTrain : public frc::Subsystem, public frc::RobotDriveBase {
6060
MOTORTYPE m_MotorLeft2 {kLeftMotor2, rev::CANSparkMax::MotorType::kBrushless};
6161
MOTORTYPE m_MotorLeft3 {kLeftMotor3, rev::CANSparkMax::MotorType::kBrushless};
6262

63-
IdleMode m_IdleMode {IdleMode::kCoast};
63+
IdleMode m_IdleMode {IdleMode::kBrake};
6464

6565
// Group the motors into their sides and then combine them into the drivetrain
6666
frc::SpeedControllerGroup m_RightMotors {m_MotorRight1, m_MotorRight2, m_MotorRight3};

src/main/include/subsystems/HatchMechanism.h

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class HatchMechanism : public frc::Subsystem {
2323
// Use PID to hold position.
2424
void RotateToTopPosition();
2525
void RotateToMidPosition();
26+
void RotateToBottomPosition();
2627

2728
// Stop moving arm. Zero speed, disable PID.
2829
void StopRotation();
@@ -55,6 +56,7 @@ class HatchMechanism : public frc::Subsystem {
5556
struct Config {
5657
double TopPosition = 0.0;
5758
double MidPosition = 0.0;
59+
double BottomPosition = 0.0;
5860
double GripThreshold = 0.0;
5961
};
6062

0 commit comments

Comments
 (0)