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 
22 
24 
25 bool Parser::parseFrame(const std::string& frame)
26 {
27  char frameHeader = ' ';
28  std::istringstream frameStream(frame);
29  unsigned short int CRC16 = CRC_INIT;
30  int frameLength = strlen(frame.c_str()); // length method counts \0 :(
31 
32  frameStream >> frameHeader;
33  if(frameStream.fail() || frameHeader != 'X')
34  {
35  qDebug() << "Could not read frame identifier or is incorrect";
36  frameStream.clear();
37  return false;
38  }
39 
40  frameStream >> _leftSpeed >> _rightSpeed;
41  frameStream >> _leftAcceleration >> _rightAcceleration;
42  frameStream >> _xAxis >> _yAxis;
43  frameStream >> _battery;
44 
45  for(int i = 0; i < 200; ++i)
46  {
47  float angle = 0, distance = 0;
48  frameStream >> angle >> distance;
49  angle = (angle * 360.0) / 200.0; // stepper motor has 200 steps per revolution
50  _lidarData.insert_or_assign(angle, distance);
51  }
52 
53  frameStream >> std::hex >> _CRC16;
54 
55  const char* buffer = frame.c_str();
56  int length = frameLength - 4; // At the end there is CRC16
57 
58  while(length--)
59  {
60  CRC16 ^= *(unsigned const char*)buffer++ << 8;
61  for(int i = 0; i < 8; i++)
62  {
63  CRC16 = CRC16 & 0x8000 ? (CRC16 << 1) ^ CRC_POLYNOMIAL : CRC16 << 1;
64  }
65  }
66 
67  if(CRC16 != _CRC16)
68  {
69  qDebug() << "Calculated CRC16 different from read one";
70  return false;
71  }
72 
73  return true;
74 }
75 
77 {
78  QString value;
79 
80  value = QString::number(_leftSpeed);
81  emit changedLeftMotorSpeed(value);
82 
83  value = QString::number(_rightSpeed);
84  emit changedRightMotorSpeed(value);
85 
86  value = QString::number(calculateAcceleration(_leftAcceleration));
87  emit changedLeftMotorAcceleration(value);
88 
89  value = QString::number(calculateAcceleration(_rightAcceleration));
91 
92  value = QString::number(calculateSpeedLeft(_xAxis, _yAxis));
93  emit changedLeftMotorGSpeed(value);
94 
95  value = QString::number(calculateSpeedRight(_xAxis, _yAxis));
96  emit changedRightMotorGSpeed(value);
97 
98  emit changedJoystick(QPoint(_xAxis, _yAxis));
99 
100  emit changedBattery(_battery);
101 
102  emit changedLidar(_lidarData);
103 }
104 
105 float Parser::calculateSpeedRight(const int& xAxis, const int& yAxis)
106 {
108  {
109  const int speed = -30;
110  qDebug() << "Right motor - turn left during backward move.";
111  return speed;
112  }
113  else if(_yAxis > H_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
114  {
115  const int speed = -5;
116  qDebug() << "Right motor - turn right during backward move.";
117  return speed;
118  }
119  else if(_yAxis < L_JOY_LIMIT_Y && _xAxis < L_JOY_LIMIT_X)
120  {
121  const int speed = 30;
122  qDebug() << "Right motor - turn left during forward move.";
123  return speed;
124  }
125  else if(_yAxis < L_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
126  {
127  const int speed = 5;
128  qDebug() << "Right motor - turn right during forward move.";
129  return speed;
130  }
131  else if(_yAxis > L_JOY_LIMIT_Y && _yAxis < H_JOY_LIMIT_Y && _xAxis < H_JOY_LIMIT_X && _xAxis > L_JOY_LIMIT_X)
132  {
133  const int speed = 0;
134  qDebug() << "Right motor - Stop.";
135  return speed;
136  }
137  else if(_yAxis >= L_JOY_LIMIT_Y && _yAxis <= H_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
138  {
139  const int speed = 0.01398 * xAxis - 27.27;
140  qDebug() << "Right motor - turn right.";
141  return speed;
142  }
144  {
145  const int speed = -0.01554 * xAxis + 30.0;
146  qDebug() << "Right motor - turn left.";
147  return speed;
148  }
149  else if(_yAxis > H_JOY_LIMIT_Y)
150  {
151  const int speed = -(0.01474 * yAxis - 30.3685);
152  qDebug() << "Right motor - move backward.";
153  return speed;
154  }
155  else if(_yAxis < L_JOY_LIMIT_Y)
156  {
157  const int speed = -0.01554 * yAxis + 30.0;
158  qDebug() << "Right motor - move forward.";
159  return speed;
160  }
161  else
162  {
163  const int speed = xAxis + yAxis;
164  return speed;
165  }
166 }
167 
168 float Parser::calculateSpeedLeft(const int& xAxis, const int& yAxis)
169 {
171  {
172  const int speed = -5;
173  qDebug() << "Left motor - turn left during backward move.";
174  return speed;
175  }
176  else if(_yAxis > H_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
177  {
178  const int speed = -30;
179  qDebug() << "Left motor - turn right during backward move.";
180  return speed;
181  }
182  else if(_yAxis < L_JOY_LIMIT_Y && _xAxis < L_JOY_LIMIT_X)
183  {
184  const int speed = 5;
185  qDebug() << "Left motor - turn left during forward move.";
186  return speed;
187  }
188  else if(_yAxis < L_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
189  {
190  const int speed = 30;
191  qDebug() << "Left motor - turn right during forward move.";
192  return speed;
193  }
194  else if(_yAxis > L_JOY_LIMIT_Y && _yAxis < H_JOY_LIMIT_Y && _xAxis < H_JOY_LIMIT_X && _xAxis > L_JOY_LIMIT_X)
195  {
196  const int speed = 0;
197  qDebug() << "Left motor - Stop.";
198  return speed;
199  }
200  else if(_yAxis >= L_JOY_LIMIT_Y && _yAxis <= H_JOY_LIMIT_Y && _xAxis > H_JOY_LIMIT_X)
201  {
202  const int speed = -(0.01398 * xAxis - 27.27);
203  qDebug() << "Left motor - turn right.";
204  return speed;
205  }
207  {
208  const int speed = -(-0.01554 * xAxis + 30.0);
209  qDebug() << "Left motor - turn left.";
210  return speed;
211  }
212  else if(_yAxis > H_JOY_LIMIT_Y)
213  {
214  const int speed = -(0.01474 * yAxis - 30.3685);
215  qDebug() << "Left motor - move backward.";
216  return speed;
217  }
218  else if(_yAxis < L_JOY_LIMIT_Y)
219  {
220  const int speed = -0.01554 * yAxis + 30.0;
221  qDebug() << "Left motor - move forward.";
222  return speed;
223  }
224  else
225  {
226  const int speed = xAxis + yAxis;
227  return speed;
228  }
229 }
230 
231 float Parser::calculateAcceleration(const int& acceleration)
232 {
233  return acceleration;
234 }
int _xAxis
Definition: parser.hh:143
float calculateSpeedLeft(const int &xAxis, const int &yAxis)
Calculate speed value on left motor from joystick swing.
Definition: parser.cpp:168
float calculateSpeedRight(const int &xAxis, const int &yAxis)
Calculate speed value on right motor from joystick swing.
Definition: parser.cpp:105
float calculateAcceleration(const int &acceleration)
calculateAcceleration
Definition: parser.cpp:231
Parser()
Construct new Parser object.
Definition: parser.cpp:21
void changedRightMotorAcceleration(const QString &accelerationValue)
Signal emitted when parsed data of right motor acceleration is ready.
void changedLidar(const std::map< float, int > lidarData)
Signal emitted when parsed data of lidar is ready.
~Parser()
Destruct Parser object.
Definition: parser.cpp:23
int _leftAcceleration
Definition: parser.hh:140
void changedLeftMotorGSpeed(const QString &speedValue)
Signal emitted when parsed data of left motor given speed is ready.
void changedBattery(int battery)
Signal emitted when parsed data of battery voltage is ready.
void processData()
processData
Definition: parser.cpp:76
bool parseFrame(const std::string &frame)
Parse line from serial port into managed variables.
Definition: parser.cpp:25
void changedRightMotorSpeed(const QString &speedValue)
Signal emitted when parsed data of right motor speed is ready.
int _CRC16
Definition: parser.hh:150
void changedLeftMotorSpeed(const QString &speedValue)
Signal emitted when parsed data of left motor speed is ready.
int _battery
Definition: parser.hh:146
void changedJoystick(const QPoint &coordinates)
Signal emitted when parsed data of joystick swing is ready.
int _leftSpeed
Definition: parser.hh:137
int _rightSpeed
Definition: parser.hh:138
void changedRightMotorGSpeed(const QString &speedValue)
Signal emitted when parsed data of right motor given speed is ready.
int _rightAcceleration
Definition: parser.hh:141
void changedLeftMotorAcceleration(const QString &accelerationValue)
Signal emitted when parsed data of left motor acceleration is ready.
int _yAxis
Definition: parser.hh:144
std::map< float, int > _lidarData
Definition: parser.hh:148
#define L_JOY_LIMIT_X
Definition: parser.hh:20
#define CRC_POLYNOMIAL
Definition: parser.hh:26
#define H_JOY_LIMIT_Y
Definition: parser.hh:23
#define CRC_INIT
Definition: parser.hh:25
#define L_JOY_LIMIT_Y
Definition: parser.hh:22
#define H_JOY_LIMIT_X
Definition: parser.hh:21