-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExamplePlot.py
76 lines (64 loc) · 2.29 KB
/
ExamplePlot.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
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import math
import numpy as np
import timeit
# import exposed vscmg envirnment
import VSCMG_ENV
def quaternion_to_euler(x, y, z, w):
t0 = +2.0 * (w * x + y * z)
t1 = +1.0 - 2.0 * (x * x + y * y)
roll = math.atan2(t0, t1)
t2 = +2.0 * (w * y - z * x)
t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
pitch = math.asin(t2)
t3 = +2.0 * (w * z + x * y)
t4 = +1.0 - 2.0 * (y * y + z * z)
yaw = math.atan2(t3, t4)
return [yaw, pitch, roll]
# create VSCMG satellite object
sat=VSCMG_ENV.Satellite()
I=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
sat.setInertia(I,0.01,0.01)
# set satellite states with array of(quaternion, body rates, RW velocities, gimbal angles)
IC=np.array([1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1000.0, 1000.0, 1000.0, 1000.0, 0.0, 0.0, 0.0, 0.0])
sat.setState(IC)
# control action array of (4 x RW acerlations, 4 x gimbal rates)
action=np.array([0.0,0.0,0.0,0.0, 0.01,0.001,0.001,0.001])
t = 0 # time
dt = 0.01 # step size
# take dynamical step of size dt with control action as input
states=sat.step(action,t,0.1)
action=action*0
win = pg.GraphicsWindow()
win.setWindowTitle('VSCMG')
p1 = win.addPlot(title='Quaternion')
p1.setYRange(-1, 1, padding=0)
p1.addLegend()
data1 = np.zeros(shape=(1000,))
data2 = np.zeros(shape=(1000,))
data3 = np.zeros(shape=(1000,))
data4 = np.zeros(shape=(1000,))
curve1 = p1.plot(data1,pen=(0,0,255), name="qe_0")
curve2 = p1.plot(data2,pen=(0,255,0), name="qe_1")
curve3 = p1.plot(data3,pen=(255,255,0), name="qe_2")
curve4 = p1.plot(data4,pen=(255,0,0), name="qe_3")
pg.setConfigOptions(antialias=True)
while True:
#''' Rolling plots
obs = sat.step(action,0,0.1)
data1[:-1] = data1[1:] # shift data in the array one sample left
data2[:-1] = data2[1:] # shift data in the array one sample left
data3[:-1] = data3[1:] # shift data in the array one sample left
data4[:-1] = data4[1:] # shift data in the array one sample left
#[z,y,x]=quaternion_to_euler(obs[0],obs[1],obs[2],obs[3])
data1[-1] = obs[0]
data2[-1] = obs[1]
data3[-1] = obs[2]
data4[-1] = obs[3]
curve1.setData(data1)
curve2.setData(data2)
curve3.setData(data3)
curve4.setData(data4)
pg.QtGui.QApplication.processEvents()