From 304304d54907b85e19548dd7a796979631bbeaf4 Mon Sep 17 00:00:00 2001 From: Florent Lamiraux Date: Fri, 16 Jun 2023 07:54:51 +0200 Subject: [PATCH 1/3] Use int64_t for signal time. --- .../dynamic-graph/python/dynamic-graph-py.hh | 2 +- .../dynamic-graph/python/signal-wrapper.hh | 2 +- src/dynamic_graph/dynamic-graph-py.cc | 7 +-- src/dynamic_graph/signal-base-py.cc | 44 +++++++++---------- src/dynamic_graph/signal-wrapper.cc | 15 ++++--- tests/custom_entity.cpp | 2 +- tests/custom_entity.h | 6 +-- tests/test_bindings.py | 4 +- 8 files changed, 42 insertions(+), 40 deletions(-) diff --git a/include/dynamic-graph/python/dynamic-graph-py.hh b/include/dynamic-graph/python/dynamic-graph-py.hh index f62c16e..51c2810 100644 --- a/include/dynamic-graph/python/dynamic-graph-py.hh +++ b/include/dynamic-graph/python/dynamic-graph-py.hh @@ -40,7 +40,7 @@ void exposeSignals(); // Declare functions defined in other source files namespace signalBase { -SignalBase* createSignalWrapper(const char* name, const char* type, +SignalBase* createSignalWrapper(const char* name, const char* type, bp::object object); } // namespace signalBase namespace entity { diff --git a/include/dynamic-graph/python/signal-wrapper.hh b/include/dynamic-graph/python/signal-wrapper.hh index 30a22e2..9cdaa30 100644 --- a/include/dynamic-graph/python/signal-wrapper.hh +++ b/include/dynamic-graph/python/signal-wrapper.hh @@ -22,7 +22,7 @@ class PythonSignalContainer : public Entity { public: using Entity::Entity; - void signalRegistration(const SignalArray& signals); + void signalRegistration(const SignalArray& signals); void rmSignal(const std::string& name); }; diff --git a/src/dynamic_graph/dynamic-graph-py.cc b/src/dynamic_graph/dynamic-graph-py.cc index 0cf7355..5946ffa 100644 --- a/src/dynamic_graph/dynamic-graph-py.cc +++ b/src/dynamic_graph/dynamic-graph-py.cc @@ -32,7 +32,7 @@ namespace python { /** \brief plug a signal into another one. */ -void plug(SignalBase* signalOut, SignalBase* signalIn) { +void plug(SignalBase* signalOut, SignalBase* signalIn) { signalIn->plug(signalOut); } @@ -66,7 +66,8 @@ MapOfEntities* getEntityMap() { &dg::PoolStorage::getInstance()->getEntityMap()); } -dg::SignalBase* getSignal(dg::Entity& e, const std::string& name) { +dg::SignalBase* getSignal(dg::Entity& e, const std::string& name) +{ return &e.getSignal(name); } @@ -76,7 +77,7 @@ class PythonEntity : public dg::Entity { public: using dg::Entity::Entity; - void signalRegistration(dg::SignalBase& signal) { + void signalRegistration(dg::SignalBase& signal) { dg::Entity::signalRegistration(signal); } void signalDeregistration(const std::string& name) { diff --git a/src/dynamic_graph/signal-base-py.cc b/src/dynamic_graph/signal-base-py.cc index a6f4db6..82b46d7 100644 --- a/src/dynamic_graph/signal-base-py.cc +++ b/src/dynamic_graph/signal-base-py.cc @@ -22,8 +22,6 @@ namespace bp = boost::python; namespace dynamicgraph { namespace python { -typedef int time_type; - typedef Eigen::AngleAxis VectorUTheta; typedef Eigen::Quaternion Quaternion; @@ -79,7 +77,7 @@ void exposeSignalBase(const char* name) { }) .def( "displayDependencies", - +[](const S_t& s, int time) -> std::string { + +[](const S_t& s, sigtime_t time) -> std::string { std::ostringstream oss; s.displayDependencies(oss, time); return oss.str(); @@ -88,9 +86,9 @@ void exposeSignalBase(const char* name) { } template <> -auto exposeSignal(const std::string& name) { - typedef Signal S_t; - bp::class_ >, boost::noncopyable> obj( +auto exposeSignal(const std::string& name) { + typedef Signal S_t; + bp::class_ >, boost::noncopyable> obj( name.c_str(), bp::init()); obj.add_property( "value", @@ -107,31 +105,31 @@ auto exposeSignal(const std::string& name) { } void exposeSignals() { - exposeSignalBase("SignalBase"); + exposeSignalBase("SignalBase"); - exposeSignalsOfType("Bool"); - exposeSignalsOfType("Int"); - exposeSignalsOfType("Double"); + exposeSignalsOfType("Bool"); + exposeSignalsOfType("Int"); + exposeSignalsOfType("Double"); - exposeSignalsOfType("Vector"); - exposeSignalsOfType("Vector3"); - exposeSignalsOfType("Vector7"); + exposeSignalsOfType("Vector"); + exposeSignalsOfType("Vector3"); + exposeSignalsOfType("Vector7"); - exposeSignalsOfType("Matrix"); - exposeSignalsOfType("MatrixRotation"); - exposeSignalsOfType("MatrixHomogeneous"); - exposeSignalsOfType("MatrixTwist"); + exposeSignalsOfType("Matrix"); + exposeSignalsOfType("MatrixRotation"); + exposeSignalsOfType("MatrixHomogeneous"); + exposeSignalsOfType("MatrixTwist"); - exposeSignalsOfType("Quaternion"); - exposeSignalsOfType("VectorUTheta"); + exposeSignalsOfType("Quaternion"); + exposeSignalsOfType("VectorUTheta"); } namespace signalBase { template -SignalWrapper* createSignalWrapperTpl(const char* name, bp::object o, +SignalWrapper* createSignalWrapperTpl(const char* name, bp::object o, std::string& error) { - typedef SignalWrapper SignalWrapper_t; + typedef SignalWrapper SignalWrapper_t; if (!SignalWrapper_t::checkCallable(o, error)) { return NULL; } @@ -153,12 +151,12 @@ PythonSignalContainer* getPythonSignalContainer() { /** \brief Create an instance of SignalWrapper */ -SignalBase* createSignalWrapper(const char* name, const char* type, +SignalBase* createSignalWrapper(const char* name, const char* type, bp::object object) { PythonSignalContainer* psc = getPythonSignalContainer(); if (psc == NULL) return NULL; - SignalBase* obj = NULL; + SignalBase* obj = NULL; std::string error; SIGNAL_WRAPPER_TYPE(if, BOOL, bool) // SIGNAL_WRAPPER_TYPE(else if, UNSIGNED ,bool) diff --git a/src/dynamic_graph/signal-wrapper.cc b/src/dynamic_graph/signal-wrapper.cc index cc41fb0..a878ede 100644 --- a/src/dynamic_graph/signal-wrapper.cc +++ b/src/dynamic_graph/signal-wrapper.cc @@ -9,7 +9,7 @@ namespace dynamicgraph { namespace python { void PythonSignalContainer::signalRegistration( - const SignalArray& signals) { + const SignalArray& signals) { Entity::signalRegistration(signals); } @@ -30,10 +30,13 @@ bool SignalWrapper::checkCallable(pyobject c, std::string& error) { return true; } -template class SignalWrapper; -template class SignalWrapper; -template class SignalWrapper; -template class SignalWrapper; -template class SignalWrapper; +template class SignalWrapper; +template class SignalWrapper; +template class SignalWrapper; +template class SignalWrapper; +template class SignalWrapper; +template class SignalWrapper; +template class SignalWrapper; +template class SignalWrapper; } // namespace python } // namespace dynamicgraph diff --git a/tests/custom_entity.cpp b/tests/custom_entity.cpp index a7aa2f5..1ead109 100644 --- a/tests/custom_entity.cpp +++ b/tests/custom_entity.cpp @@ -46,7 +46,7 @@ void CustomEntity::rmValidSignal() { signalDeregistration("out_double"); } -double &CustomEntity::update(double &res, const int &inTime) { +double &CustomEntity::update(double &res, const sigtime_t &inTime) { const double &aDouble = m_sigdSIN(inTime); res = aDouble; logger().stream(MSG_TYPE_ERROR) << "start update " << res << '\n'; diff --git a/tests/custom_entity.h b/tests/custom_entity.h index fd821f0..c950a07 100644 --- a/tests/custom_entity.h +++ b/tests/custom_entity.h @@ -14,8 +14,8 @@ namespace dynamicgraph { class CustomEntity : public Entity { public: - dynamicgraph::SignalPtr m_sigdSIN; - dynamicgraph::SignalTimeDependent m_sigdTimeDepSOUT; + dynamicgraph::SignalPtr m_sigdSIN; + dynamicgraph::SignalTimeDependent m_sigdTimeDepSOUT; DYNAMIC_GRAPH_ENTITY_DECL(); CustomEntity(const std::string n); @@ -24,7 +24,7 @@ class CustomEntity : public Entity { void rmValidSignal(); - double &update(double &res, const int &inTime); + double &update(double &res, const sigtime_t &inTime); void act(); }; diff --git a/tests/test_bindings.py b/tests/test_bindings.py index 425da39..12f0e13 100644 --- a/tests/test_bindings.py +++ b/tests/test_bindings.py @@ -8,8 +8,8 @@ dynamic_graph.wrap.plug(%s, %s) did not match C++ signature: plug(""" - "dynamicgraph::SignalBase* signalOut, " - "dynamicgraph::SignalBase* signalIn)" + "dynamicgraph::SignalBase* signalOut, " + "dynamicgraph::SignalBase* signalIn)" ) From 2d2a856646b490b5ef752a81b4b494fc12bba38f Mon Sep 17 00:00:00 2001 From: Florent Lamiraux Date: Thu, 22 Jun 2023 17:36:09 +0200 Subject: [PATCH 2/3] Provide support for long int (and unsigned) for functions and signals. --- src/dynamic_graph/convert-dg-to-py.cc | 8 +++++ src/dynamic_graph/signal-base-py.cc | 42 ++++++++++++++++++++------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/dynamic_graph/convert-dg-to-py.cc b/src/dynamic_graph/convert-dg-to-py.cc index 9df3a84..d4b1f50 100644 --- a/src/dynamic_graph/convert-dg-to-py.cc +++ b/src/dynamic_graph/convert-dg-to-py.cc @@ -29,8 +29,12 @@ command::Value toValue(bp::object o, const command::Value::Type& valueType) { return Value(bp::extract(o)); case (Value::UNSIGNED): return Value(bp::extract(o)); + case (Value::UNSIGNEDLONGINT): + return Value(bp::extract(o)); case (Value::INT): return Value(bp::extract(o)); + case (Value::LONGINT): + return Value(bp::extract(o)); case (Value::FLOAT): return Value(bp::extract(o)); case (Value::DOUBLE): @@ -67,8 +71,12 @@ bp::object fromValue(const command::Value& value) { return bp::object(value.boolValue()); case (Value::UNSIGNED): return bp::object(value.unsignedValue()); + case (Value::UNSIGNEDLONGINT): + return bp::object(value.unsignedlongintValue()); case (Value::INT): return bp::object(value.intValue()); + case (Value::LONGINT): + return bp::object(value.longintValue()); case (Value::FLOAT): return bp::object(value.floatValue()); case (Value::DOUBLE): diff --git a/src/dynamic_graph/signal-base-py.cc b/src/dynamic_graph/signal-base-py.cc index 82b46d7..7214b04 100644 --- a/src/dynamic_graph/signal-base-py.cc +++ b/src/dynamic_graph/signal-base-py.cc @@ -77,9 +77,9 @@ void exposeSignalBase(const char* name) { }) .def( "displayDependencies", - +[](const S_t& s, sigtime_t time) -> std::string { + +[](const S_t& s, int depth) -> std::string { std::ostringstream oss; - s.displayDependencies(oss, time); + s.displayDependencies(oss, depth); return oss.str(); }, "Print the signal dependencies in a string"); @@ -108,7 +108,10 @@ void exposeSignals() { exposeSignalBase("SignalBase"); exposeSignalsOfType("Bool"); + exposeSignalsOfType("UInt"); exposeSignalsOfType("Int"); + exposeSignalsOfType("UInt64"); + exposeSignalsOfType("Int64"); exposeSignalsOfType("Double"); exposeSignalsOfType("Vector"); @@ -158,15 +161,32 @@ SignalBase* createSignalWrapper(const char* name, const char* type, SignalBase* obj = NULL; std::string error; - SIGNAL_WRAPPER_TYPE(if, BOOL, bool) - // SIGNAL_WRAPPER_TYPE(else if, UNSIGNED ,bool) - SIGNAL_WRAPPER_TYPE(else if, INT, int) - SIGNAL_WRAPPER_TYPE(else if, FLOAT, float) - SIGNAL_WRAPPER_TYPE(else if, DOUBLE, double) - // SIGNAL_WRAPPER_TYPE(else if, STRING ,bool) - SIGNAL_WRAPPER_TYPE(else if, VECTOR, Vector) - // SIGNAL_WRAPPER_TYPE(else if, MATRIX ,bool) - // SIGNAL_WRAPPER_TYPE(else if, MATRIX4D ,bool) + if(command::Value::typeName(command::Value::BOOL).compare(type) == 0) { + obj = createSignalWrapperTpl(name, object, error); + } + else if(command::Value::typeName(command::Value::UNSIGNED).compare(type) == + 0) { + obj = createSignalWrapperTpl(name, object, error); + } + else if(command::Value::typeName(command::Value::INT).compare(type) == 0) { + obj = createSignalWrapperTpl(name, object, error); + } + else if(command::Value::typeName(command::Value::UNSIGNEDLONGINT). + compare(type) == 0){ + obj = createSignalWrapperTpl(name, object, error); + } + else if(command::Value::typeName(command::Value::LONGINT).compare(type) == 0){ + obj = createSignalWrapperTpl(name, object, error); + } + else if(command::Value::typeName(command::Value::FLOAT).compare(type) == 0){ + obj = createSignalWrapperTpl(name, object, error); + } + else if(command::Value::typeName(command::Value::DOUBLE).compare(type) == 0){ + obj = createSignalWrapperTpl(name, object, error); + } + else if(command::Value::typeName(command::Value::VECTOR).compare(type) == 0){ + obj = createSignalWrapperTpl(name, object, error); + } else { error = "Type not understood"; } From 3c01cb29b42e21c529824928e22f80e09a51532a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 23 Jun 2023 09:59:26 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../dynamic-graph/python/dynamic-graph-py.hh | 2 +- src/dynamic_graph/dynamic-graph-py.cc | 4 +- src/dynamic_graph/signal-base-py.cc | 41 +++++++++---------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/include/dynamic-graph/python/dynamic-graph-py.hh b/include/dynamic-graph/python/dynamic-graph-py.hh index 51c2810..1a36c34 100644 --- a/include/dynamic-graph/python/dynamic-graph-py.hh +++ b/include/dynamic-graph/python/dynamic-graph-py.hh @@ -41,7 +41,7 @@ void exposeSignals(); // Declare functions defined in other source files namespace signalBase { SignalBase* createSignalWrapper(const char* name, const char* type, - bp::object object); + bp::object object); } // namespace signalBase namespace entity { diff --git a/src/dynamic_graph/dynamic-graph-py.cc b/src/dynamic_graph/dynamic-graph-py.cc index 5946ffa..dda53ac 100644 --- a/src/dynamic_graph/dynamic-graph-py.cc +++ b/src/dynamic_graph/dynamic-graph-py.cc @@ -66,8 +66,8 @@ MapOfEntities* getEntityMap() { &dg::PoolStorage::getInstance()->getEntityMap()); } -dg::SignalBase* getSignal(dg::Entity& e, const std::string& name) -{ +dg::SignalBase* getSignal(dg::Entity& e, + const std::string& name) { return &e.getSignal(name); } diff --git a/src/dynamic_graph/signal-base-py.cc b/src/dynamic_graph/signal-base-py.cc index 7214b04..eedb367 100644 --- a/src/dynamic_graph/signal-base-py.cc +++ b/src/dynamic_graph/signal-base-py.cc @@ -130,8 +130,9 @@ void exposeSignals() { namespace signalBase { template -SignalWrapper* createSignalWrapperTpl(const char* name, bp::object o, - std::string& error) { +SignalWrapper* createSignalWrapperTpl(const char* name, + bp::object o, + std::string& error) { typedef SignalWrapper SignalWrapper_t; if (!SignalWrapper_t::checkCallable(o, error)) { return NULL; @@ -155,39 +156,35 @@ PythonSignalContainer* getPythonSignalContainer() { \brief Create an instance of SignalWrapper */ SignalBase* createSignalWrapper(const char* name, const char* type, - bp::object object) { + bp::object object) { PythonSignalContainer* psc = getPythonSignalContainer(); if (psc == NULL) return NULL; SignalBase* obj = NULL; std::string error; - if(command::Value::typeName(command::Value::BOOL).compare(type) == 0) { + if (command::Value::typeName(command::Value::BOOL).compare(type) == 0) { obj = createSignalWrapperTpl(name, object, error); - } - else if(command::Value::typeName(command::Value::UNSIGNED).compare(type) == - 0) { + } else if (command::Value::typeName(command::Value::UNSIGNED).compare(type) == + 0) { obj = createSignalWrapperTpl(name, object, error); - } - else if(command::Value::typeName(command::Value::INT).compare(type) == 0) { + } else if (command::Value::typeName(command::Value::INT).compare(type) == 0) { obj = createSignalWrapperTpl(name, object, error); - } - else if(command::Value::typeName(command::Value::UNSIGNEDLONGINT). - compare(type) == 0){ + } else if (command::Value::typeName(command::Value::UNSIGNEDLONGINT) + .compare(type) == 0) { obj = createSignalWrapperTpl(name, object, error); - } - else if(command::Value::typeName(command::Value::LONGINT).compare(type) == 0){ + } else if (command::Value::typeName(command::Value::LONGINT).compare(type) == + 0) { obj = createSignalWrapperTpl(name, object, error); - } - else if(command::Value::typeName(command::Value::FLOAT).compare(type) == 0){ + } else if (command::Value::typeName(command::Value::FLOAT).compare(type) == + 0) { obj = createSignalWrapperTpl(name, object, error); - } - else if(command::Value::typeName(command::Value::DOUBLE).compare(type) == 0){ + } else if (command::Value::typeName(command::Value::DOUBLE).compare(type) == + 0) { obj = createSignalWrapperTpl(name, object, error); - } - else if(command::Value::typeName(command::Value::VECTOR).compare(type) == 0){ + } else if (command::Value::typeName(command::Value::VECTOR).compare(type) == + 0) { obj = createSignalWrapperTpl(name, object, error); - } - else { + } else { error = "Type not understood"; }