|
| 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__ |
0 commit comments