feat(arm): expose teaching RPCs and fix AUBO power state sequencing

This commit is contained in:
linbo 2026-10-26 17:34:29 +08:00
parent dcf89b34d6
commit fa3f70a975
7 changed files with 478 additions and 12 deletions

View File

@ -92,6 +92,11 @@ public:
Result playProgram() override;
Result pauseProgram() override;
Result stopProgram() override;
Result enterTeachingMode() override;
Result startTrajectoryRecording(const std::string& file_name) override;
Result completeTeaching() override;
Result stopTrajectoryRecording() override;
Result exitTeachingMode() override;
std::vector<double> ik(const std::string& base_link,
const std::string& ee_link,

View File

@ -627,14 +627,38 @@ ArmState AuboArm::getRobotState() const
{
ArmState state;
state.connected = connected_.load();
state.powered_on = state.connected;
state.brake_released = state.connected;
// A live RPC connection does not imply that the robot has power or that
// startup has released its brakes. Read both from the controller mode.
state.powered_on = false;
state.brake_released = false;
state.moving = busy_.load();
state.robot_mode = getRobotMode();
state.safety_mode = getSafetyMode();
state.control_mode = getControlMode();
state.emergency_stopped = isEmergencyStopped();
state.fault = isFault();
#if defined(CMVR_HAS_AUBO_SDK)
if (state.connected && sdk_ && sdk_->rpc_client) {
try {
const auto names = sdk_->rpc_client->getRobotNames();
if (names.empty()) {
throw std::runtime_error("robot name list is empty");
}
const auto robot = sdk_->rpc_client->getRobotInterface(names.front());
using Mode = arcs::common_interface::RobotModeType;
const auto mode = robot->getRobotState()->getRobotModeType();
state.powered_on = mode == Mode::PowerOn || mode == Mode::Idle ||
mode == Mode::BrakeReleasing || mode == Mode::BackDrive ||
mode == Mode::Running;
state.brake_released = mode == Mode::Running || mode == Mode::BackDrive;
state.fault = state.fault || mode == Mode::Error;
} catch (const std::exception& e) {
state.robot_mode = RobotMode::Unknown;
state.fault = true;
CMVR_LOG(ERROR) << "[AuboArm] read controller power state failed: " << e.what();
}
}
#endif
state.speed_scaling = speed_scaling_;
state.actual_joint_state = getJointState();
state.target_joint_state = state.actual_joint_state;
@ -722,7 +746,35 @@ RobotMode AuboArm::getRobotMode() const
if (isEmergencyStopped()) {
return RobotMode::Stopped;
}
return busy_.load() ? RobotMode::Running : RobotMode::Idle;
#if defined(CMVR_HAS_AUBO_SDK)
try {
const auto names = sdk_->rpc_client->getRobotNames();
if (names.empty()) {
return RobotMode::Unknown;
}
using Mode = arcs::common_interface::RobotModeType;
const auto mode = sdk_->rpc_client->getRobotInterface(names.front())
->getRobotState()->getRobotModeType();
switch (mode) {
case Mode::NoController:
case Mode::Disconnected: return RobotMode::Disconnected;
case Mode::ConfirmSafety:
case Mode::PowerOff:
case Mode::PowerOffing: return RobotMode::PowerOff;
case Mode::Error: return RobotMode::Fault;
case Mode::Running:
return busy_.load() ? RobotMode::Running : RobotMode::Idle;
case Mode::Idle:
case Mode::BackDrive: return RobotMode::Idle;
default: return RobotMode::Unknown;
}
} catch (const std::exception& e) {
CMVR_LOG(ERROR) << "[AuboArm] getRobotMode failed: " << e.what();
return RobotMode::Unknown;
}
#else
return RobotMode::Unknown;
#endif
}
SafetyMode AuboArm::getSafetyMode() const
@ -904,16 +956,54 @@ Result AuboArm::torqueOn()
std::vector<double> inertia(6, 0.0);
robot_interface->getRobotConfig()->setPayload(mass, cog, aom, inertia);
if (robot_interface->getRobotState()->getRobotModeType() !=
arcs::common_interface::RobotModeType::Running) {
const int power_on_ret = robot_interface->getRobotManage()->poweron();
if (power_on_ret != arcs::common_interface::AUBO_OK) {
return Result::failure(
ArmErrorCode::CommandFailed,
"[AuboArm] poweron failed: sdk ret=" +
std::to_string(power_on_ret));
using Mode = arcs::common_interface::RobotModeType;
const auto read_mode = [&]() {
return robot_interface->getRobotState()->getRobotModeType();
};
// Submit each command at most once. The controller's state, not a
// fixed sleep, determines when the next startup stage is permitted.
const auto wait_for = [&](Mode target, const char* stage) -> Result {
const auto deadline = std::chrono::steady_clock::now() +
std::chrono::seconds(15);
for (;;) {
const auto mode = read_mode();
if (mode == target || mode == Mode::Running) {
return Result::success();
}
if (mode == Mode::Error || mode == Mode::Disconnected ||
mode == Mode::NoController || isEmergencyStopped()) {
return Result::failure(ArmErrorCode::RobotNotReady,
std::string("[AuboArm] ") + stage +
" interrupted: controller mode=" + std::to_string(static_cast<int>(mode)));
}
if (std::chrono::steady_clock::now() >= deadline) {
return Result::failure(ArmErrorCode::CommandFailed,
std::string("[AuboArm] ") + stage +
" timed out: controller mode=" + std::to_string(static_cast<int>(mode)));
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
};
auto mode = read_mode();
if (mode != Mode::Running && mode != Mode::BrakeReleasing) {
if (mode == Mode::PowerOff) {
const int ret = robot_interface->getRobotManage()->poweron();
if (ret != arcs::common_interface::AUBO_OK) {
return Result::failure(ArmErrorCode::CommandFailed,
"[AuboArm] poweron failed: sdk ret=" + std::to_string(ret));
}
} else if (mode != Mode::Idle && mode != Mode::PowerOn && mode != Mode::Booting) {
return Result::failure(ArmErrorCode::RobotNotReady,
"[AuboArm] torqueOn rejected: controller mode=" +
std::to_string(static_cast<int>(mode)));
}
const auto powered = wait_for(Mode::Idle, "poweron");
if (!powered.ok()) {
return powered;
}
}
mode = read_mode();
if (mode == Mode::Idle) {
const int startup_ret = robot_interface->getRobotManage()->startup();
if (startup_ret != arcs::common_interface::AUBO_OK) {
return Result::failure(
@ -922,6 +1012,10 @@ Result AuboArm::torqueOn()
std::to_string(startup_ret));
}
}
const auto started = wait_for(Mode::Running, "startup");
if (!started.ok()) {
return started;
}
emergency_stopped_.store(false);
// A hardware emergency-stop latch is owned by the monitor. Do not
// clear it merely because poweron/startup accepted a request: motion
@ -1984,6 +2078,179 @@ Result AuboArm::stopProgram()
return unsupported_("stopProgram");
}
Result AuboArm::enterTeachingMode()
{
const auto ready = ensureConnected_("enterTeachingMode");
if (!ready.ok()) {
return ready;
}
#if defined(CMVR_HAS_AUBO_SDK)
try {
const auto robot_names = sdk_->rpc_client->getRobotNames();
if (robot_names.empty()) {
return Result::failure(ArmErrorCode::RobotNotReady,
"[AuboArm] robot name list is empty");
}
const auto robot_interface =
sdk_->rpc_client->getRobotInterface(robot_names.front());
if (!robot_interface || !robot_interface->getRobotManage()) {
return Result::failure(ArmErrorCode::RobotNotReady,
"[AuboArm] robot manage interface is unavailable");
}
const std::vector<int> free_axes(model_.dof, 1);
const int ret = robot_interface->getRobotManage()->handguideMode(
free_axes, {});
if (ret != arcs::common_interface::AUBO_OK) {
return Result::failure(
ArmErrorCode::CommandFailed,
"[AuboArm] handguideMode failed: sdk ret=" +
std::to_string(ret));
}
return Result::success();
} catch (const std::exception& e) {
return Result::failure(
ArmErrorCode::CommandFailed,
std::string("[AuboArm] enterTeachingMode failed: ") + e.what());
}
#else
return unsupported_("enterTeachingMode");
#endif
}
Result AuboArm::startTrajectoryRecording(const std::string& file_name)
{
if (file_name.empty()) {
return Result::failure(ArmErrorCode::InvalidArgument,
"trajectory file name must not be empty");
}
const auto ready = ensureConnected_("startTrajectoryRecording");
if (!ready.ok()) {
return ready;
}
#if defined(CMVR_HAS_AUBO_SDK)
try {
const auto robot_names = sdk_->rpc_client->getRobotNames();
if (robot_names.empty()) {
return Result::failure(ArmErrorCode::RobotNotReady,
"[AuboArm] robot name list is empty");
}
const auto robot_interface =
sdk_->rpc_client->getRobotInterface(robot_names.front());
if (!robot_interface || !robot_interface->getRobotManage()) {
return Result::failure(ArmErrorCode::RobotNotReady,
"[AuboArm] robot manage interface is unavailable");
}
const int ret =
robot_interface->getRobotManage()->startRecord(file_name);
if (ret != arcs::common_interface::AUBO_OK) {
return Result::failure(
ArmErrorCode::CommandFailed,
"[AuboArm] startRecord failed: sdk ret=" +
std::to_string(ret));
}
return Result::success();
} catch (const std::exception& e) {
return Result::failure(
ArmErrorCode::CommandFailed,
std::string("[AuboArm] startTrajectoryRecording failed: ") +
e.what());
}
#else
return unsupported_("startTrajectoryRecording");
#endif
}
Result AuboArm::stopTrajectoryRecording()
{
const auto ready = ensureConnected_("stopTrajectoryRecording");
if (!ready.ok()) {
return ready;
}
#if defined(CMVR_HAS_AUBO_SDK)
try {
const auto robot_names = sdk_->rpc_client->getRobotNames();
if (robot_names.empty()) {
return Result::failure(ArmErrorCode::RobotNotReady,
"[AuboArm] robot name list is empty");
}
const auto robot_interface =
sdk_->rpc_client->getRobotInterface(robot_names.front());
if (!robot_interface || !robot_interface->getRobotManage()) {
return Result::failure(ArmErrorCode::RobotNotReady,
"[AuboArm] robot manage interface is unavailable");
}
const int ret = robot_interface->getRobotManage()->stopRecord();
if (ret != arcs::common_interface::AUBO_OK) {
return Result::failure(
ArmErrorCode::CommandFailed,
"[AuboArm] stopRecord failed: sdk ret=" +
std::to_string(ret));
}
return Result::success();
} catch (const std::exception& e) {
return Result::failure(
ArmErrorCode::CommandFailed,
std::string("[AuboArm] stopTrajectoryRecording failed: ") +
e.what());
}
#else
return unsupported_("stopTrajectoryRecording");
#endif
}
Result AuboArm::exitTeachingMode()
{
const auto ready = ensureConnected_("exitTeachingMode");
if (!ready.ok()) {
return ready;
}
#if defined(CMVR_HAS_AUBO_SDK)
try {
const auto robot_names = sdk_->rpc_client->getRobotNames();
if (robot_names.empty()) {
return Result::failure(ArmErrorCode::RobotNotReady,
"[AuboArm] robot name list is empty");
}
const auto robot_interface =
sdk_->rpc_client->getRobotInterface(robot_names.front());
if (!robot_interface || !robot_interface->getRobotManage()) {
return Result::failure(ArmErrorCode::RobotNotReady,
"[AuboArm] robot manage interface is unavailable");
}
const int ret =
robot_interface->getRobotManage()->exitHandguideMode();
if (ret != arcs::common_interface::AUBO_OK) {
return Result::failure(
ArmErrorCode::CommandFailed,
"[AuboArm] exitHandguideMode failed: sdk ret=" +
std::to_string(ret));
}
return Result::success();
} catch (const std::exception& e) {
return Result::failure(
ArmErrorCode::CommandFailed,
std::string("[AuboArm] exitTeachingMode failed: ") + e.what());
}
#else
return unsupported_("exitTeachingMode");
#endif
}
Result AuboArm::completeTeaching()
{
const auto stop_result = stopTrajectoryRecording();
const auto exit_result = exitTeachingMode();
if (!stop_result.ok() && !exit_result.ok()) {
return Result::failure(
ArmErrorCode::CommandFailed,
stop_result.message + "; " + exit_result.message);
}
if (!stop_result.ok()) {
return stop_result;
}
return exit_result;
}
std::vector<double> AuboArm::ik(const std::string& base_link,
const std::string& ee_link,
const CartesianPose& pose)

View File

@ -138,6 +138,40 @@ public:
virtual Result pauseProgram() = 0;
virtual Result stopProgram() = 0;
// Teaching and trajectory recording are optional vendor capabilities.
// Drivers that do not explicitly override these methods report unsupported.
virtual Result enterTeachingMode()
{
return Result::failure(
ArmErrorCode::UnsupportedCommand,
"teaching mode is not supported by this robot arm");
}
virtual Result startTrajectoryRecording(const std::string& file_name)
{
(void)file_name;
return Result::failure(
ArmErrorCode::UnsupportedCommand,
"trajectory recording is not supported by this robot arm");
}
virtual Result completeTeaching()
{
return Result::failure(
ArmErrorCode::UnsupportedCommand,
"complete teaching is not supported by this robot arm");
}
virtual Result stopTrajectoryRecording()
{
return Result::failure(
ArmErrorCode::UnsupportedCommand,
"trajectory recording is not supported by this robot arm");
}
virtual Result exitTeachingMode()
{
return Result::failure(
ArmErrorCode::UnsupportedCommand,
"teaching mode is not supported by this robot arm");
}
virtual std::vector<double> ik(const std::string& base_link,
const std::string& ee_link,
const CartesianPose& pose) = 0;

View File

@ -69,6 +69,26 @@ public:
grpc::ServerContext* context,
const api::JsonDeviceCommand_Request* request,
api::JsonDeviceCommand_Feedback* response) override;
grpc::Status EnterTeachingMode(
grpc::ServerContext* context,
const api::CommandHeader_Request* request,
api::CommandHeader_Feedback* response) override;
grpc::Status StartTrajectoryRecording(
grpc::ServerContext* context,
const api::StartTrajectoryRecording_Request* request,
api::StartTrajectoryRecording_Response* response) override;
grpc::Status CompleteTeaching(
grpc::ServerContext* context,
const api::CommandHeader_Request* request,
api::CommandHeader_Feedback* response) override;
grpc::Status StopTrajectoryRecording(
grpc::ServerContext* context,
const api::CommandHeader_Request* request,
api::CommandHeader_Feedback* response) override;
grpc::Status ExitTeachingMode(
grpc::ServerContext* context,
const api::CommandHeader_Request* request,
api::CommandHeader_Feedback* response) override;
private:
device::DeviceManager& dmgr_;

View File

@ -25,6 +25,17 @@ grpc::Status resultToStatus(const device::Result& result)
if (result.ok()) {
return grpc::Status::OK;
}
if (result.code == device::ArmErrorCode::UnsupportedCommand) {
return grpc::Status(grpc::StatusCode::UNIMPLEMENTED, result.message);
}
if (result.code == device::ArmErrorCode::InvalidArgument) {
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, result.message);
}
if (result.code == device::ArmErrorCode::NotConnected ||
result.code == device::ArmErrorCode::RobotNotReady) {
return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION,
result.message);
}
return grpc::Status(grpc::StatusCode::INTERNAL, result.message);
}
@ -729,4 +740,108 @@ grpc::Status gRPCArmServiceImpl::ExecuteJsonCommand(
}
}
grpc::Status gRPCArmServiceImpl::EnterTeachingMode(
grpc::ServerContext*,
const api::CommandHeader_Request* request,
api::CommandHeader_Feedback* response)
{
auto control_lease = ControlCommandArbiter::instance().tryAcquireControl();
if (!control_lease) return setControlBusy(response);
try {
const std::string device_id = request->device_id();
auto arm = dmgr_.getDevice<device::RobotArm>(device_id);
if (!arm) return setDeviceNotFound(response, device_id);
const auto result = arm->enterTeachingMode();
fillFeedback(response, result.ok(), result.ok() ? "" : result.message);
if (result.ok()) logRpcSuccess("EnterTeachingMode", device_id);
return resultToStatus(result);
} catch (const std::exception& e) {
fillFeedback(response, false, e.what());
return grpc::Status(grpc::StatusCode::INTERNAL, e.what());
}
}
grpc::Status gRPCArmServiceImpl::StartTrajectoryRecording(
grpc::ServerContext*,
const api::StartTrajectoryRecording_Request* request,
api::StartTrajectoryRecording_Response* response)
{
auto control_lease = ControlCommandArbiter::instance().tryAcquireControl();
if (!control_lease) return setControlBusy(response);
try {
const std::string device_id = request->header().device_id();
auto arm = dmgr_.getDevice<device::RobotArm>(device_id);
if (!arm) return setDeviceNotFound(response, device_id);
const auto result = arm->startTrajectoryRecording(request->file_name());
if (result.ok()) logRpcSuccess("StartTrajectoryRecording", device_id);
return setResponseResult(response, result);
} catch (const std::exception& e) {
fillFeedback(response->mutable_header(), false, e.what());
return grpc::Status(grpc::StatusCode::INTERNAL, e.what());
}
}
grpc::Status gRPCArmServiceImpl::CompleteTeaching(
grpc::ServerContext*,
const api::CommandHeader_Request* request,
api::CommandHeader_Feedback* response)
{
auto control_lease = ControlCommandArbiter::instance().tryAcquireControl();
if (!control_lease) return setControlBusy(response);
try {
const std::string device_id = request->device_id();
auto arm = dmgr_.getDevice<device::RobotArm>(device_id);
if (!arm) return setDeviceNotFound(response, device_id);
const auto result = arm->completeTeaching();
fillFeedback(response, result.ok(), result.ok() ? "" : result.message);
if (result.ok()) logRpcSuccess("CompleteTeaching", device_id);
return resultToStatus(result);
} catch (const std::exception& e) {
fillFeedback(response, false, e.what());
return grpc::Status(grpc::StatusCode::INTERNAL, e.what());
}
}
grpc::Status gRPCArmServiceImpl::StopTrajectoryRecording(
grpc::ServerContext*,
const api::CommandHeader_Request* request,
api::CommandHeader_Feedback* response)
{
auto control_lease = ControlCommandArbiter::instance().tryAcquireControl();
if (!control_lease) return setControlBusy(response);
try {
const std::string device_id = request->device_id();
auto arm = dmgr_.getDevice<device::RobotArm>(device_id);
if (!arm) return setDeviceNotFound(response, device_id);
const auto result = arm->stopTrajectoryRecording();
fillFeedback(response, result.ok(), result.ok() ? "" : result.message);
if (result.ok()) logRpcSuccess("StopTrajectoryRecording", device_id);
return resultToStatus(result);
} catch (const std::exception& e) {
fillFeedback(response, false, e.what());
return grpc::Status(grpc::StatusCode::INTERNAL, e.what());
}
}
grpc::Status gRPCArmServiceImpl::ExitTeachingMode(
grpc::ServerContext*,
const api::CommandHeader_Request* request,
api::CommandHeader_Feedback* response)
{
auto control_lease = ControlCommandArbiter::instance().tryAcquireControl();
if (!control_lease) return setControlBusy(response);
try {
const std::string device_id = request->device_id();
auto arm = dmgr_.getDevice<device::RobotArm>(device_id);
if (!arm) return setDeviceNotFound(response, device_id);
const auto result = arm->exitTeachingMode();
fillFeedback(response, result.ok(), result.ok() ? "" : result.message);
if (result.ok()) logRpcSuccess("ExitTeachingMode", device_id);
return resultToStatus(result);
} catch (const std::exception& e) {
fillFeedback(response, false, e.what());
return grpc::Status(grpc::StatusCode::INTERNAL, e.what());
}
}
} // namespace cmvr::service

View File

@ -251,6 +251,21 @@ message ServoJ {
}
}
// 开始记录示教轨迹。轨迹文件由机械臂控制器管理。
message StartTrajectoryRecording {
message Request {
// 通用请求头,包含目标设备 ID 和请求时间戳。
CommandHeader.Request header = 1;
// 轨迹文件名;不能为空。文件位置和格式由机械臂厂商 SDK 决定。
string file_name = 2;
}
message Response {
// 通用响应头,包含成功标志、错误信息和响应时间戳。
CommandHeader.Feedback header = 1;
}
}
// 机械臂关节状态。
message JointState {
// 关节名称列表,无单位;与 position、velocity、effort 按索引对应。

View File

@ -45,4 +45,14 @@ service ArmService {
rpc computeForwardKinematics(ComputeForwardKinematics.Request) returns (ComputeForwardKinematics.Response);
// 执行机械臂厂商扩展 JSON 命令;当前用于 AUBO 控制柜 Standard DI/DO。
rpc ExecuteJsonCommand(JsonDeviceCommand.Request) returns (JsonDeviceCommand.Feedback);
// 进入拖动示教模式;仅支持具备该能力的机械臂。
rpc EnterTeachingMode(CommandHeader.Request) returns (CommandHeader.Feedback);
// 开始记录示教轨迹;轨迹文件由机械臂控制器管理。
rpc StartTrajectoryRecording(StartTrajectoryRecording.Request) returns (StartTrajectoryRecording.Response);
// 完成示教:停止轨迹记录并退出拖动示教模式。
rpc CompleteTeaching(CommandHeader.Request) returns (CommandHeader.Feedback);
// 单独停止轨迹记录,不退出拖动示教模式。
rpc StopTrajectoryRecording(CommandHeader.Request) returns (CommandHeader.Feedback);
// 单独退出拖动示教模式,不停止轨迹记录。
rpc ExitTeachingMode(CommandHeader.Request) returns (CommandHeader.Feedback);
}