Skip to content
Snippets Groups Projects
Commit 4cf9ee3a authored by Fabian Reister's avatar Fabian Reister
Browse files

periodic specialized skill

parent ea6ec318
No related branches found
No related tags found
1 merge request!224Draft: Feature/skills
......@@ -17,6 +17,7 @@ armarx_add_library(
./provider/Skill.cpp
./provider/PeriodicSkill.cpp
./provider/SpecializedSkill.cpp
./provider/PeriodicSpecializedSkill.cpp
./provider/SkillDescription.cpp
./provider/SkillStatusUpdate.cpp
./provider/helper/LambdaSkillImplementation.cpp
......@@ -27,6 +28,7 @@ armarx_add_library(
./provider/Skill.h
./provider/PeriodicSkill.h
./provider/SpecializedSkill.h
./provider/PeriodicSpecializedSkill.h
./provider/SkillDescription.h
./provider/SkillStatusUpdate.h
./provider/helper/LambdaSkillImplementation.h
......
/**
* This file is part of ArmarX.
*
* ArmarX is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* ArmarX is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Fabian Reister ( fabian dot reister at kit dot edu )
* @date 2022
* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
* GNU General Public License
*/
#include "PeriodicSpecializedSkill.h"
namespace armarx::skills
{
} // namespace armarx::skills
......@@ -21,8 +21,8 @@
#pragma once
#include "ArmarXCore/core/time/Metronome.h"
#include <ArmarXCore/core/time/Frequency.h>
#include <ArmarXCore/core/time/Metronome.h>
#include "Skill.h"
#include "SpecializedSkill.h"
......@@ -30,15 +30,69 @@
namespace armarx::skills
{
template <class AronT>
class PeriodicSpecializedSkill : virtual public SpecializedSkill<AronT>
class PeriodicSpecializedSkill : public SpecializedSkill<AronT>
{
public:
using Base = SpecializedSkill<AronT>;
PeriodicSpecializedSkill() = delete;
PeriodicSpecializedSkill(const SkillDescription& skillDescription,
const armarx::Frequency& frequency);
const armarx::Frequency& frequency) :
Base(skillDescription), frequency(frequency)
{
}
protected:
typename Skill::Status _execute(const AronT& params,
const Skill::CallbackT& callback) final;
typename Skill::Status
_execute(const AronT& params, const Skill::CallbackT& callback) final
{
if (not initialize(params))
{
onFailed();
return Skill::Status::Failed;
}
core::time::Metronome metronome(frequency);
while (not Skill::stopped and not Skill::timeoutReached)
{
const auto status = executeOnce(params);
switch (status)
{
case Status::Running:
// nothing to do here
break;
case Status::Succeeded:
onSucceeded();
return Skill::Status::Succeeded;
case Status::Failed:
onFailed();
return Skill::Status::Failed;
}
const auto sleepDuration = metronome.waitForNextTick();
if (not sleepDuration.isPositive())
{
ARMARX_INFO << deactivateSpam() << "PeriodicSkill: execution took too long ("
<< -sleepDuration << " vs " << frequency.toCycleDuration() << ")";
}
}
if (Skill::stopped)
{
onStopped();
return Skill::Status::Stopped;
}
if (Skill::timeoutReached)
{
onTimeoutReached();
return Skill::Status::TimeoutReached;
}
throw armarx::LocalException("should not happen.");
}
enum class Status
{
......@@ -59,61 +113,4 @@ namespace armarx::skills
const armarx::Frequency frequency;
};
template <class AronT>
PeriodicSpecializedSkill<AronT>::PeriodicSpecializedSkill(const SkillDescription& skillDescription,
const armarx::Frequency& frequency) :
Skill(skillDescription), frequency(frequency)
{
}
template <class AronT>
Skill::Status
PeriodicSpecializedSkill<AronT>::_execute(const AronT& params, const Skill::CallbackT& callback)
{
if(not initialize(params))
{
onFailed();
return Skill::Status::Failed;
}
core::time::Metronome metronome(frequency);
while (not Skill::stopped and not Skill::timeoutReached)
{
const auto status = executeOnce(params);
switch (status)
{
case Status::Running:
// nothing to do here
break;
case Status::Succeeded:
onSucceeded();
return Skill::Status::Succeeded;
case Status::Failed:
onFailed();
return Skill::Status::Failed;
}
const auto sleepDuration = metronome.waitForNextTick();
if (not sleepDuration.isPositive())
{
ARMARX_INFO << deactivateSpam() << "PeriodicSkill: execution took too long ("
<< -sleepDuration << " vs " << frequency.toCycleDuration() << ")";
}
}
if (Skill::stopped)
{
onStopped();
return Skill::Status::Stopped;
}
if (Skill::timeoutReached)
{
onTimeoutReached();
return Skill::Status::TimeoutReached;
}
throw armarx::LocalException("should not happen.");
}
} // namespace armarx::skills
......@@ -15,6 +15,8 @@ namespace armarx
class SpecializedSkill : public Skill
{
public:
using ParamType = AronT;
using Skill::Skill;
virtual ~SpecializedSkill() = default;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment