orwell 0.0.0
Controller.hxx
1/*
2 Copyright (c) Tobias Löw
3 Written by Tobias Löw <https://tobiloew.ch>
4
5 This file is part of orwell.
6
7 gafro is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 3 as
9 published by the Free Software Foundation.
10
11 gafro is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with gafro. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22#include <sackmesser/Configurations.hpp>
23//
24#include <orwell/Controller.hpp>
25
26namespace orwell
27{
28
29 template <int dof>
30 Controller<dof>::Controller(const sackmesser::Interface::Ptr &interface, const std::string &name)
31 {
32 config_ = interface->getConfigurations()->load<Configuration>(name);
33 }
34
35 template <int dof>
36 bool Controller<dof>::Configuration::load(const std::string &ns, const std::shared_ptr<sackmesser::Configurations> &server)
37 {
38 std::array<double, 7> command_limits;
39
40 if (!server->loadParameter<7>(ns + "/command_limits", &command_limits))
41 {
42 return false;
43 }
44
45 command_limits_ = Eigen::Vector<double, 7>(command_limits.data());
46
47 return true;
48 }
49
50 template <int dof>
52 {
53 return robot_model_;
54 }
55
56 template <int dof>
57 void Controller<dof>::setRobotModel(const typename RobotModel<dof>::Ptr &robot_model)
58 {
59 robot_model_ = robot_model;
60 }
61
62 template <int dof>
63 void Controller<dof>::setStoppingCriterion(const std::shared_ptr<StoppingCriterion<dof>> &stopping_criterion)
64 {
65 stopping_criterion_ = stopping_criterion;
66 }
67
68 template <int dof>
69 bool Controller<dof>::isFinished(const RobotState<dof> &robot_state) const
70 {
71 if (!stopping_criterion_)
72 {
73 return false;
74 }
75
76 return stopping_criterion_->isFinished(robot_state);
77 }
78
79 template <int dof>
80 template <class Derived>
81 typename Derived::Ptr Controller<dof>::cast()
82 {
83 return std::dynamic_pointer_cast<Derived>(this->shared_from_this());
84 }
85
86 template <int dof>
88 {
89 robot_state_ = robot_state;
90
91 this->getRobotModel()->computeCartesianState(robot_state);
92
93 return limitCommand(computeCommand());
94 }
95
96 template <int dof>
98 {
99 return robot_state_;
100 }
101
102 template <int dof>
104 {
105 return command.array().min(config_.command_limits_.array()).max(-config_.command_limits_.array());
106 }
107
108} // namespace orwell
base class for controller implementation
RobotState< dof >::Vector getControlCommand(const RobotState< dof > &robot_state)
general function to obtain control commands
bool isFinished(const RobotState< dof > &robot_state) const
check if the StoppingCriterion is fulfilled
const RobotState< dof > & getRobotState() const
proved access to the RobotState to derived classes
RobotModel< dof >::Ptr getRobotModel()
proved access to the RobotModel to derived classes
Derived::Ptr cast()
cast Controller to a derived class
void setRobotModel(const typename RobotModel< dof >::Ptr &robot_model)
set the RobotModel
virtual RobotState< dof >::Vector computeCommand()=0
will be invoked when calling getControlCommand
Controller(const sackmesser::Interface::Ptr &interface, const std::string &name)
loads configuration parameters
void setStoppingCriterion(const std::shared_ptr< StoppingCriterion< dof > > &stopping_criterion)
(optional) StoppingCriterion
std::shared_ptr< RobotModel > Ptr
typedef
describes the RobotState
Eigen::Vector< double, dof > Vector
typedef
base class for a stopping criterion for a Controller