mobile_io.cpp
Go to the documentation of this file.
1 #include "mobile_io.hpp"
2 
3 #include <stdexcept>
4 
7 
8 namespace hebi {
9 namespace experimental {
10 
11 bool MobileIOState::getButton(size_t button) const {
12  if (button < 1 || button > NumButtons)
13  throw std::out_of_range("Invalid button number");
14  return buttons_[button - 1];
15 }
16 
17 float MobileIOState::getAxis(size_t axis) const {
18  if (axis < 1 || axis > NumButtons)
19  throw std::out_of_range("Invalid axis number");
20  return axes_[axis - 1];
21 }
22 
23 
25  for (size_t i = 0; i < NumButtons; ++i) {
26  if (prev.buttons_[i] && curr.buttons_[i])
27  buttons_[i] = ButtonState::On;
28  else if (!prev.buttons_[i] && curr.buttons_[i])
29  buttons_[i] = ButtonState::ToOn;
30  if (!prev.buttons_[i] && !curr.buttons_[i])
31  buttons_[i] = ButtonState::Off;
32  else if (prev.buttons_[i] && !curr.buttons_[i])
33  buttons_[i] = ButtonState::ToOff;
34  }
35 }
36 
38  if (button < 1 || button > NumButtons)
39  throw std::out_of_range("Invalid button number");
40  return buttons_[button - 1];
41 }
42 
43 std::unique_ptr<MobileIO> MobileIO::create(const std::string& family, const std::string& name) {
44  hebi::Lookup lookup;
45  std::shared_ptr<hebi::Group> group = lookup.getGroupFromNames({ family }, { name });
46  if (!group)
47  return nullptr;
48  return std::unique_ptr<MobileIO>(new MobileIO(group));
49 }
50 
52  bool tmp;
53  return getState(tmp);
54 }
55 
56 MobileIOState MobileIO::getState(bool& got_feedback) {
57  // Update if we get another packet from the Mobile IO device
58  // (on "failure to get data", just return last data)
59  got_feedback = false;
60  if (group_->getNextFeedback(fbk_)) {
61  got_feedback = true;
62  // We assume the Mobile IO controller is only ever talking to one
63  // device at a time...
64  auto& f0 = fbk_[0];
65  // Update all the buttons in the current state:
66  for (int i = 1; i <= NumButtons; ++i) {
67  if (f0.io().b().hasInt(i)) {
68  current_state_.buttons_[i - 1] = f0.io().b().getInt(i) == 1;
69  }
70  }
71  // And the axes
72  for (int i = 1; i <= NumButtons; ++i) {
73  if (f0.io().a().hasFloat(i)) {
74  current_state_.axes_[i - 1] = f0.io().a().getFloat(i);
75  } else if (f0.io().a().hasInt(i)) {
76  // IO devices may send exact integers as ints instead of floats
77  // to save space on wire, so we check this, too
78  current_state_.axes_[i - 1] = f0.io().a().getInt(i);
79  }
80  }
81  }
82  return current_state_;
83 }
84 
85 bool MobileIO::setSnap(size_t axis_number, float snap_to) {
86  hebi::GroupCommand cmd(group_->size());
87  cmd[0].io().a().setFloat(axis_number, snap_to);
88  return group_->sendCommand(cmd);
89 }
90 
91 bool MobileIO::setAxisValue(size_t axis_number, float value) {
92  hebi::GroupCommand cmd(group_->size());
93  cmd[0].io().f().setFloat(axis_number, value);
94  return group_->sendCommand(cmd);
95 }
96 
97 bool MobileIO::setButtonMode(size_t button_number, ButtonMode mode) {
98  hebi::GroupCommand cmd(group_->size());
99  cmd[0].io().b().setInt(button_number, mode == ButtonMode::Toggle ? 1 : 0);
100  return group_->sendCommand(cmd);
101 }
102 
103 bool MobileIO::setButtonOutput(size_t button_number, bool on) {
104  hebi::GroupCommand cmd(group_->size());
105  cmd[0].io().e().setInt(button_number, on ? 1 : 0);
106  return group_->sendCommand(cmd);
107 }
108 
109 bool MobileIO::setLedColor(uint8_t r, uint8_t g, uint8_t b) {
110  hebi::GroupCommand cmd(group_->size());
111  cmd[0].led().set({r, g, b});
112  return group_->sendCommand(cmd);
113 }
114 
115 bool MobileIO::sendText(const std::string& message) {
116  hebi::GroupCommand cmd(group_->size());
117  cmd[0].appendLog().set(message);
118  return group_->sendCommand(cmd);
119 }
120 
122  hebi::GroupCommand cmd(group_->size());
123  cmd[0].clearLog().set();
124  return group_->sendCommand(cmd);
125 }
126 
127 MobileIO::MobileIO(std::shared_ptr<hebi::Group> group)
128  : group_(group), fbk_(group_->size())
129 { }
130 
131 } // namespace experimental
132 } // namespace hebi
float getAxis(size_t axis) const
Definition: mobile_io.cpp:17
bool sendText(const std::string &message)
Definition: mobile_io.cpp:115
static std::unique_ptr< MobileIO > create(const std::string &family, const std::string &name)
Definition: mobile_io.cpp:43
std::array< float, NumButtons > axes_
Definition: mobile_io.hpp:33
std::bitset< NumButtons > buttons_
Definition: mobile_io.hpp:32
Definition: arm.cpp:5
MobileIO(std::shared_ptr< hebi::Group >)
Definition: mobile_io.cpp:127
bool setLedColor(uint8_t r, uint8_t g, uint8_t b)
Definition: mobile_io.cpp:109
bool getButton(size_t button) const
Definition: mobile_io.cpp:11
static constexpr size_t NumButtons
Definition: mobile_io.hpp:20
ButtonState get(int button) const
Definition: mobile_io.cpp:37
bool setSnap(size_t axis_number, float snap_to)
Definition: mobile_io.cpp:85
bool setButtonMode(size_t button_number, ButtonMode mode)
Definition: mobile_io.cpp:97
MobileIOState getState()
Definition: mobile_io.cpp:51
bool setButtonOutput(size_t button_number, bool on)
Definition: mobile_io.cpp:103
A list of Command objects appropriate for sending to a Group of modules; the size() must match the nu...
bool setAxisValue(size_t axis_number, float value)
Definition: mobile_io.cpp:91
MobileIODiff(const MobileIOState &prev, const MobileIOState &current)
Definition: mobile_io.cpp:24
Maintains a registry of network-connected modules and returns Group objects to the user...
Definition: lookup.hpp:24
std::shared_ptr< Group > getGroupFromNames(const std::vector< std::string > &families, const std::vector< std::string > &names, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from modules with the given names and families.
Definition: lookup.cpp:11


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