group.cpp
Go to the documentation of this file.
1 #include "group.hpp"
2 #include "group_command.hpp"
3 #include "group_feedback.hpp"
4 #include "group_info.hpp"
5 #include "log_file.hpp"
6 
7 namespace hebi {
8 
9 #ifndef DOXYGEN_OMIT_INTERNAL
10 void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void* user_data)
11 {
12  reinterpret_cast<Group*>(user_data)->callAttachedHandlers(group_feedback);
13 }
14 #endif // DOXYGEN_OMIT_INTERNAL
15 
17 {
18  // Wrap this:
19  GroupFeedback wrapped_fbk(group_feedback);
20  // Call handlers:
21  std::lock_guard<std::mutex> lock_guard(handler_lock_);
22  for (unsigned int i = 0; i < handlers_.size(); i++)
23  {
24  GroupFeedbackHandler handler = handlers_[i];
25  try
26  {
27  handler(wrapped_fbk);
28  }
29  catch (...)
30  {
31 
32  }
33  }
34 }
35 
37  float initial_feedback_frequency,
38  int32_t initial_command_lifetime)
40 {
41  if (initial_feedback_frequency != 0)
42  setFeedbackFrequencyHz(initial_feedback_frequency);
43  if (initial_command_lifetime != 0)
44  setCommandLifetimeMs(initial_command_lifetime);
45 }
46 
47 std::shared_ptr<Group> Group::createImitation(size_t size)
48 {
49  return std::make_shared<Group>(hebiGroupCreateImitation(size));
50 }
51 
52 Group::~Group() noexcept
53 {
54  // Cleanup group object allocated by the C library
55  if (internal_ != nullptr)
57 }
58 
60 {
61  return number_of_modules_;
62 }
63 
65 {
67 }
68 
69 bool Group::sendCommand(const GroupCommand& group_command)
70 {
71  return (hebiGroupSendCommand(internal_, group_command.internal_) == HebiStatusSuccess);
72 }
73 
74 bool Group::sendCommandWithAcknowledgement(const GroupCommand& group_command, int32_t timeout_ms)
75 {
76  return (hebiGroupSendCommandWithAcknowledgement(internal_, group_command.internal_, timeout_ms) == HebiStatusSuccess);
77 }
78 
80 {
82 }
83 
84 bool Group::getNextFeedback(GroupFeedback& feedback, int32_t timeout_ms)
85 {
86  return (hebiGroupGetNextFeedback(internal_, feedback.internal_, timeout_ms) == HebiStatusSuccess);
87 }
88 
89 bool Group::requestInfo(GroupInfo& info, int32_t timeout_ms)
90 {
91  return (hebiGroupRequestInfo(internal_, info.internal_, timeout_ms) == HebiStatusSuccess);
92 }
93 
94 std::string Group::startLog(const std::string& dir)
95 {
96  HebiStringPtr str;
97  if (hebiGroupStartLog(internal_, dir.c_str(), nullptr, &str) == HebiStatusSuccess)
98  {
99  assert(str);
100  size_t len;
101 
102  hebiStringGetString(str, nullptr, &len);
103  char* buffer = new char[len];
104  hebiStringGetString(str, buffer, &len);
105  std::string ret(buffer, --len);
106 
107  delete[] buffer;
108  hebiStringRelease(str);
109 
110  return ret;
111  }
112  return "";
113 }
114 
115 std::string Group::startLog(const std::string& dir, const std::string& file)
116 {
117  HebiStringPtr str;
118  if (hebiGroupStartLog(internal_, dir.c_str(), file.c_str(), &str) == HebiStatusSuccess)
119  {
120  assert(str);
121  size_t len;
122 
123  hebiStringGetString(str, nullptr, &len);
124  char* buffer = new char[len];
125  hebiStringGetString(str, buffer, &len);
126  std::string ret(buffer, --len);
127 
128  delete[] buffer;
129  hebiStringRelease(str);
130 
131  return ret;
132  }
133  return "";
134 }
135 
136 std::shared_ptr<LogFile> Group::stopLog()
137 {
138  auto internal = hebiGroupStopLog(internal_);
139  if (internal == nullptr) {
140  return std::shared_ptr<LogFile>();
141  }
142 
143  return std::shared_ptr<LogFile>(
144  new LogFile(internal, hebiLogFileGetNumberOfModules(internal))
145  );
146 }
147 
148 bool Group::setFeedbackFrequencyHz(float frequency)
149 {
151 }
152 
154 {
156 }
157 
159 {
160  std::lock_guard<std::mutex> lock_guard(handler_lock_);
161  handlers_.push_back(handler);
162  if (handlers_.size() == 1) // (i.e., this was the first one)
163  hebiGroupRegisterFeedbackHandler(internal_, callbackWrapper, reinterpret_cast<void*>(this));
164 }
165 
167 {
168  std::lock_guard<std::mutex> lock_guard(handler_lock_);
170  handlers_.clear();
171 }
172 
173 } // namespace hebi
void addFeedbackHandler(GroupFeedbackHandler handler)
Adds a handler function to be called by the internal feedback request thread.
Definition: group.cpp:158
void hebiStringRelease(HebiStringPtr str)
Releases a string instance.
std::string startLog(const std::string &dir)
Starts log (stopping any active log).
Definition: group.cpp:94
A list of Feedback objects that can be received from a Group of modules; the size() must match the nu...
void clearFeedbackHandlers()
Removes all feedback handlers presently added.
Definition: group.cpp:166
HebiLogFilePtr hebiGroupStopLog(HebiGroupPtr group)
Stops logging data to a file.
Represents a group of physical HEBI modules, and allows Command, Feedback, and Info objects to be sen...
Definition: group.hpp:49
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
HebiStatusCode hebiGroupSendCommand(HebiGroupPtr group, HebiGroupCommandPtr command)
Sends a command to the given group without requesting an acknowledgement.
Group(HebiGroupPtr group, float initial_feedback_frequency=0.0f, int32_t initial_command_lifetime=0)
Definition: group.cpp:36
float getFeedbackFrequencyHz()
Gets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:153
~Group() noexcept
Destructor cleans up group.
Definition: group.cpp:52
HebiStatusCode hebiGroupStartLog(HebiGroupPtr group, const char *dir, const char *file, HebiStringPtr *ret)
Starts logging data to a file.
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:74
struct _HebiGroup * HebiGroupPtr
The C-style&#39;s API representation of a group.
Definition: hebi.h:330
struct _HebiGroupFeedback * HebiGroupFeedbackPtr
The C-style&#39;s API representation of group feedback.
Definition: hebi.h:346
void callAttachedHandlers(HebiGroupFeedbackPtr group_feedback)
Definition: group.cpp:16
Definition: color.hpp:5
void hebiGroupRelease(HebiGroupPtr group)
Release resources for a given group; group should not be used after this call.
int size()
Returns the number of modules in the group.
Definition: group.cpp:59
bool sendCommand(const GroupCommand &group_command)
Send a command to the given group without requesting an acknowledgement.
Definition: group.cpp:69
bool setCommandLifetimeMs(int32_t ms)
Sets the command lifetime for the modules in this group.
Definition: group.cpp:64
std::shared_ptr< LogFile > stopLog()
Stops any active log.
Definition: group.cpp:136
HebiStatusCode hebiGroupSetFeedbackFrequencyHz(HebiGroupPtr group, float frequency)
Sets the feedback request loop frequency (in Hz).
float hebiGroupGetFeedbackFrequencyHz(HebiGroupPtr group)
Returns the current feedback request loop frequency (in Hz).
HebiStatusCode hebiGroupRequestInfo(HebiGroupPtr group, HebiGroupInfoPtr info, int32_t timeout_ms)
Requests info from the group, and writes it to the provided info object.
HebiStatusCode hebiGroupSetCommandLifetime(HebiGroupPtr group, int32_t lifetime_ms)
Sets the command lifetime for the group, in milliseconds.
static std::shared_ptr< Group > createImitation(size_t size)
Creates an imitation group of provided size.
Definition: group.cpp:47
HebiStatusCode hebiGroupSendFeedbackRequest(HebiGroupPtr group)
Requests feedback from the group.
HebiStatusCode hebiGroupSendCommandWithAcknowledgement(HebiGroupPtr group, HebiGroupCommandPtr command, int32_t timeout_ms)
Sends a command to the given group, requesting an acknowledgement of transmission to be sent back...
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:84
HebiGroupPtr internal_
Definition: group.hpp:55
A list of Command objects appropriate for sending to a Group of modules; the size() must match the nu...
std::mutex handler_lock_
Definition: group.hpp:65
HebiGroupCommandPtr internal_
HebiStatusCode hebiStringGetString(HebiStringPtr str, char *buffer, size_t *length)
Copy the string into a buffer.
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:89
HebiGroupFeedbackPtr internal_
std::vector< GroupFeedbackHandler > handlers_
Definition: group.hpp:71
HebiStatusCode hebiGroupGetNextFeedback(HebiGroupPtr group, HebiGroupFeedbackPtr feedback, int32_t timeout_ms)
Returns the most recently stored feedback from a sent feedback request, or returns the next one recei...
size_t hebiLogFileGetNumberOfModules(HebiLogFilePtr log_file)
Retrieve the number of modules in the group represented by an opened log file.
HebiGroupInfoPtr internal_
Definition: group_info.hpp:22
size_t hebiGroupGetSize(HebiGroupPtr group)
Returns the number of modules in a group.
HebiGroupPtr hebiGroupCreateImitation(size_t size)
Creates an "imitation" group with the specified number of modules.
HebiStatusCode hebiGroupRegisterFeedbackHandler(HebiGroupPtr group, GroupFeedbackHandlerFunction handler, void *user_data)
Add a function that is called whenever feedback is returned from the group.
void hebiGroupClearFeedbackHandlers(HebiGroupPtr group)
Removes all feedback handling functions from the queue to be called on receipt of group feedback...
struct _HebiString * HebiStringPtr
The C-style&#39;s API representation of a string.
Definition: hebi.h:308
const int number_of_modules_
Definition: group.hpp:60
void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void *user_data)
Definition: group.cpp:10
bool setFeedbackFrequencyHz(float frequency)
Sets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:148
friend void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void *user_data)
Definition: group.cpp:10
bool sendFeedbackRequest()
Requests feedback from the group.
Definition: group.cpp:79
std::function< void(const GroupFeedback &)> GroupFeedbackHandler
Definition of a callback function for GroupFeedback returned from a Group of modules.
Definition: group.hpp:43


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:14