SwitchCoordinationMode.cpp
Go to the documentation of this file.
2
3#include <armarx/control/common/common.aron.generated.h>
4#include <armarx/control/common/control_law/aron/TaskspaceControllerConfig.aron.generated.h>
5#include <armarx/control/common/coordination/aron/CoordinationConfig.aron.generated.h>
9
11{
12 using namespace armarx::control::common;
13
15 ::armarx::skills::SimpleSpecializedSkill<ParamType>(GetSkillDescription()), srv_(srv)
16 {
17 }
18
20 SwitchCoordinationMode::exit(const SpecializedExitInput& in)
21 {
22
24 }
25
27 SwitchCoordinationMode::main(const SpecializedMainInput& in)
28 {
29 ARMARX_INFO << "Switching coordination mode";
31
32 const auto& robotUnit =
34
35 auto names = robotUnit->getActivatedNJointControllerNames();
36 ARMARX_INFO << "Activated controllers: \n" << common::sVecToString(names);
37
38 auto candidateCtrlNames = filterControllerNames(names, in.parameters.controllerNamePattern);
39
41 if (candidateCtrlNames.size() > 1)
42 {
43 ARMARX_WARNING << "There are more than one controllers matching your name pattern: "
44 << common::sVecToString(candidateCtrlNames);
45 return MakeFailedResult();
46 }
47
48 if (candidateCtrlNames.empty())
49 {
50 ARMARX_WARNING << "No controllers matching your pattern "
51 << in.parameters.controllerNamePattern
52 << " found. Available: " << common::sVecToString(names);
53 return MakeFailedResult();
54 }
55
56 const std::string candidateName = candidateCtrlNames[0];
57 ARMARX_INFO << "Found matching controller " << candidateName;
58 const bool enable = in.parameters.enable;
59
60 auto controller = Ice::checkedCast<armarx::control::TSColAvoidCtrlInterfacePrx>(
61 robotUnit->getNJointController(candidateName));
62
63 if (enable)
64 {
65 auto cfgCoord = coordination::arondto::SyncModeConfig();
66 {
67 const armarx::PackagePath configPath(in.parameters.configPackageName,
68 in.parameters.configRelPath);
69 auto filename = configPath.toSystemPath();
70 ARMARX_INFO << "using coordination config: " << filename;
71 ARMARX_CHECK(std::filesystem::is_regular_file(filename))
72 << filename << " does not exists";
73
74 std::ifstream ifs{filename};
75
76 nlohmann::json jsonConfig;
77 ifs >> jsonConfig;
78
79 armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck reader;
80 cfgCoord.read(reader, jsonConfig);
81 }
82
83 auto cfgDTO = controller->getConfig();
84 auto cfg = control_law::arondto::TSMixImpVelColConfigDict();
85 cfg.fromAron(cfgDTO);
86 auto& desiredPose = cfgCoord.desiredPose;
87 auto& poseL = cfg.limbs.at("LeftArm").desiredPose;
88 auto& poseR = cfg.limbs.at("RightArm").desiredPose;
89 if (cfgCoord.leadershipMode == coordination::arondto::LeadershipMode::object_as_leader)
90 {
91 desiredPose = poseL;
92 desiredPose.block<3, 1>(0, 3) = 0.5 * (getPos(poseL) + getPos(poseR));
93 }
94 else if (cfgCoord.leadershipMode ==
95 coordination::arondto::LeadershipMode::left_as_leader)
96 {
97 desiredPose = poseL;
98 }
99 else if (cfgCoord.leadershipMode ==
100 coordination::arondto::LeadershipMode::right_as_leader)
101 {
102 desiredPose = poseR;
103 }
104 if (cfgCoord.followerFrameMode == armarx::control::common::arondto::PoseFrameMode::root)
105 {
106 }
107 else
108 {
110 << "The pose target computation only support 'root' mode. Please fix the code "
111 "before. Now the followerFrameMode is set to 'PoseFrameMode::root'.";
112 cfgCoord.followerFrameMode = armarx::control::common::arondto::PoseFrameMode::root;
113 }
114 ARMARX_INFO << "Coordinator: setting leader " << VAROUT(desiredPose);
115 for (auto& pair : cfg.limbs)
116 {
117 ARMARX_INFO << "Disableing force torque guard for " << pair.first;
118 controller->enableSafeGuardForceTorque(pair.first, false, false);
119 }
120 controller->useCoordinator("", cfgCoord.toAronDTO());
121 }
122 else
123 {
124 controller->disableCoordinator();
125 }
126 return MakeSucceededResult();
127 }
128
129 std::vector<std::string>
130 SwitchCoordinationMode::filterControllerNames(const std::vector<std::string>& controllerNames,
131 const std::vector<std::string>& patterns)
132 {
133 if (patterns.empty())
134 {
135 return controllerNames;
136 }
137
138 std::vector<std::string> matches;
139 // No reserve() needed here since we expect very few results.
140
141 for (const auto& name : controllerNames)
142 {
143 bool allPatternsFound = true;
144
145 for (const auto& pattern : patterns)
146 {
147 if (name.find(pattern) == std::string::npos)
148 {
149 allPatternsFound = false;
150 break;
151 }
152 }
153
154 if (allPatternsFound)
155 {
156 matches.push_back(name);
157 }
158 }
159
160 return matches;
161 }
162
163
164} // namespace armarx::control::skills::skills
#define VAROUT(x)
armarx::plugins::RobotUnitComponentPlugin & getRobotUnitPlugin()
std::vector< std::string > filterControllerNames(const std::vector< std::string > &controllerNames, const std::vector< std::string > &patterns)
static MainResult MakeSucceededResult(aron::data::DictPtr data=nullptr)
Definition Skill.cpp:413
virtual MainResult main()
Override this method with the actual implementation.
Definition Skill.cpp:542
virtual ExitResult exit()
Override this method with the actual implementation.
Definition Skill.cpp:535
static MainResult MakeFailedResult(aron::data::DictPtr data=nullptr)
Definition Skill.cpp:422
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::string sVecToString(const std::vector< std::string > &vec, const std::string &delimiter)
Definition utils.cpp:439
Eigen::Block< Eigen::Matrix4f, 3, 1 > getPos(Eigen::Matrix4f &matrix)
Definition utils.cpp:301
This file offers overloads of toIce() and fromIce() functions for STL container types.
This file is part of ArmarX.
::armarx::control::client::ComponentPlugin * controlComponentPluginUser
A result struct for skill exit function.
Definition Skill.h:69
A result struct for th main method of a skill.
Definition Skill.h:62
#define ARMARX_TRACE
Definition trace.h:77