186 lines
10 KiB
C++
186 lines
10 KiB
C++
|
|
// Integration harness: the actual MainWindow and RobotClient talk only to a
|
||
|
|
// loopback fake service. No physical robot or user settings are accessed.
|
||
|
|
#include <QtTest>
|
||
|
|
#include <QApplication>
|
||
|
|
#include <QCheckBox>
|
||
|
|
#include <QComboBox>
|
||
|
|
#include <QLineEdit>
|
||
|
|
#include <QDoubleSpinBox>
|
||
|
|
#include <QSettings>
|
||
|
|
#include <QSlider>
|
||
|
|
#include <QSpinBox>
|
||
|
|
#include <QTemporaryDir>
|
||
|
|
#include <QMainWindow>
|
||
|
|
#include <QPushButton>
|
||
|
|
#include <grpcpp/grpcpp.h>
|
||
|
|
#include "cmvr/api/arm_service.grpc.pb.h"
|
||
|
|
#include <atomic>
|
||
|
|
#include <mutex>
|
||
|
|
#include <stdexcept>
|
||
|
|
#include <cstdio>
|
||
|
|
#define private public
|
||
|
|
#include "../src/mainwindow.h"
|
||
|
|
#undef private
|
||
|
|
#include "../src/robotclient.h"
|
||
|
|
#include "../src/tcpcalibrationwidget.h"
|
||
|
|
#include "../src/tcpjogcontroller.h"
|
||
|
|
|
||
|
|
using namespace TcpCalibration;
|
||
|
|
class FakeArm : public cmvr::api::ArmService::Service {
|
||
|
|
public:
|
||
|
|
std::atomic<int> moves{0}, stops{0};
|
||
|
|
std::atomic<int> offsetMode{0};
|
||
|
|
grpc::Status ExecuteJsonCommand(grpc::ServerContext *,
|
||
|
|
const cmvr::api::JsonDeviceCommand::Request *r,
|
||
|
|
cmvr::api::JsonDeviceCommand::Feedback *s) override {
|
||
|
|
if (r->header().device_id() != "arm-a"
|
||
|
|
|| r->request_json() != "{\"command\":\"get_active_tcp_offset\"}")
|
||
|
|
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "wrong offset request");
|
||
|
|
s->mutable_header()->set_success(true);
|
||
|
|
const std::string units = offsetMode == 1 ? "mm_deg" : "m_rad";
|
||
|
|
const std::string device = offsetMode == 2 ? "other-arm" : "arm-a";
|
||
|
|
const std::string values = offsetMode == 3 ? "[0,0,null,0,0,0]" : "[0,0,0,0,0,0]";
|
||
|
|
s->set_response_json("{\"success\":true,\"device_id\":\"" + device +
|
||
|
|
"\",\"transform\":\"T_flange_tcp\",\"units\":\"" + units +
|
||
|
|
"\",\"rotation_convention\":\"RzRyRx\",\"offset\":" + values + "}");
|
||
|
|
return grpc::Status::OK;
|
||
|
|
}
|
||
|
|
std::mutex mutex;
|
||
|
|
cmvr::api::MoveL::Request last;
|
||
|
|
Pose current{{.4,.2,.5,0,0,0}};
|
||
|
|
grpc::Status GetArmInfo(grpc::ServerContext *, const cmvr::api::GetArmInfo::Request *r,
|
||
|
|
cmvr::api::GetArmInfo::Response *s) override {
|
||
|
|
s->mutable_header()->set_success(true); s->set_device_id(r->header().device_id());
|
||
|
|
s->set_driver_type("AuboARM"); s->set_manufacturer("Aubo"); s->set_dof(6);
|
||
|
|
s->set_default_base_frame("base"); s->set_default_tcp_frame("tcp");
|
||
|
|
for(int i=0;i<6;++i) s->add_joint_names("joint"+std::to_string(i));
|
||
|
|
return grpc::Status::OK;
|
||
|
|
}
|
||
|
|
grpc::Status getRobotState(grpc::ServerContext *, const cmvr::api::GetRobotState::Request *,
|
||
|
|
cmvr::api::GetRobotState::Response *s) override {
|
||
|
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
|
s->mutable_header()->set_success(true); auto *state=s->mutable_state();
|
||
|
|
state->set_connected(true); state->set_powered_on(true);
|
||
|
|
state->set_robot_mode(cmvr::api::ARM_ROBOT_MODE_IDLE);
|
||
|
|
state->set_safety_mode(cmvr::api::ARM_SAFETY_MODE_NORMAL);
|
||
|
|
auto *p=state->mutable_actual_tcp_pose();
|
||
|
|
p->set_x(current[0]); p->set_y(current[1]); p->set_z(current[2]);
|
||
|
|
p->set_rx(current[3]); p->set_ry(current[4]); p->set_rz(current[5]);
|
||
|
|
auto *j=state->mutable_actual_joint_state();
|
||
|
|
for(int i=0;i<6;++i) { j->add_position(.1); j->add_velocity(0); }
|
||
|
|
return grpc::Status::OK;
|
||
|
|
}
|
||
|
|
grpc::Status moveL(grpc::ServerContext *, const cmvr::api::MoveL::Request *r,
|
||
|
|
cmvr::api::MoveL::Response *s) override {
|
||
|
|
std::lock_guard<std::mutex> lock(mutex); last=*r;
|
||
|
|
const auto &p=r->target(); current={{p.x(),p.y(),p.z(),p.rx(),p.ry(),p.rz()}};
|
||
|
|
s->mutable_header()->set_success(true); ++moves;
|
||
|
|
return grpc::Status::OK;
|
||
|
|
}
|
||
|
|
grpc::Status stopMotion(grpc::ServerContext *, const cmvr::api::CommandHeader::Request *,
|
||
|
|
cmvr::api::CommandHeader::Feedback *s) override {
|
||
|
|
s->set_success(true); ++stops; return grpc::Status::OK;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
static void require(bool ok,const char *detail) { if(!ok) throw std::runtime_error(detail); }
|
||
|
|
static bool until(const std::function<bool()> &predicate,int timeout=5000) {
|
||
|
|
QElapsedTimer timer; timer.start();
|
||
|
|
while(!predicate() && timer.elapsed()<timeout) QTest::qWait(10);
|
||
|
|
return predicate();
|
||
|
|
}
|
||
|
|
int main(int argc,char **argv) {
|
||
|
|
QApplication app(argc,argv);
|
||
|
|
QTemporaryDir temporary;
|
||
|
|
app.setOrganizationName("CMVR-TCP-Isolated-Test"); app.setApplicationName("Integration");
|
||
|
|
QSettings::setDefaultFormat(QSettings::IniFormat);
|
||
|
|
QSettings::setPath(QSettings::IniFormat,QSettings::UserScope,temporary.path());
|
||
|
|
FakeArm fake; grpc::ServerBuilder builder; int port=0;
|
||
|
|
builder.AddListeningPort("127.0.0.1:0",grpc::InsecureServerCredentials(),&port);
|
||
|
|
builder.RegisterService(&fake); auto server=builder.BuildAndStart();
|
||
|
|
try {
|
||
|
|
require(server && port>0,"loopback server startup failed");
|
||
|
|
MainWindow window;
|
||
|
|
window.m_demoCheck->setChecked(false);
|
||
|
|
window.m_hostEdit->setText("127.0.0.1"); window.m_portSpin->setValue(port);
|
||
|
|
window.m_armIdEdit->setText("arm-a");
|
||
|
|
const QString endpoint=window.endpoint();
|
||
|
|
QSettings settings; QString error;
|
||
|
|
TcpToolStore::Tool a{QStringLiteral("probe-a"),{{0,0,.2,0,0,1.5707963267948966}},.1,4};
|
||
|
|
TcpToolStore::Tool b{QStringLiteral("probe-b"),{{0,0,.1,0,0,0}},.1,4};
|
||
|
|
require(TcpToolStore::save(settings,endpoint,"arm-a",a,false,error),"save arm-a");
|
||
|
|
require(TcpToolStore::save(settings,endpoint,"arm-b",b,false,error),"save arm-b");
|
||
|
|
require(TcpToolStore::save(settings,"other-host:1","arm-a",b,false,error),"save other host");
|
||
|
|
window.m_tcpCalibration->toolsChanged(endpoint,"arm-a",a.name);
|
||
|
|
require(window.m_tcpFrameCombo->findData("local:probe-a")>=0,"saved TCP not in jog dropdown");
|
||
|
|
require(window.m_tcpFrameCombo->findData("local:probe-b")<0,"other robot TCP leaked");
|
||
|
|
window.m_armIdEdit->setText("arm-b");
|
||
|
|
require(window.m_tcpFrameCombo->findData("local:probe-a")<0,"old robot TCP remained");
|
||
|
|
require(window.m_tcpFrameCombo->findData("local:probe-b")>=0,"selected robot TCP missing");
|
||
|
|
window.m_armIdEdit->setText("arm-a");
|
||
|
|
window.toggleConnection();
|
||
|
|
require(until([&]{return window.m_connected;}),"connection failed");
|
||
|
|
require(window.selectedLocalTcp(),"local selection lost on connect");
|
||
|
|
QSignalSpy offsets(window.m_client, &RobotClient::controllerTcpOffsetReceived);
|
||
|
|
for (int mode = 1; mode <= 3; ++mode) {
|
||
|
|
fake.offsetMode = mode;
|
||
|
|
window.m_client->requestControllerTcpOffset("arm-a", 100 + mode);
|
||
|
|
require(until([&] { return offsets.count() == mode; }), "offset response missing");
|
||
|
|
require(!offsets.last().at(3).toBool(), "invalid controller offset accepted");
|
||
|
|
}
|
||
|
|
fake.offsetMode = 0;
|
||
|
|
window.m_tcpCalibration->findChild<QPushButton *>("tcpReadControllerOffset")->click();
|
||
|
|
require(until([&] { return window.m_tcpCalibration->referenceConfirmed(); }),
|
||
|
|
"controller offset did not reach calibration UI");
|
||
|
|
require(fake.moves == 0, "read-only offset request caused motion");
|
||
|
|
window.m_tcpCalibration->findChild<QCheckBox *>("tcpSourceConfirmed")->setChecked(true);
|
||
|
|
window.m_jogSpeedSlider->setValue(100);
|
||
|
|
window.startCartesianJog(0,1);
|
||
|
|
require(until([&]{return fake.moves.load()>0;}),"local TCP did not dispatch a real MoveL RPC");
|
||
|
|
window.stopArmJog();
|
||
|
|
require(until([&]{return fake.stops.load()>0;}),"release did not dispatch stopMotion");
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(fake.mutex);
|
||
|
|
require(fake.last.header().device_id()=="arm-a","wrong device in movement");
|
||
|
|
require(fake.last.base_frame()=="base" && fake.last.tcp_frame()=="tcp","local name leaked into controller request");
|
||
|
|
require(fake.last.target().y()>.2 && std::abs(fake.last.target().x()-.4)<1e-9,"tool orientation did not affect actual movement target");
|
||
|
|
}
|
||
|
|
const int afterStop=fake.moves.load(); QTest::qWait(180);
|
||
|
|
require(fake.moves.load()==afterStop,"movement continued after release");
|
||
|
|
Pose before; { std::lock_guard<std::mutex> lock(fake.mutex); before=fake.current; }
|
||
|
|
window.startCartesianJog(3,1);
|
||
|
|
require(until([&]{return fake.moves.load()>afterStop;}),"rotation not dispatched");
|
||
|
|
window.stopArmJog();
|
||
|
|
Pose selectedBefore{}, selectedAfter{}, after;
|
||
|
|
{ std::lock_guard<std::mutex> lock(fake.mutex); after=fake.current; }
|
||
|
|
toolPose(before,a.offset,selectedBefore); toolPose(after,a.offset,selectedAfter);
|
||
|
|
for(int i=0;i<3;++i) require(std::abs(selectedBefore[size_t(i)]-selectedAfter[size_t(i)])<1e-9,"rotation moved the selected TCP endpoint");
|
||
|
|
const int beforeSwitch=fake.moves.load();
|
||
|
|
window.startCartesianJog(0,1);
|
||
|
|
window.m_armIdEdit->setText("arm-b");
|
||
|
|
QTest::qWait(180);
|
||
|
|
require(fake.moves.load()==beforeSwitch,"pending request moved after robot switch");
|
||
|
|
auto *confirmed=window.m_tcpCalibration->findChild<QCheckBox *>("tcpSourceConfirmed");
|
||
|
|
window.m_tcpCalibration->findChild<QDoubleSpinBox *>("tcpSource0")->setValue(100);
|
||
|
|
confirmed->setChecked(true);
|
||
|
|
require(window.m_tcpCalibration->referenceConfirmed(),"nonzero reference not confirmed");
|
||
|
|
bool invalidBeforeDispatch=false;
|
||
|
|
QObject::connect(window.m_client,&RobotClient::moveJPoseRequested,&window,[&] {
|
||
|
|
invalidBeforeDispatch=!window.m_tcpCalibration->referenceConfirmed();
|
||
|
|
});
|
||
|
|
const int beforeOrdinaryMove=fake.moves.load();
|
||
|
|
QVariantMap ordinary{{"x",.4},{"y",.2},{"z",.5},{"rx",0.},{"ry",0.},{"rz",0.}};
|
||
|
|
window.m_client->requestMoveJPose("arm-b",ordinary,"base","tool0",.01,.1);
|
||
|
|
require(invalidBeforeDispatch,"ordinary MoveL left a stale active TCP confirmation");
|
||
|
|
require(until([&]{return fake.moves.load()>beforeOrdinaryMove;}),"ordinary MoveL regression");
|
||
|
|
confirmed->setChecked(true);
|
||
|
|
window.m_client->requestRpc("cmvr.api.ArmService","moveL","{}","arm-b");
|
||
|
|
require(!window.m_tcpCalibration->referenceConfirmed(),"raw Arm RPC left a stale reference");
|
||
|
|
window.toggleConnection();
|
||
|
|
require(until([&]{return !window.m_connected;}),"disconnect failed");
|
||
|
|
std::puts("PASS: per-robot dropdown, save propagation, real loopback MoveL targets, TCP pivot, stop, robot switch and active TCP invalidation before ordinary MoveL/RPC");
|
||
|
|
} catch(const std::exception &error) {
|
||
|
|
std::fprintf(stderr,"FAIL: %s\n",error.what()); server->Shutdown(); return 1;
|
||
|
|
}
|
||
|
|
server->Shutdown(); return 0;
|
||
|
|
}
|