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