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

Added base shooter class #19

Merged
merged 8 commits into from
Jan 15, 2024
51 changes: 51 additions & 0 deletions components/shooter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from magicbot import tunable
from rev import CANSparkMax
from ids import SparkMaxIds, TalonIds
import phoenix6
from phoenix6.configs import (
config_groups,
MotorOutputConfigs,
)
from phoenix6.controls import VoltageOut
import phoenix6.hardware


class Shooter:
flywheel_speed = tunable(0.0)
inject_speed = tunable(0.0)

def __init__(self):
self.top_flywheel = phoenix6.hardware.TalonFX(TalonIds.top_flywheel)
self.bottom_flywheel = phoenix6.hardware.TalonFX(TalonIds.bottom_flywheel)
self.inject_flywheel = CANSparkMax(
SparkMaxIds.inject_flywheel, CANSparkMax.MotorType.kBrushless
)

# Configure bottom flywheel motor
bottom_config = self.bottom_flywheel.configurator
bottom_flywheel_config = MotorOutputConfigs()
bottom_flywheel_config.inverted = config_groups.InvertedValue.CLOCKWISE_POSITIVE

bottom_config.apply(bottom_flywheel_config)

self.inject_flywheel.setInverted(True)

Copy link
Member

Choose a reason for hiding this comment

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

I think it's safe to say we need more feedback methods.
I'm thinking of two feedback methods:

  1. actual_velocity
  2. flywheel error

self.should_inject = False

def shoot(self):
self.should_inject = True

def is_ready(self):
Copy link
Member

Choose a reason for hiding this comment

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

I think we should make these so-called "feedback" functions with feedback decorators from magicbot

e.g.
@feedback
def is_ready(self) -> bool

return True

def execute(self):
"""This gets called at the end of the control loop"""
flywheel_request = VoltageOut(12.0 * self.flywheel_speed)
if self.should_inject:
self.inject_flywheel.set(self.inject_speed)
else:
self.inject_flywheel.set(0.0)

self.top_flywheel.set_control(flywheel_request)
self.bottom_flywheel.set_control(flywheel_request)
self.should_inject = False
8 changes: 8 additions & 0 deletions ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ class TalonIds(enum.IntEnum):
drive_4 = 4
steer_4 = 8

top_flywheel = 9
bottom_flywheel = 10


@enum.unique
class CancoderIds(enum.IntEnum):
swerve_1 = 1
swerve_2 = 2
swerve_3 = 3
swerve_4 = 4


@enum.unique
class SparkMaxIds(enum.IntEnum):
inject_flywheel = 1
6 changes: 6 additions & 0 deletions robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import magicbot

from components.chassis import Chassis
from components.shooter import Shooter
import math

from utilities.scalers import rescale_js
Expand All @@ -15,6 +16,7 @@ class MyRobot(magicbot.MagicRobot):

# Components
chassis: Chassis
shooter: Shooter

max_speed = magicbot.tunable(Chassis.max_wheel_speed * 0.95)

Expand Down Expand Up @@ -78,6 +80,10 @@ def teleopPeriodic(self) -> None:
if self.rumble_timer.hasElapsed(self.rumble_duration):
self.gamepad.setRumble(wpilib.XboxController.RumbleType.kBothRumble, 0)

# injecting
if self.gamepad.getBButton():
self.shooter.shoot()

def testInit(self) -> None:
pass

Expand Down