HEBI C++ API  1.0.0
command.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "hebi.h"
4 #include "color.hpp"
5 #include <string>
6 #include "util.hpp"
7 #include "gains.hpp"
8 
9 namespace hebi {
10 
30 class Command final
31 {
32  public:
33  enum class ControlStrategy {
35  Off,
37  DirectPWM,
39  Strategy2,
41  Strategy3,
43  Strategy4,
44  };
45 
46  // Note: this is 'protected' instead of 'private' for easier use with Doxygen
47  protected:
49  class FloatField final
50  {
51  public:
52  #ifndef DOXYGEN_OMIT_INTERNAL
53  FloatField(HebiCommandPtr internal, HebiCommandFloatField field);
54  #endif // DOXYGEN_OMIT_INTERNAL
55  explicit operator bool() const;
69  bool has() const;
72  float get() const;
74  void set(float value);
76  void clear();
77 
78  private:
79  HebiCommandPtr const internal_;
80  HebiCommandFloatField const field_;
81 
83  };
89  class HighResAngleField final
90  {
91  public:
92  #ifndef DOXYGEN_OMIT_INTERNAL
93  HighResAngleField(HebiCommandPtr internal, HebiCommandHighResAngleField field);
94  #endif // DOXYGEN_OMIT_INTERNAL
95  explicit operator bool() const;
109  bool has() const;
115  double get() const;
127  void get(int64_t* revolutions, float* radian_offset) const;
132  void set(double radians);
139  void set(int64_t revolutions, float radian_offset);
141  void clear();
142 
143  private:
144  HebiCommandPtr const internal_;
145  HebiCommandHighResAngleField const field_;
146 
148  };
149 
152  class NumberedFloatField final
153  {
154  public:
155  #ifndef DOXYGEN_OMIT_INTERNAL
156  NumberedFloatField(HebiCommandPtr internal, HebiCommandNumberedFloatField field);
157  #endif // DOXYGEN_OMIT_INTERNAL
158  bool has(size_t fieldNumber) const;
169  float get(size_t fieldNumber) const;
175  void set(size_t fieldNumber, float value);
181  void clear(size_t fieldNumber);
182 
183  private:
184  HebiCommandPtr const internal_;
185  HebiCommandNumberedFloatField const field_;
186 
188  };
189 
191  class BoolField final
192  {
193  public:
194  #ifndef DOXYGEN_OMIT_INTERNAL
195  BoolField(HebiCommandPtr internal, HebiCommandBoolField field);
196  #endif // DOXYGEN_OMIT_INTERNAL
197  bool has() const;
201  bool get() const;
203  void set(bool value);
205  void clear();
206 
207  private:
208  HebiCommandPtr const internal_;
209  HebiCommandBoolField const field_;
210 
212  };
213 
215  class StringField final
216  {
217  public:
218  #ifndef DOXYGEN_OMIT_INTERNAL
219  StringField(HebiCommandPtr internal, HebiCommandStringField field);
220  #endif // DOXYGEN_OMIT_INTERNAL
221  explicit operator bool() const;
235  bool has() const;
238  std::string get() const;
240  void set(const std::string& value);
242  void clear();
243 
244  private:
245  HebiCommandPtr const internal_;
246  HebiCommandStringField const field_;
247 
249  };
250 
252  class FlagField final
253  {
254  public:
255  #ifndef DOXYGEN_OMIT_INTERNAL
256  FlagField(HebiCommandPtr internal, HebiCommandFlagField field);
257  #endif // DOXYGEN_OMIT_INTERNAL
258  explicit operator bool() const;
272  bool has() const;
274  void set();
276  void clear();
277 
278  private:
279  HebiCommandPtr const internal_;
280  HebiCommandFlagField const field_;
281 
283  };
284 
286  template <typename T>
287  class EnumField final
288  {
289  public:
290  #ifndef DOXYGEN_OMIT_INTERNAL
291  EnumField(HebiCommandPtr internal, HebiCommandEnumField field)
292  : internal_(internal), field_(field) {}
293  #endif // DOXYGEN_OMIT_INTERNAL
294  explicit operator bool() const { return has(); }
308  bool has() const { return (hebiCommandGetEnum(internal_, field_, nullptr) == HebiStatusSuccess); }
311  T get() const { int32_t ret{}; hebiCommandGetEnum(internal_, field_, &ret); return static_cast<T>(ret); }
313  void set(T _value) { int32_t value = static_cast<int32_t>(_value); hebiCommandSetEnum(internal_, field_, &value); }
315  void clear() { hebiCommandSetEnum(internal_, field_, nullptr); }
316 
317  private:
318  HebiCommandPtr const internal_;
319  HebiCommandEnumField const field_;
320 
322  };
323 
325  class IoBank final
326  {
327  public:
328  #ifndef DOXYGEN_OMIT_INTERNAL
329  IoBank(HebiCommandPtr internal, HebiCommandIoPinBank bank);
330  #endif // DOXYGEN_OMIT_INTERNAL
331  bool hasInt(size_t pinNumber) const;
342  bool hasFloat(size_t pinNumber) const;
348  int64_t getInt(size_t pinNumber) const;
355  float getFloat(size_t pinNumber) const;
361  void setInt(size_t pinNumber, int64_t value);
367  void setFloat(size_t pinNumber, float value);
372  void clear(size_t pinNumber);
373 
374  private:
375  HebiCommandPtr const internal_;
376  HebiCommandIoPinBank const bank_;
377 
379  };
381  class LedField final
382  {
383  public:
384  #ifndef DOXYGEN_OMIT_INTERNAL
385  LedField(HebiCommandPtr internal, HebiCommandLedField field);
386  #endif // DOXYGEN_OMIT_INTERNAL
387  bool has() const;
402  Color get() const;
408  void set(const Color& color);
413  void clear();
414 
415  private:
416  HebiCommandPtr const internal_;
417  HebiCommandLedField const field_;
418 
420  };
421 
423  class Io final
424  {
425  public:
426  #ifndef DOXYGEN_OMIT_INTERNAL
427  Io(HebiCommandPtr internal)
428  : internal_(internal),
429  a_(internal, HebiCommandIoBankA),
430  b_(internal, HebiCommandIoBankB),
431  c_(internal, HebiCommandIoBankC),
432  d_(internal, HebiCommandIoBankD),
433  e_(internal, HebiCommandIoBankE),
434  f_(internal, HebiCommandIoBankF)
435  {
436  }
437  #endif // DOXYGEN_OMIT_INTERNAL
438 
439  // With all submessage and field getters: Note that the returned reference
440  // should not be used after the lifetime of this parent.
441 
442  // Subfields ----------------
443 
445  IoBank& a() { return a_; }
447  const IoBank& a() const { return a_; }
449  IoBank& b() { return b_; }
451  const IoBank& b() const { return b_; }
453  IoBank& c() { return c_; }
455  const IoBank& c() const { return c_; }
457  IoBank& d() { return d_; }
459  const IoBank& d() const { return d_; }
461  IoBank& e() { return e_; }
463  const IoBank& e() const { return e_; }
465  IoBank& f() { return f_; }
467  const IoBank& f() const { return f_; }
468 
469  private:
470  HebiCommandPtr const internal_;
471 
472  IoBank a_;
473  IoBank b_;
474  IoBank c_;
475  IoBank d_;
476  IoBank e_;
477  IoBank f_;
478 
480  };
481 
483 
485  class Settings final
486  {
487  // Note: this is 'protected' instead of 'private' for easier use with Doxygen
488  protected:
490  class Actuator final
491  {
492  public:
493  #ifndef DOXYGEN_OMIT_INTERNAL
494  Actuator(HebiCommandPtr internal)
495  : internal_(internal),
496  position_gains_(internal, HebiCommandFloatPositionKp, HebiCommandBoolPositionDOnError),
497  velocity_gains_(internal, HebiCommandFloatVelocityKp, HebiCommandBoolVelocityDOnError),
498  effort_gains_(internal, HebiCommandFloatEffortKp, HebiCommandBoolEffortDOnError),
499  spring_constant_(internal, HebiCommandFloatSpringConstant),
500  reference_position_(internal, HebiCommandFloatReferencePosition),
501  reference_effort_(internal, HebiCommandFloatReferenceEffort),
502  control_strategy_(internal, HebiCommandEnumControlStrategy)
503  {
504  }
505  #endif // DOXYGEN_OMIT_INTERNAL
506 
507  // With all submessage and field getters: Note that the returned reference
508  // should not be used after the lifetime of this parent.
509 
510  // Submessages ----------------
511 
513  CommandGains& positionGains() { return position_gains_; }
515  const CommandGains& positionGains() const { return position_gains_; }
517  CommandGains& velocityGains() { return velocity_gains_; }
519  const CommandGains& velocityGains() const { return velocity_gains_; }
521  CommandGains& effortGains() { return effort_gains_; }
523  const CommandGains& effortGains() const { return effort_gains_; }
524 
525  // Subfields ----------------
526 
528  FloatField& springConstant() { return spring_constant_; }
530  const FloatField& springConstant() const { return spring_constant_; }
532  FloatField& referencePosition() { return reference_position_; }
534  const FloatField& referencePosition() const { return reference_position_; }
536  FloatField& referenceEffort() { return reference_effort_; }
538  const FloatField& referenceEffort() const { return reference_effort_; }
540  EnumField<ControlStrategy>& controlStrategy() { return control_strategy_; }
542  const EnumField<ControlStrategy>& controlStrategy() const { return control_strategy_; }
543 
544  private:
545  HebiCommandPtr const internal_;
546 
547  CommandGains position_gains_;
548  CommandGains velocity_gains_;
549  CommandGains effort_gains_;
550 
551  FloatField spring_constant_;
552  FloatField reference_position_;
553  FloatField reference_effort_;
554  EnumField<ControlStrategy> control_strategy_;
555 
557  };
558 
559  public:
560  #ifndef DOXYGEN_OMIT_INTERNAL
561  Settings(HebiCommandPtr internal)
562  : internal_(internal),
563  actuator_(internal),
564  name_(internal, HebiCommandStringName),
565  family_(internal, HebiCommandStringFamily),
566  save_current_settings_(internal, HebiCommandFlagSaveCurrentSettings)
567  {
568  }
569  #endif // DOXYGEN_OMIT_INTERNAL
570 
571  // With all submessage and field getters: Note that the returned reference
572  // should not be used after the lifetime of this parent.
573 
574  // Submessages ----------------
575 
577  Actuator& actuator() { return actuator_; }
579  const Actuator& actuator() const { return actuator_; }
580 
581  // Subfields ----------------
582 
584  StringField& name() { return name_; }
586  const StringField& name() const { return name_; }
588  StringField& family() { return family_; }
590  const StringField& family() const { return family_; }
592  FlagField& saveCurrentSettings() { return save_current_settings_; }
594  const FlagField& saveCurrentSettings() const { return save_current_settings_; }
595 
596  private:
597  HebiCommandPtr const internal_;
598 
599  Actuator actuator_;
600 
601  StringField name_;
602  StringField family_;
603  FlagField save_current_settings_;
604 
606  };
607 
609  class Actuator final
610  {
611  public:
612  #ifndef DOXYGEN_OMIT_INTERNAL
613  Actuator(HebiCommandPtr internal)
614  : internal_(internal),
615  velocity_(internal, HebiCommandFloatVelocity),
616  effort_(internal, HebiCommandFloatEffort),
617  position_(internal, HebiCommandHighResAnglePosition)
618  {
619  }
620  #endif // DOXYGEN_OMIT_INTERNAL
621 
622  // With all submessage and field getters: Note that the returned reference
623  // should not be used after the lifetime of this parent.
624 
625  // Subfields ----------------
626 
628  FloatField& velocity() { return velocity_; }
630  const FloatField& velocity() const { return velocity_; }
632  FloatField& effort() { return effort_; }
634  const FloatField& effort() const { return effort_; }
636  HighResAngleField& position() { return position_; }
638  const HighResAngleField& position() const { return position_; }
639 
640  private:
641  HebiCommandPtr const internal_;
642 
643  FloatField velocity_;
644  FloatField effort_;
645  HighResAngleField position_;
646 
648  };
649 
650  private:
655  HebiCommandPtr internal_;
656 
657  public:
658  #ifndef DOXYGEN_OMIT_INTERNAL
659 
663  Command(HebiCommandPtr );
664  #endif // DOXYGEN_OMIT_INTERNAL
665 
668  Command(Command&& other);
672  ~Command() noexcept; /* annotating specified destructor as noexcept is best-practice */
673 
674  // With all submessage and field getters: Note that the returned reference
675  // should not be used after the lifetime of this parent.
676 
677  // Submessages -------------------------------------------------------------
678 
680  Io& io() { return io_; }
682  const Io& io() const { return io_; }
684  Settings& settings() { return settings_; }
686  const Settings& settings() const { return settings_; }
688  Actuator& actuator() { return actuator_; }
690  const Actuator& actuator() const { return actuator_; }
691 
692  // Subfields -------------------------------------------------------------
693 
694  #ifndef DOXYGEN_OMIT_INTERNAL
695  NumberedFloatField& debug();
698  const NumberedFloatField& debug() const;
699  #endif // DOXYGEN_OMIT_INTERNAL
700  LedField& led();
703  const LedField& led() const;
704 
705  private:
706  Io io_;
707  Settings settings_;
708  Actuator actuator_;
709 
710  NumberedFloatField debug_;
711  LedField led_;
712 
717 
718  /* Disable move assigment operator. */
719  Command& operator= (const Command&& other) = delete;
720 };
721 
722 } // namespace hebi
IoBank & f()
I/O pin bank f (pins 1-8 available)
Definition: command.hpp:465
const CommandGains & velocityGains() const
Controller gains for the velocity PID loop.
Definition: command.hpp:519
FlagField & saveCurrentSettings()
Indicates if the module should save the current values of all of its settings.
Definition: command.hpp:592
IoBank & d()
I/O pin bank d (pins 1-8 available)
Definition: command.hpp:457
const FloatField & velocity() const
Velocity of the module output (post-spring), in radians/second.
Definition: command.hpp:630
const FloatField & effort() const
Effort at the module output; units vary (e.g., N * m for rotational joints and N for linear stages)...
Definition: command.hpp:634
#define HEBI_DISABLE_COPY(Class)
Definition: util.hpp:17
const FloatField & springConstant() const
The spring constant of the module.
Definition: command.hpp:530
A message field representable by a bool value.
Definition: command.hpp:191
Actuator & actuator()
Actuator-specific commands.
Definition: command.hpp:688
Actuator-specific commands.
Definition: command.hpp:609
Io & io()
Any available digital or analog output pins on the device.
Definition: command.hpp:680
Settings & settings()
Module settings that are typically changed at a slower rate.
Definition: command.hpp:684
const StringField & name() const
Sets the name for this module. Name must be null-terminated character string for the name; must be <=...
Definition: command.hpp:586
FloatField & springConstant()
The spring constant of the module.
Definition: command.hpp:528
A message field for interfacing with a bank of I/O pins.
Definition: command.hpp:325
const Actuator & actuator() const
Actuator-specific commands.
Definition: command.hpp:690
Actuator & actuator()
Actuator-specific settings, such as controller gains.
Definition: command.hpp:577
StringField & family()
Sets the family for this module. Name must be null-terminated character string for the family; must b...
Definition: command.hpp:588
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:327
IoBank & a()
I/O pin bank a (pins 1-8 available)
Definition: command.hpp:445
A message field representable by a std::string.
Definition: command.hpp:215
Definition: color.hpp:5
A combination of the position, velocity, and effort loops with P feeding to T and V feeding to PWM; d...
A message field for interfacing with an LED.
Definition: command.hpp:381
The motor is not given power (equivalent to a 0 PWM value)
bool has() const
True if (and only if) the field has a value.
Definition: command.hpp:308
CommandGains & effortGains()
Controller gains for the effort PID loop.
Definition: command.hpp:521
#define HEBI_DISABLE_COPY_MOVE(Class)
Definition: util.hpp:7
IoBank & c()
I/O pin bank c (pins 1-8 available)
Definition: command.hpp:453
HighResAngleField & position()
Position of the module output (post-spring), in radians.
Definition: command.hpp:636
CommandGains & velocityGains()
Controller gains for the velocity PID loop.
Definition: command.hpp:517
Actuator-specific settings, such as controller gains.
Definition: command.hpp:490
const Io & io() const
Any available digital or analog output pins on the device.
Definition: command.hpp:682
Module settings that are typically changed at a slower rate.
Definition: command.hpp:485
FloatField & referencePosition()
The internal encoder reference offset (setting this matches the current position to the given referen...
Definition: command.hpp:532
A combination of the position, velocity, and effort loops with P and V feeding to T; documented on do...
A two-state message field (either set/true or cleared/false).
Definition: command.hpp:252
const FloatField & referenceEffort() const
The internal effort reference offset (setting this matches the current effort to the given reference ...
Definition: command.hpp:538
const IoBank & e() const
I/O pin bank e (pins 1-8 available)
Definition: command.hpp:463
IoBank & b()
I/O pin bank b (pins 1-8 available)
Definition: command.hpp:449
A message field containing a numbered set of single-precision floating point values.
Definition: command.hpp:152
const Settings & settings() const
Module settings that are typically changed at a slower rate.
Definition: command.hpp:686
const IoBank & c() const
I/O pin bank c (pins 1-8 available)
Definition: command.hpp:455
const IoBank & b() const
I/O pin bank b (pins 1-8 available)
Definition: command.hpp:451
EnumField< ControlStrategy > & controlStrategy()
How the position, velocity, and effort PID loops are connected in order to control motor PWM...
Definition: command.hpp:540
FloatField & velocity()
Velocity of the module output (post-spring), in radians/second.
Definition: command.hpp:628
Any available digital or analog output pins on the device.
Definition: command.hpp:423
const IoBank & d() const
I/O pin bank d (pins 1-8 available)
Definition: command.hpp:459
FloatField & referenceEffort()
The internal effort reference offset (setting this matches the current effort to the given reference ...
Definition: command.hpp:536
ControlStrategy
Definition: command.hpp:33
const FloatField & referencePosition() const
The internal encoder reference offset (setting this matches the current position to the given referen...
Definition: command.hpp:534
void clear()
Removes any currently set value for this field.
Definition: command.hpp:315
A combination of the position, velocity, and effort loops with P, V, and T feeding to PWM; documented...
IoBank & e()
I/O pin bank e (pins 1-8 available)
Definition: command.hpp:461
Command(Command &&other)
Move constructor (necessary for containment in STL template classes)
Definition: command.cpp:318
A message field for an angle measurement which does not lose precision at very high angles...
Definition: command.hpp:89
const CommandGains & positionGains() const
Controller gains for the position PID loop.
Definition: command.hpp:515
StringField & name()
Sets the name for this module. Name must be null-terminated character string for the name; must be <=...
Definition: command.hpp:584
const IoBank & f() const
I/O pin bank f (pins 1-8 available)
Definition: command.hpp:467
const FlagField & saveCurrentSettings() const
Indicates if the module should save the current values of all of its settings.
Definition: command.hpp:594
const IoBank & a() const
I/O pin bank a (pins 1-8 available)
Definition: command.hpp:447
const EnumField< ControlStrategy > & controlStrategy() const
How the position, velocity, and effort PID loops are connected in order to control motor PWM...
Definition: command.hpp:542
const Actuator & actuator() const
Actuator-specific settings, such as controller gains.
Definition: command.hpp:579
A direct PWM value (-1 to 1) can be sent to the motor (subject to onboard safety limiting).
const CommandGains & effortGains() const
Controller gains for the effort PID loop.
Definition: command.hpp:523
Structure to describe an RGB color.
Definition: color.hpp:8
CommandGains & positionGains()
Controller gains for the position PID loop.
Definition: command.hpp:513
const StringField & family() const
Sets the family for this module. Name must be null-terminated character string for the family; must b...
Definition: command.hpp:590
A message field representable by a single-precision floating point value.
Definition: command.hpp:49
const HighResAngleField & position() const
Position of the module output (post-spring), in radians.
Definition: command.hpp:638
A message field representable by an enum of a given type.
Definition: command.hpp:287
FloatField & effort()
Effort at the module output; units vary (e.g., N * m for rotational joints and N for linear stages)...
Definition: command.hpp:632
LedField & led()
The module&#39;s LED.
Definition: command.cpp:347