Roomba App  1.0
parser.cpp
Go to the documentation of this file.
1 
12 #include <QByteArray>
13 #include <QDebug>
14 #include <QPoint>
15 
16 #include <sstream>
17 #include <string>
18 
19 #include "parser.hh"
20 
21 bool Parser::parseFrame(const std::string& frame)
22 {
23  char frameHeader = ' ';
24  std::istringstream frameStream(frame);
25  unsigned short int CRC16 = CRC_INIT;
26  int frameLength = strlen(frame.c_str()); // length method counts \0 :(
27 
28  frameStream >> frameHeader;
29  if(frameStream.fail() || frameHeader != 'X')
30  {
31  qDebug() << "Could not read frame identifier or is incorrect";
32  frameStream.clear();
33  return false;
34  }
35 
36  frameStream >> _leftSpeed >> _rightSpeed;
37  frameStream >> _leftAcceleration >> _rightAcceleration;
38  frameStream >> _xAxis >> _yAxis;
39  frameStream >> _battery;
40 
41  for(int i = 0; i < 200; ++i)
42  {
43  float angle = 0, distance = 0;
44  frameStream >> angle >> distance;
45  angle = (angle * 360.0) / 200.0; // stepper motor has 200 steps per revolution
46  _lidarData.insert_or_assign(angle, distance);
47  }
48 
49  frameStream >> std::hex >> _CRC16;
50 
51  const char* buffer = frame.c_str();
52  int length = frameLength - 4; // At the end there is CRC16
53 
54  while(length--)
55  {
56  CRC16 ^= *(unsigned const char*)buffer++ << 8;
57  for(int i = 0; i < 8; i++)
58  {
59  CRC16 = CRC16 & 0x8000 ? (CRC16 << 1) ^ CRC_POLYNOMIAL : CRC16 << 1;
60  }
61  }
62 
63  if(CRC16 != _CRC16)
64  {
65  qDebug() << "Calculated CRC16 different from read one";
66  return false;
67  }
68 
69  return true;
70 }
71 
73 {
74  QString value;
75 
76  value = QString::number(_leftSpeed);
77  emit changedLeftMotorSpeed(value);
78 
79  value = QString::number(_rightSpeed);
80  emit changedRightMotorSpeed(value);
81 
82  value = QString::number(_leftAcceleration);
83  emit changedLeftMotorAcceleration(value);
84 
85  value = QString::number(_rightAcceleration);
87 
88  value = QString::number(calculateSpeedLeft(_xAxis, _yAxis));
89  if(value.toFloat() > 0)
90  {
91  emit leftMotorDirection(1);
92  }
93  else if(value.toFloat() < 0)
94  {
95  emit leftMotorDirection(-1);
96  }
97  else
98  {
99  emit leftMotorDirection(0);
100  }
101  emit changedLeftMotorGSpeed(value);
102 
103  value = QString::number(calculateSpeedRight(_xAxis, _yAxis));
104  if(value.toFloat() > 0)
105  {
106  emit rightMotorDirection(1);
107  }
108  else if(value.toFloat() < 0)
109  {
110  emit rightMotorDirection(-1);
111  }
112  else
113  {
114  emit rightMotorDirection(0);
115  }
116  emit changedRightMotorGSpeed(value);
117 
118  emit changedJoystick(QPoint(_xAxis, _yAxis));
119 
120  emit changedBattery(_battery);
121 
122  emit changedLidar(_lidarData);
123 
124  for(auto&& [angle, distance] : _lidarData)
125  {
126  if(distance < 300)
127  {
128  emit setObstacleCloseStatus();
129  return;
130  }
131  }
132 
134 }
135 
136 float Parser::calculateSpeedRight(const int& xAxis, const int& yAxis)
137 {
139  {
140  const int speed = -30;
141  return speed;
142  }
143  else if(_yAxis > H_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
144  {
145  const int speed = -5;
146  return speed;
147  }
148  else if(_yAxis < L_JOY_LIMIT_Y && _xAxis < L_JOY_LIMIT_X)
149  {
150  const int speed = 30;
151  return speed;
152  }
153  else if(_yAxis < L_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
154  {
155  const int speed = 5;
156  return speed;
157  }
158  else if(_yAxis > L_JOY_LIMIT_Y && _yAxis < H_JOY_LIMIT_Y && _xAxis < H_JOY_LIMIT_X && _xAxis > L_JOY_LIMIT_X)
159  {
160  const int speed = 0;
161  return speed;
162  }
163  else if(_yAxis >= L_JOY_LIMIT_Y && _yAxis <= H_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
164  {
165  const int speed = 0.01398 * xAxis - 27.27;
166  return speed;
167  }
169  {
170  const int speed = -0.01554 * xAxis + 30.0;
171  return speed;
172  }
173  else if(_yAxis > H_JOY_LIMIT_Y)
174  {
175  const int speed = -(0.01474 * yAxis - 30.3685);
176  return speed;
177  }
178  else if(_yAxis < L_JOY_LIMIT_Y)
179  {
180  const int speed = -0.01554 * yAxis + 30.0;
181  return speed;
182  }
183  else
184  {
185  const int speed = xAxis + yAxis;
186  return speed;
187  }
188 }
189 
190 float Parser::calculateSpeedLeft(const int& xAxis, const int& yAxis)
191 {
193  {
194  const int speed = -5;
195  return speed;
196  }
197  else if(_yAxis > H_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
198  {
199  const int speed = -30;
200  return speed;
201  }
202  else if(_yAxis < L_JOY_LIMIT_Y && _xAxis < L_JOY_LIMIT_X)
203  {
204  const int speed = 5;
205  return speed;
206  }
207  else if(_yAxis < L_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
208  {
209  const int speed = 30;
210  return speed;
211  }
212  else if(_yAxis > L_JOY_LIMIT_Y && _yAxis < H_JOY_LIMIT_Y && _xAxis < H_JOY_LIMIT_X && _xAxis > L_JOY_LIMIT_X)
213  {
214  const int speed = 0;
215  return speed;
216  }
217  else if(_yAxis >= L_JOY_LIMIT_Y && _yAxis <= H_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
218  {
219  const int speed = -(0.01398 * xAxis - 27.27);
220  return speed;
221  }
223  {
224  const int speed = -(-0.01554 * xAxis + 30.0);
225  return speed;
226  }
227  else if(_yAxis > H_JOY_LIMIT_Y)
228  {
229  const int speed = -(0.01474 * yAxis - 30.3685);
230  return speed;
231  }
232  else if(_yAxis < L_JOY_LIMIT_Y)
233  {
234  const int speed = -0.01554 * yAxis + 30.0;
235  return speed;
236  }
237  else
238  {
239  const int speed = xAxis + yAxis;
240  return speed;
241  }
242 }
int _xAxis
The joystick swing on the x axis.
Definition: parser.hh:153
float calculateSpeedLeft(const int &xAxis, const int &yAxis)
Calculate speed value on the left motor from the joystick swing.
Definition: parser.cpp:190
float calculateSpeedRight(const int &xAxis, const int &yAxis)
Calculate speed value on the right motor from the joystick swing.
Definition: parser.cpp:136
void changedRightMotorAcceleration(const QString &accelerationValue)
A signal emitted when parsed data of the right motor acceleration is ready.
void setObstacleCloseStatus()
Set the Obstacle Close Status.
void changedLidar(const std::map< float, int > lidarData)
A signal emitted when parsed data of the lidar is ready.
void leftMotorDirection(const int &direction)
A signal emitted to pass wheel rotating direction to the openGL widget.
int _leftAcceleration
The left motor acceleration.
Definition: parser.hh:151
void changedLeftMotorGSpeed(const QString &speedValue)
A signal emitted when parsed data of the left motor given speed is ready.
void changedBattery(int battery)
A signal emitted when parsed data of the battery voltage is ready.
void processData()
Process data from a one frame.
Definition: parser.cpp:72
bool parseFrame(const std::string &frame)
Parse line from the serial port into managed variables.
Definition: parser.cpp:21
void changedRightMotorSpeed(const QString &speedValue)
A signal emitted when parsed data of the right motor speed is ready.
int _CRC16
Sent CRC from the device.
Definition: parser.hh:157
void changedLeftMotorSpeed(const QString &speedValue)
A signal emitted when parsed data of the left motor speed is ready.
int _battery
The battery voltage.
Definition: parser.hh:155
void changedJoystick(const QPoint &coordinates)
A signal emitted when parsed data of a joystick swing is ready.
int _leftSpeed
The left motor speed.
Definition: parser.hh:149
int _rightSpeed
The right motor speed.
Definition: parser.hh:150
void changedRightMotorGSpeed(const QString &speedValue)
A signal emitted when parsed data of the right motor given speed is ready.
void resetObstacleCloseStatus()
Reset the Obstacle Close Status.
int _rightAcceleration
The right motor acceleration.
Definition: parser.hh:152
void rightMotorDirection(const int &direction)
A signal emitted to pass wheel rotating direction to the openGL widget.
void changedLeftMotorAcceleration(const QString &accelerationValue)
A signal emitted when parsed data of the left motor acceleration is ready.
int _yAxis
The joystick swing on the y axis.
Definition: parser.hh:154
std::map< float, int > _lidarData
Data from a laser sensor.
Definition: parser.hh:156
Parser class declaration.
#define L_JOY_LIMIT_X
Joystick low limit in the x axis.
Definition: parser.hh:17
#define CRC_POLYNOMIAL
Definition: parser.hh:23
#define H_JOY_LIMIT_Y
Joystick high limit in the y axis.
Definition: parser.hh:20
#define CRC_INIT
Definition: parser.hh:22
#define L_JOY_LIMIT_Y
Joystick low limit in the y axis.
Definition: parser.hh:19
#define H_JOY_LIMIT_X
Joystick high limit in the x axis.
Definition: parser.hh:18