snark
thin_reader.h
1 // This file is part of snark, a generic and flexible library
2 // for robotics research.
3 //
4 // Copyright (C) 2011 The University of Sydney
5 //
6 // snark is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // snark is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with snark. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef SNARK_SENSORS_VELODYNE_THIN_READER_H_
20 #define SNARK_SENSORS_VELODYNE_THIN_READER_H_
21 
22 #ifndef WIN32
23 #include <stdlib.h>
24 #endif
25 #include <snark/sensors/velodyne/thin/thin.h>
26 #include <snark/timing/time.h>
27 
28 namespace snark {
29 
31 class thin_reader : public boost::noncopyable
32 {
33  public:
34  const char* read()
35  {
36  if( !std::cin.good() || std::cin.eof() ) { return NULL; }
37  comma::uint16 size;
38  std::cin.read( reinterpret_cast< char* >( &size ), 2 );
39  if( std::cin.gcount() < 2 ) { return NULL; }
40  std::cin.read( m_buf, size );
41  if( std::cin.gcount() < size ) { return NULL; }
42  comma::int64 seconds;
43  comma::int32 nanoseconds;
44  ::memcpy( &seconds, m_buf, sizeof( comma::int64 ) );
45  ::memcpy( &nanoseconds, m_buf + sizeof( comma::int64 ), sizeof( comma::int32 ) );
46  m_timestamp = boost::posix_time::ptime( snark::timing::epoch, boost::posix_time::seconds( static_cast< long >( seconds ) ) + boost::posix_time::microseconds( nanoseconds / 1000 ) );
47  velodyne::thin::deserialize( m_packet, m_buf + timeSize );
48  return reinterpret_cast< char* >( &m_packet );
49  }
50 
51  void close() {}
52 
53  boost::posix_time::ptime timestamp() const { return m_timestamp; }
54 
55  private:
56  enum { timeSize = 12 };
57  char m_buf[ velodyne::thin::maxBufferSize + timeSize ];
58  velodyne::packet m_packet;
59  boost::posix_time::ptime m_timestamp;
60 };
61 
62 }
63 
64 #endif // SNARK_SENSORS_VELODYNE_THIN_READER_H_