command.cpp
Go to the documentation of this file.
1 #include "command.hpp"
2 #include <cmath>
3 #include <limits>
4 
5 #ifndef M_PI
6 #define M_PI 3.14159265358979323846
7 #endif
8 
9 namespace hebi {
10 
12  : internal_(internal), field_(field)
13 {
14 }
15 
16 Command::FloatField::operator bool() const
17 {
18  return has();
19 }
20 
22 {
24 }
25 
27 {
28  float ret;
30  {
31  ret = std::numeric_limits<float>::quiet_NaN();
32  }
33  return ret;
34 }
35 
36 void Command::FloatField::set(float value)
37 {
39 }
40 
42 {
44 }
45 
47  : internal_(internal), field_(field)
48 {
49 }
50 
51 Command::HighResAngleField::operator bool() const
52 {
53  return has();
54 }
55 
57 {
58  return (hebiCommandGetHighResAngle(internal_, field_, nullptr, nullptr) == HebiStatusSuccess);
59 }
60 
62 {
63  int64_t revolutions;
64  float radian_offset;
65  if (hebiCommandGetHighResAngle(internal_, field_, &revolutions, &radian_offset) != HebiStatusSuccess)
66  {
67  return std::numeric_limits<double>::quiet_NaN();
68  }
69  return (
70  static_cast<double>(revolutions) * 2.0 * M_PI +
71  static_cast<double>(radian_offset)
72  );
73 }
74 
75 void Command::HighResAngleField::get(int64_t* revolutions, float* radian_offset) const
76 {
77  if (hebiCommandGetHighResAngle(internal_, field_, revolutions, radian_offset) != HebiStatusSuccess)
78  {
79  *revolutions = 0;
80  *radian_offset = std::numeric_limits<float>::quiet_NaN();
81  }
82 }
83 
84 void Command::HighResAngleField::set(double radians)
85 {
86  double revolutions_raw = radians / 2.0 / M_PI;
87  double revolutions_int_d;
88  double radian_offset_d = std::modf (revolutions_raw, &revolutions_int_d);
89  radian_offset_d = radian_offset_d * 2.0 * M_PI;
90 
91  int64_t revolutions_int = std::isnan(revolutions_int_d) ? 0 : static_cast<int64_t>(revolutions_int_d);
92  float radian_offset = static_cast<float>(radian_offset_d);
93  hebiCommandSetHighResAngle(internal_, field_, &revolutions_int, &radian_offset);
94 }
95 
96 void Command::HighResAngleField::set(int64_t revolutions, float radian_offset)
97 {
98  hebiCommandSetHighResAngle(internal_, field_, &revolutions, &radian_offset);
99 }
100 
102 {
103  hebiCommandSetHighResAngle(internal_, field_, nullptr, nullptr);
104 }
105 
107  : internal_(internal), field_(field)
108 {
109 }
110 
111 bool Command::NumberedFloatField::has(size_t fieldNumber) const
112 {
113  return (hebiCommandGetNumberedFloat(internal_, field_, fieldNumber, nullptr) == HebiStatusSuccess);
114 }
115 
116 float Command::NumberedFloatField::get(size_t fieldNumber) const
117 {
118  float ret;
120  {
121  ret = std::numeric_limits<float>::quiet_NaN();
122  }
123  return ret;
124 }
125 
126 void Command::NumberedFloatField::set(size_t fieldNumber, float value)
127 {
128  hebiCommandSetNumberedFloat(internal_, field_, fieldNumber, &value);
129 }
130 
131 void Command::NumberedFloatField::clear(size_t fieldNumber)
132 {
133  hebiCommandSetNumberedFloat(internal_, field_, fieldNumber, nullptr);
134 }
135 
137  : internal_(internal), field_(field)
138 {
139 }
140 
142 {
143  return (hebiCommandGetBool(internal_, field_, nullptr) == HebiStatusSuccess);
144 }
145 
147 {
148  int ret;
150  {
151  ret = 0;
152  }
153  return static_cast<bool>(ret);
154 }
155 
156 void Command::BoolField::set(bool value)
157 {
158  auto val = static_cast<int>(value);
160 }
161 
163 {
165 }
166 
168  : internal_(internal), field_(field)
169 {
170 }
171 
172 Command::StringField::operator bool() const
173 {
174  return has();
175 }
176 
178 {
179  return (hebiCommandGetString(internal_, field_, nullptr, nullptr) == HebiStatusSuccess);
180 }
181 
182 std::string Command::StringField::get() const
183 {
184  // Get the size first
185  size_t length;
186  if (hebiCommandGetString(internal_, field_, nullptr, &length) != HebiStatusSuccess)
187  {
188  // String field doesn't exist -- return an empty string
189  return "";
190  }
191  auto buffer = new char [length];
192  hebiCommandGetString(internal_, field_, buffer, &length);
193  std::string tmp(buffer, length - 1);
194  delete[] buffer;
195  return tmp;
196 }
197 
198 void Command::StringField::set(const std::string& value)
199 {
200  const char* buffer = value.c_str();
201  size_t length = value.size();
202  hebiCommandSetString(internal_, field_, buffer, &length);
203 }
204 
206 {
207  hebiCommandSetString(internal_, field_, nullptr, nullptr);
208 }
209 
211  : internal_(internal), field_(field)
212 {
213 }
214 
215 Command::FlagField::operator bool() const
216 {
217  return has();
218 }
219 
221 {
222  return hebiCommandGetFlag(internal_, field_) == 1;
223 }
224 
226 {
228 }
229 
231 {
233 }
234 
236  : internal_(internal), bank_(bank)
237 {
238 }
239 
240 bool Command::IoBank::hasInt(size_t pinNumber) const
241 {
242  return (hebiCommandGetIoPinInt(internal_, bank_, pinNumber, nullptr) == HebiStatusSuccess);
243 }
244 
245 bool Command::IoBank::hasFloat(size_t pinNumber) const
246 {
247  return (hebiCommandGetIoPinFloat(internal_, bank_, pinNumber, nullptr) == HebiStatusSuccess);
248 }
249 
250 int64_t Command::IoBank::getInt(size_t pinNumber) const
251 {
252  int64_t ret;
253  hebiCommandGetIoPinInt(internal_, bank_, pinNumber, &ret);
254  return ret;
255 }
256 
257 float Command::IoBank::getFloat(size_t pinNumber) const
258 {
259  float ret;
260  hebiCommandGetIoPinFloat(internal_, bank_, pinNumber, &ret);
261  return ret;
262 }
263 
264 void Command::IoBank::setInt(size_t pinNumber, int64_t value)
265 {
266  hebiCommandSetIoPinInt(internal_, bank_, pinNumber, &value);
267 }
268 
269 void Command::IoBank::setFloat(size_t pinNumber, float value)
270 {
271  hebiCommandSetIoPinFloat(internal_, bank_, pinNumber, &value);
272 }
273 
274 void Command::IoBank::clear(size_t pinNumber)
275 {
276  hebiCommandSetIoPinInt(internal_, bank_, pinNumber, nullptr);
277  hebiCommandSetIoPinFloat(internal_, bank_, pinNumber, nullptr);
278 }
279 
281  : internal_(internal), field_(field)
282 {
283 }
284 
286 {
287  return
288  (hebiCommandGetLedColor(internal_, field_, nullptr, nullptr, nullptr) == HebiStatusSuccess) ||
290 }
291 
293 {
294  bool module_ctrl = hebiCommandHasLedModuleControl(internal_, field_) == 1;
295  uint8_t r, g, b;
297  return Color(r, g, b, module_ctrl ? 0 : 255);
298 }
299 
300 void Command::LedField::set(const Color& new_color)
301 {
302  if (new_color.getAlpha() == 0)
303  {
305  }
306  else
307  {
309  new_color.getRed(), new_color.getGreen(), new_color.getBlue());
310  }
311 }
312 
314 {
316 }
317 
319  : internal_(command),
320  io_(internal_),
328 {
329 }
331 {
332 }
333 
335  : internal_(other.internal_),
336  io_(internal_),
344 {
345  // NOTE: it would be nice to also cleanup the actual internal pointer of other
346  // but alas we cannot change a const variable.
347 }
348 
350 {
351  return debug_;
352 }
354 {
355  return reset_;
356 }
358 {
359  return boot_;
360 }
362 {
363  return stop_boot_;
364 }
366 {
367  return led_;
368 }
369 
370 } // namespace hebi
void clear()
Removes any currently set value for this field.
Definition: command.cpp:162
void set(const Color &color)
Commands a color that overrides the module&#39;s control of the LED (if the alpha channel is 255)...
Definition: command.cpp:300
void hebiCommandSetLedOverrideColor(HebiCommandPtr cmd, HebiCommandLedField field, uint8_t r, uint8_t g, uint8_t b)
void clear()
Removes any currently set value for this field.
Definition: command.cpp:101
NumberedFloatField(HebiCommandPtr internal, HebiCommandNumberedFloatField field)
Definition: command.cpp:106
HebiStatusCode hebiCommandGetNumberedFloat(HebiCommandPtr cmd, HebiCommandNumberedFloatField field, size_t number, float *value)
HebiCommandBoolField const field_
Definition: command.hpp:209
HebiCommandPtr const internal_
Definition: command.hpp:208
HebiCommandPtr const internal_
Definition: command.hpp:144
HebiCommandHighResAngleField
Definition: hebi.h:79
void hebiCommandSetBool(HebiCommandPtr cmd, HebiCommandBoolField field, const int32_t *value)
Boot the module from bootloader into application.
Definition: hebi.h:104
HebiStatusCode hebiCommandGetIoPinFloat(HebiCommandPtr cmd, HebiCommandIoPinBank field, size_t pin_number, float *value)
void hebiCommandClearLed(HebiCommandPtr cmd, HebiCommandLedField field)
LedField(HebiCommandPtr internal, HebiCommandLedField field)
Definition: command.cpp:280
float get() const
If the field has a value, returns that value; otherwise, returns a default.
Definition: command.cpp:26
bool has() const
Returns true if the LED command has been set, and false otherwise.
Definition: command.cpp:285
void clear()
Removes any currently set value for this field.
Definition: command.cpp:41
HebiCommandIoPinBank const bank_
Definition: command.hpp:376
HebiCommandPtr const internal_
Definition: command.hpp:375
HebiCommandPtr const internal_
Definition: command.hpp:79
FlagField & stopBoot()
Stop the module from automatically booting into application.
Definition: command.cpp:361
uint8_t getRed() const
Returns the red channel; value is between 0 and 255.
Definition: color.hpp:30
void hebiCommandSetLedModuleControl(HebiCommandPtr cmd, HebiCommandLedField field)
std::string get() const
If the field has a value, returns a copy of that value; otherwise, returns a default.
Definition: command.cpp:182
void setInt(size_t pinNumber, int64_t value)
Sets the particular pin to a integer value (representing a digital output).
Definition: command.cpp:264
Command objects have various fields that can be set; when sent to the module, these fields control in...
Definition: command.hpp:30
~Command() noexcept
Cleans up command object as necessary.
Definition: command.cpp:330
Command(HebiCommandPtr)
Wraps an existing C-style object that is managed by its parent. NOTE: this should not be used except ...
Definition: command.cpp:318
void hebiCommandSetFlag(HebiCommandPtr cmd, HebiCommandFlagField field, int32_t value)
Definition: color.hpp:5
float getFloat(size_t pinNumber) const
If this numbered pin in this bank has an floating point (e.g., analog or PWM) value, returns that value; otherwise returns a default.
Definition: command.cpp:257
int64_t getInt(size_t pinNumber) const
If this numbered pin in this bank has an integer (e.g., digital) value, returns that value; otherwise...
Definition: command.cpp:250
void clear(size_t fieldNumber)
Removes any currently set value for the numbered subvalue of this field.
Definition: command.cpp:131
void set(size_t fieldNumber, float value)
Sets the particular numbered subvalue of this field to a given value.
Definition: command.cpp:126
void set()
Sets this flag.
Definition: command.cpp:225
A message field for interfacing with an LED.
Definition: command.hpp:381
bool has(size_t fieldNumber) const
True if (and only if) the particular numbered subvalue of this field has a value. ...
Definition: command.cpp:111
FloatField(HebiCommandPtr internal, HebiCommandFloatField field)
Definition: command.cpp:11
HebiCommandNumberedFloatField const field_
Definition: command.hpp:185
HebiStatusCode hebiCommandGetBool(HebiCommandPtr cmd, HebiCommandBoolField field, int32_t *value)
HebiCommandPtr const internal_
Definition: command.hpp:279
Restart the module.
Definition: hebi.h:103
HebiCommandHighResAngleField const field_
Definition: command.hpp:145
HebiCommandIoPinBank
Definition: hebi.h:111
HebiCommandFloatField const field_
Definition: command.hpp:80
int32_t hebiCommandGetFlag(HebiCommandPtr cmd, HebiCommandFlagField field)
FlagField & reset()
Restart the module.
Definition: command.cpp:353
uint8_t getAlpha() const
Definition: color.hpp:39
HebiStatusCode hebiCommandGetIoPinInt(HebiCommandPtr cmd, HebiCommandIoPinBank field, size_t pin_number, int64_t *value)
HebiStatusCode hebiCommandGetLedColor(HebiCommandPtr cmd, HebiCommandLedField field, uint8_t *r, uint8_t *g, uint8_t *b)
HebiStatusCode hebiCommandGetString(HebiCommandPtr cmd, HebiCommandStringField field, char *buffer, size_t *length)
const mpreal modf(const mpreal &v, mpreal &n)
Definition: mpreal.h:2094
A two-state message field (either set/true or cleared/false).
Definition: command.hpp:252
FlagField reset_
Definition: command.hpp:735
IoBank(HebiCommandPtr internal, HebiCommandIoPinBank bank)
Definition: command.cpp:235
void hebiCommandSetHighResAngle(HebiCommandPtr cmd, HebiCommandHighResAngleField field, const int64_t *int_part, const float *dec_part)
A message field containing a numbered set of single-precision floating point values.
Definition: command.hpp:152
HebiCommandFlagField const field_
Definition: command.hpp:280
HebiCommandPtr const internal_
Definition: command.hpp:245
#define M_PI
Definition: hebi.h:12
HebiCommandFloatField
Definition: hebi.h:32
bool has() const
True if (and only if) the field has a value.
Definition: command.cpp:177
HebiCommandStringField
Definition: hebi.h:95
HighResAngleField(HebiCommandPtr internal, HebiCommandHighResAngleField field)
Definition: command.cpp:46
HebiCommandNumberedFloatField
Definition: hebi.h:85
HebiCommandLedField const field_
Definition: command.hpp:417
HebiCommandPtr const internal_
Definition: command.hpp:184
int32_t hebiCommandHasLedModuleControl(HebiCommandPtr cmd, HebiCommandLedField field)
HebiStatusCode hebiCommandGetFloat(HebiCommandPtr cmd, HebiCommandFloatField field, float *value)
HebiCommandFlagField
Definition: hebi.h:100
uint8_t getBlue() const
Returns the blue channel; value is between 0 and 255.
Definition: color.hpp:34
bool hasFloat(size_t pinNumber) const
True if (and only if) the particular numbered pin in this bank has an floating point (e...
Definition: command.cpp:245
LedField led_
Definition: command.hpp:738
void hebiCommandSetFloat(HebiCommandPtr cmd, HebiCommandFloatField field, const float *value)
void hebiCommandSetNumberedFloat(HebiCommandPtr cmd, HebiCommandNumberedFloatField field, size_t number, const float *value)
uint8_t getGreen() const
Returns the green channel; value is between 0 and 255.
Definition: color.hpp:32
Actuator actuator_
Definition: command.hpp:732
bool get() const
If the field has a value, returns that value; otherwise, returns false.
Definition: command.cpp:146
FlagField(HebiCommandPtr internal, HebiCommandFlagField field)
Definition: command.cpp:210
FlagField stop_boot_
Definition: command.hpp:737
bool has() const
True if (and only if) the field has a value.
Definition: command.cpp:21
void clear()
Removes any currently set value for this field, so that the module maintains its previous state of LE...
Definition: command.cpp:313
void set(double radians)
Sets the field to a given double value (in radians). Note that double precision floating point number...
Definition: command.cpp:84
FlagField boot_
Definition: command.hpp:736
FlagField & boot()
Boot the module from bootloader into application.
Definition: command.cpp:357
void clear()
Removes any currently set value for this field.
Definition: command.cpp:205
StringField(HebiCommandPtr internal, HebiCommandStringField field)
Definition: command.cpp:167
NumberedFloatField & debug()
Values for internal debug functions (channel 1-9 available).
Definition: command.cpp:349
void set(float value)
Sets the field to a given value.
Definition: command.cpp:36
struct _HebiCommand * HebiCommandPtr
The C-style&#39;s API representation of a command.
Definition: hebi.h:315
Settings settings_
Definition: command.hpp:731
void hebiCommandSetIoPinInt(HebiCommandPtr cmd, HebiCommandIoPinBank field, size_t pin_number, const int64_t *value)
void set(bool value)
Sets the field to a given value.
Definition: command.cpp:156
BoolField(HebiCommandPtr internal, HebiCommandBoolField field)
Definition: command.cpp:136
NumberedFloatField debug_
Definition: command.hpp:734
float get(size_t fieldNumber) const
If the particular numbered subvalue of this field has a value, returns that value; otherwise returns ...
Definition: command.cpp:116
double get() const
If the field has a value, returns that value as a double; otherwise, returns a default.
Definition: command.cpp:61
void hebiCommandSetIoPinFloat(HebiCommandPtr cmd, HebiCommandIoPinBank field, size_t pin_number, const float *value)
HebiCommandStringField const field_
Definition: command.hpp:246
bool has() const
Returns true if the flag is set, false if it is cleared.
Definition: command.cpp:220
void setFloat(size_t pinNumber, float value)
Sets the particular pin to a floating point value (representing a PWM output).
Definition: command.cpp:269
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
void hebiCommandSetString(HebiCommandPtr cmd, HebiCommandStringField field, const char *buffer, const size_t *length)
bool has() const
True if (and only if) the field has a value.
Definition: command.cpp:141
HebiCommandLedField
Definition: hebi.h:120
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool() isnan(const half &a)
Definition: Half.h:372
HebiCommandBoolField
Definition: hebi.h:89
Structure to describe an RGB color.
Definition: color.hpp:8
Color get() const
Returns the current LED command.
Definition: command.cpp:292
EIGEN_DEVICE_FUNC const Scalar & b
bool has() const
True if (and only if) the field has a value.
Definition: command.cpp:56
Indicates if the module should save the current values of all of its settings.
Definition: hebi.h:102
bool hasInt(size_t pinNumber) const
True if (and only if) the particular numbered pin in this bank has an integer (e.g., digital) value.
Definition: command.cpp:240
void clear()
Clears this flag (e.g., sets it to false/off).
Definition: command.cpp:230
HebiCommandPtr const internal_
Definition: command.hpp:416
HebiStatusCode hebiCommandGetHighResAngle(HebiCommandPtr cmd, HebiCommandHighResAngleField field, int64_t *int_part, float *dec_part)
void clear(size_t pinNumber)
Removes any currently set value for this pin.
Definition: command.cpp:274
void set(const std::string &value)
Sets the field to a given value.
Definition: command.cpp:198
HebiCommandPtr internal_
Definition: command.hpp:667
LedField & led()
The module&#39;s LED.
Definition: command.cpp:365


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:03