forked from avidbots/flatland
-
Notifications
You must be signed in to change notification settings - Fork 3
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 avidbots#45 from avidbots/lua-preprocessor-for-yaml
Lua preprocessor for yaml
- Loading branch information
Showing
15 changed files
with
685 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
.. image:: ../_static/flatland_logo2.png | ||
:width: 250px | ||
:align: right | ||
:target: ../_static/flatland_logo2.png | ||
|
||
Yaml Preprocessor | ||
============================== | ||
|
||
Flatland Server has a lua preprocessor for YAML with simple bindings for the environment variables and rosparam. | ||
The intent is to be able to build parametric models that are defined by dimensions and flags found in either environment variables or rosparam, in a similar way to xacro+urdf. Because this is parsed at model load time, any roslaunch rosparam loading will have completed and those parameters will be available. | ||
|
||
body/joint/plugin enabled flag | ||
------------------------------ | ||
Model bodies, plugins and joints now have a new flag `enabled` which can be set to true or false either directly in the yaml, or based on more complex logic from a lua `$eval` string that returns "true" or "false". Disabled bodies, plugins and joints are skipped during yaml loading, and as a result are never instantiated. From Flatland's perspective `enabled: false` causes the affected body/plugin/joint to be deleted. | ||
|
||
bindings for env and param | ||
------------------------------- | ||
|
||
Additional lua function bindings (beyond the normal standard libraries such as string, math, etc.): | ||
|
||
.. code-block:: lua | ||
env(EnvName) -- blank string + warning if not found | ||
env(EnvName, Default) | ||
param(ParamPath) -- blank string + warning if not found | ||
param(ParamPath, Default) | ||
Sample expressions | ||
------------------------------ | ||
|
||
.. code-block:: yaml | ||
foo: $eval "Some arbitrary LUA expression" | ||
bar: | # Multiline string | ||
$eval -- $eval flag required to trigger LUA parsing | ||
if env("FRONT_WHEEL",true) then | ||
return 1.2 | ||
else | ||
return 2.4 | ||
end | ||
Lua expressions can explicitly `return` their value, but if no `return` is given, one will be prepended to the statement. | ||
|
||
env + param examples | ||
----------------------------- | ||
|
||
.. code-block:: yaml | ||
# in: (SOME_ENV not set) | ||
foo: $eval env("SOME_ENV") | ||
# out: | ||
foo: "" | ||
.. code-block:: yaml | ||
# in: (SOME_ENV not set) | ||
foo: $eval env("SOME_ENV", false) | ||
# out: | ||
foo: false | ||
.. code-block:: yaml | ||
# in: (export SOME_ENV=true) | ||
foo: $eval env("SOME_ENV") | ||
# out: | ||
foo: true | ||
.. code-block:: yaml | ||
# in: (rosparam /test/param not set) | ||
foo: $eval param("/test/param", 0)/2.0 | ||
# out: | ||
foo: 0 | ||
.. code-block:: yaml | ||
# in: (rosparam /test/param set to 5.0) | ||
foo: $eval param("/test/param", 0)/2.0 + 1 | ||
# out: | ||
foo: 2.5 | ||
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
106 changes: 106 additions & 0 deletions
106
flatland_server/include/flatland_server/yaml_preprocessor.h
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,106 @@ | ||
/* | ||
* ______ __ __ __ | ||
* /\ _ \ __ /\ \/\ \ /\ \__ | ||
* \ \ \L\ \ __ __ /\_\ \_\ \ \ \____ ___\ \ ,_\ ____ | ||
* \ \ __ \/\ \/\ \\/\ \ /'_` \ \ '__`\ / __`\ \ \/ /',__\ | ||
* \ \ \/\ \ \ \_/ |\ \ \/\ \L\ \ \ \L\ \/\ \L\ \ \ \_/\__, `\ | ||
* \ \_\ \_\ \___/ \ \_\ \___,_\ \_,__/\ \____/\ \__\/\____/ | ||
* \/_/\/_/\/__/ \/_/\/__,_ /\/___/ \/___/ \/__/\/___/ | ||
* @copyright Copyright 2018 Avidbots Corp. | ||
* @name yaml_preprocessor.h | ||
* @brief Yaml preprocessor using Lua | ||
* @author Joseph Duchesne | ||
* | ||
* Software License Agreement (BSD License) | ||
* | ||
* Copyright (c) 2017, Avidbots Corp. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* * Neither the name of the Avidbots Corp. nor the names of its | ||
* contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#ifndef FLATLAND_SERVER_YAML_PREPROCESSOR_H | ||
#define FLATLAND_SERVER_YAML_PREPROCESSOR_H | ||
|
||
#include <flatland_server/exceptions.h> | ||
#include <yaml-cpp/yaml.h> | ||
|
||
#include <lauxlib.h> | ||
#include <lua.h> | ||
#include <lualib.h> | ||
#include <set> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace flatland_server { | ||
|
||
/** | ||
*/ | ||
namespace YamlPreprocessor { | ||
/** | ||
* @brief Preprocess with a given node | ||
* @param[in/out] node A Yaml node to parse | ||
* @return The parsed YAML::Node | ||
*/ | ||
void Parse(YAML::Node &node); | ||
|
||
/** | ||
* @brief Constructor with a given path to a yaml file, throws exception on | ||
* failure | ||
* @param[in] path Path to the yaml file | ||
* @return The parsed YAML::Node | ||
*/ | ||
YAML::Node LoadParse(const std::string &path); | ||
|
||
/** | ||
* @brief Find and run any $eval nodes | ||
* @param[in/out] node A Yaml node to recursively parse | ||
*/ | ||
void ProcessNodes(YAML::Node &node); | ||
|
||
/** | ||
* @brief Find and run any $eval expressions | ||
* @param[in/out] node A Yaml string node to parse | ||
*/ | ||
void ProcessScalarNode(YAML::Node &node); | ||
|
||
/** | ||
* @brief Get an environment variable with an optional default value | ||
* @param[in/out] lua_State The lua state/stack to read/write to/from | ||
*/ | ||
int LuaGetEnv(lua_State *L); | ||
|
||
/** | ||
* @brief Get a rosparam with an optional default value | ||
* @param[in/out] lua_State The lua state/stack to read/write to/from | ||
*/ | ||
int LuaGetParam(lua_State *L); | ||
}; | ||
} | ||
|
||
#endif // FLATLAND_SERVER_YAML_PREPROCESSOR_H |
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
Oops, something went wrong.