Skip to content

Commit

Permalink
drivetrain day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrusnaficy committed Nov 30, 2023
1 parent fd72ba6 commit 50e4568
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
.vscode
Empty file added command/drivetrain.py
Empty file.
6 changes: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CAN_IDS_DRIVETRAIN = {
'left_1': 0,
'left_2': 1,
'right_1': 2,
'right_2': 3
}
1 change: 0 additions & 1 deletion constants.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

28 changes: 28 additions & 0 deletions subsystem/config/subsystem_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Generic, TypeVar
import commands2

class Subsystem(commands2.SubsystemBase):
"""
Extendable subsystem
"""
...

T = TypeVar("T", bound=Subsystem)


class BasicCommand(commands2.CommandBase):
"""
Extendable basic command
"""
...


class SubsystemCommand(BasicCommand, Generic[T]):
"""
Extendable subsystem command
"""

def __init__(self, subsystem: T):
super().__init__()
self.subsystem = subsystem
self.addRequirements(subsystem)
46 changes: 46 additions & 0 deletions subsystem/drivetrain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import config

from ctre import TalonFX, ControlMode
from subsystem.config.subsystem_base import Subsystem

class Drivetrain(Subsystem):

left_motor_1 = TalonFX(config.CAN_IDS_DRIVETRAIN['left_1'])
right_motor_1 = TalonFX(config.CAN_IDS_DRIVETRAIN['right_1'])
left_motor_2 = TalonFX(config.CAN_IDS_DRIVETRAIN['left_2'])
right_motor_2 = TalonFX(config.CAN_IDS_DRIVETRAIN['right_2'])

def set_raw_output(self, speed: float, is_left: bool) -> None:
if is_left:
self.left_motor_1.set(ControlMode.PercentOutput, speed)
self.left_motor_2.set(ControlMode.PercentOutput, speed)
else:
self.right_motor_1.set(ControlMode.PercentOutput, speed)
self.right_motor_2.set(ControlMode.PercentOutput, speed)

def set_velocity(self, velocity: float, is_left: bool) -> None:
if is_left:
self.left_motor_1.set(ControlMode.Velocity, velocity)
self.left_motor_2.set(ControlMode.Velocity, velocity)
else:
self.right_motor_1.set(ControlMode.Velocity, velocity)
self.right_motor_2.set(ControlMode.Velocity, velocity)


def get_raw_output(self, is_left: bool) -> float:
if is_left:
return self.left_motor_1.getMotorOutputPercent()
else:
return self.right_motor_1.getMotorOutputPercent()

def get_velocity(self, is_left: bool) -> float:
if is_left:
return self.left_motor_1.getSelectedSensorVelocity()
else:
return self.right_motor_1.getSelectedSensorVelocity()

def stop(self) -> None:
self.right_motor_1.set(0)
self.right_motor_2.set(0)
self.left_motor_1.set(0)
self.left_motor_2.set(0)

0 comments on commit 50e4568

Please sign in to comment.