Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Modal Models #501

Merged
merged 82 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
ee0a1d3
lf: Added minimal prototype for modes in LF grammar
a-sr May 1, 2021
01a5d1f
diagrams: Added prototype for supporting modal models
a-sr May 4, 2021
7416fbd
experimental: Added some examples using modal model syntax
a-sr May 6, 2021
7edf3c8
experimental: Added missing example model
a-sr May 6, 2021
fd31ab9
modes: Removed priority in transition labels
a-sr Aug 5, 2021
1205e8d
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Aug 6, 2021
42f7b38
Merge remote-tracking branch 'origin/cycle-visualization-bugfix' into…
a-sr Aug 11, 2021
c141660
Integrated modes into instantiation graph structure.
a-sr Aug 12, 2021
980e6d3
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Aug 18, 2021
a4d0a75
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Aug 18, 2021
fcf3519
modes: Added partial and experimental support for modes in C target
a-sr Aug 20, 2021
64d9ddf
modes: Added support for reset of inner statemachines
a-sr Aug 25, 2021
f199b13
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Aug 25, 2021
ac9566d
modes: Added basic support for history transitions
a-sr Aug 30, 2021
0f93e77
Added support for timers in modes
a-sr Sep 10, 2021
3ff0241
Fixed ordering of reactions in modes w.r.t. reactions outside of modes
a-sr Sep 10, 2021
36b43eb
Fixed non-modal definition of reactor_mode_t type
a-sr Sep 11, 2021
ac58cad
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Sep 11, 2021
5acf6b9
Added arrow decorators for history transitions
a-sr Sep 13, 2021
2250143
modes: Restructured mode examples directory
a-sr Sep 15, 2021
7122fe4
modes: Switched to classical history keyword
a-sr Sep 15, 2021
45eec2c
modes: Adjusted mode examples
a-sr Sep 15, 2021
dba8d3e
oomph: Added terminal to target platform
a-sr Sep 15, 2021
bf3a838
diagrams: Added more node ids to stabilize appearance during refresh
a-sr Sep 15, 2021
a67aa08
diagrams: Fixed mislocated edges for reactors in reactors in modes
a-sr Sep 15, 2021
4092c50
modes: Improved naming of mode local variables in generated code
a-sr Sep 15, 2021
3789220
modes: Switched back to continue as history keyword
a-sr Sep 22, 2021
d382814
Added SineAvgMax example without real modes
a-sr Sep 29, 2021
dfa9292
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Sep 29, 2021
c9152ed
Adjusted existing example to new mode syntax keywords
a-sr Sep 29, 2021
069c9bd
modes: Adjusted mode code generation to latest changes
a-sr Oct 6, 2021
103ed96
modes: Improved collapse expand behavior
a-sr Oct 20, 2021
8295f17
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Oct 20, 2021
038ea7c
modes: Adjusted naming of generated fields and functions
a-sr Oct 20, 2021
6f137b4
modes: Updated submodule to point to corresponding branch
a-sr Oct 20, 2021
0692a61
modes: Added support for resetting state variables
a-sr Oct 20, 2021
dafdeda
modes: Adjusted handling of delayed connections for modes
a-sr Nov 4, 2021
e85069c
modes: Added validation rule for initial modes
a-sr Nov 17, 2021
73f4a3a
modes: Restructured some initial mode examples
a-sr Nov 18, 2021
d71c8d3
modes: Added ReflexGame example with modes.
a-sr Nov 18, 2021
24f2c58
modes: Adjusted scoping for mode content
a-sr Dec 7, 2021
3a8116e
modes: Added test models for current features
a-sr Dec 7, 2021
2b26c40
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Dec 8, 2021
3d38a67
modes: Converted ModeInstance from xtend to plain Java
a-sr Dec 8, 2021
fcc94f4
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Dec 10, 2021
62e7337
Added check to prevent exception due to erroneous model.
a-sr Dec 10, 2021
30825eb
modes: Added experimental support for threaded execution for modes
a-sr Dec 15, 2021
dfc0547
modes: Changes target property value of clock-sync
a-sr Dec 16, 2021
ef58c50
modes: Some fixes for tests
a-sr Dec 16, 2021
ead8107
modes: Update of c submodule
a-sr Dec 16, 2021
a8e4316
modes: Improved data structure creation for reset of state variables.
a-sr Dec 20, 2021
f133077
modes: Added explicit cast in test model
a-sr Dec 21, 2021
e7716be
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Feb 3, 2022
338d6f9
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Feb 3, 2022
fb06eaa
modes: Added utility for modes.
a-sr Feb 3, 2022
2d62a20
modes: Post-merge fixes
a-sr Feb 3, 2022
a109faf
modes: Added modal models inspired by behavior trees.
a-sr Feb 4, 2022
1a5bc57
modes: Replaced transition keywords by strings to keep them out of user
a-sr Feb 7, 2022
452f229
modes: Adjusted project template to circumvent initial keyword.
a-sr Feb 7, 2022
278a095
modes: Removed old examples
a-sr Feb 7, 2022
b850353
Merge branch 'modal-models' of [email protected]:icyphy/lingua-franca.gi…
a-sr Feb 7, 2022
882df13
Merge branch 'modal-models' of [email protected]:icyphy/lingua-franca.git
a-sr Feb 7, 2022
23f89de
modes: Added support for having connections and reactions write to the
a-sr Feb 8, 2022
5bad2a9
modes: Added support for breaking cycles by using modes
a-sr Feb 8, 2022
f6971db
modes: Fixed check for special handling of connections in validator
a-sr Feb 8, 2022
89f06bf
modes: Synced with reactor-c
a-sr Feb 8, 2022
fb64629
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Feb 8, 2022
db293e9
modes: Ported mode diagram synthesis from xtend to java
a-sr Feb 11, 2022
21355bd
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Feb 11, 2022
5a2276d
Merge branch 'xtend-to-java-diagrams' into modal-models
a-sr Feb 11, 2022
74cced9
modes: Changed transition modifier syntax.
a-sr Feb 11, 2022
fba4482
Merge remote-tracking branch 'origin/master' into modal-models
a-sr Feb 17, 2022
dd4a827
modes: Fixed collectElements in ASTUtils and generalized toward modes.
a-sr Feb 17, 2022
6c66170
modes: Included ModesUtil in ASTUtils
a-sr Feb 17, 2022
2886b28
modes: Synced with reactor-c branch
a-sr Feb 17, 2022
a886eae
Merge with master and resolve conflicts and update reactor-c
edwardalee Feb 26, 2022
b162d4d
Merged (again) with master
edwardalee Feb 26, 2022
7936294
Specify modal-models branch of reactor-c.
edwardalee Feb 27, 2022
4e638b5
Fixed error with conversion to Java
edwardalee Feb 27, 2022
486e641
Specify modal-models branch of reactor-c (again)
edwardalee Feb 27, 2022
228b0a6
Update experimental/C/src/ModalModels/BehaviorTrees/robohub_example_a…
lhstrh Feb 28, 2022
954a63d
Update experimental/C/src/ModalModels/BehaviorTrees/robohub_example_a…
lhstrh Feb 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions example/C/src/Patterns/lib/SendersAndReceivers.lf
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ reactor SendOnce {
*
* @param offset The starting time.
* @param period The period.
* @param initial The first output.
* @param start The first output.
* @param increment The increment between outputs
*/
reactor SendCount(
offset:time(0),
period:time(1 sec),
initial:int(0),
start:int(0),
increment:int(1)
) {
state count:int(initial);
state count:int(start);
output out:int;
timer t(offset, period);
reaction(t) -> out {=
Expand Down Expand Up @@ -80,7 +80,7 @@ reactor SendOnceAndReceive {
*
* @param offset The time of the first output.
* @param period The period of the outputs.
* @param initial The initial output value.
* @param start The initial output value.
* @param increment The increment between outputs.
*
* @input in The input to report.
Expand All @@ -98,7 +98,7 @@ reactor SendPeriodicallyAndReceive extends SendCount, Receive {
*
* @param offset The time of the first output.
* @param period The period of the outputs.
* @param initial The initial output value.
* @param start The initial output value.
* @param increment The increment between outputs.
*
* @input in The input to report.
Expand All @@ -118,7 +118,7 @@ reactor ReceiveAndSendPeriodically extends Receive, SendCount {
reactor SendPeriodicallyAndReceiveMultiport (
offset:time(0),
period:time(1 sec),
initial:int(0),
start:int(0),
increment:int(1),
width:int(4)
) {
Expand All @@ -127,7 +127,7 @@ reactor SendPeriodicallyAndReceiveMultiport (

timer t(offset, period);

state count:int(initial);
state count:int(start);

reaction(t) -> out {=
SET(out, self->count);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Implements a modal LF version of the hierarchical statemachine
* for the behavior tree in presented in this article:
* https://robohub.org/introduction-to-behavior-trees/
*
* Compared to the simple variant this uses modes more extensively, which
* results in the correct behavior.
* Moreover, modeling the sequence in Nominal as modal enables the potential
* use of a history transition that could allow modeling the continuation
* of the task squecnce at the point where it was left when the battery ran out.
*/
target C {
// logging: debug
}

reactor GenericTask(name:string("")) {
output success:bool
output failure:bool

initial mode Running {
// Just for testing
timer work(0, 250msec)
timer finish(1sec, 1sec)

reaction(work) {=
printf("%s\n", self->name);
=}

reaction(finish) -> success, Succeeded, failure, Failed {=
int r = rand() % 6;
if (r == 0) {
SET(failure, true);
SET_MODE(Failed);
} else {
SET(success, true);
SET_MODE(Succeeded);
}
=}
}

mode Succeeded {}
mode Failed {}
}

reactor NominalBehavior {
input BatteryOK:bool

output success:bool
output failure:bool

initial mode MoveToObj {
MoveToObjTask = new GenericTask(name="MoveToObj")

MoveToObjTask.failure -> failure

reaction(MoveToObjTask.success) -> CloseGrip {=
SET_MODE(CloseGrip);
=}
}

mode CloseGrip {
CloseGripTask = new GenericTask(name="CloseGrip")

CloseGripTask.failure -> failure

reaction(CloseGripTask.success) -> MoveHome {=
SET_MODE(MoveHome);
=}
}

mode MoveHome {
MoveHomeTask = new GenericTask(name="MoveHome")

MoveHomeTask.failure -> failure

reaction(MoveHomeTask.success) -> success {=
SET(success, true);
=}
}
}

reactor Robot {
input BatteryOK:bool

output success:bool
output failure:bool

initial mode Nominal {
NominalBehavior = new NominalBehavior()

NominalBehavior.success -> success
NominalBehavior.failure -> failure

reaction(BatteryOK) -> Charging {=
if (!BatteryOK->value) {
SET_MODE(Charging);
printf("Battery empty\n");
}
=}
}

mode Charging {
GoCharge = new GenericTask(name="GoCharge")

GoCharge.failure -> failure

reaction(BatteryOK, GoCharge.success) -> Nominal {=
// Assumes simultaneous presence
if (BatteryOK->value && GoCharge.success->value) {
SET_MODE(Nominal);
printf("Battery charged\n");
}
=}
}
}

main reactor {
timer Battery(1sec, 1sec)
state battery_state:int(1)

robot = new Robot()

reaction(Battery) -> robot.BatteryOK {=
self->battery_state--;
SET(robot.BatteryOK, self->battery_state > 0);
if (self->battery_state <= 0) {
self->battery_state = 5;
}
=}

reaction(robot.success) {=
printf("Total success\n");
request_stop();
=}

reaction(robot.failure) {=
printf("Utter failure\n");
request_stop();
=}

}
116 changes: 116 additions & 0 deletions experimental/C/src/ModalModels/BehaviorTrees/robohub_example_simple.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Implements a modal LF version of the hierarchical statemachine
* for the behavior tree in presented in this article:
* https://robohub.org/introduction-to-behavior-trees/
*
* It implements the core behavior sequence by chaining up reactors.
* However, this currently does not work correctly with modes because
* there is no support for reacting to entering a mode.
* Hence, when switching to Charging the task is not started and if
* switching back to Nominal the sequence would not restart.
*/
target C;

reactor GenericTask(name:string("")) {
input start:bool
output success:bool
output failure:bool

logical action continue_task

reaction(start, continue_task) -> continue_task, success, failure {=
printf("%s\n", self->name);
int r = rand() % 10;
if (r == 0) {
SET(failure, true);
} else if (r >= 6) {
SET(success, true);
} else {
schedule(continue_task, MSEC(250));
}
=}
}

reactor Robot {
input start:bool
input BatteryOK:bool
output success:bool
output failure:bool

initial mode Nominal {
MoveToObj = new GenericTask(name="MoveToObj")
CloseGrip = new GenericTask(name="CloseGrip")
MoveHome = new GenericTask(name="MoveHome")

start -> MoveToObj.start // No resume after charging
MoveToObj.success -> CloseGrip.start
CloseGrip.success -> MoveHome.start
MoveHome.success -> success

MoveToObj.failure -> failure
CloseGrip.failure -> failure
MoveHome.failure -> failure

// Potential solution for resuming after charging
// reaction(entry) -> MoveToObj.start {=
// // PROBLEM!!
// SET(MoveToObj.start, true);
// =}

reaction(BatteryOK) -> Charging {=
if (!BatteryOK->value) {
SET_MODE(Charging);
printf("Battery empty\n");
}
=}
}

mode Charging {
GoCharge = new GenericTask(name="GoCharge")

GoCharge.failure -> failure

// Potential solution for starting task when mode is entered because no start event is provided
// reaction(entry) -> GoCharge.start {=
// SET(GoCharge.start, true);
// =}

reaction(BatteryOK, GoCharge.success) -> Nominal {=
// Assumes simultaneous presence
if (BatteryOK->value && GoCharge.success->value) {
SET_MODE(Nominal);
printf("Battery charged\n");
}
=}
}
}

main reactor {
timer Battery(1sec, 1sec)
state battery_state:int(1)

robot = new Robot()

reaction(startup) -> robot.start {=
SET(robot.start, true);
=}

reaction(Battery) -> robot.BatteryOK {=
self->battery_state--;
SET(robot.BatteryOK, self->battery_state > 0);
if (self->battery_state <= 0) {
self->battery_state = 5;
}
=}

reaction(robot.success) {=
printf("Total success\n");
request_stop();
=}

reaction(robot.failure) {=
printf("Utter failure\n");
request_stop();
=}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ reactor ModalModel(sample_size(10)) {
state sample({=[None] * sample_size=});
state count(0);

state mode(0); # Only present w/o mode support
state _mode(0); # Only present w/o mode support

// These actions only mimic the Ptolemy structure
logical action processAVG
Expand All @@ -47,7 +47,7 @@ reactor ModalModel(sample_size(10)) {

/** @label Mode AVG: Collect */
reaction(data) -> processAVG {=
if self.mode == 0: # Only present w/o mode support
if self._mode == 0: # Only present w/o mode support
self.sample[self.count] = data.value
self.count += 1
if self.count == self.sample_size:
Expand All @@ -57,19 +57,19 @@ reactor ModalModel(sample_size(10)) {

/** @label Mode AVG: Process and Transition */
reaction(processAVG) -> out {=
if self.mode == 0: # Only present w/o mode support
if self._mode == 0: # Only present w/o mode support
#print("Processing: ", self.sample)
out.set(sum(self.sample) / self.sample_size)
# Transition to MAX
self.mode = 1 # set_mode(MAX)
self._mode = 1 # set_mode(MAX)
=}

// }
// mode MAX {

/** @label Mode MAX: Collect */
reaction(data) -> processMAX {=
if self.mode == 1: # Only present w/o mode support
if self._mode == 1: # Only present w/o mode support
self.sample[self.count] = data.value
self.count += 1
if self.count == self.sample_size:
Expand All @@ -79,11 +79,11 @@ reactor ModalModel(sample_size(10)) {

/** @label Mode MAX: Process and Transition */
reaction(processMAX) -> out {=
if self.mode == 1: # Only present w/o mode support
if self._mode == 1: # Only present w/o mode support
#print("Processing: ", self.sample)
out.set(max(self.sample))
# Transition to MAX
self.mode = 0 # set_mode(AVG)
self._mode = 0 # set_mode(AVG)
=}
// }
}
Expand Down Expand Up @@ -121,7 +121,7 @@ reactor Plotter {
=}
}

main reactor SineAvgMax {
main reactor {
s = new Sinewave(sample_rate = 125 msec, frequency = 0.44)
m = new ModalModel()
p = new Plotter()
Expand Down
Loading