iCub-main
Loading...
Searching...
No Matches
eomcParser.cpp
Go to the documentation of this file.
1// -*- Mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3/*
4* Copyright (C) 2012 iCub Facility, Istituto Italiano di Tecnologia
5* Authors: Valentina Gaggero
6* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
7*
8*/
9
10
11#include <yarp/os/Bottle.h>
12#include <algorithm>
13#include <string.h>
14#include <iostream>
15
16
17
18#include <eomcParser.h>
19#include <embObjMotionControlDefaults.h> // include the header with default values
20
21#include <yarp/os/LogStream.h>
22
23#include "EoCommon.h"
24#include "EOarray.h"
25#include "EoProtocol.h"
26#include "EoProtocolMN.h"
27#include "EoProtocolMC.h"
28#include "EoProtocolAS.h"
29#include <vector>
30
31
32using namespace std;
33using namespace yarp::dev;
34using namespace yarp::os;
35using namespace yarp::dev::eomc;
36using namespace yarp::dev::eomc::configType;
37
38//------------------------------------------------------------------------------
39// Utilities functions for parsing the configuration file of embObjMotionControl
40//------------------------------------------------------------------------------
41inline void loadStrings(std::vector<std::string>& dest, const yarp::os::Bottle& source)
42{
43 dest.clear();
44 for (unsigned int i = 1; i < source.size(); ++i)
45 {
46 dest.push_back(source.get(i).asString());
47 }
48}
49
50
51// Static map for eOmc_ctrl_out_type_t: config string -> (enum, description)
53{
54 eOmc_ctrl_out_type_t enumValue;
55 std::string description;
56};
57
58static const std::map<std::string, CtrlOutTypeInfo> ctrlOutTypeConfigMap =
59{
60 {std::string(eomc::ParamValues::outputType::n_a), {eomc_ctrl_out_type_n_a, "Not applicable / Not set"}},
61 {std::string(eomc::ParamValues::outputType::pwm), {eomc_ctrl_out_type_pwm, "PWM (Pulse Width Modulation)"}},
62 {std::string(eomc::ParamValues::outputType::velocity), {eomc_ctrl_out_type_vel_pwm, "Velocity control output"}},
63 {std::string(eomc::ParamValues::outputType::current), {eomc_ctrl_out_type_cur, "Current control output"}},
64 {std::string(eomc::ParamValues::outputType::vel_cur), {eomc_ctrl_out_type_vel_cur, "Velocity control output with inner current loop"}}
65};
66
70
71// Helper function to get control output type from configuration string
72bool getCtrlOutTypeFromConfig(const std::string &configName, eOmc_ctrl_out_type_t &outType, std::string &description)
73{
74 auto it = ctrlOutTypeConfigMap.find(configName);
75 if (it != ctrlOutTypeConfigMap.end())
76 {
77 outType = it->second.enumValue;
78 description = it->second.description;
79 return true;
80 }
81
82 outType = eomc_ctrl_out_type_n_a;
83 description = "Unknown control output type";
84 return false;
85}
86
87
88
89//------------------------------------------------------------------------------
90// Parser class implementation
91//------------------------------------------------------------------------------
92yarp::dev::eomc::Parser::Parser(int numofjoints, string boardname)
93 : _defaultSettings(defaults::DefaultsFactory::getInstance().getDefaults())
94{
95 _njoints = numofjoints;
96 _boardname = boardname;
97
98 _optionalParametersUsed.clear();
99};
100
102{
103 // std::vector manages memory automatically, no need to delete
104}
105
106//---------------------------------------------------
107// auxiliary functions to parse groups in config file
108// --------------------------------------------------
109bool Parser::extractGroup(Bottle &input, Bottle &out, const std::string &key1, const std::string &txt, int size, bool mandatory)
110{
111 size++;
112 out.clear();
113 Bottle &tmp=input.findGroup(key1.c_str(), txt.c_str());
114 if (tmp.isNull())
115 {
116 std::string message = key1 + " parameter not found for board " + _boardname + " in bottle " + input.toString();
117 if(mandatory)
118 yError () << message.c_str();
119 // else
120 // yWarning() << message.c_str();
121 return false;
122 }
123
124 if(tmp.size()!=size)
125 {
126 yError () << key1.c_str() << " incorrect number of entries in BOARD " << _boardname;
127 return false;
128 }
129
130 out=tmp;
131 return true;
132}
133
134bool Parser::areControlPidGroupEqualInJointSet(const std::vector<std::string> &controlGroup, const std::vector<int> &joint2set, const std::string &controlName)
135{
136 if(static_cast<int>(controlGroup.size()) != _njoints)
137 {
138 yError() << "embObjMC BOARD " << _boardname << "Control group" << controlName.c_str() << "has" << controlGroup.size() << "entries but" << _njoints << "joints are expected. Quitting.";
139 return false;
140 }
141
142 if(static_cast<int>(joint2set.size()) != _njoints)
143 {
144 yError() << "embObjMC BOARD " << _boardname << "Invalid joint2set size while checking" << controlName.c_str() << ". Quitting.";
145 return false;
146 }
147
148 std::vector<bool> setNameAssigned(_njoints, false);
149 std::vector<std::string> setName(_njoints);
150
151 for(int j = 0; j < _njoints; ++j)
152 {
153 const int setId = joint2set[j];
154 if((setId < 0) || (setId >= _njoints))
155 {
156 yError() << "embObjMC BOARD " << _boardname << "Invalid set id" << setId << "for joint" << j << "while checking" << controlName.c_str() << ". Quitting.";
157 return false;
158 }
159
160 if(!setNameAssigned[setId])
161 {
162 setNameAssigned[setId] = true;
163 setName[setId] = controlGroup[j];
164 continue;
165 }
166
167 if(setName[setId] != controlGroup[j])
168 {
169 yError() << "embObjMC BOARD " << _boardname << "Joints belonging to set" << setId
170 << "must have the same" << controlName.c_str() << "name. Joint" << j
171 << "uses" << controlGroup[j].c_str() << "while expected" << setName[setId].c_str() << ". Quitting.";
172 return false;
173 }
174 }
175
176 return true;
177}
178
179inline bool Parser::GetGroupBottle(yarp::os::Searchable& config, const std::string& controlName, yarp::os::Bottle& outBottle, bool mandatory)
180{
181 if(isEquals(controlName, eomc::ParamValues::NONE_STR))
182 {
183 outBottle.clear();
184 return true;
185 }
186 yarp::os::Bottle& tmp = config.findGroup(controlName.c_str());
187 if (tmp.isNull())
188 {
189 outBottle.clear();
190 if(mandatory)
191 {
192 yError() << "embObjMC BOARD " << _boardname << "Unable to find" << controlName.c_str() << " in config file.";
193 }
194 return false;
195 }
196
197 outBottle = tmp;
198 return true;
199}
200
201
202//---------------------------------------------------
203// auxiliary functions to parse PIDs in config file
204// --------------------------------------------------
205bool Parser::readUserNameControlsGroup(yarp::os::Searchable &config)
206{
207 Bottle controlsGroup, xtmp, jointsetcfg;
208 std::vector<JointsSet> jsets(_njoints);
209 std::vector<int> joint2set(_njoints, -1);
210
211 auto readControlNames = [&](const std::string &paramName,
212 const std::string &description,
213 std::vector<std::string> &dest) -> bool
214 {
215 if (!extractGroup(controlsGroup, xtmp, paramName, description, _njoints))
216 {
217 return false;
218 }
219
220 loadStrings(dest, xtmp);
221 return areControlPidGroupEqualInJointSet(dest, joint2set, paramName);
222 };
223
224 if(!GetGroupBottle(config, "CONTROLS", controlsGroup)) return false;
225
226 if(GetGroupBottle(config, "JOINTSET_CFG", jointsetcfg, false))
227 {
228 if(!parseJointsetCfgGroup(config, jsets, joint2set))
229 {
230 yError() << "embObjMC BOARD " << _boardname << "Unable to parse JOINTSET_CFG while checking control groups consistency. Quitting.";
231 return false;
232 }
233 }
234 else
235 {
236 // Backward compatibility: if JOINTSET_CFG is not present, keep the old global check behaviour.
237 std::fill(joint2set.begin(), joint2set.end(), 0);
238 }
239
240 if (!readControlNames(std::string(ParamValues::controlMode::position), "Position Control ", _userNameControlPosition))
241 return false;
242
243 if (!readControlNames(std::string(ParamValues::controlMode::velocity), "Velocity Control ", _userNameControlVelocity))
244 return false;
245
246 for(int j = 0; j < _njoints; ++j)
247 {
248 if(!isEquals(_userNameControlVelocity[j], _userNameControlPosition[j]))
249 {
250 yError() << "embObjMC BOARD " << _boardname << ": velocityControl parameter for joint" << j << "(" << _userNameControlVelocity[j].c_str()
251 << ") must be equal to positionControl parameter (" << _userNameControlPosition[j].c_str() << ") or set to " << ParamValues::NONE_STR
252 << " because embedded control uses the same pid parameters for both position and velocity control. Quitting.";
253 return false;
254 }
255 }
256
257 if (!readControlNames(std::string(ParamValues::controlMode::mixed), "Mixed Control ", _userNameControlMixed))
258 return false;
259
260 for(int j = 0; j < _njoints; ++j)
261 {
262 if(!isEquals(_userNameControlMixed[j], _userNameControlPosition[j]))
263 {
264 yError() << "embObjMC BOARD " << _boardname << ": mixedControl parameter for joint" << j << "(" << _userNameControlMixed[j].c_str()
265 << ") must be equal to positionControl parameter (" << _userNameControlPosition[j].c_str() << ") or set to " << ParamValues::NONE_STR
266 << " because embedded control uses the same pid parameters for both position and velocity control. Quitting.";
267 return false;
268 }
269 }
270
271 if (!readControlNames(std::string(ParamValues::controlMode::torque), "Torque Control ", _userNameControlTorque))
272 return false;
273
274 if (!readControlNames(std::string(ParamValues::controlMode::current), "Current Pid ", _userNameControlCurrent))
275 return false;
276
277 if (!readControlNames(std::string(ParamValues::controlMode::positionDirect), "Position Direct Control ", _userNameControlPositionDirect))
278 return false;
279
280 if (!readControlNames(std::string(ParamValues::controlMode::velocityDirect), "Velocity Direct Control ", _userNameControlVelocityDirect))
281 return false;
282
283 return true;
284}
285
286
287bool yarp::dev::eomc::Parser::getOutputType(yarp::os::Bottle& pidsGroup, eOmc_ctrl_out_type_t &outType)
288{
289 Value &valOutputType= pidsGroup.find("outputType");
290 if( (valOutputType.isNull()) || (!valOutputType.isString()) )
291 {
292 yError() << "embObjMC BOARD " << _boardname << "Unable read outputType parameter. Quitting.";
293 return false;
294 }
295 std::string description;
296 return getCtrlOutTypeFromConfig(valOutputType.toString(), outType, description);
297}
298
299
300
301 bool Parser::readControlLaw(yarp::os::Bottle& pidsGroup, std::string &controlLaw_str)
302{
303 // 2) read control_law
304 Value &valControlLaw = pidsGroup.find("controlLaw");
305 if ((valControlLaw.isNull()) || (!valControlLaw.isString()))
306 {
307 yError() << "embObjMC BOARD " << _boardname << "Unable read control law parameter. Quitting.";
308 return false;
309 }
310
311 controlLaw_str = valControlLaw.toString();
312 return true;
313}
314
315bool Parser::parsePidUnitsType(yarp::os::Bottle& pidsGroup, eOmc_ctrl_out_type_t outType,
316 yarp::dev::PidFeedbackUnitsEnum &fbk_pidunits,
317 yarp::dev::PidOutputUnitsEnum& out_pidunits)
318{
319 Value &fbkControlUnits = pidsGroup.find("fbkControlUnits");
320 Value &outControlUnits = pidsGroup.find("outputControlUnits");
321 if(fbkControlUnits.isNull())
322 {
323 yError() << "embObjMC BOARD " << _boardname << " missing fbkControlUnits parameter";
324 return false;
325 }
326 if(!fbkControlUnits.isString())
327 {
328 yError() << "embObjMC BOARD " << _boardname << " fbkControlUnits parameter is not a string";
329 return false;
330 }
331 if (outControlUnits.isNull())
332 {
333 yError() << "embObjMC BOARD " << _boardname << " missing outputControlUnits parameter";
334 return false;
335 }
336 if (!outControlUnits.isString())
337 {
338 yError() << "embObjMC BOARD " << _boardname << " outputControlUnits parameter is not a string";
339 return false;
340 }
341
342 if(fbkControlUnits.toString() == eomc::ParamValues::fbkUnits::metric)
343 {
344 fbk_pidunits = yarp::dev::PidFeedbackUnitsEnum::METRIC;
345 }
346 else if(fbkControlUnits.toString() == eomc::ParamValues::fbkUnits::machine)
347 {
348 fbk_pidunits = yarp::dev::PidFeedbackUnitsEnum::RAW_MACHINE_UNITS;
349 }
350 else
351 {
352 yError() << "embObjMC BOARD " << _boardname << " invalid fbkControlUnits value: " << fbkControlUnits.toString().c_str();
353 return false;
354 }
355
356 const std::string outUnitsStr = outControlUnits.toString();
357 if (outUnitsStr == eomc::ParamValues::outUnits::machine)
358 {
359 out_pidunits = yarp::dev::PidOutputUnitsEnum::RAW_MACHINE_UNITS;
360 }
361 else if (outUnitsStr == eomc::ParamValues::outUnits::metric)
362 {
363 // PidOutputUnitsEnum depends on the physical output type declared in outputType.
364 switch (outType)
365 {
366 case eomc_ctrl_out_type_pwm: out_pidunits = yarp::dev::PidOutputUnitsEnum::DUTYCYCLE_PWM_PERCENT; break;
367 case eomc_ctrl_out_type_vel_pwm:
368 case eomc_ctrl_out_type_vel_cur:
369 out_pidunits = yarp::dev::PidOutputUnitsEnum::VELOCITY_METRIC;
370 break;
371 case eomc_ctrl_out_type_cur: out_pidunits = yarp::dev::PidOutputUnitsEnum::CURRENT_METRIC; break;
372 default:
373 yError() << "embObjMC BOARD " << _boardname
374 << " outputControlUnits=metric_units is not supported for outputType " << (int)outType;
375 return false;
376 }
377 }
378 else
379 {
380 yError() << "embObjMC BOARD " << _boardname << " invalid outputControlUnits value: \"" << outUnitsStr
381 << "\". Valid values are: \"" << eomc::ParamValues::outUnits::metric
382 << "\" or \"" << eomc::ParamValues::outUnits::machine << "\". Quitting.";
383 return false;
384 }
385
386 return true;
387}
388
389//---------------------------------------------------
390// Parse PIDs functions in config file
391// --------------------------------------------------
392bool Parser::parsePids(yarp::os::Searchable &config, PidControllers_t &pids, bool lowLevPidisMandatory)
393{
394 // Read the CONTROLS group to get the user names of the control pids choosen by user.
395 // It checks that all joints in the same set have the same control name for each control type.
396
397 if(!readUserNameControlsGroup(config))
398 return false;
399
400 if(!parseSelectedCurrentPid(config, lowLevPidisMandatory, pids.cur))
401 return false;
402
403 if(!parseSelectedPositionControl(config, pids.trj))
404 return false;
405
406 if(!parseSelectedVelocityControl(config, pids.vel))
407 return false;
408
409 if(!parseSelectedMixedControl(config, pids.mix))
410 return false;
411
412 if(!parseSelectedPositionDirectControl(config, pids.dir_pos))
413 return false;
414
415 if(!parseSelectedVelocityDirectControl(config, pids.dir_vel))
416 return false;
417
418 if(!parseSelectedTorqueControl(config, pids.trq))
419 return false;
420
421 return true;
422}
423
424bool Parser::parseSelectedCurrentPid(yarp::os::Searchable &config, bool pidisMandatory, std::vector<eomc::PidInfo> &curr_pids)
425{
426 // first of all verify current pid has been configured if it is mandatory
427 int enabledCurrentPids = 0;
428 for(int j = 0; j < _njoints; ++j)
429 {
430 if(!isEquals(_userNameControlCurrent[j], eomc::ParamValues::NONE_STR))
431 {
432 ++enabledCurrentPids;
433 }
434 }
435
436 if((enabledCurrentPids == 0) && pidisMandatory)
437 {
438 for (auto& current_pid : curr_pids)
439 {
440 current_pid.enabled = false;
441 }
442 yError() << "embObjMC BOARD " << _boardname << "CurrentPid is mandatory. It should be different from none for at least one joint.";
443 return false;
444 }
445
446 std::map<std::string, std::vector<eomc::PidInfo>> parsedGroups;
447 std::map<std::string, std::string> groupControlLaw;
448 std::map<std::string, yarp::dev::PidFeedbackUnitsEnum> groupFbkUnits;
449 std::map<std::string, yarp::dev::PidOutputUnitsEnum> groupOutUnits;
450
451 for(int j = 0; j < _njoints; ++j)
452 {
453 const std::string &groupName = _userNameControlCurrent[j];
454 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
455 {
456 continue;
457 }
458
459 if(parsedGroups.find(groupName) != parsedGroups.end())
460 {
461 continue;
462 }
463
464 Bottle bot_ctrl;
465 if (!GetGroupBottle(config, groupName, bot_ctrl)) return false;
466
467 std::string strControlLaw;
468 if(!readControlLaw(bot_ctrl, strControlLaw)) return false;
469
470 if (strControlLaw != eomc::ParamValues::controlLaw::current)
471 {
472 yError() << "embObjMC BOARD" << _boardname << "Unable to use" << strControlLaw << "control law for current pid group" << groupName.c_str() << ". Quitting.";
473 return false;
474 }
475
476 yarp::dev::PidFeedbackUnitsEnum fbk_unitstype;
477 yarp::dev::PidOutputUnitsEnum out_unitstype;
478 if (!parsePidUnitsType(bot_ctrl, eomc_ctrl_out_type_cur, fbk_unitstype, out_unitstype)) return false;
479
480 std::vector<eomc::PidInfo> parsed(_njoints);
481 if(!parsePidsGroup2FOC(bot_ctrl, parsed)) return false;
482
483 parsedGroups[groupName] = parsed;
484 groupControlLaw[groupName] = strControlLaw;
485 groupFbkUnits[groupName] = fbk_unitstype;
486 groupOutUnits[groupName] = out_unitstype;
487 }
488
489 for(int j = 0; j < _njoints; ++j)
490 {
491 const std::string &groupName = _userNameControlCurrent[j];
492 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
493 {
494 curr_pids[j].enabled = false;
495 curr_pids[j].usernamePidSelected = eomc::ParamValues::NONE_STR;
496 continue;
497 }
498
499 curr_pids[j] = parsedGroups[groupName][j];
500 curr_pids[j].enabled = true;
501 curr_pids[j].fbk_PidUnits = groupFbkUnits[groupName];
502 curr_pids[j].out_PidUnits = groupOutUnits[groupName];
503 curr_pids[j].controlLaw = groupControlLaw[groupName];
504 curr_pids[j].usernamePidSelected = groupName;
505 curr_pids[j].out_type = eomc_ctrl_out_type_cur;
506 }
507
508 return true;
509}
510
511bool Parser::parseSelectedPositionControl(yarp::os::Searchable &config, std::vector<eomc::PidInfo> &pos_pids)
512{
513 int enabledPositionPids = 0;
514 for(int j = 0; j < _njoints; ++j)
515 {
516 if(!isEquals(_userNameControlPosition[j], eomc::ParamValues::NONE_STR))
517 {
518 ++enabledPositionPids;
519 }
520 }
521
522 if(enabledPositionPids == 0)
523 {
524 for (auto& p_pid : pos_pids) p_pid.enabled = false;
525 yError() << "embObjMC BOARD " << _boardname << "position Pid is mandatory. It should be different from none for at least one joint.";
526 return false;
527 }
528
529 std::map<std::string, std::vector<eomc::PidInfo>> parsedGroups;
530 std::map<std::string, std::string> groupControlLaw;
531 std::map<std::string, yarp::dev::PidFeedbackUnitsEnum> groupFbkUnits;
532 std::map<std::string, yarp::dev::PidOutputUnitsEnum> groupOutUnits;
533 std::map<std::string, eOmc_ctrl_out_type_t> groupOutType;
534
535 for(int j = 0; j < _njoints; ++j)
536 {
537 const std::string &groupName = _userNameControlPosition[j];
538 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
539 {
540 continue;
541 }
542
543 if(parsedGroups.find(groupName) != parsedGroups.end())
544 {
545 continue;
546 }
547
548 Bottle bot_ctrl = config.findGroup(groupName.c_str());
549 if(bot_ctrl.isNull())
550 {
551 yError() << "embObjMC BOARD " << _boardname << "Unable to find position control group " << groupName.c_str() << " in config file. Quitting.";
552 return false;
553 }
554
555 std::string strControlLaw;
556 if(!readControlLaw(bot_ctrl, strControlLaw)) return false;
557
559 {
560 yError() << "embObjMC BOARD " << _boardname << " Unable to use " << strControlLaw << " control law for position pid group " << groupName.c_str() << ". Quitting.";
561 return false;
562 }
563
564 eOmc_ctrl_out_type_t out_type;
565 if(!getOutputType(bot_ctrl, out_type)) return false;
566
567 yarp::dev::PidFeedbackUnitsEnum fbk_unitstype;
568 yarp::dev::PidOutputUnitsEnum out_unitstype;
569 if (!parsePidUnitsType(bot_ctrl, out_type, fbk_unitstype, out_unitstype)) return false;
570
571 std::vector<eomc::PidInfo> parsed(_njoints);
572 bool parseOk = false;
573 switch (out_type)
574 {
575 case eomc_ctrl_out_type_pwm:
576 case eomc_ctrl_out_type_cur:
577 parseOk = parsePidsGroupRegulationParams(bot_ctrl, parsed);
578 break;
579 case eomc_ctrl_out_type_vel_pwm:
580 case eomc_ctrl_out_type_vel_cur:
581 parseOk = parsePidsGroupMinimalParams(bot_ctrl, parsed);
582 break;
583 default:
584 yError() << "embObjMC BOARD " << _boardname << "Unsupported output type for position control group " << groupName.c_str() << ". Quitting.";
585 return false;
586 }
587 if(!parseOk)
588 {
589 yError() << "embObjMC BOARD " << _boardname << "Unable to parse pid values for " << groupName.c_str() << ". Quitting.";
590 return false;
591 }
592
593 parsedGroups[groupName] = parsed;
594 groupControlLaw[groupName] = strControlLaw;
595 groupFbkUnits[groupName] = fbk_unitstype;
596 groupOutUnits[groupName] = out_unitstype;
597 groupOutType[groupName] = out_type;
598 }
599
600 for(int j = 0; j < _njoints; ++j)
601 {
602 const std::string &groupName = _userNameControlPosition[j];
603 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
604 {
605 pos_pids[j].enabled = false;
606 pos_pids[j].usernamePidSelected = eomc::ParamValues::NONE_STR;
607 continue;
608 }
609
610 pos_pids[j] = parsedGroups[groupName][j];
611 pos_pids[j].enabled = true;
612 pos_pids[j].fbk_PidUnits = groupFbkUnits[groupName];
613 pos_pids[j].out_PidUnits = groupOutUnits[groupName];
614 pos_pids[j].controlLaw = groupControlLaw[groupName];
615 pos_pids[j].usernamePidSelected = groupName;
616 pos_pids[j].out_type = groupOutType[groupName];
617 }
618
619 return true;
620}
621
622bool Parser::parseSelectedVelocityControl(yarp::os::Searchable &config, std::vector<eomc::PidInfo> &vel_pids)
623{
624 int enabledVelocityPids = 0;
625 for(int j = 0; j < _njoints; ++j)
626 {
627 if(!isEquals(_userNameControlVelocity[j], eomc::ParamValues::NONE_STR))
628 {
629 ++enabledVelocityPids;
630 }
631 }
632
633 if(enabledVelocityPids == 0)
634 {
635 for (auto& v_pid : vel_pids)
636 {
637 v_pid.enabled = false;
638 v_pid.usernamePidSelected = eomc::ParamValues::NONE_STR;
639 }
640
641 yWarning() << "embObjMC BOARD " << _boardname << "Velocity pid has been desabled";
642 return true;
643 }
644
645 std::map<std::string, std::vector<eomc::PidInfo>> parsedGroups;
646 std::map<std::string, std::string> groupControlLaw;
647 std::map<std::string, yarp::dev::PidFeedbackUnitsEnum> groupFbkUnits;
648 std::map<std::string, yarp::dev::PidOutputUnitsEnum> groupOutUnits;
649 std::map<std::string, eOmc_ctrl_out_type_t> groupOutType;
650
651 for(int j = 0; j < _njoints; ++j)
652 {
653 const std::string &groupName = _userNameControlVelocity[j];
654 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
655 {
656 continue;
657 }
658
659 if(parsedGroups.find(groupName) != parsedGroups.end())
660 {
661 continue;
662 }
663
664 Bottle bot_ctrl;
665 if (!GetGroupBottle(config, groupName, bot_ctrl)) return false;
666
667 std::string strControlLaw;
668 if(!readControlLaw(bot_ctrl, strControlLaw)) return false;
669
671 {
672 yError() << "embObjMC BOARD " << _boardname << " Unable to use " << strControlLaw << " control law for velocity pid group " << groupName.c_str() << ". Quitting.";
673 return false;
674 }
675
676 eOmc_ctrl_out_type_t out_type;
677 if(!getOutputType(bot_ctrl, out_type)) return false;
678
679 yarp::dev::PidFeedbackUnitsEnum fbk_unitstype;
680 yarp::dev::PidOutputUnitsEnum out_unitstype;
681 if (!parsePidUnitsType(bot_ctrl, out_type, fbk_unitstype, out_unitstype)) return false;
682
683 std::vector<eomc::PidInfo> parsed(_njoints);
684 bool parseOk = false;
685 switch (out_type)
686 {
687 case eomc_ctrl_out_type_pwm:
688 case eomc_ctrl_out_type_cur:
689 parseOk = parsePidsGroupRegulationParams(bot_ctrl, parsed);
690 break;
691 case eomc_ctrl_out_type_vel_pwm:
692 case eomc_ctrl_out_type_vel_cur:
693 parseOk = parsePidsGroupMinimalParams(bot_ctrl, parsed);
694 break;
695 default:
696 yError() << "embObjMC BOARD " << _boardname << "Unsupported output type for position control group " << groupName.c_str() << ". Quitting.";
697 return false;
698 }
699 if(!parseOk)
700 {
701 yError() << "embObjMC BOARD " << _boardname << "Unable to parse pid values for " << groupName.c_str() << ". Quitting.";
702 return false;
703 }
704
705 parsedGroups[groupName] = parsed;
706 groupControlLaw[groupName] = strControlLaw;
707 groupFbkUnits[groupName] = fbk_unitstype;
708 groupOutUnits[groupName] = out_unitstype;
709 groupOutType[groupName] = out_type;
710 }
711
712 for(int j = 0; j < _njoints; ++j)
713 {
714 const std::string &groupName = _userNameControlVelocity[j];
715 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
716 {
717 vel_pids[j].enabled = false;
718 vel_pids[j].usernamePidSelected = eomc::ParamValues::NONE_STR;
719 continue;
720 }
721
722 vel_pids[j] = parsedGroups[groupName][j];
723 vel_pids[j].enabled = true;
724 vel_pids[j].fbk_PidUnits = groupFbkUnits[groupName];
725 vel_pids[j].out_PidUnits = groupOutUnits[groupName];
726 vel_pids[j].controlLaw = groupControlLaw[groupName];
727 vel_pids[j].usernamePidSelected = groupName;
728 vel_pids[j].out_type = groupOutType[groupName];
729 }
730
731 return true;
732
733}
734
735bool Parser::parseSelectedMixedControl(yarp::os::Searchable &config, std::vector<eomc::PidInfo> &mix_pids)
736{
737 int enabledMixedPids = 0;
738 for(int j = 0; j < _njoints; ++j)
739 {
740 if(!isEquals(_userNameControlMixed[j], eomc::ParamValues::NONE_STR))
741 {
742 ++enabledMixedPids;
743 }
744 }
745
746 if(enabledMixedPids == 0)
747 {
748 for (auto& m_pid : mix_pids)
749 {
750 m_pid.enabled = false;
751 m_pid.usernamePidSelected = eomc::ParamValues::NONE_STR;
752 }
753
754 yInfo() << "embObjMC BOARD " << _boardname << "Mixed pid has been desabled";
755 return true;
756 }
757
758 for(int j = 0; j < _njoints; ++j)
759 {
760 if(!isEquals(_userNameControlMixed[j], _userNameControlPosition[j]))
761 {
762 yError() << "embObjMC BOARD " << _boardname << "Mixed control law has a different name than position control law for joint" << j << ". Currently fw doesn't support different pid values for mixed control from position control, so the control law name should be the same. Quitting.";
763 return false;
764 }
765 }
766
767 std::map<std::string, std::vector<eomc::PidInfo>> parsedGroups;
768 std::map<std::string, std::string> groupControlLaw;
769 std::map<std::string, yarp::dev::PidFeedbackUnitsEnum> groupFbkUnits;
770 std::map<std::string, yarp::dev::PidOutputUnitsEnum> groupOutUnits;
771 std::map<std::string, eOmc_ctrl_out_type_t> groupOutType;
772
773 for(int j = 0; j < _njoints; ++j)
774 {
775 const std::string &groupName = _userNameControlMixed[j];
776 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
777 {
778 continue;
779 }
780
781 if(parsedGroups.find(groupName) != parsedGroups.end())
782 {
783 continue;
784 }
785
786 Bottle bot_ctrl;
787 if (!GetGroupBottle(config, groupName, bot_ctrl)) return false;
788
789 std::string strControlLaw;
790 if(!readControlLaw(bot_ctrl, strControlLaw)) return false;
791
792 if(strControlLaw != ParamValues::controlLaw::trajectory)
793 {
794 yError() << "embObjMC BOARD " << _boardname << "Unable to use " << strControlLaw << " control law for mixed pid group " << groupName.c_str() << ". Quitting.";
795 return false;
796 }
797
798 eOmc_ctrl_out_type_t out_type;
799 if(!getOutputType(bot_ctrl, out_type)) return false;
800
801 yarp::dev::PidFeedbackUnitsEnum fbk_unitstype;
802 yarp::dev::PidOutputUnitsEnum out_unitstype;
803 if (!parsePidUnitsType(bot_ctrl, out_type, fbk_unitstype, out_unitstype)) return false;
804
805 std::vector<eomc::PidInfo> parsed(_njoints);
806 bool parseOk = false;
807 switch (out_type)
808 {
809 case eomc_ctrl_out_type_pwm:
810 case eomc_ctrl_out_type_cur:
811 parseOk = parsePidsGroupRegulationParams(bot_ctrl, parsed);
812 break;
813 case eomc_ctrl_out_type_vel_pwm:
814 case eomc_ctrl_out_type_vel_cur :
815 parseOk = parsePidsGroupMinimalParams(bot_ctrl, parsed);
816 break;
817 default:
818 yError() << "embObjMC BOARD " << _boardname << "Unsupported output type for position control group " << groupName.c_str() << ". Quitting.";
819 return false;
820 }
821 if(!parseOk)
822 {
823 yError() << "embObjMC BOARD " << _boardname << "Unable to parse pid values for " << groupName.c_str() << ". Quitting.";
824 return false;
825 }
826
827 parsedGroups[groupName] = parsed;
828 groupControlLaw[groupName] = strControlLaw;
829 groupFbkUnits[groupName] = fbk_unitstype;
830 groupOutUnits[groupName] = out_unitstype;
831 groupOutType[groupName] = out_type;
832 }
833
834 for(int j = 0; j < _njoints; ++j)
835 {
836 const std::string &groupName = _userNameControlMixed[j];
837 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
838 {
839 mix_pids[j].enabled = false;
840 mix_pids[j].usernamePidSelected = eomc::ParamValues::NONE_STR;
841 continue;
842 }
843
844 mix_pids[j] = parsedGroups[groupName][j];
845 mix_pids[j].enabled = true;
846 mix_pids[j].fbk_PidUnits = groupFbkUnits[groupName];
847 mix_pids[j].out_PidUnits = groupOutUnits[groupName];
848 mix_pids[j].controlLaw = groupControlLaw[groupName];
849 mix_pids[j].usernamePidSelected = groupName;
850 mix_pids[j].out_type = groupOutType[groupName];
851 }
852
853 return true;
854
855}
856
857bool Parser::parseSelectedPositionDirectControl(yarp::os::Searchable &config, std::vector<eomc::PidInfo> &posdir_pids)
858{
859 int enabledPosDirectPids = 0;
860 for(int j = 0; j < _njoints; ++j)
861 {
862 if(!isEquals(_userNameControlPositionDirect[j], ParamValues::NONE_STR))
863 {
864 ++enabledPosDirectPids;
865 }
866 }
867
868 if(enabledPosDirectPids == 0)
869 {
870 for (auto& pd_pid : posdir_pids)
871 {
872 pd_pid.enabled = false;
873 pd_pid.usernamePidSelected = ParamValues::NONE_STR;
874 }
875
876 registerOptionalParameter("position direct pid", "0.0 for all pid values", false);
877 //yWarning() << "embObjMC BOARD " << _boardname << "position direct pid has been disabled";
878 return true;
879 }
880
881 std::map<std::string, std::vector<eomc::PidInfo>> parsedGroups;
882 std::map<std::string, std::string> groupControlLaw;
883 std::map<std::string, yarp::dev::PidFeedbackUnitsEnum> groupFbkUnits;
884 std::map<std::string, yarp::dev::PidOutputUnitsEnum> groupOutUnits;
885 std::map<std::string, eOmc_ctrl_out_type_t> groupOutType;
886
887 for(int j = 0; j < _njoints; ++j)
888 {
889 const std::string &groupName = _userNameControlPositionDirect[j];
890 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
891 {
892 continue;
893 }
894
895 if(parsedGroups.find(groupName) != parsedGroups.end())
896 {
897 continue;
898 }
899
900 Bottle bot_ctrl;
901 if (!GetGroupBottle(config, groupName, bot_ctrl)) return false;
902
903 std::string strControlLaw;
904 if(!readControlLaw(bot_ctrl, strControlLaw)) return false;
905
906 if(strControlLaw != ParamValues::controlLaw::direct)
907 {
908 yError() << "embObjMC BOARD " << _boardname << " Unable to use " << strControlLaw << " control law for position direct pid group " << groupName.c_str() << ". Quitting.";
909 return false;
910 }
911
912 eOmc_ctrl_out_type_t out_type;
913 if(!getOutputType(bot_ctrl, out_type)) return false;
914
915 yarp::dev::PidFeedbackUnitsEnum fbk_unitstype;
916 yarp::dev::PidOutputUnitsEnum out_unitstype;
917 if (!parsePidUnitsType(bot_ctrl, out_type, fbk_unitstype, out_unitstype)) return false;
918
919 std::vector<eomc::PidInfo> parsed(_njoints);
920 bool parseOk = false;
921 switch (out_type)
922 {
923 case eomc_ctrl_out_type_pwm:
924 case eomc_ctrl_out_type_cur:
925 parseOk = parsePidsGroupRegulationParams(bot_ctrl, parsed);
926 break;
927 case eomc_ctrl_out_type_vel_pwm:
928 case eomc_ctrl_out_type_vel_cur:
929 parseOk = parsePidsGroupMinimalParams(bot_ctrl, parsed);
930 break;
931 default:
932 yError() << "embObjMC BOARD " << _boardname << "Unsupported output type for position control group " << groupName.c_str() << ". Quitting.";
933 return false;
934 }
935 if(!parseOk)
936 {
937 yError() << "embObjMC BOARD " << _boardname << "Unable to parse pid values for " << groupName.c_str() << ". Quitting.";
938 return false;
939 }
940
941 parsedGroups[groupName] = parsed;
942 groupControlLaw[groupName] = strControlLaw;
943 groupFbkUnits[groupName] = fbk_unitstype;
944 groupOutUnits[groupName] = out_unitstype;
945 groupOutType[groupName] = out_type;
946 }
947
948 for(int j = 0; j < _njoints; ++j)
949 {
950 const std::string &groupName = _userNameControlPositionDirect[j];
951 if(isEquals(groupName, eomc::ParamValues::NONE_STR))
952 {
953 posdir_pids[j].enabled = false;
954 posdir_pids[j].usernamePidSelected = ParamValues::NONE_STR;
955 continue;
956 }
957
958 posdir_pids[j] = parsedGroups[groupName][j];
959 posdir_pids[j].enabled = true;
960 posdir_pids[j].fbk_PidUnits = groupFbkUnits[groupName];
961 posdir_pids[j].out_PidUnits = groupOutUnits[groupName];
962 posdir_pids[j].controlLaw = groupControlLaw[groupName];
963 posdir_pids[j].usernamePidSelected = groupName;
964 posdir_pids[j].out_type = groupOutType[groupName];
965 }
966
967 return true;
968}
969
970bool Parser::parseSelectedVelocityDirectControl(yarp::os::Searchable &config, std::vector<eomc::PidInfo> &veldir_pids)
971{
972 int enabledVelDirectPids = 0;
973 for(int j = 0; j < _njoints; ++j)
974 {
975 if(!isEquals(_userNameControlVelocityDirect[j], ParamValues::NONE_STR))
976 {
977 ++enabledVelDirectPids;
978 }
979 }
980
981 if(enabledVelDirectPids == 0)
982 {
983 for (auto& vd_pid : veldir_pids)
984 {
985 vd_pid.enabled = false;
986 vd_pid.usernamePidSelected = ParamValues::NONE_STR;
987 }
988
989 registerOptionalParameter("velocity direct pid", "0.0 for all pid values", false);
990 return true;
991 }
992
993 std::map<std::string, std::vector<eomc::PidInfo>> parsedGroups;
994 std::map<std::string, std::string> groupControlLaw;
995 std::map<std::string, yarp::dev::PidFeedbackUnitsEnum> groupFbkUnits;
996 std::map<std::string, yarp::dev::PidOutputUnitsEnum> groupOutUnits;
997 std::map<std::string, eOmc_ctrl_out_type_t> groupOutType;
998
999 for(int j = 0; j < _njoints; ++j)
1000 {
1001 const std::string &groupName = _userNameControlVelocityDirect[j];
1002 if(isEquals(groupName, ParamValues::NONE_STR))
1003 {
1004 continue;
1005 }
1006
1007 if(parsedGroups.find(groupName) != parsedGroups.end())
1008 {
1009 continue;
1010 }
1011
1012 Bottle bot_ctrl;
1013 if (!GetGroupBottle(config, groupName, bot_ctrl)) return false;
1014
1015 std::string strControlLaw;
1016 if(!readControlLaw(bot_ctrl, strControlLaw)) return false;
1017
1018 if(strControlLaw != ParamValues::controlLaw::direct)
1019 {
1020 yError() << "embObjMC BOARD " << _boardname << " Unable to use " << strControlLaw << " control law for velocity direct pid group " << groupName.c_str() << ". Quitting.";
1021 return false;
1022 }
1023
1024 eOmc_ctrl_out_type_t out_type;
1025 if(!getOutputType(bot_ctrl, out_type)) return false;
1026
1027 yarp::dev::PidFeedbackUnitsEnum fbk_unitstype;
1028 yarp::dev::PidOutputUnitsEnum out_unitstype;
1029 if (!parsePidUnitsType(bot_ctrl, out_type, fbk_unitstype, out_unitstype)) return false;
1030
1031 // Temporary workaround: map PWM to VEL and CUR to VEL+CUR for velocity direct control.
1032 if (out_type == eomc_ctrl_out_type_pwm)
1033 {
1034 out_type = eomc_ctrl_out_type_vel_pwm;
1035 }
1036 else if (out_type == eomc_ctrl_out_type_cur)
1037 {
1038 out_type = eomc_ctrl_out_type_vel_cur;
1039 }
1040
1041 std::vector<eomc::PidInfo> parsed(_njoints);
1042 if(!parsePidsGroup2FOC(bot_ctrl, parsed))
1043 {
1044 yError() << "embObjMC BOARD " << _boardname << "Unable to parse pid values for " << groupName.c_str() << ". Quitting.";
1045 return false;
1046 }
1047
1048 parsedGroups[groupName] = parsed;
1049 groupControlLaw[groupName] = strControlLaw;
1050 groupFbkUnits[groupName] = fbk_unitstype;
1051 groupOutUnits[groupName] = out_unitstype;
1052 groupOutType[groupName] = out_type;
1053 }
1054
1055 for(int j = 0; j < _njoints; ++j)
1056 {
1057 const std::string &groupName = _userNameControlVelocityDirect[j];
1058 if(isEquals(groupName, ParamValues::NONE_STR))
1059 {
1060 veldir_pids[j].enabled = false;
1061 veldir_pids[j].usernamePidSelected = ParamValues::NONE_STR;
1062 continue;
1063 }
1064
1065 veldir_pids[j] = parsedGroups[groupName][j];
1066 veldir_pids[j].enabled = true;
1067 veldir_pids[j].fbk_PidUnits = groupFbkUnits[groupName];
1068 veldir_pids[j].out_PidUnits = groupOutUnits[groupName];
1069 veldir_pids[j].controlLaw = groupControlLaw[groupName];
1070 veldir_pids[j].usernamePidSelected = groupName;
1071 veldir_pids[j].out_type = groupOutType[groupName];
1072 }
1073
1074 return true;
1075}
1076
1077bool Parser::parseSelectedTorqueControl(yarp::os::Searchable &config, std::vector<eomc::TrqPidInfo> &trq_pids)
1078{
1079 int enabledTorquePids = 0;
1080 for(int j = 0; j < _njoints; ++j)
1081 {
1082 if(!isEquals(_userNameControlTorque[j], ParamValues::NONE_STR))
1083 {
1084 ++enabledTorquePids;
1085 }
1086 }
1087
1088 if(enabledTorquePids == 0)
1089 {
1090 for (auto& t_pid : trq_pids)
1091 {
1092 t_pid.enabled = false;
1093 t_pid.usernamePidSelected = ParamValues::NONE_STR;
1094 }
1095 return true;
1096 }
1097
1098 std::map<std::string, std::vector<eomc::TrqPidInfo>> parsedGroups;
1099 std::map<std::string, std::string> groupControlLaw;
1100 std::map<std::string, yarp::dev::PidFeedbackUnitsEnum> groupFbkUnits;
1101 std::map<std::string, yarp::dev::PidOutputUnitsEnum> groupOutUnits;
1102 std::map<std::string, eOmc_ctrl_out_type_t> groupOutType;
1103
1104 for(int j = 0; j < _njoints; ++j)
1105 {
1106 const std::string &groupName = _userNameControlTorque[j];
1107 if(isEquals(groupName, ParamValues::NONE_STR))
1108 {
1109 continue;
1110 }
1111
1112 if(parsedGroups.find(groupName) != parsedGroups.end())
1113 {
1114 continue;
1115 }
1116
1117 Bottle bot_ctrl;
1118 if (!GetGroupBottle(config, groupName, bot_ctrl)) return false;
1119
1120 std::string strControlLaw;
1121 if(!readControlLaw(bot_ctrl, strControlLaw)) return false;
1122
1123 if(strControlLaw != ParamValues::controlLaw::torque)
1124 {
1125 yError() << "embObjMC BOARD " << _boardname << " Unable to use " << strControlLaw << " control law for torque pid group " << groupName.c_str() << ". Quitting.";
1126 return false;
1127 }
1128
1129 eOmc_ctrl_out_type_t out_type;
1130 if(!getOutputType(bot_ctrl, out_type)) return false;
1131
1132 yarp::dev::PidFeedbackUnitsEnum fbk_unitstype;
1133 yarp::dev::PidOutputUnitsEnum out_unitstype;
1134 if (!parsePidUnitsType(bot_ctrl, out_type, fbk_unitstype, out_unitstype)) return false;
1135
1136 std::vector<eomc::TrqPidInfo> parsed(_njoints);
1137 bool parseOk = false;
1138 switch (out_type)
1139 {
1140 case eomc_ctrl_out_type_pwm:
1141 case eomc_ctrl_out_type_cur:
1142 parseOk = parsePidsGroupTorqueCompensationParams(bot_ctrl, parsed);
1143 break;
1144 case eomc_ctrl_out_type_vel_cur:
1145 case eomc_ctrl_out_type_vel_pwm:
1146 parseOk = parsePidsGroupRegulationParams(bot_ctrl, parsed);
1147 break;
1148 default:
1149 yError() << "embObjMC BOARD " << _boardname << "Unsupported output type for position control group " << groupName.c_str() << ". Quitting.";
1150 return false;
1151 }
1152 if(!parseOk)
1153 {
1154 yError() << "embObjMC BOARD " << _boardname << "Unable to parse pid values for " << groupName.c_str() << ". Quitting.";
1155 return false;
1156 }
1157
1158 parsedGroups[groupName] = parsed;
1159 groupControlLaw[groupName] = strControlLaw;
1160 groupFbkUnits[groupName] = fbk_unitstype;
1161 groupOutUnits[groupName] = out_unitstype;
1162 groupOutType[groupName] = out_type;
1163 }
1164
1165 for(int j = 0; j < _njoints; ++j)
1166 {
1167 const std::string &groupName = _userNameControlTorque[j];
1168 if(isEquals(groupName, ParamValues::NONE_STR))
1169 {
1170 trq_pids[j].enabled = false;
1171 trq_pids[j].usernamePidSelected = ParamValues::NONE_STR;
1172 continue;
1173 }
1174
1175 trq_pids[j] = parsedGroups[groupName][j];
1176 trq_pids[j].enabled = true;
1177 trq_pids[j].fbk_PidUnits = groupFbkUnits[groupName];
1178 trq_pids[j].out_PidUnits = groupOutUnits[groupName];
1179 trq_pids[j].controlLaw = groupControlLaw[groupName];
1180 trq_pids[j].usernamePidSelected = groupName;
1181 trq_pids[j].out_type = groupOutType[groupName];
1182 }
1183
1184 return true;
1185}
1186
1187
1188/*
1189 <group name="2FOC_CUR_CONTROL">
1190 <param name="controlLaw"> low_lev_current </param>
1191 <param name="fbkControlUnits"> machine_units </param>
1192 <param name="outputControlUnits"> machine_units </param>
1193 <param name="cur_kff"> 0 0 </param>
1194 <param name="cur_kp"> 8 8 </param>
1195 <param name="cur_kd"> 0 0 </param>
1196 <param name="cur_ki"> 2 2 </param>
1197 <param name="cur_shift"> 10 10 </param>
1198 <param name="cur_maxOutput"> 32000 32000 </param>
1199 <param name="cur_maxInt"> 32000 32000 </param>
1200 </group>
1201
1202 <group name="2FOC_VEL_CONTROL">
1203 <param name="controlLaw"> low_lev_velocity </param>
1204 <param name="fbkControlUnits"> machine_units </param>
1205 <param name="outputControlUnits"> machine_units </param>
1206 <param name="spd_kff"> 0 0 </param>
1207 <param name="spd_kp"> 12 12 </param>
1208 <param name="spd_kd"> 0 0 </param>
1209 <param name="spd_ki"> 16 16 </param>
1210 <param name="spd_shift"> 10 10 </param>
1211 <param name="spd_maxOutput"> 32000 32000 </param>
1212 <param name="spd_maxInt"> 32000 32000 </param>
1213 </group>
1214*/
1215
1216bool Parser::parsePidsGroup2FOC(yarp::os::Bottle& pidsGroup, std::vector<eomc::PidInfo> &curr_pids)
1217{
1218 Bottle xtmp;
1219
1220 if (!extractGroup(pidsGroup, xtmp, "kff", "kff parameter", _njoints)) return false;
1221 for (int j = 0; j<_njoints; j++) curr_pids[j].pid.kff = xtmp.get(j + 1).asFloat64();
1222
1223 if (!extractGroup(pidsGroup, xtmp, "kp", "kp parameter", _njoints)) return false;
1224 for (int j = 0; j<_njoints; j++) curr_pids[j].pid.kp = xtmp.get(j + 1).asFloat64();
1225
1226 if (!extractGroup(pidsGroup, xtmp, "kd", "kd parameter", _njoints)) return false;
1227 for (int j = 0; j<_njoints; j++) curr_pids[j].pid.kd = xtmp.get(j + 1).asFloat64();
1228
1229 if (!extractGroup(pidsGroup, xtmp, "maxOutput", "maxOutput parameter", _njoints)) return false;
1230 for (int j = 0; j<_njoints; j++) curr_pids[j].pid.max_output = xtmp.get(j + 1).asFloat64();
1231
1232 if (!extractGroup(pidsGroup, xtmp, "ki", "ki parameter", _njoints)) return false;
1233 for (int j = 0; j<_njoints; j++) curr_pids[j].pid.ki = xtmp.get(j + 1).asFloat64();
1234
1235 if (!extractGroup(pidsGroup, xtmp, "maxInt", "maxInt parameter", _njoints)) return false;
1236 for (int j = 0; j<_njoints; j++) curr_pids[j].pid.max_int = xtmp.get(j + 1).asFloat64();
1237
1238 if (!extractGroup(pidsGroup, xtmp, "shift", "shift parameter", _njoints)) return false;
1239 for (int j = 0; j<_njoints; j++) curr_pids[j].pid.scale = xtmp.get(j + 1).asFloat64();
1240
1241 return true;
1242}
1243
1244
1245bool Parser::parsePidsGroupMinimalParams(Bottle& pidsGroup, std::vector<eomc::PidInfo> &pids)
1246{
1247 /*
1248 <param name = "kff"> 1 1 < / param>
1249 <param name = "kp"> 5 5 < / param>
1250 <param name = "kd"> 0 0 < / param>
1251 <param name = "maxOutput"> 32000 32000 < / param>
1252 */
1253
1254 Bottle xtmp;
1255
1256 if (!extractGroup(pidsGroup, xtmp, "kff", "kff parameter", _njoints)) return false;
1257 for (int j = 0; j<_njoints; j++) pids[j].pid.kff = xtmp.get(j + 1).asFloat64();
1258
1259 if (!extractGroup(pidsGroup, xtmp, "kp", "kp parameter", _njoints)) return false;
1260 for (int j = 0; j<_njoints; j++) pids[j].pid.kp = xtmp.get(j + 1).asFloat64();
1261
1262 if (!extractGroup(pidsGroup, xtmp, "kd", "kd parameter", _njoints)) return false;
1263 for (int j = 0; j<_njoints; j++) pids[j].pid.kd = xtmp.get(j + 1).asFloat64();
1264
1265 if (!extractGroup(pidsGroup, xtmp, "maxOutput", "maxOutput parameter", _njoints)) return false;
1266 for (int j = 0; j<_njoints; j++) pids[j].pid.max_output = xtmp.get(j + 1).asFloat64();
1267
1268 return true;
1269}
1270
1271bool Parser::parsePidsGroupRegulationParams(Bottle& pidsGroup, std::vector<eomc::PidInfo> &pids)
1272{
1273 /*
1274 <param name = "kff"> 1 1 < / param>
1275 <param name = "kp"> 5 5 < / param>
1276 <param name = "kd"> 0 0 < / param>
1277 <param name = "maxOutput"> 32000 32000 < / param>
1278 */
1279
1280 if (!parsePidsGroupMinimalParams(pidsGroup, pids)) return false;
1281
1282 /*
1283 <param name = "ki"> 7111.0 1066.0 < / param>
1284 <param name = "maxInt"> 750 1000 < / param>
1285 */
1286
1287 Bottle xtmp;
1288
1289 if (!extractGroup(pidsGroup, xtmp, "ki", "ki parameter", _njoints)) return false;
1290 for (int j = 0; j<_njoints; j++) pids[j].pid.ki = xtmp.get(j + 1).asFloat64();
1291
1292 if (!extractGroup(pidsGroup, xtmp, "maxInt", "maxInt parameter", _njoints)) return false;
1293 for (int j = 0; j<_njoints; j++) pids[j].pid.max_int = xtmp.get(j + 1).asFloat64();
1294
1295 return true;
1296}
1297
1298bool Parser::parsePidsGroupRegulationParams(Bottle& pidsGroup, std::vector<eomc::TrqPidInfo>& pids)
1299{
1300 std::vector<eomc::PidInfo> basePids(_njoints);
1301 if (!parsePidsGroupRegulationParams(pidsGroup, basePids)) return false;
1302 for (int j = 0; j < _njoints; j++)
1303 {
1304 static_cast<eomc::PidInfo&>(pids[j]) = basePids[j];
1305 }
1306 return true;
1307}
1308
1309bool Parser::parsePidsGroupTorqueCompensationParams(Bottle& pidsGroup, std::vector<eomc::TrqPidInfo> &pids)
1310{
1311 /*
1312 <param name = "kff"> 1 1 < / param>
1313 <param name = "kp"> 5 5 < / param>
1314 <param name = "kd"> 0 0 < / param>
1315 <param name = "maxOutput"> 32000 32000 < / param>
1316 <param name = "ki"> 7111.0 1066.0 < / param>
1317 <param name = "maxInt"> 750 1000 < / param>
1318 */
1319
1320 if (!parsePidsGroupRegulationParams(pidsGroup, pids)) return false;
1321
1322 Bottle xtmp;
1323
1324 if (!extractGroup(pidsGroup, xtmp, "ktau", "ktau parameter", _njoints)) return false;
1325 for (int j = 0; j<_njoints; j++) pids[j].ktau = xtmp.get(j + 1).asFloat64();
1326
1327 if (!extractGroup(pidsGroup, xtmp, "filterType", "filterType param", _njoints)) return false;
1328 for (int j = 0; j<_njoints; j++) pids[j].filterType = xtmp.get(j + 1).asInt32();
1329
1330 // Friction parameters
1331 if (!extractGroup(pidsGroup, xtmp, "viscousPos", "viscousPos parameter", _njoints, false))
1332 {
1333 registerOptionalParameter("viscousPos", std::to_string(_defaultSettings.torquePidDefaults.viscousPos), true);
1334 for (int j = 0; j<_njoints; j++) pids[j].viscousPos = _defaultSettings.torquePidDefaults.viscousPos;
1335 }
1336 else
1337 {
1338 for (int j = 0; j<_njoints; j++) pids[j].viscousPos = xtmp.get(j + 1).asFloat64();
1339 }
1340
1341 if (!extractGroup(pidsGroup, xtmp, "viscousNeg", "viscousNeg parameter", _njoints, false))
1342 {
1343 registerOptionalParameter("viscousNeg", std::to_string(_defaultSettings.torquePidDefaults.viscousNeg), true);
1344 for (int j = 0; j<_njoints; j++) pids[j].viscousNeg = _defaultSettings.torquePidDefaults.viscousNeg;
1345 }
1346 else
1347 {
1348 for (int j = 0; j<_njoints; j++) pids[j].viscousNeg = xtmp.get(j + 1).asFloat64();
1349 }
1350
1351 if (!extractGroup(pidsGroup, xtmp, "coulombPos", "coulombPos parameter", _njoints, false))
1352 {
1353 registerOptionalParameter("coulombPos", std::to_string(_defaultSettings.torquePidDefaults.coulombPos), true);
1354 for (int j = 0; j<_njoints; j++) pids[j].coulombPos = _defaultSettings.torquePidDefaults.coulombPos;
1355 }
1356 else
1357 {
1358 for (int j = 0; j<_njoints; j++) pids[j].coulombPos = xtmp.get(j + 1).asFloat64();
1359 }
1360
1361 if (!extractGroup(pidsGroup, xtmp, "coulombNeg", "coulombNeg parameter", _njoints, false))
1362 {
1363 registerOptionalParameter("coulombNeg", std::to_string(_defaultSettings.torquePidDefaults.coulombNeg), true);
1364 for (int j = 0; j<_njoints; j++) pids[j].coulombNeg = _defaultSettings.torquePidDefaults.coulombNeg;
1365 }
1366 else
1367 {
1368 for (int j = 0; j<_njoints; j++) pids[j].coulombNeg = xtmp.get(j + 1).asFloat64();
1369 }
1370
1371 if (!extractGroup(pidsGroup, xtmp, "velocityThres", "velocity threshold parameter for torque control", _njoints, false))
1372 {
1373 registerOptionalParameter("velocityThres", std::to_string(_defaultSettings.torquePidDefaults.velocityThres), true);
1374 for (int j = 0; j<_njoints; j++) pids[j].velocityThres = _defaultSettings.torquePidDefaults.velocityThres;
1375 }
1376 else
1377 {
1378 for (int j = 0; j<_njoints; j++) pids[j].velocityThres = xtmp.get(j + 1).asFloat64();
1379 }
1380
1381
1382 return true;
1383}
1384
1385
1386bool Parser::parseFocGroup(yarp::os::Searchable &config, std::vector<focBasedSpecificInfo_t>& foc_based_info, std::string groupName, std::vector<std::unique_ptr<eomc::ITemperatureSensor>>& temperatureSensorsVector)
1387{
1388 Bottle &focGroup=config.findGroup(groupName);
1389 if (focGroup.isNull() )
1390 {
1391 yError() << "embObjMC BOARD " << _boardname << " detected that Group " << groupName << " is not found in configuration file";
1392 return false;
1393 }
1394
1395 Bottle xtmp;
1396 unsigned int i;
1397 // 1. HasHallSensor
1398 if (!extractGroup(focGroup, xtmp, "HasHallSensor", "HasHallSensor 0/1 ", _njoints))
1399 return false;
1400
1401 for (i = 1; i < xtmp.size(); i++)
1402 foc_based_info[i - 1].hasHallSensor = xtmp.get(i).asInt32() != 0;
1403
1404 // 2. HasTempSensor is depracated, use TemperatureSensorType
1405 if (extractGroup(focGroup, xtmp, "HasTempSensor", "HasTempSensor 0/1 ", _njoints, false))
1406 {
1407 yError() << _boardname << "ATTENTION: HasTempSensor is deprecated. Use TemperatureSensorType (PT100, PT1000, NONE). If not specified, TemperatureSensorType defaults to NONE and the firmware does not check the motor temperature";
1408 return false;
1409 }
1410
1411 if (!extractGroup(focGroup, xtmp, "TemperatureSensorType", "TemperatureSensorType PT100/PT1000/NONE ", _njoints, false))
1412 {
1414 for (i = 0; i < (unsigned)_njoints; i++)
1415 {
1416 foc_based_info[i].hasTempSensor = 0;
1417 temperatureSensorsVector.at(i) = std::make_unique<eomc::TemperatureSensorNONE>();
1418 }
1419 }
1420 else //I have the TemperatureSensorType
1421 {
1422 for (i = 1; i < xtmp.size(); i++)
1423 {
1424 std::string s = xtmp.get(i).asString();
1426 {
1427 foc_based_info[i - 1].hasTempSensor = 1;
1428 temperatureSensorsVector.at(i-1) = std::make_unique<eomc::TemperatureSensorPT100>();
1429
1430 }
1432 {
1433
1434 foc_based_info[i - 1].hasTempSensor = 1;
1435 temperatureSensorsVector.at(i-1) = std::make_unique<eomc::TemperatureSensorPT1000>();
1436 }
1437 else if (isEquals(s, eomc::ParamValues::temperatureSensor::NONE))
1438 {
1439 foc_based_info[i - 1].hasTempSensor = 0;
1440 temperatureSensorsVector.at(i-1) = std::make_unique<eomc::TemperatureSensorNONE>();
1441 }
1442 else
1443 {
1444 yError() << _boardname << "Not supported TemperatureSensorType" << s << "Available TemperatureSensorType are: " << eomc::ParamValues::temperatureSensor::PT100 << ", " << eomc::ParamValues::temperatureSensor::PT1000 << " and " << eomc::ParamValues::temperatureSensor::NONE;
1445 foc_based_info[i - 1].hasTempSensor = 0;
1446 temperatureSensorsVector.at(i-1) = std::make_unique<eomc::TemperatureSensorNONE>();
1447 return false;
1448 }
1449 }
1450 }
1451
1452 // 3. HasRotorEncoder
1453 if (!extractGroup(focGroup, xtmp, "HasRotorEncoder", "HasRotorEncoder 0/1 ", _njoints))
1454 {
1455 return false;
1456 }
1457 else
1458 {
1459
1460 for (i = 1; i < xtmp.size(); i++)
1461 foc_based_info[i - 1].hasRotorEncoder = xtmp.get(i).asInt32() != 0;
1462 }
1463 if (!extractGroup(focGroup, xtmp, "HasRotorEncoderIndex", "HasRotorEncoderIndex 0/1 ", _njoints))
1464 {
1465 return false;
1466 }
1467 else
1468 {
1469 for (i = 1; i < xtmp.size(); i++)
1470 foc_based_info[i - 1].hasRotorEncoderIndex = xtmp.get(i).asInt32() != 0;
1471 }
1472
1473 if (!extractGroup(focGroup, xtmp, "Verbose", "Verbose 0/1 ", _njoints, false))
1474 {
1475 registerOptionalParameter("Verbose", "0 (disabled)", false);
1476 for (i = 0; i < (unsigned)_njoints; i++)
1477 foc_based_info[i].verbose = 0;
1478 }
1479 else
1480 {
1481 for (i = 1; i < xtmp.size(); i++)
1482 foc_based_info[i - 1].verbose = xtmp.get(i).asInt32() != 0;
1483 }
1484
1485 std::vector<int> AutoCalibration (_njoints);
1486 if (!extractGroup(focGroup, xtmp, "AutoCalibration", "AutoCalibration 0/1 ", _njoints, false))
1487 {
1488 registerOptionalParameter("AutoCalibration", "0 (disabled)", false);
1489 for (i = 0; i < (unsigned)_njoints; i++)
1490 AutoCalibration[i] = 0;
1491 }
1492 else
1493 {
1494 for (i = 1; i < xtmp.size(); i++)
1495 AutoCalibration[i - 1] = xtmp.get(i).asInt32();
1496 }
1497
1498 std::vector<float> Kbemf (_njoints);
1499 if (!extractGroup(focGroup, xtmp, "Kbemf", "Kbemf", _njoints, true))
1500 {
1501 yError() << "In " << _boardname << " missing param Kbemf";
1502 return false;
1503 }
1504 else
1505 {
1506 for (i = 1; i < xtmp.size(); i++)
1507 foc_based_info[i-1].kbemf = xtmp.get(i).asFloat32();
1508 }
1509
1510 if (!extractGroup(focGroup, xtmp, "RotorIndexOffset", "RotorIndexOffset", _njoints))
1511 {
1512 return false;
1513 }
1514 else
1515 {
1516 for (i = 1; i < xtmp.size(); i++)
1517 {
1518 if(AutoCalibration[i-1] == 0)
1519 {
1520 foc_based_info[i - 1].rotorIndexOffset = xtmp.get(i).asInt32();
1521 if (foc_based_info[i - 1].rotorIndexOffset <0 || foc_based_info[i - 1].rotorIndexOffset >359)
1522 {
1523 yError() << "In " << _boardname << "joint " << i-1 << ": rotorIndexOffset should be in [0,359] range." ;
1524 return false;
1525 }
1526 }
1527 else
1528 {
1529 yWarning() << "In " << _boardname << "joint " << i-1 << ": motor autocalibration is enabled!!! ATTENTION!!!" ;
1530 foc_based_info[i - 1].rotorIndexOffset = -1;
1531 }
1532 }
1533 }
1534
1535
1536 //Now I verify if rotor encoder hasn't index, then rotor offset must be zero.
1537 for (i = 0; i < (unsigned)_njoints; i++)
1538 {
1539 if((0 == foc_based_info[i].hasRotorEncoderIndex) && (0 != foc_based_info[i].rotorIndexOffset))
1540 {
1541 yError() << "In " << _boardname << "joint " << i << ": inconsistent configuration: if rotor encoder hasn't index then its offset should be 0." ;
1542 return false;
1543 }
1544 }
1545
1546 // Number of motor poles
1547 if (!extractGroup(focGroup, xtmp, "MotorPoles", "MotorPoles", _njoints))
1548 {
1549 return false;
1550 }
1551 else
1552 {
1553 for (i = 1; i < xtmp.size(); i++)
1554 foc_based_info[i - 1].motorPoles = xtmp.get(i).asInt32();
1555 }
1556
1557 if (!extractGroup(focGroup, xtmp, "HasSpeedEncoder", "HasSpeedEncoder 0/1 ", _njoints))
1558 {
1559 yWarning () << "missing param HasSpeedEncoder";
1560 // optional by now
1561 //return false;
1562 }
1563 else
1564 {
1565 for (i = 1; i < xtmp.size(); i++)
1566 foc_based_info[i - 1].hasSpeedEncoder = xtmp.get(i).asInt32() != 0;
1567 }
1568
1569 return true;
1570
1571}
1572
1573
1574
1575//---------------------------------------------------
1576// Parse JointSets functions in config file
1577// --------------------------------------------------
1578bool Parser::parseJointsetCfgGroup(yarp::os::Searchable &config, std::vector<JointsSet> &jsets, std::vector<int> &joint2set)
1579{
1580 Bottle jointsetcfg, xml;
1581
1582 if(!GetGroupBottle(config, "JOINTSET_CFG", jointsetcfg)) return false;
1583
1584
1585 Bottle xtmp;
1586 int numofsets = 0;
1587
1588 if(!extractGroup(jointsetcfg, xtmp, "numberofsets", "number of sets ", 1))
1589 return false;
1590
1591 numofsets = xtmp.get(1).asInt32();
1592 if((0 == numofsets) || (numofsets > _njoints))
1593 {
1594 yError() << "embObjMC BOARD " << _boardname << "Number of jointsets is not correct. it should belong to (1, " << _njoints << ")";
1595 return false;
1596 }
1597
1598 if(!checkAndSetVectorSize(jsets, numofsets, "parseJointsetCfgGroup"))
1599 return false;
1600
1601 if(!checkAndSetVectorSize(joint2set, _njoints, "parseJointsetCfgGroup"))
1602 return false;
1603
1604 for(unsigned int s=0;s<(unsigned)numofsets;s++)
1605 {
1606 char jointset_string[80];
1607 sprintf(jointset_string, "JOINTSET_%d", s);
1608 bool formaterror = false;
1609
1610
1611 Bottle &js_cfg = jointsetcfg.findGroup(jointset_string);
1612 if(js_cfg.isNull())
1613 {
1614 yError() << "embObjMC BOARD " << _boardname << "cannot find " << jointset_string;
1615 return false;
1616 }
1617
1618 //1) id of set
1619 jsets.at(s).id=s;
1620
1621
1622 //2) list of joints
1623 Bottle &b_listofjoints=js_cfg.findGroup("listofjoints", "list of joints");
1624 if (b_listofjoints.isNull())
1625 {
1626 yError() << "embObjMC BOARD " << _boardname << "listofjoints parameter not found";
1627 return false;
1628 }
1629
1630 int numOfJointsInSet = b_listofjoints.size()-1;
1631 if((numOfJointsInSet < 1) || (numOfJointsInSet>_njoints))
1632 {
1633 yError() << "embObjMC BOARD " << _boardname << "numof joints of set " << s << " is not correct";
1634 return false;
1635 }
1636
1637
1638 for (int j = 0; j <numOfJointsInSet; j++)
1639 {
1640 int jointofthisset = b_listofjoints.get(j+1).asInt32();
1641
1642 if((jointofthisset< 0) || (jointofthisset>_njoints))
1643 {
1644 yError() << "embObjMC BOARD " << _boardname << "invalid joint number for set " << s;
1645 return false;
1646 }
1647
1648 jsets.at(s).joints.push_back(jointofthisset);
1649
1650 //2.1) fill map joint to set
1651 joint2set.at(jointofthisset) = s;
1652 }
1653
1654 // 3) constraints
1655 if(!extractGroup(js_cfg, xtmp, "constraint", "type of jointset constraint ", 1))
1656 {
1657 return false;
1658 }
1659
1660 eOmc_jsetconstraint_t constraint;
1661 if(!convert(xtmp.get(1).asString(), constraint, formaterror))
1662 {
1663 return false;
1664 }
1665 jsets.at(s).cfg.constraints.type = constraint;
1666
1667 //param1
1668 if(!extractGroup(js_cfg, xtmp, "param1", "param1 of jointset constraint ", 1))
1669 {
1670 return false;
1671 }
1672 jsets.at(s).cfg.constraints.param1 = (float)xtmp.get(1).asFloat64();
1673
1674 //param2
1675 if(!extractGroup(js_cfg, xtmp, "param2", "param2 of jointset constraint ", 1))
1676 {
1677 return false;
1678 }
1679 jsets.at(s).cfg.constraints.param2 = (float)xtmp.get(1).asFloat64();
1680
1681
1682 }
1683 return true;
1684}
1685
1686
1687//---------------------------------------------------
1688// Parse timeouts in config file
1689// --------------------------------------------------
1690bool Parser::parseTimeoutsGroup(yarp::os::Searchable &config, std::vector<timeouts_t> &timeouts)
1691{
1692
1693 Bottle timeoutsGroup, xtmp;
1694
1695 // for(auto i=0; i<_njoints; i++)
1696 // {
1697 // yError() << "embObjMC BOARD " << _boardname << " joint " << i
1698 // << " _defaultSettings.timeoutsDefaults:"
1699 // << " velocity_ref=" << (double)_defaultSettings.timeoutsDefaults.velocity_ref << " (str: " << std::to_string(_defaultSettings.timeoutsDefaults.velocity_ref) << ")"
1700 // << " current_ref=" << (double)_defaultSettings.timeoutsDefaults.current_ref << " (str: " << std::to_string(_defaultSettings.timeoutsDefaults.current_ref) << ")"
1701 // << " pwm_ref=" << (double)_defaultSettings.timeoutsDefaults.pwm_ref << " (str: " << std::to_string(_defaultSettings.timeoutsDefaults.pwm_ref) << ")"
1702 // << " torque_ref=" << (double)_defaultSettings.timeoutsDefaults.torque_ref << " (str: " << std::to_string(_defaultSettings.timeoutsDefaults.torque_ref) << ")"
1703 // << " torque_fbk=" << (double)_defaultSettings.timeoutsDefaults.torque_fbk << " (str: " << std::to_string(_defaultSettings.timeoutsDefaults.torque_fbk) << ")";
1704 // }
1705
1706 if(!GetGroupBottle(config, "TIMEOUTS", timeoutsGroup, false))
1707 {
1708 //yWarning() << "embObjMC BOARD " << _boardname << " TIMEOUTS group not found in configuration file. Using default values.";
1709 for(auto i=0; i<_njoints; i++)
1710 {
1711 timeouts[i].velocity_ref = _defaultSettings.timeoutsDefaults.velocity_ref;
1712 timeouts[i].current_ref = _defaultSettings.timeoutsDefaults.current_ref;
1713 timeouts[i].pwm_ref = _defaultSettings.timeoutsDefaults.pwm_ref;
1714 timeouts[i].torque_ref = _defaultSettings.timeoutsDefaults.torque_ref;
1715 timeouts[i].torque_fbk = _defaultSettings.timeoutsDefaults.torque_fbk;
1716 }
1717 return true;
1718 }
1719
1720
1721 if (!extractGroup(timeoutsGroup, xtmp, "velocity", "a list of timeout to be used in the vmo control", _njoints, false))
1722 {
1723 registerOptionalParameter("timeout.velocity", std::to_string(_defaultSettings.timeoutsDefaults.velocity_ref), true);
1724 for(auto i=0; i<_njoints; i++) timeouts[i].velocity_ref = _defaultSettings.timeoutsDefaults.velocity_ref;
1725 }
1726 else
1727 {
1728 for(auto i=0; i<_njoints; i++) timeouts[i].velocity_ref = xtmp.get(i+1).asInt32();
1729 }
1730
1731 if (!extractGroup(timeoutsGroup, xtmp, "current", "a list of timeout to be used in the vmo control", _njoints, false))
1732 {
1733 registerOptionalParameter("timeout.current", std::to_string(_defaultSettings.timeoutsDefaults.current_ref), true);
1734 for(auto i=0; i<_njoints; i++) timeouts[i].current_ref = _defaultSettings.timeoutsDefaults.current_ref;
1735 }
1736 else
1737 {
1738 for(auto i=0; i<_njoints; i++) timeouts[i].current_ref = xtmp.get(i+1).asInt32();
1739 }
1740
1741 if (!extractGroup(timeoutsGroup, xtmp, "pwm", "a list of timeout to be used in the vmo control", _njoints, false))
1742 {
1743 registerOptionalParameter("timeout.pwm", std::to_string(_defaultSettings.timeoutsDefaults.pwm_ref), true);
1744 for(auto i=0; i<_njoints; i++) timeouts[i].pwm_ref = _defaultSettings.timeoutsDefaults.pwm_ref;
1745 }
1746 else
1747 {
1748 for(auto i=0; i<_njoints; i++) timeouts[i].pwm_ref = xtmp.get(i+1).asInt32();
1749 }
1750
1751 if (!extractGroup(timeoutsGroup, xtmp, "torque", "a list of timeout to be used in the vmo control", _njoints, false))
1752 {
1753 registerOptionalParameter("timeout.torque", std::to_string(_defaultSettings.timeoutsDefaults.torque_ref), true);
1754 for(auto i=0; i<_njoints; i++) timeouts[i].torque_ref = _defaultSettings.timeoutsDefaults.torque_ref;
1755 }
1756 else
1757 {
1758 for(auto i=0; i<_njoints; i++) timeouts[i].torque_ref = xtmp.get(i+1).asInt32();
1759 }
1760
1761 if (!extractGroup(timeoutsGroup, xtmp, "torque_measure", "a list of timeout to be used in the vmo control", _njoints, false))
1762 {
1763 registerOptionalParameter("timeout.torque_measure", std::to_string(_defaultSettings.timeoutsDefaults.torque_fbk), true);
1764 for(auto i=0; i<_njoints; i++) timeouts[i].torque_fbk = _defaultSettings.timeoutsDefaults.torque_fbk;
1765 }
1766 else
1767 {
1768 for(auto i=0; i<_njoints; i++) timeouts[i].torque_fbk = xtmp.get(i+1).asInt32();
1769 }
1770
1771 return true;
1772}
1773
1774//---------------------------------------------------
1775// Parse current limits in config file
1776// --------------------------------------------------
1777bool Parser::parseCurrentLimits(yarp::os::Searchable &config, std::vector<motorCurrentLimits_t> &currLimits)
1778{
1779 Bottle limits, xtmp;
1780 if(!GetGroupBottle(config, "LIMITS", limits)) return false;
1781
1782 // current limit
1783 if (!extractGroup(limits, xtmp, "motorOverloadCurrents","a list of current limits", _njoints))
1784 return false;
1785
1786 for(auto i=1; i<xtmp.size(); i++) currLimits[i-1].overloadCurrent=xtmp.get(i).asFloat64();
1787
1788 // nominal current
1789 if (!extractGroup(limits, xtmp, "motorNominalCurrents","a list of nominal current limits", _njoints))
1790 return false;
1791 for(auto i=1; i<xtmp.size(); i++) currLimits[i-1].nominalCurrent =xtmp.get(i).asFloat64();
1792
1793 // peak current
1794 if (!extractGroup(limits, xtmp, "motorPeakCurrents","a list of peak current limits", _njoints))
1795 return false;
1796 for(auto i=1; i<xtmp.size(); i++) currLimits[i-1].peakCurrent=xtmp.get(i).asFloat64();
1797
1798 return true;
1799
1800}
1801
1802//---------------------------------------------------
1803// Parse temperature limits in config file
1804// --------------------------------------------------
1805bool Parser::parseTemperatureLimits(yarp::os::Searchable &config, std::vector<temperatureLimits_t> &temperatureLimits)
1806{
1807 Bottle limits, xtmp;
1808 if(!GetGroupBottle(config, "LIMITS", limits)) return false;
1809
1810
1811 // hardware limit
1812 if(!extractGroup(limits, xtmp, "hardwareTemperatureLimits", "a list of temperature limits", _njoints, false))
1813 {
1814 registerOptionalParameter("hardwareTemperatureLimits", std::to_string(_defaultSettings.temperatureLimitsDefaults.hardware), false);
1815 // yWarning("hardwareTemperatureLimits param not found in config file for board %s. Please update robot configuration files or contact https://github.com/robotology/icub-support if needed. Using default values.", _boardname.c_str());
1816 for (auto i = 0; i < (unsigned)_njoints; i++)
1817 {
1818 temperatureLimits[i].hardware = _defaultSettings.temperatureLimitsDefaults.hardware;
1819 temperatureLimits[i].warning = _defaultSettings.temperatureLimitsDefaults.warning;
1820 }
1821 }
1822 else
1823 {
1824 for (auto i = 1; i < xtmp.size(); i++) temperatureLimits[i - 1].hardware = xtmp.get(i).asFloat64();
1825 if (!extractGroup(limits, xtmp, "warningTemperatureLimits", "a list of warning temperature limits", _njoints, false))
1826 {
1827 // warning limit - parsing it only if hardwareTemperatureLimit available
1828 registerOptionalParameter("warningTemperatureLimits", std::to_string(_defaultSettings.temperatureLimitsDefaults.warning), false);
1829 //yWarning("warningTemperatureLimits param not found in config file for board %s. Please update robot configuration files or contact https://github.com/robotology/icub-support if needed. Using default values.", _boardname.c_str());
1830
1831 for (auto i = 0; i < (unsigned)_njoints; i++) temperatureLimits[i].warning = _defaultSettings.temperatureLimitsDefaults.warning;
1832 }
1833 else
1834 {
1835 for (auto i = 1; i < xtmp.size(); i++) temperatureLimits[i - 1].warning = xtmp.get(i).asFloat64();
1836 }
1837 }
1838
1839
1840
1841 //Now I verify that warning temperature limits is < 85% of hardwareTemperatureLimit.
1842 for (auto i = 0; i < (unsigned)_njoints; i++)
1843 {
1844 if(temperatureLimits[i].warning > (0.85 * temperatureLimits[i].hardware))
1845 {
1846 yError() << "In " << _boardname << "joint " << i << ": inconsistent temperature limits. warningTemperatureLimit must be smaller than 85% of hardwareTemperatureLimit" ;
1847 return false;
1848 }
1849 }
1850 return true;
1851}
1852
1853//---------------------------------------------------
1854// Parse joint limits in config file
1855// --------------------------------------------------
1856bool Parser::parseJointsLimits(yarp::os::Searchable &config, std::vector<jointLimits_t> &jointsLimits)
1857{
1858 Bottle limits, xtmp;
1859 if(!GetGroupBottle(config, "LIMITS", limits)) return false;
1860
1861 // max limit
1862 if (!extractGroup(limits, xtmp, "jntPosMax","a list of user maximum angles (in degrees)", _njoints))
1863 return false;
1864 else
1865 for(auto i=1; i<xtmp.size(); i++) jointsLimits[i-1].posMax = xtmp.get(i).asFloat64();
1866
1867 // min limit
1868 if (!extractGroup(limits, xtmp, "jntPosMin","a list of user minimum angles (in degrees)", _njoints))
1869 return false;
1870 else
1871 for(auto i=1; i<xtmp.size(); i++) jointsLimits[i-1].posMin = xtmp.get(i).asFloat64();
1872
1873 // max hardware limit
1874 if (!extractGroup(limits, xtmp, "hardwareJntPosMax","a list of hardware maximum angles (in degrees)", _njoints))
1875 return false;
1876 else
1877 {
1878 for(auto i=1; i<xtmp.size(); i++) jointsLimits[i-1].posHwMax = xtmp.get(i).asFloat64();
1879
1880 //check hardware limits are bigger then user limits
1881 for(auto i=0; i<(unsigned)_njoints; i++)
1882 {
1883 if(jointsLimits[i].posMax > jointsLimits[i].posHwMax)
1884 {
1885 yError() << "embObjMotionControl: user has set a limit bigger then hardware limit!. Please check jntPosMax.";
1886 return false;
1887 }
1888 }
1889 }
1890
1891 // min hardware limit
1892 if (!extractGroup(limits, xtmp, "hardwareJntPosMin","a list of hardware minimum angles (in degrees)", _njoints))
1893 {
1894 return false;
1895 }
1896 else
1897 {
1898 for(auto i=1; i<xtmp.size(); i++) jointsLimits[i-1].posHwMin = xtmp.get(i).asFloat64();
1899
1900 //check hardware limits are bigger then user limits
1901 for(auto i=0; i<(unsigned)_njoints; i++)
1902 {
1903 if(jointsLimits[i].posMin < jointsLimits[i].posHwMin)
1904 {
1905 yError() << "embObjMotionControl: user has set a limit bigger then hardware limit!. Please check jntPosMin.";
1906 return false;
1907 }
1908 }
1909
1910 }
1911
1912 // joint Velocity command max limit
1913 if (!extractGroup(limits, xtmp, "jntVelMax", "a list of maximum velocities for the joints (in degrees/s)", _njoints))
1914 return false;
1915 else
1916 for (auto i = 1; i<xtmp.size(); i++) jointsLimits[i - 1].velMax = xtmp.get(i).asFloat64();
1917
1918 return true;
1919}
1920
1921//---------------------------------------------------
1922// Parse rotor limits in config file
1923// --------------------------------------------------
1924bool Parser::parseRotorsLimits(yarp::os::Searchable &config, std::vector<rotorLimits_t> &rotorsLimits)
1925{
1926 Bottle limits, xtmp;
1927 if(!GetGroupBottle(config, "LIMITS", limits)) return false;
1928
1929 // Rotor max limit
1930 if (!extractGroup(limits, xtmp, "rotorPosMax","a list of maximum rotor angles (in degrees)", _njoints))
1931 return false;
1932 for(auto i=1; i<xtmp.size(); i++) rotorsLimits[i-1].posMax = xtmp.get(i).asFloat64();
1933
1934 // Rotor min limit
1935 if (!extractGroup(limits, xtmp, "rotorPosMin","a list of minimum roto angles (in degrees)", _njoints))
1936 return false;
1937 for(auto i=1; i<xtmp.size(); i++) rotorsLimits[i-1].posMin = xtmp.get(i).asFloat64();
1938
1939 // Motor pwm limit
1940 if (!extractGroup(limits, xtmp, "motorPwmLimit","a list of motor PWM limits", _njoints))
1941 return false;
1942
1943 for(auto i=1; i<xtmp.size(); i++)
1944 {
1945 rotorsLimits[i-1].pwmMax = xtmp.get(i).asFloat64();
1946 if(rotorsLimits[i-1].pwmMax<0)
1947 {
1948 yError() << "In board " << _boardname << " motorPwmLimit should be a positive value";
1949 return false;
1950 }
1951 }
1952 return true;
1953}
1954
1955
1956//---------------------------------------------------
1957// Parse behavioral flags in config file
1958// --------------------------------------------------
1959bool Parser::parseMotioncontrolVersion(yarp::os::Searchable &config, int &version)
1960{
1961 if (!config.findGroup("GENERAL").find("MotioncontrolVersion").isInt32())
1962 {
1963 yError() << "Missing MotioncontrolVersion parameter. RobotInterface cannot start. Please contact icub-support@iit.it";
1964 return false;
1965 }
1966
1967 version = config.findGroup("GENERAL").find("MotioncontrolVersion").asInt32();
1968 return true;
1969
1970}
1971
1972
1973bool Parser::isVerboseEnabled(yarp::os::Searchable &config)
1974{
1975 bool ret = false;
1976 if(!config.findGroup("GENERAL").find("verbose").isBool())
1977 {
1978 yError() << "embObjMotionControl::open() detects that general->verbose bool param is different from accepted values (true / false). Assuming false";
1979 ret = false;
1980 }
1981 else
1982 {
1983 ret = config.findGroup("GENERAL").find("verbose").asBool();
1984 }
1985 return ret;
1986}
1987
1988bool Parser::parseBehaviourFalgs(yarp::os::Searchable &config, bool &useRawEncoderData, bool &pwmIsLimited)
1989{
1990
1991 // Check useRawEncoderData = do not use calibration data!
1992 Value use_raw = config.findGroup("GENERAL").find("useRawEncoderData");
1993
1994 if(use_raw.isNull())
1995 {
1996 useRawEncoderData = false;
1997 }
1998 else
1999 {
2000 if(!use_raw.isBool())
2001 {
2002 yWarning() << "embObjMotionControl::open() detected that useRawEncoderData bool param is different from accepted values (true / false). Assuming false";
2003 useRawEncoderData = false;
2004 }
2005 else
2006 {
2007 useRawEncoderData = use_raw.asBool();
2008 if(useRawEncoderData)
2009 {
2010 yWarning() << "embObjMotionControl::open() detected that it is using raw data from encoders! Be careful See 'useRawEncoderData' param in config file";
2011 yWarning() << "DO NOT USE OR CALIBRATE THE ROBOT IN THIS CONFIGURATION!";
2012 yWarning() << "CHECK IF THE FAULT BUTTON IS PRESSED and press ENTER to continue";
2013 getchar();
2014 }
2015 }
2016 }
2017
2018 // Check useRawEncoderData = do not use calibration data!
2019 Value use_limitedPWM = config.findGroup("GENERAL").find("useLimitedPWM");
2020 if(use_limitedPWM.isNull())
2021 {
2022 pwmIsLimited = false;
2023 }
2024 else
2025 {
2026 if(!use_limitedPWM.isBool())
2027 {
2028 pwmIsLimited = false;
2029 }
2030 else
2031 {
2032 pwmIsLimited = use_limitedPWM.asBool();
2033 }
2034 }
2035
2036 return true;
2037}
2038
2039bool Parser::parseMaintenanceModeGroup(yarp::os::Searchable &config, bool &skipRecalibrationEnabled)
2040{
2041 // Extract group MaintenanceModeGroup
2042 Bottle &maintenanceGroup=config.findGroup("MAINTENANCE");
2043 if (maintenanceGroup.isNull())
2044 {
2045 skipRecalibrationEnabled = false;
2046 return true;
2047 }
2048
2049 // Check skipRecalibrationEnabled = do not recalibrate joint at yarprobointerface restart! Used by low level motion controller
2050 Value skip_recalibration = maintenanceGroup.find("skipRecalibration");
2051 if (skip_recalibration.isNull())
2052 {
2053 skipRecalibrationEnabled = false;
2054 }
2055 else
2056 {
2057 if (!skip_recalibration.isBool())
2058 {
2059 yError() << "embObjMotionControl::open() detected that skipRecalibration bool param is different from accepted values (true / false). Assuming false";
2060 skipRecalibrationEnabled = false;
2061 }
2062 else
2063 {
2064 skipRecalibrationEnabled = skip_recalibration.asBool();
2065 if(skipRecalibrationEnabled)
2066 {
2067 yWarning() << "embObjMotionControl::open() detected that skipRecalibration is requested! Be careful See 'skipRecalibration' param in config file";
2068 yWarning() << "THE ROBOT WILL SKIP THE RECALIBRATION IN THIS CONFIGURATION!";
2069 }
2070 }
2071 }
2072 return true;
2073}
2074
2075//---------------------------------------------------
2076// Parse mechanical info in config file
2077// --------------------------------------------------
2078bool Parser::parseAxisInfo(yarp::os::Searchable &config, int axisMap[], std::vector<axisInfo_t> &axisInfo)
2079{
2080 Bottle general, xtmp;
2081 if(!GetGroupBottle(config, "GENERAL", general)) return false;
2082
2083 if (!extractGroup(general, xtmp, "AxisMap", "a list of reordered indices for the axes", _njoints))
2084 return false;
2085
2086 for (auto i = 1; i < xtmp.size(); i++)
2087 {
2088 int user_joint = xtmp.get(i).asInt32();
2089 if(user_joint>= _njoints)
2090 {
2091 yError() << "embObjMC BOARD " << _boardname << "In AxisMap param: joint " << i-1 << "has been mapped to not-existing joint ("<< user_joint <<"). Here there are only "<< _njoints <<"joints";
2092 return false;
2093 }
2094 axisMap[i-1] = user_joint;
2095 }
2096
2097
2098 if (!extractGroup(general, xtmp, "AxisName", "a list of strings representing the axes names", _njoints))
2099 return false;
2100
2101 //beware: axis name has to be remapped here because they are not set using the toHw() helper function
2102 for (auto i = 1; i < xtmp.size(); i++)
2103 {
2104 int mappedto = axisInfo[i-1].mappedto;
2105 axisInfo[axisMap[i - 1]].name = xtmp.get(i).asString();
2106 }
2107
2108 if (!extractGroup(general, xtmp, "AxisType", "a list of strings representing the axes type (revolute/prismatic)", _njoints))
2109 return false;
2110
2111 //beware: axis type has to be remapped here because they are not set using the toHw() helper function
2112 for (auto i = 1; i < xtmp.size(); i++)
2113 {
2114 string s = xtmp.get(i).asString();
2115 int mappedto = axisInfo[i-1].mappedto;
2116 if (s == eomc::ParamValues::axisType::revolute) axisInfo[axisMap[i - 1]].type = yarp::dev::JointTypeEnum::VOCAB_JOINTTYPE_REVOLUTE;
2117 else if (s == eomc::ParamValues::axisType::prismatic) axisInfo[axisMap[i - 1]].type = yarp::dev::JointTypeEnum::VOCAB_JOINTTYPE_PRISMATIC;
2118 else
2119 {
2120 yError() << "embObjMC BOARD " << _boardname << "Unknown AxisType value" << s.c_str() << "!";
2121 axisInfo[axisMap[i - 1]].type = yarp::dev::JointTypeEnum::VOCAB_JOINTTYPE_UNKNOWN;
2122 return false;
2123 }
2124 }
2125
2126 return true;
2127}
2128
2129bool Parser::parseEncoderFactor(yarp::os::Searchable &config, double encoderFactor[])
2130{
2131 Bottle general, xtmp;
2132 if(!GetGroupBottle(config, "GENERAL", general)) return false;
2133
2134 double tmp_A2E;
2135
2136 // Encoder scales
2137 if (!extractGroup(general, xtmp, "Encoder", "a list of scales for the encoders", _njoints))
2138 return false;
2139
2140 for (auto i = 1; i < xtmp.size(); i++)
2141 {
2142 tmp_A2E = xtmp.get(i).asFloat64();
2143 if (tmp_A2E<0)
2144 {
2145 yWarning() << "embObjMC BOARD " << _boardname << "Encoder parameter should be positive!";
2146 }
2147 encoderFactor[i - 1] = tmp_A2E;
2148 }
2149
2150 return true;
2151}
2152
2153bool Parser::parsefullscalePWM(yarp::os::Searchable &config, double dutycycleToPWM[])
2154{
2155 Bottle general, xtmp;
2156 if(!GetGroupBottle(config, "GENERAL", general)) return false;
2157
2158 double tmpval;
2159
2160 // fullscalePWM
2161 if (!extractGroup(general, xtmp, "fullscalePWM", "a list of scales for the fullscalePWM conversion factor", _njoints))
2162 return false;
2163
2164 for (auto i = 1; i < xtmp.size(); i++)
2165 {
2166 tmpval = xtmp.get(i).asFloat64();
2167 if (tmpval<0)
2168 {
2169 yError() << "embObjMC BOARD " << _boardname << "fullscalePWM parameter should be positive!";
2170 return false;
2171 }
2172 dutycycleToPWM[i - 1] = tmpval / 100.0;
2173 }
2174
2175 return true;
2176}
2177
2178bool Parser::parseAmpsToSensor(yarp::os::Searchable &config, double ampsToSensor[])
2179{
2180 Bottle general, xtmp;
2181 if(!GetGroupBottle(config, "GENERAL", general)) return false;
2182
2183 // ampsToSensor
2184 if (!extractGroup(general, xtmp, "ampsToSensor", "a list of scales for the ampsToSensor conversion factor", _njoints))
2185 return false;
2186
2187 for (auto i = 1; i < xtmp.size(); i++)
2188 {
2189 double tmpval = xtmp.get(i).asFloat64();
2190 if (tmpval<0)
2191 {
2192 yError() << "embObjMC BOARD " << _boardname << "ampsToSensor parameter should be positive!";
2193 return false;
2194 }
2195 ampsToSensor[i - 1] = tmpval;
2196 }
2197
2198 return true;
2199}
2200
2201bool Parser::parseGearboxValues(yarp::os::Searchable &config, double gearbox_M2J[], double gearbox_E2J[])
2202{
2203 Bottle general, xtmp;
2204 if(!GetGroupBottle(config, "GENERAL", general)) return false;
2205
2206 // Gearbox_M2J
2207 if (!extractGroup(general, xtmp, "Gearbox_M2J", "The gearbox reduction ratio", _njoints))
2208 return false;
2209
2210 for (auto i = 1; i < xtmp.size(); i++)
2211 {
2212 gearbox_M2J[i-1] = xtmp.get(i).asFloat64();
2213 if (gearbox_M2J[i-1]==0)
2214 {
2215 yError() << "embObjMC BOARD " << _boardname << "Using a gearbox value = 0 may cause problems! Check your configuration files";
2216 return false;
2217 }
2218 }
2219
2220
2221 //Gearbox_E2J
2222 if (!extractGroup(general, xtmp, "Gearbox_E2J", "The gearbox reduction ratio between encoder and joint", _njoints))
2223 return false;
2224
2225 int test = xtmp.size();
2226 for (auto i = 1; i < xtmp.size(); i++)
2227 {
2228 gearbox_E2J[i-1] = xtmp.get(i).asFloat64();
2229 if (gearbox_E2J[i-1]==0)
2230 {
2231 yError() << "embObjMC BOARD " << _boardname << "Using a gearbox value = 0 may cause problems! Check your configuration files";
2232 return false;
2233 }
2234 }
2235
2236
2237 return true;
2238}
2239
2240bool Parser::parseUseMotorSpeedFbkFlags(yarp::os::Searchable &config, int useMotorSpeedFbk[])
2241{
2242 Bottle general, xtmp;
2243 if(!GetGroupBottle(config, "GENERAL", general)) return false;
2244
2245 if(!extractGroup(general, xtmp, "useMotorSpeedFbk", "Use motor speed feedback", _njoints))
2246 return false;
2247 for (auto i = 1; i < xtmp.size(); i++)
2248 {
2249 useMotorSpeedFbk[i-1] = xtmp.get(i).asInt32();
2250 }
2251
2252 return true;
2253}
2254
2255bool Parser::parseCouplingInfo(yarp::os::Searchable &config, couplingInfo_t &couplingInfo)
2256{
2257 Bottle coupling_bottle, xtmp;
2258 if(!GetGroupBottle(config, "COUPLINGS", coupling_bottle)) return false;
2259
2260 int fixedMatrix4X4Size = 16;
2261 int fixedMatrix4X6Size = 24;
2262 bool formaterror =false;
2263
2264 // matrix J2M
2265 if (!extractGroup(coupling_bottle, xtmp, "matrixJ2M", "matrixJ2M ", fixedMatrix4X4Size))
2266 return false;
2267
2268 if(false == convert(xtmp, couplingInfo.matrixJ2M, formaterror, fixedMatrix4X4Size))
2269 {
2270 yError() << "embObjMC BOARD " << _boardname << " has detected an illegal format for some of the values of CONTROLLER.matrixJ2M";
2271 return false;
2272 }
2273
2274 // matrix E2J
2275 if (!extractGroup(coupling_bottle, xtmp, "matrixE2J", "matrixE2J ", fixedMatrix4X6Size))
2276 return false;
2277
2278 formaterror = false;
2279 if(false == convert(xtmp, couplingInfo.matrixE2J, formaterror, fixedMatrix4X6Size))
2280 {
2281 yError() << "embObjMC BOARD " << _boardname << " has detected an illegal format for some of the values of CONTROLLER.matrixE2J";
2282 return false;
2283 }
2284
2285 // matrix M2J
2286 if (!extractGroup(coupling_bottle, xtmp, "matrixM2J", "matrixM2J ", fixedMatrix4X4Size))
2287 return false;
2288
2289 formaterror = false;
2290 if( false == convert(xtmp, couplingInfo.matrixM2J, formaterror, fixedMatrix4X4Size))
2291 {
2292 yError() << "embObjMC BOARD " << _boardname << " has detected an illegal format for some of the values of CONTROLLER.matrixM2J";
2293 return false;
2294 }
2295
2296 return true;
2297}
2298
2299//---------------------------------------------------
2300// Parse other motion control info in config file
2301// --------------------------------------------------
2302bool Parser::parseDeadzoneValue(yarp::os::Searchable &config, double deadzone[], bool *found)
2303{
2304 Bottle xtmp;
2305
2306 Bottle general = config.findGroup("OTHER_CONTROL_PARAMETERS");
2307 if (general.isNull())
2308 {
2309 // yWarning() << "embObjMC BOARD " << _boardname << "Missing OTHER_CONTROL_PARAMETERS.DeadZone parameter. I'll use default value. (see documentation for more datails)";
2310 *found = false;
2311 return true;
2312 }
2313
2314 // DeadZone
2315 if (!extractGroup(general, xtmp, "deadZone", "The deadzone of joint", _njoints, false))
2316 {
2317 *found = false;
2318 return true;
2319 }
2320
2321 *found = true;
2322 for (auto i = 1; i < xtmp.size(); i++) deadzone[i-1] = xtmp.get(i).asFloat64();
2323
2324 return true;
2325}
2326
2327bool Parser::parseKalmanFilterParams(yarp::os::Searchable &config, std::vector<kalmanFilterParams_t> &kalmanFilterParams)
2328{
2329
2330 Bottle general;
2331 if(!GetGroupBottle(config, "KALMAN_FILTER", general, false/*Not mandatory*/))
2332 {
2333 registerOptionalParameter("kalmanFilter", "disabled", false);
2334
2335 // if you don't specify the Kalman Filter group disable the kalmam filter for all the joints.
2336 // Get the default values structure from singleton
2337
2338 for(int j=0; j<_njoints; j++)
2339 {
2340 kalmanFilterParams[j].enabled = _defaultSettings.controlParametersDefaults.kalmanFilterParams.enabled;
2341 kalmanFilterParams[j].x0 = _defaultSettings.controlParametersDefaults.kalmanFilterParams.x0;
2342 kalmanFilterParams[j].Q = _defaultSettings.controlParametersDefaults.kalmanFilterParams.Q;
2343 kalmanFilterParams[j].R = _defaultSettings.controlParametersDefaults.kalmanFilterParams.R;
2344 kalmanFilterParams[j].P0 = _defaultSettings.controlParametersDefaults.kalmanFilterParams.P0;
2345 }
2346 return true;
2347 }
2348 else
2349 {
2350 Bottle xtmp;
2351
2352 // kalmanFilterEnabled
2353 if (!extractGroup(general, xtmp, "kalmanFilterEnabled", "kalman filter of joint: ", _njoints, true))
2354 return false;
2355 for (int j = 0; j<_njoints; j++) kalmanFilterParams[j].enabled = xtmp.get(j + 1).asBool();
2356
2357 // x0_0
2358 if (!extractGroup(general, xtmp, "x0", "Initial state x0 of kalman filter of joint: ", _njoints, true))
2359 return false;
2360 for (int j = 0; j<_njoints; j++) kalmanFilterParams[j].x0.at(0) = xtmp.get(j + 1).asFloat32();
2361
2362 // x0_1
2363 if (!extractGroup(general, xtmp, "x1", "Initial state x1 of kalman filter of joint: ", _njoints, true))
2364 return false;
2365 for (int j = 0; j<_njoints; j++) kalmanFilterParams[j].x0.at(1) = xtmp.get(j + 1).asFloat32();
2366
2367 // x0_2
2368 if (!extractGroup(general, xtmp, "x2", "Initial state x2 of kalman filter of joint: ", _njoints, true))
2369 return false;
2370 for (int j = 0; j<_njoints; j++) kalmanFilterParams[j].x0.at(2) = xtmp.get(j + 1).asFloat32();
2371
2372 // Q0
2373 if (!extractGroup(general, xtmp, "Q0", "Process Q0 noise covariance matrix of kalman filter of joint: ", _njoints, true))
2374 return false;
2375 for (int j = 0; j<_njoints; j++) kalmanFilterParams[j].Q.at(0) = xtmp.get(j + 1).asFloat32();
2376
2377 // Q1
2378 if (!extractGroup(general, xtmp, "Q1", "Process Q1 noise covariance matrix of kalman filter of joint: ", _njoints, true))
2379 return false;
2380 for (int j = 0; j<_njoints; j++) kalmanFilterParams[j].Q.at(1) = xtmp.get(j + 1).asFloat32();
2381
2382 // Q2
2383 if (!extractGroup(general, xtmp, "Q2", "Process Q2 noise covariance matrix of kalman filter of joint: ", _njoints, true))
2384 return false;
2385 for (int j = 0; j<_njoints; j++) kalmanFilterParams[j].Q.at(2) = xtmp.get(j + 1).asFloat32();
2386
2387 // R
2388 if (!extractGroup(general, xtmp, "R", "Measurement noise covariance matrix of kalman filter for joint: ", _njoints, true))
2389 return false;
2390 for (int j = 0; j<_njoints; j++) kalmanFilterParams[j].R = xtmp.get(j + 1).asFloat32();
2391
2392 // P0
2393 if (!extractGroup(general, xtmp, "P0", "Initial state estimation error covariance matrix of kalman filter of joint: ", _njoints, true))
2394 return false;
2395 for (int j = 0; j<_njoints; j++) kalmanFilterParams[j].P0 = xtmp.get(j + 1).asFloat32();
2396 }
2397
2398 return true;
2399}
2400
2401bool Parser::parseImpedanceGroup(yarp::os::Searchable &config,std::vector<impedanceParameters_t> &impedance)
2402{
2403 Bottle impedanceGroup, xtmp;
2404 if(!GetGroupBottle(config, "IMPEDANCE", impedanceGroup)) return false;
2405
2406
2407 if (!extractGroup(impedanceGroup, xtmp, "stiffness", "stiffness parameter", _njoints))
2408 return false;
2409
2410 for (auto j=0; j<_njoints; j++)
2411 impedance[j].stiffness = xtmp.get(j+1).asFloat64();
2412
2413 if (!extractGroup(impedanceGroup, xtmp, "damping", "damping parameter", _njoints))
2414 return false;
2415
2416 for (auto j=0; j<_njoints; j++)
2417 impedance[j].damping = xtmp.get(j+1).asFloat64();
2418
2419 return true;
2420}
2421
2422bool Parser::parseLugreGroup(yarp::os::Searchable &config,std::vector<lugreParameters_t> &lugre)
2423{
2424 Bottle lugreGroup, xtmp;
2425 if(!GetGroupBottle(config, "LUGRE", lugreGroup, false/*Not mandatory*/))
2426 {
2427
2428 for (int j = 0; j<_njoints; ++j)
2429 {
2430 lugre[j].Km = _defaultSettings.controlParametersDefaults.lugre.Km;
2431 }
2432
2433 return true;
2434 }
2435
2436 if (!extractGroup(lugreGroup, xtmp, "Km", "torque constant parameter", _njoints))
2437 return false;
2438
2439 for (auto j=0; j<_njoints; j++)
2440 lugre[j].Km = xtmp.get(j+1).asFloat64();
2441
2442 if (!extractGroup(lugreGroup, xtmp, "Kw", "viscous friction parameter", _njoints))
2443 return false;
2444
2445 for (auto j=0; j<_njoints; j++)
2446 lugre[j].Kw = xtmp.get(j+1).asFloat64();
2447
2448 if (!extractGroup(lugreGroup, xtmp, "S0", "hysteresis position parameter", _njoints))
2449 return false;
2450
2451 for (auto j=0; j<_njoints; j++)
2452 lugre[j].S0 = xtmp.get(j+1).asFloat64();
2453
2454 if (!extractGroup(lugreGroup, xtmp, "S1", "hysteresis velocity parameter", _njoints))
2455 return false;
2456
2457 for (auto j=0; j<_njoints; j++)
2458 lugre[j].S1 = xtmp.get(j+1).asFloat64();
2459
2460 if (!extractGroup(lugreGroup, xtmp, "Vth", "velocity threshold parameter", _njoints))
2461 return false;
2462
2463 for (auto j=0; j<_njoints; j++)
2464 lugre[j].Vth = xtmp.get(j+1).asFloat64();
2465
2466 if (!extractGroup(lugreGroup, xtmp, "Fc_pos", "Coulomb force parameter (forward)", _njoints))
2467 return false;
2468
2469 for (auto j=0; j<_njoints; j++)
2470 lugre[j].Fc_pos = xtmp.get(j+1).asFloat64();
2471
2472 if (!extractGroup(lugreGroup, xtmp, "Fc_neg", "Coulomb force parameter (backward)", _njoints))
2473 return false;
2474
2475 for (auto j=0; j<_njoints; j++)
2476 lugre[j].Fc_neg = xtmp.get(j+1).asFloat64();
2477
2478 if (!extractGroup(lugreGroup, xtmp, "Fs_pos", "Stribeck force parameter (forward)", _njoints))
2479 return false;
2480
2481 for (auto j=0; j<_njoints; j++)
2482 lugre[j].Fs_pos = xtmp.get(j+1).asFloat64();
2483
2484 if (!extractGroup(lugreGroup, xtmp, "Fs_neg", "Stribeck force parameter (backward)", _njoints))
2485 return false;
2486
2487 for (auto j=0; j<_njoints; j++)
2488 lugre[j].Fs_neg = xtmp.get(j+1).asFloat64();
2489
2490 return true;
2491
2492}
2493
2494
2495
2496
2497bool Parser::convert(std::string const &fromstring, eOmc_jsetconstraint_t &jsetconstraint, bool& formaterror)
2498{
2499 const char *t = fromstring.c_str();
2500
2501 eObool_t usecompactstring = eobool_false;
2502 jsetconstraint = eomc_string2jsetconstraint(t, usecompactstring);
2503
2504 if(eomc_jsetconstraint_unknown == jsetconstraint)
2505 {
2506 usecompactstring = eobool_true;
2507 jsetconstraint = eomc_string2jsetconstraint(t, usecompactstring);
2508 }
2509
2510 if(eomc_jsetconstraint_unknown == jsetconstraint)
2511 {
2512 yError() << "embObjMC BOARD " << _boardname << "String" << t << "cannot be converted into a proper eOmc_jsetconstraint_t";
2513 formaterror = true;
2514 return false;
2515 }
2516
2517 return true;
2518}
2519
2520bool Parser::convert(Bottle &bottle, vector<double> &matrix, bool &formaterror, int targetsize)
2521{
2522 matrix.resize(0);
2523
2524 int tmp = bottle.size();
2525 int sizeofmatrix = tmp - 1; // first position of bottle contains the tag "matrix"
2526
2527 // check if there are really the target number of elements in matrix.
2528 if(targetsize != sizeofmatrix)
2529 {
2530 yError() << "embObjMC BOARD " << _boardname << " in converting string do matrix.In the matrix there are not" << targetsize << "elements";
2531 return false;
2532 }
2533
2534 formaterror = false;
2535 for(int i=0; i<sizeofmatrix; i++)
2536 {
2537 double item = 0;
2538
2539 // ok, i use the standard converter ... but what if it is not a double format? so far we dont check.
2540 item = bottle.get(i+1).asFloat64();
2541 matrix.push_back(item);
2542 }
2543
2544 // in here we could decide to return false if any previous conversion function has returned error
2545
2546 return true;
2547}
2548
2549
2550
2554// void Parser::debugUtil_printControlLaws(void)
2555// {
2556// //////// debug prints
2557// yError() << "position control law: ";
2558// for(int x=0; x<_njoints; x++)
2559// {
2560// yError() << " - j " << x << _positionControlLaw[x].c_str();
2561// }
2562
2563// yError() << "velocity control law: ";
2564// for(int x=0; x<_njoints; x++)
2565// {
2566// yError() << "- j " << x << _velocityControlLaw[x].c_str();
2567// }
2568
2569
2570// yError() << "torque control law: ";
2571// for(int x=0; x<_njoints; x++)
2572// {
2573// yError() << " - j " << x << _torqueControlLaw[x].c_str();
2574// }
2575// //////end
2576
2577// }
2578
2579/*
2580void PidInfo::dumpdata(void)
2581 if(!GetGroupBottle(config, "LUGRE", lugreGroup, false))
2582
2583 cout << "Is enabled " << enabled;
2584 cout << ". Username pid selected is " << usernamePidSelected;
2585 switch(controlLaw)
2586 {
2587 case PidAlgo_simple:
2588 cout << ". Control law is " << "PidAlgo_simple";
2589 break;
2590
2591 case PIdAlgo_velocityInnerLoop:
2592 cout << ". Control law is " << "PIdAlgo_velocityInnerLoop";
2593 break;
2594
2595 case PidAlgo_currentInnerLoop:
2596 cout << ". Control law is " << "PidAlgo_currentInnerLoop";
2597 break;
2598 default :
2599 cout << ". Control law is " << "unknown";
2600 }
2601
2602 cout << ". PID fbk Unit type is " << (int)fbk_PidUnits;
2603 cout << ". PID out Unit type is " << (int)out_PidUnits;
2604
2605 cout << " kp is " << pid.kp;
2606 cout << endl;
2607
2608}
2609*/
2611{
2612 switch(cfg.constraints.type)
2613 {
2614 case eomc_jsetconstraint_none:
2615 cout << "constraint is " << "eomc_jsetconstraint_none";
2616 break;
2617 case eomc_jsetconstraint_cerhand:
2618 cout << "constraint is " << "eomc_jsetconstraint_cerhand";
2619 break;
2620 case eomc_jsetconstraint_trifid:
2621 cout << "constraint is " << "eomc_jsetconstraint_trifid";
2622 break;
2623 default :
2624 cout << ". constraint is " << "unknown";
2625 }
2626
2627 cout << " param1="<< cfg.constraints.param1 << " param2=" << cfg.constraints.param2 << endl;
2628
2629}
2630
2631// bool Parser::checkJointTypes(PidInfo *pids, const std::string &pid_type)
2632// {
2633// //Here we check that all joints have same type units in order to create pid_type helper with correct factor.
2634
2635// int firstjoint = -1;
2636
2637// // verify if pid type is torque or some other
2638// if(pid_type == "TORQUE")
2639// {
2640// // since we are working with a torque pid, we first cast it as such
2641// // this allows the loop to correctly point to the corresponding memory
2642// TrqPidInfo* trq_pids = (TrqPidInfo*) pids;
2643
2644// for(int i=0; i<_njoints; i++)
2645// {
2646// // if we already had an enabled PID, compare with current one
2647// if(firstjoint != -1 && !checkSinglePid(trq_pids[firstjoint], trq_pids[i], firstjoint, i, pid_type))
2648// {
2649// return false;
2650// }
2651// // if we haven't found an enabled PID yet, and this one is enabled, save it
2652// if(firstjoint == -1 && trq_pids[i].enabled)
2653// {
2654// firstjoint = i;
2655// }
2656// }
2657// }
2658// else
2659// {
2660// for(int i=0; i<_njoints; i++)
2661// {
2662// // if we already had an enabled PID, compare with current one
2663// if(firstjoint != -1 && !checkSinglePid(pids[firstjoint], pids[i], firstjoint, i, pid_type))
2664// {
2665// return false;
2666// }
2667// // if we haven't found an enabled PID yet, and this one is enabled, save it
2668// if(firstjoint == -1 && pids[i].enabled)
2669// {
2670// firstjoint = i;
2671// }
2672// }
2673// }
2674
2675// return true;
2676// }
2677
2678// bool Parser::checkSinglePid(PidInfo &firstPid, PidInfo &currentPid, const int &firstjoint, const int &currentjoint, const std::string &pid_type)
2679// {
2680// // check if the PID we are checking is enabled
2681// if(currentPid.enabled)
2682// {
2683// // if it has different unit types from the previous enabled PIDs
2684// if(firstPid.fbk_PidUnits != currentPid.fbk_PidUnits ||
2685// firstPid.out_PidUnits != currentPid.out_PidUnits)
2686// {
2687// yError() << "embObjMC BOARD " << _boardname << "all joints with " << pid_type << " enabled should have same controlunits type. Joint " << firstjoint << " differs from joint " << currentjoint;
2688// return false;
2689// }
2690// }
2691// return true;
2692// }
2693
2694void Parser::registerOptionalParameter(std::string_view paramName, std::string_view defaultValue, bool wasUsed)
2695{
2696 // Create a new entry for this optional parameter
2697 OptionalParameterInfo_t param_info;
2698 param_info.parameterName = paramName;
2699 param_info.defaultValue = defaultValue;
2700 param_info.wasUsed = wasUsed;
2701
2702 // Add to tracking vector
2703 _optionalParametersUsed.push_back(param_info);
2704}
2705
2707{
2708 if(_optionalParametersUsed.empty())
2709 {
2710 return;
2711 }
2712
2713 // Build the table header
2714 std::string line;
2715 line = "\n";
2716 line += "╔═══════════════════════════════════════════════════════════════════╗\n";
2717 size_t pad = (_boardname.length() < 16) ? (16 - _boardname.length()) : 0;
2718 line += "║ OPTIONAL PARAMETERS SUMMARY FOR BOARD: " + _boardname + std::string(pad, ' ') + "║\n";
2719 line += "╠═══════════════════════════════════════════════════════════════════╣\n";
2720 line += "║ Parameter Name │ Used │ Default Value │\n";
2721 line += "╟───────────────────────────────┼──────┼────────────────────────────╢\n";
2722
2723 // Add rows for each parameter
2724 for(const auto &param : _optionalParametersUsed)
2725 {
2726 // Parameter name (max 31 chars)
2727 std::string param_name = param.parameterName;
2728 if(param_name.length() > 31) param_name = param_name.substr(0, 28) + "...";
2729 std::string param_name_padded = param_name + std::string(31 - param_name.length(), ' ');
2730
2731 // Used flag
2732 std::string used_str = param.wasUsed ? "YES" : "NO";
2733 std::string used_padded = used_str + std::string(4 - used_str.length(), ' ');
2734
2735 // Default value (max 27 chars)
2736 std::string default_val = param.defaultValue;
2737 if(default_val.length() > 27) default_val = default_val.substr(0, 24) + "...";
2738 std::string default_val_padded = default_val + std::string(27 - default_val.length(), ' ');
2739
2740 line += "║ " + param_name_padded + "│ " + used_padded + " │ " + default_val_padded + "│\n";
2741 }
2742
2743 // Close the table
2744 line += "╚═══════════════════════════════════════════════════════════════════╝\n";
2745
2746 // Print using yCWarning as requested
2747 yWarning() << line.c_str();
2748
2749}
2750
2752{
2753 _optionalParametersUsed.clear();
2754}
2755
2756
2757
2758
2759
bool parseJointsetCfgGroup(yarp::os::Searchable &config, std::vector< JointsSet > &jsets, std::vector< int > &jointtoset)
bool parseJointsLimits(yarp::os::Searchable &config, std::vector< jointLimits_t > &jointsLimits)
bool parseUseMotorSpeedFbkFlags(yarp::os::Searchable &config, int useMotorSpeedFbk[])
bool parseMaintenanceModeGroup(yarp::os::Searchable &config, bool &skipRecalibrationEnabled)
bool parseAmpsToSensor(yarp::os::Searchable &config, double ampsToSensor[])
bool parseFocGroup(yarp::os::Searchable &config, std::vector< focBasedSpecificInfo_t > &foc_based_info, std::string groupName, std::vector< std::unique_ptr< eomc::ITemperatureSensor > > &temperatureSensorsVector)
Parser(int numofjoints, std::string boardname)
bool parseMotioncontrolVersion(yarp::os::Searchable &config, int &version)
bool parseLugreGroup(yarp::os::Searchable &config, std::vector< lugreParameters_t > &lugre)
bool parseAxisInfo(yarp::os::Searchable &config, int axisMap[], std::vector< axisInfo_t > &axisInfo)
void registerOptionalParameter(std::string_view paramName, std::string_view defaultValue, bool wasUsed)
bool parseTimeoutsGroup(yarp::os::Searchable &config, std::vector< timeouts_t > &timeouts)
bool parseCouplingInfo(yarp::os::Searchable &config, couplingInfo_t &couplingInfo)
bool parseTemperatureLimits(yarp::os::Searchable &config, std::vector< temperatureLimits_t > &temperatureLimits)
bool parsePids(yarp::os::Searchable &config, PidControllers_t &pids, bool lowLevPidisMandatory)
bool parseDeadzoneValue(yarp::os::Searchable &config, double deadzone[], bool *found)
bool isVerboseEnabled(yarp::os::Searchable &config)
bool parseCurrentLimits(yarp::os::Searchable &config, std::vector< motorCurrentLimits_t > &currLimits)
bool parseEncoderFactor(yarp::os::Searchable &config, double encoderFactor[])
bool parsefullscalePWM(yarp::os::Searchable &config, double dutycycleToPWM[])
bool parseRotorsLimits(yarp::os::Searchable &config, std::vector< rotorLimits_t > &rotorsLimits)
bool parseImpedanceGroup(yarp::os::Searchable &config, std::vector< impedanceParameters_t > &impedance)
bool parseKalmanFilterParams(yarp::os::Searchable &config, std::vector< kalmanFilterParams_t > &kalmanFilterParams)
bool parseGearboxValues(yarp::os::Searchable &config, double gearbox_M2J[], double gearbox_E2J[])
bool parseBehaviourFalgs(yarp::os::Searchable &config, bool &useRawEncoderData, bool &pwmIsLimited)
void loadStrings(std::vector< std::string > &dest, const yarp::os::Bottle &source)
bool getCtrlOutTypeFromConfig(const std::string &configName, eOmc_ctrl_out_type_t &outType, std::string &description)
A map of control output types from configuration strings to their enum values and descriptions.
static const std::map< std::string, CtrlOutTypeInfo > ctrlOutTypeConfigMap
static constexpr std::string_view prismatic
static constexpr std::string_view NONE_STR
Default (fallback) values for embObjMotionControl configuration parameters.
out
Definition sine.m:8
eOmc_ctrl_out_type_t enumValue
std::string description