-
Notifications
You must be signed in to change notification settings - Fork 0
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
Tail subsystem and commands #3
base: main
Are you sure you want to change the base?
Changes from 5 commits
f29fcef
a69616d
20d5748
03c0beb
03bc419
5ce4902
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.sert2521.bunnybots2024.commands | ||
|
||
|
||
import edu.wpi.first.math.controller.PIDController | ||
import edu.wpi.first.wpilibj2.command.Command | ||
import org.sert2521.bunnybots2024.TunedConstants | ||
import org.sert2521.bunnybots2024.subsystems.TailSubsystem | ||
|
||
|
||
class TailDownCommand : Command() { | ||
private val tailSubsystem = TailSubsystem | ||
private val tailPid = PIDController( | ||
TunedConstants.TAIL_P, | ||
TunedConstants.TAIL_I, | ||
TunedConstants.TAIL_D | ||
) | ||
|
||
init { | ||
addRequirements(tailSubsystem) | ||
} | ||
|
||
override fun initialize() { | ||
tailSubsystem.setInversion(false) | ||
tailPid.setpoint = Math.PI / 2 /* assuming that the only position the tail can start this command | ||
* in is the raised position. */ | ||
tailPid.setTolerance(0.01) | ||
} | ||
|
||
override fun execute() { | ||
val calculate = tailPid.calculate(tailSubsystem.getAngleRads()) | ||
tailSubsystem.setVoltage(calculate) | ||
} | ||
|
||
override fun isFinished(): Boolean { | ||
return tailPid.atSetpoint() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should end this command. If the command ends after it hits the setpoint, then after it hits its setpoint it will go limp (with braking mode but still). I don't believe braking mode is enough to pull totes on its own. You can just make this return false and let the next command to the tail cancel this one through the default behavior of subsystem requirements. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's either that or make the defaultCommand of Tail be a version of this command with a goal that keeps it at its current location |
||
} | ||
|
||
override fun end(interrupted: Boolean) { | ||
tailSubsystem.stopMotor() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.sert2521.bunnybots2024.commands | ||
|
||
|
||
import edu.wpi.first.math.controller.PIDController | ||
import edu.wpi.first.wpilibj2.command.Command | ||
import org.sert2521.bunnybots2024.TunedConstants | ||
import org.sert2521.bunnybots2024.subsystems.TailSubsystem | ||
|
||
|
||
class TailUpCommand : Command() { | ||
private val tailSubsystem = TailSubsystem | ||
private val tailPid = PIDController( | ||
TunedConstants.TAIL_P, | ||
TunedConstants.TAIL_I, | ||
TunedConstants.TAIL_D | ||
) | ||
|
||
init { | ||
addRequirements(tailSubsystem) | ||
} | ||
|
||
override fun initialize() { | ||
tailSubsystem.setInversion(true) | ||
tailPid.setpoint = Math.PI / 2 /* here it is being assumed that the only position the tail can start this command | ||
* in is the lowered position. */ | ||
} | ||
|
||
override fun execute() { | ||
val calculate = tailPid.calculate(tailSubsystem.getAngleRads()) | ||
tailSubsystem.setVoltage(calculate) | ||
} | ||
|
||
override fun isFinished(): Boolean { | ||
return tailPid.atSetpoint() | ||
} | ||
|
||
override fun end(interrupted: Boolean) { | ||
tailSubsystem.stopMotor() | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are using a relative encoder instead of an absolute encoder, we should probably have a function that resets the encoder to a certain value. Since we don't have a feedforward here the literal value isn't needed, so I would just set it to zero. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.sert2521.bunnybots2024.subsystems | ||
|
||
import com.revrobotics.CANSparkBase | ||
import com.revrobotics.CANSparkLowLevel | ||
import com.revrobotics.CANSparkMax | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
import org.sert2521.bunnybots2024.ElectricIDs | ||
|
||
object TailSubsystem : SubsystemBase() { | ||
private val tailMotor = CANSparkMax(ElectricIDs.TAIL_ID, CANSparkLowLevel.MotorType.kBrushless) | ||
private val tailEncoder = tailMotor.encoder | ||
/* | ||
I'm not sure if using a relative encoder is the right move here | ||
because i'm relying on the up and down commands finishing at the | ||
same angles every time. I imagine that can lead to a compounding of slight | ||
errors over time. | ||
*/ | ||
private const val POSITION_CONVERSION_FACTOR = Math.PI/24 | ||
Onkornix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
init { | ||
tailMotor.setSmartCurrentLimit(40) | ||
tailMotor.idleMode = CANSparkBase.IdleMode.kBrake | ||
// setInversion(false) | ||
|
||
tailEncoder.setPositionConversionFactor(POSITION_CONVERSION_FACTOR) | ||
|
||
} | ||
|
||
fun setVoltage(voltage: Double) { | ||
tailMotor.setVoltage(voltage) | ||
} | ||
|
||
fun setInversion(inverted: Boolean) { | ||
tailMotor.inverted = inverted | ||
} | ||
|
||
fun stopMotor() { | ||
tailMotor.stopMotor() | ||
} | ||
|
||
fun getAngleRads(): Double { | ||
return tailEncoder.position | ||
} | ||
|
||
// fun getVelocity(): Double { | ||
// return tailEncoder.velocity | ||
// } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TailUpCommand and TailDownCommand can be one file with the setpoint as an initializing argument