Skip to content

Commit

Permalink
Add support for Network blocks in the visual language (#1795)
Browse files Browse the repository at this point in the history
1. The `mailbox` entity has been taken to a higher level for use in visual language
2. Support for `TrikJoinNetwork`, `TrikWaitForMessage`, `TrikSendMessage` blocks in visual language
3. Cut out unnecessary functionality due to the use of the TrikJoinNetwork block
  • Loading branch information
MinyazevR authored Oct 24, 2024
1 parent b61826c commit 255135e
Show file tree
Hide file tree
Showing 36 changed files with 655 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright 2024 CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#pragma once

#include "device.h"
#include "kitBase/kitBaseDeclSpec.h"

namespace kitBase {
namespace robotModel {
namespace robotParts {

/// Base class for robot network communication.
class ROBOTS_KIT_BASE_EXPORT Communicator : public Device
{
Q_OBJECT
Q_CLASSINFO("name", "communicator")
Q_CLASSINFO("friendlyName", tr("Communicator"))
Q_CLASSINFO("direction", "output")

public:
/// Constructor, takes device type info and port on which this device is configured.
Communicator(const DeviceInfo &info, const PortInfo &port);

/// Deinitializing a network communicator
virtual void release() = 0;
};
}
}
}
2 changes: 2 additions & 0 deletions plugins/robots/common/kitBase/kitBase.pri
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ HEADERS += \
$$PWD/include/kitBase/robotModel/robotParts/colorSensorAmbient.h \
$$PWD/include/kitBase/robotModel/robotParts/colorSensorReflected.h \
$$PWD/include/kitBase/robotModel/robotParts/speaker.h \
$$PWD/include/kitBase/robotModel/robotParts/communicator.h \
$$PWD/include/kitBase/robotModel/robotParts/motor.h \
$$PWD/include/kitBase/robotModel/robotParts/display.h \
$$PWD/include/kitBase/robotModel/robotParts/button.h \
Expand Down Expand Up @@ -126,6 +127,7 @@ SOURCES += \
$$PWD/src/robotModel/portInfo.cpp \
$$PWD/src/robotModel/deviceInfo.cpp \
$$PWD/src/robotModel/robotParts/speaker.cpp \
$$PWD/src/robotModel/robotParts/communicator.cpp \
$$PWD/src/robotModel/robotParts/motor.cpp \
$$PWD/src/robotModel/robotParts/display.cpp \
$$PWD/src/robotModel/robotParts/button.cpp \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "kitBase/robotModel/robotParts/motor.h"
#include "kitBase/robotModel/robotParts/random.h"
#include "kitBase/robotModel/robotParts/communicator.h"

const int updateInterval = 200;

Expand Down Expand Up @@ -72,10 +73,14 @@ void CommonRobotModel::connectToRobot()
void CommonRobotModel::stopRobot()
{
for (robotParts::Device * const device : mConfiguration.devices()) {
robotParts::Motor * const motor = dynamic_cast<robotParts::Motor *>(device);
auto* const motor = qobject_cast<robotParts::Motor *>(device);
if (motor) {
motor->off();
}
auto* const communicator = qobject_cast<robotParts::Communicator *>(device);
if (communicator) {
communicator->release();
}
}
/// @todo: add known deinitialization methods here (for example sensors termination after extending their inteface)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright 2024 CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#include "kitBase/robotModel/robotParts/communicator.h"

using namespace kitBase::robotModel;
using namespace robotParts;

Communicator::Communicator(const DeviceInfo &info, const PortInfo &port)
: Device(info, port)
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright 2024 CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#pragma once
#include <kitBase/robotModel/robotParts/communicator.h>

namespace trik {
namespace robotModel {
namespace parts {

/// Class for TRIK robot network communication.
class TrikNetworkCommunicator : public kitBase::robotModel::robotParts::Communicator
{
Q_OBJECT

public:
TrikNetworkCommunicator(const kitBase::robotModel::DeviceInfo &info
, const kitBase::robotModel::PortInfo &port);


/// Send a message to a robot
virtual void send(const QString& message, int hullNumber) = 0;

/// Receive message from robot
virtual QString receive(bool wait) = 0;

/// Join the network with the specified hull number
virtual void joinNetwork(const QString &ip, int port, int hullNumber) = 0;

/// Send an interrupt to stop waiting for a message
virtual void stopWaiting() = 0;

/// Clean the incoming message queue
virtual void clearQueue() = 0;
};

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class TrikRobotModelBase : public kitBase::robotModel::CommonRobotModel

virtual kitBase::robotModel::PortInfo video2Port() const;
virtual kitBase::robotModel::PortInfo lidarPort() const;
virtual kitBase::robotModel::DeviceInfo networkInfo() const;
};

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Copyright 2024 CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */


#include "joinNetworkBlock.h"

using namespace trik;
using namespace blocks::details;

JoinNetworkBlock::JoinNetworkBlock(kitBase::robotModel::RobotModelInterface &robotModel):
kitBase::blocksBase::common::DeviceBlock<robotModel::parts::TrikNetworkCommunicator>(robotModel)
{}

void JoinNetworkBlock::doJob(robotModel::parts::TrikNetworkCommunicator &network)
{
auto address = stringProperty("Address");
auto port = intProperty("IPPort");
auto hullNumber = intProperty("HullNumber");
network.joinNetwork(address, port, hullNumber);
emit done(mNextBlockId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright 2024 CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#pragma once

#include <kitBase/blocksBase/common/deviceBlock.h>
#include "trikKit/robotModel/parts/trikNetworkCommunicator.h"

namespace trik {
namespace blocks {
namespace details {

class JoinNetworkBlock: public kitBase::blocksBase::common::DeviceBlock<robotModel::parts::TrikNetworkCommunicator>
{
Q_OBJECT
public:
JoinNetworkBlock(kitBase::robotModel::RobotModelInterface &robotModel);

private:
void doJob(robotModel::parts::TrikNetworkCommunicator &network) override;
};

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright 2024 CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#include "sendMessageBlock.h"

using namespace trik;
using namespace blocks::details;

SendMessageBlock::SendMessageBlock(kitBase::robotModel::RobotModelInterface &robotModel):
kitBase::blocksBase::common::DeviceBlock<robotModel::parts::TrikNetworkCommunicator>(robotModel)
{}

void SendMessageBlock::doJob(robotModel::parts::TrikNetworkCommunicator &network)
{
auto message = stringProperty("Message");
auto hullNumber = intProperty("HullNumber");
network.send(message, hullNumber);
emit done(mNextBlockId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright 2024 CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#pragma once

#include <kitBase/blocksBase/common/deviceBlock.h>
#include "trikKit/robotModel/parts/trikNetworkCommunicator.h"

namespace trik {
namespace blocks {
namespace details {

class SendMessageBlock: public kitBase::blocksBase::common::DeviceBlock<robotModel::parts::TrikNetworkCommunicator>
{
Q_OBJECT
public:
SendMessageBlock(kitBase::robotModel::RobotModelInterface &robotModel);

private:
void doJob(robotModel::parts::TrikNetworkCommunicator &network) override;
};

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* Copyright 2024 CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#include "waitForMessageBlock.h"
#include "trikKit/robotModel/parts/trikNetworkCommunicator.h"
#include <utils/abstractTimer.h>
#include <qrutils/stringUtils.h>
#include <kitBase/robotModel/robotModelInterface.h>
#include <kitBase/robotModel/robotModelUtils.h>

using namespace trik;
using namespace blocks::details;
using namespace kitBase::robotModel;

WaitForMessageBlock::WaitForMessageBlock(kitBase::robotModel::RobotModelInterface &robotModel):
WaitBlock(robotModel)
{
mActiveWaitingTimer->setSingleShot(true);
mActiveWaitingTimer->setInterval(100);
}

void WaitForMessageBlock::handle()
{
evalCode(stringProperty("Variable") + " = " + mMessage);
stop();
emit done(mNextBlockId);
return;
}

void WaitForMessageBlock::run()
{
mNetwork = RobotModelUtils::findDevice<robotModel::parts::TrikNetworkCommunicator>(mRobotModel, "CommunicatorPort");

if (!mNetwork) {
error(tr("Device not found for port name CommunicatorPort"));
return;
}

auto wait = boolProperty("Synchronized");

if (!wait) {
mMessage = utils::StringUtils::wrap(utils::StringUtils::dequote(mNetwork->receive(wait)));
handle();
return;
}

mActiveWaitingTimer->start();
}

void WaitForMessageBlock::timerTimeout()
{
auto message = mNetwork->receive(false);
if (message != QString()) {
mMessage = utils::StringUtils::wrap(utils::StringUtils::dequote(message));
handle();
return;
}

mActiveWaitingTimer->start();
}

DeviceInfo WaitForMessageBlock::device() const
{
return DeviceInfo::create<robotModel::parts::TrikNetworkCommunicator>();
}
Loading

0 comments on commit 255135e

Please sign in to comment.