Skip to content

Commit 236cc4a

Browse files
committed
[Tests] Add trivial python unit test
1 parent ae7339a commit 236cc4a

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

src/dynamic_graph/tutorial/simu.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,45 @@
33
import matplotlib.pyplot as pl
44
import numpy as np
55

6-
# define inverted pendulum
7-
a = dgt.InvertedPendulum("IP")
8-
a.setCartMass(1.0)
9-
a.setPendulumMass(1.0)
10-
a.setPendulumLength(1.0)
116

12-
b = dgt.FeedbackController("K")
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)
1313

14-
# plug signals
15-
stateOut = a.signal('state')
16-
forceIn = a.signal('force')
17-
stateIn = b.signal('state')
18-
forceOut = b.signal('force')
14+
b = dgt.FeedbackController("K")
1915

20-
dg.plug(stateOut, stateIn)
21-
dg.plug(forceOut, forceIn)
16+
# plug signals
17+
stateOut = a.signal('state')
18+
forceIn = a.signal('force')
19+
stateIn = b.signal('state')
20+
forceOut = b.signal('force')
2221

23-
timeStep = 0.001
22+
dg.plug(stateOut, stateIn)
23+
dg.plug(forceOut, forceIn)
2424

25-
# Set value of state signal
26-
s = stateOut
27-
f = forceIn
25+
# Set value of state signal
26+
s = stateOut
27+
f = forceIn
2828

29-
s.value = (0.0, 0.1, 0.0, 0.0)
29+
s.value = (0.0, 0.1, 0.0, 0.0)
3030

31-
gain = ((
32-
0.0,
33-
27.0,
34-
0.001,
35-
0.001,
36-
), )
37-
b.setGain(gain, )
31+
gain = ((
32+
0.0,
33+
27.0,
34+
0.001,
35+
0.001,
36+
), )
37+
b.setGain(gain, )
38+
39+
return s, f, a
3840

3941

4042
def play(nbSteps):
43+
s, f, a = build_graph()
44+
timeStep = 0.001
4145
timeSteps = []
4246
values = []
4347
forces = []

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ TARGET_INCLUDE_DIRECTORIES(pendulum PUBLIC dynamic-graph::dynamic-graph)
77
ADD_UNIT_TEST(controller controller.cpp)
88
TARGET_LINK_LIBRARIES(controller ${PROJECT_NAME} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} dynamic-graph::dynamic-graph)
99
TARGET_INCLUDE_DIRECTORIES(controller PUBLIC dynamic-graph::dynamic-graph)
10+
11+
IF(BUILD_PYTHON_INTERFACE)
12+
ADD_PYTHON_UNIT_TEST(simu "tests/simu.py" src)
13+
ENDIF(BUILD_PYTHON_INTERFACE)

tests/simu.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
3+
from dynamic_graph.tutorial.simu import build_graph
4+
5+
6+
class DynamicGraphTutorialTest(unittest.TestCase):
7+
def test_simu(self):
8+
s, f, a = build_graph()
9+
self.assertEqual(a.getCartMass(), 1)
10+
11+
12+
if __name__ == '__main__':
13+
unittest.main()

0 commit comments

Comments
 (0)