Skip to content

Commit ab21c05

Browse files
committed
simplifying pendulum controller
1 parent 857e4df commit ab21c05

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed

docs/src/define_controller.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
# Defining a Controller
22
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).
44
We focus on a simple pendulum swing-up.
55

6+
Load Dojo and use the pendulum mechanism with desired simulation time step, desired gravity and desired damping at the joint.
67

7-
8-
9-
# ## Setup
8+
```julia
109
using Dojo
1110

12-
# ## Mechanism
1311
mechanism = get_mechanism(:pendulum,
1412
timestep=0.01,
1513
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+
1822

19-
# ## Controller
23+
```julia
2024
function controller!(mechanism, k)
2125
## Target state
2226
x_goal = [1.0 * π; 0.0]
@@ -27,32 +31,34 @@ function controller!(mechanism, k)
2731
## Gains
2832
K = [5.0 0.5] * 0.1
2933

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)
4437
end
38+
```
4539

46-
# ##Simulate
40+
We initialize the pendulum in the lower position, and we will let the controller perform the swing-up movement.
41+
```julia
4742
initialize!(mechanism, :pendulum,
4843
angle=0.0 * π,
4944
angular_velocity=0.0);
45+
```
5046

47+
We simulate the system for 2 seconds using the `controller!`.
48+
```julia
5149
storage = simulate!(mechanism, 2.0, controller!,
5250
record=true,
5351
verbose=true);
52+
```
5453

55-
# ## Visualize
54+
We visualize the results.
55+
```julia
5656
vis = Visualizer()
57-
render(vis)
57+
open(vis)
5858
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+
```

docs/src/define_simulation.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Defining a Simulation
2-
Here, we explain how to simulate a dynamical system i.e. a [`mechanism`](@ref) forward in time.
2+
Here, we explain how to simulate a dynamical system i.e. a [`Mechanism`](@ref) forward in time.
33
The example that we are trying to replicate the Dzhanibekov effect shown below.
44

55
![dzhanibekov](./assets/dzhanibekov_nasa.gif)
@@ -45,4 +45,6 @@ visualize(mech, storage, vis=vis)
4545

4646
And voila! You should see something like this;
4747

48-
![dzhanibekov](./../../examples/animations/dzhanibekov.gif)
48+
```@raw html
49+
<img src="./../../examples/animations/dzhanibekov.gif" width="300"/>
50+
```

examples/simulation/pendulum.jl

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,9 @@ function controller!(mechanism, k)
2323
## Gains
2424
K = [5.0 0.5] * 0.1
2525

26-
off = 0
27-
for joint in mechanism.joints
28-
nu = input_dimension(joint)
29-
30-
## Get joint configuration + velocity
31-
xi = x[off .+ (1:2nu)]
32-
xi_goal = x_goal[off .+ (1:2nu)]
33-
34-
## Control
35-
ui = -K * (xi - xi_goal)
36-
set_input!(joint, ui)
37-
38-
off += nu
39-
end
26+
# Control inputs
27+
u = -K * (x - x_goal)
28+
set_input!(mechanism, u)
4029
end
4130

4231
# ##Simulate

0 commit comments

Comments
 (0)