// Only an ephemeral localhost arm is used; no physical motion is possible. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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 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::quiet_NaN(),std::numeric_limits::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 &condition,int timeout=2500) { QElapsedTimer timer; timer.start(); while(!condition() && timer.elapsed()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 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 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; }