Skip to content

Commit b66b5c8

Browse files
authored
Merge pull request #828 from IKhonakhbeeva/simple_robot
Parameter for blocks, animation of moving and over fixes [AUTOZIP]
2 parents 126fe41 + 3f40877 commit b66b5c8

File tree

28 files changed

+336
-156
lines changed

28 files changed

+336
-156
lines changed

initvars.qmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SUBDIRS=
66
#need to update index because of https://stackoverflow.com/questions/16035240/
77
system(git update-index --refresh)
88
# ATM we use xenial 16.04 as main linux builder, and it lacks new git with --broken option.
9-
TRIK_STUDIO_VERSION = start-$$system(git describe --tags --always --dirty --abbrev=6)
9+
TRIK_STUDIO_VERSION = junior-$$system(git describe --tags --always --dirty --abbrev=6)
1010
TRIK_STUDIO_YEAR = 2020
1111

1212
#message($$DESTDIR)

plugins/robots/common/kitBase/include/kitBase/robotModel/robotModelInterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ public slots:
199199

200200
// Hack to manually rotate the robot
201201
void turnManuallyOn(qreal angle);
202+
203+
// Hack to manually control the robot
204+
void endManual(bool success);
205+
202206
};
203207

204208
}

plugins/robots/common/trikKit/src/blocks/details/trikBackwardOneCellBlock.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ BackwardOneCellBlock::BackwardOneCellBlock(kitBase::robotModel::RobotModelInterf
2424
{
2525
}
2626

27-
void BackwardOneCellBlock::run()
28-
{
29-
emit mRobotModel.moveManually(-1);
30-
auto timer = mRobotModel.timeline().produceTimer();
31-
timer->setRepeatable(false);
32-
connect(timer, &utils::AbstractTimer::timeout, this, [this](){
33-
emit done(mNextBlockId);
34-
});
35-
timer->start(500);
27+
void BackwardOneCellBlock::run() {
28+
const auto result = -eval<int>("CellsNumber");
29+
if (!errorsOccured()) {
30+
mConnections << connect(&mRobotModel, &kitBase::robotModel::RobotModelInterface::endManual
31+
, this, &BackwardOneCellBlock::endMoving);
32+
emit mRobotModel.moveManually(result);
33+
}
34+
}
35+
36+
void BackwardOneCellBlock::endMoving(bool success) {
37+
for (auto connection : mConnections) {
38+
disconnect(connection);
39+
}
40+
if (!success) emit warning(tr("Movement is impossible!"));
41+
emit done(mNextBlockId);
3642
}

plugins/robots/common/trikKit/src/blocks/details/trikBackwardOneCellBlock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class BackwardOneCellBlock : public kitBase::blocksBase::RobotsBlock
3232

3333
private:
3434
kitBase::robotModel::RobotModelInterface &mRobotModel;
35+
QList<QMetaObject::Connection> mConnections;
36+
private slots:
37+
void endMoving(bool success);
3538
};
3639

3740
}

plugins/robots/common/trikKit/src/blocks/details/trikForwardOneCellBlock.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ ForwardOneCellBlock::ForwardOneCellBlock(kitBase::robotModel::RobotModelInterfac
2424
{
2525
}
2626

27-
void ForwardOneCellBlock::run()
28-
{
29-
emit mRobotModel.moveManually(1);
30-
auto timer = mRobotModel.timeline().produceTimer();
31-
timer->setRepeatable(false);
32-
connect(timer, &utils::AbstractTimer::timeout, this, [this](){
33-
emit done(mNextBlockId);
34-
});
35-
timer->start(500);
27+
void ForwardOneCellBlock::run() {
28+
const auto result = eval<int>("CellsNumber");
29+
if (!errorsOccured()) {
30+
mConnections << connect(&mRobotModel, &kitBase::robotModel::RobotModelInterface::endManual
31+
, this, &ForwardOneCellBlock::endMoving);
32+
emit mRobotModel.moveManually(result);
33+
}
34+
}
35+
36+
void ForwardOneCellBlock::endMoving(bool success) {
37+
for (auto connection : mConnections) {
38+
disconnect(connection);
39+
}
40+
if (!success) emit warning(tr("Movement is impossible!"));
41+
emit done(mNextBlockId);
3642
}

plugins/robots/common/trikKit/src/blocks/details/trikForwardOneCellBlock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class ForwardOneCellBlock : public kitBase::blocksBase::RobotsBlock
3232

3333
private:
3434
kitBase::robotModel::RobotModelInterface &mRobotModel;
35+
QList<QMetaObject::Connection> mConnections;
36+
private slots:
37+
void endMoving(bool success);
3538
};
3639

3740
}

plugins/robots/common/twoDModel/include/twoDModel/engine/model/robotModel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ private slots:
218218
twoDModel::robotModel::TwoDRobotModel &mRobotModel;
219219
SensorsConfiguration mSensorsConfiguration;
220220

221+
QPointF mWaitPos;
222+
bool mIsCollide;
223+
bool mIsRiding;
221224
QPointF mPos;
222225
qreal mAngle;
223226
qreal mGyroAngle;

plugins/robots/common/twoDModel/src/engine/items/startPosition.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "startPosition.h"
1616

1717
#include "twoDModel/engine/model/constants.h"
18+
#include <qrkernel/settingsManager.h>
19+
#include <QtMath>
1820

1921
using namespace twoDModel::items;
2022

@@ -96,3 +98,23 @@ void StartPosition::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
9698
{
9799
Q_UNUSED(event)
98100
}
101+
102+
void StartPosition::resizeItem(QGraphicsSceneMouseEvent *event)
103+
{
104+
Q_UNUSED(event)
105+
auto gridSize = qReal::SettingsManager::value("2dGridCellSize").toInt();
106+
auto x = pos().x() + gridSize/2;
107+
auto y = pos().y() + gridSize/2;
108+
auto roundedX = x - fmod(x, gridSize);
109+
auto roundedX2 = roundedX + ((x > 0) ? gridSize : -gridSize);
110+
if (qAbs(roundedX - x) > qAbs(roundedX2 - x)) {
111+
roundedX = roundedX2;
112+
}
113+
auto roundedY = y - fmod(y, gridSize);
114+
auto roundedY2 = roundedY + ((y > 0) ? gridSize : -gridSize);
115+
if (qAbs(roundedY - y) > qAbs(roundedY2 - y)) {
116+
roundedY = roundedY2;
117+
}
118+
QGraphicsItem::setPos(roundedX - gridSize/2, roundedY - gridSize/2);
119+
update();
120+
}

plugins/robots/common/twoDModel/src/engine/items/startPosition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class StartPosition : public graphicsUtils::RotateItem
3030
QDomElement serialize(QDomElement &parent) const override;
3131
void deserialize(const QDomElement &startPositionElement) override;
3232
void deserializeCompatibly(const QDomElement &robotElement);
33+
void resizeItem(QGraphicsSceneMouseEvent *event) override;
3334

3435
private:
3536
void drawFieldForResizeItem(QPainter* painter) override;

plugins/robots/common/twoDModel/src/engine/model/robotModel.cpp

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434

3535
#include "src/engine/items/startPosition.h"
3636
#include "src/engine/items/wallItem.h"
37+
#include "utils/abstractTimer.h"
3738

3839
using namespace twoDModel::model;
3940
using namespace kitBase::robotModel;
4041
using namespace kitBase::robotModel::robotParts;
4142

4243
const int positionStampsCount = 50;
44+
const qreal manualSpeed = 0.5;
4345

4446
RobotModel::RobotModel(robotModel::TwoDRobotModel &robotModel
4547
, const Settings &settings
@@ -95,18 +97,20 @@ void RobotModel::moveCell(int n) {
9597
auto shiftPos = QTransform().rotate(mAngle).map(QPointF(gridSize, 0));
9698
if (n < 0) shiftPos = -shiftPos;
9799
for (int i = 0; i < abs(n); i++) {
98-
auto moveLine = QLineF(rotationCenter(), rotationCenter() + shiftPos);
100+
auto moveLine = QLineF(rotationCenter(), rotationCenter() + shiftPos * (i + 1));
99101
for (auto wall : mWorldModel->walls()) {
100102
auto wallLine = QLineF(wall->begin(), wall->end());
101103
if (moveLine.intersect(wallLine, nullptr) == QLineF::BoundedIntersection) {
102-
mWorldModel->errorReporter()->addError(tr("Movement is impossible!"));
103-
markerDown(Qt::red);
104+
mIsRiding = true;
105+
mWaitPos = mPos + shiftPos * i;
106+
mIsCollide = true;
104107
return;
105108
}
106109
}
107-
mPos = position() + shiftPos;
108-
nextFragment();
109110
}
111+
mIsRiding = true;
112+
mWaitPos = mPos + shiftPos * abs(n);
113+
mIsCollide = false;
110114
}
111115

112116
void RobotModel::turnOn(qreal angle) {
@@ -359,8 +363,20 @@ QVector<int> RobotModel::gyroscopeCalibrate()
359363
void RobotModel::nextStep()
360364
{
361365
// Changing position quietly, they must not be caught by UI here.
362-
mPos += mPhysicsEngine->positionShift(*this).toPointF();
363-
mAngle += mPhysicsEngine->rotation(*this);
366+
// No physics for simple robot
367+
// mPos += mPhysicsEngine->positionShift(*this).toPointF();
368+
// mAngle += mPhysicsEngine->rotation(*this);
369+
if (mIsRiding) {
370+
auto delta = mWaitPos - mPos;
371+
auto lenSquare = QPointF::dotProduct(delta, delta);
372+
if (lenSquare <= manualSpeed * manualSpeed) {
373+
mPos = mWaitPos;
374+
mIsRiding = false;
375+
emit mRobotModel.endManual(!mIsCollide);
376+
} else {
377+
mPos += delta * manualSpeed / qSqrt(lenSquare);
378+
}
379+
}
364380
emit positionRecalculated(mPos, mAngle);
365381
}
366382

@@ -371,26 +387,27 @@ void RobotModel::recalculateParams()
371387
return;
372388
}
373389

374-
auto calculateMotorOutput = [&](WheelEnum wheel) {
375-
const PortInfo &port = mWheelsToMotorPortsMap.value(wheel, PortInfo());
376-
if (!port.isValid() || port.name() == "None") {
377-
return;
378-
}
390+
// No motors on simple model
391+
// auto calculateMotorOutput = [&](WheelEnum wheel) {
392+
// const PortInfo &port = mWheelsToMotorPortsMap.value(wheel, PortInfo());
393+
// if (!port.isValid() || port.name() == "None") {
394+
// return;
395+
// }
379396

380-
Wheel * const engine = mMotors.value(port, nullptr);
381-
if (!engine) {
382-
return;
383-
}
397+
// Wheel * const engine = mMotors.value(port, nullptr);
398+
// if (!engine) {
399+
// return;
400+
// }
384401

385-
engine->spoiledSpeed = mSettings.realisticMotors() ? varySpeed(engine->speed) : engine->speed;
386-
};
402+
// engine->spoiledSpeed = mSettings.realisticMotors() ? varySpeed(engine->speed) : engine->speed;
403+
// };
387404

388-
calculateMotorOutput(left);
389-
calculateMotorOutput(right);
405+
// calculateMotorOutput(left);
406+
// calculateMotorOutput(right);
390407

391408
nextStep();
392-
countSpeedAndAcceleration();
393-
countMotorTurnover();
409+
// countSpeedAndAcceleration();
410+
// countMotorTurnover();
394411
countBeep();
395412
}
396413

0 commit comments

Comments
 (0)