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])
28  else if (!prev.buttons_[i] && curr.buttons_[i])
30  if (!prev.buttons_[i] && !curr.buttons_[i])
32  else if (prev.buttons_[i] && !curr.buttons_[i])
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
hebi::experimental::MobileIOState::axes_
std::array< float, NumButtons > axes_
Definition: mobile_io.hpp:33
hebi::experimental::MobileIO::setSnap
bool setSnap(size_t axis_number, float snap_to)
Definition: mobile_io.cpp:85
hebi::experimental::MobileIO::setButtonMode
bool setButtonMode(size_t button_number, ButtonMode mode)
Definition: mobile_io.cpp:97
mobile_io.hpp
hebi::experimental::MobileIO::group_
std::shared_ptr< hebi::Group > group_
Definition: mobile_io.hpp:95
lookup.hpp
hebi::experimental::MobileIOState::getButton
bool getButton(size_t button) const
Definition: mobile_io.cpp:11
hebi::Lookup::getGroupFromNames
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::experimental::MobileIO::clearText
bool clearText()
Definition: mobile_io.cpp:121
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
hebi::experimental::MobileIO::setAxisValue
bool setAxisValue(size_t axis_number, float value)
Definition: mobile_io.cpp:91
hebi::experimental::MobileIO::fbk_
hebi::GroupFeedback fbk_
Definition: mobile_io.hpp:96
hebi::Lookup
Maintains a registry of network-connected modules and returns Group objects to the user.
Definition: lookup.hpp:24
hebi::experimental::NumButtons
static constexpr size_t NumButtons
Definition: mobile_io.hpp:20
group_command.hpp
hebi::experimental::MobileIODiff::ButtonState::ToOff
@ ToOff
hebi::experimental::MobileIODiff::buttons_
std::array< ButtonState, NumButtons > buttons_
Definition: mobile_io.hpp:53
hebi::experimental::MobileIO::create
static std::unique_ptr< MobileIO > create(const std::string &family, const std::string &name)
Definition: mobile_io.cpp:43
hebi::experimental::MobileIOState
Definition: mobile_io.hpp:26
hebi::experimental::MobileIO::current_state_
MobileIOState current_state_
Definition: mobile_io.hpp:97
hebi::experimental::MobileIO::ButtonMode
ButtonMode
Definition: mobile_io.hpp:59
hebi::experimental::MobileIODiff::ButtonState::On
@ On
hebi::experimental::MobileIOState::buttons_
std::bitset< NumButtons > buttons_
Definition: mobile_io.hpp:32
hebi::experimental::MobileIO::ButtonMode::Toggle
@ Toggle
hebi::experimental::MobileIODiff::ButtonState
ButtonState
Definition: mobile_io.hpp:44
hebi::experimental::MobileIO::setButtonOutput
bool setButtonOutput(size_t button_number, bool on)
Definition: mobile_io.cpp:103
hebi
Definition: arm.cpp:5
hebi::experimental::MobileIO::setLedColor
bool setLedColor(uint8_t r, uint8_t g, uint8_t b)
Definition: mobile_io.cpp:109
hebi::experimental::MobileIODiff::MobileIODiff
MobileIODiff(const MobileIOState &prev, const MobileIOState &current)
Definition: mobile_io.cpp:24
hebi::experimental::MobileIODiff::get
ButtonState get(int button) const
Definition: mobile_io.cpp:37
hebi::experimental::MobileIODiff::ButtonState::Off
@ Off
hebi::experimental::MobileIO::sendText
bool sendText(const std::string &message)
Definition: mobile_io.cpp:115
hebi::experimental::MobileIODiff::ButtonState::ToOn
@ ToOn
hebi::experimental::MobileIO::getState
MobileIOState getState()
Definition: mobile_io.cpp:51
hebi::experimental::MobileIOState::getAxis
float getAxis(size_t axis) const
Definition: mobile_io.cpp:17
hebi::experimental::MobileIO::MobileIO
MobileIO(std::shared_ptr< hebi::Group >)
Definition: mobile_io.cpp:127


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