26 if (!_sdf->HasElement(
"type"))
28 std::cerr <<
"conversion does not have a type element" << std::endl;
32 std::string identifier = _sdf->Get<std::string>(
"type");
36 std::cerr <<
"Cannot creatae ConversionFunction with unknown identifier: " 37 << identifier << std::endl;
57 std::cerr <<
"Warning: Registering ConversionFunction with identifier: " 58 << _identifier <<
" twice" << std::endl;
62 std::cout <<
"Registered ConversionFunction type " 63 << _identifier << std::endl;
76 if (!_sdf->HasElement(
"rotorConstant"))
78 std::cerr <<
"ConversionFunctionBasic::Expected element rotorConstant" 82 double gain = _sdf->Get<
double>(
"rotorConstant");
89 return this->rotorConstant*std::abs(_cmd)*_cmd;
96 if (!_tag.compare(
"rotor_constant"))
97 _output = this->rotorConstant;
101 gzmsg <<
"ConversionFunctionBasic::GetParam <" << _tag <<
">=" << _output <<
108 : rotorConstant(_rotorConstant)
110 gzmsg <<
"ConversionFunctionBasic::Create conversion function" 112 <<
"\t- rotorConstant: " << this->
rotorConstant << std::endl;
124 if (!_sdf->HasElement(
"rotorConstantL"))
126 std::cerr <<
"ConversionFunctionBasic: expected element rotorConstantL" 131 if (!_sdf->HasElement(
"rotorConstantR"))
133 std::cerr <<
"ConversionFunctionBasic::Expected element rotorConstantR" 138 if (!_sdf->HasElement(
"deltaL"))
140 std::cerr <<
"ConversionFunctionBasic::Expected element deltaL" 145 if (!_sdf->HasElement(
"deltaR"))
147 std::cerr <<
"ConversionFunctionBasic::Expected element deltaR" 153 _sdf->Get<
double>(
"rotorConstantR"),
154 _sdf->Get<
double>(
"deltaL"),
155 _sdf->Get<
double>(
"deltaR"));
161 double basic = _cmd*std::abs(_cmd);
163 if (basic <= this->deltaL)
165 return this->rotorConstantL*(basic - this->deltaL);
167 else if (basic >= this->deltaR)
169 return this->rotorConstantR*(basic - this->deltaR);
179 double _rotorConstantR,
182 rotorConstantL(_rotorConstantL),
183 rotorConstantR(_rotorConstantR),
188 "ConversionFunctionBessa: rotorConstantL should be >= 0");
190 "ConversionFunctionBessa: rotorConstantR should be >= 0");
192 "ConversionFunctionBessa: deltaL should be <= 0");
194 "ConversionFunctionBessa: deltaR should be >= 0");
196 gzmsg <<
"ConversionFunctionBessa:" << std::endl
199 <<
"\t- deltaL: " << this->
deltaL << std::endl
200 <<
"\t- deltaR: " << this->
deltaR << std::endl;
207 if (!_tag.compare(
"rotor_constant_l"))
209 else if (!_tag.compare(
"rotor_constant_r"))
211 else if (!_tag.compare(
"delta_l"))
213 else if (!_tag.compare(
"delta_r"))
218 gzmsg <<
"ConversionFunctionBessa::GetParam <" << _tag <<
">=" << _output <<
231 if (!_sdf->HasElement(
"inputValues"))
233 std::cerr <<
"ConversionFunctionLinearInterp::Expected element inputValues" 238 if (!_sdf->HasElement(
"outputValues"))
240 std::cerr <<
"ConversionFunctionLinearInterp::Expected element outputValues" 245 std::vector<double> in =
Str2Vector(_sdf->Get<std::string>(
"inputValues"));
246 std::vector<double> out =
Str2Vector(_sdf->Get<std::string>(
"outputValues"));
250 std::cerr <<
"ConversionFunctionLinearInterp::" 251 <<
"Need at least one input/output pair" 256 if (in.size() != out.size())
258 std::cerr <<
"ConversionFunctionLinearInterp::" 259 <<
"Number of input and output values should be the same" 270 GZ_ASSERT(!lookupTable.empty(),
"Lookup table is empty");
273 auto iter = lookupTable.lower_bound(_cmd);
275 if (iter == lookupTable.end())
279 return lookupTable.rbegin()->second;
282 double i1 = iter->first;
283 double o1 = iter->second;
285 if (iter == lookupTable.begin())
290 double i0 = iter->first;
291 double o0 = iter->second;
293 double w1 = _cmd - i0;
294 double w0 = i1 - _cmd;
296 return (o0*w0 + o1*w1)/(w0 + w1);
301 const std::vector<double> &_input,
302 const std::vector<double> &_output)
304 GZ_ASSERT(_input.size() == _output.size(),
"input and output do not match");
306 for (
int i = 0; i < _input.size(); i++)
308 lookupTable[_input[i]] = _output[i];
310 gzmsg <<
"ConversionFunctionLinearInterp::Create conversion function" 312 gzmsg <<
"\t- Input values:" << std::endl;
313 for (
auto& i : lookupTable)
314 std::cout << i.first <<
" ";
315 std::cout << std::endl;
316 gzmsg <<
"\t- Output values:" << std::endl;
317 for (
auto& i : lookupTable)
318 std::cout << i.second <<
" ";
319 std::cout << std::endl;
331 return this->lookupTable;
ConversionFunctionLinearInterp(const std::vector< double > &_input, const std::vector< double > &_output)
Constructor.
virtual bool GetParam(std::string _tag, double &_output)
Return paramater in scalar form for the given tag.
virtual double convert(double _cmd)
Convert thruster state (e.g. angular velocity) to thrust force.
std::map< std::string, ConversionFunctionCreator > creators_
Map of each registered identifiers to its corresponding creator.
ConversionFunctionBessa(double _rotorConstantL, double _rotorConstantR, double _deltaL, double _deltaR)
Constructor.
ConversionFunction *(* ConversionFunctionCreator)(sdf::ElementPtr)
Function pointer to create a certain conversion function.
static const std::string IDENTIFIER
The unique identifier of this conversion function.
virtual bool GetParam(std::string _tag, double &_output)
Return paramater in scalar form for the given tag.
Asymmetric conversion function with dead-zone nonlinearity. This corresponds to what is called Model ...
virtual bool GetParam(std::string _tag, double &_output)
Return paramater in scalar form for the given tag.
static ConversionFunction * create(sdf::ElementPtr _sdf)
Create a ConversionFunction object according to its sdf Description.
Factory singleton class that creates a ConversionFunction from sdf.
Description of the conversion function fo a thruster.
static const std::string IDENTIFIER
The unique identifier of this conversion function.
Abstact base class for a thruster conversion function.
The most basic conversion function: Thrust = const.*w*abs(w) This corresponds to what is attrributed ...
double deltaR
Dead-zone for omega > 0.
ConversionFunction * CreateConversionFunction(sdf::ElementPtr _sdf)
Create a ConversionFunction object according to its sdf Description.
static ConversionFunction * create(sdf::ElementPtr _sdf)
Create a ConversionFunction object according to its sdf Description.
static ConversionFunction * create(sdf::ElementPtr _sdf)
Create a ConversionFunction object according to its sdf Description.
double rotorConstantR
Rotor constant for omega > 0.
std::vector< double > Str2Vector(std::string _input)
Conversion of a string to a double vector.
bool RegisterCreator(const std::string &_identifier, ConversionFunctionCreator _creator)
Register a ConversionFunction class with its creator.
double rotorConstantL
Rotor constant for omega < 0.
virtual double convert(double _cmd)
Convert thruster state (e.g. angular velocity) to thrust force.
static ConversionFunctionFactory & GetInstance()
Return the singleton instance of this factory.
virtual std::map< double, double > GetTable()
Return input and output vectors of the lookup table.
REGISTER_CONVERSIONFUNCTION_CREATOR(ConversionFunctionBasic,&ConversionFunctionBasic::create) ConversionFunction *ConversionFunctionBasic
static const std::string IDENTIFIER
The unique identifier of this conversion function.
virtual double convert(double _cmd)
Convert thruster state (e.g. angular velocity) to thrust force.
double deltaL
Dead-zone for omega < 0.
Conversion using linear interpolation between given data points.
ConversionFunctionBasic(double _rotorConstant)
Constructor.