HEBI C++ API  3.15.0
group.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "hebi.h"
4 
5 #include <array>
6 #include <functional>
7 #include <memory>
8 #include <mutex>
9 #include <vector>
10 
11 #include "util.hpp"
12 
13 namespace hebi {
14 
15 class LogFile;
16 class Group;
17 class GroupCommand;
18 class GroupFeedback;
19 class GroupInfo;
20 
21 namespace util {
22 
23 class MobileIO;
24 
25 } // namespace util
26 
31 using GroupFeedbackHandler = std::function<void(const GroupFeedback&)>;
32 
33 enum InfoExtraFields : uint64_t {
34  EthernetInfo = HebiInfoExtraFieldsEthernetInfo, // IP address, subnet, etc.
35  UserData = HebiInfoExtraFieldsUserData, // user data bytes and floats
36  FirmwareInfo = HebiInfoExtraFieldsFirmwareInfo, // mechanical/electrical revs, etc
37  RuntimeData = HebiInfoExtraFieldsRuntimeData, // time on, time commanded
38 };
39 
40 class UserState {
41  friend class Group;
42 
43 private:
44  std::array<bool, 9> has_state_bits_;
45  std::array<double, 9> state_values_;
46 public:
47  // Creates a user state with all values empty
48  UserState() = default;
49  // Creates a user state with all values specified
50  UserState(double v1, double v2, double v3, double v4, double v5, double v6, double v7,
51  double v8, double v9) :
52  state_values_({v1, v2, v3, v4, v5, v6, v7, v8, v9}) {
53  // Has state for all fields
54  has_state_bits_.fill(true);
55  }
56 
63  bool hasValue(size_t number) const {
64  return (number >= 1 && number <= 9) ? (has_state_bits_[number - 1]) : false;
65  }
66 
73  double getValue(size_t number) const {
74  return (number >= 1 && number <= 9) ? (state_values_[number - 1]) : 0;
75  }
76 
83  void setValue(size_t number, double value) {
84  if (number < 1 || number > 9)
85  return;
86  has_state_bits_[number - 1] = true;
87  state_values_[number - 1] = value;
88  }
89 
95  void clearValue(size_t number) {
96  if (number < 1 || number > 9)
97  return;
98  has_state_bits_[number - 1] = false;
99  }
100 };
101 
106 class Group final {
107  friend class util::MobileIO;
108 
109 private:
113  HebiGroupPtr internal_;
114 
118  const size_t number_of_modules_;
119 
123  std::mutex handler_lock_;
124 
129  std::vector<GroupFeedbackHandler> handlers_;
130 
131 #ifndef DOXYGEN_OMIT_INTERNAL
132 
136  friend void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void* user_data);
137 #endif // DOXYGEN_OMIT_INTERNAL
138 
143  void callAttachedHandlers(HebiGroupFeedbackPtr group_feedback);
144 
145 public:
150  static const int32_t DEFAULT_TIMEOUT_MS = 500;
151 
152 #ifndef DOXYGEN_OMIT_INTERNAL
153 
158  Group(HebiGroupPtr group, float initial_feedback_frequency = 0.0f, int32_t initial_command_lifetime = 0);
159 #endif // DOXYGEN_OMIT_INTERNAL
160 
164  ~Group() noexcept; /* annotating specified destructor as noexcept is best-practice */
165 
169  size_t size() const;
170 
181  bool setCommandLifetimeMs(int32_t ms);
182 
188  int32_t getCommandLifetimeMs() const;
189 
201  bool sendCommand(const GroupCommand& group_command);
202 
222  bool sendCommandWithAcknowledgement(const GroupCommand& group_command, int32_t timeout_ms = DEFAULT_TIMEOUT_MS);
223 
235  bool sendFeedbackRequest();
236 
257  bool getNextFeedback(GroupFeedback& feedback, int32_t timeout_ms = DEFAULT_TIMEOUT_MS);
258 
267  bool requestInfo(GroupInfo& info, int32_t timeout_ms = DEFAULT_TIMEOUT_MS);
268 
277  bool requestInfoExtra(GroupInfo& info, InfoExtraFields extra_fields, int32_t timeout_ms = DEFAULT_TIMEOUT_MS);
278 
287  std::string startLog(const std::string& dir) const;
288 
299  std::string startLog(const std::string& dir, const std::string& file) const;
300 
307  std::shared_ptr<LogFile> stopLog() const;
308 
316  bool logUserState(const UserState& state) const;
317 
325  bool setFeedbackFrequencyHz(float frequency);
331  float getFeedbackFrequencyHz() const;
340  void clearFeedbackHandlers();
341 
349  static std::shared_ptr<Group> createImitation(size_t size);
350 
351 private:
356 };
357 
358 } // namespace hebi
void addFeedbackHandler(GroupFeedbackHandler handler)
Adds a handler function to be called by the internal feedback request thread.
Definition: group.cpp:163
std::shared_ptr< LogFile > stopLog() const
Stops any active log.
Definition: group.cpp:131
A list of Feedback objects that can be received from a Group of modules; the size() must match the nu...
Definition: group_feedback.hpp:18
void clearFeedbackHandlers()
Removes all feedback handlers presently added.
Definition: group.cpp:170
Represents a group of physical HEBI modules, and allows Command, Feedback, and Info objects to be sen...
Definition: group.hpp:106
A list of Info objects that can be received from a Group of modules; the size() must match the number...
Definition: group_info.hpp:18
void clearValue(size_t number)
Clears the specific value set in the UserState.
Definition: group.hpp:95
Definition: group.hpp:35
~Group() noexcept
Destructor cleans up group.
Definition: group.cpp:42
bool sendCommandWithAcknowledgement(const GroupCommand &group_command, int32_t timeout_ms=DEFAULT_TIMEOUT_MS)
Send a command to the given group, requesting an acknowledgement of transmission to be sent back.
Definition: group.cpp:65
InfoExtraFields
Definition: group.hpp:33
Definition: group.hpp:40
Definition: arm.cpp:10
static const int32_t DEFAULT_TIMEOUT_MS
The default timeout for any send-with-acknowledgement or request operation is 500 ms.
Definition: group.hpp:150
bool sendCommand(const GroupCommand &group_command)
Send a command to the given group without requesting an acknowledgement.
Definition: group.cpp:58
bool setCommandLifetimeMs(int32_t ms)
Sets the command lifetime for the modules in this group.
Definition: group.cpp:50
bool requestInfoExtra(GroupInfo &info, InfoExtraFields extra_fields, int32_t timeout_ms=DEFAULT_TIMEOUT_MS)
Request info from the group, and store it in the passed-in info object. Include extra fields.
Definition: group.cpp:88
#define HEBI_DISABLE_COPY_MOVE(Class)
Definition: util.hpp:6
Definition: log_file.hpp:11
Definition: mobile_io.hpp:24
static std::shared_ptr< Group > createImitation(size_t size)
Creates an imitation group of provided size.
Definition: group.cpp:38
Definition: group.hpp:34
int32_t getCommandLifetimeMs() const
Returns the command lifetime for the modules in this group.
Definition: group.cpp:54
bool hasValue(size_t number) const
Does the UserState have the specific value set?
Definition: group.hpp:63
bool getNextFeedback(GroupFeedback &feedback, int32_t timeout_ms=DEFAULT_TIMEOUT_MS)
Returns the most recently stored feedback from a sent feedback request, or returns the next one recei...
Definition: group.cpp:74
std::string startLog(const std::string &dir) const
Starts log (stopping any active log).
Definition: group.cpp:95
size_t size() const
Returns the number of modules in the group.
Definition: group.cpp:48
float getFeedbackFrequencyHz() const
Gets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:161
std::function< void(const GroupFeedback &)> GroupFeedbackHandler
Definition of a callback function for GroupFeedback returned from a Group of modules.
Definition: group.hpp:31
A list of Command objects appropriate for sending to a Group of modules; the size() must match the nu...
Definition: group_command.hpp:19
UserState()=default
bool requestInfo(GroupInfo &info, int32_t timeout_ms=DEFAULT_TIMEOUT_MS)
Request info from the group, and store it in the passed-in info object.
Definition: group.cpp:81
UserState(double v1, double v2, double v3, double v4, double v5, double v6, double v7, double v8, double v9)
Definition: group.hpp:50
double getValue(size_t number) const
Get the specific UserState value.
Definition: group.hpp:73
Definition: group.hpp:37
void setValue(size_t number, double value)
Set the specific value in the UserState.
Definition: group.hpp:83
bool logUserState(const UserState &state) const
Adds a message directly to the log file without sending anything to the group. The message can have 9...
Definition: group.cpp:140
bool setFeedbackFrequencyHz(float frequency)
Sets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:157
bool sendFeedbackRequest()
Requests feedback from the group.
Definition: group.cpp:72
Definition: group.hpp:36