BLEProthesisInterface.cpp
Go to the documentation of this file.
1 #include <sstream>
2 
5 
7  _worker{new BLEProthesisInterfaceQtWorkerThread{mac, *this}},
8  _protocol{protocol}
9 {
10  _worker->start();
11 }
12 
14 {
15  _worker->kill();
16  _worker->quit();
17  _worker->exit();
18  _worker->wait(1000); // ms
19  _worker->terminate();
20  _worker->wait();
21 }
22 
24 {
25  return _thumbPWM;
26 }
27 
29 {
30  return _thumbPos;
31 }
32 
34 {
35  return _fingerPWM;
36 }
37 
39 {
40  return _fingerPos;
41 }
42 
44 {
45  return _state;
46 }
47 
48 void BLEProthesisInterface::sendRaw(const std::string& cmd)
49 {
50  if (_state != State::Running)
51  {
52  throw std::invalid_argument
53  {
54  "BLEProthesisInterface::sendRaw( cmd = " + cmd +
55  " ): The UART service is not connected!"
56  };
57  }
58  _worker->sendCommand(cmd);
59 }
60 
61 void BLEProthesisInterface::sendGrasp(std::uint64_t n)
62 {
63  if (n > getMaxG())
64  {
65  throw std::invalid_argument
66  {
67  "BLEProthesisInterface::sendGrasp( n = " + std::to_string(n) +
68  " ): the maximal value for n is " + std::to_string(getMaxG())
69  };
70  }
71  sendRaw("g" + std::to_string(n) + "\n");
72 }
73 
74 void BLEProthesisInterface::sendThumbPWM(uint64_t v, uint64_t mxPWM, uint64_t pos)
75 {
76  if (v < getMinV() || v > getMaxV())
77  {
78  throw std::invalid_argument
79  {
80  "sendThumbPWM( v = " + std::to_string(v) +
81  " , mxPWM = " + std::to_string(mxPWM) +
82  " , pos = " + std::to_string(pos) +
83  " ): The interval for v is [" + std::to_string(getMinV()) +
84  ", " + std::to_string(getMaxV()) + "]"
85  };
86  }
87  if (mxPWM > getMaxPWM())
88  {
89  throw std::invalid_argument
90  {
91  "sendThumbPWM( v = " + std::to_string(v) +
92  " , mxPWM = " + std::to_string(mxPWM) +
93  " , pos = " + std::to_string(pos) +
94  " ): The interval for mxPWM is [0, , " +
95  std::to_string(getMaxPWM()) + "]"
96  };
97  }
98  if (pos > getMaxPosThumb())
99  {
100  throw std::invalid_argument
101  {
102  "sendThumbPWM( v = " + std::to_string(v) +
103  " , mxPWM = " + std::to_string(mxPWM) +
104  " , pos = " + std::to_string(pos) +
105  " ): The interval for pos is [0, " +
107  };
108  }
109  std::stringstream str;
110  str << 'M' << 2 << ',' << v << ',' << mxPWM << ',' << pos << '\n';
111  sendRaw(str.str());
112 }
113 
114 void BLEProthesisInterface::sendFingerPWM(uint64_t v, uint64_t mxPWM, uint64_t pos)
115 {
116  if (v < getMinV() || v > getMaxV())
117  {
118  throw std::invalid_argument
119  {
120  "sendThumbPWM( v = " + std::to_string(v) +
121  " , mxPWM = " + std::to_string(mxPWM) +
122  " , pos = " + std::to_string(pos) +
123  " ): The interval for v is [" + std::to_string(getMinV()) +
124  ", " + std::to_string(getMaxV()) + "]"
125  };
126  }
127  if (mxPWM > getMaxPWM())
128  {
129  throw std::invalid_argument
130  {
131  "sendThumbPWM( v = " + std::to_string(v) +
132  " , mxPWM = " + std::to_string(mxPWM) +
133  " , pos = " + std::to_string(pos) +
134  " ): The interval for mxPWM is [0, , " +
135  std::to_string(getMaxPWM()) + "]"
136  };
137  }
138  if (pos > getMaxPosFingers())
139  {
140  throw std::invalid_argument
141  {
142  "sendThumbPWM( v = " + std::to_string(v) +
143  " , mxPWM = " + std::to_string(mxPWM) +
144  " , pos = " + std::to_string(pos) +
145  " ): The interval for pos is [0, " +
147  };
148  }
149  std::stringstream str;
150  str << 'M' << 3 << ',' << v << ',' << mxPWM << ',' << pos << '\n';
151  sendRaw(str.str());
152 }
153 
155 {
156  _verboseReceive = b;
157 }
158 
160 {
161  _verboseSend = b;
162 }
163 
165 {
166  switch (s)
167  {
169  return "Created";
171  return "DiscoveringDevices";
173  return "DiscoveringDevicesDone";
175  return "Disconnected";
177  return "Connecting";
179  return "ConnectingDone";
181  return "DiscoveringServices";
183  return "DiscoveringServicesDone";
185  return "ConnectingService";
187  return "Running";
189  return "Killed";
190  }
191  return "UNKNOWN VALUE FOR BLEProthesisInterface::State: " + std::to_string(static_cast<int>(s));
192 }
BLEProthesisInterface::State::Running
@ Running
BLEProthesisInterface::State::Disconnected
@ Disconnected
BLEProthesisInterface::getMaxG
static constexpr std::uint64_t getMaxG()
Definition: BLEProthesisInterface.h:66
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
BLEProthesisInterface::verboseReceive
void verboseReceive(bool b=true)
Definition: BLEProthesisInterface.cpp:154
BLEProthesisInterface::State::Killed
@ Killed
BLEProthesisInterface::State::DiscoveringServices
@ DiscoveringServices
BLEProthesisInterface::State::ConnectingDone
@ ConnectingDone
BLEProthesisInterface::getMaxV
static constexpr std::uint64_t getMaxV()
Definition: BLEProthesisInterface.h:74
BLEProthesisInterface::getMaxPosThumb
static constexpr std::uint64_t getMaxPosThumb()
Definition: BLEProthesisInterface.h:82
BLEProthesisInterface::getMinV
static constexpr std::uint64_t getMinV()
Definition: BLEProthesisInterface.h:70
BLEProthesisInterface::State::Connecting
@ Connecting
BLEProthesisInterface::getFingerPWM
std::int64_t getFingerPWM() const
Definition: BLEProthesisInterface.cpp:33
BLEProthesisInterface::State
State
Definition: BLEProthesisInterface.h:13
BLEProthesisInterface::getState
State getState() const
Definition: BLEProthesisInterface.cpp:43
BLEProthesisInterface::SensorValueProtocol
SensorValueProtocol
Definition: BLEProthesisInterface.h:27
BLEProthesisInterface::State::Created
@ Created
BLEProthesisInterface::getThumbPos
std::int64_t getThumbPos() const
Definition: BLEProthesisInterface.cpp:28
BLEProthesisInterfaceQtWorkerThread
Definition: BLEProthesisInterfaceQtWorkerThread.h:16
BLEProthesisInterface::State::DiscoveringServicesDone
@ DiscoveringServicesDone
BLEProthesisInterface::BLEProthesisInterface
BLEProthesisInterface(const std::string &mac, SensorValueProtocol protocol=SensorValueProtocol::mx_pos_pwm)
Definition: BLEProthesisInterface.cpp:6
BLEProthesisInterface::verboseSend
void verboseSend(bool b=true)
Definition: BLEProthesisInterface.cpp:159
BLEProthesisInterface::getMaxPosFingers
static constexpr std::uint64_t getMaxPosFingers()
Definition: BLEProthesisInterface.h:86
BLEProthesisInterface::State::ConnectingService
@ ConnectingService
BLEProthesisInterface::sendThumbPWM
void sendThumbPWM(std::uint64_t v, std::uint64_t maxPWM, std::uint64_t pos)
Definition: BLEProthesisInterface.cpp:74
BLEProthesisInterface::getFingerPos
std::int64_t getFingerPos() const
Definition: BLEProthesisInterface.cpp:38
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
BLEProthesisInterface::~BLEProthesisInterface
~BLEProthesisInterface()
Definition: BLEProthesisInterface.cpp:13
BLEProthesisInterfaceQtWorkerThread.h
to_string
std::string to_string(BLEProthesisInterface::State s)
Definition: BLEProthesisInterface.cpp:164
BLEProthesisInterface.h
BLEProthesisInterface::getThumbPWM
std::int64_t getThumbPWM() const
Definition: BLEProthesisInterface.cpp:23
BLEProthesisInterface::State::DiscoveringDevicesDone
@ DiscoveringDevicesDone
BLEProthesisInterface::sendGrasp
void sendGrasp(std::uint64_t n)
Definition: BLEProthesisInterface.cpp:61
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
BLEProthesisInterface::State::DiscoveringDevices
@ DiscoveringDevices
BLEProthesisInterface::sendRaw
void sendRaw(const std::string &cmd)
Definition: BLEProthesisInterface.cpp:48
BLEProthesisInterface::sendFingerPWM
void sendFingerPWM(std::uint64_t v, std::uint64_t maxPWM, std::uint64_t pos)
Definition: BLEProthesisInterface.cpp:114
BLEProthesisInterface::getMaxPWM
static constexpr std::uint64_t getMaxPWM()
Definition: BLEProthesisInterface.h:78