Follower.cpp
Go to the documentation of this file.
1 #include "Follower.h"
2 
3 #include <Eigen/Core>
4 #include <Eigen/Geometry>
5 
6 #include <SimoxUtility/math/convert.h>
7 #include <VirtualRobot/Robot.h>
8 #include <VirtualRobot/RobotNodeSet.h>
9 #include <VirtualRobot/VirtualRobot.h>
10 
12 
13 #include <RobotAPI/interface/units/RobotUnit/TaskSpaceActiveImpedanceControl.h>
14 
20 #include <armarx/control/skills/skills/platform_follower_controller/aron/FollowerParams.aron.generated.h>
21 
23 {
24 
27  {
28  ParamType defaultParams;
29  defaultParams.platformControlConfig.nodeSetNameLeft = "LeftArm";
30  defaultParams.platformControlConfig.nodeSetNameRight = "RightArm";
31  defaultParams.platformControlConfig.kPos = 12;
32  defaultParams.platformControlConfig.kAngle = 8;
33  defaultParams.platformControlConfig.dPos = 0;
34  defaultParams.platformControlConfig.dAngle = 0;
35 
36  return ::armarx::skills::SkillDescription{.skillId = {.skillName = "Follower"},
37  .description =
38  "TODO: Description of skill Follower.",
39  .rootProfileDefaults = defaultParams.toAron(),
40  .timeout = ::armarx::Duration::Days(1),
41  .parametersType = ParamType::ToAronType()};
42  }
43 
44  Follower::Follower(const Services& services, const Parameters& parameters) :
45  ::armarx::skills::SimpleSpecializedSkill<ParamType>(GetSkillDescription()),
46  // ::armarx::skills::mixin::RobotReadingSkillMixin(services.mns),
47  services_(services),
48  parameters_(parameters)
49  {
51 
52  // auto robotUnit = controlComponentPlugin->getRobotUnitPlugin().getRobotUnit();
53  // robotUnit->loadLibFromPackage("armarx_control", "armarx_control_njoint_mp_controller");
54  // robotUnit->loadLibFromPackage("armarx_control", "armarx_control_njoint_controller");
55  // robotUnit->loadLibFromPackage("eurobin_wp2_skills", "eurobin_wp2_skills_platform_follower_controller");
56  }
57 
58  /*
59  bool
60  Follower::isAvailable(const SpecializedInitInput&) const
61  {
62  return true;
63  }
64  */
65 
66  /*
67  ::armarx::skills::Skill::InitResult
68  Follower::init(const SpecializedInitInput&)
69  {
70  return InitResult{.status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
71  }
72  */
73 
74 
76  Follower::main(const SpecializedMainInput& in)
77  {
78  // robotReader.connect();
79  VirtualRobot::RobotPtr robot = services_.robotReader.getRobotWaiting(parameters_.robotName);
82 
83  VirtualRobot::RobotNodeSetPtr rnsRight = robot->getRobotNodeSet("RightArm"); // TODO
84  VirtualRobot::RobotNodeSetPtr rnsLeft = robot->getRobotNodeSet("LeftArm"); // TODO
85 
86  std::map<std::string,
89  controllers;
90  for (const auto& rns : {rnsRight, rnsLeft})
91  {
92  /// create controllers with controller builder
94 
95  builder.withNodeSet(rns->getName()).withNamePrefix(rns->getName());
96 
97  /// load predefined config from file
98  {
99  if (in.parameters.jointConfigFileName.empty())
100  {
101  const std::string controllerClassName =
104  const armarx::PackagePath configPath(
105  "armarx_control",
106  "controller_config/NJointTaskspaceImpedanceController/"
107  "bimanual_transport_default.json");
108  ARMARX_IMPORTANT << "Using config `" << configPath.toSystemPath() << "`.";
109  ARMARX_CHECK(std::filesystem::exists(configPath.toSystemPath()))
110  << configPath.toSystemPath();
111 
112  builder.withConfig(configPath.toSystemPath());
113  }
114  else
115  {
116  builder.withConfig(in.parameters.jointConfigFileName);
117  }
118 
119  ARMARX_CHECK(builder.config().limbs.count("LeftArm") > 0);
120  ARMARX_CHECK(builder.config().limbs.count("RightArm") > 0);
121 
122  // const auto& leftConfig = builder.config().limbs.at("LeftArm");
123  // const auto& rightConfig = builder.config().limbs.at("RightArm");
124 
125  // /// to be removed
126  // ARMARX_INFO << VAROUT(armConfig.kpImpedance);
127  // ARMARX_INFO << VAROUT(armConfig.kdImpedance);
128  // ARMARX_INFO << VAROUT(armConfig.kpNullspace);
129  // ARMARX_INFO << VAROUT(armConfig.kdNullspace);
130  // ARMARX_INFO << VAROUT(armConfig.desiredPose);
131  // ARMARX_INFO << VAROUT(armConfig.desiredTwist);
132  // if (armConfig.desiredNullspaceJointAngles.has_value())
133  // {
134  // ARMARX_INFO << VAROUT(armConfig.desiredNullspaceJointAngles.value());
135  // }
136  // ARMARX_INFO << VAROUT(armConfig.torqueLimit);
137  // ARMARX_INFO << VAROUT(armConfig.qvelFilter);
138  }
139 
140 
141  ARMARX_INFO << "Setting desired pose for controller with nodeset " << rns->getName() << ".";
142  auto& armConfig = builder.config().limbs.at(rns->getName());
143 
144 
145  // TODO add offset in global z-direction
146  armConfig.desiredPose = rns->getTCP()->getPoseInRootFrame();
147  ARMARX_IMPORTANT << VAROUT(armConfig.desiredPose);
148 
149  ARMARX_TRACE;
150  ARMARX_INFO << "creating controller";
151 
152  auto ctrlWrapper = builder.create();
153  const std::string controllerName = builder.getControllerName();
154 
155  // The controller is scoped by default, which means that if the ControllerWrapper goes out of scope, it will deactivate
156  // and delete the NJointController. To prevent this, you can use the daemonize(true) method if needed.
157  ctrlWrapper->daemonize(false);
158 
159  ARMARX_TRACE;
160  ARMARX_CHECK_NOT_NULL(ctrlWrapper);
161 
162  controllers.emplace(rns->getName(), std::move(ctrlWrapper));
163  }
164 
166 
167  ARMARX_INFO << "Activating controller";
168  for (auto& c : controllers)
169  {
170  c.second->updateConfig();
171 
172  if (not services_.controlComponentPlugin->getRobotUnitPlugin()
173  .getRobotUnit()
174  ->isSimulation())
175  {
176  c.second->activate();
177  }
178  }
179 
180 
181  // TODO sleep for in.getMinExecutionTime() usleep(10000);
182 
183  // TODO Trigger speech output
184 
185  // TOOD start platform following
186  const auto platformControlConfig = in.parameters.platformControlConfig;
187 
188  // TODO stop platform unit -> trigger placing
189 
190  constexpr auto ControllerType = ::armarx::control::njoint_controller::platform::
191  ControllerType::PlatformFollowerController;
192 
193  auto platformControllerBuilder =
195  platformControllerBuilder.withNodeSet("Platform").withNamePrefix("Andre");
196 
197 
198  {
199  const std::string controllerClassName =
202  const armarx::PackagePath configPath(
203  "armarx_control", "controller_config/PlatformFollowerController/default.json");
204  ARMARX_IMPORTANT << "Using config `" << configPath.toSystemPath() << "`.";
205  ARMARX_CHECK(std::filesystem::exists(configPath.toSystemPath()));
206 
207  platformControllerBuilder.withConfig(configPath.toSystemPath());
208  }
209  // TODO add support of using a diffeerent default parameters, for different robots.
210  // platformControllerBuilder.config().params.kPos = in.parameters.platformControlConfig.kPos;
211  // platformControllerBuilder.config().params.kAngle = in.parameters.platformControlConfig.kAngle;
212  // platformControllerBuilder.config().params.dPos = in.parameters.platformControlConfig.dPos;
213  // platformControllerBuilder.config().params.dAngle = in.parameters.platformControlConfig.dAngle;
214 
215  // platformControllerBuilder.config().params.maxAngleVel = in.parameters.platformControlConfig.maxAngleVel;
216  // platformControllerBuilder.config().params.maxPosVel = in.parameters.platformControlConfig.maxPosVel;
217 
218  // platformControllerBuilder.config().params.minAngleTolerance = in.parameters.platformControlConfig.minAngleTolerance;
219  // platformControllerBuilder.config().params.minPosTolerance = in.parameters.platformControlConfig.minPosTolerance;
220 
221  auto platformCtrlWrapper = platformControllerBuilder.create();
222 
223  ARMARX_INFO << "Activating platform controller.";
224  platformCtrlWrapper->activate();
225 
226  while (not shouldSkillTerminate())
227  {
229  }
230 
231  ARMARX_INFO << "Stop requested. Deactivating controllers.";
232  for (auto& c : controllers)
233  {
234  c.second->deactivate();
235  }
236  platformCtrlWrapper->deactivate();
237 
238 
239  return MainResult{
240  .status = ::armarx::skills::TerminatedSkillStatus::Succeeded,
241  .data = nullptr,
242  };
243  }
244 
245  /*
246  ::armarx::skills::Skill::ExitResult
247  Follower::exit(const SpecializedExitInput&)
248  {
249  return ExitResult{.status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
250  }
251  */
252 
253 } // namespace armarx::control::skills::skills::platform_follower_controller
skills
This file is part of ArmarX.
armarx::control::common::ControllerType::TSImp
@ TSImp
armarx::skills::SimpleSpecializedSkill< arondto::FollowerParams >::ParamType
arondto::FollowerParams ParamType
Definition: SimpleSpecializedSkill.h:14
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::skills::SkillID::skillName
std::string skillName
Definition: SkillID.h:60
armarx::core::time::Clock::WaitFor
static void WaitFor(const Duration &duration)
Wait for a certain duration on the virtual clock.
Definition: Clock.cpp:104
armarx::control::skills::skills::platform_follower_controller::Follower::Services
Definition: Follower.h:28
armarx::skills::SkillDescription
Definition: SkillDescription.h:18
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
Follower.h
armarx::control::skills::skills::platform_follower_controller::Follower::Parameters::robotName
std::string robotName
Definition: Follower.h:37
armarx::control::skills::skills::platform_follower_controller::Follower::GetSkillDescription
::armarx::skills::SkillDescription GetSkillDescription()
Definition: Follower.cpp:26
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::armem::robot_state::VirtualRobotReader::synchronizeRobot
bool synchronizeRobot(VirtualRobot::Robot &robot, const armem::Time &timestamp) const
Definition: VirtualRobotReader.cpp:20
armarx::control::client::ControllerWrapper
Wrapper class for an NJointController proxy.
Definition: ControllerWrapper.h:127
type.h
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::control::common::ControllerTypeNames
const simox::meta::EnumNames< ControllerType > ControllerTypeNames
Definition: type.h:110
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::control::skills::skills::platform_follower_controller::Follower::Services::controlComponentPlugin
::armarx::control::client::ComponentPlugin * controlComponentPlugin
Definition: Follower.h:31
armarx::skills::SkillDescription::skillId
SkillID skillId
Definition: SkillDescription.h:20
armarx::control::client::ComponentPlugin::getRobotUnitPlugin
armarx::plugins::RobotUnitComponentPlugin & getRobotUnitPlugin()
Definition: ComponentPlugin.h:106
armarx::control::client::ComponentPlugin::createControllerBuilder
auto createControllerBuilder(Args... args)
Definition: ComponentPlugin.h:83
ControllerBuilder.h
armarx::skills::Skill::MainResult
A result struct for th main method of a skill.
Definition: Skill.h:48
ExpressionException.h
armarx::control::skills::skills::platform_follower_controller::Follower::Follower
Follower(const Services &services, const Parameters &parameters)
Definition: Follower.cpp:44
armarx::control::skills::skills::platform_follower_controller::Follower::Parameters
Definition: Follower.h:35
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::skills::SimpleSpecializedSkill< arondto::FollowerParams >::main
Skill::MainResult main() final
Definition: SimpleSpecializedSkill.h:71
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::control::common::ControllerType
ControllerType
Definition: type.h:29
armarx::skills::Skill::shouldSkillTerminate
bool shouldSkillTerminate() const
Returns whether the skill should terminate as soon as possible.
Definition: Skill.cpp:371
controller_descriptions.h
ImpedanceController.h
armarx::control::skills::skills::platform_follower_controller::Follower::Services::robotReader
armem::robot_state::VirtualRobotReader robotReader
Definition: Follower.h:32
armarx::core::time::Duration::Days
static Duration Days(std::int64_t days)
Constructs a duration in days.
Definition: Duration.cpp:167
armarx::control::skills::skills::platform_follower_controller
Definition: Follower.cpp:22
armarx::PackagePath
Definition: PackagePath.h:55
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::plugins::RobotUnitComponentPlugin::getRobotUnit
RobotUnitInterfacePrx getRobotUnit() const
Definition: RobotUnitComponentPlugin.cpp:112
controller_descriptions.h