-
Notifications
You must be signed in to change notification settings - Fork 16
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 #20 from lf-lang/date23
Furuta pendulum example
- Loading branch information
Showing
16 changed files
with
273 additions
and
157 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pendulum.csv | ||
pendulum.pdf |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* A simulation of a Furuta pendulum with a modal controller | ||
* based on the Ptolemy II model constructed by Johan Eker | ||
* and described in this paper: | ||
* | ||
* J. Liu, J. Eker, J. W. Janneck, and E. A. Lee, | ||
* “Realistic simulations of embedded control systems,” | ||
* IFAC Proceedings Volumes, vol. 35, no. 1, pp. 391–396, 2002. | ||
* | ||
* This program specifies a build script that only code generates | ||
* and compiles the program, as usual, but also executes the | ||
* program and processes its output to generate and open a | ||
* plot. | ||
* You have to have installed gnuplot and have it in your | ||
* PATH for this script to work as expected (and also cmake). | ||
* | ||
* @author Edward A. Lee | ||
* @author Alexander Schulz-Rosengarten | ||
*/ | ||
target C { | ||
timeout: 3 secs, | ||
fast: true, | ||
flags: "-lm", | ||
build: "./build_run_plot.sh FurutaPendulum" | ||
} | ||
import PendulumController from "PendulumController.lf"; | ||
import PendulumSimulation from "PendulumSimulation.lf"; | ||
import Print from "Print.lf"; | ||
|
||
main reactor { | ||
s = new PendulumSimulation(); | ||
c = new PendulumController(); | ||
p = new Print(); | ||
|
||
s.phi, s.d_phi -> c.phi, c.d_phi; | ||
s.theta, s.d_theta -> c.theta, c.d_theta; | ||
c.control -> s.u; | ||
|
||
c.control -> p.control; | ||
c.modeID -> p.modeID; | ||
c.energy -> p.energy; | ||
s.theta -> p.theta; | ||
s.phi -> p.phi; | ||
} |
38 changes: 38 additions & 0 deletions
38
C/src/modal_models/FurutaPendulum/FurutaPendulumDisturbance.lf
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* This variant of the Furuta pendulum example introduces | ||
* a disturbance to bring the pendulum out of balance. | ||
* | ||
* @author Edward A. Lee | ||
* @author Alexander Schulz-Rosengarten | ||
*/ | ||
target C { | ||
timeout: 5 secs, | ||
fast: true, | ||
flags: "-lm", | ||
build: "./build_run_plot.sh FurutaPendulumDisturbance" | ||
} | ||
import PendulumController from "PendulumController.lf"; | ||
import PendulumSimulation from "PendulumSimulation.lf"; | ||
import Print from "Print.lf"; | ||
|
||
main reactor { | ||
s = new PendulumSimulation(); | ||
c = new PendulumController(); | ||
p = new Print(); | ||
|
||
timer disturb(3 sec); | ||
|
||
reaction(disturb) -> s.d {= | ||
lf_set(s.d, 0.5); | ||
=} | ||
|
||
s.phi, s.d_phi -> c.phi, c.d_phi; | ||
s.theta, s.d_theta -> c.theta, c.d_theta; | ||
c.control -> s.u; | ||
|
||
c.control -> p.control; | ||
c.modeID -> p.modeID; | ||
c.energy -> p.energy; | ||
s.theta -> p.theta; | ||
s.phi -> p.phi; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* A utility reactor to print the pendulum state into a csv file. | ||
*/ | ||
target C; | ||
|
||
preamble {= | ||
#include <math.h> | ||
#define PI 3.14159265 | ||
=} | ||
|
||
reactor Print(filename:string("pendulum.csv")) { | ||
input control:double; | ||
input modeID:double; | ||
input energy:double; | ||
input theta:double; | ||
input phi:double; | ||
|
||
state file:FILE*({=NULL=}); | ||
|
||
reaction(startup) {= | ||
self->file = fopen(self->filename, "w"); | ||
if(self->file == NULL) { | ||
lf_print_error_and_exit("Failed to open file: %s", self->filename); | ||
} else { | ||
fprintf(self->file, "Time,Control,Mode,Energy,Theta,Phi\n"); | ||
} | ||
=} | ||
|
||
reaction(control, modeID, energy, theta, phi) {= | ||
double t = lf_time_logical_elapsed() / 1.0e9; | ||
fprintf(self->file, | ||
"%f,%f,%f,%f,%f,%f\n", | ||
t, | ||
control->value, | ||
modeID->value, | ||
energy->value, | ||
fmod(theta->value, PI), | ||
fmod(phi->value, PI) | ||
); | ||
=} | ||
|
||
reaction(shutdown) {= | ||
fclose(self->file); | ||
=} | ||
} |
Oops, something went wrong.