-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathturret.py
More file actions
56 lines (42 loc) · 1.91 KB
/
turret.py
File metadata and controls
56 lines (42 loc) · 1.91 KB
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
from rev import SparkMax, SparkLowLevel
from wpimath.controller import PIDController, SimpleMotorFeedforwardRadians
from commands2 import Subsystem, Command, ParallelCommandGroup
from utils import Utils
from camera import AprilTagCamera
from led import LEDController
from typing import Callable
import constants
class Turret(Subsystem):
def __init__(self):
self.yaw = SparkMax(constants.kTurretId, SparkLowLevel.MotorType.kBrushless)
self.encoder = self.yaw.getEncoder()
self.pid = PIDController(0.05, 0, 0)
self.pid.setTolerance(0.1)
# self.target_RPM = 4500
self.feedforward = SimpleMotorFeedforwardRadians(0, 0.002)
def activateYawClockwise(self) -> Command:
return self.run(lambda: self.yaw.set(0.5))
def activateYaw(self, rotate: Callable[[], float]) -> Command:
return self.run(lambda: self.yaw.set(rotate()))
def followYawTag(self, camera: AprilTagCamera, led: LEDController) -> Command:
yaw = camera.getYawFromBestTarget()
rotation = 0
if yaw != -1:
rotation = self.pid.calculate(yaw, 0)
led.blinkGreen().schedule()
else:
led.red().schedule()
return self.run(lambda: self.yaw.set(rotation))
def stopYaw(self) -> Command:
return self.run(lambda: self.yaw.set(0))
def activateYawCounterClockwise(self) -> Command:
return self.run(lambda: self.yaw.set(-0.7))
def clamp(self,value, min_value, max_value):
return max(min(value, max_value), min_value)
def centerTurret(self, alignment):
normalized_alignment = Utils.normalize(alignment, 1080) #chutando que a camera tem 1080
output = self.pid.calculate(alignment, 0)
output = self.clamp(output, -1,1)
self.yaw.setVoltage(output * 12)
def commandCenterTurret(self,alignment) -> Command:
return self.run(lambda: self.centerTurret(alignment))