-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hokuyo.cc~
executable file
·2048 lines (1700 loc) · 55.3 KB
/
Hokuyo.cc~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Driver class for hokuyo URG-04LX and UTM-30LX
Aleksandr Kushleyev <akushley(at)seas(dot)upenn(dot)edu>
University of Pennsylvania, 2008
BSD license.
--------------------------------------------------------------------
Copyright (c) 2008 Aleksandr Kushleyev
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Hokuyo.hh"
// Constructor
//connect using serial or USB connection
Hokuyo::Hokuyo()
{
_type=HOKUYO_TYPE_URG_04LX;
_baud=0;
_comProtocol=HOKUYO_SERIAL;
_device=std::string("Unknown");
_mode=HOKUYO_SCIP20;
_streaming=false;
//initialize vars to invalid values
_scan_start = -1;
_scan_end = -1;
_scan_skip = -1;
_encoding = -1;
_scan_type = -1;
_packet_length = -1;
_dist_min = -1;
_dist_max = -1;
_dist_res = -1;
_angle_res = -1;
_angle_min = 0;
_angle_max = 0;
_scan_rate = -1;
_count_min = -1;
_count_max = -1;
_count_zero = -1;
//initialize mutexes
pthread_mutex_init(&_data_mutex,NULL);
pthread_cond_init(&_data_cond,NULL);
}
Hokuyo::~Hokuyo()
{
if(sd.Disconnect())
std::cout << "Hokuyo::~Hokuyo - sd.Disconnect() failed to close terminal" <<std::endl;
//clean up mutexes
pthread_mutex_destroy(&_data_mutex);
pthread_cond_destroy(&_data_cond);
}
//connect to the scanner - need to provide device name ("/dev/ttyACM0"),
//baud rate (e.g. 115200), and, optionally, mode (currently SCIP2.0 only)
int Hokuyo::Connect(std::string dev_str, const int baud_rate, const int mode)
{
_device=dev_str;
//connect to the device
if(sd.Connect((char *)(_device.c_str()),baud_rate))
{
std::cout << "Hokuyo::Connect - Connect() failed!" << std::endl;
return -1;
}
//find out what rate the sensor is currently running at. _baud will be set to the current baud rate.
if(!_testBaudRate(115200))
std::cout << "Hokuyo baud rate is 115200bps..." << std::endl;
else if(!_testBaudRate(19200))
std::cout << "Hokuyo baud rate is 19200bps..." << std::endl;
else if(!_testBaudRate(38400))
std::cout << "Hokuyo baud rate is 38400bps..." << std::endl;
else
{
std::cout << "Hokuyo::Connect: failed to detect baud rate!" << std::endl;
std::cout << "Hokuyo::Connect: make sure that the sensor is upgraded to SCIP 2.0" << std::endl;
std::cout << "Hokuyo::Connect: the sensor does not have start in SCIP2.0 but must support it" << std::endl;
sd.Disconnect();
return -1;
}
fflush(stdout);
// Make sure requested baud isn't already set
if(_baud == baud_rate)
std::cout << "Hokuyo::Connect - Hokuyo is already operating @ " << baud_rate << std::endl;
//need to set the baud rate
else
{
std::cout << "Hokuyo::Connect: Attempting to set requested baud rate..." << std::endl;
if (!_setBaudRate(baud_rate))
{
//success
std::cout << "Hokuyo::Connect: Operating @ " << _baud << std::endl;
}
//could not set the baud rate
else
{
std::cout << "Hokuyo::Connect - Unable to set the requested Hokuyo baud rate of " << baud_rate << std::endl;
sd.Disconnect();
return -1;
}
}
// get the status data from sensor to determine its type
if(_getSensorInfoAndParams())
{
std::cout << "Hokuyo::Connect - could not get status data from sensor" << std::endl;
sd.Disconnect();
return -1;
}
std::cout << "Hokuyo::Connect: Turning the laser on" <<std::endl;
if (LaserOn())
{
std::cout << "Hokuyo::Connect: was not able to turn the laser on" << std::endl;
sd.Disconnect();
return -1;
}
std::cout << "Hokuyo::Connect: Initialization Complete." <<std::endl;
return 0;
}
//stop the sensor and disconnect from the device
int Hokuyo::Disconnect() {
if (!sd.IsConnected())
return 0;
if (!LaserOff())
std::cout<<"Hokuyo::Disconnect: Laser has been shut off"<<std::endl;
else
std::cout<<"Hokuyo::Disconnect: ERROR: Wasn't able to shut off the laser"<<std::endl;
sd.Disconnect();
return 0;
}
//find the start of packet in case something got out of sync
int Hokuyo::FindPacketStart()
{
static char data[HOKUYO_MAX_PACKET_LENGTH];
char seq[2]={0x0A,0x0A}; //two LFs
if (!sd.IsConnected())
{
std::cout << "Hokuyo::_findPacketStart: not connected to the sensor!" << std::endl;
return -1;
}
//set the io mode to return when the termination sequence is read
//then we know that we've reached the end of some packet
sd.Set_IO_BLOCK_W_TIMEOUT_W_TERM_SEQUENCE(seq,2,true);
//read until the end sequence is found or until read too much
int num_chars=sd.ReadChars(data,HOKUYO_MAX_PACKET_LENGTH);
if ( num_chars< 1 )
{
std::cout << "Hokuyo::_findPacketStart: Error: terminating character was not read"<<std::endl;
return -1;
}
return 0;
}
//measure the length of a packet in bytes
int Hokuyo::_measurePacketLength()
{
static char dataIn[HOKUYO_MAX_PACKET_LENGTH];
char seq[2]={0x0A,0x0A}; //two LFs
if (!sd.IsConnected())
{
std::cout << "Hokuyo::_measurePacketLength: not connected to the sensor!" << std::endl;
return -1;
}
//set the io mode to return when the termination sequence is read
//then we know that we've reached the end of some packet
sd.Set_IO_BLOCK_W_TIMEOUT_W_TERM_SEQUENCE(seq,2,true);
//read until the end sequence is found or until read too much
int num_chars=sd.ReadChars(dataIn,HOKUYO_MAX_PACKET_LENGTH);
if ( num_chars< 1)
{
std::cout << "Hokuyo::_measurePacketLength: Error: terminating character was not read"<<std::endl;
return -1;
}
//return the number of characters read, which is the packet length
return num_chars;
}
//read the packet either as a one blocked read or look for the terminating sequence
int Hokuyo::_readPacket(char * data, int & packet_length, int timeout_us)
{
if (!sd.IsConnected())
{
std::cout << "Hokuyo::_readPacket: not connected to the sensor!" << std::endl;
return -1;
}
//if packet length is greater than zero, it means that we already know
//the expected length, therefore just use that - dont need to explicitly
//search for the end of packet - just need to check for it at the end.
if (packet_length > 0)
{
//std::cout <<"Hokuyo::_readPacket: using previous packet length" <<std::endl;
sd.Set_IO_BLOCK_W_TIMEOUT();
if (sd.ReadChars(data,packet_length,timeout_us) != packet_length){
std::cout << "Hokuyo::_readPacket: could not read the number of expected characters!" << std::endl;
return -1;
}
}
//packet length is unknown, therefore we need to find it
else
{
//std::cout <<"Hokuyo::_readPacket: need to measure packet length" <<std::endl;
char term_sequence[2]={0x0A,0x0A};
//set the io mode to return when the termination sequence is read
//then we know that we've reached the end of some packet
sd.Set_IO_BLOCK_W_TIMEOUT_W_TERM_SEQUENCE(term_sequence, 2, true);
//read until the end sequence is found or until read too much
int chars_read=sd.ReadChars(data,HOKUYO_MAX_PACKET_LENGTH,timeout_us);
if (chars_read < 0)
{
std::cout << "Hokuyo::_readPacket: could not read the terminating character while trying to determine the packet length!" << std::endl;
return -1;
}
packet_length=chars_read;
}
return 0;
}
//extract the data by verifying the checksum and removing it along with LFs
int Hokuyo::_extractPacket(char * full_packet, char * extracted_packet, int packet_length)
{
int lenNLFC=0; //length of data without LFs and checksum
int lineLength;
int lineCount=0;
if (packet_length < 1)
{
std::cout<<"Hokuyo::_extractPacket: bad length!!!"<<std::endl;
return -1;
}
lineLength=_checkLineChecksum(full_packet, HOKUYO_MAX_LINE_LENGTH);
//line length will be zero at the end of packet, when the last LF is read
while (lineLength > 0)
{
lineCount++;
//copy the data
memcpy(extracted_packet,full_packet,lineLength);
extracted_packet+=lineLength;
full_packet+=(lineLength+2); //advance the pointer 2 chars ahead to skip the checksum and endline char
lenNLFC+=lineLength;
//check the next line
lineLength=_checkLineChecksum(full_packet, HOKUYO_MAX_LINE_LENGTH);
}
if (lineLength < 0)
{
std::cout<<"Hokuyo::_extractPacket: error extracting a line!!! line count="<<lineCount<<std::endl;
return -1;
}
//std::cout<<"Hokuyo::_extractPacket: line count="<<lineCount<<std::endl;
return lenNLFC;
}
//decode the packet using 2- or 3-character enconding
int Hokuyo::_decodePacket(char * extracted_packet, unsigned int * extracted_data, int extracted_length, int encoding)
{
int data_length=0;
if (extracted_length < 1)
{
std::cout<<"Hokuyo::_decodePacket: bad length!!!"<<std::endl;
return -1;
}
switch (encoding)
{
case HOKUYO_2DIGITS:
data_length = 0;
for(int i=0;i<extracted_length-1;i++) //FIXME: do we really need -1 here?
{
*extracted_data=((extracted_packet[i] - 0x30)<<6) + extracted_packet[i+1] - 0x30;
i++;
data_length++;
if (data_length > HOKUYO_MAX_NUM_POINTS)
{
std::cout << "Hokuyo::_decodePacket: returned too many data points" << std::endl;
return -1;
}
}
break;
case HOKUYO_3DIGITS:
data_length = 0;
for(int i=0;i<extracted_length-1;i++) //FIXME: do we really need -1 here?
{
*extracted_data=((extracted_packet[i] - 0x30)<<12) + ((extracted_packet[i+1] - 0x30)<<6) + extracted_packet[i+2] - 0x30;
i+=2;
extracted_data++;
data_length++;
if (data_length > HOKUYO_MAX_NUM_POINTS)
{
std::cout << "Hokuyo::_decodePacket: returned too many data points" << std::endl;
return -1;
}
}
break;
default:
std::cout << "Hokuyo::_decodePacket: bad encoding" << std::endl;
return -1;
}
return data_length;
}
//verify the checksum of the line, which is the last character of the line
int Hokuyo::_checkLineChecksum(char * line, int max_length)
{
char * end_line;
int line_length;
char sum=0;
end_line=(char *)memchr((void *)line,0x0A, max_length);
if (end_line == NULL)
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout<< "Hokuyo::_checkLineChecksum: Error: end of line is not found"<<std::endl;
#endif
return -1;
}
line_length=end_line-line;
//sanity check
if ( (line_length < 0) || (line_length > HOKUYO_MAX_LINE_LENGTH))
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout<< "Hokuyo::_checkLineChecksum: Error: bad line length"<<std::endl;
#endif
return -1;
}
//empty line
if (line_length==0)
return 0;
//compute the checksum up to the checksum character
for (int j=0;j<line_length-1;j++)
sum+=line[j];
//encode the sum
sum = (sum & 0x3f) + 0x30;
if (sum!=line[line_length-1])
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout<<"Hokuyo::_checkLineChecksum:Checksum ERROR!!!"<<std::endl;
#endif
//std::cout<<"line_len="<<line_length<<std::endl;
//std::cout<<"line: "<<line<<std::endl;
//std::cout<<"prev line: "<<std::endl;
//std::cout<<(line-100)<<std::endl;
return -1;
}
return (line_length-1);
}
//read one line from the sensor and optionally verify the checksum
//max specifies the maximum expected length of the line
int Hokuyo::_readLine(char * line,int max_chars, int timeout_us,bool checkSum)
{
char seq[1]={0x0A}; //LF
if (!sd.IsConnected())
{
std::cout << "Hokuyo::_readLine: not connected to the sensor!" << std::endl;
return -1;
}
//set the io mode to return when LF is read or timeout or read too much
sd.Set_IO_BLOCK_W_TIMEOUT_W_TERM_SEQUENCE(seq,1,true);
//read the line
int num_chars=sd.ReadChars(line,max_chars, timeout_us);
if ( num_chars< 1)
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout << "Hokuyo::_readLine: Error: terminating character was not read"<<std::endl;
#endif
return -1;
}
//empty line, therefore return
if (num_chars==1)
{
if (line[0]==0x0A)
return 0;
else
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout << "Hokuyo::_readLine: Error: read one char and it was not an endl"<<std::endl;
#endif
return -1;
}
}
if (!checkSum)
return num_chars-1;
if (_checkLineChecksum(line, num_chars) != num_chars-2)
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout << "Hokuyo::_readLine: Error: checksum error"<<std::endl;
#endif
return -1;
}
return (num_chars-2);
}
//turn the laser on
int Hokuyo::LaserOn()
{
if (LaserOff())
return -1;
if (_laserOnOff(HOKUYO_TURN_LASER_ON))
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout<<"Hokuyo::LaserOn: ERROR: Wasn't able to turn on the laser"<<std::endl;
#endif
return -1;
}
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout<<"Hokuyo::LaserOn: Laser has been turned on"<<std::endl;
#endif
return 0;
}
//turn the laser off
int Hokuyo::LaserOff()
{
for (int i=0;i<HOKUYO_NUM_STOP_LASER_RETRIES; i++)
{
if (!_laserOnOff(HOKUYO_TURN_LASER_OFF))
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout<<"Hokuyo::LaserOff: Laser has been shut off"<<std::endl;
#endif
return 0;
}
else if (i==HOKUYO_NUM_STOP_LASER_RETRIES-1)
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout<<"Hokuyo::LaserOff: ERROR: Wasn't able to shut off the laser"<<std::endl;
#endif
}
usleep(HOKUYO_LASER_STOP_DELAY_US);
}
return -1;
}
//turn on/off the laser
int Hokuyo::_laserOnOff(bool turnOn)
{
char line[HOKUYO_MAX_LINE_LENGTH];
if (!sd.IsConnected())
{
std::cout << "Hokuyo::_laserOnOff: not connected to the sensor!" << std::endl;
return -1;
}
char cmd[3];
char resp1[2];
char resp2[2];
if (turnOn)
{
memcpy(cmd,"BM\n",3);
memcpy(resp1,"00",2);
memcpy(resp2,"02",2);
}
else
{
memcpy(cmd,"QT\n",3);
memcpy(resp1,"00",2);
memcpy(resp2,"00",2);
}
//clear out the serial input buffer
sd.FlushInputBuffer();
//turn the laser on or off
if(sd.WriteChars(cmd,3) != 3)
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout << "Hokuyo::_laserOnOff: could not write command" << std::endl;
#endif
return -1;
}
//read back the echo of the command
int timeout_us=200000;
int line_len=_readLine(line,HOKUYO_MAX_LINE_LENGTH,timeout_us,false);
if(line_len < 0)
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout << "Hokuyo::_laserOnOff: could not read line 1" << std::endl;
#endif
return -1;
}
if(strncmp(cmd,line,line_len))
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout << "Hokuyo::_laserOnOff: response does not match (line 1):" << std::endl;
_printStrings(cmd,line,line_len);
#endif
return -1;
}
//read back the status response
line_len=_readLine(line,HOKUYO_MAX_LINE_LENGTH,timeout_us,true);
if(line_len < 0)
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout << "Hokuyo::LaserOnOff: could not read line 2" << std::endl;
#endif
return -1;
}
if( (strncmp(resp1,line,line_len)!=0) && (strncmp(resp2,line,line_len)!=0))
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout << "Hokuyo::LaserOnOff: response does not match (line 2):" << std::endl;
_printStrings(resp1,line,line_len);
_printStrings(resp2,line,line_len);
#endif
return -1;
}
//read off the LF
line_len=_readLine(line,HOKUYO_MAX_LINE_LENGTH,timeout_us,false);
if(line_len < 0)
{
#ifdef HOKUYO_LOW_LEVEL_DEBUG
std::cout << "Hokuyo::LaserOnOff: could not read line 3" << std::endl;
#endif
return -1;
}
if (!turnOn)
{
_streaming=false;
}
return 0;
}
//print strings for debuggin purposes
void Hokuyo::_printStrings(const char * exp, char * got, int num)
{
std::cout << "Expected: ";
for (int i=0;i<num;i++)
{
if (exp[i]==0x0A)
{
break;
}
std::cout<<exp[i];
}
std::cout <<std::endl<< "Got: ";
for (int i=0;i<num;i++) std::cout<<got[i];
std::cout <<std::endl;
}
//create a scan request string based on the desired scan parameters
int Hokuyo::CreateScanRequest(int scanStart, int scanEnd, int scanSkip, int encoding,
int scanType, int numScans, char * req,
bool & need_to_read_off_cmd_and_status)
{
//error checking
if (scanStart < 0)
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: bad scanStart" <<std::endl;
return -1;
}
if (scanEnd < 0)
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: bad scanEnd" <<std::endl;
return -1;
}
if ((_type == HOKUYO_TYPE_UTM_30LX) && (scanEnd > HOKUYO_MAX_NUM_POINTS_UTM_30LX))
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: scanEnd is greater than maximum number of points for UTM-30LX" <<std::endl;
std::cout << "Maximum= "<<HOKUYO_MAX_NUM_POINTS_UTM_30LX<<", requested= "<<scanEnd<<std::endl;
return -1;
}
if ((_type == HOKUYO_TYPE_URG_04LX) && (scanEnd > HOKUYO_MAX_NUM_POINTS_URG_04LX))
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: scanEnd is greater than maximum number of points for URG-04LX" <<std::endl;
std::cout << "Maximum= "<<HOKUYO_MAX_NUM_POINTS_URG_04LX<<", requested= "<<scanEnd<<std::endl;
return -1;
}
//correct order of start and end
if (scanEnd <= scanStart)
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: scanEnd < scanStart" <<std::endl;
return -1;
}
//valid number of scans
if ((numScans < 0) || (numScans > 99))
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: bad numScans" <<std::endl;
return -1;
}
//
if ((_type == HOKUYO_TYPE_UTM_30LX) && (encoding == HOKUYO_2DIGITS))
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: UTM-30LX does not support 2-digit data mode." <<std::endl;
return -1;
}
//single scan
if (numScans==1)
{
if (scanType==HOKUYO_SCAN_REGULAR)
{
if (encoding == HOKUYO_3DIGITS)
{
sprintf(req,"GD%04d%04d%02x\n",scanStart,scanEnd,scanSkip);
need_to_read_off_cmd_and_status=false;
}
else if (encoding == HOKUYO_2DIGITS)
{
sprintf(req,"GS%04d%04d%02x\n",scanStart,scanEnd,scanSkip);
need_to_read_off_cmd_and_status=false;
}
else
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: invalid selection of character encoding:"<<encoding << std::endl;
return -1;
}
}
else if ((scanType==HOKUYO_SCAN_SPECIAL_ME) && (_type == HOKUYO_TYPE_UTM_30LX) )
{
if (encoding == HOKUYO_3DIGITS)
{
sprintf(req,"ME%04d%04d%02x0%02d\n",scanStart,scanEnd,scanSkip,numScans);
need_to_read_off_cmd_and_status=true;
}
else
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: invalid selection of character encoding :"<<encoding <<" not supported by UTM_30LX" << std::endl;
return -1;
}
}
else
{
std::cout << "Hokuyo::CreateScanRequest: invalid scan type :"<<scanType <<" not supported by the sensor" << std::endl;
return -1;
}
}
//streaming
else if (numScans==0)
{
need_to_read_off_cmd_and_status=true;
if (scanType==HOKUYO_SCAN_REGULAR)
{
if (encoding == HOKUYO_3DIGITS)
sprintf(req,"MD%04d%04d%02x0%02d\n",scanStart,scanEnd,scanSkip,numScans);
else if (encoding == HOKUYO_2DIGITS)
sprintf(req,"MS%04d%04d%02x0%02d\n",scanStart,scanEnd,scanSkip,numScans);
else
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: invalid selection of character encoding:"<<encoding << std::endl;
return -1;
}
}
else if ((scanType==HOKUYO_SCAN_SPECIAL_ME) && (_type == HOKUYO_TYPE_UTM_30LX) )
{
if (encoding == HOKUYO_3DIGITS)
sprintf(req,"ME%04d%04d%02x0%02d\n",scanStart,scanEnd,scanSkip,numScans);
else
{
std::cout << "Hokuyo::CreateScanRequest: ERROR: invalid selection of character encoding :"<<encoding <<" not supported by UTM_30LX" << std::endl;
return -1;
}
}
else
{
std::cout << "Hokuyo::CreateScanRequest: invalid scan type :"<<scanType <<" not supported by the sensor" << std::endl;
return -1;
}
}
else
{
std::cout << "Hokuyo::CreateScanRequest: invalid number of scans :"<< numScans<< std::endl;
return -1;
}
return 0;
}
//request a scan (obsolete function)
int Hokuyo::RequestScan(char * req)
{
if (!sd.IsConnected())
{
std::cout << "Hokuyo::RequestScan: ERROR: not connected to the sensor!" << std::endl;
return -1;
}
sd.FlushInputBuffer();
//write the data request command
int reqLen = strlen(req);
if(sd.WriteChars(req,reqLen) != reqLen)
{
std::cout << "Hokuyo::RequestScan: ERROR: could not write command" << std::endl;
return -1;
}
return 0;
}
//get the confirmation of the command, requesting a scan
int Hokuyo::ConfirmScan(char * req, int timeout_us)
{
char line[HOKUYO_MAX_LINE_LENGTH];
int line_len;
//read back the echo of the command
line_len=_readLine(line,HOKUYO_MAX_LINE_LENGTH,timeout_us,false);
if(line_len < 0)
{
std::cout << "Hokuyo::ConfirmScan: ERROR: could not read echo of the command (line 1)" << std::endl;
return -1;
}
if(strncmp(req,line,line_len))
{
std::cout << "Hokuyo::ConfirmScan: ERROR: response does not match the echo of the command (line 1):"<<std::endl;
_printStrings(req,line,line_len);
return -1;
}
//read back the status response
line_len=_readLine(line,HOKUYO_MAX_LINE_LENGTH,timeout_us,true);
if(line_len < 0)
{
std::cout << "Hokuyo::ConfirmScan: ERROR: could not read status response(line 2)" << std::endl;
return -1;
}
if(strncmp("00",line,2))
{
std::cout << "Hokuyo::ConfirmScan: ERROR: status response does not match (line 2):"<<std::endl;
_printStrings("00",line,line_len);
if(strncmp("10",line,2)==0)
{
std::cout << "Hokuyo::ConfirmScan: ERROR: (response from scanner)it looks like we need to turn the laser back on" << std::endl;
LaserOn();
}
else if(strncmp("01",line,2)==0)
std::cout << "Hokuyo::ConfirmScan: ERROR: (response from scanner) Starting step has non-numeric value" << std::endl;
else if(strncmp("02",line,2)==0)
std::cout << "Hokuyo::ConfirmScan: ERROR: (response from scanner) End step has non-numeric value" << std::endl;
else if(strncmp("03",line,2)==0)
std::cout << "Hokuyo::ConfirmScan: ERROR: (response from scanner) Cluster count (skip) has non-numeric value" << std::endl;
else if(strncmp("04",line,2)==0)
std::cout << "Hokuyo::ConfirmScan: ERROR: (response from scanner)End step is out of range" << std::endl;
else if(strncmp("05",line,2)==0)
std::cout << "Hokuyo::ConfirmScan: ERROR: (response from scanner)End step is smaller than starting step" << std::endl;
else
std::cout << "Hokuyo::ConfirmScan: ERROR: (response from scanner)Looks like hardware trouble" << std::endl;
return -1;
}
return 0;
}
//request (if needed) a scan from sensor, read the packet and parse everything, returning good data
int Hokuyo::GetScan(unsigned int * range, int & n_range,int scan_start,
int scan_end, int scan_skip, int encoding,
int scan_type, int num_scans)
{
char req[128];
char good_status[2];
char line[HOKUYO_MAX_LINE_LENGTH];
char full_packet[HOKUYO_MAX_PACKET_LENGTH];
char extracted_packet[HOKUYO_MAX_PACKET_LENGTH];
int line_len;
bool need_to_request_scan;
if (!sd.IsConnected())
{
std::cout << "Hokuyo::GetScan: ERROR: not connected to the sensor!" << std::endl;
return -1;
}
//settings have changed, therefore we need to figure out the packet length
if ( (_scan_start!=scan_start) || (_scan_end!=scan_end) || (_scan_skip!=scan_skip) || (_encoding!=encoding) || (_scan_type!=scan_type) )
{
_packet_length=-1;
_scan_ready=false;
if (_streaming)
{
if (!LaserOff())
{
std::cout<<"Hokuyo::GetScan: Laser has been shut off"<<std::endl;
usleep(100000);
}
else
{
std::cout<<"Hokuyo::GetScan: ERROR: Wasn't able to shut off the laser"<<std::endl;
}
}
//std::cout<<"need to check packet size!!!!" <<std::endl;
}
else
{
//std::cout<<"reusing old packet size" <<std::endl;
}
memcpy(good_status,"99",2*sizeof(char));
bool need_to_read_off_cmd_and_status;
if (CreateScanRequest(scan_start,scan_end,scan_skip,encoding,scan_type,num_scans,req,need_to_read_off_cmd_and_status))
{
std::cout << "Hokuyo::GetScan: ERROR: could not create scan request string!" << std::endl;
return -1;
}
if (num_scans==1)
need_to_request_scan=true;
else if (num_scans==0)
{
if (!_streaming)
need_to_request_scan=true;
else
need_to_request_scan=false;
}
else
{
std::cout << "Hokuyo::GetScan: ERROR: invalid number of scans:"<< num_scans << std::endl;
return -1;
}
int timeout_us=0;
switch (_type)
{
case HOKUYO_TYPE_URG_04LX:
timeout_us=HOKUYO_URG_04LX_GET_SCAN_TIMEOUT_US;
break;
case HOKUYO_TYPE_UTM_30LX:
timeout_us=HOKUYO_UTM_30LX_GET_SCAN_TIMEOUT_US;
break;
}
//only send the request command if we need to
//this means only when we request a single scan or to start streaming
if (need_to_request_scan)
{
//std::cout<<"Requesting: "<<req<<std::endl;
if (RequestScan(req))
{
std::cout << "Hokuyo::GetScan: ERROR: could not request scan!" << std::endl;
return -1;
}
if (ConfirmScan(req,timeout_us))
{
std::cout << "Hokuyo::GetScan: ERROR: could not confirm scan!" << std::endl;
return -1;
}
//the read was successful, so it means that if we requested streaming, we got it
if (num_scans==0)
_streaming=true;
if ((num_scans==0) || (scan_type==HOKUYO_SCAN_SPECIAL_ME))
{
//read off the LF character
line_len=_readLine(line,HOKUYO_MAX_LINE_LENGTH,timeout_us,false);
if(line_len < 0)
{
std::cout << "Hokuyo::GetScan: ERROR: could not read LF (line 3) "<< std::endl;
return -1;
}