MotionLib  1.0.0
SamBuCa motion library
Units.hpp
1 /*
2  * Copyright (C) 2021 CERN
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  * Created on: 2021-11-23
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  */
24 
25 #ifndef MOTION_CONTROLLER_UNITS_HPP__
26 #define MOTION_CONTROLLER_UNITS_HPP__
27 
28 #include <chrono>
29 #include <cstdint>
30 #include <iterator>
31 #include <set>
32 #include <string>
33 
34 #include "util/serialize.hpp"
35 
36 namespace smc {
37 namespace units {
38 
47 enum Unit
48 {
49  UNKNOWN,
50  MILLIMETERS,
51  METERS,
52  STEPS,
53  DEGREES,
54  RADIANS,
55  TURNS,
56  STEPS_PER_TURN,
57  MILLIMETERS_PER_MINUTE,
58  METERS_PER_SECOND,
59  STEPS_PER_MILLIMETER,
60  MILLIMETERS_PER_SECOND,
61  MILLIMETERS_PER_SECOND_SQUARED,
62 
63  unknown = UNKNOWN,
64  millimeters = MILLIMETERS,
65  meters = METERS,
66  steps = STEPS,
67  degrees = DEGREES,
68  radians = RADIANS,
69  turns = TURNS,
70  steps_per_turn = STEPS_PER_TURN,
71  millimeters_per_minute = MILLIMETERS_PER_MINUTE,
72  meters_per_second = METERS_PER_SECOND,
73  steps_per_millimeter = STEPS_PER_MILLIMETER,
74  millimeters_per_second = MILLIMETERS_PER_SECOND,
75  millimeters_per_second_squared = MILLIMETERS_PER_SECOND_SQUARED,
76 
77  mm = MILLIMETERS,
78  m = METERS,
79  deg = DEGREES,
80  rad = RADIANS,
81  mm_per_min = MILLIMETERS_PER_MINUTE,
82  m_per_s = METERS_PER_SECOND,
83  steps_per_mm = STEPS_PER_MILLIMETER,
84  mm_per_s = MILLIMETERS_PER_SECOND,
85  mm_per_s2 = MILLIMETERS_PER_SECOND_SQUARED
86 };
87 
88 typedef Unit unit_t;
89 
90 struct Value
91 {
92  typedef double value_type;
93  unit_t unit;
94  value_type value;
95 
96  Value();
97  Value(unit_t unit, value_type value);
98 
99  bool canConvert(unit_t unit) const;
100  bool convert(unit_t unit);
101 
102  Value &operator+=(value_type raw);
103  Value &operator-=(value_type raw);
104  Value &operator*=(value_type raw);
105  Value &operator/=(value_type raw);
106 
107  std::string toString(void) const;
108 
109 protected:
110  bool checkConvert(value_type &converted, unit_t unit) const;
111 };
112 typedef Value value_t;
113 
114 Value operator*(double raw, unit_t unit);
115 
116 Value operator"" _mm(long double raw);
117 Value operator"" _m(long double raw);
118 Value operator"" _steps(long double raw);
119 Value operator"" _deg(long double raw);
120 Value operator"" _rad(long double raw);
121 Value operator"" _turns(long double raw);
122 Value operator"" _mm_per_min(long double raw);
123 Value operator"" _m_per_s(long double raw);
124 Value operator"" _mm_per_s(long double raw);
125 Value operator"" _mm_per_s2(long double raw);
126 Value operator"" _steps_per_mm(long double raw);
127 Value operator"" _steps_per_turn(long double raw);
128 
129 Value operator*(const Value &value1, const Value &value2);
130 Value operator/(const Value &value1, const Value &value2);
131 
132 template<class R, class P>
133 Value operator*(const Value &value, std::chrono::duration<R, P> duration);
134 template<class R, class P>
135 Value operator/(const Value &value, std::chrono::duration<R, P> duration);
136 
137 struct IOPort
138 {
139  IOPort(const IOPort &) = default;
140  IOPort(IOPort &&) = default;
141  IOPort &operator=(const IOPort &) = default;
142  IOPort &operator=(IOPort &&) = default;
143 
144  typedef size_t size_type;
145  typedef uint64_t value_type;
146  typedef std::bidirectional_iterator_tag iterator_category;
147  struct iterator;
148  struct const_iterator;
149  struct reference;
150  struct const_reference;
151 
152  explicit IOPort() : value{0}, size{0} {}
153  explicit IOPort(bool value) :
154  value{value ? uint64_t{1} : uint64_t{0}},
155  size{1}
156  {}
157  explicit IOPort(uint8_t value, size_type size = 8) :
158  value{value},
159  size{size}
160  {}
161  explicit IOPort(uint16_t value, size_type size = 16) :
162  value{value},
163  size{size}
164  {}
165  explicit IOPort(uint32_t value, size_type size = 32) :
166  value{value},
167  size{size}
168  {}
169  explicit IOPort(uint64_t value, size_type size = 64) :
170  value{value},
171  size{size}
172  {}
173 
174  bool operator==(const IOPort &port) const
175  {
176  return value == port.value && size == port.size;
177  }
178  bool operator!=(const IOPort &port) const { return !(port == *this); }
179 
180  inline bool operator[](size_type pos) const
181  {
182  return ((value >> pos) & 1);
183  };
184  inline operator bool() const { return value; }
185 
186  inline reference operator[](size_type offset);
187 
188  inline const_iterator begin() const;
189  inline const_iterator end() const;
190 
191  inline const_iterator const_begin() const;
192  inline const_iterator const_end() const;
193 
194  inline iterator begin();
195  inline iterator end();
196 
200  enum class StrFmt
201  {
202  AUTO,
203  BIN,
204  HEX
205  };
206 
207  std::string toString(StrFmt format = StrFmt::AUTO) const;
208 
209  value_type value;
210  size_type size;
211 };
212 typedef IOPort io_port_t;
213 
217 } /* namespace units */
218 
224 const std::string &to_string(units::Unit unit);
225 
232 template<>
233 units::Unit from_string(const std::string &);
234 
240 inline std::string to_string(const units::Value &value)
241 {
242  return value.toString();
243 }
244 
250 template<>
251 const std::set<units::Unit> &getEnumValues();
252 
258 inline std::string to_string(const units::io_port_t &port)
259 {
260  return port.toString();
261 }
262 
269 template<>
270 units::io_port_t from_string(const std::string &);
271 
272 } /* namespace smc */
273 
274 #include "Units.hxx"
275 
276 #endif /* MOTION_CONTROLLER_UNITS_HPP__ */
AxisPositionMonitor::State from_string(const std::string &str)
convert string to state
const std::set< AxisPositionMonitor::State > & getEnumValues()
Get devices types list (to be iterated)
const std::string & to_string(Axis::State state)
convert State to string
Definition: Axis.cpp:78
main motion-lib namespace
Definition: Client.cpp:30
StrFmt
IOPort format for string representation.
Definition: Units.hpp:201
value_type value
port value
Definition: Units.hpp:209
size_type size
size in bits
Definition: Units.hpp:210