-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphys.py
54 lines (40 loc) · 1.62 KB
/
phys.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
from dataclasses import dataclass
import math
@dataclass
class RotationalInertia:
moment: float
angular_velocity: float
class Clutch:
i_input_torque: float
i_kinetic_maxtorque: float
i_static_maxtorque: float
p_slip_tol: float
c_input_shaft: RotationalInertia
c_output_shaft: RotationalInertia
__state: int
STATE_LOCKED = 0
STATE_UNLOCKED = 1
def __init__(self):
self.__state = Clutch.STATE_UNLOCKED
def __state_transition(self):
should_unlock = abs(self.i_input_torque) > self.i_static_maxtorque
if self.__state == Clutch.STATE_LOCKED:
if should_unlock:
self.__state = Clutch.STATE_UNLOCKED
else: # unlocked
if not should_unlock: # if the input torque is in the rated range
slip = self.c_input_shaft.angular_velocity - self.c_output_shaft.angular_velocity
if slip <= self.p_slip_tol:
self.__state = Clutch.STATE_LOCKED
def tick(self, dt):
self.__state_transition()
if self.__state == Clutch.STATE_LOCKED:
angaccel = self.i_input_torque / (self.c_input_shaft.moment + self.c_output_shaft.moment)
angvel = self.c_input_shaft.angular_velocity + dt * angaccel
self.c_input_shaft.angular_velocity = angvel
self.c_output_shaft.angular_velocity = angvel
else:
slip = self.c_input_shaft.angular_velocity - self.c_output_shaft.angular_velocity
torque_out = math.copysign(self.i_kinetic_maxtorque, slip)
self.c_input_shaft.angular_velocity += dt * ((self.i_input_torque - torque_out) / self.c_input_shaft.moment)
self.c_output_shaft.angular_velocity += dt * (torque_out / self.c_output_shaft.moment)