LinearConvertedValue.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * 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 this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 
25 #pragma once
26 
27 
28 #include <cmath>
29 
31 
33 {
34 
35  /**
36  * @class LinearConvertedValue
37  * @ingroup Library-ethercat
38  * @brief Brief description of class LinearConvertedValue.
39  *
40  * Detailed description of class LinearConvertedValue.
41  */
42  template <typename T>
44  {
45 
46  public:
48  {
49  raw = nullptr;
50  offset = factor = 0;
51  offsetBeforeFactor = true;
52  }
53 
54  /**
55  * @brief init
56  * @param raw
57  * @param node
58  * @param defaultValue
59  * @param offsetBeforeFactor if true the offset is added before multiplying with factor. If false: the other way around.
60  * @param nameForDebugging This name is printend in case an error is encountered (Its only purpose is to ease debugging)
61  */
62  void
63  init(T* raw,
64  const hardware_config::types::LinearConfig linearConfig,
65  float defaultValue = std::nanf("1"),
66  bool offsetBeforeFactor = true,
67  const char* nameForDebugging = "")
68  {
69  init(raw,
70  linearConfig.getFactor(),
71  linearConfig.getOffset(),
72  defaultValue,
73  offsetBeforeFactor,
74  nameForDebugging);
75  }
76 
77  void
78  init(T* raw,
79  float factor,
80  float offset,
81  float defaultValue = std::nanf("1"),
82  bool offsetBeforeFactor = true,
83  const char* nameForDebugging = "")
84  {
85 #if 0
86  const auto rawAsInt = reinterpret_cast<std::uint64_t>(raw);
87  ARMARX_CHECK_EQUAL((rawAsInt % alignof(T)), 0)
88  << "\nThe alignment is wrong!\nIt has to be " << alignof(T)
89  << ", but the data is aligned with " << rawAsInt % alignof(std::max_align_t)
90  << "!\nThis is an offset of " << (rawAsInt % alignof(T))
91  << " bytes!\nThe datatype is " << GetTypeString<T>() << "\nIts size is "
92  << sizeof(T) << "\nraw = " << raw << " bytes\nThe name is " << nameForDebugging;
93 #endif
94  this->offsetBeforeFactor = offsetBeforeFactor;
95  this->factor = factor;
96  this->offset = offset;
97  this->raw = raw;
98  if (!std::isnan(defaultValue))
99  {
100  value = defaultValue;
101  write();
102  }
103  else
104  {
105  value = 0;
106  read();
107  }
108  }
109 
110  void
112  {
113  if (offsetBeforeFactor)
114  {
115  value = ((*raw) + offset) * factor;
116  }
117  else
118  {
119  value = (*raw) * factor + offset;
120  }
121  }
122 
123  void
125  {
126  if (offsetBeforeFactor)
127  {
128  *raw = static_cast<T>((value / factor) - offset);
129  }
130  else
131  {
132  *raw = static_cast<T>((value)-offset) / factor;
133  }
134  }
135 
136  float value;
137 
138  T
139  getRaw() const
140  {
141  return *raw;
142  }
143 
144  float
145  getFactor() const
146  {
147  return factor;
148  }
149 
150  float
151  getOffset() const
152  {
153  return offset;
154  }
155 
156  bool
158  {
159  return offsetBeforeFactor;
160  }
161 
162  private:
163  T* raw;
164  float offset, factor;
165  bool offsetBeforeFactor;
166  };
167 
168 } // namespace armarx::control::ethercat
armarx::control::ethercat::LinearConvertedValue::getFactor
float getFactor() const
Definition: LinearConvertedValue.h:145
armarx::control::hardware_config::types::LinearConfig
The LinearConfig class represents a linear conversion and has a factor and offset.
Definition: Types.h:17
armarx::control::hardware_config::types::LinearConfig::getOffset
float getOffset() const
Definition: Types.cpp:59
armarx::control::ethercat::LinearConvertedValue
Brief description of class LinearConvertedValue.
Definition: LinearConvertedValue.h:43
armarx::control::ethercat::LinearConvertedValue::LinearConvertedValue
LinearConvertedValue()
Definition: LinearConvertedValue.h:47
armarx::control::ethercat::LinearConvertedValue::value
float value
Definition: LinearConvertedValue.h:136
armarx::control::ethercat
Definition: Bus.cpp:24
Types.h
armarx::control::ethercat::LinearConvertedValue::read
void read()
Definition: LinearConvertedValue.h:111
armarx::control::hardware_config::types::LinearConfig::getFactor
float getFactor() const
Get the factor.
Definition: Types.cpp:53
armarx::control::ethercat::LinearConvertedValue::getOffset
float getOffset() const
Definition: LinearConvertedValue.h:151
armarx::control::ethercat::LinearConvertedValue::write
void write()
Definition: LinearConvertedValue.h:124
armarx::control::ethercat::LinearConvertedValue::init
void init(T *raw, float factor, float offset, float defaultValue=std::nanf("1"), bool offsetBeforeFactor=true, const char *nameForDebugging="")
Definition: LinearConvertedValue.h:78
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::control::ethercat::LinearConvertedValue::getRaw
T getRaw() const
Definition: LinearConvertedValue.h:139
armarx::control::ethercat::LinearConvertedValue::getOffsetBeforeFactor
bool getOffsetBeforeFactor() const
Definition: LinearConvertedValue.h:157
armarx::control::ethercat::LinearConvertedValue::init
void init(T *raw, const hardware_config::types::LinearConfig linearConfig, float defaultValue=std::nanf("1"), bool offsetBeforeFactor=true, const char *nameForDebugging="")
init
Definition: LinearConvertedValue.h:63