PeriodicSkill.cpp
Go to the documentation of this file.
1 /**
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @author Fabian Reister ( fabian dot reister at kit dot edu )
17  * @date 2022
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #include "PeriodicSkill.h"
23 
28 
30 
31 namespace armarx::skills
32 {
34  const armarx::Frequency& frequency) :
35  Skill(skillDescription), frequency(frequency)
36  {
37  }
38 
41  {
43 
44  while (true)
45  {
47 
48  const auto res = step();
49  switch (res.status)
50  {
52  // nothing to do here
53  break;
55  return MakeAbortedResult();
57  return MakeSucceededResult(res.data);
59  return MakeFailedResult();
60  }
61 
62  const auto sleepDuration = metronome.waitForNextTick();
63  if (not sleepDuration.isPositive())
64  {
65  ARMARX_INFO << deactivateSpam() << __PRETTY_FUNCTION__
66  << ": execution took too long (" << -sleepDuration
67  << " too long. Expected " << frequency.toCycleDuration() << ")";
68  }
69  }
70 
71  // never happens
72  throw skills::error::SkillException(__PRETTY_FUNCTION__, "Should not happen!");
73  }
74 
77  {
78  ARMARX_IMPORTANT << "Dummy executing once skill '" << description.skillId
79  << "'. Please overwrite this method!";
81  }
82 
83 } // namespace armarx::skills
armarx::skills::PeriodicSkill::frequency
const armarx::Frequency frequency
Definition: PeriodicSkill.h:52
armarx::skills::PeriodicSkill::main
Skill::MainResult main() final
Do not use anymore.
Definition: PeriodicSkill.cpp:40
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
LocalException.h
armarx::skills::ActiveOrTerminatedSkillStatus::Succeeded
@ Succeeded
armarx::skills::ActiveOrTerminatedSkillStatus::Failed
@ Failed
armarx::skills
This file is part of ArmarX.
Definition: PeriodicUpdateWidget.cpp:11
armarx::skills::SkillDescription
Definition: SkillDescription.h:18
Frequency.h
armarx::core::time::Frequency
Represents a frequency.
Definition: Frequency.h:17
armarx::skills::Skill::MakeFailedResult
static MainResult MakeFailedResult()
Definition: Skill.cpp:324
armarx::core::time::Frequency::toCycleDuration
Duration toCycleDuration() const
Definition: Frequency.cpp:67
Skill.h
armarx::skills::PeriodicSkill::PeriodicSkill
PeriodicSkill(const SkillDescription &skillDescription, const armarx::Frequency &frequency)
Definition: PeriodicSkill.cpp:33
armarx::skills::Skill::description
SkillDescription description
Definition: Skill.h:336
armarx::skills::SkillDescription::skillId
SkillID skillId
Definition: SkillDescription.h:20
armarx::skills::ActiveOrTerminatedSkillStatus::Aborted
@ Aborted
Metronome.h
armarx::skills::error::SkillException
A base class for skill exceptions.
Definition: Exception.h:40
armarx::skills::Skill::MakeAbortedResult
static MainResult MakeAbortedResult()
Definition: Skill.cpp:333
armarx::skills::Skill::MakeSucceededResult
static MainResult MakeSucceededResult(aron::data::DictPtr data=nullptr)
Definition: Skill.cpp:315
armarx::skills::Skill::MainResult
A result struct for th main method of a skill.
Definition: Skill.h:48
armarx::skills::ActiveOrTerminatedSkillStatus::Running
@ Running
armarx::core::time::Metronome::waitForNextTick
Duration waitForNextTick()
Wait and block until the target period is met.
Definition: Metronome.cpp:31
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::core::time::Metronome
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition: Metronome.h:35
armarx::skills::PeriodicSkill::step
virtual StepResult step()
Override this method with your own step function.
Definition: PeriodicSkill.cpp:76
armarx::skills::Skill
Base class for skills.
Definition: Skill.h:29
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:92
armarx::skills::Skill::throwIfSkillShouldTerminate
void throwIfSkillShouldTerminate(const std::string &abortedMessage="")
Definition: Skill.cpp:291
Logging.h
PeriodicSkill.h
armarx::skills::PeriodicSkill::StepResult
Definition: PeriodicSkill.h:36