Skip to content

Commit 94d0af5

Browse files
committed
Add entity Switch
1 parent 171862b commit 94d0af5

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ SET(NEWHEADERS
8686
sot/core/periodic-call.hh
8787
sot/core/periodic-call-entity.hh
8888
sot/core/trajectory.hh
89+
sot/core/switch.hh
8990
)
9091
INSTALL(FILES ${NEWHEADERS}
9192
DESTINATION include/sot/core

include/sot/core/switch.hh

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) 2018, Joseph Mirabel
2+
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3+
//
4+
// This file is part of sot-core.
5+
// sot-core is free software: you can redistribute it
6+
// and/or modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation, either version
8+
// 3 of the License, or (at your option) any later version.
9+
//
10+
// sot-core is distributed in the hope that it will be
11+
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12+
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// General Lesser Public License for more details. You should have
14+
// received a copy of the GNU Lesser General Public License along with
15+
// sot-core. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#ifndef __SOT_SWITCH_H__
18+
# define __SOT_SWITCH_H__
19+
20+
#include <iostream>
21+
22+
#include <dynamic-graph/entity.h>
23+
#include <dynamic-graph/signal.h>
24+
#include <dynamic-graph/signal-ptr.h>
25+
#include <dynamic-graph/signal-time-dependent.h>
26+
#include <dynamic-graph/pool.h>
27+
#include <dynamic-graph/command-bind.h>
28+
#include <dynamic-graph/command-getter.h>
29+
30+
#include <sot/core/config.hh>
31+
32+
namespace dynamicgraph {
33+
namespace sot {
34+
/// Switch
35+
template <typename Value, typename Time = int>
36+
class SOT_CORE_DLLAPI Switch : public dynamicgraph::Entity
37+
{
38+
DYNAMIC_GRAPH_ENTITY_DECL();
39+
40+
Switch (const std::string& name) :
41+
Entity (name),
42+
selectionSIN(NULL,"Switch("+name+")::input(int)::selection"),
43+
boolSelectionSIN(NULL,"Switch("+name+")::input(bool)::boolSelection"),
44+
signalSOUT ("Switch("+name+")::output(" + typeName() + ")::sout")
45+
{
46+
signalSOUT.setFunction (boost::bind (&Switch::signal, this, _1, _2));
47+
signalRegistration (selectionSIN << boolSelectionSIN << signalSOUT);
48+
49+
using command::makeCommandVoid1;
50+
std::string docstring =
51+
"\n"
52+
" Set number of input signals\n";
53+
addCommand ("setSignalNumber", makeCommandVoid1
54+
(*this, &Switch::setSignalNumber, docstring));
55+
}
56+
57+
~Switch () {}
58+
59+
/// Header documentation of the python class
60+
virtual std::string getDocString () const
61+
{
62+
return
63+
"Dynamically select a given signal based on a input information.\n";
64+
}
65+
66+
void setSignalNumber (const int& n)
67+
{
68+
assert (n>=0);
69+
const std::size_t oldSize = signals.size();
70+
for (std::size_t i = n; i < oldSize; ++i)
71+
{
72+
std::ostringstream oss; oss << "sin" << i;
73+
signalDeregistration(oss.str());
74+
delete signals[i];
75+
}
76+
signals.resize(n,NULL);
77+
78+
for (std::size_t i = oldSize; i < (std::size_t)n; ++i)
79+
{
80+
assert (signals[i]==NULL);
81+
std::ostringstream oss;
82+
oss << "Switch("<< getName()<< ")::input(" << typeName() << ")::sin" << i;
83+
signals[i] = new Signal_t (NULL,oss.str());
84+
signalRegistration(*signals[i]);
85+
}
86+
}
87+
88+
private:
89+
typedef SignalPtr<Value, Time> Signal_t;
90+
typedef std::vector<Signal_t*> Signals_t;
91+
92+
static const std::string& typeName ();
93+
94+
Value& signal (Value& ret, const Time& time)
95+
{
96+
int sel;
97+
if (selectionSIN.isPlugged()) {
98+
sel = selectionSIN (time);
99+
} else {
100+
const bool& b = boolSelectionSIN(time);
101+
sel = b ? 1 : 0;
102+
}
103+
if (sel < 0 || sel >= int(signals.size()))
104+
throw std::runtime_error ("Signal selection is out of range.");
105+
106+
ret = (*signals[sel]) (time);
107+
return ret;
108+
}
109+
110+
Signals_t signals;
111+
SignalPtr <int, Time> selectionSIN;
112+
SignalPtr <bool, Time> boolSelectionSIN;
113+
114+
Signal <Value, Time> signalSOUT;
115+
};
116+
} // namespace sot
117+
} // namespace dynamicgraph
118+
#endif // __SOT_SWITCH_H__

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ SET(plugins
9292
tools/periodic-call-entity
9393
tools/joint-trajectory-entity
9494
tools/latch
95+
tools/switch
9596
9697
control/control-gr
9798
control/control-pd

src/tools/switch.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2017, Joseph Mirabel
2+
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3+
//
4+
// This file is part of sot_hpp.
5+
// sot_hpp is free software: you can redistribute it
6+
// and/or modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation, either version
8+
// 3 of the License, or (at your option) any later version.
9+
//
10+
// sot_hpp is distributed in the hope that it will be
11+
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12+
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// General Lesser Public License for more details. You should have
14+
// received a copy of the GNU Lesser General Public License along with
15+
// sot_hpp. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#include <sot/core/switch.hh>
18+
19+
#include <dynamic-graph/factory.h>
20+
21+
#include "type-name-helper.hh"
22+
23+
namespace dynamicgraph {
24+
namespace sot {
25+
template <typename Value, typename Time>
26+
const std::string& Switch<Value,Time>::typeName ()
27+
{
28+
return TypeNameHelper<Value>::typeName;
29+
}
30+
31+
typedef Switch<Vector,int> SwitchVector;
32+
template<>
33+
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN (SwitchVector, "SwitchVector");
34+
} // namespace sot
35+
} // namespace dynamicgraph

0 commit comments

Comments
 (0)