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

Tail subsystem and commands #3

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/org/sert2521/bunnybots2024/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ object PhysicalConstants {

object ElectricIDs{
const val INTAKE_ID = -1
const val TAIL_ID = -1
}

object TunedConstants{

const val TAIL_P = 0.5
const val TAIL_I = 0.0
const val TAIL_D = 0.5
}

Copy link
Contributor

@fruzzmuffin fruzzmuffin Dec 6, 2024

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

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()
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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()
}
}
40 changes: 40 additions & 0 deletions src/main/java/org/sert2521/bunnybots2024/commands/TailUpCommand.kt
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()
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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
// }
}