MoveJointsToPosition.cpp
Go to the documentation of this file.
1 #include "MoveJointsToPosition.h"
2 
4 
5 namespace armarx::skills
6 {
7  namespace
8  {
9  joint_control::arondto::MoveJointsToPositionAcceptedType GetDefaultParameterization()
10  {
11  joint_control::arondto::MoveJointsToPositionAcceptedType ret;
12  ret.jointTargetTolerance = 0.05;
13  return ret;
14  }
15  }
16 
17  SkillDescription MoveJointsToPosition::Description = skills::SkillDescription{
18  "MoveJointsToPosition", "Move the joints to position",
20  joint_control::arondto::MoveJointsToPositionAcceptedType::ToAronType(),
21  GetDefaultParameterization().toAron()
22  };
23 
25  JointControlSkill(mns, context),
26  mixin::RobotReadingSkillMixin(mns),
27  PeriodicSimpleSpecializedSkill<joint_control::arondto::MoveJointsToPositionAcceptedType>(Description, armarx::core::time::Frequency::Hertz(10))
28  {
29  }
30 
31  void MoveJointsToPosition::reset()
32  {
33  already_running = false;
34  }
35 
36  Skill::InitResult MoveJointsToPosition::init(const SpecializedInitInput &in)
37  {
39  return {.status = TerminatedSkillStatus::Succeeded};
40  }
41 
42  PeriodicSkill::StepResult MoveJointsToPosition::step(const SpecializedMainInput& in)
43  {
44  auto robot = robotReader.queryState({in.parameters.robotName}, armem::Time::Now());
45  if (!robot)
46  {
47  ARMARX_WARNING << "Unable to get robot from robotstatememory. Abort.";
48  return {ActiveOrTerminatedSkillStatus::Failed, nullptr};
49  }
50 
51  if ((armem::Time::Now() - robot->timestamp).toMilliSeconds() > 300)
52  {
53  ARMARX_WARNING << deactivateSpam(1) << "Robot from Memory is too old. Try again.";
55  }
56 
57  std::map<std::string, float> filteredTargets;
58  auto currentValues = robot->jointMap;
59  for (const auto& [key, val] : in.parameters.targetJointMap)
60  {
61  if (const auto& it = currentValues.find(key); it != currentValues.end())
62  {
63  ARMARX_INFO << deactivateSpam(1) << "Current value of " << key << " is " << it->second << " and it will be set to " << val;
64  filteredTargets.insert({key, val});
65  }
66  }
67 
68  if (filteredTargets.size() == 0)
69  {
70  return {ActiveOrTerminatedSkillStatus::Failed, nullptr};
71  }
72 
73  auto currentJointValueTolerance = in.parameters.jointTargetTolerance;
74  ARMARX_INFO << deactivateSpam(1) << "Joint target tolerance: " << currentJointValueTolerance;
75 
76  auto currentMaxDelta = std::numeric_limits<float>::max();
77 
78  robot = robotReader.queryState({in.parameters.robotName}, armem::Time::Now());
79 
80  currentValues = robot->jointMap;
81  float maxDeltaTmp = 0;
82  for (const auto& [key, value] : filteredTargets)
83  {
84  const float delta = fabs((value - currentValues.at(key)));
85  maxDeltaTmp = std::max(delta, maxDeltaTmp);
86  }
87  currentMaxDelta = maxDeltaTmp;
88 
89  ARMARX_IMPORTANT << deactivateSpam(1) << "Current joint values are: \n" << currentValues;
90  ARMARX_IMPORTANT << deactivateSpam(1) << "MaxDelta is: " << currentMaxDelta;
91 
92  if (currentMaxDelta <= currentJointValueTolerance)
93  {
95  }
96 
97  if (!already_running)
98  {
99  already_running = true;
100 
101  NameControlModeMap controlModes;
102  for (const auto& p : filteredTargets)
103  {
104  controlModes[p.first] = ePositionControl;
105  }
106  context.kinematicUnitPrx->switchControlMode(controlModes);
107 
108  context.kinematicUnitPrx->setJointAngles(filteredTargets);
109  }
110  return {ActiveOrTerminatedSkillStatus::Running, nullptr};
111  }
112 
113  Skill::ExitResult MoveJointsToPosition::exit(const SpecializedExitInput &in)
114  {
115  std::vector<std::string> joints;
116  std::for_each(in.parameters.targetJointMap.begin(), in.parameters.targetJointMap.end(), [&joints](const std::pair<std::string, float>& p) { joints.push_back(p.first); });
117 
118  stopJointMovement(joints);
119 
120  return {.status = TerminatedSkillStatus::Succeeded};
121  }
122 }
armarx::skills::JointControlSkill
Definition: JointControlSkill.h:58
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::skills::TerminatedSkillStatus::Succeeded
@ Succeeded
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::skills::ActiveOrTerminatedSkillStatus::Succeeded
@ Succeeded
armarx::skills::ActiveOrTerminatedSkillStatus::Failed
@ Failed
armarx::skills
This file is part of ArmarX.
Definition: PeriodicUpdateWidget.cpp:11
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::skills::mixin::RobotReadingSkillMixin::robotReader
armem::robot_state::VirtualRobotReader robotReader
Definition: RobotReadingSkillMixin.h:11
armarx::core::time::Frequency
Represents a frequency.
Definition: Frequency.h:17
armarx::armem::robot_state::RobotReader::connect
virtual void connect(armem::client::MemoryNameSystem &memoryNameSystem)
Definition: RobotReader.cpp:48
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::skills::JointControlSkillContext
Definition: JointControlSkill.h:47
MoveJointsToPosition.h
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::skills::ActiveOrTerminatedSkillStatus::Running
@ Running
armarx::skills::MoveJointsToPosition::MoveJointsToPosition
MoveJointsToPosition(armem::client::MemoryNameSystem &mns, JointControlSkillContext &context)
Definition: MoveJointsToPosition.cpp:24
JointControlUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::skills::JointControlSkill::stopJointMovement
TerminatedSkillStatus stopJointMovement(const std::vector< std::string > &joints)
Definition: JointControlSkill.h:68
armarx::skills::JointControlSkillContext::kinematicUnitPrx
KinematicUnitInterfacePrx kinematicUnitPrx
Definition: JointControlSkill.h:50
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::robot_state::RobotReader::queryState
std::optional< RobotState > queryState(const std::string &robotName, const armem::Time &timestamp) const
Definition: RobotReader.cpp:186
armarx::skills::JointControlSkill::context
JointControlSkillContext & context
Definition: JointControlSkill.h:83
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
armarx::skills::MoveJointsToPosition::Description
static SkillDescription Description
Definition: MoveJointsToPosition.h:48