CmvrControlStation/tests/jointjog_restore_test.cpp

120 lines
7.5 KiB
C++
Raw Permalink Normal View History

// Only an ephemeral localhost arm is used; no physical motion is possible.
#include <QtTest>
#include <QApplication>
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSettings>
#include <QSlider>
#include <QDoubleSpinBox>
#include <QTemporaryDir>
#include <QTableWidget>
#include <atomic>
#include <limits>
#include <cstring>
#include <google/protobuf/unknown_field_set.h>
#include <grpcpp/grpcpp.h>
#include "cmvr/api/arm_service.grpc.pb.h"
#define private public
#include "../src/mainwindow.h"
#undef private
#include "../src/robotclient.h"
class LegacyArm final : public cmvr::api::ArmService::Service {
public:
std::atomic<int> currentMode{0};
// Older services expose the ordered position array but no GetArmInfo/names.
grpc::Status getRobotState(grpc::ServerContext *,const cmvr::api::GetRobotState::Request *,
cmvr::api::GetRobotState::Response *reply) override {
reply->mutable_header()->set_success(true);
auto *state=reply->mutable_state(); state->set_connected(true); state->set_powered_on(true);
for(int i=0;i<6;++i) { state->mutable_actual_joint_state()->add_position(.1*(i+1)); state->mutable_actual_joint_state()->add_velocity(0); }
const int mode=currentMode.load();
if(mode) {
auto *joints=state->mutable_actual_joint_state();
const double values[]={0.0,-125.0,320.0,std::numeric_limits<double>::quiet_NaN(),std::numeric_limits<double>::infinity()};
for(double value:values) {
const auto *field=joints->GetDescriptor()->FindFieldByNumber(6);
if(field) joints->GetReflection()->AddDouble(joints,field,value);
else {
quint64 bits;std::memcpy(&bits,&value,sizeof(bits));
joints->GetReflection()->MutableUnknownFields(joints)->AddFixed64(6,bits);
}
}
}
return grpc::Status::OK;
}
};
static bool waitFor(const std::function<bool()> &condition,int timeout=2500) {
QElapsedTimer timer; timer.start();
while(!condition() && timer.elapsed()<timeout) QTest::qWait(10);
return condition();
}
static void require(bool ok,const char *message) { if(!ok) throw std::runtime_error(message); }
int main(int argc,char **argv) {
QApplication app(argc,argv); QTemporaryDir settings;
QCoreApplication::setOrganizationName("CMVR-JointJog-Restore-Test"); QCoreApplication::setApplicationName("Test");
QSettings::setDefaultFormat(QSettings::IniFormat);
QSettings::setPath(QSettings::IniFormat,QSettings::UserScope,settings.path());
QSettings().setValue("connection/demo",false);
LegacyArm service; grpc::ServerBuilder builder; int port=0;
builder.AddListeningPort("127.0.0.1:0",grpc::InsecureServerCredentials(),&port); builder.RegisterService(&service);
auto server=builder.BuildAndStart(); if(!server || !port) return 2;
int result=0;
{
MainWindow window;
try {
window.m_armIdEdit->setText("legacy_arm");
window.m_client->connectToServer("127.0.0.1",port,false);
require(waitFor([&] { return window.m_connected; }),"loopback connection failed");
window.m_client->setArmTelemetry("legacy_arm",true);
require(waitFor([&] { return window.m_jointJogPlusButtons.size()==6; }),"joint rows remain empty without GetArmInfo");
require(window.m_jointJogPositionLabels[2]->text().contains("0.3000"),"live joint position was not displayed");
require(window.m_jointTable->columnCount()==5,"joint current column is missing");
require(window.m_jointTable->item(0,4)->text()==QStringLiteral("未提供"),"legacy missing current looks like a real zero");
service.currentMode=1;
require(waitFor([&] { return window.m_jointTable->item(2,4)->text()==QStringLiteral("320"); }),"real current not displayed in mA");
require(window.m_jointTable->item(0,4)->text()==QStringLiteral("0"),"real zero current lost");
require(window.m_jointTable->item(1,4)->text()==QStringLiteral("-125"),"signed current lost");
require(window.m_jointTable->item(3,4)->text()==QStringLiteral("无效") && window.m_jointTable->item(4,4)->text()==QStringLiteral("无效"),"nonfinite current displayed as valid");
require(window.m_jointTable->item(5,4)->text()==QStringLiteral("未提供"),"partial current array filled with zero");
service.currentMode=0;
require(waitFor([&] { return window.m_jointTable->item(2,4)->text()==QStringLiteral("未提供"); }),"stale current survived omitted data");
QPointer<QPushButton> original=window.m_jointJogPlusButtons[2];
window.updateArmInfo("legacy_arm",{{"jointNames",QStringList{}}});
require(window.m_jointJogPlusButtons.size()==6 && original,"empty info erased telemetry-derived rows");
QSignalSpy speeds(window.m_client,&RobotClient::speedJRequested);
QSignalSpy stops(window.m_client,&RobotClient::armStopRequested);
window.m_jogSpeedSlider->setValue(28);
QMetaObject::invokeMethod(original,"pressed",Qt::DirectConnection);
require(speeds.size()==1,"press did not request SpeedJ");
const auto velocity=speeds.last()[1].toList();
require(velocity.size()==6 && qAbs(velocity[2].toDouble()-window.m_velocitySpin->value()*.28)<1e-9,"wrong joint speed or multiplier");
for(int i=0;i<6;++i) if(i!=2) require(velocity[i].toDouble()==0,"another joint received speed");
QMetaObject::invokeMethod(original,"released",Qt::DirectConnection);
require(!stops.isEmpty(),"release did not request stop");
window.m_client->setArmTelemetry("legacy_arm",false);
QTest::qWait(100);
const QStringList names={"axis1","axis2","axis3","axis4","axis5","axis6"};
window.updateArmInfo("legacy_arm",{{"jointNames",names}});
QPointer<QPushButton> refreshed=window.m_jointJogPlusButtons[0];
window.updateArmInfo("legacy_arm",{{"jointNames",names}});
QCoreApplication::sendPostedEvents(nullptr,QEvent::DeferredDelete);
require(refreshed && window.m_jointJogPlusButtons[0]==refreshed,"unchanged info destroys held jog controls");
window.updateArmInfo("another_arm",{{"jointNames",QStringList{"wrong"}}});
require(window.m_activeArmJointNames==names,"another robot changed this robot's controls");
window.updateArmInfo("legacy_arm",{{"jointNames",QStringList{"axis1","axis2","axis3","axis4"}}});
require(window.m_jointJogPlusButtons.size()==4,"joint count was fixed to six");
QMetaObject::invokeMethod(window.m_jointJogMinusButtons[1],"pressed",Qt::DirectConnection);
const auto negative=speeds.last()[1].toList();
require(negative.size()==4 && negative[1].toDouble()<0,"negative jog does not use the current robot's joint count");
QMetaObject::invokeMethod(window.m_jointJogMinusButtons[1],"released",Qt::DirectConnection);
std::printf("PASS: legacy telemetry restores rows, live angles, SpeedJ ratio, release stop, stable refresh and robot isolation\n");
} catch(const std::exception &error) { std::fprintf(stderr,"FAIL: %s\n",error.what()); result=1; }
window.m_client->disconnectFromServer(); waitFor([&] { return !window.m_connected; });
for(auto *button:window.m_jointJogPlusButtons) if(button->isEnabled()) result=1;
}
server->Shutdown(); server->Wait(); return result;
}