Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
78e8957
added trajectory saving
ladaegorova18 Mar 11, 2022
861d908
Added net connection, trajectory serialization to json and pause/run …
Apr 10, 2022
21156a0
Removed .proto file
Apr 10, 2022
3d6e534
added net connection run/pause/restart from Unity, renamed trajectory…
Apr 15, 2022
710e851
added some comments, fixed tabulations
Apr 15, 2022
ac8c00e
Added sending run/pause/restart signals from TRIK Studio to Unity
Apr 20, 2022
53a413d
added checkBox sendData and comboBox UnityIp to additionalPreferences
Apr 24, 2022
cc7c592
fixed vera errors, added license
May 5, 2022
7a87218
Merged upstream/master
May 5, 2022
21400ba
lupdate
May 5, 2022
65e7037
added QT += network
May 5, 2022
64df440
renamed some variables to mVariables
May 6, 2022
5ef1af1
removed some empty strings
May 6, 2022
8688514
lupdate
May 6, 2022
c738a8f
added passing parameters by link
May 10, 2022
441910c
lupdate
May 10, 2022
8b6149b
added forward declarations
May 10, 2022
40b7ffd
changed lines length
May 11, 2022
169906c
lupdate
May 11, 2022
d0b13f5
renamed ConnectionToVizualizator -> ConnectionToVisualizer
May 11, 2022
5131260
added QScopedPointers, separated saving position and rotation
May 12, 2022
b3c21e3
added comments
May 12, 2022
0d5d1f1
changed lines length
May 11, 2022
c2c4e10
renamed ConnectionToVizualizator -> ConnectionToVisualizer
May 11, 2022
5989474
added QScopedPointers, separated saving position and rotation
May 12, 2022
45e25cf
added comments
May 12, 2022
6437234
rebased upstream/master
May 12, 2022
57566f2
Merge branch 'master' into master
ladaegorova18 May 12, 2022
56f96e2
lupdate
May 12, 2022
33286dd
removed sizeof(8)
May 13, 2022
3d94758
removed qcsopedpointers
May 13, 2022
f8d9ce5
added endState in the end of trajectory
May 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -116,7 +116,7 @@ class TWO_D_MODEL_EXPORT RobotModel : public QObject

bool isRiding() const;

void serialize(QDomElement &parent) const;
QDomElement serialize(QDomElement &parent) const;
void serializeWorldModel(QDomElement &parent) const;
void deserialize(const QDomElement &robotElement);
void deserializeWorldModel(const QDomElement &world);
Expand Down Expand Up @@ -187,6 +187,19 @@ public slots:
/// Emitted when left or right wheel was reconnected to another port.
void wheelOnPortChanged(WheelEnum wheel, const kitBase::robotModel::PortInfo &port);

/// Emitted when robot starts or ends play beep sound
void trajectorySoundStateChanged(QString id, int time);

/// Emitted when robot starts or ends draw
void trajectoryMarkerColorChanged(QString id, QColor color);

/// Emitted when robot position and/or rotation changed
void trajectoryPosOrAngleChanged(QString id, QPointF position, qreal rotation);
void trajectoryOnitemDragged(QString id, QPointF position, qreal rotation);

void trajectorySave();
void onStopPlaying();

private:
QVector2D robotDirectionVector() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "twoDModel/engine/twoDModelDisplayWidget.h"

#include "twoDModel/twoDModelDeclSpec.h"
#include "plugins/robots/common/twoDModel/src/engine/trajectory/connectionToVizualizator.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем этот #include?


class QComboBox;
class QPushButton;
Expand All @@ -47,6 +48,10 @@ class ControllerInterface;

namespace twoDModel {

//namespace trajectory {
//class ConnectionToVizualizator;
//}

namespace model {
class Model;
class RobotModel;
Expand Down Expand Up @@ -108,6 +113,7 @@ class TWO_D_MODEL_EXPORT TwoDModelWidget : public QWidget
QDomDocument generateWorldModelWithBlobsXml() const;
QDomDocument generateWorldModelXml() const;
QDomDocument generateBlobsXml() const;
ConnectionToVizualizator *mConnToVizualizator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отсутствует инициализация {}, не указано, кто владеет объектом


public slots:
void zoomIn() override;
Expand All @@ -129,6 +135,9 @@ public slots:
/// Emitted when user has stopped intepretation from the 2D model window.
void stopButtonPressed();

/// Emitted when user has stopped intepretation from the remote vizualizator
void restartRequested();

protected:
void changeEvent(QEvent *e) override;
void showEvent(QShowEvent *e) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -17,6 +17,7 @@
#include <qrutils/graphicsUtils/abstractItem.h>

#include "twoDModel/engine/model/robotModel.h"
#include "src/engine/trajectory/trajectorySaver.h"
#include "twoDModel/engine/model/constants.h"
#include "twoDModel/engine/model/worldModel.h"
#include "src/engine/view/scene/twoDModelScene.h"
Expand All @@ -32,6 +33,7 @@
#include "parts/box2DItem.h"

using namespace twoDModel::model::physics;

using namespace parts;
using namespace mathUtils;

Expand All @@ -44,6 +46,7 @@ Box2DPhysicsEngine::Box2DPhysicsEngine (const WorldModel &worldModel
, mWorld(new b2World(b2Vec2(0, 0)))
, mPrevPosition(b2Vec2(0, 0))
, mPrevAngle(0)
, mTrajSaver(new TrajectorySaver(nullptr))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Думаю, что тут надо бы не nullptr

{
connect(&worldModel, &model::WorldModel::wallAdded,
this, [this](const QSharedPointer<QGraphicsItem> &i) {itemAdded(i.data());});
Expand All @@ -53,6 +56,10 @@ Box2DPhysicsEngine::Box2DPhysicsEngine (const WorldModel &worldModel
this, [this](const QSharedPointer<QGraphicsItem> &i) {itemAdded(i.data());});
connect(&worldModel, &model::WorldModel::itemRemoved,
this, [this](const QSharedPointer<QGraphicsItem> &i) {itemRemoved(i.data());});

connect(this, &Box2DPhysicsEngine::trajectoryPosOrAngleChanged, mTrajSaver, &TrajectorySaver::saveItemPosOrAngle);
connect(this, &Box2DPhysicsEngine::trajectoryItemDragged, mTrajSaver, &TrajectorySaver::onItemDragged);
connect(this, &Box2DPhysicsEngine::sendNextFrame, mTrajSaver, &TrajectorySaver::sendFrame);
}

Box2DPhysicsEngine::~Box2DPhysicsEngine(){
Expand Down Expand Up @@ -145,6 +152,16 @@ void Box2DPhysicsEngine::addRobot(model::RobotModel * const robot)
});

connect(robot, &model::RobotModel::deserialized, this, &Box2DPhysicsEngine::onMouseReleased);


connect(robot, &RobotModel::trajectorySoundStateChanged, mTrajSaver, &TrajectorySaver::saveBeepState);
connect(robot, &RobotModel::trajectoryMarkerColorChanged, mTrajSaver, &TrajectorySaver::saveMarkerState);
connect(robot, &RobotModel::trajectoryPosOrAngleChanged, mTrajSaver, &TrajectorySaver::saveItemPosOrAngle);
connect(robot, &RobotModel::trajectoryOnitemDragged, mTrajSaver, &TrajectorySaver::onItemDragged);

connect(robot, &RobotModel::trajectorySave, mTrajSaver, &TrajectorySaver::saveToFile);
// connect(robot, &RobotModel::OnStartPlaying, mTrajSaver, &TrajectorySaver::OnStartInterpretation);
connect(robot, &RobotModel::onStopPlaying, mTrajSaver, &TrajectorySaver::onStopInterpretation);
});
}

Expand Down Expand Up @@ -353,7 +370,10 @@ void Box2DPhysicsEngine::nextFrame()
item->setPos(scenePos - item->boundingRect().center());
item->setRotation(angleToScene(mBox2DDynamicItems[item]->getRotation()));
}
auto *abstractItem = dynamic_cast<graphicsUtils::AbstractItem *>(item);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qobject_cast не подойдёт?

emit trajectoryPosOrAngleChanged(abstractItem->id(), abstractItem->pos(), abstractItem->rotation());
}
emit sendNextFrame();
}

void Box2DPhysicsEngine::clearForcesAndStop()
Expand All @@ -364,6 +384,7 @@ void Box2DPhysicsEngine::clearForcesAndStop()
body->SetLinearVelocity({0, 0});
body->SetAngularVelocity(0);
}
//emit trajectorySave();
}

bool Box2DPhysicsEngine::isRobotStuck() const
Expand Down Expand Up @@ -428,6 +449,7 @@ void Box2DPhysicsEngine::onItemDragged(graphicsUtils::AbstractItem *item)
bItem->setRotation(0);
bItem->moveToPosition(positionToBox2D(localScenePos + localCenter));
bItem->setRotation(angleToBox2D(localRotation));
emit trajectoryItemDragged(item->id(), item->pos(), item->rotation());
}
} else {
b2Vec2 pos = positionToBox2D(collidingPolygon.boundingRect().center());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -19,6 +19,7 @@
#include <qrutils/mathUtils/geometry.h>

#include "twoDModel/engine/model/worldModel.h"
#include "plugins/robots/common/twoDModel/src/engine/trajectory/trajectorySaver.h"

class b2World;
class b2Body;
Expand All @@ -34,6 +35,10 @@ namespace twoDModel {
class SensorItem;
}

namespace trajectory {
class TrajectorySaver;
}

namespace model {
namespace physics {
namespace parts {
Expand All @@ -44,6 +49,7 @@ namespace twoDModel {

class Box2DPhysicsEngine : public PhysicsEngineBase
{
Q_OBJECT
public:
Box2DPhysicsEngine(const WorldModel &worldModel, const QList<RobotModel *> &robots);
~Box2DPhysicsEngine();
Expand Down Expand Up @@ -84,6 +90,12 @@ public slots:
void onMousePressed();
void onRecoverRobotPosition(const QPointF &pos);

signals:
void trajectoryPosOrAngleChanged(QString id, QPointF pos, qreal rotation);
void trajectoryItemDragged(QString id, QPointF pos, qreal rotation);
void trajectorySave();
void sendNextFrame();

protected:
void onPixelsInCmChanged(qreal value) override;
void itemAdded(QGraphicsItem *item) override;
Expand All @@ -104,9 +116,11 @@ public slots:
QMap<QGraphicsItem *, parts::Box2DItem *> mBox2DResizableItems; // Takes ownership on b2Body instances
QMap<QGraphicsItem *, parts::Box2DItem *> mBox2DDynamicItems; // Doesn't take ownership
QMap<RobotModel *, QSet<twoDModel::view::SensorItem *>> mRobotSensors; // Doesn't take ownership
QList<parts::Box2DItem *> inMoveItems;

b2Vec2 mPrevPosition;
float mPrevAngle;
TrajectorySaver const *mTrajSaver {};
};

}
Expand Down
58 changes: 37 additions & 21 deletions plugins/robots/common/twoDModel/src/engine/model/robotModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -68,6 +68,7 @@ void RobotModel::reinit()
mBeepTime = 0;
mDeltaDegreesOfAngle = 0;
mAcceleration = QPointF(0, 0);
//emit OnStartPlaying();
}

void RobotModel::clear()
Expand All @@ -94,8 +95,8 @@ RobotModel::Wheel *RobotModel::initMotor(int radius, int speed, uint64_t degrees
mMotors[port].reset(motor);

/// @todo We need some mechanism to set correspondence between motors and encoders. In NXT motors and encoders are
/// physically plugged into one port, so we can find corresponding port by name. But in TRIK encoders can be
/// connected differently.
/// physically plugged into one port, so we can find corresponding port by name. But in TRIK encoders can be
/// connected differently.
for (const Device * const device : mRobotModel.configuration().devices()) {
if (device->deviceInfo().isA<EncoderSensor>()
&& (device->port().name() == port.name() || device->port().nameAliases().contains(port.name())))
Expand All @@ -111,6 +112,7 @@ RobotModel::Wheel *RobotModel::initMotor(int radius, int speed, uint64_t degrees
void RobotModel::playSound(int timeInMs)
{
mBeepTime = qMax(mBeepTime, timeInMs);
emit trajectorySoundStateChanged(mRobotModel.robotId(), mBeepTime);
}

void RobotModel::setNewMotor(int speed, uint degrees, const PortInfo &port, bool breakMode)
Expand Down Expand Up @@ -179,6 +181,8 @@ void RobotModel::stopRobot()
mIsFirstAngleStamp = true;
mPosStamps.clear();
emit playingSoundChanged(false);
emit onStopPlaying();
emit trajectorySave();
for (auto &&engine : mMotors) {
engine->speed = 0;
engine->breakMode = true;
Expand Down Expand Up @@ -251,9 +255,9 @@ QPainterPath RobotModel::sensorBoundingPath(const PortInfo &port) const
QPainterPath tempSensorPath;
tempSensorPath.addRect(sensorRect(port, sensorPos));
const QTransform transformSensor = QTransform()
.translate(sensorPos.x(), sensorPos.y()) // /\ And going back again
.translate(sensorPos.x(), sensorPos.y()) // /\ And going back again
.rotate(mSensorsConfiguration.direction(port)) // || Then rotating
.translate(-sensorPos.x(), -sensorPos.y()); // || First translating to zero
.translate(-sensorPos.x(), -sensorPos.y()); // || First translating to zero
return transformSensor.map(tempSensorPath);
}

Expand Down Expand Up @@ -301,11 +305,13 @@ QColor RobotModel::markerColor() const
void RobotModel::markerDown(const QColor &color)
{
mMarker = color;
emit trajectoryMarkerColorChanged(mRobotModel.robotId(), color);
}

void RobotModel::markerUp()
{
mMarker = Qt::transparent;
emit trajectoryMarkerColorChanged(mRobotModel.robotId(), Qt::transparent);
}

QVector<int> RobotModel::accelerometerReading() const
Expand Down Expand Up @@ -372,6 +378,10 @@ void RobotModel::nextFragment()
}

emit robotRided(mPos, mAngle);
if (isRiding())
{
emit trajectoryPosOrAngleChanged(mRobotModel.robotId(), mPos, mAngle);
}
}

QPointF RobotModel::position() const
Expand All @@ -384,6 +394,7 @@ void RobotModel::setPosition(const QPointF &newPos)
if (newPos != mPos) {
mPos = newPos;
emit positionChanged(newPos);
emit trajectoryOnitemDragged(mRobotModel.robotId(), mPos, mAngle);
}
}

Expand All @@ -397,6 +408,7 @@ void RobotModel::setRotation(qreal angle)
if (!mathUtils::Math::eq(mAngle, angle)) {
mAngle = angle;
emit rotationChanged(angle);
emit trajectoryOnitemDragged(mRobotModel.robotId(), mPos, mAngle);
}
}

Expand All @@ -415,23 +427,19 @@ bool RobotModel::onTheGround() const
return mIsOnTheGround;
}

void RobotModel::serialize(QDomElement &parent) const
QDomElement RobotModel::serialize(QDomElement &parent) const
{
QDomElement curRobot = parent.ownerDocument().createElement("robot");
curRobot.setAttribute("id", mRobotModel.robotId());
mSensorsConfiguration.serialize(curRobot);
serializeWheels(curRobot);
QDomElement robot = parent.ownerDocument().createElement("robot");
parent.appendChild(robot);
robot.setAttribute("id", mRobotModel.robotId());
robot.setAttribute("position", QString::number(mPos.x()) + ":" + QString::number(mPos.y()));
robot.setAttribute("direction", QString::number(mAngle));

bool replaced = false;
for (QDomElement robot = parent.firstChildElement("robot"); !robot.isNull()
; robot = robot.nextSiblingElement("robot")) {
if (robot.attribute("id") == mRobotModel.robotId()) {
parent.replaceChild(curRobot, robot);
replaced = true;
break;
}
}
if (!replaced) parent.appendChild(curRobot);
mSensorsConfiguration.serialize(robot);
mStartPositionMarker->serialize(robot);
serializeWheels(robot);

return robot;
}

void RobotModel::serializeWorldModel(QDomElement &parent) const
Expand Down Expand Up @@ -471,8 +479,16 @@ void RobotModel::deserializeWorldModel(const QDomElement &world)

void RobotModel::deserialize(const QDomElement &robotElement)
{
const QString positionStr = robotElement.attribute("position", "0:0");
const QStringList splittedStr = positionStr.split(":");
const qreal x = static_cast<qreal>(splittedStr[0].toDouble());
const qreal y = static_cast<qreal>(splittedStr[1].toDouble());
onRobotReturnedOnGround();
setPosition(QPointF(x, y));
setRotation(robotElement.attribute("direction", "0").toDouble());
mStartPositionMarker->deserializeCompatibly(robotElement);
deserializeWheels(robotElement);
configuration().deserialize(robotElement);
emit deserialized(QPointF(x, y), robotElement.attribute("direction", "0").toDouble());
nextFragment();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Timeline::Timeline(QObject *parent)
qRegisterMetaType<qReal::interpretation::StopReason>();
registered = true;
}

connect(&mTimer, &QTimer::timeout, this, &Timeline::onTimer);
mTimer.setInterval(defaultRealTimeInterval);
}
Expand Down
Loading