This repository has been archived by the owner on Feb 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrobot.py
156 lines (132 loc) · 6.75 KB
/
robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
Team 3200 Robot base class
"""
# Module imports:
import wpilib
from wpilib import XboxController
from magicbot import MagicRobot, tunable
# Component imports:
from components.driveTrain import DriveTrain
from components.pneumatics import Pneumatics
from components.buttonManager import ButtonManager, ButtonEvent
from components.breakSensors import Sensors
from components.winch import Winch
from components.shooterMotors import ShooterMotorCreation
from components.shooterLogic import ShooterLogic
from components.loaderLogic import LoaderLogic
from components.elevator import Elevator
from components.scorpionLoader import ScorpionLoader
from components.feederMap import FeederMap
# Other imports:
from robotMap import RobotMap, XboxMap
from utils.componentUtils import testComponentCompatibility
from utils.motorHelper import createMotor
from utils.sensorFactories import gyroFactory, breaksensorFactory
from utils.acturatorFactories import compressorFactory, solenoidFactory
import utils.math
class MyRobot(MagicRobot):
"""
Base robot class of Magic Bot Type
"""
shooter: ShooterLogic
loader: LoaderLogic
feeder: FeederMap
sensors: Sensors
shooterMotors: ShooterMotorCreation
driveTrain: DriveTrain
winch: Winch
buttonManager: ButtonManager
pneumatics: Pneumatics
elevator: Elevator
scorpionLoader: ScorpionLoader
sensitivityExponent = tunable(1.8)
def createObjects(self):
"""
Robot-wide initialization code should go here. Replaces robotInit
"""
self.map = RobotMap()
self.xboxMap = XboxMap(XboxController(1), XboxController(0))
self.instantiateSubsystemGroup("motors", createMotor)
self.instantiateSubsystemGroup("gyros", gyroFactory)
self.instantiateSubsystemGroup("digitalInput", breaksensorFactory)
self.instantiateSubsystemGroup("compressors", compressorFactory)
self.instantiateSubsystemGroup("solenoids", solenoidFactory)
# Check each componet for compatibility
testComponentCompatibility(self, ShooterLogic)
testComponentCompatibility(self, ShooterMotorCreation)
testComponentCompatibility(self, DriveTrain)
testComponentCompatibility(self, Winch)
testComponentCompatibility(self, ButtonManager)
testComponentCompatibility(self, Pneumatics)
testComponentCompatibility(self, Elevator)
testComponentCompatibility(self, ScorpionLoader)
def autonomousInit(self):
"""Run when autonomous is enabled."""
self.shooter.autonomousEnabled()
self.loader.stopLoading()
def teleopInit(self):
# Register button events for doof
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kX, ButtonEvent.kOnPress, self.pneumatics.toggleLoader)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kY, ButtonEvent.kOnPress, self.loader.setAutoLoading)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kB, ButtonEvent.kOnPress, self.loader.setManualLoading)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kA, ButtonEvent.kOnPress, self.shooter.shootBalls)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kA, ButtonEvent.kOnPress, self.loader.stopLoading)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kA, ButtonEvent.kOnRelease, self.shooter.doneShooting)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kA, ButtonEvent.kOnRelease, self.loader.determineNextAction)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kBumperRight, ButtonEvent.kOnPress, self.elevator.setRaise)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kBumperRight, ButtonEvent.kOnRelease, self.elevator.stop)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kBumperLeft, ButtonEvent.kOnPress, self.elevator.setLower)
self.buttonManager.registerButtonEvent(self.xboxMap.mech, XboxController.Button.kBumperLeft, ButtonEvent.kOnRelease, self.elevator.stop)
self.buttonManager.registerButtonEvent(self.xboxMap.drive, XboxController.Button.kBumperLeft, ButtonEvent.kOnPress, self.driveTrain.enableCreeperMode)
self.buttonManager.registerButtonEvent(self.xboxMap.drive, XboxController.Button.kBumperLeft, ButtonEvent.kOnRelease, self.driveTrain.disableCreeperMode)
self.shooter.autonomousDisabled()
def teleopPeriodic(self):
"""
Must include. Called running teleop.
"""
self.xboxMap.controllerInput()
driveLeft = utils.math.expScale(self.xboxMap.getDriveLeft(), self.sensitivityExponent) * self.driveTrain.driveMotorsMultiplier
driveRight = utils.math.expScale(self.xboxMap.getDriveRight(), self.sensitivityExponent) * self.driveTrain.driveMotorsMultiplier
self.driveTrain.setTank(driveLeft, driveRight)
if self.xboxMap.getMechDPad() == 0:
self.winch.setRaise()
else:
self.winch.stop()
self.scorpionLoader.checkController()
def testInit(self):
"""
Function called when testInit is called.
"""
print("testInit was Successful")
def testPeriodic(self):
"""
Called during test mode alot
"""
pass
def instantiateSubsystemGroup(self, groupName, factory):
"""
For each subsystem find all groupNames and call factory.
Each one is saved to groupName_subsystem and subsystem_groupName
"""
config = self.map.configMapper
containerName = "subsystem" + groupName[0].upper() + groupName[1:]
if not hasattr(self, containerName):
setattr(self, containerName, {})
self.subsystemGyros = {}
#note this is a dicontary refernce, so changes to it
#are changes to self.<containerName>
container = getattr(self, containerName)
subsystems = config.getSubsystems()
createdCount = 0
for subsystem in subsystems:
items = {key:factory(descp) for (key, descp) in config.getGroupDict(subsystem, groupName).items()}
if(len(items) == 0):
continue
container[subsystem] = items
createdCount += len(container[subsystem])
groupName_subsystem = "_".join([groupName,subsystem])
self.logger.info("Creating %s", groupName_subsystem)
setattr(self, groupName_subsystem, container[subsystem])
self.logger.info(f"Created {createdCount} items for {groupName} groups with `{factory.__name__}` into `{containerName}")
if __name__ == '__main__':
wpilib.run(MyRobot)