Skip to content

Commit fa90bae

Browse files
AbbeySiegokcroboticsteamdanielbrownmsm
authored
added Manual Arm Command and fixed errors (#39)
let's go! * added Manual Arm Command and fixed errors * added one of the relative encoders for the arm motors, and made ProcessIO actually get called in ArmIO * added logging for part of the arm * fixed joystick stuff for second driver * added relative encoder to Sensors * arm logs (the oldest one should be with additional weight, the others are bare) --------- Co-authored-by: okcroboticsteam <okcroboticsteam@gmail.com> Co-authored-by: danielbrownmsm <daniel.brown@msmrockets.org>
1 parent a288499 commit fa90bae

File tree

17 files changed

+282
-37
lines changed

17 files changed

+282
-37
lines changed
1.54 MB
Binary file not shown.
1.62 MB
Binary file not shown.
924 KB
Binary file not shown.

src/main/cpp/RobotContainer.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ RobotContainer::RobotContainer() {
1010
VOKC_CALL(this->InitHardware(hardware_));
1111

1212
VOKC_CALL(this->InitSwerve());
13+
VOKC_CALL(this->InitArm());
14+
15+
1316

1417
// Initialize the Gamepads
1518
VOKC_CALL(InitGamepads());
@@ -67,6 +70,9 @@ bool RobotContainer::InitActuators(Actuators *actuators_interface) {
6770
actuators_interface->right_front_steer_motor = std::make_unique<rev::CANSparkMax>(RIGHT_FRONT_STEER_MOTOR, BRUSHLESS);
6871
actuators_interface->right_back_steer_motor = std::make_unique<rev::CANSparkMax>(RIGHT_BACK_STEER_MOTOR, BRUSHLESS);
6972

73+
actuators_interface->arm_lift_motor = std::make_unique<rev::CANSparkMax>(ARM_LIFT_MOTOR, BRUSHLESS);
74+
actuators_interface->arm_up_motor = std::make_unique<rev::CANSparkMax>(ARM_UP_MOTOR, BRUSHLESS);
75+
actuators_interface->arm_extend_motor = std::make_unique<rev::CANSparkMax>(ARM_EXTEND_MOTOR, BRUSHLESS);
7076
return true;
7177
}
7278

@@ -123,6 +129,10 @@ bool RobotContainer::InitSensors(const Actuators &actuators,
123129
OKC_CHECK(sensor_interface->right_front_steer_encoder != nullptr);
124130
OKC_CHECK(sensor_interface->right_back_steer_encoder != nullptr);
125131

132+
sensor_interface->arm_lift_encoder = std::make_unique<rev::SparkMaxRelativeEncoder>(actuators.arm_lift_motor->GetEncoder());
133+
134+
OKC_CHECK(sensor_interface->arm_lift_encoder != nullptr);
135+
126136
return true;
127137
}
128138

@@ -144,11 +154,28 @@ bool RobotContainer::InitSwerve() {
144154
return true;
145155
}
146156

157+
bool RobotContainer::InitArm() {
158+
OKC_CALL(SetupArmInterface(hardware_, arm_hw_));
159+
160+
arm_sw_ = std::make_shared<ArmSoftwareInterface>();
161+
162+
arm_io_ = std::make_shared<ArmIO>(arm_hw_.get(), arm_sw_.get());
163+
164+
arm_ = std::make_shared<Arm>(arm_sw_.get());
165+
166+
OKC_CALL(arm_->Init());
167+
168+
return true;
169+
}
170+
147171
bool RobotContainer::InitGamepads() {
148172
// Get joystick IDs from parameters.toml
149173
int gamepad1_id = RobotParams::GetParam("gamepad1_id", 0);
174+
int gamepad2_id = RobotParams::GetParam("gamepad2_id", 1);
150175

151176
gamepad1_ = std::make_shared<frc::Joystick>(gamepad1_id);
177+
gamepad2_ = std::make_shared<frc::Joystick>(gamepad2_id);
178+
152179

153180
// Initialize the joystick buttons
154181
driver_a_button_ =
@@ -170,5 +197,8 @@ bool RobotContainer::InitCommands() {
170197

171198
swerve_teleop_command_ = std::make_shared<TeleOpSwerveCommand>(swerve_drive_, gamepad1_);
172199

200+
manual_arm_command_ =std::make_shared<ManualArmCommand>(arm_, gamepad2_);
201+
arm_->SetDefaultCommand(*manual_arm_command_);
202+
173203
return true;
174204
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "commands/arm/ManualArmCommand.h"
2+
3+
ManualArmCommand::ManualArmCommand(std::shared_ptr<Arm> arm, std::shared_ptr<frc::Joystick> gamepad) {
4+
arm_ = arm;
5+
gamepad_ = gamepad;
6+
7+
if (arm_ != nullptr) {
8+
this->AddRequirements(arm_.get());
9+
}
10+
}
11+
12+
void ManualArmCommand::Initialize() {
13+
VOKC_CHECK(arm_ != nullptr);
14+
15+
arm_->SetControlMode(Manual);
16+
}
17+
18+
void ManualArmCommand::Execute() {
19+
VOKC_CHECK(arm_ != nullptr);
20+
VOKC_CHECK(gamepad_ != nullptr);
21+
22+
23+
double lift_power_ = this->gamepad_->GetRawAxis(1);
24+
double up_power_ = this->gamepad_->GetRawAxis(0);
25+
double extend_power_ = this->gamepad_->GetRawAxis(2);
26+
27+
VOKC_CALL(arm_->SetManualLiftPower(lift_power_));
28+
VOKC_CALL(arm_->SetManualUpPower(-lift_power_));
29+
// VOKC_CALL(arm_->SetManualExtendPower(extend_power_));
30+
31+
32+
}
33+
34+
void ManualArmCommand::End(bool interrupted) {
35+
// do nothing lol
36+
}
37+
38+
bool ManualArmCommand::IsFinished() {
39+
// this command should never end, unless it is interrupted by another
40+
return false;
41+
}

src/main/cpp/hardware/Hardware.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,30 @@ bool SetupSwerveDriveInterface(
6262
interface = std::make_shared<SwerveDriveHardwareInterface>(swerve_drive_interface);
6363

6464
return true;
65+
}
66+
bool SetupArmInterface(std::unique_ptr<Hardware> &hardware,
67+
std::shared_ptr<ArmHardwareInterface> &interface){
68+
OKC_CHECK(hardware->actuators != nullptr);
69+
70+
71+
//Get actuators interface for arm
72+
std::unique_ptr<Actuators> &actuators = hardware->actuators;
73+
std::unique_ptr<Sensors> &sensors = hardware->sensors;
74+
75+
//make sure the actuators actually exist
76+
OKC_CHECK(actuators->arm_lift_motor != nullptr);
77+
OKC_CHECK(actuators->arm_up_motor != nullptr);
78+
OKC_CHECK(actuators->arm_extend_motor != nullptr);
79+
80+
ArmHardwareInterface arm_interface = {actuators->arm_lift_motor.get(),
81+
actuators->arm_up_motor.get(),
82+
actuators->arm_extend_motor.get(),
83+
sensors->arm_lift_encoder.get()
84+
};
85+
interface = std::make_shared<ArmHardwareInterface>(arm_interface);
86+
87+
return true;
88+
89+
90+
6591
}

src/main/cpp/io/ArmIO.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
void ArmIO::Periodic() {
55
// Process all the inputs and outputs to/from high level software.
6+
67
VOKC_CALL(ProcessIO());
78
}
89

@@ -31,23 +32,23 @@ bool ArmIO::ProcessIO() {
3132
sw_interface_->reset_encoders = false;
3233
}
3334

34-
// if the encoder should be set to a specific value
35-
if (sw_interface_->set_encoder_to_val) {
36-
OKC_CALL(SetEncoder(sw_interface_->encoder_val_to_set));
37-
38-
sw_interface_->set_encoder_to_val = false;
39-
}
35+
4036

4137
// Get the hardware sensor values.
4238
// limit switches
43-
sw_interface_->deployed_limit_switch_val =
44-
hw_interface_->deploy_limit_switch->Get(); //???
45-
sw_interface_->retracted_limit_switch_val =
46-
hw_interface_->retracted_limit_switch->Get(); //???
39+
// sw_interface_->deployed_limit_switch_val =
40+
// hw_interface_->deploy_limit_switch->Get(); //???
41+
// sw_interface_->retracted_limit_switch_val =
42+
// hw_interface_->retracted_limit_switch->Get(); //???
4743

4844
// intake position encoder
49-
sw_interface_->arm_position_encoder_val =
50-
hw_interface_->arm_position_motor->GetEncoder().GetPosition();
45+
OKC_CHECK(hw_interface_->arm_extend_motor != nullptr);
46+
OKC_CHECK(hw_interface_->arm_up_motor != nullptr);
47+
hw_interface_->arm_lift_motor->Set(sw_interface_->arm_lift_power);
48+
hw_interface_->arm_up_motor->Set(sw_interface_->arm_up_power);
49+
hw_interface_->arm_extend_motor->Set(sw_interface_->arm_extend_power);
50+
51+
sw_interface_->arm_lift_encoder_val = hw_interface_->arm_lift_encoder->GetPosition();
5152

5253
return true;
5354
}
@@ -64,7 +65,8 @@ bool ArmIO::UpdateArmConfig(ArmConfig &config) {
6465
// Apply the configuration
6566
// Open Loop Ramp Rate
6667
hw_interface_->arm_lift_motor->SetOpenLoopRampRate(open_loop_ramp);
67-
hw_interface_->indexer_motor->SetOpenLoopRampRate(open_loop_ramp);
68+
hw_interface_->arm_up_motor->SetOpenLoopRampRate(open_loop_ramp);
69+
hw_interface_->arm_extend_motor->SetOpenLoopRampRate(open_loop_ramp);
6870

6971
// Java code has this commented out as well, I'm assuming because it was
7072
// meesing something up. well, the intake works (probably, been a while
@@ -73,15 +75,19 @@ bool ArmIO::UpdateArmConfig(ArmConfig &config) {
7375
// hw_interface_->intake_position_motor->SetOpenLoopRampRate(open_loop_ramp);
7476

7577
// current limiting, so the neo 550 on the indexer doesn't stall and smoke
76-
hw_interface_->indexer_motor->SetSmartCurrentLimit(max_indexer_current);
78+
hw_interface_->arm_lift_motor->SetSmartCurrentLimit(max_indexer_current);
79+
hw_interface_->arm_up_motor->SetSmartCurrentLimit(max_indexer_current);
80+
hw_interface_->arm_extend_motor->SetSmartCurrentLimit(max_indexer_current);
7781

7882
return true;
7983
}
80-
84+
//HACK: this is a hack
8185
bool ArmIO::ResetEncoders() {
8286
OKC_CHECK(hw_interface_ != nullptr);
8387

84-
hw_interface_->arm_position_motor->GetEncoder().SetPosition(0.0);
88+
hw_interface_->arm_lift_motor->GetEncoder().SetPosition(0.0);
89+
hw_interface_->arm_up_motor->GetEncoder().SetPosition(0.0);
90+
hw_interface_->arm_extend_motor->GetEncoder().SetPosition(0.0);
8591

8692
// we currently don't use the encoder for these other two motors, so we
8793
// don't need to reset them yet. yet.
@@ -91,10 +97,3 @@ bool ArmIO::ResetEncoders() {
9197
return true;
9298
}
9399

94-
bool ArmIO::SetEncoder(double &val) {
95-
OKC_CHECK(hw_interface_ != nullptr);
96-
97-
hw_interface_->arm_position_motor->GetEncoder().SetPosition(val);
98-
99-
return true;
100-
}

src/main/cpp/subsystems/Arm.cpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ bool Arm::Init() {
44
arm_pid_ = std::make_shared<frc::PIDController>(0, 0, 0);
55

66
inches_pid_ = std::make_shared<frc::PIDController>(0, 0, 0);
7+
8+
arm_lift_output_log_ = wpi::log::DoubleLogEntry(TeamOKC::log, "/arm/lift_output");
9+
arm_lift_enc_log_ = wpi::log::DoubleLogEntry(TeamOKC::log, "/arm/lift_enc");
10+
711
return true;
812
}
13+
bool Arm::SetControlMode(const ArmMode &mode){
14+
mode_= mode;
915

16+
return true;
17+
}
1018
bool Arm::SetDegrees(double degrees) {
1119
this->arm_pid_->SetSetpoint(degrees);
1220

@@ -19,11 +27,54 @@ bool Arm::SetExtend(double inches) {
1927
return true;
2028
}
2129

30+
bool Arm::SetManualLiftPower(double power)
31+
{
32+
lift_power_ = power;
33+
34+
return true;
35+
}
36+
37+
bool Arm::SetManualExtendPower(double power)
38+
{
39+
extend_power_ = power;
40+
41+
return true;
42+
}
43+
44+
bool Arm::ManualControl() {
45+
OKC_CHECK(interface_ != nullptr);
46+
47+
interface_->arm_lift_power = lift_power_;
48+
interface_->arm_up_power = up_power_;
49+
interface_->arm_extend_power = extend_power_;
50+
51+
arm_lift_output_log_.Append(interface_->arm_lift_power);
52+
arm_lift_enc_log_.Append(interface_->arm_lift_encoder_val);
53+
54+
55+
return true;
56+
}
57+
bool Arm::SetManualUpPower(double power)
58+
{
59+
up_power_ = power;
60+
61+
return true;
62+
63+
}
64+
2265
void Arm::Periodic() {
23-
this->interface_->arm_lift_power =
24-
this->arm_pid_->Calculate(this->interface_->arm_encoder);
25-
this->interface_->arm_extend_power =
26-
this->inches_pid_->Calculate(this->interface_->arm_extend_encoder);
66+
67+
switch (mode_) {
68+
case Manual:
69+
VOKC_CALL(this->ManualControl());
70+
break;
71+
case Auto:
72+
//this->interface_->arm_lift_power = this->arm_pid_->Caluculate(this->interface_->arm_encoder);
73+
//this->interface_->arm_up_power = this->arm_pid_->Caluculate(this->interface_->arm_extend_encoder);
74+
break;
75+
default:
76+
VOKC_CHECK_MSG(false, "Unhandled enum")
77+
}
2778
}
2879

2980
// TODO: Understand this example code and implement it correctly

src/main/deploy/parameters.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ competition = false
44

55
[ui]
66
gamepad1_id = 0
7-
gamepad2_id = 2
7+
gamepad2_id = 1
88

99
[robot]
1010
neo_550_ticks_per_rev = 42

src/main/include/Robot.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
#include <frc/TimedRobot.h>
88
#include <frc2/command/Command.h>
99

10+
1011
#include "RobotContainer.h"
1112

13+
14+
//Arm hardware
15+
16+
17+
18+
19+
20+
1221
class Robot : public frc::TimedRobot {
1322
public:
1423
void RobotInit() override;

0 commit comments

Comments
 (0)