mia_hand_driver  rel 1.0.0
serial_port.cpp
Go to the documentation of this file.
2 
3 namespace mia_hand
4 {
6  mot_pos(0),
7  mot_spe(0),
8  mot_cur(0),
9  fin_sg_raw{0, 0}
10 {
11 
12 }
13 
14 SerialPort::SerialPort(std::mutex* p_finger_data_mtx,
15  std::mutex* p_connection_mtx, bool* p_is_connected):
16  p_finger_data_mtx_(p_finger_data_mtx),
17  p_connection_mtx_(p_connection_mtx),
18  p_is_connected_(p_is_connected)
19 {
20 
21 }
22 
24 {
25  if (this->IsOpen())
26  {
27  this->Close();
28  }
29 }
30 
31 bool SerialPort::open(uint16_t port_num)
32 {
33  if (this->IsOpen())
34  {
35  this->Close();
36  }
37 
38  bool no_exceptions = true;
39 
40  std::string port_name = "/dev/ttyUSB";
41  port_name += std::to_string(port_num);
42 
43  try
44  {
45  this->Open(port_name);
46  }
47  catch (LibSerial::OpenFailed& e)
48  {
49  no_exceptions = false;
50  }
51  catch (std::runtime_error& e)
52  {
53  no_exceptions = false;
54  }
55 
56  if (no_exceptions)
57  {
58  this->SetBaudRate(LibSerial::BaudRate::BAUD_115200);
59  this->FlushIOBuffers();
60  }
61 
62  return no_exceptions;
63 }
64 
66 {
67  bool no_exceptions = true;
68 
69  try
70  {
71  this->Close();
72  }
73  catch (LibSerial::NotOpen& e)
74  {
75  // Keep program running.
76  }
77  catch (std::runtime_error& e)
78  {
79  no_exceptions = false;
80  }
81 
82  return no_exceptions;
83 }
84 
85 void SerialPort::sendCommand(const std::string& command)
86 {
87  serial_write_mtx_.lock();
88 
89  try
90  {
91  Write(command);
92  WriteByte((char) 0x2A);
93  WriteByte((char) 0x0D);
94  }
95  catch (LibSerial::NotOpen& e)
96  {
97  // TODO: decide if notifying somehow the exception or not.
98  }
99  catch (std::runtime_error& e)
100  {
101  // TODO: decide if notifying somehow the exception or not.
102  }
103 
104  serial_write_mtx_.unlock();
105 
106  return;
107 }
108 
110  FingerSerialInfo& index,
111  FingerSerialInfo& mrl,
112  bool& is_checking_on)
113 {
114  bool no_exceptions = true;
115 
116  try
117  {
118  this->ReadLine(stream_msg_, '\n', 1000);
119  }
120  catch (LibSerial::NotOpen& e)
121  {
122  no_exceptions = false;
123  }
124  catch (std::runtime_error& e)
125  {
126  no_exceptions = false;
127 
128  p_connection_mtx_->lock();
129 
130  if (is_checking_on)
131  {
132  *p_is_connected_ = false;
133  }
134 
135  p_connection_mtx_->unlock();
136  }
137 
138  if (no_exceptions)
139  {
140  switch (stream_msg_[0])
141  {
142  case '<':
143 
144  if ('?' == stream_msg_[1])
145  {
146  p_connection_mtx_->lock();
147  is_checking_on = false;
148  *p_is_connected_ = true;
149  p_connection_mtx_->unlock();
150  }
151 
152  break;
153 
154  case 'e':
155 
156  p_finger_data_mtx_->lock();
157 
158  thumb.mot_pos = (stream_msg_[9] - 48) * 100
159  + (stream_msg_[10] - 48) * 10 + stream_msg_[11] - 48;
160 
161  if ('-' == stream_msg_[6])
162  {
163  thumb.mot_pos = -thumb.mot_pos;
164  }
165 
166  index.mot_pos = (stream_msg_[27] - 48) * 100
167  + (stream_msg_[28] - 48) * 10 + stream_msg_[29] - 48;
168 
169  if ('-' == stream_msg_[24])
170  {
171  index.mot_pos = -index.mot_pos;
172  }
173 
174  mrl.mot_pos = (stream_msg_[18] - 48) * 100
175  + (stream_msg_[19] - 48) * 10 + stream_msg_[20] - 48;
176 
177  if ('-' == stream_msg_[15])
178  {
179  mrl.mot_pos = -mrl.mot_pos;
180  }
181 
182  p_finger_data_mtx_->unlock();
183 
184  break;
185 
186  case 's':
187 
188  p_finger_data_mtx_->lock();
189 
190  thumb.mot_spe = (stream_msg_[10] - 48) * 10 + stream_msg_[11] - 48;
191 
192  if ('-' == stream_msg_[6])
193  {
194  thumb.mot_spe = -thumb.mot_spe;
195  }
196 
197  index.mot_spe = (stream_msg_[28] - 48) * 10 + stream_msg_[29] - 48;
198 
199  if ('-' == stream_msg_[24])
200  {
201  index.mot_spe = -index.mot_spe;
202  }
203 
204  mrl.mot_spe = (stream_msg_[19] - 48) * 10 + stream_msg_[20] - 48;
205 
206  if ('-' == stream_msg_[15])
207  {
208  mrl.mot_spe = -mrl.mot_spe;
209  }
210 
211  p_finger_data_mtx_->unlock();
212 
213  break;
214 
215  case 'c':
216 
217  p_finger_data_mtx_->lock();
218 
219  thumb.mot_cur = (stream_msg_[8] - 48) * 1000
220  + (stream_msg_[9] - 48) * 100
221  + (stream_msg_[10] - 48) * 10 + stream_msg_[11] - 48;
222 
223  if ('-' == stream_msg_[6])
224  {
225  thumb.mot_cur = -thumb.mot_cur;
226  }
227 
228  index.mot_cur = (stream_msg_[26] - 48) * 1000
229  + (stream_msg_[27] - 48) * 100
230  + (stream_msg_[28] - 48) * 10 + stream_msg_[29] - 48;
231 
232  if ('-' == stream_msg_[24])
233  {
234  index.mot_cur = -index.mot_cur;
235  }
236 
237  mrl.mot_cur = (stream_msg_[17] - 48) * 1000
238  + (stream_msg_[18] - 48) * 100
239  + (stream_msg_[19] - 48) * 10 + stream_msg_[20] - 48;
240 
241  if ('-' == stream_msg_[15])
242  {
243  mrl.mot_cur = -mrl.mot_cur;
244  }
245 
246  p_finger_data_mtx_->unlock();
247 
248  break;
249 
250  case 'a':
251 
252  p_finger_data_mtx_->lock();
253 
254  thumb.fin_sg_raw[0] = (stream_msg_[35] - 48) * 1000
255  + (stream_msg_[36] - 48) * 100
256  + (stream_msg_[37] - 48) * 10
257  + stream_msg_[38] - 48;
258 
259  if ('-' == stream_msg_[33])
260  {
261  thumb.fin_sg_raw[0] = -thumb.fin_sg_raw[0];
262  }
263 
264  thumb.fin_sg_raw[1] = (stream_msg_[44] - 48) * 1000
265  + (stream_msg_[45] - 48) * 100
266  + (stream_msg_[46] - 48) * 10
267  + stream_msg_[47] - 48;
268 
269  if ('-' == stream_msg_[42])
270  {
271  thumb.fin_sg_raw[1] = -thumb.fin_sg_raw[1];
272  }
273 
274  index.fin_sg_raw[0] = (stream_msg_[26] - 48) * 1000
275  + (stream_msg_[27] - 48) * 100
276  + (stream_msg_[28] - 48) * 10
277  + stream_msg_[29] - 48;
278 
279  if ('-' == stream_msg_[24])
280  {
281  index.fin_sg_raw[0] = -index.fin_sg_raw[0];
282  }
283 
284  index.fin_sg_raw[1] = (stream_msg_[17] - 48) * 1000
285  + (stream_msg_[18] - 48) * 100
286  + (stream_msg_[19] - 48) * 10
287  + stream_msg_[20] - 48;
288 
289  if ('-' == stream_msg_[15])
290  {
291  index.fin_sg_raw[1] = -index.fin_sg_raw[1];
292  }
293 
294  mrl.fin_sg_raw[0] = (stream_msg_[8] - 48) * 1000
295  + (stream_msg_[9] - 48) * 100
296  + (stream_msg_[10] - 48) * 10
297  + stream_msg_[11] - 48;
298 
299  if ('-' == stream_msg_[6])
300  {
301  mrl.fin_sg_raw[0] = -mrl.fin_sg_raw[0];
302  }
303 
304  mrl.fin_sg_raw[1] = (stream_msg_[53] - 48) * 1000
305  + (stream_msg_[54] - 48) * 100
306  + (stream_msg_[55] - 48) * 10
307  + stream_msg_[56] - 48;
308 
309  if ('-' == stream_msg_[51])
310  {
311  mrl.fin_sg_raw[1] = -mrl.fin_sg_raw[1];
312  }
313 
314  p_finger_data_mtx_->unlock();
315 
316  break;
317 
318  default:
319 
320  break;
321  }
322  }
323 
324  return;
325 }
326 } // namespace
mia_hand::SerialPort::SerialPort
SerialPort(std::mutex *p_finger_data_mtx, std::mutex *p_connection_mtx, bool *p_is_connected)
Class destructor.
Definition: serial_port.cpp:14
mia_hand::FingerSerialInfo::fin_sg_raw
int16_t fin_sg_raw[2]
Definition: serial_port.h:42
mia_hand::SerialPort::close
bool close()
Close the serial port.
Definition: serial_port.cpp:65
serial_port.h
mia_hand::FingerSerialInfo
Struct containing all info that could regard a Mia hand motor.
Definition: serial_port.h:28
mia_hand::FingerSerialInfo::FingerSerialInfo
FingerSerialInfo()
Definition: serial_port.cpp:5
mia_hand::SerialPort::sendCommand
void sendCommand(const std::string &command)
Send a command to the Mia hand attached to the serial port.
Definition: serial_port.cpp:85
mia_hand::SerialPort::p_is_connected_
bool * p_is_connected_
Definition: serial_port.h:93
mia_hand::SerialPort::~SerialPort
~SerialPort()
Class destructor.
Definition: serial_port.cpp:23
mia_hand::SerialPort::open
bool open(uint16_t port_num)
Open a serial port.
Definition: serial_port.cpp:31
mia_hand::FingerSerialInfo::mot_cur
int16_t mot_cur
Definition: serial_port.h:41
mia_hand::SerialPort::p_connection_mtx_
std::mutex * p_connection_mtx_
Definition: serial_port.h:91
mia_hand::FingerSerialInfo::mot_pos
int16_t mot_pos
Definition: serial_port.h:39
mia_hand::FingerSerialInfo::mot_spe
int8_t mot_spe
Definition: serial_port.h:40
mia_hand::SerialPort::parseStream
void parseStream(FingerSerialInfo &thumb, FingerSerialInfo &index, FingerSerialInfo &mrl, bool &is_checking_on)
Parse message received from the Mia hand.
Definition: serial_port.cpp:109
mia_hand::SerialPort::serial_write_mtx_
std::mutex serial_write_mtx_
Definition: serial_port.h:89
mia_hand
Definition: cpp_driver.h:16
mia_hand::SerialPort::stream_msg_
std::string stream_msg_
Message received from the Mia hand.
Definition: serial_port.h:87
mia_hand::SerialPort::p_finger_data_mtx_
std::mutex * p_finger_data_mtx_
Definition: serial_port.h:90