-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallwheelform.cpp
322 lines (275 loc) · 12.6 KB
/
allwheelform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "allwheelform.h"
#include "ui_allwheelform.h"
#include <QCloseEvent>
#include <QDebug>
#include <QtCore>
#include "Graphics.h"
#include "Common.h"
#define SCALE 150
#define LINK_THICKNESS 0.03*SCALE
AllWheelForm::AllWheelForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::AllWheelForm)
{
this->geometry = new RobotGeometry();
this->steeringMotors = new AllWheelState();
this->drivingMotors = new AllWheelState();
this->graphicItemsCreated = false;
this->leftFrontWheel = NULL;
this->rightFrontWheel = NULL;
this->leftRearWheel = NULL;
this->rightRearWheel = NULL;
this->closeICRItem = NULL;
this->farICRItem = NULL;
this->baseLink = NULL;
this->leftFrontLink = NULL;
this->leftRearLink = NULL;
this->rightFrontLink = NULL;
this->rightReartLink = NULL;
ui->setupUi(this);
this->robotSketchScene = new QGraphicsScene(this);
ui->graphicsView->setScene(this->robotSketchScene);
this->ICR.setX(0);
this->ICR.setY(0);
this->stateTableModel = new QStandardItemModel(0, 2, this); //0 Rows and 2 Columns
this->stateTableModel->setHorizontalHeaderItem(0, new QStandardItem(QString("Param")));
this->stateTableModel->setHorizontalHeaderItem(1, new QStandardItem(QString("Value")));
ui->stateTableView->setModel(this->stateTableModel);
this->createGrphicItems();
}
AllWheelForm::~AllWheelForm()
{
qDeleteAll(this->robotSketchScene->items());
this->robotSketchScene->items().clear();
delete ui;
delete this->robotSketchScene;
delete this->steeringMotors;
delete this->drivingMotors;
delete this->geometry;
delete this->stateTableModel;
}
void AllWheelForm::setRow(int &rowNumber, const QString &label, const QString &value)
{
QStandardItem *item = new QStandardItem(label);
item->setToolTip(label);
this->stateTableModel->setItem(rowNumber, 0, item);
item = new QStandardItem(value);
item->setToolTip(value);
this->stateTableModel->setItem(rowNumber++, 1, item);
}
void AllWheelForm::createGrphicItems()
{
if (!this->graphicItemsCreated && this->geometry->jointPositionsAcquired) {
qreal leftTopX = -this->geometry->leftFrontJoint.y()*SCALE;
qreal leftTopY = -this->geometry->leftFrontJoint.x()*SCALE;
qreal rightTopX = -this->geometry->rightFrontJoint.y()*SCALE;
qreal rightTopY = -this->geometry->rightFrontJoint.x()*SCALE;
qreal leftBottomX = -this->geometry->leftRearJoint.y()*SCALE;
qreal leftBottomY = -this->geometry->leftRearJoint.x()*SCALE;
qreal rightBottomX = -this->geometry->rightRearJoint.y()*SCALE;
qreal rightBottomY = -this->geometry->rightRearJoint.x()*SCALE;
qreal wheelOffset = this->geometry->wheelOffset*SCALE;
qreal wheelRadius = this->geometry->wheelRadius*SCALE;
qreal wheelWidth = this->geometry->wheelWidth*SCALE;
this->baseLink = new QGraphicsRectItem(leftTopX, leftTopY, rightTopX-leftTopX, rightBottomY-rightTopY);
this->baseLink->setBrush(BRUSH_CLEAR);
this->baseLink->setPen(PEN_BLACK_BOLD);
this->robotSketchScene->addItem(this->baseLink);
this->closeICRItem = new QGraphicsItemGroup();
QGraphicsLineItem *item = new QGraphicsLineItem(0, -10, 0, 10);
item->setPen(PEN_RED_BOLD);
this->closeICRItem->addToGroup(item);
item = new QGraphicsLineItem(-10, 0, 10, 0);
item->setPen(PEN_RED_BOLD);
this->closeICRItem->addToGroup(item);
this->robotSketchScene->addItem(this->closeICRItem);
this->closeICRItem->setVisible(false);
this->farICRItem = new QGraphicsItemGroup();
item = new QGraphicsLineItem(0, 0, 10, 0);
item->setPen(PEN_RED_BOLD);
this->farICRItem->addToGroup(item);
item = new QGraphicsLineItem(0, 0, 5, 5);
item->setPen(PEN_RED_BOLD);
this->farICRItem->addToGroup(item);
item = new QGraphicsLineItem(0, 0, 5, -5);
item->setPen(PEN_RED_BOLD);
this->farICRItem->addToGroup(item);
this->robotSketchScene->addItem(this->farICRItem);
this->farICRItem->setVisible(false);
this->leftFrontWheel = new QGraphicsRectItem(-wheelOffset-wheelWidth/2, -wheelRadius, wheelWidth, wheelRadius*2);
this->rightFrontWheel = new QGraphicsRectItem(wheelOffset-wheelWidth/2, -wheelRadius, wheelWidth, wheelRadius*2);
this->leftRearWheel = new QGraphicsRectItem(-wheelOffset-wheelWidth/2, -wheelRadius, wheelWidth, wheelRadius*2);
this->rightRearWheel = new QGraphicsRectItem(wheelOffset-wheelWidth/2, -wheelRadius, wheelWidth, wheelRadius*2);
this->leftFrontWheel->setBrush(BRUSH_BLACK);
this->leftFrontWheel->setPen(PEN_BLACK);
this->rightFrontWheel->setBrush(BRUSH_BLACK);
this->rightFrontWheel->setPen(PEN_BLACK);
this->leftRearWheel->setBrush(BRUSH_BLACK);
this->leftRearWheel->setPen(PEN_BLACK);
this->rightRearWheel->setBrush(BRUSH_BLACK);
this->rightRearWheel->setPen(PEN_BLACK);
this->robotSketchScene->addItem(this->leftFrontWheel);
this->robotSketchScene->addItem(this->rightFrontWheel);
this->robotSketchScene->addItem(this->leftRearWheel);
this->robotSketchScene->addItem(this->rightRearWheel);
this->leftFrontWheel->setPos(leftTopX, leftTopY);
this->rightFrontWheel->setPos(rightTopX, rightTopY);
this->leftRearWheel->setPos(leftBottomX, leftBottomY);
this->rightRearWheel->setPos(rightBottomX, rightBottomY);
this->leftFrontLink = new QGraphicsRectItem(-wheelOffset+wheelWidth/2, -LINK_THICKNESS/2, wheelOffset-wheelWidth/2, LINK_THICKNESS);
this->leftRearLink = new QGraphicsRectItem(-wheelOffset+wheelWidth/2, -LINK_THICKNESS/2, wheelOffset-wheelWidth/2, LINK_THICKNESS);
this->rightFrontLink = new QGraphicsRectItem(0, -LINK_THICKNESS/2, wheelOffset-wheelWidth/2, LINK_THICKNESS);
this->rightReartLink = new QGraphicsRectItem(0, -LINK_THICKNESS/2, wheelOffset-wheelWidth/2, LINK_THICKNESS);
this->leftFrontLink->setBrush(BRUSH_CLEAR);
this->leftRearLink->setBrush(BRUSH_CLEAR);
this->rightFrontLink->setBrush(BRUSH_CLEAR);
this->rightReartLink->setBrush(BRUSH_CLEAR);
this->leftFrontLink->setPen(PEN_GREEN_BOLD);
this->leftRearLink->setPen(PEN_GREEN_BOLD);
this->rightFrontLink->setPen(PEN_GREEN_BOLD);
this->rightReartLink->setPen(PEN_GREEN_BOLD);
this->robotSketchScene->addItem(this->leftFrontLink);
this->robotSketchScene->addItem(this->rightFrontLink);
this->robotSketchScene->addItem(this->leftRearLink);
this->robotSketchScene->addItem(this->rightReartLink);
this->leftFrontLink->setPos(leftTopX, leftTopY);
this->rightFrontLink->setPos(rightTopX, rightTopY);
this->leftRearLink->setPos(leftBottomX, leftBottomY);
this->rightReartLink->setPos(rightBottomX, rightBottomY);
this->graphicItemsCreated = true;
}
}
void AllWheelForm::redrawSketch()
{
if (!this->graphicItemsCreated) {
this->createGrphicItems();
}
else {
this->leftFrontWheel->setRotation(-this->steeringMotors->leftFront*180.0/M_PI);
this->rightFrontWheel->setRotation(-this->steeringMotors->rightFront*180.0/M_PI);
this->leftRearWheel->setRotation(-this->steeringMotors->leftRear*180.0/M_PI);
this->rightRearWheel->setRotation(-this->steeringMotors->rightRear*180.0/M_PI);
this->leftFrontLink->setRotation(-this->steeringMotors->leftFront*180.0/M_PI);
this->rightFrontLink->setRotation(-this->steeringMotors->rightFront*180.0/M_PI);
this->leftRearLink->setRotation(-this->steeringMotors->leftRear*180.0/M_PI);
this->rightReartLink->setRotation(-this->steeringMotors->rightRear*180.0/M_PI);
qreal y = this->ICR.x()*-SCALE;
qreal x = this->ICR.y()*-SCALE;
qreal w = ui->graphicsView->width();
if (x > w/2) {
this->closeICRItem->setVisible(false);
this->farICRItem->setVisible(true);
this->farICRItem->setRotation(180);
this->farICRItem->setPos(w/2-5, 0);
}
else if (x < -w/2) {
this->closeICRItem->setVisible(false);
this->farICRItem->setVisible(true);
this->farICRItem->setRotation(0);
this->farICRItem->setPos(-w/2+5, 0);
}
else {
this->closeICRItem->setVisible(true);
this->farICRItem->setVisible(false);
this->closeICRItem->setPos(x, y);
}
}
}
void AllWheelForm::on_forwardButton_clicked()
{
emit predefinedControlSelected(lunabotics::proto::AllWheelControl::DRIVE_FORWARD);
}
void AllWheelForm::on_backwardButton_clicked()
{
emit predefinedControlSelected(lunabotics::proto::AllWheelControl::DRIVE_BACKWARD);
}
void AllWheelForm::on_leftButton_clicked()
{
emit predefinedControlSelected(lunabotics::proto::AllWheelControl::CRAB_LEFT);
}
void AllWheelForm::on_rightButton_clicked()
{
emit predefinedControlSelected(lunabotics::proto::AllWheelControl::CRAB_RIGHT);
}
void AllWheelForm::on_turnLeftButton_clicked()
{
emit predefinedControlSelected(lunabotics::proto::AllWheelControl::TURN_CCW);
}
void AllWheelForm::on_turnRightButton_clicked()
{
emit predefinedControlSelected(lunabotics::proto::AllWheelControl::TURN_CW);
}
void AllWheelForm::on_stopButton_clicked()
{
emit predefinedControlSelected(lunabotics::proto::AllWheelControl::STOP);
}
void AllWheelForm::closeEvent(QCloseEvent *event)
{
emit closing();
event->accept();
}
void AllWheelForm::allWheelStateUpdated(AllWheelState *steering, AllWheelState *driving)
{
this->steeringMotors->setState(steering);
this->drivingMotors->setState(driving);
this->redrawSketch();
this->updateTable();
}
void AllWheelForm::updateTable()
{
this->stateTableModel->clear();
int row = 0;
this->setRow(row, "driving.left.front", QString("%1 Rad/s").arg(QString::number(round(this->drivingMotors->leftFront, 2), 'f', 2)));
this->setRow(row, "driving.right.front", QString("%1 Rad/s").arg(QString::number(round(this->drivingMotors->rightFront, 2), 'f', 2)));
this->setRow(row, "driving.left.rear", QString("%1 Rad/s").arg(QString::number(round(this->drivingMotors->leftRear, 2), 'f', 2)));
this->setRow(row, "driving.right.rear", QString("%1 Rad/s").arg(QString::number(round(this->drivingMotors->rightRear, 2), 'f', 2)));
this->setRow(row, "steering.left.front", QString("%1 Rad").arg(QString::number(round(this->steeringMotors->leftFront, 2), 'f', 2)));
this->setRow(row, "steering.right.front", QString("%1 Rad").arg(QString::number(round(this->steeringMotors->rightFront, 2), 'f', 2)));
this->setRow(row, "steering.left.rear", QString("%1 Rad").arg(QString::number(round(this->steeringMotors->leftRear, 2), 'f', 2)));
this->setRow(row, "steering.right.rear", QString("%1 Rad").arg(QString::number(round(this->steeringMotors->rightRear, 2), 'f', 2)));
this->setRow(row, "ICR.x", QString("%1 m").arg(QString::number(this->ICR.x(), 'f', 2)));
this->setRow(row, "ICR.y", QString("%1 m").arg(QString::number(this->ICR.y(), 'f', 2)));
}
void AllWheelForm::ICRUpdated(QPointF ICR)
{
this->ICR = ICR;
this->updateTable();
this->redrawSketch();
}
void AllWheelForm::on_sendICRButton_clicked()
{
emit ICRControlSelected(QPointF(ui->ICRXEdit->text().toFloat(), ui->ICRYEdit->text().toFloat()), ui->velocityEdit->text().toFloat());
}
void AllWheelForm::updateJoints(RobotGeometry *geometry)
{
this->geometry->setGeometry(geometry);
this->redrawSketch();
}
void AllWheelForm::on_resetButton_clicked()
{
ui->lfDrivingEdit->setText("0");
ui->rfDrivingEdit->setText("0");
ui->lrDrivingEdit->setText("0");
ui->rrDrivingEdit->setText("0");
ui->lfSteeringEdit->setText("0");
ui->rfSteeringEdit->setText("0");
ui->lrSteeringEdit->setText("0");
ui->rrSteeringEdit->setText("0");
AllWheelState *empty = new AllWheelState();
emit explicitControlSelected(empty, empty);
delete empty;
}
void AllWheelForm::on_sendButton_clicked()
{
AllWheelState *steering = new AllWheelState(ui->lfSteeringEdit->text().toFloat(), ui->rfSteeringEdit->text().toFloat(), ui->lrSteeringEdit->text().toFloat(), ui->rrSteeringEdit->text().toFloat());
AllWheelState *driving = new AllWheelState(ui->lfDrivingEdit->text().toFloat(), ui->rfDrivingEdit->text().toFloat(), ui->lrDrivingEdit->text().toFloat(), ui->rrDrivingEdit->text().toFloat());
emit explicitControlSelected(steering, driving);
delete steering;
delete driving;
}
void AllWheelForm::on_sendCrabButton_clicked()
{
qreal vel = ui->crabVelocityEdit->text().toFloat();
qreal head = ui->crabHeadingEdit->text().toFloat();
emit crabControlSelected(head, vel);
}