mrp2_serial.cpp
Go to the documentation of this file.
2 
3 static const double MILLION = 1000000.0;
4 static const double BILLION = 1000000000.0;
5 
6 MRP2_Serial::MRP2_Serial (std::string port_name, uint32_t baudrate, std::string mode, bool simple)
7  : _port_name(port_name), _baudrate(baudrate), _mode(mode), simple_(simple)
8 {
9  std::fill_n(speeds, 2, 0);
10  e_stop = false;
11  dir_left =false;
12  dir_right=true;
13  tempDataIndex = 0;
14  seekForChar = true;
15  line_ok_ = true;
16  startChar = '$';
18  use_usb_ = false;
19  read_timeout_ = 0.02;
20 
21 }
22 
23 MRP2_Serial::MRP2_Serial (uint16_t vendor_id, uint16_t product_id, int ep_in_addr, int ep_out_addr, bool simple)
24  : vendor_id_(vendor_id), product_id_(product_id), ep_in_addr_(ep_in_addr), ep_out_addr_(ep_out_addr), simple_(simple)
25 {
26  std::fill_n(speeds, 2, 0);
27  e_stop = false;
28  dir_left =false;
29  dir_right=true;
30  tempDataIndex = 0;
31  seekForChar = true;
32  line_ok_ = true;
33  startChar = '$';
35  use_usb_ = true;
36  read_timeout_ = 0.02;
37 }
38 
39 
41 {
42 
43 }
44 
45 void
47 {
48 }
49 
50 void
51 MRP2_Serial::set_speeds(int32_t left_speed, int32_t right_speed)
52 {
53  uint8_t send_array[20];
54 
55  left_speed *= -1;
56  right_speed *= -1;
57 
58  unsigned char s1 = (unsigned char)(left_speed >> 24);
59  unsigned char s2 = (unsigned char)(left_speed >> 16);
60  unsigned char s3 = (unsigned char)(left_speed >> 8);
61  unsigned char s4 = (unsigned char)left_speed;
62 
63  unsigned char s5 = (unsigned char)(right_speed >> 24);
64  unsigned char s6 = (unsigned char)(right_speed >> 16);
65  unsigned char s7 = (unsigned char)(right_speed >> 8);
66  unsigned char s8 = (unsigned char)right_speed;
67 
68  send_array[0] = '$';
69  send_array[1] = setSPEEDS;
70  send_array[2] = 8;
71  send_array[3] = checksum_check_array(send_array, 3);
72  send_array[4] = s1;
73  send_array[5] = s2;
74  send_array[6] = s3;
75  send_array[7] = s4;
76  send_array[8] = s5;
77  send_array[9] = s6;
78  send_array[10] = s7;
79  send_array[11] = s8;
80  send_array[12] = checksum_check_array(send_array, 12);
81 
82  send_and_get_reply(setSPEEDS, send_array, 13, true);
83 
84 }
85 
86 void
87 MRP2_Serial::set_speed_l(int32_t left_speed)
88 {
89  uint8_t send_array[20];
90 
91  left_speed *= -1;
92 
93  unsigned char s1 = (unsigned char)(left_speed >> 24);
94  unsigned char s2 = (unsigned char)(left_speed >> 16);
95  unsigned char s3 = (unsigned char)(left_speed >> 8);
96  unsigned char s4 = (unsigned char)left_speed;
97 
98  send_array[0] = '$';
99  send_array[1] = setSPEED_L;
100  send_array[2] = 4;
101  send_array[3] = checksum_check_array(send_array, 3);
102  send_array[4] = s1;
103  send_array[5] = s2;
104  send_array[6] = s3;
105  send_array[7] = s4;
106  send_array[8] = checksum_check_array(send_array, 8);
107 
108  send_and_get_reply(setSPEED_L, send_array, 9, true);
109 
110 }
111 
112 void
113 MRP2_Serial::set_speed_r(int32_t right_speed)
114 {
115  uint8_t send_array[20];
116 
117  right_speed *= -1;
118 
119  unsigned char s5 = (unsigned char)(right_speed >> 24);
120  unsigned char s6 = (unsigned char)(right_speed >> 16);
121  unsigned char s7 = (unsigned char)(right_speed >> 8);
122  unsigned char s8 = (unsigned char)right_speed;
123 
124  send_array[0] = '$';
125  send_array[1] = setSPEED_R;
126  send_array[2] = 4;
127  send_array[3] = checksum_check_array(send_array, 3);
128  send_array[4] = s5;
129  send_array[5] = s6;
130  send_array[6] = s7;
131  send_array[7] = s8;
132  send_array[8] = checksum_check_array(send_array, 8);
133 
134  send_and_get_reply(setSPEED_R, send_array, 9, true);
135 
136 }
137 
138 void
139 MRP2_Serial::set_param_pid(char side, char param, float value)
140 {
141 
142  //printf("Float value: %f\n", value);
143  uint8_t send_array[20];
144 
145  union {
146  float f;
147  unsigned char bytes[4];
148  } val;
149 
150  val.f = value;
151 
152 
153  unsigned char fl_array[4];
154  memcpy(fl_array, &val, 4);
155 
156 
157  if(side == 'L')
158  {
159  switch(param)
160  {
161  case 'P':
162  send_array[1] = setPARAM_KP_L;
163  break;
164  case 'I':
165  send_array[1] = setPARAM_KI_L;
166  break;
167  case 'D':
168  send_array[1] = setPARAM_KD_L;
169  break;
170  default:
171  break;
172  }
173  }else if(side == 'R')
174  {
175  switch(param)
176  {
177  case 'P':
178  send_array[1] = setPARAM_KP_R;
179  break;
180  case 'I':
181  send_array[1] = setPARAM_KI_R;
182  break;
183  case 'D':
184  send_array[1] = setPARAM_KD_R;
185  break;
186  default:
187  break;
188  }
189  }
190 
191  //printf("Side: %c, param: %c:\n", side, param);
192  //print_array(fl_array, 4);
193 
194  send_array[0] = '$';
195  send_array[2] = 4;
196  send_array[3] = checksum_check_array(send_array, 3);
197  send_array[4] = fl_array[3];
198  send_array[5] = fl_array[2];
199  send_array[6] = fl_array[1];
200  send_array[7] = fl_array[0];
201  send_array[8] = checksum_check_array(send_array, 8);
202 
203  send_and_get_reply(send_array[1], send_array, 9, true);
204 }
205 
206 void
207 MRP2_Serial::set_param_imax(char side, uint32_t value)
208 {
209  uint8_t send_array[20];
210 
211  if(side == 'L'){
212  send_array[1] = setPARAM_IMAX_L;
213  }else if(side == 'R'){
214  send_array[1] = setPARAM_IMAX_R;
215  }
216 
217  send_array[0] = '$';
218  send_array[2] = 4;
219  send_array[3] = checksum_check_array(send_array, 3);
220  send_array[4] = (unsigned char)(value >> 24);
221  send_array[5] = (unsigned char)(value >> 16);
222  send_array[6] = (unsigned char)(value >> 8);
223  send_array[7] = (unsigned char)value;
224  send_array[8] = checksum_check_array(send_array, 8);
225 
226  send_and_get_reply(send_array[1], send_array, 9, true);
227 
228 }
229 
230 void
232 {
233  uint8_t send_array[20];
234 
235  send_array[0] = '$';
236  send_array[1] = setMAXSPEED_FWD;
237  send_array[2] = 4;
238  send_array[3] = checksum_check_array(send_array, 3);
239  send_array[4] = (unsigned char)(value >> 24);
240  send_array[5] = (unsigned char)(value >> 16);
241  send_array[6] = (unsigned char)(value >> 8);
242  send_array[7] = (unsigned char)value;
243  send_array[8] = checksum_check_array(send_array, 8);
244 
245  send_and_get_reply(setMAXSPEED_FWD, send_array, 9, true);
246 }
247 
248 void
250 {
251  uint8_t send_array[20];
252 
253  send_array[0] = '$';
254  send_array[1] = setMAXSPEED_REV;
255  send_array[2] = 4;
256  send_array[3] = checksum_check_array(send_array, 3);
257  send_array[4] = (unsigned char)(value >> 24);
258  send_array[5] = (unsigned char)(value >> 16);
259  send_array[6] = (unsigned char)(value >> 8);
260  send_array[7] = (unsigned char)value;
261  send_array[8] = checksum_check_array(send_array, 8);
262 
263  send_and_get_reply(setMAXSPEED_REV, send_array, 9, true);
264 }
265 
266 void
268 {
269  uint8_t send_array[20];
270 
271  send_array[0] = '$';
272  send_array[1] = setMAXACCEL;
273  send_array[2] = 4;
274  send_array[3] = checksum_check_array(send_array, 3);
275  send_array[4] = (unsigned char)(value >> 24);
276  send_array[5] = (unsigned char)(value >> 16);
277  send_array[6] = (unsigned char)(value >> 8);
278  send_array[7] = (unsigned char)value;
279  send_array[8] = checksum_check_array(send_array, 8);
280 
281 
282  send_and_get_reply(setMAXACCEL, send_array, 9, true);
283 }
284 
285 void
287 {
288  uint8_t send_array[20];
289 
290  send_array[0] = '$';
291  send_array[1] = setESTOP;
292  send_array[2] = 1;
293  send_array[3] = checksum_check_array(send_array, 3);
294  send_array[4] = (unsigned char)value;
295  send_array[5] = checksum_check_array(send_array, 6);
296 
297  send_and_get_reply(setESTOP, send_array, 6, true);
298 }
299 
300 void
302 {
303  uint8_t send_array[20];
304  send_array[0] = '$';
305  send_array[1] = clearDIAG;
306  send_array[2] = 1;
307  send_array[3] = checksum_check_array(send_array, 3);
308  send_array[4] = diag;
309  send_array[5] = checksum_check_array(send_array, 5);
310 
311  send_and_get_reply(clearDIAG, send_array, 6, true);
312 }
313 
314 std::vector<int>
316 {
317  if (update) {
318  uint8_t send_array[2];
319  send_array[0] = '$';
320  send_array[1] = getSPEEDS;
321  send_and_get_reply(getSPEEDS, send_array, 2, false);
322  }
323  return _speeds;
324 }
325 
326 int
328 {
329  if (update) {
330  uint8_t send_array[2];
331  send_array[0] = '$';
332  send_array[1] = getSPEED_L;
333  send_and_get_reply(getSPEED_L, send_array, 2, false);
334  }
335  return _speed_l;
336 }
337 
338 int
340 {
341  if (update) {
342  uint8_t send_array[2];
343  send_array[0] = '$';
344  send_array[1] = getSPEED_R;
345  send_and_get_reply(getSPEED_R, send_array, 2, false);
346  }
347  return _speed_r;
348 }
349 
350 float
351 MRP2_Serial::get_param_pid(char side, char param, bool update)
352 {
353  uint8_t send_array[2];
354 
355  send_array[0] = '$';
356 
357  if(side == 'L')
358  {
359  if(param == 'P'){
360  if(update){
361  send_array[1] = getPARAM_KP_L;
362  send_and_get_reply(send_array[1], send_array, 2, false);
363  }
364  return _Kp_l;
365  }else if(param == 'I'){
366  if(update){
367  send_array[1] = getPARAM_KI_L;
368  send_and_get_reply(send_array[1], send_array, 2, false);
369  }
370  return _Ki_l;
371  }else if(param == 'D'){
372  if(update){
373  send_array[1] = getPARAM_KD_L;
374  send_and_get_reply(send_array[1], send_array, 2, false);
375  }
376  return _Kd_l;
377  }
378  }
379  else if(side == 'R'){
380  if(param == 'P'){
381  if(update){
382  send_array[1] = getPARAM_KP_R;
383  send_and_get_reply(send_array[1], send_array, 2, false);
384  }
385  return _Kp_r;
386  }else if(param == 'I'){
387  if(update){
388  send_array[1] = getPARAM_KI_R;
389  send_and_get_reply(send_array[1], send_array, 2, false);
390  }
391  return _Ki_r;
392  }else if(param == 'D'){
393  if(update){
394  send_array[1] = getPARAM_KD_R;
395  send_and_get_reply(send_array[1], send_array, 2, false);
396  }
397  return _Kd_r;
398  }
399  }
400 
401 }
402 
403 std::vector<int>
405 {
406  if (update) {
407  uint8_t send_array[2];
408 
409  send_array[0] = '$';
410 
411  if(side == 'L')
412  {
413  send_array[1] = getPARAM_IMAX_L;
414  }else if(side =='R')
415  {
416  send_array[1] = getPARAM_IMAX_R;
417  }
418 
419  send_and_get_reply(send_array[1], send_array, 2, false);
420  }
421  return _imax;
422 }
423 
424 int
426 {
427  if (update) {
428  uint8_t send_array[2];
429  send_array[0] = '$';
430  send_array[1] = getMAXSPEED_FWD;
431  send_and_get_reply(getMAXSPEED_FWD, send_array, 2, false);
432  }
433  return _maxspeed_fwd;
434 }
435 
436 int
438 {
439  if (update) {
440  uint8_t send_array[2];
441  send_array[0] = '$';
442  send_array[1] = getMAXSPEED_REV;
443  send_and_get_reply(getMAXSPEED_REV, send_array, 2, false);
444  }
445  return _maxspeed_rev;
446 }
447 
448 int
450 {
451  if (update) {
452  uint8_t send_array[2];
453  send_array[0] = '$';
454  send_array[1] = getMAXACCEL;
455  send_and_get_reply(getMAXACCEL, send_array, 2, false);
456  }
457  return _maxaccel;
458 }
459 
460 int
462 {
463  if (update) {
464  uint8_t send_array[2];
465  send_array[0] = '$';
466  send_array[1] = getBATT_VOLT;
467  send_and_get_reply(getBATT_VOLT, send_array, 2, false);
468  }
469  return _batt_volt;
470 }
471 
472 int
474 {
475  if (update) {
476  uint8_t send_array[2];
477  send_array[0] = '$';
478  send_array[1] = getBATT_CURRENT;
479  send_and_get_reply(getBATT_CURRENT, send_array, 2, false);
480  }
481  return _batt_current;
482 }
483 
484 int
486 {
487  if (update) {
488  uint8_t send_array[2];
489  send_array[0] = '$';
490  send_array[1] = getBATT_SOC;
491  send_and_get_reply(getBATT_SOC, send_array, 2, false);
492  }
493  return _batt_soc;
494 }
495 
496 std::vector<int>
498 {
499  if (update) {
500  uint8_t send_array[2];
501  send_array[0] = '$';
502  send_array[1] = getPOSITIONS;
503  send_and_get_reply(getPOSITIONS, send_array, 2, false);
504  }
505  return _positions;
506 }
507 
508 int
510 {
511  if (update) {
512  uint8_t send_array[2];
513  send_array[0] = '$';
514  send_array[1] = getPOSITION_L;
515  send_and_get_reply(getPOSITION_L, send_array, 2, false);
516  }
517  return _position_l;
518 }
519 
520 int
522 {
523  if (update) {
524  uint8_t send_array[2];
525  send_array[0] = '$';
526  send_array[1] = getPOSITION_R;
527  send_and_get_reply(getPOSITION_R, send_array, 2, false);
528  }
529  return _position_r;
530 }
531 
532 std::vector<int>
534 {
535  if (update) {
536  uint8_t send_array[2];
537  send_array[0] = '$';
538  send_array[1] = getBUMPERS;
539  send_and_get_reply(getBUMPERS, send_array, 2, false);
540  }
541  return _bumpers;
542 }
543 
544 void
546 {
547  uint8_t send_array[10];
548 
549  send_array[0] = '$';
550  send_array[1] = resetPOSITIONS;
551  send_array[2] = 0;
552  send_array[3] = checksum(4);
553 
554  send_and_get_reply(resetPOSITIONS, send_array, 4, true);
555 }
556 
557 void
559 {
560  uint8_t send_array[10];
561 
562  send_array[0] = '$';
563  send_array[1] = resetPOSITION_L;
564  send_array[2] = 0;
565  send_array[3] = checksum(4);
566 
567  send_and_get_reply(resetPOSITION_L, send_array, 4, true);
568 }
569 
570 void
572 {
573  uint8_t send_array[10];
574 
575  send_array[0] = '$';
576  send_array[1] = resetPOSITION_R;
577  send_array[2] = 0;
578  send_array[3] = checksum(4);
579 
580  send_and_get_reply(resetPOSITION_R, send_array, 4, true);
581 }
582 
583 bool
585 {
586  if (update) {
587  uint8_t send_array[2];
588  send_array[0] = '$';
589  send_array[1] = getESTOP;
590  send_and_get_reply(getESTOP, send_array, 2, false);
591  }
592  return _estop;
593 }
594 
595 void
597 {
598  uint8_t send_array[2];
599  send_array[0] = '$';
600  send_array[1] = getDIAG;
601  send_and_get_reply(getSPEED_R, send_array, 2, false);
602 }
603 
604 bool
606 {
607  switch(diag){
608  case DIAG_MOTOR_STALL_L:
609  return _diag_motor_stall_l;
610  break;
611  case DIAG_MOTOR_STALL_R:
612  return _diag_motor_stall_r;
613  break;
614  case DIAG_BATT_LOW:
615  return _diag_batt_low;
616  break;
617  case DIAG_BATT_HIGH:
618  return _diag_batt_high;
619  break;
620  case DIAG_MOTOR_DRVR_ERR:
621  return _diag_motor_drvr_err;
622  break;
623  case DIAG_AUX_LIGHTS_ERR:
624  return _diag_aux_lights_err;
625  break;
626  }
627 }
628 
629 int
631 {
632  if (update) {
633  uint8_t send_array[2];
634  send_array[0] = '$';
635  send_array[1] = getBATT_CELL_CAPACITY;
636  send_and_get_reply(getBATT_CELL_CAPACITY, send_array, 2, false);
637  }
638  return _batt_cell_capacity;
639 }
640 
641 void
643 {
644  uint8_t send_array[20];
645 
646  send_array[0] = '$';
647  send_array[1] = setBUMPER_ESTOP;
648  send_array[2] = 1;
649  send_array[3] = checksum_check_array(send_array, 3);
650  send_array[4] = (unsigned char)value;
651  send_array[5] = checksum_check_array(send_array, 6);
652 
653  send_and_get_reply(setBUMPER_ESTOP, send_array, 6, true);
654 }
655 
656 bool
658 {
659  if (update) {
660  uint8_t send_array[2];
661  send_array[0] = '$';
662  send_array[1] = getBUMPER_ESTOP;
663  send_and_get_reply(getBUMPER_ESTOP, send_array, 2, false);
664  }
665  return _bumper_estop;
666 }
667 
668 bool
670 {
671  if (update) {
672  uint8_t send_array[2];
673  send_array[0] = '$';
674  send_array[1] = getESTOP_BTN;
675  send_and_get_reply(getESTOP_BTN, send_array, 2, false);
676  }
677  return _estop_btn;
678 }
679 
680 std::vector<int>
682 {
683  if (update) {
684  uint8_t send_array[2];
685  send_array[0] = '$';
686  send_array[1] = getSONARS;
687  send_and_get_reply(getSONARS, send_array, 2, false);
688  }
689  return _sonars;
690 }
691 
692 int
693 MRP2_Serial::send_and_get_reply(uint8_t _command, uint8_t *send_array, int send_size, bool is_ack)
694 {
695  struct timeval tv1, tv2;
696  struct timespec ctv1, ctv2;
697 
698  double _time_diff = 0;
699  int _ret_val = 0;
700 
701  gettimeofday(&tv1, NULL);
702 
703  //printf("%ld.%06ld - SAGR called: %d\n", tv1.tv_sec, tv1.tv_usec, _command);
704 
705  while (!line_ok_)
706  {
707  usleep(1);
708  }
709 
710  line_ok_ = false;
711 
712  gettimeofday(&tv1, NULL);
713 
714  //printf("%ld.%06ld - Serial send called: %d\n", tv1.tv_sec, tv1.tv_usec, _command);
715 
716  if(use_usb_)
717  int ret = usb_port.write_bytes(send_array,send_size);
718  else
719  int ret = serial_port.send_buf(send_array,send_size);
720 
721  gettimeofday(&tv1, NULL);
722  gettimeofday(&tv2, NULL);
723 
724  clock_gettime(CLOCK_MONOTONIC, &ctv1);
725  clock_gettime(CLOCK_MONOTONIC, &ctv2);
726 
727  //printf("%ld.%06ld - Command sent: %d\n", ctv1.tv_sec, ctv1.tv_nsec, _command);
728 
729  if (is_ack)
730  {
731  _ack_data = 0;
732  while (_ack_data != _command && _time_diff < read_timeout_)
733  {
734  _ret_val = read_serial(ACK);
735  //gettimeofday(&tv2, NULL);
736  //_time_diff = (double) (tv2.tv_usec - tv1.tv_usec) / MILLION + (double) (tv2.tv_sec - tv1.tv_sec);
737  clock_gettime(CLOCK_MONOTONIC, &ctv2);
738  _time_diff = ctv2.tv_sec - ctv1.tv_sec + (ctv2.tv_nsec - ctv1.tv_nsec) / BILLION;
739  usleep(1);
740  }
741  line_ok_ = true;
742  /*if(_ack_data != _command && _time_diff >= _time_out)
743  printf("ACK Timeout!, Command: %d\n", _command);
744  else if (_ack_data == _command && _time_diff < _time_out)
745  printf("%ld.%06ld - Command reply received: %d\n", ctv2.tv_sec, ctv2.tv_nsec, _command);*/
746  }
747  else
748  {
749  while (_ret_val == 0 && _time_diff < read_timeout_)
750  {
751 
752  _ret_val = read_serial(_command);
753  //gettimeofday(&tv2, NULL);
754  //_time_diff = (double) (tv2.tv_usec - tv1.tv_usec) / MILLION + (double) (tv2.tv_sec - tv1.tv_sec);
755  clock_gettime(CLOCK_MONOTONIC, &ctv2);
756  _time_diff = ctv2.tv_sec - ctv1.tv_sec + (ctv2.tv_nsec - ctv1.tv_nsec) / BILLION;
757  usleep(1);
758  }
759  line_ok_ = true;
760  /*if(_ret_val == 0 && _time_diff >= _time_out)
761  printf("Read Timeout!, Command: %d\n", _command);
762  else if (_ret_val != 0 && _time_diff < _time_out)
763  printf("%ld.%06ld - Command reply received: %d\n", ctv2.tv_sec, ctv2.tv_nsec, _command);*/
764 
765  }
766 
767 
768  return _ret_val;
769 }
770 
771 bool
773 {
774  return line_ok_;
775 }
776 
777 int
778 MRP2_Serial::read_serial (uint8_t _command_to_read)
779 {
780  uint8_t inData[24] = {0};
781  int recievedData = 0;
782 
783  if(use_usb_)
784  recievedData = usb_port.read_bytes(inData,24);
785  else
786  recievedData = serial_port.poll_comport(inData, 24);
787 
788  /*if(_command_to_read == getSONARS && recievedData > 0)
789  print_array(inData, recievedData);*/
790 
791  if (recievedData == 0)
792  return 0;
793 
794  if (simple_)
795  return process_simple(inData, recievedData, _command_to_read);
796  else
797  return process(inData, recievedData, _command_to_read);
798 }
799 
800 int
801 MRP2_Serial::process_simple (uint8_t *inData, int recievedData, uint8_t _command_to_read)
802 {
803  int ret_val_ = 0;
804 
805  uint8_t* ret_data_ = new uint8_t[recievedData];
806  memcpy( ret_data_, inData, recievedData * sizeof(uint8_t) );
807 
808  int data_len = first_validator(ret_data_);
809  if(second_validator(ret_data_, data_len) != -1)
810  {
811  ret_val_ = execute_command(ret_data_);
812  }
813  return ret_val_;
814 }
815 
816 int
817 MRP2_Serial::process (uint8_t *inData, int recievedData, uint8_t _command_to_read)
818 {
819  int startIndex = 0;
820  int proc_length = 0;
821  int while_cnt = 0;
822  int _ret_val = 0;
823 
824  if (recievedData > 0)
825  {
826 
827  if(tempDataIndex == 0)
828  {
829  memcpy(tempData, inData, recievedData);
830  tempDataIndex = recievedData;
831  }else{
832  memcpy(&tempData[tempDataIndex], &inData[0], recievedData);
833  tempDataIndex += recievedData;
834  }
835 
836 
838  }
839 
840  if (tempDataIndex > 3 && tempData[0] == startChar)
841  {
842 
843  int data_len = first_validator(tempData);
844 
845 
846  if (tempDataIndex < data_len+5)
847  {
848  return _ret_val;
849  }
850 
851 
852  if (data_len != -1)
853  {
854  if (tempData[1] != _command_to_read)
855  {
856  tempData[0] = '0';
857  return _ret_val;
858  }
859  }
860 
861  if(data_len == 0 && tempDataIndex >= 3)
862  {
863  _ret_val = execute_command(tempData);
864  tempData[0] = '0';
866  }else if(data_len > 0 && tempDataIndex >= data_len+5)
867  {
868  if(second_validator(tempData, data_len) != -1)
869  {
870 
871  _ret_val = execute_command(tempData);
872 
873  }
874  tempData[0] = '0';
876 
877  }else if(data_len == -1){
878 
879  tempData[0] = '0';
881  }
882 
883  }
884 
885  return _ret_val;
886 
887 }
888 
889 void
890 MRP2_Serial::array_chopper(uint8_t *buf, int start, int end) {
891  int k = 0;
892  for (int i = start; i < start+end; i++)
893  {
894  tempData[k] = buf[i];
895  k++;
896  }
897 };
898 
899 unsigned char
901 {
902  uint8_t ret = 0;
903  for(int i=0; i<size; i++)
904  {
905  ret = ret + sendArray[i];
906  }
907  if(size > 3)
908  {
909  return (ret & 255) - 1;
910  }
911  return ret & 255 ;
912 }
913 
914 unsigned char
915 MRP2_Serial::checksum_check_array(uint8_t *arr, int size)
916 {
917  uint8_t ret = 0;
918  for(int i=0; i<size; i++)
919  {
920  ret = ret + arr[i];
921  }
922  if(size > 3)
923  {
924  ret = (ret & 0xFF) - 1;
925  }
926  else{
927  ret = ret & 0xFF;
928  }
929  return ret;
930 }
931 
932 bool
933 MRP2_Serial::checksum_match(uint8_t *buf, int size) {
934  uint8_t checksum = 0;
935  int i = 0;
936  for(i = 0; i<size; i++)
937  {
938  checksum = checksum + buf[i];
939  }
940  if(size > 3)
941  {
942  checksum = (checksum & 0xFF) - 1;
943 
944  }else{
945  checksum = checksum & 0xFF;
946  }
947 
948  if(checksum == buf[size])
949  {
950  return true;
951  }
952  return false;
953 };
954 
955 int
957  if(checksum_match(buf, 3))
958  {
959  return buf[2];
960  }
961  return -1;
962 };
963 
964 int
965 MRP2_Serial::second_validator(uint8_t *buf, int data_len) {
966  int total_len = data_len + 5;
967  if(checksum_match(buf, 4 + data_len))
968  {
969  return 1;
970  }
971  return -1;
972 };
973 
974 int
975 MRP2_Serial::find_message_start(uint8_t *buf, int lastIndex) {
976  int start = 0;
977  for (start = 0; start < lastIndex ; start++)
978  {
979  if(buf[start] == startChar)
980  {
981  break;
982  }
983  }
984 
985  lastIndex = lastIndex - start;
986 
987  array_chopper(buf, start, lastIndex+1);
988 
989  return lastIndex;
990 };
991 
992 int
994 
995  union{
996  float f;
997  uint8_t bytes[4];
998  }val;
999 
1000  if(buf[1] == getBUMPERS)
1001  {
1002  _bumpers.clear();
1003  _bumpers.push_back((buf[4] >> 3) & 0x01); // front left
1004  _bumpers.push_back((buf[4] >> 2) & 0x01); // front right
1005  _bumpers.push_back((buf[4] >> 1) & 0x01); // rear left
1006  _bumpers.push_back(buf[4] & 0x01); // rear right
1007 
1008  }
1009 
1010  if(buf[1] == 52)
1011  {
1012  _ack_data = buf[4];
1013  }
1014 
1015  if(buf[1] == getSPEEDS)
1016  {
1017  int l_speed = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1018  int r_speed= buf[8] + (buf[9] << 8) + (buf[10] << 16) + (buf[11] << 24);
1019 
1020  _speed_l = l_speed*-1;
1021  _speed_r = r_speed*-1;
1022  _speeds.clear();
1023  _speeds.push_back(_speed_l);
1024  _speeds.push_back(_speed_r);
1025 
1026  }
1027 
1028  if(buf[1] == getSPEED_L)
1029  {
1030  int l_speed = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1031 
1032  _speed_l = l_speed*-1;
1033  _speeds.clear();
1034  _speeds.push_back(_speed_l);
1035  _speeds.push_back(_speed_r);
1036 
1037  }
1038 
1039  if(buf[1] == getSPEED_R)
1040  {
1041  int r_speed = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1042 
1043  _speed_r = r_speed*-1;
1044  _speeds.clear();
1045  _speeds.push_back(_speed_l);
1046  _speeds.push_back(_speed_r);
1047 
1048  }
1049 
1050  if(buf[1] == getPARAM_KP_L)
1051  {
1052  //printf("KP_L: 4:%d, 5:%d, 6:%d, 7:%d\n", buf[4], buf[5], buf[6], buf[7]);
1053 
1054  val.bytes[0] = buf[4];
1055  val.bytes[1] = buf[5];
1056  val.bytes[2] = buf[6];
1057  val.bytes[3] = buf[7];
1058 
1059  _Kp_l = val.f;
1060  //printf("kp_l:%f\n", _Kp_l);
1061  }
1062 
1063  if(buf[1] == getPARAM_KI_L)
1064  {
1065  val.bytes[0] = buf[4];
1066  val.bytes[1] = buf[5];
1067  val.bytes[2] = buf[6];
1068  val.bytes[3] = buf[7];
1069  _Ki_l = val.f;
1070  //printf("ki_l:%f\n", _Ki_l);
1071  }
1072 
1073  if(buf[1] == getPARAM_KD_L)
1074  {
1075  val.bytes[0] = buf[4];
1076  val.bytes[1] = buf[5];
1077  val.bytes[2] = buf[6];
1078  val.bytes[3] = buf[7];
1079  _Kd_l = val.f;
1080  //printf("kd_l:%f\n", _Kd_l);
1081  }
1082 
1083  if(buf[1] == getPARAM_KP_R)
1084  {
1085  val.bytes[0] = buf[4];
1086  val.bytes[1] = buf[5];
1087  val.bytes[2] = buf[6];
1088  val.bytes[3] = buf[7];
1089  _Kp_r = val.f;
1090  //printf("kp_r:%f\n", _Kp_r);
1091  }
1092 
1093  if(buf[1] == getPARAM_KI_R)
1094  {
1095  val.bytes[0] = buf[4];
1096  val.bytes[1] = buf[5];
1097  val.bytes[2] = buf[6];
1098  val.bytes[3] = buf[7];
1099  _Ki_r = val.f;
1100  //printf("ki_r:%f\n", _Ki_r);
1101  }
1102 
1103  if(buf[1] == getPARAM_KD_R)
1104  {
1105  val.bytes[0] = buf[4];
1106  val.bytes[1] = buf[5];
1107  val.bytes[2] = buf[6];
1108  val.bytes[3] = buf[7];
1109  _Kd_r = val.f;
1110  //printf("kd_r:%f\n", _Kd_r);
1111  }
1112 
1113  if(buf[1] == getPARAM_IMAX_L)
1114  {
1115  _imax_l = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1116  _imax.clear();
1117  _imax.push_back(_imax_l);
1118  _imax.push_back(_imax_r);
1119  }
1120 
1121  if(buf[1] == getPARAM_IMAX_R)
1122  {
1123  _imax_r = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1124  _imax.clear();
1125  _imax.push_back(_imax_l);
1126  _imax.push_back(_imax_r);
1127  }
1128 
1129  if(buf[1] == getMAXSPEED_FWD)
1130  {
1131  _maxspeed_fwd = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1132  }
1133 
1134  if(buf[1] == getMAXSPEED_REV)
1135  {
1136  _maxspeed_rev = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1137  }
1138 
1139  if(buf[1] == getMAXACCEL)
1140  {
1141  _maxaccel = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1142  }
1143 
1144  if(buf[1] == getBATT_VOLT)
1145  {
1146  _batt_volt = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1147  }
1148 
1149  if(buf[1] == getBATT_CURRENT)
1150  {
1151  _batt_current = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1152  }
1153 
1154  if(buf[1] == getBATT_SOC)
1155  {
1156  _batt_soc = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
1157  }
1158 
1159  if(buf[1] == getPOSITION_L)
1160  {
1161 
1162  int l_position = buf[5] + (buf[6] << 8) + (buf[7] << 16) + (buf[8] << 24);
1163  if(buf[4] == 1)
1164  {
1165  l_position *= -1;
1166  }
1167 
1168  _position_l = l_position;
1169  _positions.clear();
1170  _positions.push_back(_position_l);
1171  _positions.push_back(_position_r);
1172 
1173  }
1174 
1175  if(buf[1] == getPOSITION_R)
1176  {
1177 
1178  int r_position = buf[5] + (buf[6] << 8) + (buf[7] << 16) + (buf[8] << 24);
1179  if(buf[4] == 1)
1180  {
1181  r_position *= -1;
1182  }
1183 
1184  _position_r = r_position;
1185  _positions.clear();
1186  _positions.push_back(_position_l);
1187  _positions.push_back(_position_r);
1188 
1189  }
1190 
1191  if(buf[1] == getESTOP)
1192  {
1193  _estop = buf[4];
1194  }
1195 
1196  if(buf[1] == getDIAG)
1197  {
1198  _diag_motor_stall_l = buf[5];
1199  _diag_motor_stall_r = buf[6];
1200  _diag_batt_low = buf[7];
1201  _diag_batt_high = buf[8];
1202  _diag_motor_drvr_err = buf[9];
1203  _diag_aux_lights_err = buf[10];
1204  }
1205 
1206  if(buf[1] == getBATT_CELL_CAPACITY)
1207  {
1208  _batt_cell_capacity = buf[5] + (buf[6] << 8) + (buf[7] << 16) + (buf[8] << 24);
1209  }
1210 
1211  if(buf[1] == getBUMPER_ESTOP)
1212  {
1213  _bumper_estop = buf[4];
1214  }
1215 
1216  if(buf[1] == getESTOP_BTN)
1217  {
1218  _estop_btn = buf[4];
1219  }
1220 
1221  if(buf[1] == getSONARS)
1222  {
1223  _sonars.clear();
1224  _sonars.push_back(buf[4] + (buf[5] << 8));
1225  _sonars.push_back(buf[6] + (buf[7] << 8));
1226  _sonars.push_back(buf[8] + (buf[9] << 8));
1227  _sonars.push_back(buf[10] + (buf[11] << 8));
1228  _sonars.push_back(buf[12] + (buf[13] << 8));
1229  _sonars.push_back(buf[14] + (buf[15] << 8));
1230  _sonars.push_back(buf[16] + (buf[17] << 8));
1231  }
1232 
1233  return buf[1];
1234 
1235 };
1236 
1237 void
1238 MRP2_Serial::print_array(uint8_t *buf, int length) {
1239  printf("Array: ");
1240  int i = 0;
1241  for (i = 0; i < length; i++)
1242  {
1243  //printf("%02x ",buf[i] );
1244  printf("%d ",buf[i] );
1245  }
1246  printf("\n");
1247 };
1248 
1249 void
1251  read_timeout_ = timeout;
1252 }
1253 
1254 double
1256  return read_timeout_;
1257 }
bool get_diag(int diag)
void set_estop(bool value)
void set_param_pid(char side, char param, float value)
uint16_t product_id_
Definition: mrp2_serial.h:180
double read_timeout_
Definition: mrp2_serial.h:175
void set_param_imax(char side, uint32_t value)
int _maxspeed_fwd
Definition: mrp2_serial.h:150
ROSCPP_DECL void start()
void reset_position_r()
f
void set_read_timeout(double timeout)
int poll_comport(unsigned char *, int)
int send_buf(unsigned char *, int)
int send_and_get_reply(uint8_t _command, uint8_t *send_array, int send_size, bool is_ack)
void update_diag()
milvus::SerialComm serial_port
Definition: mrp2_serial.h:177
int get_batt_volt(bool update=false)
void clear_diag(int diag)
int write_bytes(unsigned char *, int)
Definition: usb_comm.cpp:104
int open_device(uint16_t vendor_id, uint16_t product_id, int ep_in_addr, int ep_out_addr)
Definition: usb_comm.cpp:19
bool _diag_aux_lights_err
Definition: mrp2_serial.h:151
bool is_available()
uint16_t vendor_id_
Definition: mrp2_serial.h:180
int get_maxaccel(bool update=false)
std::vector< int > get_sonars(bool update=false)
void set_speed_l(int32_t left_speed)
Definition: mrp2_serial.cpp:87
int get_speed_r(bool update=false)
static const double BILLION
Definition: mrp2_serial.cpp:4
std::vector< int > _sonars
Definition: mrp2_serial.h:154
double _Ki_r
Definition: mrp2_serial.h:152
double _Kp_l
Definition: mrp2_serial.h:152
int get_batt_current(bool update=false)
void set_maxspeed_rev(uint32_t value)
int process_simple(uint8_t *inData, int recievedData, uint8_t _command_to_read)
std::string _port_name
Definition: mrp2_serial.h:164
int first_validator(uint8_t *buf)
void set_speed_r(int32_t right_speed)
bool get_estop(bool update=false)
void set_speeds(int32_t left_speed, int32_t right_speed)
Definition: mrp2_serial.cpp:51
bool checksum_match(uint8_t *buf, int size)
MRP2_Serial(std::string port_name, uint32_t baudrate=38400, std::string mode="8N1", bool simple=true)
Definition: mrp2_serial.cpp:6
double _Ki_l
Definition: mrp2_serial.h:152
bool _diag_motor_stall_l
Definition: mrp2_serial.h:151
int get_speed_l(bool update=false)
std::vector< int > get_positions(bool update=false)
bool dir_right
Definition: mrp2_serial.h:160
void array_chopper(uint8_t *buf, int start, int end)
int get_position_r(bool update=false)
int ep_out_addr_
Definition: mrp2_serial.h:181
void set_max_accel(uint32_t value)
void print_array(uint8_t *buf, int length)
void update()
Definition: mrp2_serial.cpp:46
static const double MILLION
Definition: mrp2_serial.cpp:3
bool get_estop_button(bool update=false)
double get_read_timeout(void)
int execute_command(uint8_t *buf)
std::vector< int > _positions
Definition: mrp2_serial.h:154
int open_port(std::string port_name, int baudrate, std::string mode_s)
Definition: serial_comm.cpp:18
uint8_t tempDataIndex
Definition: mrp2_serial.h:168
double _Kp_r
Definition: mrp2_serial.h:152
int process(uint8_t *inData, int recievedData, uint8_t _command_to_read)
bool get_bumper_estop(bool update=false)
int second_validator(uint8_t *buf, int data_len)
int get_position_l(bool update=false)
int speeds[2]
Definition: mrp2_serial.h:156
int _bumper_estop
Definition: mrp2_serial.h:150
unsigned char checksum(int size)
int _maxspeed_rev
Definition: mrp2_serial.h:150
void set_bumper_estop(bool value)
std::vector< int > get_speeds(bool update=false)
int _batt_cell_capacity
Definition: mrp2_serial.h:150
int get_maxspeed_rev(bool update=false)
int get_batt_soc(bool update=false)
double _Kd_l
Definition: mrp2_serial.h:152
bool seekForChar
Definition: mrp2_serial.h:170
bool _diag_batt_high
Definition: mrp2_serial.h:151
std::vector< int > _bumpers
Definition: mrp2_serial.h:154
double _Kd_r
Definition: mrp2_serial.h:152
unsigned char checksum_check_array(uint8_t *arr, int size)
milvus::UsbComm usb_port
Definition: mrp2_serial.h:178
void reset_positions()
std::vector< int > _imax
Definition: mrp2_serial.h:154
bool _diag_batt_low
Definition: mrp2_serial.h:151
int read_serial(uint8_t _command_to_read)
int read_bytes(unsigned char *, int)
Definition: usb_comm.cpp:84
bool _diag_motor_stall_r
Definition: mrp2_serial.h:151
int find_message_start(uint8_t *buf, int lastIndex)
int get_batt_cell_capacity(bool update=false)
std::vector< int > get_param_imax(char side, bool update=false)
bool _diag_motor_drvr_err
Definition: mrp2_serial.h:151
void set_maxspeed_fwd(uint32_t value)
virtual ~MRP2_Serial()
Definition: mrp2_serial.cpp:40
float get_param_pid(char side, char param, bool update=false)
int _batt_current
Definition: mrp2_serial.h:150
std::vector< int > get_bumpers(bool update=false)
void reset_position_l()
std::vector< int > _speeds
Definition: mrp2_serial.h:154
std::string _mode
Definition: mrp2_serial.h:164
uint8_t _ack_data
Definition: mrp2_serial.h:172
char sendArray[20]
Definition: mrp2_serial.h:157
char startChar
Definition: mrp2_serial.h:171
uint8_t tempData[10000]
Definition: mrp2_serial.h:167
int get_maxspeed_fwd(bool update=false)


mrp2_hardware
Author(s): Akif Hacinecipoglu
autogenerated on Mon Feb 28 2022 22:53:03