Unverified Commit 73ec4a43 authored by Guilhem Saurel's avatar Guilhem Saurel Committed by GitHub
Browse files

Merge pull request #10 from nim65s/devel

format
parents e60289d4 8b1594ef
Pipeline #18176 failed
# format (Guilhem Saurel, 2022-04-05)
4f6fe74a60b3f740a598fe0059db2c1c7f1fe174
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v13.0.1
hooks:
- id: clang-format
args: [-i, --style=Google]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
- id: check-symlinks
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: destroyed-symlinks
- id: detect-private-key
- id: end-of-file-fixer
- id: fix-byte-order-marker
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
......@@ -56,8 +56,8 @@ SET(${PROJECT_NAME}_HEADERS
)
SET(${PROJECT_NAME}_SOURCES
src/inverted-pendulum
src/feedback-controller
src/inverted-pendulum.cpp
src/feedback-controller.cpp
)
ADD_LIBRARY(${PROJECT_NAME} SHARED
......
......@@ -2,8 +2,9 @@
[![License](https://img.shields.io/badge/License-BSD%202--Clause-orange.svg)](https://opensource.org/licenses/BSD-2-Clause)
[![Pipeline status](https://gitlab.laas.fr/stack-of-tasks/dynamic-graph-tutorial/badges/master/pipeline.svg)](https://gitlab.laas.fr/stack-of-tasks/dynamic-graph-tutorial/commits/master)
[![Coverage report](https://gitlab.laas.fr/stack-of-tasks/dynamic-graph-tutorial/badges/master/coverage.svg?job=doc-coverage)](http://projects.laas.fr/gepetto/doc/stack-of-tasks/dynamic-graph-tutorial/master/coverage/)
[![Coverage report](https://gitlab.laas.fr/stack-of-tasks/dynamic-graph-tutorial/badges/master/coverage.svg?job=doc-coverage)](https://gepettoweb.laas.fr/doc/stack-of-tasks/dynamic-graph-tutorial/master/coverage/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/stack-of-tasks/dynamic-graph-tutorial/master.svg)](https://results.pre-commit.ci/latest/github/stack-of-tasks/dynamic-graph-tutorial)
See INSTALL for installation instruction.
......
Subproject commit 7e797cbcae0947acce802c0bc63765a2b7c75828
Subproject commit 3d6176d439963702d97b82555e3007de05a4e6a4
......@@ -8,4 +8,3 @@
</center>
</body>
</head>
......@@ -4,4 +4,3 @@
<LINK HREF="package.css" REL="stylesheet" TYPE="text/css">
</HEAD>
<BODY>
......@@ -240,4 +240,3 @@ DIV.fragment
text-decoration: none;
color:#0066CC
}
......@@ -11,7 +11,8 @@ As an example, we review below class dynamicgraph::tutorial::InvertedPendulum.
\section dg_tutorial_inverted_pendulum_cxx_interface Interface
The interface is defined in file <c>include/dynamic-graph/tutorial/inverted-pendulum.hh</c>.
The interface is defined in file
<c>include/dynamic-graph/tutorial/inverted-pendulum.hh</c>.
First, we include
\li the header defining Entity class and
......@@ -20,8 +21,8 @@ First, we include
\code
#include <dynamic-graph/entity.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/signal-ptr.h>
\endcode
Then in namespace <c>dynamicgraph::tutorial</c> we define class InvertedPendulum
......@@ -108,14 +109,16 @@ First, we include headers defining
\subsection dg_tutorial_inverted_pendulum_cxx_implementation_headers Headers
\code
#include <dynamic-graph/factory.h>
#include <dynamic-graph/command-setter.h>
#include <dynamic-graph/command-getter.h>
#include "dynamic-graph/tutorial/inverted-pendulum.hh"
#include <dynamic-graph/command-setter.h>
#include <dynamic-graph/factory.h>
#include "command-increment.hh"
#include "dynamic-graph/tutorial/inverted-pendulum.hh"
\endcode
\subsection dg_tutorial_inverted_pendulum_cxx_implementation_entity_registration Entity registration
\subsection dg_tutorial_inverted_pendulum_cxx_implementation_entity_registration
Entity registration
The second step consists in
\li registering our new class into the entity factory and
......@@ -130,7 +133,8 @@ using a macro defined in <c>dynamic-graph/factory.h</c>:
\li the name of the type of the corresponding python class. It is highly
recommended to use the same name for both.
\subsection dg_tutorial_inverted_pendulum_cxx_implementation_constructor Constructor
\subsection dg_tutorial_inverted_pendulum_cxx_implementation_constructor
Constructor
Then we define the class constructor
\li passing the instance name to Entity class constructor,
......@@ -160,10 +164,9 @@ We set input and output signal as constant with a given value
\endcode
The following lines of code define and register commands into the entity.
A \ref dynamicgraph::command::Command "command" is created by calling a constructor with
\li a string: the name of the command,
\li a pointer to a newly created command and
\li a string documenting the command:
A \ref dynamicgraph::command::Command "command" is created by calling a
constructor with \li a string: the name of the command, \li a pointer to a newly
created command and \li a string documenting the command:
\code
std::string docstring;
......@@ -180,10 +183,12 @@ A \ref dynamicgraph::command::Command "command" is created by calling a construc
\endcode
In this example, command::Increment is a command specific to our class
InvertedPendulum. This new command is explained in page \ref dg_tutorial_inverted_pendulum_command.
InvertedPendulum. This new command is explained in page \ref
dg_tutorial_inverted_pendulum_command.
Setter and getter commands are available through classes templated by the type of entity using the command and the type
of the parameter. Be aware that only a prespecified set of types are supported for commands, see class
Setter and getter commands are available through classes templated by the type
of entity using the command and the type of the parameter. Be aware that only a
prespecified set of types are supported for commands, see class
dynamicgraph::command::Value. \code docstring =
"\n"
" Set cart mass\n"
......@@ -203,18 +208,22 @@ dynamicgraph::command::Value. \code docstring =
\note
It is important to notice that
\li commands passed to method Entity::addCommand will be destroyed automatically by Entity class destructor. The user
should therefore not destroy them, \li commands should be defined and registered in the class constructor. Commands
\li commands passed to method Entity::addCommand will be destroyed automatically
by Entity class destructor. The user should therefore not destroy them, \li
commands should be defined and registered in the class constructor. Commands
defined later on will not be reachable by python bindings.
\subsection dg_tutorial_inverted_pendulum_cxx_implementation_newtypes Registering new types: advanced feature
\subsection dg_tutorial_inverted_pendulum_cxx_implementation_newtypes
Registering new types: advanced feature
Signals are templated by the type of data they convey. In this example, we hae defined our own class of vectors
InvertedPendulum::Vector. In order to be able to create signals with this type, we need to register the new type: \code
Signals are templated by the type of data they convey. In this example, we hae
defined our own class of vectors InvertedPendulum::Vector. In order to be able
to create signals with this type, we need to register the new type: \code
dynamicgraph::DefaultCastRegisterer<InvertedPendulum::Vector> IPVectorCast;
\endcode
\note
The new type should implement operator<< and operator>> in order to store variables in streams.
The new type should implement operator<< and operator>> in order to store
variables in streams.
*/
......@@ -23,11 +23,12 @@ We first define the new class by deriving dynamicgraph::command::Command:
The constructor takes
\li a reference to a InvertedPendulum and calls parent class,
\li a vector a types specifying the number and types of input arguments of the command and
\li a string documenting the command.
In this case, there is only one argument of type <c>double</c>. Note the use of <c>boost::assign::list_of</c> to build
a vector in one command line: \code Increment(InvertedPendulum& entity, const std::string& docstring) : Command(entity,
boost::assign::list_of(Value::DOUBLE), docstring)
\li a vector a types specifying the number and types of input arguments of the
command and \li a string documenting the command. In this case, there is only
one argument of type <c>double</c>. Note the use of
<c>boost::assign::list_of</c> to build a vector in one command line: \code
Increment(InvertedPendulum& entity, const std::string& docstring) :
Command(entity, boost::assign::list_of(Value::DOUBLE), docstring)
{
}
\endcode
......
......@@ -3,15 +3,16 @@
\section dg_tutorial_inverted_pendulum_python_intro Introduction
Generating python bindings for new Entity classes is straightforward. We only need to add the following lines into file
<c>src/CMakeLists.txt</c>:
Generating python bindings for new Entity classes is straightforward. We only
need to add the following lines into file <c>src/CMakeLists.txt</c>:
<code>
DYNAMIC_GRAPH_PYTHON_MODULE("tutorial" ${LIBRARY_NAME} wrap)
</code>
This will create and install a python module called <c>dynamic_graph.tutorial</c>, linked with library
<c>${LIBRARY_NAME} (libdynamic-graph-tutorial.so)</c>. When importing this module, two new python classes deriving from
<c>Entity</c> are created: \li InvertedPendulum, and \li FeedbackController. \sa <c>src/simu.py</c> for an example
python script.
This will create and install a python module called
<c>dynamic_graph.tutorial</c>, linked with library <c>${LIBRARY_NAME}
(libdynamic-graph-tutorial.so)</c>. When importing this module, two new python
classes deriving from <c>Entity</c> are created: \li InvertedPendulum, and \li
FeedbackController. \sa <c>src/simu.py</c> for an example python script.
*/
......@@ -3,10 +3,11 @@
\section intro_dg_tutorial Introduction
This tutorial implements a simple application of the dynamic-graph package with two entities represented by the
following classes: \li dynamicgraph::tutorial::InvertedPendulum: that implements the dynamics of a pendulum on a cart
and \li dynamicgraph::tutorial::FeedbackController: that implements a closed-loop control loop that stabilizes the
pendulum.
This tutorial implements a simple application of the dynamic-graph package with
two entities represented by the following classes: \li
dynamicgraph::tutorial::InvertedPendulum: that implements the dynamics of a
pendulum on a cart and \li dynamicgraph::tutorial::FeedbackController: that
implements a closed-loop control loop that stabilizes the pendulum.
\section dg_tutorial_inverted_pendulum_ Prerequisite
......@@ -17,9 +18,10 @@ This tutorial requires prior installation of packages:
\section dg_tutorial_inverted_pendulum_overview Overview
This tutorial show:
\li how to \ref dg_tutorial_inverted_pendulum_cxx "create a new entity" with signals and commands,
\li how to \ref dg_tutorial_inverted_pendulum_python "build a python module" to create and control this entity in a
python interpreter.
\li how to \ref dg_tutorial_inverted_pendulum_cxx "create a new entity" with
signals and commands, \li how to \ref dg_tutorial_inverted_pendulum_python
"build a python module" to create and control this entity in a python
interpreter.
*/
......@@ -8,9 +8,9 @@
#define DG_TUTORIAL_FEEDBACK_CONTROLLER_HH
#include <dynamic-graph/entity.h>
#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/linear-algebra.h>
namespace dynamicgraph {
namespace tutorial {
......
......@@ -10,8 +10,8 @@
#define DG_TUTORIAL_INVERTED_PENDULUM_HH
#include <dynamic-graph/entity.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/signal-ptr.h>
namespace dynamicgraph {
namespace tutorial {
......@@ -23,9 +23,9 @@ namespace tutorial {
The equation of motion is:
\f{eqnarray*}{
\left ( M + m \right ) \ddot x - m l \ddot \theta \cos \theta + m l \dot \theta^2 \sin \theta &=& F\\
m l (-g \sin \theta - \ddot x \cos \theta + l \ddot \theta) &=& 0
\f}
\left ( M + m \right ) \ddot x - m l \ddot \theta \cos \theta + m l \dot
\theta^2 \sin \theta &=& F\\ m l (-g \sin \theta - \ddot x \cos \theta + l
\ddot \theta) &=& 0 \f}
where
\li the state is a vector of dimension 4
......@@ -82,7 +82,9 @@ class InvertedPendulum : public Entity {
virtual const std::string& getClassName(void) const { return CLASS_NAME; }
/// Header documentation of the python class
virtual std::string getDocString() const { return "Classical inverted pendulum dynamic model\n"; }
virtual std::string getDocString() const {
return "Classical inverted pendulum dynamic model\n";
}
/// Integrate equation of motion over time step given as input
void incr(double inTimeStep);
......@@ -153,7 +155,8 @@ class InvertedPendulum : public Entity {
/**
\brief Compute the evolution of the state of the pendulum
*/
::dynamicgraph::Vector computeDynamics(const ::dynamicgraph::Vector& inState, const double& inControl,
::dynamicgraph::Vector computeDynamics(const ::dynamicgraph::Vector& inState,
const double& inControl,
double inTimeStep);
};
} // namespace tutorial
......
[tool.black]
exclude = "cmake"
[flake8]
exclude = cmake
max-line-length = 88
ignore = E226, E704, E24, E121, W504, E126, E123, W503, E203
......@@ -14,10 +14,10 @@ def build_graph():
b = dgt.FeedbackController("K")
# plug signals
stateOut = a.signal('state')
forceIn = a.signal('force')
stateIn = b.signal('state')
forceOut = b.signal('force')
stateOut = a.signal("state")
forceIn = a.signal("force")
stateIn = b.signal("state")
forceOut = b.signal("force")
dg.plug(stateOut, stateIn)
dg.plug(forceOut, forceIn)
......@@ -29,7 +29,9 @@ def build_graph():
s.value = np.array((0.0, 0.1, 0.0, 0.0))
gain = np.array((0.0, 27.0, 0.001, 0.001))
b.setGain(gain, )
b.setGain(
gain,
)
return s, f, a
......@@ -72,5 +74,5 @@ def play(nbSteps):
pl.show()
if __name__ == '__main__':
if __name__ == "__main__":
play(100)
......@@ -4,10 +4,12 @@
* Florent Lamiraux
*/
#include <dynamic-graph/factory.h>
#include <dynamic-graph/command-setter.h>
#include <dynamic-graph/command-getter.h>
#include "dynamic-graph/tutorial/feedback-controller.hh"
#include <dynamic-graph/command-getter.h>
#include <dynamic-graph/command-setter.h>
#include <dynamic-graph/factory.h>
#include "constant.hh"
using namespace dynamicgraph;
......@@ -20,8 +22,10 @@ DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(FeedbackController, "FeedbackController");
FeedbackController::FeedbackController(const std::string& inName)
: Entity(inName),
stateSIN(NULL, "FeedbackController(" + inName + ")::input(vector)::state"),
forceSOUT(stateSIN, "FeedbackController(" + inName + ")::output(double)::force"),
stateSIN(NULL,
"FeedbackController(" + inName + ")::input(vector)::state"),
forceSOUT(stateSIN,
"FeedbackController(" + inName + ")::output(double)::force"),
gain_(Matrix(4, 1)) {
// Register signals into the entity.
signalRegistration(stateSIN);
......@@ -38,7 +42,8 @@ FeedbackController::FeedbackController(const std::string& inName)
boost::function2<double&, double&, const int&> ftest =
boost::bind(&FeedbackController::computeForceFeedback, this, _1, _2);
forceSOUT.setFunction(boost::bind(&FeedbackController::computeForceFeedback, this, _1, _2));
forceSOUT.setFunction(
boost::bind(&FeedbackController::computeForceFeedback, this, _1, _2));
std::string docstring;
// setGain
docstring =
......@@ -46,7 +51,8 @@ FeedbackController::FeedbackController(const std::string& inName)
" Set gain of controller\n"
" takes a tuple of 4 floating point numbers as input\n"
"\n";
addCommand(std::string("setGain"), new ::dynamicgraph::command::Setter<FeedbackController, Matrix>(
addCommand(std::string("setGain"),
new ::dynamicgraph::command::Setter<FeedbackController, Matrix>(
*this, &FeedbackController::setGain, docstring));
// getGain
......@@ -55,17 +61,20 @@ FeedbackController::FeedbackController(const std::string& inName)
" Get gain of controller\n"
" return a tuple of 4 floating point numbers\n"
"\n";
addCommand(std::string("getGain"), new ::dynamicgraph::command::Getter<FeedbackController, Matrix>(
addCommand(std::string("getGain"),
new ::dynamicgraph::command::Getter<FeedbackController, Matrix>(
*this, &FeedbackController::getGain, docstring));
}
FeedbackController::~FeedbackController() {}
double& FeedbackController::computeForceFeedback(double& force, const int& inTime) {
double& FeedbackController::computeForceFeedback(double& force,
const int& inTime) {
const Vector& state = stateSIN(inTime);
if (state.size() != 4)
throw dynamicgraph::ExceptionSignal(dynamicgraph::ExceptionSignal::GENERIC, "state signal size is ",
throw dynamicgraph::ExceptionSignal(dynamicgraph::ExceptionSignal::GENERIC,
"state signal size is ",
"%d, should be 4.", state.size());
Vector v(-gain_ * state);
force = v(0);
......
......@@ -4,13 +4,15 @@
* Florent Lamiraux
*/
#include "dynamic-graph/tutorial/inverted-pendulum.hh"
#include <dynamic-graph/command-getter.h>
#include <dynamic-graph/command-setter.h>
#include <dynamic-graph/factory.h>
#include <boost/format.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/command-setter.h>
#include <dynamic-graph/command-getter.h>
#include "dynamic-graph/tutorial/inverted-pendulum.hh"
#include "command-increment.hh"
#include "constant.hh"
......@@ -60,7 +62,8 @@ InvertedPendulum::InvertedPendulum(const std::string& inName)
"\n"
" Set cart mass\n"
"\n";
addCommand(std::string("setCartMass"), new ::dynamicgraph::command::Setter<InvertedPendulum, double>(
addCommand(std::string("setCartMass"),
new ::dynamicgraph::command::Setter<InvertedPendulum, double>(
*this, &InvertedPendulum::setCartMass, docstring));
// getCartMass
......@@ -68,7 +71,8 @@ InvertedPendulum::InvertedPendulum(const std::string& inName)
"\n"
" Get cart mass\n"
"\n";
addCommand(std::string("getCartMass"), new ::dynamicgraph::command::Getter<InvertedPendulum, double>(
addCommand(std::string("getCartMass"),
new ::dynamicgraph::command::Getter<InvertedPendulum, double>(
*this, &InvertedPendulum::getCartMass, docstring));
// setPendulumMass
......@@ -76,7 +80,8 @@ InvertedPendulum::InvertedPendulum(const std::string& inName)
"\n"
" Set pendulum mass\n"
"\n";
addCommand(std::string("setPendulumMass"), new ::dynamicgraph::command::Setter<InvertedPendulum, double>(
addCommand(std::string("setPendulumMass"),
new ::dynamicgraph::command::Setter<InvertedPendulum, double>(
*this, &InvertedPendulum::setPendulumMass, docstring));
// getPendulumMass
......@@ -84,7 +89,8 @@ InvertedPendulum::InvertedPendulum(const std::string& inName)
"\n"
" Get pendulum mass\n"
"\n";
addCommand(std::string("getPendulumMass"), new ::dynamicgraph::command::Getter<InvertedPendulum, double>(
addCommand(std::string("getPendulumMass"),
new ::dynamicgraph::command::Getter<InvertedPendulum, double>(
*this, &InvertedPendulum::getPendulumMass, docstring));
// setPendulumLength
......@@ -92,7 +98,8 @@ InvertedPendulum::InvertedPendulum(const std::string& inName)
"\n"
" Set pendulum length\n"
"\n";
addCommand(std::string("setPendulumLength"), new ::dynamicgraph::command::Setter<InvertedPendulum, double>(
addCommand(std::string("setPendulumLength"),
new ::dynamicgraph::command::Setter<InvertedPendulum, double>(
*this, &InvertedPendulum::setPendulumLength, docstring));
// getPendulumLength
......@@ -100,15 +107,19 @@ InvertedPendulum::InvertedPendulum(const std::string& inName)
"\n"
" Get pendulum length\n"
"\n";
addCommand(std::string("getPendulumLength"), new ::dynamicgraph::command::Getter<InvertedPendulum, double>(
addCommand(std::string("getPendulumLength"),
new ::dynamicgraph::command::Getter<InvertedPendulum, double>(
*this, &InvertedPendulum::getPendulumLength, docstring));
}
InvertedPendulum::~InvertedPendulum() {}
Vector InvertedPendulum::computeDynamics(const Vector& inState, const double& inControl, double inTimeStep) {
Vector InvertedPendulum::computeDynamics(const Vector& inState,
const double& inControl,
double inTimeStep) {
if (inState.size() != 4)
throw dynamicgraph::ExceptionSignal(dynamicgraph::ExceptionSignal::GENERIC, "state signal size is ",
throw dynamicgraph::ExceptionSignal(dynamicgraph::ExceptionSignal::GENERIC,
"state signal size is ",
"%d, should be 4.", inState.size());
double dt = inTimeStep;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment