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 all 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
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 TailRaiseAndLowerCommand(private val setPoint: Double) : Command() {
private val tailSubsystem = TailSubsystem
private val tailPid = PIDController(
TunedConstants.TAIL_P,
TunedConstants.TAIL_I,
TunedConstants.TAIL_D
)

init {
addRequirements(tailSubsystem)
}

override fun initialize() {
tailPid.setpoint = setPoint

if (setPoint > tailSubsystem.getAngleRads()) {
tailSubsystem.setInversion(false)
}
else if (setPoint < tailSubsystem.getAngleRads()) {
tailSubsystem.setInversion(true)
}
}

override fun execute() {
val nextVoltage = tailPid.calculate(tailSubsystem.getAngleRads())
tailSubsystem.setVoltage(nextVoltage)
}

override fun isFinished(): Boolean {
return tailPid.atSetpoint()
}

override fun end(interrupted: Boolean) {
// not stopping motor because TailStayStillCommand() should hold the motor in place
// when the tail is not being raised or lowered.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.sert2521.bunnybots2024.commands

import edu.wpi.first.wpilibj2.command.Command
import org.sert2521.bunnybots2024.subsystems.TailSubsystem

class TailStayStillCommand : Command() {
private val tailSubsystem = TailSubsystem


init {
addRequirements(tailSubsystem)
}

override fun initialize() {
tailSubsystem.setVoltage(0.0)
}

override fun execute() {}

override fun isFinished(): Boolean {
return false
}

override fun end(interrupted: Boolean) {
if (!interrupted) { //
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,54 @@
package org.sert2521.bunnybots2024.subsystems

import com.revrobotics.CANSparkBase
import com.revrobotics.CANSparkLowLevel
import com.revrobotics.CANSparkMax
import edu.wpi.first.wpilibj2.command.Command
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import org.sert2521.bunnybots2024.ElectricIDs
import org.sert2521.bunnybots2024.commands.TailStayStillCommand

object TailSubsystem : SubsystemBase() {
private val tailMotor = CANSparkMax(ElectricIDs.TAIL_ID, CANSparkLowLevel.MotorType.kBrushless)
private val tailEncoder = tailMotor.encoder

init {
tailMotor.setSmartCurrentLimit(40)
tailMotor.idleMode = CANSparkBase.IdleMode.kBrake

tailEncoder.setPositionConversionFactor(Math.PI/24)

// if inversion is false that means the tail was lowered when the motor was powered off
if (!tailEncoder.inverted) {
tailEncoder.setPosition(Math.PI/2)
}

}

override fun setDefaultCommand(defaultCommand: Command?) {
super.setDefaultCommand(TailStayStillCommand())
}

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