cmvr-es/cmvr-es/devices/arm/robot_arm.h

224 lines
9.5 KiB
C++

#ifndef CMVR_ES_ROBOT_ARM_H
#define CMVR_ES_ROBOT_ARM_H
#include <cstddef>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "algorithms/kinematics/ik_solver/common/include/ik_solver.h"
#include "common/types/arm/arm_types.h"
#include "devices/abstract_device.h"
namespace cmvr::device {
struct RecordedTrajectoryInfo {
std::string file_name;
std::size_t samples{};
double duration_s{};
double sampling_frequency_hz{};
};
struct TrajectoryRecordingStatus {
std::string directory, file_name, error;
bool recording{};
std::size_t samples{};
};
class RobotArm : public AbstractDevice {
public:
~RobotArm() override = default;
DeviceKind kind() const noexcept override { return DeviceKind::Arm; }
virtual RobotModel getRobotModel() const = 0;
virtual RobotModel queryRobotModel() const { return getRobotModel(); }
virtual std::size_t getDof() const = 0;
virtual ArmState getRobotState() const = 0;
virtual JointGroupState getJointState() const = 0;
virtual CartesianPose getTcpPose(FrameType frame = FrameType::Base) const = 0;
virtual RobotMode getRobotMode() const = 0;
virtual SafetyMode getSafetyMode() const = 0;
virtual ControlMode getControlMode() const = 0;
// ActionQueue requires synchronous motion and cooperative cancellation.
// Backends opt in only after both semantics are implemented.
virtual bool supportsActionQueueMotion() const noexcept { return false; }
virtual Result listBaseFrame(std::vector<std::string>& frame_names) const
{
frame_names.clear();
return Result::failure(
ArmErrorCode::UnsupportedCommand,
"ListBaseFrame is not supported by this robot arm");
}
virtual Result listTCPFrame(std::vector<std::string>& frame_names) const
{
frame_names.clear();
return Result::failure(
ArmErrorCode::UnsupportedCommand,
"ListTCPFrame is not supported by this robot arm");
}
virtual bool executeJsonCommand(const std::string&,
std::string& response_json)
{
response_json =
R"({"success":false,"error_code":"unsupported_command","error_message":"JSON command unsupported"})";
return false;
}
virtual Result torqueOn() = 0;
virtual Result torqueOff() = 0;
virtual Result calibrateZeroQ(const std::string& joint_name) = 0;
virtual Result emergencyStop() = 0;
virtual Result protectiveStop() = 0;
virtual Result recoverProtectiveStop(
const JointTrajectory& path,
const MotionOptions& options) = 0;
virtual Result setSpeedScaling(double scaling) = 0;
virtual double getSpeedScaling() const = 0;
virtual bool isProtectiveStopped() const = 0;
virtual bool isEmergencyStopped() const = 0;
virtual bool isFault() const = 0;
virtual Result moveJ(const JointPositionCommand& target,
const MotionOptions& options) = 0;
virtual Result speedJ(const JointVelocityCommand& velocity,
double acceleration,
double duration) = 0;
virtual Result stopJ(double acceleration) = 0;
virtual Result moveL(const CartesianPose& target,
const MotionOptions& options,
FrameType frame = FrameType::Base) = 0;
// Named-frame overload. Empty names select the driver's configured defaults.
virtual Result moveL(const CartesianPose& target,
const MotionOptions& options,
const std::string& base_frame,
const std::string& tcp_frame)
{
if (base_frame.empty() && tcp_frame.empty()) {
return moveL(target, options, FrameType::Base);
}
return Result::failure(
ArmErrorCode::UnsupportedCommand,
"named base_frame/tcp_frame are not supported by this robot arm");
}
virtual Result speedL(const CartesianVelocity& velocity,
double acceleration,
double duration,
FrameType frame = FrameType::Base) = 0;
virtual Result stopL(std::optional<double> acceleration = std::nullopt) = 0;
virtual Result speedL(const CartesianVelocity& velocity,
const SpeedLOptions& options,
double duration,
FrameType frame = FrameType::Base) {
if (options.linear_jerk || options.capture_reference ||
options.continuous_linear_reversal.value_or(false)) {
return Result::failure(ArmErrorCode::UnsupportedCommand, "speedL options are not supported by this arm");
}
return speedL(velocity, options.acceleration, duration, frame);
}
virtual SpeedLReference getSpeedLReference() const { return {}; }
// Submission success and the asynchronous execution result are distinct.
virtual Result getSpeedLResult() const {
return Result::failure(ArmErrorCode::UnsupportedCommand, "speedL result is unavailable");
}
virtual Result stopMotion() = 0;
virtual Result moveP(const CartesianPose& target,
const MotionOptions& options,
FrameType frame = FrameType::Base)
{
if (options.is_tcp_pose_valid) {
return Result::failure(
ArmErrorCode::UnsupportedCommand,
"MoveP TCP pose constraints are not supported by this robot arm");
}
return moveL(target, options, frame);
}
// Plan to the exact joint configuration without converting through FK/IK.
virtual Result moveP(const JointPositionCommand& target,
const MotionOptions& options)
{
return Result::failure(ArmErrorCode::UnsupportedCommand,
"joint-target MoveP is not supported by this robot arm");
}
virtual Result startServoMode(const ServoOptions& options) = 0;
virtual Result servoJ(const JointPositionCommand& target) = 0;
virtual Result servoL(const CartesianPose& target,
FrameType frame = FrameType::Base) = 0;
virtual Result servoSpeedJ(const JointVelocityCommand& velocity) = 0;
virtual Result servoSpeedL(const CartesianVelocity& velocity,
FrameType frame = FrameType::Base) = 0;
virtual Result stopServoMode() = 0;
virtual Result connect(const std::string& ip, int port) = 0;
virtual Result disconnect() = 0;
virtual bool isConnected() const = 0;
virtual Result powerOn() = 0;
virtual Result powerOff() = 0;
virtual Result brakeRelease() = 0;
virtual Result shutdown() = 0;
virtual Result clearFault() = 0;
// Only backends that can distinguish planning faults may recover them automatically.
virtual Result recoverSpeedLPlanningFault() {
return isFault()
? Result::failure(ArmErrorCode::RobotInFault, "arm fault requires explicit recovery")
: Result::success();
}
virtual Result unlockProtectiveStop() = 0;
virtual Result loadProgram(const std::string& program_name) = 0;
virtual Result playProgram() = 0;
virtual Result pauseProgram() = 0;
virtual Result stopProgram() = 0;
// Preserve optional vendor teaching capabilities from linbo_dev.
virtual Result enterTeachingMode() {
return Result::failure(ArmErrorCode::UnsupportedCommand, "teaching mode is not supported by this robot arm");
}
virtual Result startTrajectoryRecording(const std::string&) {
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 startTrajectoryRecording(const std::string& name, double frequency_hz)
{
if (frequency_hz == 0 || frequency_hz == 100)
return startTrajectoryRecording(name);
return Result::failure(ArmErrorCode::UnsupportedCommand, "configurable CSV recording unsupported");
}
virtual Result listRecordedTrajectories(std::vector<RecordedTrajectoryInfo>&,
TrajectoryRecordingStatus&) const
{
return Result::failure(ArmErrorCode::UnsupportedCommand, "CSV trajectories unsupported");
}
virtual Result replayRecordedTrajectory(const std::string&, double, bool,
const MotionOptions&)
{
return Result::failure(ArmErrorCode::UnsupportedCommand, "CSV replay unsupported");
}
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;
virtual std::shared_ptr<cmvr::IKSolver> kinematicsSolver() const = 0;
virtual CartesianPose fk(const std::string& base_link,
const std::string& ee_link) = 0;
virtual CartesianPose fk(bool is_tcp = true) = 0;
virtual CartesianVelocity getSpeedLCommandTwistBase() const = 0;
virtual bool busy() const = 0;
};
} // namespace cmvr::device
#endif // CMVR_ES_ROBOT_ARM_H