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)
31  : internal_(group), number_of_modules_(hebiGroupGetSize(internal_)) {
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
hebiGroupGetNextFeedback
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...
hebi::LogFile
Definition: log_file.hpp:11
hebi::Group::sendCommandWithAcknowledgement
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
HebiStatusSuccess
@ HebiStatusSuccess
Definition: hebi.h:24
hebiGroupGetFeedbackFrequencyHz
float hebiGroupGetFeedbackFrequencyHz(HebiGroupPtr group)
Returns the current feedback request loop frequency (in Hz).
HebiGroupFeedbackPtr
struct HebiGroupFeedback_ * HebiGroupFeedbackPtr
The C-style's API representation of a feedback object for a group of modules.
Definition: hebi.h:476
HebiGroupPtr
struct HebiGroup_ * HebiGroupPtr
The C-style's API representation of a group.
Definition: hebi.h:493
hebi::GroupFeedback
A list of Feedback objects that can be received from a Group of modules; the size() must match the nu...
Definition: group_feedback.hpp:16
hebiGroupClearFeedbackHandlers
void hebiGroupClearFeedbackHandlers(HebiGroupPtr group)
Removes all feedback handling functions from the queue to be called on receipt of group feedback.
hebiGroupStartLog
HebiStatusCode hebiGroupStartLog(HebiGroupPtr group, const char *dir, const char *file, HebiStringPtr *ret)
Starts logging data to a file.
hebi::GroupCommand
A list of Command objects appropriate for sending to a Group of modules; the size() must match the nu...
Definition: group_command.hpp:17
hebiGroupSendCommandWithAcknowledgement
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.
hebi::Group::setFeedbackFrequencyHz
bool setFeedbackFrequencyHz(float frequency)
Sets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:119
hebiGroupSetCommandLifetime
HebiStatusCode hebiGroupSetCommandLifetime(HebiGroupPtr group, int32_t lifetime_ms)
Sets the command lifetime for the group, in milliseconds.
hebi::Group::sendCommand
bool sendCommand(const GroupCommand &group_command)
Send a command to the given group without requesting an acknowledgement.
Definition: group.cpp:54
hebiGroupSendFeedbackRequest
HebiStatusCode hebiGroupSendFeedbackRequest(HebiGroupPtr group)
Requests feedback from the group.
hebiStringRelease
void hebiStringRelease(HebiStringPtr str)
Releases a string instance.
hebiGroupRelease
void hebiGroupRelease(HebiGroupPtr group)
Release resources for a given group; group should not be used after this call.
group_command.hpp
hebiGroupCreateImitation
HebiGroupPtr hebiGroupCreateImitation(size_t size)
Group API.
hebiGroupRegisterFeedbackHandler
HebiStatusCode hebiGroupRegisterFeedbackHandler(HebiGroupPtr group, GroupFeedbackHandlerFunction handler, void *user_data)
Add a function that is called whenever feedback is returned from the group.
hebi::GroupFeedback::internal_
HebiGroupFeedbackPtr internal_
Definition: group_feedback.hpp:23
hebi::Group::createImitation
static std::shared_ptr< Group > createImitation(size_t size)
Creates an imitation group of provided size.
Definition: group.cpp:38
group.hpp
hebiGroupStopLog
HebiLogFilePtr hebiGroupStopLog(HebiGroupPtr group)
Stops logging data to a file.
hebi::Group::internal_
HebiGroupPtr internal_
Definition: group.hpp:34
hebi::Group::clearFeedbackHandlers
void clearFeedbackHandlers()
Removes all feedback handlers presently added.
Definition: group.cpp:132
hebi::Group::getFeedbackFrequencyHz
float getFeedbackFrequencyHz()
Gets the frequency of the internal feedback request + callback thread.
Definition: group.cpp:123
hebiGroupSendCommand
HebiStatusCode hebiGroupSendCommand(HebiGroupPtr group, HebiGroupCommandPtr command)
Sends a command to the given group without requesting an acknowledgement.
log_file.hpp
HebiStringPtr
struct HebiString_ * HebiStringPtr
The C-style's API representation of a string.
Definition: hebi.h:521
hebiGroupGetSize
size_t hebiGroupGetSize(HebiGroupPtr group)
Returns the number of modules in a group.
hebi::GroupCommand::internal_
HebiGroupCommandPtr internal_
Definition: group_command.hpp:24
hebi::GroupFeedbackHandler
std::function< void(const GroupFeedback &)> GroupFeedbackHandler
Definition of a callback function for GroupFeedback returned from a Group of modules.
Definition: group.hpp:23
group_feedback.hpp
hebi::Group::requestInfo
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
hebi::Group::stopLog
std::shared_ptr< LogFile > stopLog()
Stops any active log.
Definition: group.cpp:110
hebi
Definition: arm.cpp:5
hebi::Group::~Group
~Group() noexcept
Destructor cleans up group.
Definition: group.cpp:42
hebi::GroupInfo
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
hebi::Group::handlers_
std::vector< GroupFeedbackHandler > handlers_
Definition: group.hpp:50
hebi::Group::getNextFeedback
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
hebi::Group::callAttachedHandlers
void callAttachedHandlers(HebiGroupFeedbackPtr group_feedback)
Definition: group.cpp:16
hebi::Group::callbackWrapper
friend void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void *user_data)
Definition: group.cpp:11
hebi::Group::addFeedbackHandler
void addFeedbackHandler(GroupFeedbackHandler handler)
Adds a handler function to be called by the internal feedback request thread.
Definition: group.cpp:125
hebiLogFileGetNumberOfModules
size_t hebiLogFileGetNumberOfModules(HebiLogFilePtr log_file)
Retrieve the number of modules in the group represented by an opened log file.
hebiGroupSetFeedbackFrequencyHz
HebiStatusCode hebiGroupSetFeedbackFrequencyHz(HebiGroupPtr group, float frequency)
Sets the feedback request loop frequency (in Hz).
hebi::Group::handler_lock_
std::mutex handler_lock_
Definition: group.hpp:44
hebi::GroupInfo::internal_
HebiGroupInfoPtr internal_
Definition: group_info.hpp:23
hebi::Group::number_of_modules_
const int number_of_modules_
Definition: group.hpp:39
hebiGroupRequestInfo
HebiStatusCode hebiGroupRequestInfo(HebiGroupPtr group, HebiGroupInfoPtr info, int32_t timeout_ms)
Requests info from the group, and writes it to the provided info object.
hebiStringGetString
HebiStatusCode hebiStringGetString(HebiStringPtr str, char *buffer, size_t *length)
String Functions.
hebi::Group::setCommandLifetimeMs
bool setCommandLifetimeMs(int32_t ms)
Sets the command lifetime for the modules in this group.
Definition: group.cpp:50
hebi::Group::Group
Group(HebiGroupPtr group, float initial_feedback_frequency=0.0f, int32_t initial_command_lifetime=0)
Definition: group.cpp:30
hebi::Group::size
int size()
Returns the number of modules in the group.
Definition: group.cpp:48
hebi::callbackWrapper
void callbackWrapper(HebiGroupFeedbackPtr group_feedback, void *user_data)
Definition: group.cpp:11
hebi::Group::startLog
std::string startLog(const std::string &dir)
Starts log (stopping any active log).
Definition: group.cpp:72
group_info.hpp
hebi::Group::sendFeedbackRequest
bool sendFeedbackRequest()
Requests feedback from the group.
Definition: group.cpp:62
hebi::Group
Represents a group of physical HEBI modules, and allows Command, Feedback, and Info objects to be sen...
Definition: group.hpp:29


hebi_cpp_api_ros
Author(s): Chris Bollinger , Matthew Tesch
autogenerated on Fri Aug 2 2024 08:35:18