command.cpp
Go to the documentation of this file.
1 #include "command.hpp"
2 
3 #include <cmath>
4 #include <limits>
5 
6 #ifndef M_PI
7 #define M_PI 3.14159265358979323846
8 #endif
9 
10 namespace hebi {
11 
13  : internal_(internal), field_(field) {}
14 
15 bool Command::FloatField::has() const { return (floatGetter(internal_, field_, nullptr) == HebiStatusSuccess); }
16 
17 float Command::FloatField::get() const {
18  float ret;
19  if (floatGetter(internal_, field_, &ret) != HebiStatusSuccess) {
20  ret = std::numeric_limits<float>::quiet_NaN();
21  }
22  return ret;
23 }
24 
25 void Command::FloatField::set(float value) { hebiCommandSetFloat(internal_, field_, &value); }
26 
28 
30  : internal_(internal), field_(field) {}
31 
33  return (highResAngleGetter(internal_, field_, nullptr, nullptr) == HebiStatusSuccess);
34 }
35 
37  int64_t revolutions;
38  float radian_offset;
39  if (highResAngleGetter(internal_, field_, &revolutions, &radian_offset) != HebiStatusSuccess) {
40  return std::numeric_limits<double>::quiet_NaN();
41  }
42  return (static_cast<double>(revolutions) * 2.0 * M_PI + static_cast<double>(radian_offset));
43 }
44 
45 void Command::HighResAngleField::get(int64_t* revolutions, float* radian_offset) const {
46  if (highResAngleGetter(internal_, field_, revolutions, radian_offset) != HebiStatusSuccess) {
47  *revolutions = 0;
48  *radian_offset = std::numeric_limits<float>::quiet_NaN();
49  }
50 }
51 
52 void Command::HighResAngleField::set(double radians) {
53  double revolutions_raw = radians / 2.0 / M_PI;
54  double revolutions_int_d;
55  double radian_offset_d = std::modf(revolutions_raw, &revolutions_int_d);
56  radian_offset_d = radian_offset_d * 2.0 * M_PI;
57 
58  int64_t revolutions_int = std::isnan(revolutions_int_d) ? 0 : static_cast<int64_t>(revolutions_int_d);
59  float radian_offset = static_cast<float>(radian_offset_d);
60  hebiCommandSetHighResAngle(internal_, field_, &revolutions_int, &radian_offset);
61 }
62 
63 void Command::HighResAngleField::set(int64_t revolutions, float radian_offset) {
64  hebiCommandSetHighResAngle(internal_, field_, &revolutions, &radian_offset);
65 }
66 
68 
70  : internal_(internal), field_(field) {}
71 
72 bool Command::NumberedFloatField::has(size_t fieldNumber) const {
73  return (numberedFloatGetter(internal_, field_, fieldNumber, nullptr) == HebiStatusSuccess);
74 }
75 
76 float Command::NumberedFloatField::get(size_t fieldNumber) const {
77  float ret;
78  if (numberedFloatGetter(internal_, field_, fieldNumber, &ret) != HebiStatusSuccess) {
79  ret = std::numeric_limits<float>::quiet_NaN();
80  }
81  return ret;
82 }
83 
84 void Command::NumberedFloatField::set(size_t fieldNumber, float value) {
85  hebiCommandSetNumberedFloat(internal_, field_, fieldNumber, &value);
86 }
87 
88 void Command::NumberedFloatField::clear(size_t fieldNumber) {
89  hebiCommandSetNumberedFloat(internal_, field_, fieldNumber, nullptr);
90 }
91 
93  : internal_(internal), field_(field) {}
94 
95 bool Command::BoolField::has() const { return (boolGetter(internal_, field_, nullptr) == HebiStatusSuccess); }
96 
98  bool ret{};
99  boolGetter(internal_, field_, &ret);
100  return static_cast<bool>(ret);
101 }
102 
103 void Command::BoolField::set(bool value) {
104  auto val = static_cast<int>(value);
105  hebiCommandSetBool(internal_, field_, &val);
106 }
107 
109 
111  : internal_(internal), field_(field) {}
112 
114  return (hebiCommandGetString(internal_, field_, nullptr, nullptr) == HebiStatusSuccess);
115 }
116 
117 std::string Command::StringField::get() const {
118  // Get the size first
119  size_t length;
120  if (hebiCommandGetString(internal_, field_, nullptr, &length) != HebiStatusSuccess) {
121  // String field doesn't exist -- return an empty string
122  return "";
123  }
124  auto buffer = new char[length];
125  hebiCommandGetString(internal_, field_, buffer, &length);
126  std::string tmp(buffer, length - 1);
127  delete[] buffer;
128  return tmp;
129 }
130 
131 void Command::StringField::set(const std::string& value) {
132  const char* buffer = value.c_str();
133  size_t length = value.size();
134  hebiCommandSetString(internal_, field_, buffer, &length);
135 }
136 
137 void Command::StringField::clear() { hebiCommandSetString(internal_, field_, nullptr, nullptr); }
138 
140  : internal_(internal), field_(field) {}
141 
142 bool Command::FlagField::has() const { return flagGetter(internal_, field_) == 1; }
143 
145 
147 
148 Command::IoBank::IoBank(HebiCommandRef& internal, HebiCommandIoPinBank bank) : internal_(internal), bank_(bank) {}
149 
150 bool Command::IoBank::hasInt(size_t pinNumber) const {
151  return (intIoPinGetter(internal_, bank_, pinNumber, nullptr) == HebiStatusSuccess);
152 }
153 
154 bool Command::IoBank::hasFloat(size_t pinNumber) const {
155  return (floatIoPinGetter(internal_, bank_, pinNumber, nullptr) == HebiStatusSuccess);
156 }
157 
158 int64_t Command::IoBank::getInt(size_t pinNumber) const {
159  int64_t ret;
160  intIoPinGetter(internal_, bank_, pinNumber, &ret);
161  return ret;
162 }
163 
164 float Command::IoBank::getFloat(size_t pinNumber) const {
165  float ret;
166  floatIoPinGetter(internal_, bank_, pinNumber, &ret);
167  return ret;
168 }
169 
170 void Command::IoBank::setInt(size_t pinNumber, int64_t value) {
171  hebiCommandSetIoPinInt(internal_, bank_, pinNumber, &value);
172 }
173 
174 void Command::IoBank::setFloat(size_t pinNumber, float value) {
175  hebiCommandSetIoPinFloat(internal_, bank_, pinNumber, &value);
176 }
177 
178 void Command::IoBank::clear(size_t pinNumber) {
179  hebiCommandSetIoPinInt(internal_, bank_, pinNumber, nullptr);
180  hebiCommandSetIoPinFloat(internal_, bank_, pinNumber, nullptr);
181 }
182 
183 Command::LedField::LedField(HebiCommandRef& internal, HebiCommandLedField field) : internal_(internal), field_(field) {}
184 
186  return ledGetter(internal_, field_, nullptr, nullptr, nullptr, nullptr) == HebiStatusSuccess;
187 }
188 
190  uint8_t r, g, b, a;
191  if (ledGetter(internal_, field_, &r, &g, &b, &a) != HebiStatusSuccess) {
192  r = 0;
193  g = 0;
194  b = 0;
195  a = 0;
196  }
197  return Color(r, g, b, a);
198 }
199 
200 void Command::LedField::set(const Color& new_color) {
201  hebiCommandSetLed(internal_, field_, &new_color);
202 }
203 
205  hebiCommandSetLed(internal_, field_, nullptr);
206 }
207 
209  : internal_(command),
221 }
222 
224  : internal_(other.internal_),
225  io_(internal_ref_),
226  settings_(internal_, internal_ref_),
227  actuator_(internal_ref_),
228  debug_(internal_ref_, HebiCommandNumberedFloatDebug),
229  append_log_(internal_, HebiCommandStringAppendLog),
230  reset_(internal_ref_, HebiCommandFlagReset),
231  boot_(internal_ref_, HebiCommandFlagBoot),
232  stop_boot_(internal_ref_, HebiCommandFlagStopBoot),
233  clear_log_(internal_ref_, HebiCommandFlagClearLog),
234  led_(internal_ref_, HebiCommandLedLed) {
235  // NOTE: it would be nice to also cleanup the actual internal pointer of other
236  // but alas we cannot change a const variable.
238 }
239 
240 } // namespace hebi
hebi::Command::StringField::has
bool has() const
True if (and only if) the field has a value.
Definition: command.cpp:113
hebi::floatGetter
HebiStatusCode floatGetter(const RefT &ref, MetadataT &metadata, int field, float *value)
Definition: message_helpers.cpp:21
HebiCommandFlagClearLog
@ HebiCommandFlagClearLog
Stop the module from automatically booting into application.
Definition: hebi.h:134
hebi::Command::internal_
HebiCommandPtr internal_
Definition: command.hpp:744
hebi::hebiCommandSetBool
void hebiCommandSetBool(HebiCommandRef &command, HebiCommandBoolField field, const int32_t *value)
Definition: message_helpers.cpp:457
hebi::Command::HighResAngleField::clear
void clear()
Removes any currently set value for this field.
Definition: command.cpp:67
hebi::Command::HighResAngleField::set
void set(double radians)
Sets the field to a given double value (in radians). Note that double precision floating point number...
Definition: command.cpp:52
hebi::Command::StringField::StringField
StringField(HebiCommandPtr internal, HebiCommandStringField field)
Definition: command.cpp:110
HebiCommandLedField
HebiCommandLedField
Definition: hebi.h:153
hebi::Color
Structure to describe an RGB color.
Definition: color.hpp:8
HebiCommandFlagReset
@ HebiCommandFlagReset
Indicates if the module should save the current values of all of its settings.
Definition: hebi.h:131
hebi::hebiCommandSetHighResAngle
void hebiCommandSetHighResAngle(HebiCommandRef &command, HebiCommandHighResAngleField field, const int64_t *int_part, const float *dec_part)
Definition: message_helpers.cpp:418
hebi::hebiCommandSetNumberedFloat
void hebiCommandSetNumberedFloat(HebiCommandRef &command, HebiCommandNumberedFloatField field, size_t number, const float *value)
Definition: message_helpers.cpp:436
HebiStatusSuccess
@ HebiStatusSuccess
Definition: hebi.h:24
HebiCommandStringAppendLog
@ HebiCommandStringAppendLog
The family for this module. The string must be null-terminated and less than 21 characters.
Definition: hebi.h:126
HebiCommandFlagBoot
@ HebiCommandFlagBoot
Restart the module.
Definition: hebi.h:132
HebiCommandFloatField
HebiCommandFloatField
Command Enums.
Definition: hebi.h:55
hebi::Command::boot_
FlagField boot_
Definition: command.hpp:827
hebi::ledGetter
HebiStatusCode ledGetter(const RefT &ref, MetadataT &metadata, int field, uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a)
Definition: message_helpers.cpp:356
hebi::hebiCommandSetIoPinFloat
void hebiCommandSetIoPinFloat(HebiCommandRef &command, HebiCommandIoPinBank bank, size_t pin_number, const float *value)
Definition: message_helpers.cpp:508
hebiCommandGetReference
void hebiCommandGetReference(HebiCommandPtr command, HebiCommandRef *ref)
hebi::Command::NumberedFloatField::set
void set(size_t fieldNumber, float value)
Sets the particular numbered subvalue of this field to a given value.
Definition: command.cpp:84
hebi::Command::LedField::clear
void clear()
Removes any currently set value for this field, so that the module maintains its previous state of LE...
Definition: command.cpp:204
hebi::Command::StringField::get
std::string get() const
If the field has a value, returns a copy of that value; otherwise, returns a default.
Definition: command.cpp:117
hebi::Command::StringField::clear
void clear()
Removes any currently set value for this field.
Definition: command.cpp:137
hebi::Command::IoBank::hasInt
bool hasInt(size_t pinNumber) const
True if (and only if) the particular numbered pin in this bank has an integer (e.g....
Definition: command.cpp:150
hebi::Command::NumberedFloatField::NumberedFloatField
NumberedFloatField(HebiCommandRef &internal, HebiCommandNumberedFloatField field)
Definition: command.cpp:69
HebiCommandNumberedFloatField
HebiCommandNumberedFloatField
Definition: hebi.h:112
hebi::Command::BoolField::set
void set(bool value)
Sets the field to a given value.
Definition: command.cpp:103
command.hpp
hebi::Command::internal_ref_
HebiCommandRef internal_ref_
Definition: command.hpp:745
HebiCommandFlagStopBoot
@ HebiCommandFlagStopBoot
Boot the module from bootloader into application.
Definition: hebi.h:133
hebi::Command::HighResAngleField::has
bool has() const
True if (and only if) the field has a value.
Definition: command.cpp:32
hebi::flagGetter
bool flagGetter(const RefT &ref, MetadataT &metadata, int field)
Definition: message_helpers.cpp:246
hebi::hebiCommandSetLed
void hebiCommandSetLed(HebiCommandRef &command, HebiCommandLedField field, const Color *color)
Definition: message_helpers.cpp:530
HebiCommandFlagField
HebiCommandFlagField
Definition: hebi.h:129
hebi::Command::HighResAngleField::HighResAngleField
HighResAngleField(HebiCommandRef &internal, HebiCommandHighResAngleField field)
Definition: command.cpp:29
hebi::Command::clear_log_
FlagField clear_log_
Definition: command.hpp:829
hebi::Command::NumberedFloatField::clear
void clear(size_t fieldNumber)
Removes any currently set value for the numbered subvalue of this field.
Definition: command.cpp:88
HebiCommandPtr
struct HebiCommand_ * HebiCommandPtr
Typedefs.
Definition: hebi.h:444
hebi::Command::BoolField::has
bool has() const
True if (and only if) the field has a value.
Definition: command.cpp:95
HebiCommandLedLed
@ HebiCommandLedLed
Definition: hebi.h:154
hebi::Command::BoolField::get
bool get() const
If the field has a value, returns that value; otherwise, returns false.
Definition: command.cpp:97
M_PI
#define M_PI
Definition: command.cpp:7
hebiCommandSetString
void hebiCommandSetString(HebiCommandPtr command, HebiCommandStringField field, const char *buffer, const size_t *length)
hebi::Command::HighResAngleField::get
double get() const
If the field has a value, returns that value as a double; otherwise, returns a default.
Definition: command.cpp:36
HebiCommandNumberedFloatDebug
@ HebiCommandNumberedFloatDebug
Definition: hebi.h:113
hebi::Command::debug_
NumberedFloatField debug_
Definition: command.hpp:824
hebi::highResAngleGetter
HebiStatusCode highResAngleGetter(const RefT &ref, MetadataT &metadata, int field, int64_t *revs, float *offset)
Definition: message_helpers.cpp:50
hebi::Command::FlagField::clear
void clear()
Clears this flag (e.g., sets it to false/off).
Definition: command.cpp:146
hebi::hebiCommandSetIoPinInt
void hebiCommandSetIoPinInt(HebiCommandRef &command, HebiCommandIoPinBank bank, size_t pin_number, const int64_t *value)
Definition: message_helpers.cpp:486
hebi::Command::actuator_
Actuator actuator_
Definition: command.hpp:822
hebi::Command::IoBank::setFloat
void setFloat(size_t pinNumber, float value)
Sets the particular pin to a floating point value (representing a PWM output).
Definition: command.cpp:174
hebi::Command::FloatField::set
void set(float value)
Sets the field to a given value.
Definition: command.cpp:25
hebi::Command::IoBank::getInt
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:158
hebi::Command::LedField::get
Color get() const
Returns the current LED command.
Definition: command.cpp:189
hebi
Definition: arm.cpp:5
hebi::boolGetter
HebiStatusCode boolGetter(const RefT &ref, MetadataT &metadata, int field, bool *value)
Definition: message_helpers.cpp:269
hebi::Command::FloatField::has
bool has() const
True if (and only if) the field has a value.
Definition: command.cpp:15
HebiCommandHighResAngleField
HebiCommandHighResAngleField
Definition: hebi.h:106
hebi::Command::FlagField::FlagField
FlagField(HebiCommandRef &internal, HebiCommandFlagField field)
Definition: command.cpp:139
HebiCommandBoolField
HebiCommandBoolField
Definition: hebi.h:116
hebi::Command::io_
Io io_
Definition: command.hpp:820
hebi::Command
Command objects have various fields that can be set; when sent to the module, these fields control in...
Definition: command.hpp:33
hebi::Command::settings_
Settings settings_
Definition: command.hpp:821
hebi::Command::BoolField::clear
void clear()
Removes any currently set value for this field.
Definition: command.cpp:108
HebiCommandStringField
HebiCommandStringField
Definition: hebi.h:123
HebiCommandIoPinBank
HebiCommandIoPinBank
Definition: hebi.h:144
hebi::Command::FloatField::FloatField
FloatField(HebiCommandRef &internal, HebiCommandFloatField field)
Definition: command.cpp:12
hebi::Command::IoBank::clear
void clear(size_t pinNumber)
Removes any currently set value for this pin.
Definition: command.cpp:178
hebi::Command::IoBank::setInt
void setInt(size_t pinNumber, int64_t value)
Sets the particular pin to a integer value (representing a digital output).
Definition: command.cpp:170
hebi::Command::append_log_
StringField append_log_
Definition: command.hpp:825
hebiCommandGetString
HebiStatusCode hebiCommandGetString(HebiCommandPtr command, HebiCommandStringField field, char *buffer, size_t *length)
Command API.
hebi::Command::led_
LedField led_
Definition: command.hpp:830
hebi::Command::NumberedFloatField::has
bool has(size_t fieldNumber) const
True if (and only if) the particular numbered subvalue of this field has a value.
Definition: command.cpp:72
hebi::Command::Command
Command(HebiCommandPtr)
Wraps an existing C-style object that is managed by its parent. NOTE: this should not be used except ...
Definition: command.cpp:208
hebi::hebiCommandSetFlag
void hebiCommandSetFlag(HebiCommandRef &command, HebiCommandFlagField field, int32_t value)
Definition: message_helpers.cpp:472
hebi::Command::BoolField::BoolField
BoolField(HebiCommandRef &internal, HebiCommandBoolField field)
Definition: command.cpp:92
hebi::Command::FloatField::get
float get() const
If the field has a value, returns that value; otherwise, returns a default.
Definition: command.cpp:17
hebi::Command::IoBank::IoBank
IoBank(HebiCommandRef &internal, HebiCommandIoPinBank bank)
Definition: command.cpp:148
hebi::intIoPinGetter
HebiStatusCode intIoPinGetter(const RefT &ref, MetadataT &metadata, int index, size_t pin_number, int64_t *value)
Definition: message_helpers.cpp:209
hebi::Command::NumberedFloatField::get
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:76
hebi::Command::IoBank::getFloat
float getFloat(size_t pinNumber) const
If this numbered pin in this bank has an floating point (e.g., analog or PWM) value,...
Definition: command.cpp:164
hebi::Command::LedField::has
bool has() const
Returns true if the LED command has been set, and false otherwise.
Definition: command.cpp:185
hebi::Command::stop_boot_
FlagField stop_boot_
Definition: command.hpp:828
hebi::Command::StringField::set
void set(const std::string &value)
Sets the field to a given value.
Definition: command.cpp:131
HebiCommandRef_
Definition: hebi.h:632
hebi::numberedFloatGetter
HebiStatusCode numberedFloatGetter(const RefT &ref, MetadataT &metadata, int field, size_t number, float *value)
Definition: message_helpers.cpp:81
hebi::Command::FlagField::has
bool has() const
Returns true if the flag is set, false if it is cleared.
Definition: command.cpp:142
hebi::Command::reset_
FlagField reset_
Definition: command.hpp:826
hebi::Command::FloatField::clear
void clear()
Removes any currently set value for this field.
Definition: command.cpp:27
hebi::hebiCommandSetFloat
void hebiCommandSetFloat(HebiCommandRef &command, HebiCommandFloatField field, const float *value)
Definition: message_helpers.cpp:403
hebi::floatIoPinGetter
HebiStatusCode floatIoPinGetter(const RefT &ref, MetadataT &metadata, int index, size_t pin_number, float *value)
Definition: message_helpers.cpp:172
hebi::Command::IoBank::hasFloat
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:154
hebi::Command::FlagField::set
void set()
Sets this flag.
Definition: command.cpp:144
hebi::Command::LedField::set
void set(const Color &color)
Commands a color that overrides the module's control of the LED (if the alpha channel is 255),...
Definition: command.cpp:200
hebi::Command::LedField::LedField
LedField(HebiCommandRef &internal, HebiCommandLedField field)
Definition: command.cpp:183


hebi_cpp_api_ros
Author(s): Chris Bollinger , Matthew Tesch
autogenerated on Fri Aug 2 2024 08:35:18