Main MRPT website > C++ reference for MRPT 1.5.7
CCANBusReader.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 // This file contains portions of code from sicklms200.cc from the Player/Stage project.
11 
12 #include "hwdrivers-precomp.h" // Precompiled headers
13 
14 #include <mrpt/utils/crc.h>
15 #include <mrpt/utils/CTicTac.h>
16 #include <mrpt/system/os.h>
17 #include <cstdio> // printf
18 
20 
22 
23 #define RET_ERROR(msg) { cout << "[" << __CURRENT_FUNCTION_NAME__ <<"] " << msg << endl; return false; }
24 
25 using namespace std;
26 using namespace mrpt;
27 using namespace mrpt::utils;
28 using namespace mrpt::obs;
29 using namespace mrpt::hwdrivers;
30 
31 char hexCharToInt(char n)
32 {
33  if (n >= '0' && n <= '9')
34  return (n-'0');
35  else
36 
37  if (n >= 'A' && n <= 'F')
38  return (n-'A'+10);
39  else
40  return 0;
41 }
42 
43 /*-------------------------------------------------------------
44  CCANBusReader
45 -------------------------------------------------------------*/
46 CCANBusReader::CCANBusReader() :
47  mrpt::utils::COutputLogger("CCANBusReader"),
48  m_com_port(),
49  m_mySerialPort( NULL ),
50  m_com_baudRate(57600),
51  m_nTries_connect(1),
52  m_nTries_current(0),
53  m_canbus_speed(250000),
54  m_canreader_timestamp(false),
55  m_CANBusChannel_isOpen(false)
56 {
57  m_sensorLabel = "CANBusReader";
59 }
60 
61 /*-------------------------------------------------------------
62  ~CCANBusReader
63 -------------------------------------------------------------*/
65 {
67  {
68  try
69  {
71  }
72  catch(...) {}
73  }
74 
75  if (m_mySerialPort)
76  {
77  delete m_mySerialPort;
78  m_mySerialPort = NULL;
79  }
80 }
81 
83 {
84  mrpt::obs::CObservationCANBusJ1939Ptr obs = mrpt::obs::CObservationCANBusJ1939::Create();
85  bool thereIsObservation;
86  bool hardwareError;
87 
88  doProcessSimple( thereIsObservation, *obs, hardwareError );
89  if( thereIsObservation )
90  appendObservation(obs);
91  else
92  cout << "No frame received" << endl;
93 }
94 
95 /*-------------------------------------------------------------
96  doProcess
97 -------------------------------------------------------------*/
99  bool &outThereIsObservation,
100  mrpt::obs::CObservationCANBusJ1939 &outObservation,
101  bool &hardwareError )
102 {
103  outThereIsObservation = false;
104  hardwareError = false;
105 
106  if ( !tryToOpenComms() )
107  {
108  hardwareError = true;
109  return;
110  }
111 
112  m_state = ssWorking;
113 
114  // Wait for a scan:
115  uint8_t out_prio,out_pdu_format,out_pdu_spec,out_src_address,out_data_length;
116  uint16_t out_pgn;
117  vector<uint8_t> out_data;
118  vector<char> out_raw_frame;
120  out_prio,
121  out_pdu_format,
122  out_pdu_spec,
123  out_src_address,
124  out_data_length,
125  out_pgn,
126  out_data,
127  out_raw_frame ))
128  return;
129 
130  // Yes, we have a new scan:
131  // cout << "we've got a frame" << endl;
132  // -----------------------------------------------
133  // Extract the observation:
134  // -----------------------------------------------
135  outObservation.timestamp = mrpt::system::now();
136  outObservation.sensorLabel = m_sensorLabel; // Set label
137 
138  // And the scan ranges:
139  outObservation.m_priority = out_prio;
140  outObservation.m_pdu_spec = out_pdu_spec;
141  outObservation.m_pdu_format = out_pdu_format;
142  outObservation.m_src_address = out_src_address;
143  outObservation.m_pgn = out_pgn;
144  outObservation.m_data_length = out_data_length;
145  outObservation.m_data.resize( out_data.size() );
146  for(uint8_t k = 0; k < out_data.size(); ++k)
147  outObservation.m_data[k] = out_data[k];
148  outObservation.m_raw_frame.resize( out_raw_frame.size() );
149  for(uint8_t k = 0; k < out_raw_frame.size(); ++k)
150  outObservation.m_raw_frame[k] = out_raw_frame[k];
151 
152  // we've got a new observation
153  outThereIsObservation = true;
154 }
155 
156 /*-------------------------------------------------------------
157  loadConfig_sensorSpecific
158 -------------------------------------------------------------*/
160  const mrpt::utils::CConfigFileBase &configSource,
161  const std::string &iniSection )
162 {
163 // m_sensorPose = CPose3D(
164 // configSource.read_float(iniSection,"pose_x",0),
165 // configSource.read_float(iniSection,"pose_y",0),
166 // configSource.read_float(iniSection,"pose_z",0),
167 // DEG2RAD( configSource.read_float(iniSection,"pose_yaw",0) ),
168 // DEG2RAD( configSource.read_float(iniSection,"pose_pitch",0) ),
169 // DEG2RAD( configSource.read_float(iniSection,"pose_roll",0) )
170 // ); // irrelevant
171 
172  m_canbus_speed = configSource.read_int(iniSection,"CANBusSpeed",m_canbus_speed);
173  m_canreader_timestamp = configSource.read_bool(iniSection,"useCANReaderTimestamp",m_canreader_timestamp);
174 
175 #ifdef MRPT_OS_WINDOWS
176  m_com_port = configSource.read_string(iniSection, "COM_port_WIN", m_com_port, true );
177 #else
178  m_com_port = configSource.read_string(iniSection, "COM_port_LIN", m_com_port, true );
179 #endif
180 
181  m_com_baudRate = configSource.read_int(iniSection, "COM_baudRate", m_com_baudRate );
182  m_nTries_connect = configSource.read_int(iniSection, "nTries_connect", m_nTries_connect );
183 }
184 
185 /*-------------------------------------------------------------
186  Tries to open the com port and setup
187  all the LMS protocol. Returns true if OK or already open.
188 -------------------------------------------------------------*/
190 {
191  if (err_msg) *err_msg="";
192  try
193  {
194  if (!m_mySerialPort)
195  {
196  // There is no COMMS port open yet...
197  if (!m_com_port.empty())
198  {
199 // cout << "Creating port" << endl;
200  m_mySerialPort = new CSerialPort(); // Create the port myself:
201  }
202  else
203  throw std::logic_error("ERROR: No serial port attached with bindIO, neither it set with 'setSerialPort'");
204  }
205 
206  // We assure now we have a stream... try to open it, if it's not done yet.
207  bool just_open = false;
208 // CSerialPort* COM = dynamic_cast<CSerialPort*>(m_stream);
209  if (m_mySerialPort!=NULL)
210  {
211  if (!m_mySerialPort->isOpen())
212  {
213  // Try to open it now:
215  m_mySerialPort->open(); // will raise an exception on error.
216 
217  // Set basic params:
218  m_mySerialPort->setConfig(9600);
219  m_mySerialPort->setTimeouts(100,0,10,0,50);
220 
221  just_open = true;
222  }
223  }
224 
225  // It seems the port was open and working so we are done here.
226  if (!just_open)
227  return true;
228 
229  // ==================================================================
230  // Otherwise, it was just opened now, we must send the
231  // ** initialization commands **
232  // and put the CAN Converter in recording mode:
233  // ==================================================================
234  cout << "Setting up serial comms in port " << m_com_port;
235  if (!setupSerialComms()) RET_ERROR("error");
236  cout << " ... done" << endl;
237 
238  // initialize
239  // set CAN Bus speed
240  /**/
241  bool res;
242  cout << "Setting up CAN BUS Speed at: " << m_canbus_speed << endl;
243  for (int nTry=0;nTry<250000/*4*/;nTry++)
244  if (true == (res=sendCANBusReaderSpeed()))
245  break;
246  if(!res) return false;
247  cout << " ... done" << endl;
248 
249  // open the CAN channel. If true, at this point, frames should be poping out the CAN Bus
250  cout << "Opening CAN BUS and starting to receive." << endl;
251  for (int nTry=0;nTry<250000/*4*/;nTry++)
252  if (true==(res=CANBusOpenChannel()))
253  break;
254  if(!res) return false;
255  cout << " ... done" << endl;
256 
257 // cout << "Autopoll" << endl;
258 // for (int nTry=0;nTry<250000/*4*/;nTry++)
259 // if (true==(res=CANBusAutoPoll()))
260 // break;
261 // if(!res) return false;
262 // cout << " ... done" << endl;
263 
264  return res;
265  /**/
266  }
267  catch(std::exception &e)
268  {
269  std::string s = "[CCANBusReader] Error trying to open CANBusReader at port ";
270  s+= e.what();
271  if (err_msg) *err_msg=s;
273  return false;
274  }
275 }
276 
277 /*-------------------------------------------------------------
278  Tries to send the command to set up the speed of the
279  CAN Bus reader -> tractor link
280 -------------------------------------------------------------*/
282 {
283  // command: S0 --> S8 according to the selected speed
284  unsigned char cmd[2];
285 
286  cmd[0] = 'S';
287  switch( m_canbus_speed )
288  {
289  case 10000: cmd[1] = '0'; break;
290  case 20000: cmd[1] = '1'; break;
291  case 50000: cmd[1] = '2'; break;
292  case 100000: cmd[1] = '3'; break;
293  case 125000: cmd[1] = '4'; break;
294  case 250000: cmd[1] = '5'; break;
295  case 500000: cmd[1] = '6'; break;
296  case 800000: cmd[1] = '7'; break;
297  case 1000000: cmd[1] = '8'; break;
298  default: RET_ERROR("Incorrect CAN Bus speed"); break;
299  }
300  sendCommandToCANReader(cmd,2);
301  return waitACK(50);
302 }
303 
305 {
306  unsigned char cmd[1];
307  cmd[0] = 'O';
308  sendCommandToCANReader(cmd,1);
310  return m_CANBusChannel_isOpen;
311 }
312 
314 {
315  unsigned char cmd[1];
316  cmd[0] = 'C';
317  sendCommandToCANReader(cmd,1);
318 // m_CANBusChannel_isOpen = !waitACK(50);
319  m_CANBusChannel_isOpen = false;
320 // return !m_CANBusChannel_isOpen;
321  return true;
322 }
323 
325 {
326  unsigned char cmd[1];
327  cmd[0] = 'A';
328  sendCommandToCANReader(cmd,1);
329  return waitACK(50);
330 }
331 
333 {
334  unsigned char cmd[2];
335  cmd[0] = 'X';
336  cmd[1] = '1';
337  sendCommandToCANReader(cmd,2);
338  return waitACK(50);
339 }
340 
342 {
343  unsigned char cmd[1];
344  cmd[0] = 'P';
345  sendCommandToCANReader(cmd,1);
346  return waitACK(50);
347 }
348 
349 /*-------------------------------------------------------------
350  waitContinuousSampleFrame
351 -------------------------------------------------------------*/
353  uint8_t &out_prio,
354  uint8_t &out_pdu_format,
355  uint8_t &out_pdu_spec,
356  uint8_t &out_src_address,
357  uint8_t &out_data_length,
358  uint16_t &out_pgn,
359  vector<uint8_t> &out_data,
360  vector<char> &out_raw_frame)
361 {
362 // CSerialPort* COM = dynamic_cast<CSerialPort*>(m_stream);
363 // ASSERTMSG_(COM!=NULL,"No I/O channel bound to this object");
364 
365  size_t nRead,nBytesToRead;
366  size_t nFrameBytes = 0;
367  size_t lengthField;
368  unsigned char buf[40];
369 
370  // clear buffer
371  for(uint8_t k = 0; k < 40; ++k) buf[k]=0;
372 
373  uint8_t dlc = 0;
374  while( nFrameBytes < (lengthField=(10U+dlc+1U) ) )
375  {
376 // cout << "trying to receive" << endl;
377  if(lengthField > 30)
378  {
379  cout << "#" << int(dlc) << " ";
380  nFrameBytes = 0; // No es cabecera de trama correcta
381  for(uint8_t k = 0; k < 40; ++k) buf[k]=0;
382  dlc = 0;
383  }
384 
385  if (nFrameBytes < 10)
386  nBytesToRead = 1;
387  else
388  {
389  dlc = 2*uint8_t(hexCharToInt(buf[9]));
390 // cout << "dlc: " << int(dlc) << endl;
391  nBytesToRead = (lengthField) - nFrameBytes;
392  }
393 
394  try
395  {
396  nRead = m_mySerialPort->Read( buf+nFrameBytes,nBytesToRead );
397 // cout << "to read: " << nBytesToRead << " received: " << nRead << " -> ";
398 // for( uint8_t k = 0; k < nRead; ++k )
399 // cout << int(buf[k+nFrameBytes]);
400 // cout << endl;
401  }
402  catch (std::exception &e)
403  {
404  // Disconnected?
405  MRPT_LOG_ERROR_STREAM( "[waitContinuousSampleFrame] Disconnecting due to comms error: " << e.what());
406  return false;
407  }
408 
409  if ( !nRead )
410  return false;
411 
412  if (nRead<nBytesToRead)
414 
415  // Reading OK:
416  // Was it the first one?
417  if (nFrameBytes>0 || (nFrameBytes == 0 && buf[0]==0x54 /*T*/))
418  nFrameBytes+=nRead;
419  else
420  {
421  nFrameBytes = 0; // No es cabecera de trama correcta
422  for(uint8_t k = 0; k < 40; ++k) buf[k]=0;
423  }
424  } // end while
425 
426  // Process frame
427  // convert ASCII text into integer
428  vector<uint8_t> aux;
429  out_raw_frame.resize(nFrameBytes);
430  for(uint8_t k = 0; k < nFrameBytes; ++k)
431  {
432  aux.push_back( hexCharToInt(buf[k]) );
433  out_raw_frame[k] = buf[k];
434  }
435 
436  out_prio = (aux[1] << 2) | (aux[2] >> 2);
437  out_pdu_format = (aux[3] << 4) | aux[4];
438  out_pdu_spec = (aux[5] << 4) | aux[6];
439  out_src_address = (aux[7] << 4) | aux[8];
440  out_data_length = aux[9];
441  out_pgn = uint16_t(out_pdu_format) << 8 | uint16_t(out_pdu_spec);
442  out_data.resize(out_data_length);
443  for(uint8_t k = 0, k2 = 0; k < 2*out_data_length; k+=2, k2++)
444  out_data[k2] = (aux[10+k] << 4) | aux[11+k];
445 
446  if( buf[nFrameBytes-1] != 0x0D )
447  {
448  cout << format("[CCANBusReader::waitContinuousSampleFrame] expected 0x0D ending flag, 0x%X found instead", buf[nFrameBytes]) << endl;
449  return false; // Bad ending flag
450  }
451 
452  // All OK
453  return true;
454 }
455 
456 /*-------------------------------------------------------------
457  initialize
458 -------------------------------------------------------------*/
460 {
461  string err_str;
463  if (!tryToOpenComms(&err_str))
464  {
465  cerr << err_str << endl;
466  throw std::logic_error(err_str);
467  }
468 }
469 
470 /*-----------------------------------------------------------------
471  Assures laser is connected and operating at 38400, in
472  its case returns true.
473  -----------------------------------------------------------------*/
475 {
476  ASSERT_(m_com_baudRate==9600 || m_com_baudRate==38400 || m_com_baudRate == 57600 || m_com_baudRate==500000);
477 
478 // CSerialPort* COM = dynamic_cast<CSerialPort*>(m_stream);
479  if (m_mySerialPort==NULL) return true;
480 
481  int detected_rate = 0;
482  for (size_t reps=0;!detected_rate && reps<m_nTries_connect;reps++)
483  {
484  m_nTries_current=reps;
485 
486  int rates[] = {0, 9600,38400,57600,500000};
487 
488  // Try first the desired rate to speed up the process, just in case
489  // the converter is already setup from a previous run:
490  rates[0] = m_com_baudRate;
491 
492  detected_rate = 0;
493 
494  for (size_t i=0;!detected_rate && i<sizeof(rates)/sizeof(rates[0]);i++)
495  {
496  // Are we already receiving at 500k?
497  // ------------------------------------------------
498  m_mySerialPort->setConfig( rates[i] );
499  mrpt::system::sleep(100);
501 
502  // close the connection
503  /**/
504  cout << endl << "Closing CAN Channel " << endl;
505  for (int nTry=0;nTry<250000/*4*/;nTry++)
506  if( true==CANBusCloseChannel() )
507  break;
508  cout << " ... done" << endl;
509  /**/
510 
511  mrpt::system::sleep(100);
513 
514  for (int nTry=0;nTry<250000/*4*/ && !detected_rate;nTry++)
515  {
517 
518  // Ask for the laser version at the current rate:
519  if( queryVersion(true) )
520  {
521  detected_rate = rates[i];
522  break;
523  }
525  } // for tries
526  // There is no link, or the baudrate is wrong...
527  }
528 
529  // Try again in a while:
530  if (!detected_rate && reps!=(m_nTries_connect-1))
531  mrpt::system::sleep(5000);
532  }
533 
534  // Switch "this" serial port to the detected baudrate
535  setBaudRate( detected_rate );
536 
539 
540  // Wait...
541  mrpt::system::sleep(500);
542 
543  // And check comms at the new baud rate:
544  return true;
545 }
546 
547 /*-----------------------------------------------------------------
548  Query to LMS a status query.
549  Returns true if response is read ok.
550  -----------------------------------------------------------------*/
551 bool CCANBusReader::queryVersion(bool printOutVersion)
552 {
554 
555  uint8_t cmd[1];
556  cmd[0] = 'V';
557  uint16_t cmd_len = 1;
558 
559  if (!sendCommandToCANReader(cmd,cmd_len,false)) return false;
560  return waitForVersion(500, printOutVersion);
561 }
562 
563 // Returns false if timeout
565 {
566 // CSerialPort* COM = dynamic_cast<CSerialPort*>(m_stream);
567 // ASSERT_(COM);
568 
569  uint8_t b = 0;
570  CTicTac tictac;
571  tictac.Tic();
572 
573  do
574  {
575  if ( m_mySerialPort->Read(&b,1) )
576  {
577  // Byte rx:
578  if(b==0x0D/*0x30*/)
579  {
580  cout << int(b) << endl;
581  return true; // [CR]
582  }
583 
584  }
585  } while(tictac.Tac()<timeout_ms*1e-3 );
586 
587  if (b==0x07) // [BELL]
588  RET_ERROR(format("ERROR received."))
589  else if (b!=0)
590  RET_ERROR(format("Unexpected code received: 0x%02X",b))
591  else
592  return false; //RET_ERROR("Timeout")
593 }
594 
595 bool CCANBusReader::waitForVersion(uint16_t timeout, bool printOutVersion )
596 {
597 // CSerialPort* COM = dynamic_cast<CSerialPort*>(m_stream);
598 // ASSERT_(COM);
599 
600  uint8_t b;
601  unsigned int nBytes=0;
602 
603  CTicTac tictac;
604  tictac.Tic();
605  const double maxTime = timeout*1e-3;
606 
607  while( nBytes<6 )
608  {
609  if ( m_mySerialPort->Read(&b,1) )
610  {
611 // cout << "received " << nBytes << " bytes: " << char(b) << endl;
612  // First byte must be STX:
613  if ( nBytes > 0 || (nBytes == 0 && b == 'V') )
614  {
615  // Store in frame:
616  m_received_frame_buffer[nBytes] = b;
617  nBytes++;
618  }
619  }
620  if (tictac.Tac()>=maxTime)
621  {
622  cout << "Version timeout" << endl;
623  return false; // Timeout
624  }
625  }
626 
627  // Check len:
628  if (m_received_frame_buffer[nBytes-1] != 0x0D )
629  {
630  printf("[CCANBusReader::waitForVersion] Error: expected 0x0D final byte, received %x\n", m_received_frame_buffer[nBytes-1]);
631  return false;
632  }
633 
634  if( printOutVersion )
635  {
636  cout << "Version: ";
637  for(uint8_t k = 0; k < nBytes; ++k)
638  cout << char(m_received_frame_buffer[k]);
639  cout << endl;
640  }
641  return true;
642 }
643 // Returns false if timeout
645 {
646 // CSerialPort* COM = dynamic_cast<CSerialPort*>(m_stream);
647 // ASSERT_(COM);
648 
649  uint8_t b;
650  unsigned int nBytes=0;
651 
652  CTicTac tictac;
653  tictac.Tic();
654  const double maxTime = timeout*1e-3;
655  uint8_t dlc = 0;
656  while( nBytes<10 || (nBytes<(10U+dlc+1U/*CR*/)) )
657  {
658  if(m_mySerialPort->Read(&b,1) )
659  {
660  // First byte must be STX:
661  if( nBytes>1 || (!nBytes && b==0x54 /*'T'*/) )
662  {
663  // Store in frame:
664  m_received_frame_buffer[nBytes] = b;
665  nBytes++;
666  }
667  if( nBytes == 10 )
668  dlc = 2*uint8_t(hexCharToInt(m_received_frame_buffer[9])); // here is the number of BYTES of data -> 2 hex values for byte
669  }
670  if (tictac.Tac()>=maxTime)
671  return false; // Timeout
672  }
673  // Check final flag
674  if (m_received_frame_buffer[10U+dlc] != 0x0D)
675  {
676  printf("[CCANBusReader::waitIncomingFrame] Error: expected 0x0D as final flag, received %x\n",m_received_frame_buffer[10U+dlc]);
677  return false;
678  }
679 
680 #if 0
681  printf("RX: ");
682  for (unsigned int i=0;i<nBytes;i++)
683  printf("%02X ",m_received_frame_buffer[i]);
684  printf("\n");
685 #endif
686 
687  // OK
688  return true;
689 }
690 
691 bool CCANBusReader::sendCommandToCANReader(const uint8_t *cmd,const uint16_t cmd_len, bool wait)
692 {
693  MRPT_UNUSED_PARAM(wait);
694  uint8_t cmd_full[1024];
695  ASSERT_(sizeof(cmd_full)>cmd_len);
696 
697 // CSerialPort* COM = dynamic_cast<CSerialPort*>(m_stream);
698 // ASSERT_(COM);
699 
700  // command is just plain text so no frame header nor CRC is needed
701  memcpy(cmd_full,cmd,cmd_len);
702  cmd_full[cmd_len] = 0x0D; // [CR] at the end
703 
704  const size_t toWrite = cmd_len+1;
705 
706 #if 1
707  printf("TX: ");
708  for (unsigned int i=0;i<toWrite;i++)
709  printf("%02X ",cmd_full[i]);
710  printf("\n");
711 #endif
712 
713 // const int NTRIES = 3;
714 
715 // for (int k=0;k<NTRIES;k++)
716 // {
717  if (toWrite!=m_mySerialPort->Write( cmd_full, toWrite ))
718  {
719  cout << "[CCANBusReader::SendCommandToCANReader] Error writing data to serial port." << endl;
720  return false;
721  }
722  return true;
723 // mrpt::system::sleep(15);
724 // if(wait)
725 // {
726 // if(waitACK(5000))
727 // return true;
728 // mrpt::system::sleep(10);
729 // }
730 // else
731 // return true; // perform special wait outside this method
732 // }
733 
734  return false;
735 }
736 
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
Definition: os.cpp:358
bool sendCANBusReaderSpeed()
Sends the specified speed to the CAN Converter.
bool read_bool(const std::string &section, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
bool isOpen() const
Returns if port has been correctly open.
unsigned __int16 uint16_t
Definition: rptypes.h:46
A communications serial port built as an implementation of a utils::CStream.
Definition: CSerialPort.h:43
bool waitACK(uint16_t timeout_ms)
void setTimeouts(int ReadIntervalTimeout, int ReadTotalTimeoutMultiplier, int ReadTotalTimeoutConstant, int WriteTotalTimeoutMultiplier, int WriteTotalTimeoutConstant)
Changes the timeouts of the port, in milliseconds.
char hexCharToInt(char n)
std::string m_sensorLabel
See CGenericSensor.
bool waitContinuousSampleFrame(uint8_t &out_prio, uint8_t &out_pdu_format, uint8_t &out_pdu_spec, uint8_t &out_src_address, uint8_t &out_data_length, uint16_t &out_pgn, std::vector< uint8_t > &out_data, std::vector< char > &out_raw_frame)
This class stores a message from a CAN BUS with the protocol J1939.
GLenum GLsizei n
Definition: glext.h:4618
void doProcessSimple(bool &outThereIsObservation, mrpt::obs::CObservationCANBusJ1939 &outObservation, bool &hardwareError)
Specific laser scanner "software drivers" must process here new data from the I/O stream...
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:70
Contains classes for various device interfaces.
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
CSerialPort * m_mySerialPort
Will be !=NULL only if I created it, so I must destroy it at the end.
Definition: CCANBusReader.h:87
bool waitIncomingFrame(uint16_t timeout)
size_t Write(const void *Buffer, size_t Count)
Implements the virtual method responsible for writing to the stream.
STL namespace.
void setBaudRate(int baud)
Changes the serial port baud rate (call prior to &#39;doProcess&#39;); valid values are 9600,38400 and 500000.
GLdouble s
Definition: glext.h:3602
void Tic()
Starts the stopwatch.
Definition: CTicTac.cpp:77
bool tryToOpenComms(std::string *err_msg=NULL)
Tries to open the com port and setup all the LMS protocol. Returns true if OK or already open...
This class allows loading and storing values and vectors of different types from a configuration text...
unsigned char uint8_t
Definition: rptypes.h:43
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
void doProcess()
This method will be invoked at a minimum rate of "process_rate" (Hz)
bool CANBusCloseChannel()
Closes the CAN Channel.
bool waitForVersion(uint16_t timeout, bool printOutVersion=false)
void loadConfig_sensorSpecific(const mrpt::utils::CConfigFileBase &configSource, const std::string &iniSection)
See the class documentation at the top for expected parameters.
#define RET_ERROR(msg)
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
virtual ~CCANBusReader()
Destructor.
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
Definition: threads.cpp:57
void setConfig(int baudRate, int parity=0, int bits=8, int nStopBits=1, bool enableFlowControl=false)
Changes the configuration of the port.
This class implements a high-performance stopwatch.
Definition: CTicTac.h:24
This "software driver" implements the communication protocol for interfacing a SICK LMS 2XX laser sca...
Definition: CCANBusReader.h:53
This namespace contains representation of robot actions and observations.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
GLubyte GLubyte b
Definition: glext.h:5575
uint8_t m_received_frame_buffer[2000]
Definition: CCANBusReader.h:84
GLsizei const GLchar ** string
Definition: glext.h:3919
#define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace)
This must be inserted in all CGenericSensor classes implementation files:
void initialize()
Set-up communication with the laser.
void setSerialPortName(const std::string &COM_name)
Sets the serial port to open (it is an error to try to change this while open yet).
Definition: CSerialPort.h:65
std::string sensorLabel
An arbitrary label that can be used to identify the sensor.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp. Where available, this should contain the accurate satellite-based time...
bool sendCommandToCANReader(const uint8_t *cmd, const uint16_t cmd_len, bool wait=true)
void purgeBuffers()
Purge tx and rx buffers.
void open()
Open the port.
Definition: CSerialPort.cpp:87
std::vector< char > m_raw_frame
The ASCII frame.
uint16_t m_pgn
The Parameter Group Number within this frame.
double Tac()
Stops the stopwatch.
Definition: CTicTac.cpp:92
#define ASSERT_(f)
void appendObservation(const mrpt::utils::CSerializablePtr &obj)
Like appendObservations() but for just one observation.
std::string m_com_port
If set to non-empty, the serial port will be attempted to be opened automatically when this class is ...
Definition: CCANBusReader.h:86
static CObservationCANBusJ1939Ptr Create()
int m_com_baudRate
Baudrate: 9600, 38400, 500000.
Definition: CCANBusReader.h:88
uint8_t m_src_address
The address of the source node within this frame.
GLuint res
Definition: glext.h:6298
#define MRPT_LOG_ERROR_STREAM(__CONTENTS)
std::vector< uint8_t > m_data
The data within this frame (0-8 bytes)
bool CANBusOpenChannel()
Opens the CAN Channel.
size_t Read(void *Buffer, size_t Count)
Implements the virtual method responsible for reading from the stream - Unlike CStream::ReadBuffer, this method will not raise an exception on zero bytes read, as long as there is not any fatal error in the communications.
bool queryVersion(bool printOutVersion=false)
unsigned int m_nTries_connect
Default = 1.
Definition: CCANBusReader.h:89



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019