HEBI C++ API  3.10.0
group.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "hebi.h"
4 
5 #include <functional>
6 #include <memory>
7 #include <mutex>
8 #include <vector>
9 
10 #include "util.hpp"
11 
12 namespace hebi {
13 
14 class LogFile;
15 class GroupCommand;
16 class GroupFeedback;
17 class GroupInfo;
18 
19 namespace util {
20 
21 class MobileIO;
22 
23 } // namespace util
24 
29 using GroupFeedbackHandler = std::function<void(const GroupFeedback&)>;
30 
31 enum InfoExtraFields : uint64_t {
32  EthernetInfo = HebiInfoExtraFieldsEthernetInfo, // IP address, subnet, etc.
33  UserData = HebiInfoExtraFieldsUserData, // user data bytes and floats
34  FirmwareInfo = HebiInfoExtraFieldsFirmwareInfo, // mechanical/electrical revs, etc
35  RuntimeData = HebiInfoExtraFieldsRuntimeData, // time on, time commanded
36 };
37 
42 class Group final {
43  friend class util::MobileIO;
44 
45 private:
49  HebiGroupPtr internal_;
50 
54  const int number_of_modules_;
55 
59  std::mutex handler_lock_;
60 
65  std::vector<GroupFeedbackHandler> handlers_;
66 
67 #ifndef DOXYGEN_OMIT_INTERNAL
68 
72  friend void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void* user_data);
73 #endif // DOXYGEN_OMIT_INTERNAL
74 
79  void callAttachedHandlers(HebiGroupFeedbackPtr group_feedback);
80 
81 public:
86  static const int32_t DEFAULT_TIMEOUT_MS = 500;
87 
88 #ifndef DOXYGEN_OMIT_INTERNAL
89 
94  Group(HebiGroupPtr group, float initial_feedback_frequency = 0.0f, int32_t initial_command_lifetime = 0);
95 #endif // DOXYGEN_OMIT_INTERNAL
96 
100  ~Group() noexcept; /* annotating specified destructor as noexcept is best-practice */
101 
105  int size() const;
106 
117  bool setCommandLifetimeMs(int32_t ms);
118 
124  int32_t getCommandLifetimeMs() const;
125 
137  bool sendCommand(const GroupCommand& group_command);
138 
158  bool sendCommandWithAcknowledgement(const GroupCommand& group_command, int32_t timeout_ms = DEFAULT_TIMEOUT_MS);
159 
171  bool sendFeedbackRequest();
172 
193  bool getNextFeedback(GroupFeedback& feedback, int32_t timeout_ms = DEFAULT_TIMEOUT_MS);
194 
203  bool requestInfo(GroupInfo& info, int32_t timeout_ms = DEFAULT_TIMEOUT_MS);
204 
213  bool requestInfoExtra(GroupInfo& info, InfoExtraFields extra_fields, int32_t timeout_ms = DEFAULT_TIMEOUT_MS);
214 
223  std::string startLog(const std::string& dir) const;
224 
235  std::string startLog(const std::string& dir, const std::string& file) const;
236 
243  std::shared_ptr<LogFile> stopLog() const;
244 
252  bool setFeedbackFrequencyHz(float frequency);
258  float getFeedbackFrequencyHz() const;
267  void clearFeedbackHandlers();
268 
276  static std::shared_ptr<Group> createImitation(size_t size);
277 
278 private:
283 };
284 
285 } // namespace hebi
void addFeedbackHandler(GroupFeedbackHandler handler)
Adds a handler function to be called by the internal feedback request thread.
Definition: group.cpp:148
std::shared_ptr< LogFile > stopLog() const
Stops any active log.
Definition: group.cpp:133
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:155
Represents a group of physical HEBI modules, and allows Command, Feedback, and Info objects to be sen...
Definition: group.hpp:42
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
Definition: group.hpp:33
~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:31
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:86
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:32
int32_t getCommandLifetimeMs() const
Returns the command lifetime for the modules in this group.
Definition: group.cpp:54
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
int size() const
Returns the number of modules in the group.
Definition: group.cpp:48
std::string startLog(const std::string &dir) const
Starts log (stopping any active log).
Definition: group.cpp:95
float getFeedbackFrequencyHz() const
Gets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:146
std::function< void(const GroupFeedback &)> GroupFeedbackHandler
Definition of a callback function for GroupFeedback returned from a Group of modules.
Definition: group.hpp:29
A list of Command objects appropriate for sending to a Group of modules; the size() must match the nu...
Definition: group_command.hpp:19
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
Definition: group.hpp:35
bool setFeedbackFrequencyHz(float frequency)
Sets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:142
bool sendFeedbackRequest()
Requests feedback from the group.
Definition: group.cpp:72
Definition: group.hpp:34