group.cpp
Go to the documentation of this file.
1 #include "group.hpp"
2 
3 #include "group_command.hpp"
4 #include "group_feedback.hpp"
5 #include "group_info.hpp"
6 #include "log_file.hpp"
7 
8 namespace hebi {
9 
10 #ifndef DOXYGEN_OMIT_INTERNAL
11 void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void* user_data) {
12  reinterpret_cast<Group*>(user_data)->callAttachedHandlers(group_feedback);
13 }
14 #endif // DOXYGEN_OMIT_INTERNAL
15 
17  // Wrap this:
18  GroupFeedback wrapped_fbk(group_feedback);
19  // Call handlers:
20  std::lock_guard<std::mutex> lock_guard(handler_lock_);
21  for (unsigned int i = 0; i < handlers_.size(); i++) {
22  GroupFeedbackHandler handler = handlers_[i];
23  try {
24  handler(wrapped_fbk);
25  } catch (...) {
26  }
27  }
28 }
29 
30 Group::Group(HebiGroupPtr group, float initial_feedback_frequency, int32_t initial_command_lifetime)
32  if (initial_feedback_frequency != 0)
33  setFeedbackFrequencyHz(initial_feedback_frequency);
34  if (initial_command_lifetime != 0)
35  setCommandLifetimeMs(initial_command_lifetime);
36 }
37 
38 std::shared_ptr<Group> Group::createImitation(size_t size) {
39  return std::make_shared<Group>(hebiGroupCreateImitation(size));
40 }
41 
42 Group::~Group() noexcept {
43  // Cleanup group object allocated by the C library
44  if (internal_ != nullptr)
46 }
47 
49 
50 bool Group::setCommandLifetimeMs(int32_t ms) {
52 }
53 
54 bool Group::sendCommand(const GroupCommand& group_command) {
55  return (hebiGroupSendCommand(internal_, group_command.internal_) == HebiStatusSuccess);
56 }
57 
58 bool Group::sendCommandWithAcknowledgement(const GroupCommand& group_command, int32_t timeout_ms) {
59  return (hebiGroupSendCommandWithAcknowledgement(internal_, group_command.internal_, timeout_ms) == HebiStatusSuccess);
60 }
61 
63 
64 bool Group::getNextFeedback(GroupFeedback& feedback, int32_t timeout_ms) {
65  return (hebiGroupGetNextFeedback(internal_, feedback.internal_, timeout_ms) == HebiStatusSuccess);
66 }
67 
68 bool Group::requestInfo(GroupInfo& info, int32_t timeout_ms) {
69  return (hebiGroupRequestInfo(internal_, info.internal_, timeout_ms) == HebiStatusSuccess);
70 }
71 
72 std::string Group::startLog(const std::string& dir) {
73  HebiStringPtr str;
74  if (hebiGroupStartLog(internal_, dir.c_str(), nullptr, &str) == HebiStatusSuccess) {
75  assert(str);
76  size_t len;
77 
78  hebiStringGetString(str, nullptr, &len);
79  char* buffer = new char[len];
80  hebiStringGetString(str, buffer, &len);
81  std::string ret(buffer, --len);
82 
83  delete[] buffer;
84  hebiStringRelease(str);
85 
86  return ret;
87  }
88  return "";
89 }
90 
91 std::string Group::startLog(const std::string& dir, const std::string& file) {
92  HebiStringPtr str;
93  if (hebiGroupStartLog(internal_, dir.c_str(), file.c_str(), &str) == HebiStatusSuccess) {
94  assert(str);
95  size_t len;
96 
97  hebiStringGetString(str, nullptr, &len);
98  char* buffer = new char[len];
99  hebiStringGetString(str, buffer, &len);
100  std::string ret(buffer, --len);
101 
102  delete[] buffer;
103  hebiStringRelease(str);
104 
105  return ret;
106  }
107  return "";
108 }
109 
110 std::shared_ptr<LogFile> Group::stopLog() {
111  auto internal = hebiGroupStopLog(internal_);
112  if (internal == nullptr) {
113  return std::shared_ptr<LogFile>();
114  }
115 
116  return std::shared_ptr<LogFile>(new LogFile(internal, hebiLogFileGetNumberOfModules(internal)));
117 }
118 
119 bool Group::setFeedbackFrequencyHz(float frequency) {
121 }
122 
124 
126  std::lock_guard<std::mutex> lock_guard(handler_lock_);
127  handlers_.push_back(handler);
128  if (handlers_.size() == 1) // (i.e., this was the first one)
129  hebiGroupRegisterFeedbackHandler(internal_, callbackWrapper, reinterpret_cast<void*>(this));
130 }
131 
133  std::lock_guard<std::mutex> lock_guard(handler_lock_);
135  handlers_.clear();
136 }
137 
138 } // namespace hebi
void addFeedbackHandler(GroupFeedbackHandler handler)
Adds a handler function to be called by the internal feedback request thread.
Definition: group.cpp:125
void hebiStringRelease(HebiStringPtr str)
Releases a string instance.
std::string startLog(const std::string &dir)
Starts log (stopping any active log).
Definition: group.cpp:72
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:132
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:29
A list of Info objects that can be received from a Group of modules; the size() must match the number...
Definition: group_info.hpp:16
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:30
float getFeedbackFrequencyHz()
Gets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:123
~Group() noexcept
Destructor cleans up group.
Definition: group.cpp:42
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:58
void callAttachedHandlers(HebiGroupFeedbackPtr group_feedback)
Definition: group.cpp:16
Definition: arm.cpp: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:48
bool sendCommand(const GroupCommand &group_command)
Send a command to the given group without requesting an acknowledgement.
Definition: group.cpp:54
bool setCommandLifetimeMs(int32_t ms)
Sets the command lifetime for the modules in this group.
Definition: group.cpp:50
struct HebiString_ * HebiStringPtr
The C-style&#39;s API representation of a string.
Definition: hebi.h:521
std::shared_ptr< LogFile > stopLog()
Stops any active log.
Definition: group.cpp:110
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:38
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:64
HebiGroupPtr internal_
Definition: group.hpp:34
std::function< void(const GroupFeedback &)> GroupFeedbackHandler
Definition of a callback function for GroupFeedback returned from a Group of modules.
Definition: group.hpp:23
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:44
HebiGroupCommandPtr internal_
HebiStatusCode hebiStringGetString(HebiStringPtr str, char *buffer, size_t *length)
String Functions.
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:68
HebiGroupFeedbackPtr internal_
std::vector< GroupFeedbackHandler > handlers_
Definition: group.hpp:50
struct HebiGroupFeedback_ * HebiGroupFeedbackPtr
The C-style&#39;s API representation of a feedback object for a group of modules.
Definition: hebi.h:476
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:23
size_t hebiGroupGetSize(HebiGroupPtr group)
Returns the number of modules in a group.
HebiGroupPtr hebiGroupCreateImitation(size_t size)
Group API.
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 HebiGroup_ * HebiGroupPtr
The C-style&#39;s API representation of a group.
Definition: hebi.h:493
const int number_of_modules_
Definition: group.hpp:39
void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void *user_data)
Definition: group.cpp:11
bool setFeedbackFrequencyHz(float frequency)
Sets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:119
friend void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void *user_data)
Definition: group.cpp:11
bool sendFeedbackRequest()
Requests feedback from the group.
Definition: group.cpp:62


hebi_cpp_api_ros
Author(s): Chris Bollinger , Matthew Tesch
autogenerated on Thu May 28 2020 03:14:44