CmvrControlStation/tests/handeyemotion_test.cpp

200 lines
13 KiB
C++
Raw Permalink Normal View History

#include "../src/handeyemotionwidget.h"
#include <QtTest>
#include <QComboBox>
#include <QCheckBox>
#include <QFile>
#include <QXmlStreamReader>
#include <QDoubleSpinBox>
#include <QLineEdit>
#include <QPushButton>
#include <QSettings>
#include <QScrollArea>
#include <QScrollBar>
#include <QDir>
#include <QTemporaryDir>
#include <cmath>
class HandEyeMotionTest : public QObject {
Q_OBJECT
QTemporaryDir m_dir;
void seed(const QString &name, const QString &mode, const QString &robot="r") {
HandEyeStore::Record record;
record.name=name; record.mode=mode; record.cameraId="camera-"+name;
record.method="PARK"; record.createdAt="2026-09-30T12:00:00Z";
record.sampleCount=8; record.metrics={{"accepted",true}};
record.transform={{0,-1,0,.1, 1,0,0,.2, 0,0,1,.3, 0,0,0,1}};
QSettings settings; QString error;
QVERIFY2(HandEyeStore::save(settings,"s",robot,record,false,error),qPrintable(error));
}
static void point(HandEyeMotionWidget &widget) {
const double values[]={400,500,600};
for(int i=0;i<3;++i) widget.findChild<QDoubleSpinBox *>(QString("handEyeCameraPoint%1").arg(i))->setValue(values[i]);
}
static QPushButton *button(HandEyeMotionWidget &widget) {
return widget.findChild<QPushButton *>("handEyeConvert");
}
static QVariantMap pose() {
return {{"x",1.0},{"y",2.0},{"z",3.5},{"rx",0.0},{"ry",0.0},{"rz",1.5707963267948966}};
}
static QVariantMap state() { return {{"connected",true},{"moving",false},{"fault",false}}; }
private slots:
void initTestCase() {
QVERIFY(m_dir.isValid());
QCoreApplication::setOrganizationName("CMVR-HandEye-Motion-Test");
QCoreApplication::setApplicationName("Motion");
QSettings::setDefaultFormat(QSettings::IniFormat);
QSettings::setPath(QSettings::IniFormat,QSettings::UserScope,m_dir.path());
}
void init() { QSettings s; s.clear(); s.sync(); }
void compactPanelDoesNotOverlap() {
QFile style(QFileInfo(QString::fromUtf8(__FILE__)).dir().filePath("../styles.qss"));
QVERIFY(style.open(QIODevice::ReadOnly));
seed("Camera_01","eye_to_hand");
HandEyeMotionWidget widget;
widget.setStyleSheet(QString::fromUtf8(style.readAll()));
widget.setContext("s","r",false,false,false,{});
widget.resize(430,400); widget.show(); QTest::qWait(50);
auto *scroll=widget.findChild<QScrollArea *>(); QVERIFY(scroll);
QVERIFY(scroll->verticalScrollBar()->maximum()>0);
QCOMPARE(scroll->horizontalScrollBar()->maximum(),0);
auto *input=widget.findChild<QDoubleSpinBox *>("handEyeCameraPoint0");
auto *output=widget.findChild<QLineEdit *>("handEyeBasePoint0");
QVERIFY(input->geometry().bottom()<output->geometry().top());
QVERIFY(input->height()>=input->minimumSizeHint().height());
QVERIFY(widget.grab().save("motion-urdf-top.png"));
auto *update=widget.findChild<QPushButton *>("handEyeUpdateUrdf");
scroll->ensureWidgetVisible(update); QTest::qWait(20);
QVERIFY(scroll->viewport()->rect().intersects(QRect(update->mapTo(scroll->viewport(),QPoint()),update->size())));
QVERIFY(widget.grab().save("motion-urdf-bottom.png"));
}
void eyeToHandAndSelectionPersistence() {
seed("fixed","eye_to_hand"); seed("moving","eye_in_hand"); seed("other","eye_to_hand","other");
HandEyeMotionWidget widget; widget.setContext("s","r",false,true,false,{});
auto *combo=widget.findChild<QComboBox *>("handEyeMotionProfiles"); QVERIFY(combo); QCOMPARE(combo->count(),2);
combo->setCurrentIndex(0); QVERIFY(combo->currentText().contains("camera-fixed"));
QSignalSpy capture(&widget,&HandEyeMotionWidget::captureRequested);
QSignalSpy prepared(&widget,&HandEyeMotionWidget::basePointPrepared);
point(widget); QVERIFY(button(widget)->isEnabled()); button(widget)->click();
QCOMPARE(capture.size(),0); QCOMPARE(prepared.size(),1);
QCOMPARE(prepared.first().at(0).toString(),QString("camera-fixed"));
const auto values=prepared.first().at(2).toList();
QVERIFY(std::abs(values[0].toDouble()+.4)<1e-12); QVERIFY(std::abs(values[1].toDouble()-.6)<1e-12);
QVERIFY(std::abs(values[2].toDouble()-.9)<1e-12);
auto *result=widget.findChild<QLineEdit *>("handEyeBasePoint0"); QVERIFY(!result->text().isEmpty()); QVERIFY(result->isReadOnly());
widget.findChild<QDoubleSpinBox *>("handEyeCameraPoint0")->setValue(401); QVERIFY(result->text().isEmpty());
QSettings settings; HandEyeStore::Snapshot snapshot; QString error;
QVERIFY(HandEyeStore::load(settings,"s","r",snapshot,error)); QCOMPARE(snapshot.current,QString("fixed"));
widget.setContext("s","other",true,false,true,{}); QCOMPARE(combo->count(),1); QVERIFY(combo->currentText().contains("other"));
}
void eyeInHandUsesFreshPoseAndActiveOffset() {
seed("moving","eye_in_hand"); HandEyeMotionWidget widget;
widget.setContext("s","r",true,false,true,{{0,0,.5,0,0,0}}); point(widget);
QSignalSpy capture(&widget,&HandEyeMotionWidget::captureRequested);
QSignalSpy prepared(&widget,&HandEyeMotionWidget::basePointPrepared);
button(widget)->click(); QCOMPARE(capture.size(),1); QCOMPARE(prepared.size(),0);
const quint64 token=capture.first().at(1).toULongLong(); QVERIFY(token & (quint64(1)<<61));
widget.acceptPose(token,"wrong",pose(),state(),true,{}); QCOMPARE(prepared.size(),0);
widget.acceptPose(token,"r",pose(),state(),true,{}); QCOMPARE(prepared.size(),1);
const auto values=prepared.first().at(2).toList();
QVERIFY(std::abs(values[0].toDouble()-.4)<1e-12); QVERIFY(std::abs(values[1].toDouble()-1.6)<1e-12);
QVERIFY(std::abs(values[2].toDouble()-3.9)<1e-12);
widget.acceptPose(token,"r",pose(),state(),true,{}); QCOMPARE(prepared.size(),1);
widget.setContext("s","r",false,false,true,{{0,0,.5,0,0,0}});
QVERIFY(widget.findChild<QLineEdit *>("handEyeBasePoint0")->text().isEmpty()); QVERIFY(!button(widget)->isEnabled());
}
void lateResponsesInvalidated_data() {
QTest::addColumn<int>("change");
QTest::newRow("input")<<0; QTest::newRow("selection")<<1; QTest::newRow("robot")<<2;
QTest::newRow("reference")<<3; QTest::newRow("offset")<<4; QTest::newRow("reload")<<5;
QTest::newRow("disconnect")<<6; QTest::newRow("demo")<<7;
}
void lateResponsesInvalidated() {
QFETCH(int,change); seed("fixed","eye_to_hand"); seed("moving","eye_in_hand");
HandEyeMotionWidget widget; widget.setContext("s","r",true,false,true,{}); point(widget);
QSignalSpy capture(&widget,&HandEyeMotionWidget::captureRequested);
QSignalSpy prepared(&widget,&HandEyeMotionWidget::basePointPrepared);
button(widget)->click(); QCOMPARE(capture.size(),1); const auto token=capture.first().at(1).toULongLong();
switch(change) {
case 0:widget.findChild<QDoubleSpinBox *>("handEyeCameraPoint0")->setValue(12);break;
case 1:widget.findChild<QComboBox *>("handEyeMotionProfiles")->setCurrentIndex(0);break;
case 2:widget.setContext("s","other",true,false,true,{});break;
case 3:widget.setContext("s","r",true,false,false,{});break;
case 4:widget.setContext("s","r",true,false,true,{{0,0,.1,0,0,0}});break;
case 5:widget.reload();break;
case 6:widget.setContext("s","r",false,false,true,{});break;
case 7:widget.setContext("s","r",true,true,true,{});break;
}
widget.acceptPose(token,"r",pose(),state(),true,{}); QCOMPARE(prepared.size(),0);
QVERIFY(widget.findChild<QLineEdit *>("handEyeBasePoint0")->text().isEmpty());
}
void savedProfileUrdfExport_data() {
QTest::addColumn<QString>("mode"); QTest::addColumn<bool>("newCamera");
QTest::newRow("EIH-existing")<<QString("eye_in_hand")<<false;
QTest::newRow("ETH-existing")<<QString("eye_to_hand")<<false;
QTest::newRow("EIH-new")<<QString("eye_in_hand")<<true;
QTest::newRow("ETH-new")<<QString("eye_to_hand")<<true;
}
void savedProfileUrdfExport() {
QFETCH(QString,mode); QFETCH(bool,newCamera); seed("saved",mode);
QTemporaryDir files; const QString source=files.filePath("source.urdf"),target=files.filePath("export.urdf");
QFile input(source); QVERIFY(input.open(QIODevice::WriteOnly));
const QByteArray xml=newCamera ? QByteArray("<robot name='r'><link name='reference'/></robot>")
: QByteArray("<robot name='r'><link name='reference'/><link name='optical'/><joint name='camera' type='fixed'><parent link='reference'/><child link='optical'/></joint></robot>");
QCOMPARE(input.write(xml),qint64(xml.size())); input.close();
HandEyeMotionWidget widget; widget.setContext("s","r",false,false,false,{}); widget.setUrdfFile(source);
auto *profiles=widget.findChild<QComboBox *>("handEyeMotionProfiles"); QVERIFY(profiles); QCOMPARE(profiles->currentData().toString(),QString("saved"));
auto *reference=widget.findChild<QComboBox *>("handEyeUrdfReference"); QVERIFY(reference);
auto *camera=widget.findChild<QComboBox *>("handEyeUrdfCamera"); QVERIFY(camera);
auto *confirmed=widget.findChild<QCheckBox *>("handEyeUrdfConfirmed"); QVERIFY(confirmed);
auto *exportButton=widget.findChild<QPushButton *>("handEyeUpdateUrdf"); QVERIFY(exportButton);
reference->setCurrentIndex(reference->findData("reference")); camera->setEditText("optical");
QString error; QSignalSpy updated(&widget,&HandEyeMotionWidget::urdfUpdated);
QSignalSpy capture(&widget,&HandEyeMotionWidget::captureRequested);
QVERIFY(!exportButton->isEnabled()); QVERIFY(!widget.exportUrdf(target,error)); QVERIFY(!QFile::exists(target));
confirmed->setChecked(true); QVERIFY(exportButton->isEnabled());
QVERIFY2(widget.exportUrdf(target,error),qPrintable(error)); QCOMPARE(updated.size(),1); QCOMPARE(capture.size(),0);
QCOMPARE(updated[0][0].toString(),QString("s")); QCOMPARE(updated[0][1].toString(),QString("r")); QCOMPARE(updated[0][2].toString(),target);
QFile output(target); QVERIFY(output.open(QIODevice::ReadOnly)); QXmlStreamReader reader(output.readAll());
TcpCalibration::Pose p{}; bool found=false;
while(!reader.atEnd()) { reader.readNext(); if(reader.isStartElement() && reader.name()=="origin") {
int offset=0; for(const auto &field:{"xyz","rpy"}) { const auto values=reader.attributes().value(field).toString().split(' ');
QCOMPARE(values.size(),3); for(int i=0;i<3;++i) p[offset+i]=values[i].toDouble(); offset=3; } found=true; } }
QVERIFY(!reader.hasError()); QVERIFY(found); TcpCalibration::Matrix actual{}; QVERIFY(TcpCalibration::poseMatrix(p,actual));
QSettings settings; HandEyeStore::Snapshot saved; QVERIFY(HandEyeStore::load(settings,"s","r",saved,error));
for(int i=0;i<16;++i) QVERIFY(std::abs(actual[i]-saved.records[0].transform[i])<1e-10);
QVERIFY(input.open(QIODevice::ReadOnly)); QCOMPARE(input.readAll(),xml);
}
void urdfConfirmationInvalidated_data() {
QTest::addColumn<int>("change"); QTest::newRow("profile")<<0; QTest::newRow("robot")<<1;
QTest::newRow("endpoint")<<2; QTest::newRow("reference")<<3; QTest::newRow("camera")<<4; QTest::newRow("source")<<5;
}
void urdfConfirmationInvalidated() {
QFETCH(int,change); seed("first","eye_to_hand"); seed("second","eye_in_hand"); seed("other","eye_to_hand","other");
QTemporaryDir files; const QString source=files.filePath("source.urdf"),target=files.filePath("export.urdf");
QFile input(source); QVERIFY(input.open(QIODevice::WriteOnly)); input.write("<robot name='r'><link name='base'/></robot>"); input.close();
HandEyeMotionWidget widget; widget.setContext("s","r",false,false,false,{}); widget.setUrdfFile(source);
auto *profiles=widget.findChild<QComboBox *>("handEyeMotionProfiles"); profiles->setCurrentIndex(0);
auto *reference=widget.findChild<QComboBox *>("handEyeUrdfReference"); reference->setCurrentIndex(reference->findData("base"));
auto *camera=widget.findChild<QComboBox *>("handEyeUrdfCamera"); camera->setEditText("optical");
auto *confirmed=widget.findChild<QCheckBox *>("handEyeUrdfConfirmed"); confirmed->setChecked(true);
auto *exportButton=widget.findChild<QPushButton *>("handEyeUpdateUrdf"); QVERIFY(exportButton->isEnabled());
switch(change) {
case 0: profiles->setCurrentIndex(1); break;
case 1: widget.setContext("s","other",false,false,false,{}); break;
case 2: widget.setContext("different","r",false,false,false,{}); break;
case 3: reference->setCurrentIndex(0); break;
case 4: camera->setEditText("new_optical"); break;
case 5: widget.setUrdfFile(source); break;
}
QVERIFY(!confirmed->isChecked()); QVERIFY(!exportButton->isEnabled());
QString error; QSignalSpy updated(&widget,&HandEyeMotionWidget::urdfUpdated);
QVERIFY(!widget.exportUrdf(target,error)); QVERIFY(!QFile::exists(target)); QCOMPARE(updated.size(),0);
if(change==1 || change==2) {
QVERIFY(widget.findChild<QLineEdit *>("handEyeUrdfPath")->text().isEmpty());
confirmed->setChecked(true); QVERIFY(!exportButton->isEnabled()); QVERIFY(!widget.exportUrdf(target,error));
}
}
};
QTEST_MAIN(HandEyeMotionTest)
#include "handeyemotion_test.moc"