forked from Mechanical-Advantage/RobotCode2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArmFeedforward.py
120 lines (107 loc) · 3.39 KB
/
ArmFeedforward.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
# Copyright (c) 2023 FRC 6328
# http://github.com/Mechanical-Advantage
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file at
# the root directory of this project.
from dataclasses import dataclass
from casadi import *
from DCMotor import DCMotor
@dataclass
class JointConfig:
mass: float
length: float
moi: float
cgRadius: float
motor: DCMotor
class ArmFeedforward:
_g = 9.80665
_shoulder: JointConfig
_elbow: JointConfig
def __init__(self, shoulder: JointConfig, elbow: JointConfig):
self._shoulder = shoulder
self._elbow = elbow
def calculate(self, position, velocity, acceleration):
M = [[0, 0], [0, 0]]
C = [[0, 0], [0, 0]]
Tg = [0, 0]
M[0][0] = (
self._shoulder.mass * (self._shoulder.cgRadius**2.0)
+ self._elbow.mass
* ((self._shoulder.length**2.0) + (self._elbow.cgRadius**2.0))
+ self._shoulder.moi
+ self._elbow.moi
+ 2
* self._elbow.mass
* self._shoulder.length
* self._elbow.cgRadius
* cos(position[1])
)
M[1][0] = (
self._elbow.mass * (self._elbow.cgRadius**2)
+ self._elbow.moi
+ self._elbow.mass
* self._shoulder.length
* self._elbow.cgRadius
* cos(position[1])
)
M[0][1] = (
self._elbow.mass * (self._elbow.cgRadius**2)
+ self._elbow.moi
+ self._elbow.mass
* self._shoulder.length
* self._elbow.cgRadius
* cos(position[1])
)
M[1][1] = self._elbow.mass * (self._elbow.cgRadius**2) + self._elbow.moi
C[0][0] = (
-self._elbow.mass
* self._shoulder.length
* self._elbow.cgRadius
* sin(position[1])
* velocity[1]
)
C[1][0] = (
self._elbow.mass
* self._shoulder.length
* self._elbow.cgRadius
* sin(position[1])
* velocity[0]
)
C[0][1] = (
-self._elbow.mass
* self._shoulder.length
* self._elbow.cgRadius
* sin(position[1])
* (velocity[0] + velocity[1])
)
Tg[0] = (
self._shoulder.mass * self._shoulder.cgRadius
+ self._elbow.mass * self._shoulder.length
) * self._g * cos(
position[0]
) + self._elbow.mass * self._elbow.cgRadius * self._g * cos(
position[0] + position[1]
)
Tg[1] = (
self._elbow.mass
* self._elbow.cgRadius
* self._g
* cos(position[0] + position[1])
)
M_times_acceleration = (
M[0][0] * acceleration[0] + M[0][1] * acceleration[1],
M[1][0] * acceleration[0] + M[1][1] * acceleration[1],
)
C_times_velocity = (
C[0][0] * velocity[0] + C[0][1] * velocity[1],
C[1][0] * velocity[0] + C[1][1] * velocity[1],
)
torque = (
M_times_acceleration[0] + C_times_velocity[0] + Tg[0],
M_times_acceleration[1] + C_times_velocity[1] + Tg[1],
)
return (
self._shoulder.motor.getVoltage(torque[0], velocity[0]),
self._elbow.motor.getVoltage(torque[1], velocity[1]),
)