Skip to content

Commit

Permalink
[examples] Added repeat and monitor to language example
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigul committed Jan 10, 2024
1 parent ac5a28c commit c5ac942
Showing 1 changed file with 115 additions and 41 deletions.
156 changes: 115 additions & 41 deletions examples/language.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "a20e75d0",
"id": "ff56f06c",
"metadata": {},
"source": [
"# Plan Language\n",
Expand All @@ -14,8 +14,10 @@
"| ---------- | ---- | ---------- |\n",
"| + | **Sequential** | Executes the designators one after another, if one of the designators raises an exception the execution is aborted and the state FAILED will be returned. |\n",
"| - | **Try In Order** | Executes the designators one after another, if one designator raises an exception the exception is catched and saved but the execution is not interrupted and the other designators are executed. Returns the state SUCCEDED if at least one designator can be executed without exception. |\n",
"| * | **Repeat** | Repeat the previous language expression a number of time. Has to be used with a language expression and an integer. | \n",
"| \\| | **Parallel** | Executes all designators in parallel. For each designator there will be a new thread created and the designator is executed in this thread. If one of the designators raises an exception the returned state will be FAILED. |\n",
"| ^ | **Try All** | Executes all designators in parallel with a designated thread for each designator. Returns the state SUCCEDED if at least one designator can be executed without an exception|\n",
"| >> | **Monitor** | Monitors the execution of the attached langauge expression, will interrupt the execution as soon as a given condition is fulfilled. | \n",
"\n",
"The Sequential expression is the only one which aborts the execution once an error is raised. \n",
"\n",
Expand All @@ -24,7 +26,7 @@
},
{
"cell_type": "markdown",
"id": "ff487b1a",
"id": "991a0452",
"metadata": {},
"source": [
"## Sequential\n",
Expand All @@ -36,10 +38,12 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "2bff4725",
"id": "7a59fc84",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"from pycram.designators.action_designator import *\n",
"from pycram.pose import Pose\n",
"from pycram.enums import Arms\n",
Expand All @@ -52,7 +56,7 @@
},
{
"cell_type": "markdown",
"id": "f41b3ba3",
"id": "1d7d865a",
"metadata": {},
"source": [
"With this simple plan created we can inspect it and render the created tree structure. "
Expand All @@ -61,7 +65,7 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "f739e740",
"id": "4daddac3",
"metadata": {},
"outputs": [
{
Expand All @@ -82,7 +86,7 @@
},
{
"cell_type": "markdown",
"id": "90911029",
"id": "97f49955",
"metadata": {},
"source": [
"As you can see there is the root node which is the language expression and then there are the leafs which are the designators. When executing this plan the Sequential node will try to execute the NavigateAction and if that is finished without any error the ParkArmsAction will be executed. \n",
Expand All @@ -94,9 +98,14 @@
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6c2d3b77",
"metadata": {},
"execution_count": 1,
"id": "f362a62c",
"metadata": {
"ExecuteTime": {
"end_time": "2024-01-10T13:40:14.972773461Z",
"start_time": "2024-01-10T13:40:14.922450288Z"
}
},
"outputs": [],
"source": [
"from pycram.bullet_world import BulletWorld, Object\n",
Expand All @@ -108,16 +117,16 @@
},
{
"cell_type": "markdown",
"id": "dcc048f4",
"id": "bf78a272",
"metadata": {},
"source": [
"If you are finished with this example you can close the world with the cell below."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2dd00bcc",
"execution_count": 10,
"id": "3a949210",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -127,7 +136,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "863d992e",
"id": "72bd6598",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -140,7 +149,7 @@
},
{
"cell_type": "markdown",
"id": "ad0120e7",
"id": "21725713",
"metadata": {},
"source": [
"## Try In Order\n",
Expand All @@ -151,8 +160,8 @@
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ab55c621",
"execution_count": 2,
"id": "c1aa6b9d",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -174,7 +183,7 @@
},
{
"cell_type": "markdown",
"id": "697885fa",
"id": "6d2cae2f",
"metadata": {},
"source": [
"## Parallel \n",
Expand All @@ -194,8 +203,8 @@
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9a989b2e",
"execution_count": 3,
"id": "a22c8296",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -216,7 +225,7 @@
},
{
"cell_type": "markdown",
"id": "f419b125",
"id": "2a032e9c",
"metadata": {},
"source": [
"## Try All \n",
Expand All @@ -227,8 +236,8 @@
},
{
"cell_type": "code",
"execution_count": 7,
"id": "db23d222",
"execution_count": 4,
"id": "2a48a1e8",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -249,7 +258,7 @@
},
{
"cell_type": "markdown",
"id": "8a3011b7",
"id": "d5baaf40",
"metadata": {},
"source": [
"## Combination of Expressions \n",
Expand All @@ -262,8 +271,8 @@
},
{
"cell_type": "code",
"execution_count": 8,
"id": "158d8982",
"execution_count": 5,
"id": "4ca14fcb",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -285,7 +294,7 @@
},
{
"cell_type": "markdown",
"id": "f830db35",
"id": "bc3a62df",
"metadata": {},
"source": [
"## Code Objects\n",
Expand All @@ -296,26 +305,25 @@
},
{
"cell_type": "code",
"execution_count": 9,
"id": "864ac754",
"execution_count": 6,
"id": "0d6e37f0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is from the code object\n",
"--------------------\n",
"Code funtion\n"
"This is from the code object--------------------\n",
"Code funtion\n",
"\n"
]
}
],
"source": [
"from pycram.designators.action_designator import *\n",
"from pycram.pose import Pose\n",
"from pycram.enums import Arms\n",
"from pycram.process_module import simulated_robot\n",
"from pycram.new_language import Code\n",
"from pycram.language import Code\n",
"\n",
"def code_test(param):\n",
" print(\"-\" * 20)\n",
Expand All @@ -333,7 +341,7 @@
},
{
"cell_type": "markdown",
"id": "b4b97fe0",
"id": "42cbb2da",
"metadata": {},
"source": [
"## Exception Handling\n",
Expand All @@ -346,30 +354,28 @@
},
{
"cell_type": "code",
"execution_count": 10,
"id": "44e4df5b",
"execution_count": 7,
"id": "76566ea1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{<pycram.new_language.Parallel object at 0x7f47218c0cd0>: [PlanFailure()]}\n"
"{<pycram.language.Parallel object at 0x7fde3c03dbe0>: [PlanFailure()]}\n"
]
}
],
"source": [
"from pycram.designators.action_designator import *\n",
"from pycram.pose import Pose\n",
"from pycram.enums import Arms\n",
"from pycram.process_module import simulated_robot\n",
"from pycram.new_language import Code\n",
"from pycram.language import Code\n",
"from pycram.plan_failures import PlanFailure\n",
"\n",
"def code_test():\n",
" raise PlanFailure\n",
"\n",
"park = ParkArmsAction([Arms.BOTH])\n",
"navigate = NavigateAction([Pose([1, 1, 0])])\n",
"code_func = Code(code_test)\n",
"\n",
"plan = navigate | code_func\n",
Expand All @@ -379,6 +385,74 @@
" \n",
"print(plan.exceptions)"
]
},
{
"cell_type": "markdown",
"id": "3f12817f",
"metadata": {},
"source": [
"## Repeat \n",
"Repeat simply repeats a language expression a number of times. As all other language expressions Repeat capturtes exceptions that occur during execution and saves them to the dictionary in the root of the plan.\n",
"\n",
"Since Repeat uses the \\* operator you should keep in mind that it will be evaluated before any other operator, so use parentheses to ensure the correct structure of your plan. \n",
"\n",
"You can see an example of how to use Repeat below."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d19ead8a",
"metadata": {},
"outputs": [],
"source": [
"from pycram.designators.action_designator import *\n",
"from pycram.process_module import simulated_robot\n",
"\n",
"move_torso_up = MoveTorsoAction([0.3])\n",
"move_torso_down = MoveTorsoAction([0.])\n",
"\n",
"plan = (move_torso_up + move_torso_down) * 5\n",
"\n",
"with simulated_robot:\n",
" plan.perform()"
]
},
{
"cell_type": "markdown",
"id": "360966c9",
"metadata": {},
"source": [
"## Monitor\n",
"Monitor allows to monitor the execution of a language expression and interrupt it as soon as a given condition is fulfilled. The condition can either be a Callable which returns a boolean or a Fluent.\n",
"When executed the Monitor will create a separate thread which will check if the condition is satisfied with a frequency of 10 Hz. If the condition is satisfied the execution of the language expression will be interrupted.\n",
"\n",
"For the example on how Monitors work we will use the previous example with the robot moving up and down. We will use a Monitor to interrupt the execution after 2 seconds instead of executing the whole plan 5 times."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "84d7e236",
"metadata": {},
"outputs": [],
"source": [
"from pycram.designators.action_designator import *\n",
"from pycram.process_module import simulated_robot\n",
"from pycram.language import Monitor\n",
"\n",
"move_torso_up = MoveTorsoAction([0.3])\n",
"move_torso_down = MoveTorsoAction([0.])\n",
"\n",
"def monitor_func():\n",
" time.sleep(2)\n",
" return True\n",
"\n",
"plan = (move_torso_up + move_torso_down) * 5 >> Monitor(monitor_func)\n",
"\n",
"with simulated_robot:\n",
" plan.perform()"
]
}
],
"metadata": {
Expand Down

0 comments on commit c5ac942

Please sign in to comment.