-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from dojo-sim/jan/input_update
External forcing
- Loading branch information
Showing
10 changed files
with
105 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,59 @@ | ||
# Defining a Controller | ||
|
||
Here, we explain how to write a controller and simulate its effect on a dynamical system, i.e., a [`Mechanism`](@ref). | ||
We focus on a simple pendulum swing-up. The controller is a method that always takes 2 input arguments: | ||
We focus on the stabilization of the cartpole, which has two joints but only a single input on the cart. The controller is a method that always takes 2 input arguments: | ||
- a [`Mechanism`](@ref), | ||
- an integer `t` indicating the current simulation step. | ||
For the pendulum, the controller computes the control inputs based on the current state `x`, the goal state `x_goal` and a PID controller. | ||
- an integer `k` indicating the current simulation step. | ||
For the cartpole, the controller computes the control input based on the current state `x`, the goal state `x_goal` and an LQR controller. The simulation step is not used in this example. | ||
|
||
There are three ways to apply inputs to the system | ||
- set an input directly to a joint | ||
- set a set of inputs to all joints of the mechanism | ||
- set an external force on bodies | ||
|
||
```julia | ||
# ### Setup | ||
using Dojo | ||
using DojoEnvironments | ||
|
||
# ### Mechanism | ||
mechanism = get_mechanism(:pendulum) | ||
mechanism = get_mechanism(:cartpole) | ||
|
||
# ### Controller | ||
summed_error = 0 # for integral part | ||
K = [-0.948838; -2.54837; 48.6627; 10.871] | ||
|
||
function controller!(mechanism, k) | ||
## Target state | ||
x_goal = [π/2; 0.0] | ||
x_goal = [0;0; 0.0;0] | ||
|
||
## Current state | ||
x = get_minimal_state(mechanism) | ||
|
||
error = (x_goal-x) | ||
global summed_error += error[1]*mechanism.timestep | ||
## Control inputs | ||
u = -K' * (x - x_goal) | ||
|
||
## Gains | ||
Kp = 25 | ||
Ki = 50 | ||
Kd = 5 | ||
## 3 ways to set input: | ||
|
||
## Control inputs | ||
u = Kp*error[1] + Ki*summed_error + Kd*error[2] | ||
set_input!(mechanism, [u]) | ||
end | ||
## 1: get joint and set input | ||
cart_joint = get_joint(mechanism, :cart_joint) | ||
set_input!(cart_joint, [u]) | ||
|
||
# ### Simulate | ||
initialize!(mechanism, :pendulum, | ||
angle=0.0, angular_velocity=0.0) | ||
## 2: set input for all joints at once | ||
## set_input!(mechanism, [u;0]) # need to know joint order | ||
|
||
storage = simulate!(mechanism, 5.0, controller!, record=true) | ||
## 3: direct external force on body | ||
## cart = get_body(mechanism, :cart) | ||
## set_external_force!(cart; force=[0;u;0]) | ||
|
||
end | ||
|
||
# ### Simulate | ||
initialize!(mechanism, :cartpole; position=0, orientation=pi/4) | ||
|
||
storage = simulate!(mechanism, 10.0, controller!, record=true) | ||
|
||
|
||
# ### Visualize | ||
vis = visualize(mechanism, storage) | ||
render(vis) | ||
``` | ||
|
||
By changing the parameters, you should be able to perform a swing up. You might need to be carful due to a possible sign-change in the top position. | ||
```@raw html | ||
<img src="../assets/animations/pendulum.gif" width="300"/> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,7 +156,8 @@ export | |
Mesh, | ||
CombinedShapes, | ||
get_body, | ||
get_node | ||
get_node, | ||
set_external_force! | ||
|
||
# Joints | ||
export | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters