HEBI C++ API  1.0.0-rc1
group.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "hebi_group.h"
4 #include "group_command.hpp"
5 #include "group_feedback.hpp"
6 #include "group_info.hpp"
7 #include "util.hpp"
8 
9 #include <functional>
10 #include <memory>
11 #include <mutex>
12 
13 namespace hebi {
14 
40 typedef std::function<void (const GroupFeedback&)> GroupFeedbackHandler;
41 
42 class LogFile;
43 
48 class Group final
49 {
50  private:
54  HebiGroupPtr internal_;
55 
59  const int number_of_modules_;
60 
64  std::mutex handler_lock_;
65 
70  std::vector<GroupFeedbackHandler> handlers_;
71 
72  #ifndef DOXYGEN_OMIT_INTERNAL
73 
77  friend void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void* user_data);
78  #endif // DOXYGEN_OMIT_INTERNAL
79 
84  void callAttachedHandlers(HebiGroupFeedbackPtr group_feedback);
85 
86  public:
91  static const int DEFAULT_TIMEOUT_MS = 500;
92 
93  #ifndef DOXYGEN_OMIT_INTERNAL
94 
99  Group(HebiGroupPtr group);
100  #endif // DOXYGEN_OMIT_INTERNAL
101 
105  virtual ~Group() noexcept; /* annotating specified destructor as noexcept is best-practice */
106 
110  int size();
111 
122  bool setCommandLifetimeMs(int ms);
123 
135  bool sendCommand(const GroupCommand& group_command);
136 
156  bool sendCommandWithAcknowledgement(const GroupCommand& group_command, int timeout_ms=DEFAULT_TIMEOUT_MS);
157 
169  bool sendFeedbackRequest();
170 
191  bool getNextFeedback(GroupFeedback* feedback, int timeout_ms=DEFAULT_TIMEOUT_MS);
192 
201  bool requestInfo(GroupInfo* info, int timeout_ms=DEFAULT_TIMEOUT_MS);
202 
203  #ifndef DOXYGEN_OMIT_INTERNAL
204 
212  bool startLog(std::string dir);
213 
224  bool startLog(std::string dir, std::string file);
225 
232  std::shared_ptr<LogFile> stopLog();
233  #endif // DOXYGEN_OMIT_INTERNAL
234 
242  bool setFeedbackFrequencyHz(float frequency);
248  float getFeedbackFrequencyHz();
253  void addFeedbackHandler(GroupFeedbackHandler handler);
257  void clearFeedbackHandlers();
258 
266  static std::shared_ptr<Group> createImitation(unsigned int size);
267 
268  private:
273 };
274 
275 } // namespace hebi
void addFeedbackHandler(GroupFeedbackHandler handler)
Adds a handler function to be called by the internal feedback request thread.
Definition: group.cpp:117
A list of Feedback objects that can be received from a Group of modules; the size() must match the nu...
Definition: group_feedback.hpp:14
void clearFeedbackHandlers()
Removes all feedback handlers presently added.
Definition: group.cpp:125
bool getNextFeedback(GroupFeedback *feedback, int 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:75
Represents a group of physical HEBI modules, and allows Command, Feedback, and Info objects to be sen...
Definition: group.hpp:48
A list of Info objects that can be received from a Group of modules; the size() must match the number...
Definition: group_info.hpp:14
float getFeedbackFrequencyHz()
Gets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:112
virtual ~Group() noexcept
Destructor cleans up group.
Definition: group.cpp:43
Definition: color.hpp:5
int size()
Returns the number of modules in the group.
Definition: group.cpp:50
bool sendCommand(const GroupCommand &group_command)
Send a command to the given group without requesting an acknowledgement.
Definition: group.cpp:60
bool sendCommandWithAcknowledgement(const GroupCommand &group_command, int 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
#define HEBI_DISABLE_COPY_MOVE(Class)
Definition: util.hpp:7
Definition: log_file.hpp:10
static const int DEFAULT_TIMEOUT_MS
The default timeout for any send-with-acknowledgement or request operation is 500 ms...
Definition: group.hpp:91
A list of Command objects appropriate for sending to a Group of modules; the size() must match the nu...
Definition: group_command.hpp:14
std::function< void(const GroupFeedback &)> GroupFeedbackHandler
Definition of a callback function for GroupFeedback returned from a Group of modules.
Definition: group.hpp:40
static std::shared_ptr< Group > createImitation(unsigned int size)
Creates an imitation group of provided size.
Definition: group.cpp:38
bool setCommandLifetimeMs(int ms)
Sets the command lifetime for the modules in this group.
Definition: group.cpp:55
bool requestInfo(GroupInfo *info, int timeout_ms=DEFAULT_TIMEOUT_MS)
Request info from the group, and store it in the passed-in info object.
Definition: group.cpp:80
bool setFeedbackFrequencyHz(float frequency)
Sets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:107
bool sendFeedbackRequest()
Requests feedback from the group.
Definition: group.cpp:70