1
1
# Defining a Controller
2
2
Here, we explain how to write a controller and simulate its effect on a dynamical system
3
- i.e. a [ ` mechanism ` ] ( @ref ) .
3
+ i.e. a [ ` Mechanism ` ] ( @ref ) .
4
4
We focus on a simple pendulum swing-up.
5
5
6
+ Load Dojo and use the pendulum mechanism with desired simulation time step, desired gravity and desired damping at the joint.
6
7
7
-
8
-
9
- # ## Setup
8
+ ``` julia
10
9
using Dojo
11
10
12
- # ## Mechanism
13
11
mechanism = get_mechanism (:pendulum ,
14
12
timestep= 0.01 ,
15
13
gravity= - 9.81 ,
16
- damper=5.0,
17
- spring=0.0)
14
+ damper= 5.0 )
15
+ ```
16
+
17
+ Define the controller. This is a method that takes 2 input arguments:
18
+ - a [ ` Mechanism ` ] ( @ref ) ,
19
+ - an integer ` k ` indicating the current simulation step.
20
+ The controller computes the control inputs based on the current state ` x ` , the goal state ` x_goal ` and a proportional gain ` K ` .
21
+
18
22
19
- # ## Controller
23
+ ``` julia
20
24
function controller! (mechanism, k)
21
25
# # Target state
22
26
x_goal = [1.0 * π; 0.0 ]
@@ -27,32 +31,34 @@ function controller!(mechanism, k)
27
31
# # Gains
28
32
K = [5.0 0.5 ] * 0.1
29
33
30
- off = 0
31
- for joint in mechanism.joints
32
- nu = input_dimension(joint)
33
-
34
- ## Get joint configuration + velocity
35
- xi = x[off .+ (1:2nu)]
36
- xi_goal = x_goal[off .+ (1:2nu)]
37
-
38
- ## Control
39
- ui = -K * (xi - xi_goal)
40
- set_input!(joint, ui)
41
-
42
- off += nu
43
- end
34
+ # Control inputs
35
+ u = - K * (x - x_goal)
36
+ set_input! (mechanism, u)
44
37
end
38
+ ```
45
39
46
- # ##Simulate
40
+ We initialize the pendulum in the lower position, and we will let the controller perform the swing-up movement.
41
+ ``` julia
47
42
initialize! (mechanism, :pendulum ,
48
43
angle= 0.0 * π,
49
44
angular_velocity= 0.0 );
45
+ ```
50
46
47
+ We simulate the system for 2 seconds using the ` controller! ` .
48
+ ``` julia
51
49
storage = simulate! (mechanism, 2.0 , controller!,
52
50
record= true ,
53
51
verbose= true );
52
+ ```
54
53
55
- # ## Visualize
54
+ We visualize the results.
55
+ ``` julia
56
56
vis = Visualizer ()
57
- render (vis)
57
+ open (vis)
58
58
visualize (mechanism, storage, vis= vis);
59
+ ```
60
+
61
+ You should get something like this,
62
+ ``` @raw html
63
+ <img src="./../../examples/animations/pendulum.gif" width="300"/>
64
+ ```
0 commit comments