Commit c42f3fa4 authored by Gabriele Buondonno's avatar Gabriele Buondonno
Browse files

RoundDoubleToInt

parent 6e411599
/*
* Copyright 2018, Gepetto team, LAAS-CNRS
*
* This file is part of sot-talos-balance.
* sot-talos-balance is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
* sot-talos-balance is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with sot-talos-balance. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __sot_talos_balance_round_double_to_int_H__
#define __sot_talos_balance_round_double_to_int_H__
/* --------------------------------------------------------------------- */
/* --- API ------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
#if defined (WIN32)
# if defined (position_controller_EXPORTS)
# define ROUND_DOUBLE_TO_INT_EXPORT __declspec(dllexport)
# else
# define ROUND_DOUBLE_TO_INT_EXPORT __declspec(dllimport)
# endif
#else
# define ROUND_DOUBLE_TO_INT_EXPORT
#endif
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
#include <dynamic-graph/signal-helper.h>
#include <map>
#include "boost/assign.hpp"
namespace dynamicgraph {
namespace sot {
namespace talos_balance {
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
class ROUND_DOUBLE_TO_INT_EXPORT RoundDoubleToInt
: public ::dynamicgraph::Entity
{
DYNAMIC_GRAPH_ENTITY_DECL();
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
/* --- CONSTRUCTOR ---- */
RoundDoubleToInt( const std::string & name );
void init(){}
/* --- SIGNALS --- */
DECLARE_SIGNAL_IN(sin, double);
DECLARE_SIGNAL_OUT(sout, int);
/* --- COMMANDS --- */
/* --- ENTITY INHERITANCE --- */
virtual void display( std::ostream& os ) const;
}; // class RoundDoubleToInt
} // namespace talos_balance
} // namespace sot
} // namespace dynamicgraph
#endif // #ifndef __sot_talos_balance_round_double_to_int_H__
......@@ -37,6 +37,7 @@ ENDIF(UNIX)
SET(plugins
example
boolean-identity
round-double-to-int
int-identity
pose-quaternion-to-matrix-homo
euler-to-quat
......
/*
* Copyright 2018, Gepetto team, LAAS-CNRS
*
* This file is part of sot-talos-balance.
* sot-talos-balance is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
* sot-talos-balance is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with sot-talos-balance. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sot/talos_balance/round-double-to-int.hh"
#include <sot/core/debug.hh>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/all-commands.h>
namespace dynamicgraph
{
namespace sot
{
namespace talos_balance
{
namespace dg = ::dynamicgraph;
using namespace dg;
using namespace dg::command;
#define INPUT_SIGNALS m_sinSIN
#define OUTPUT_SIGNALS m_soutSOUT
/// Define EntityClassName here rather than in the header file
/// so that it can be used by the macros DEFINE_SIGNAL_**_FUNCTION.
typedef RoundDoubleToInt EntityClassName;
/* --- DG FACTORY ---------------------------------------------------- */
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(RoundDoubleToInt,
"RoundDoubleToInt");
/* ------------------------------------------------------------------- */
/* --- CONSTRUCTION -------------------------------------------------- */
/* ------------------------------------------------------------------- */
RoundDoubleToInt::RoundDoubleToInt(const std::string& name)
: Entity(name)
, CONSTRUCT_SIGNAL_IN(sin, double)
, CONSTRUCT_SIGNAL_OUT(sout, int, INPUT_SIGNALS)
{
Entity::signalRegistration( INPUT_SIGNALS << OUTPUT_SIGNALS );
/* Commands. */
addCommand("init", makeCommandVoid0(*this, &RoundDoubleToInt::init, docCommandVoid0("Initialize the entity.")));
}
/* ------------------------------------------------------------------- */
/* --- SIGNALS ------------------------------------------------------- */
/* ------------------------------------------------------------------- */
DEFINE_SIGNAL_OUT_FUNCTION(sout,int)
{
double in = m_sinSIN(iter);
s = int(round(in));
return s;
}
/* ------------------------------------------------------------------- */
/* --- ENTITY -------------------------------------------------------- */
/* ------------------------------------------------------------------- */
void RoundDoubleToInt::display(std::ostream& os) const
{
os << "RoundDoubleToInt " << getName();
// try
// {
// getProfiler().report_all(3, os);
// }
// catch (ExceptionSignal e) {}
}
} // namespace talos_balance
} // namespace sot
} // namespace dynamicgraph
from __future__ import print_function
import numpy as np
from sot_talos_balance.round_double_to_int import RoundDoubleToInt
rd = RoundDoubleToInt('round')
time=0
rd.sin.value = 0.
rd.sout.recompute(time)
print("%f -> %d" % (rd.sin.value, rd.sout.value))
np.testing.assert_equal(rd.sout.value,0)
time+=1
rd.sin.value = 1.
rd.sout.recompute(time)
print("%f -> %d" % (rd.sin.value, rd.sout.value))
np.testing.assert_equal(rd.sout.value,1)
time+=1
rd.sin.value = 1.3
rd.sout.recompute(time)
print("%f -> %d" % (rd.sin.value, rd.sout.value))
np.testing.assert_equal(rd.sout.value,1)
time+=1
rd.sin.value = 1.8
rd.sout.recompute(time)
print("%f -> %d" % (rd.sin.value, rd.sout.value))
np.testing.assert_equal(rd.sout.value,2)
time+=1
rd.sin.value = 2.5
rd.sout.recompute(time)
print("%f -> %d" % (rd.sin.value, rd.sout.value))
np.testing.assert_equal(rd.sout.value,3)
time+=1
rd.sin.value = -1.
rd.sout.recompute(time)
print("%f -> %d" % (rd.sin.value, rd.sout.value))
np.testing.assert_equal(rd.sout.value,-1)
time+=1
rd.sin.value = -1.3
rd.sout.recompute(time)
print("%f -> %d" % (rd.sin.value, rd.sout.value))
np.testing.assert_equal(rd.sout.value,-1)
time+=1
rd.sin.value = -1.8
rd.sout.recompute(time)
print("%f -> %d" % (rd.sin.value, rd.sout.value))
np.testing.assert_equal(rd.sout.value,-2)
time+=1
rd.sin.value = -2.5
rd.sout.recompute(time)
print("%f -> %d" % (rd.sin.value, rd.sout.value))
np.testing.assert_equal(rd.sout.value,-3)
time+=1
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