-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
61 lines (47 loc) · 1.92 KB
/
Copy pathstate.py
File metadata and controls
61 lines (47 loc) · 1.92 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
57
58
59
60
61
# Attention default units are SI
import numpy as np
class State:
def __init__(self, position, velocity, quaternion, omega):
# Position [x, y, z] (inertial frame)
self.position = np.array(position)
# Velocity [vx, vy, vz] (inertial frame)
self.velocity = np.array(velocity)
# Quaternion [w, x, y, z] (orientation from body -> inertial)
self.quaternion = np.array(quaternion)
# Angular velocity [p, q, r] (body frame)
self.omega = np.array(omega)
# get_rotation_matrix function
# Converts current quaternion state to 3x3 rotation matrix (body -> inertial)
def get_rotation_matrix(self):
w, x, y, z = self.quaternion
R = np.array([
[1 - 2*(y**2 + z**2), 2*(x*y - z*w), 2*(x*z + y*w)],
[ 2*(x*y + z*w), 1 - 2*(x**2 + z**2), 2*(y*z - x*w)],
[ 2*(x*z - y*w), 2*(y*z + x*w), 1 - 2*(x**2 + y**2)]
])
return R
# integrate function
# Updates the state using Euler integration and ensures quaternion validity
def integrate(self, derivatives, dt):
accel_linear, accel_angular, dq_dt = derivatives
# Update velocity
self.velocity += accel_linear * dt
# Update position
self.position += self.velocity * dt
# Update orientation (quaternion)
self.quaternion += dq_dt * dt
# Normalization
norm = np.linalg.norm(self.quaternion)
if norm > 0:
self.quaternion /= norm
# Update Angular Velocity
self.omega += accel_angular * dt
# copy function
# Create a new State object with independent copies of arrays
def copy(self):
return State(
position=self.position.copy(),
velocity=self.velocity.copy(),
quaternion=self.quaternion.copy(),
omega=self.omega.copy()
)