reflex.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <mutex>
27 
28 #include <Eigen/Core>
29 #include <Eigen/Geometry>
30 
32 
33 namespace armarx
34 {
35  class ReflexCombination;
36 
37  class Reflex
38  {
39  public:
41  {
42  this->interval = interval;
43 
44  task = new PeriodicTask<Reflex>(
45  this, &Reflex::calc, interval, false, "HeadStabilizationTask");
46  task->setDelayWarningTolerance(5);
47  }
48 
49  virtual ~Reflex()
50  {
51  stop();
52  }
53 
54  void
56  {
57  if (!task->isRunning())
58  {
59  task->start();
60  }
61  }
62 
63  void
64  stop()
65  {
66  if (task->isRunning())
67  {
68  task->stop();
69  }
70 
71  onStop();
72 
73  jointAngles.clear();
74  }
75 
76  void
78  {
79  if (enabled)
80  {
81  start();
82  }
83  else
84  {
85  stop();
86  }
87  }
88 
89  std::map<std::string, float>
91  {
92  std::scoped_lock lock(mutex);
93 
94  std::map<std::string, float> result(jointAngles);
95 
96  //if (( currentTime - updateTime) > xxx) {
97 
98  //result.swap(jointAngles);
99  // }
100 
101  return result;
102  }
103 
104  float
105  getWeight() const
106  {
107  return weight;
108  }
109 
110  void
111  setWeight(float weight)
112  {
113  this->weight = weight;
114  }
115 
116  virtual std::string getName() const = 0;
117 
118 
119  protected:
120  Eigen::Vector3f
122  {
123  Eigen::Vector3f euler;
124 
125  euler(0) = -std::atan2(2.0 * (q.w() * q.x() - q.y() * q.z()),
126  1.0 - 2.0 * (q.x() * q.x() + q.y() * q.y())); //roll
127  euler(1) = std::asin(2.0 * (q.w() * q.y() - q.z() * q.x())); //pitch
128  euler(2) = std::atan2(2.0 * (q.w() * q.z() + q.x() * q.y()),
129  1.0 - 2.0 * (q.y() * q.y() + q.z() * q.z())); //yaw
130 
131  return euler;
132  }
133 
134  virtual void calc() = 0;
135 
136  virtual void onStop() = 0;
137 
138  std::mutex mutex;
139 
140  std::map<std::string, float> jointAngles;
141 
142  int interval;
143 
144  bool isEnabled;
145 
147 
148  std::string name;
149 
150 
151  private:
153 
154  float weight;
155  };
156 
157  using ReflexPtr = std::shared_ptr<Reflex>;
158 } // namespace armarx
armarx::Reflex::onStop
virtual void onStop()=0
armarx::Reflex::setWeight
void setWeight(float weight)
Definition: reflex.h:111
armarx::Reflex::updateTime
IceUtil::Time updateTime
Definition: reflex.h:146
PeriodicTask.h
armarx::Reflex::getWeight
float getWeight() const
Definition: reflex.h:105
armarx::Reflex::stop
void stop()
Definition: reflex.h:64
armarx::Reflex::jointAngles
std::map< std::string, float > jointAngles
Definition: reflex.h:140
armarx::Reflex::name
std::string name
Definition: reflex.h:148
armarx::Reflex::calc
virtual void calc()=0
armarx::Reflex::~Reflex
virtual ~Reflex()
Definition: reflex.h:49
armarx::Reflex
Definition: reflex.h:37
armarx::Reflex::mutex
std::mutex mutex
Definition: reflex.h:138
armarx::Reflex::getJoints
std::map< std::string, float > getJoints()
Definition: reflex.h:90
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
armarx::Reflex::isEnabled
bool isEnabled
Definition: reflex.h:144
armarx::Reflex::getName
virtual std::string getName() const =0
q
#define q
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::Reflex::quaternionToRPY
Eigen::Vector3f quaternionToRPY(Eigen::Quaternionf q)
Definition: reflex.h:121
armarx::Quaternion< float, 0 >
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::Reflex::Reflex
Reflex(int interval)
Definition: reflex.h:40
armarx::PeriodicTask
Definition: ArmarXManager.h:70
armarx::Reflex::interval
int interval
Definition: reflex.h:142
armarx::ReflexPtr
std::shared_ptr< Reflex > ReflexPtr
Definition: reflex.h:157
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::Reflex::start
void start()
Definition: reflex.h:55
armarx::Reflex::setEnabled
void setEnabled(bool enabled)
Definition: reflex.h:77