Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amp trap goto action #73

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
50 changes: 5 additions & 45 deletions src/main/deploy/allegro2024.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"ampprep" : 2,
"trapprep" : 3
},
"threshold" : 0.75,
"pids" : {
"position" : {
"kp" : 0.0035,
Expand Down Expand Up @@ -115,50 +116,9 @@
"ampprep" : 225,
"trapprep" : 135
},
"pids" : {
"position" : {
"kp" : 0.0035,
"ki" : 0.0,
"kd" : 0.004,
"kv" : 0.000312,
"ka" : 0.0,
"kg" : 0.0,
"ks" : 0.0,
"outmax" : 12.0,
"maxv" : 270.0,
"maxa" : 270.0,
"jerk" : 0.0
}
}
},
"wrist" : {
"hw" : {
"motors" : {
"type" : "talon-fx",
"bus" : "",
"canid" : 8,
"inverted" : false,
"neutral-mode" : "brake",
"current-limit" : 60
},
"encoder" : {
"type" : "motor",
"m" : 0.17578125,
"b" : 0,
"posunits" : "deg",
"velunits" : "deg/s",
"accunits" : "deg/s/s"
}
},
"props" : {
"position-important" : "high",
"velocity-important" : "high"
},
"targets" : {
"stow" : 0,
"transfer" : -90,
"trapprep" : 0,
"ampprep" : -30
"no-go-zone" : {
"upper" : 70,
"lower" : 10
},
"pids" : {
"position" : {
Expand Down Expand Up @@ -553,7 +513,7 @@
"current-limit" : 20.0,
"p" : 0.002346041055718470,
"i" : 0.0,
"d" : 0.000001172851562500,
"d" : 0.000001172851562500
},
"drive" : {
"type": "talon-fx",
Expand Down
58 changes: 29 additions & 29 deletions src/main/java/frc/robot/automodes/AllegroTestAutoMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,35 +288,35 @@ public AllegroTestAutoMode(AutoController ctrl) throws Exception {
}
break;

/////////////////////////////////////////////////////////////////////////
//
// Wrists motor tests
//
/////////////////////////////////////////////////////////////////////////
case 80:
if (amptrap != null && amptrap.getWrist() != null) {
addSubActionPair(amptrap.getWrist(),
new MotorEncoderPowerAction(amptrap.getWrist(), getDouble("power"), getDouble("duration")),
true);
}
break;

case 81:
if (amptrap != null && amptrap.getWrist() != null) {
double duration = getDouble("duration");
double[] times = new double[] { duration, duration, duration, duration, duration };
double[] powers = new double[] { 0.1, 0.3, 0.5, 0.7, 0.9 };
addSubActionPair(amptrap.getWrist(),
new MotorPowerSequenceAction(amptrap.getWrist(), times, powers), true);
}
break;

case 82:
if (amptrap != null && amptrap.getWrist() != null) {
addSubActionPair(amptrap.getWrist(),
new MCVelocityAction(amptrap.getWrist(), "pids:velocity", getDouble("velocity"), true), true);
}
break;
// /////////////////////////////////////////////////////////////////////////
// //
// // Wrists motor tests
// //
// /////////////////////////////////////////////////////////////////////////
// case 80:
// if (amptrap != null && amptrap.getWrist() != null) {
// addSubActionPair(amptrap.getWrist(),
// new MotorEncoderPowerAction(amptrap.getWrist(), getDouble("power"), getDouble("duration")),
// true);
// }
// break;

// case 81:
// if (amptrap != null && amptrap.getWrist() != null) {
// double duration = getDouble("duration");
// double[] times = new double[] { duration, duration, duration, duration, duration };
// double[] powers = new double[] { 0.1, 0.3, 0.5, 0.7, 0.9 };
// addSubActionPair(amptrap.getWrist(),
// new MotorPowerSequenceAction(amptrap.getWrist(), times, powers), true);
// }
// break;

// case 82:
// if (amptrap != null && amptrap.getWrist() != null) {
// addSubActionPair(amptrap.getWrist(),
// new MCVelocityAction(amptrap.getWrist(), "pids:velocity", getDouble("velocity"), true), true);
// }
// break;

/////////////////////////////////////////////////////////////////////////
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private enum State {
private AmpTrapSubsystem sub_;
private MCMotionMagicAction eject_elevator_;
private MCMotionMagicAction eject_arm_;
private MCMotionMagicAction eject_wrist_;
//private MCMotionMagicAction eject_wrist_;
private MCVelocityAction eject_manipulator_;
private XeroTimer timer_;
private State state_;
Expand All @@ -36,7 +36,7 @@ public AmpTrapEjectAction(AmpTrapSubsystem sub) throws Exception {
sub_ = sub;
eject_elevator_ = new MCMotionMagicAction(sub_.getElevator(), "pids:position" , "targets:stow" , 0.5 , 1);
eject_arm_ = new MCMotionMagicAction(sub_.getArm(), "pids:position" , "targets:stow" , 0.5 , 1);
eject_wrist_ = new MCMotionMagicAction(sub_.getWrist(), "pids:position" , "targets:stow" , 0.5 , 1);
//eject_wrist_ = new MCMotionMagicAction(sub_.getWrist(), "pids:position" , "targets:stow" , 0.5 , 1);
eject_manipulator_ = new MCVelocityAction(sub_.getManipulator(), "pids:position", "targets:eject", true);
timer_ = new XeroTimer(sub_.getRobot(), "Eject", 1.5);
stow_amp_trap_ = new AmpTrapStowAction(sub_);
Expand All @@ -48,7 +48,7 @@ public void start() throws Exception{

state_ = State.ReadyToEject;
sub_.getElevator().setAction(eject_elevator_, true);
sub_.getWrist().setAction(eject_wrist_, true);
//sub_.getWrist().setAction(eject_wrist_, true);
sub_.getArm().setAction(eject_arm_, true);
}

Expand Down Expand Up @@ -81,7 +81,7 @@ public String toString(int indent) {

// Creating and defining what happens during each state before it can switch
private void stateReadyToEject(){
if (eject_elevator_.isDone() && eject_arm_.isDone() && eject_wrist_.isDone()){
if (eject_elevator_.isDone() && eject_arm_.isDone()){
state_ = State.Eject;
sub_.getManipulator().setAction(eject_manipulator_, true);
timer_.start();
Expand Down
125 changes: 125 additions & 0 deletions src/main/java/frc/robot/subsystems/amp_trap/AmpTrapGotoAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package frc.robot.subsystems.amp_trap;

import org.xero1425.base.actions.Action;
import org.xero1425.base.subsystems.motorsubsystem.MCMotionMagicAction;



public class AmpTrapGotoAction extends Action {
private MCMotionMagicAction elevatorThreshAction;
private MCMotionMagicAction elevatorGotoAction;
private MCMotionMagicAction armAction;
private AmpTrapSubsystem subsystem;
private double elevatorThreshold;
private double armNoGoZoneLower;
private double armNoGoZoneUpper;
private double armTarget;
private double elevatorTarget;
private enum State {
Goto,
ThreshGoto,
ArmGoto,
Thresh
};

private State state;

public AmpTrapGotoAction(AmpTrapSubsystem subsystem, String elevatorTarget, String armTarget) throws Exception{
this(subsystem, subsystem.getSettingsValue(elevatorTarget).getDouble(), subsystem.getSettingsValue(armTarget).getDouble());
}
public AmpTrapGotoAction(AmpTrapSubsystem subsystem, double elevatorTarget, double armTarget) throws Exception{
super(subsystem.getRobot().getMessageLogger());
this.subsystem = subsystem;

// Get the elevator threshold and the no-go zone limits for the arm from the params file
this.elevatorThreshold = subsystem.getElevator().getSettingsValue("threshold").getDouble();
this.armNoGoZoneLower = subsystem.getArm().getSettingsValue("no-go-zone:lower").getDouble();
this.armNoGoZoneUpper = subsystem.getArm().getSettingsValue("no-go-zone:upper").getDouble();

this.armTarget = armTarget;
this.elevatorTarget = elevatorTarget;

this.elevatorThreshAction = new MCMotionMagicAction(subsystem.getElevator(), "ElevatorThresh", elevatorThreshold, 0.01, 0.02);
this.elevatorGotoAction = new MCMotionMagicAction(subsystem.getArm(), "ElevatorThresh", elevatorTarget, 0.01, 0.02);

// Create the MCMotionMagicAction for the arm motor
this.armAction = new MCMotionMagicAction(subsystem.getArm(), "ArmGoto", armTarget, 3, 10);

state = State.Thresh;
}

@Override
public void start() throws Exception{
super.start();
subsystem.getElevator().setAction(elevatorGotoAction);
subsystem.getElevator().setAction(elevatorThreshAction);
subsystem.getArm().setAction(armAction);

// Start the MCMotionMagicActions
// If arm target isn't in no-go-zone
// and the arm target and arm actual aren't on opposite sides
// then they can be run in paralell
if(((armTarget < armNoGoZoneLower || armTarget > armNoGoZoneUpper) && (!(armTarget > armNoGoZoneUpper && subsystem.getArm().getPosition() < armNoGoZoneLower) || !(armTarget < armNoGoZoneLower && subsystem.getArm().getPosition() > armNoGoZoneUpper)))){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The armTarget cannot be in the no go zone, so the first part of this conditional will always be true and can be removed.
Also, I believe the conditions (ignoring the first part) is ...

if !crossesMaxToMin || !crossesMinToMax which is probably not what you want.
given the actions, you probably want this to be && because you want to go directly to the targets if you are not crossing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? if the elevator target is always above the threshold when we move the arm to the no-go-zone, we can have the arm be wherever we want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was to the first q, the second q, you are right.

subsystem.getArm().setAction(armAction);
subsystem.getElevator().setAction(elevatorGotoAction);
state = State.Goto;
}else if (elevatorTarget > elevatorThreshold){
// Otherwise, start the
subsystem.getElevator().setAction(elevatorGotoAction);
state = State.ThreshGoto;
}else{
subsystem.getElevator().setAction(elevatorThreshAction);
state = State.Thresh;
}
}

@Override
public void run() throws Exception{
super.run();
// Update the MCMotionMagicActions
if(state == State.Goto || state == State.ThreshGoto){
elevatorGotoAction.run();
}
if(state == State.Goto || state == State.ArmGoto){
armAction.run();
}
if(state == State.Thresh){
elevatorThreshAction.run();
}

if(state == State.ThreshGoto && subsystem.getElevator().getPosition() > elevatorThreshold){
subsystem.getArm().setAction(armAction);
state = State.Goto;
}
// If the elevator has moved up past the elevator threshold, move the arm to its target position
if (elevatorThreshAction.isDone() && state == State.Thresh){
subsystem.getArm().setAction(armAction);
state = State.ArmGoto;
}

if(armAction.isDone()){
if(elevatorGotoAction.isDone() && state == State.Goto){
setDone();
return;
}
if(state == State.ArmGoto){
subsystem.getElevator().setAction(elevatorGotoAction);
state = State.Goto;
}
}
}

@Override
public void cancel() {
super.cancel();
// Cancel the MCMotionMagicActions
elevatorGotoAction.cancel();
elevatorThreshAction.cancel();
armAction.cancel();
}

@Override
public String toString(int indent) {
return prefix(indent) + "AmpTrapGotoAction";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AmpTrapStowAction extends Action{
private MCMotionMagicAction stow_elevator_;
private MotorEncoderPowerAction stow_roller_;
private MCMotionMagicAction stow_pivot_;
private MCMotionMagicAction stow_wrist_;
//private MCMotionMagicAction stow_wrist_;
private MCMotionMagicAction stow_climber_;


Expand All @@ -26,7 +26,7 @@ public AmpTrapStowAction(AmpTrapSubsystem sub) throws Exception {
stow_elevator_ = new MCMotionMagicAction(sub_.getElevator(), "pids:position" , "targets:stow" , 0.5 , 1);
stow_roller_ = new MotorEncoderPowerAction(sub_.getManipulator(), 0);
stow_pivot_ = new MCMotionMagicAction(sub_.getArm(), "pids:position" , "targets:stow" , 0.5 , 1);
stow_wrist_ = new MCMotionMagicAction(sub_.getWrist(), "pids:position" , "targets:stow" , 0.5 , 1);
//stow_wrist_ = new MCMotionMagicAction(sub_.getWrist(), "pids:position" , "targets:stow" , 0.5 , 1);
stow_climber_ = new MCMotionMagicAction(sub_.getClimber(), "pids:position", "targets:stow", 0.5, 1);
}

Expand All @@ -37,15 +37,15 @@ public void start() throws Exception{
sub_.getElevator().setAction(stow_elevator_, true);
sub_.getManipulator().setAction(stow_roller_, true);
sub_.getArm().setAction(stow_pivot_, true);
sub_.getWrist().setAction(stow_wrist_, true);
//sub_.getWrist().setAction(stow_wrist_, true);
sub_.getClimber().setAction(stow_climber_, true);
}

@Override
public void run() throws Exception {
super.run() ;

if (stow_elevator_.isDone() && stow_pivot_.isDone() && stow_wrist_.isDone() && stow_climber_.isDone()) {
if (stow_elevator_.isDone() && stow_pivot_.isDone() && stow_climber_.isDone()) {
setDone();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class AmpTrapSubsystem extends Subsystem {
// The arm is attached to the top of the elevator, and moves the manipulator and wrist. 0 is fully pointed down, and it is calculated in degrees.
private MotorEncoderSubsystem arm_;

// The wrist is attached to the end of the arm. It has the manipulator attached to it. 0 is fully pointed down (the manipulator wheels are paralel with the elevator), and it is calculated in degrees.
private MotorEncoderSubsystem wrist_;
// // The wrist is attached to the end of the arm. It has the manipulator attached to it. 0 is fully pointed down (the manipulator wheels are paralel with the elevator), and it is calculated in degrees.
// private MotorEncoderSubsystem wrist_;

// The manipulator is the two rolling wheels attached to the wrist. They are used for holding and placing the note. They should use a power action, and it is calculated in revolutions.
private MotorEncoderSubsystem manipulator_;
Expand All @@ -28,8 +28,8 @@ public AmpTrapSubsystem(Subsystem parent) throws Exception {
arm_ = new MotorEncoderSubsystem(this, "arm", true);
addChild(arm_);

wrist_ = new MotorEncoderSubsystem(this, "wrist", true);
addChild(wrist_);
// wrist_ = new MotorEncoderSubsystem(this, "wrist", true);
// addChild(wrist_);

manipulator_ = new MotorEncoderSubsystem(this, "manipulator", false);
addChild(manipulator_);
Expand All @@ -46,9 +46,9 @@ public MotorEncoderSubsystem getArm() {
return arm_;
}

public MotorEncoderSubsystem getWrist() {
return wrist_;
}
// public MotorEncoderSubsystem getWrist() {
// return wrist_;
// }

public MotorEncoderSubsystem getManipulator() {
return manipulator_;
Expand Down
Loading