lds_polar_graph.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #include "lds_polar_graph.h"
31 
32 QT_CHARTS_USE_NAMESPACE
33 
34 LdsPolarGraph::LdsPolarGraph(QWidget *parent) :
35  QChartView(parent),
36  angular_min_(0),
37  angular_max_(360),
38  radial_min_(0),
39  radial_max(4000),
40  chart_(new QPolarChart),
41  series_(new QScatterSeries),
42  baud_rate_(230400),
43  port_("/dev/ttyUSB0"),
44  shutting_down_(false),
45  serial_(io_, port_)
46 {
47  port_ = "/dev/ttyUSB0"; // input your portname, ex) Linux: "/dev/ttyUSB0", Windows: "COM1", Mac: "/dev/tty.SLAB_USBtoUART" or "/dev/tty.usbserial-*"
48 
49  serial_.set_option(boost::asio::serial_port_base::baud_rate(baud_rate_));
50  boost::asio::write(serial_, boost::asio::buffer("b", 1));
51 
52  connect(&timer_, SIGNAL(timeout()), this, SLOT(loadData()));
53  timer_.start(0); // msec
54 
55  series_->setPen(QColor(Qt::transparent));
56  series_->setPen(QColor(255,0,0));
57  series_->setMarkerSize(2);
58 
59  chart_->legend()->setVisible(false);
60  chart_->addSeries(series_);
61 
62  QValueAxis *angularAxis = new QValueAxis();
63  angularAxis->setTickCount(9);
64  angularAxis->setLabelFormat("%.1f");
65  angularAxis->setShadesVisible(true);
66  angularAxis->setShadesBrush(QBrush(QColor(249, 249, 255)));
67  chart_->addAxis(angularAxis, QPolarChart::PolarOrientationAngular);
68 
69  QValueAxis *radialAxis = new QValueAxis();
70  radialAxis->setTickCount(9);
71  radialAxis->setLabelFormat("%d");
72  chart_->addAxis(radialAxis, QPolarChart::PolarOrientationRadial);
73 
74  series_->attachAxis(radialAxis);
75  series_->attachAxis(angularAxis);
76 
77  radialAxis->setRange(radial_min_, radial_max);
78  angularAxis->setRange(angular_min_, angular_max_);
79 
80  this->setChart(chart_);
81  this->setRenderHint(QPainter::Antialiasing);
82 }
83 
85 {
86  // Stop motor of LDS
87  boost::asio::write(serial_, boost::asio::buffer("e", 1));
88 }
89 
91 {
92  uint16_t *laser_sensor_data;
93  laser_sensor_data = poll();
94 
95  series_->clear();
96 
97  for (int i = angular_min_; i < angular_max_; i++)
98  {
99  series_->append(i, laser_sensor_data[i]);
100  }
101 
102  this->setChart(chart_);
103 }
104 
106 {
107  bool got_scan = false;
108  int index = 0;
109  uint8_t start_count = 0;
110  uint32_t motor_speed = 0;
111  uint16_t rpms = 0;
112  static uint16_t range_data[360] = {0, };
113  static uint16_t intensity_data[360] = {0, };
114  boost::array<uint8_t, 2520> raw_bytes;
115 
116  while (!shutting_down_ && !got_scan)
117  {
118  // Wait until first data sync of frame: 0xFA, 0xA0
119  boost::asio::read(serial_, boost::asio::buffer(&raw_bytes[start_count],1));
120 
121  if(start_count == 0)
122  {
123  if(raw_bytes[start_count] == 0xFA)
124  {
125  start_count = 1;
126  }
127  }
128  else if(start_count == 1)
129  {
130  if(raw_bytes[start_count] == 0xA0)
131  {
132  start_count = 0;
133 
134  // Now that entire start sequence has been found, read in the rest of the message
135  got_scan = true;
136 
137  boost::asio::read(serial_,boost::asio::buffer(&raw_bytes[2], 2518));
138 
139  // Read data in sets of 6
140  for(uint16_t i = 0; i < raw_bytes.size(); i=i+42)
141  {
142  if(raw_bytes[i] == 0xFA && raw_bytes[i+1] == (0xA0 + i / 42)) //&& CRC check
143  {
144  motor_speed += (raw_bytes[i+3] << 8) + raw_bytes[i+2]; //accumulate count for avg. time increment
145  rpms=(raw_bytes[i+3]<<8|raw_bytes[i+2])/10;
146 
147  for(uint16_t j = i+4; j < i+40; j=j+6)
148  {
149  index = 6*(i/42) + (j-4-i)/6;
150 
151  // Four bytes per reading
152  uint8_t byte0 = raw_bytes[j];
153  uint8_t byte1 = raw_bytes[j+1];
154  uint8_t byte2 = raw_bytes[j+2];
155  uint8_t byte3 = raw_bytes[j+3];
156 
157  // range and intensity data
158  uint16_t intensity = (byte1 << 8) + byte0;
159  intensity_data[359-index] = intensity;
160  uint16_t range = (byte3 << 8) + byte2;
161  range_data[359-index] = range;
162 
163  //printf ("i[%d]=%d,",359-index, intensity);
164  //printf ("r[%d]=%d,",359-index, range);
165  }
166  }
167  }
168  }
169  else
170  {
171  start_count = 0;
172  }
173  }
174  }
175  return range_data;
176 }
bool shutting_down_
Flag for whether the driver is supposed to be shutting down or not.
QScatterSeries * series_
uint16_t * poll()
LdsPolarGraph(QWidget *parent=0)
QPolarChart * chart_
boost::asio::serial_port serial_
Actual serial port object for reading/writing to the LFCD Laser Scanner.
std::string port_
The serial port the driver is attached to.
uint32_t baud_rate_
The baud rate for the serial connection.


hls-lfcd-lds-driver
Author(s): Pyo , Darby Lim , Gilbert , Will Son , JH Yang, SP Kong
autogenerated on Fri Apr 16 2021 02:20:09