src/dynamic_graph/tutorial/simu.py
Go to the documentation of this file.
1 import dynamic_graph as dg
2 import dynamic_graph.tutorial as dgt
3 import matplotlib.pyplot as pl
4 import numpy as np
5 
6 
7 def build_graph():
8  # define inverted pendulum
9  a = dgt.InvertedPendulum("IP")
10  a.setCartMass(1.0)
11  a.setPendulumMass(1.0)
12  a.setPendulumLength(1.0)
13 
14  b = dgt.FeedbackController("K")
15 
16  # plug signals
17  stateOut = a.signal("state")
18  forceIn = a.signal("force")
19  stateIn = b.signal("state")
20  forceOut = b.signal("force")
21 
22  dg.plug(stateOut, stateIn)
23  dg.plug(forceOut, forceIn)
24 
25  # Set value of state signal
26  s = stateOut
27  f = forceIn
28 
29  s.value = np.array((0.0, 0.1, 0.0, 0.0))
30 
31  gain = np.array((0.0, 27.0, 0.001, 0.001))
32  b.setGain(
33  gain,
34  )
35 
36  return s, f, a
37 
38 
39 def play(nbSteps):
40  s, f, a = build_graph()
41  timeStep = 0.001
42  timeSteps = []
43  values = []
44  forces = []
45 
46  # Loop over time and compute discretized state values
47  for x in range(nbSteps):
48  t = x * timeStep
49  timeSteps.append(t)
50  values.append(s.value)
51  forces.append(f.value)
52  a.incr(timeStep)
53 
54  # Convert into numpy array
55  x = np.array(timeSteps)
56  y = np.array(values).transpose()
57 
58  fig = pl.figure()
59  ax1 = fig.add_subplot(121)
60  ax2 = fig.add_subplot(122)
61 
62  # plot configuration variables
63  ax1.plot(x, y[0])
64  ax1.plot(x, y[1])
65 
66  # plot velocity variables
67  ax2.plot(x, y[2])
68  ax2.plot(x, y[3])
69  ax2.plot(x, forces)
70 
71  ax1.legend(("x", "theta"))
72  ax2.legend(("dx", "dtheta", "force"))
73 
74  pl.show()
75 
76 
77 if __name__ == "__main__":
78  play(100)
simu.play
def play(nbSteps)
Definition: src/dynamic_graph/tutorial/simu.py:39
simu.build_graph
def build_graph()
Definition: src/dynamic_graph/tutorial/simu.py:7


dynamic-graph-tutorial
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Tue Jun 20 2023 02:35:58