pinocchio  3.7.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
 
Loading...
Searching...
No Matches
dpendulum.py
1import time
2
3import numpy as np
4from numpy import pi
5from pendulum import Pendulum
6
7p = Pendulum(1)
8
9NQ = 20 # Discretization steps for position
10NV = 19 # Discretization steps for velocity
11VMAX = 5 # Max velocity (v in [-vmax,vmax])
12NU = 9 # Discretization steps for torque
13UMAX = 5 # Max torque (u in [-umax,umax])
14DT = 3e-1
15
16DQ = 2 * pi / NQ
17DV = 2.0 * (VMAX) / NV
18DU = 2.0 * (UMAX) / NU
19
20
21# Continuous to discrete
22def c2dq(q):
23 q = (q + pi) % (2 * pi)
24 return int(round(q / DQ)) % NQ
25
26
27def c2dv(v):
28 v = np.clip(v, -VMAX + 1e-3, VMAX - 1e-3)
29 return int(np.floor((v + VMAX) / DV))
30
31
32def c2du(u):
33 u = np.clip(u, -UMAX + 1e-3, UMAX - 1e-3)
34 return int(np.floor((u + UMAX) / DU))
35
36
37def c2d(qv):
38 """From continuous to discrete."""
39 return c2dq(qv[0]), c2dv(qv[1])
40
41
42# Discrete to continuous
43def d2cq(iq):
44 iq = np.clip(iq, 0, NQ - 1)
45 return iq * DQ - pi
46
47
48def d2cv(iv):
49 iv = np.clip(iv, 0, NV - 1) - (NV - 1) / 2
50 return iv * DV
51
52
53def d2cu(iu):
54 iu = np.clip(iu, 0, NU - 1) - (NU - 1) / 2
55 return iu * DU
56
57
58def d2c(iqv):
59 """From discrete to continuous"""
60 return d2cq(iqv[0]), d2cv(iqv[1])
61
62
63def x2i(x):
64 return x[0] + x[1] * NQ
65
66
67def i2x(i):
68 return [i % NQ, i / NQ]
69
70
71# --- PENDULUM
72
73
75 def __init__(self):
76 self.pendulum = Pendulum(1)
77 self.pendulum.DT = DT
78 self.pendulum.NDT = 5
79
80 @property
81 def nqv(self):
82 return [NQ, NV]
83
84 @property
85 def nx(self):
86 return NQ * NV
87
88 @property
89 def nu(self):
90 return NU
91
92 @property
93 def goal(self):
94 return x2i(c2d([0.0, 0.0]))
95
96 def reset(self, x=None):
97 if x is None:
98 x = [np.random.randint(0, NQ), np.random.randint(0, NV)]
99 else:
100 x = i2x(x)
101 assert len(x) == 2
102 self.x = x
103 return x2i(self.x)
104
105 def step(self, iu):
106 self.x = self.dynamics(self.x, iu)
107 reward = 1 if x2i(self.x) == self.goal else 0
108 return x2i(self.x), reward
109
110 def render(self):
111 q = d2cq(self.x[0])
112 self.pendulum.display(
113 np.array(
114 [
115 q,
116 ]
117 )
118 )
119 time.sleep(self.pendulum.DT)
120
121 def dynamics(self, ix, iu):
122 x = np.array(d2c(ix))
123 u = d2cu(iu)
124
125 self.xc, _ = self.pendulum.dynamics(x, u)
126 return c2d(x.T.tolist()[0])
127
128
129"""
130env = DPendulum()
131
132print env.reset(x2i([14,11]))
133hq = []
134hv = []
135hqc = []
136hvc = []
137u = 0
138for i in range(100):
139 ix,r=env.step(u)
140 q,v = i2x(ix)
141 env.render()
142 if d2cv(v)==0.0: u = MAXU-1 if u==0 else 0
143 hq.append( d2cq(env.x[0]) )
144 hv.append( d2cv(env.x[1]) )
145 hqc.append( env.xc[0,0] )
146 hvc.append( env.xc[1,0] )
147
148"""
149
150"""
151
152
153EPS = 1e-3
154q = 0.0
155v = -VMAX
156hq = []
157hv = []
158hiq = []
159hiv = []
160hqa = []
161hva = []
162
163while q<2*pi:
164 hq.append(q)
165 iq = c2dq(q)
166 hiq.append(iq)
167 hqa.append(d2cq(iq))
168 q += EPS
169while v<VMAX:
170 iv = c2dv(v)
171 hv.append(v)
172 hiv.append(iv)
173 hva.append(d2cv(iv))
174 v += EPS
175
176
177
178
179"""
dynamics(self, ix, iu)
Definition dpendulum.py:121