3#include <franka/control_types.h>
4#include <franka/duration.h>
5#include <franka/exception.h>
6#include <franka/robot.h>
7#include <franka/robot_state.h>
39 using std::runtime_error::runtime_error;
49 using std::runtime_error::runtime_error;
58class Robot :
public franka::Robot {
170 using franka::Robot::setCollisionBehavior;
342 std::unique_lock
lock(*control_mutex_);
343 return joinMotionUnsafe(
lock);
354 template <
class Rep,
class Period>
356 std::unique_lock
lock(*control_mutex_);
368 return joinMotion(std::chrono::milliseconds(0));
475 std::shared_ptr<const Model> model_;
480 template <
typename ControlSignalType>
481 using ControlFunc = std::function<
ControlSignalType(
const franka::RobotState &, franka::Duration)>;
482 using MotionGeneratorVariant = std::variant<
488 std::string fci_hostname_;
494 std::mutex state_mutex_;
495 std::shared_ptr<std::mutex> control_mutex_;
496 std::condition_variable control_finished_condition_;
497 std::exception_ptr control_exception_;
498 std::thread control_thread_;
499 MotionGeneratorVariant motion_generator_{std::nullopt};
500 bool motion_generator_running_{
false};
503 [[
nodiscard]]
bool is_in_control_unsafe()
const;
574 template <
class Rep =
long,
class Period = std::ratio<1>>
575 bool joinMotionUnsafe(
576 std::unique_lock<std::mutex> &
lock,
577 const std::optional<std::chrono::duration<Rep, Period>> &
timeout = std::nullopt) {
578 while (motion_generator_running_) {
580 if (control_finished_condition_.wait_for(
lock,
timeout.value()) == std::cv_status::timeout) {
584 control_finished_condition_.wait(
lock);
587 if (control_thread_.joinable()) control_thread_.join();
588 if (control_exception_ !=
nullptr) {
590 control_exception_ =
nullptr;
596 template <
typename ControlSignalType>
601 throw std::invalid_argument(
"The motion must not be null.");
604 if (
motion->has_started()) {
605 throw MotionReuseException(
606 "This motion object has already been started before. Motions cannot be reused; create a new motion "
607 "instance instead.");
610 std::unique_lock
lock(*control_mutex_);
611 if (is_in_control_unsafe() && motion_generator_running_) {
613 throw InvalidMotionTypeException(
614 "The type of motion cannot change during runtime. Please ensure "
616 "previous motion finished before using a new type of motion.");
618 std::get<MotionGenerator<ControlSignalType>>(motion_generator_).updateMotion(
motion);
620 joinMotionUnsafe(
lock);
623 auto motion_generator = &std::get<MotionGenerator<ControlSignalType>>(motion_generator_);
628 motion_generator_running_ =
true;
633 params_.kalman_q_process_var,
634 params_.kalman_dq_process_var,
635 params_.kalman_ddq_process_var,
636 params_.kalman_control_process_var,
637 params_.kalman_q_obs_var,
638 params_.kalman_dq_obs_var,
639 params_.kalman_q_d_obs_var,
640 params_.kalman_dq_d_obs_var,
641 params_.kalman_ddq_d_obs_var,
642 params_.kalman_control_adaptation_rate);
648 std::unique_lock
lock(*control_mutex_);
658 motion_generator_running_ =
false;
659 control_finished_condition_.notify_all();
663 std::unique_lock
lock(*control_mutex_);
664 control_exception_ = std::current_exception();
665 motion_generator_running_ =
false;
666 control_finished_condition_.notify_all();
Definition cartesian_state.hpp:17
RobotPose pose() const
Definition cartesian_state.hpp:62
RobotVelocity velocity() const
Definition cartesian_state.hpp:67
A template class representing a dynamics limit with a maximum value.
Definition dynamics_limit.hpp:23
Elbow state of the robot.
Definition elbow_state.hpp:24
Joint state of a robot.
Definition joint_state.hpp:16
Vector7d velocity() const
The velocity component of the state.
Definition joint_state.hpp:48
Vector7d position() const
The position component of the state.
Definition joint_state.hpp:43
Helper class for handling motions and reactions.
Definition motion_generator.hpp:31
Base class for motions.
Definition motion.hpp:25
Relative dynamics factors.
Definition relative_dynamics_factor.hpp:13
A class representing a Franka robot.
Definition robot.hpp:58
DynamicsLimit< Vector7d > joint_acceleration_limit
Joint acceleration limit [rad/s²].
Definition robot.hpp:564
bool joinMotion(const std::chrono::duration< Rep, Period > &timeout)
Wait for the current motion to finish with a timeout. Throw any exceptions that occurred during the m...
Definition robot.hpp:355
std::optional< ControlSignalType > current_control_signal_type()
The type of the current control signal.
Definition robot.cpp:112
std::string fci_hostname() const
The hostname of the robot.
Definition robot.cpp:110
void move(const std::shared_ptr< Motion< franka::CartesianPose > > &motion, bool async=false, bool limit_rate=false, double cutoff_frequency=franka::kDefaultCutoffFrequency)
Execute the given motion.
Definition robot.hpp:383
Vector7d currentJointVelocities()
Returns the current joint velocities of the robot.
Definition robot.hpp:279
bool joinMotion()
Wait for the current motion to finish. Throw any exceptions that occurred during the motion.
Definition robot.hpp:341
static constexpr double control_rate
Definition robot.hpp:157
DynamicsLimit< double > elbow_acceleration_limit
Elbow acceleration limit [rad/s²].
Definition robot.hpp:539
RelativeDynamicsFactor relative_dynamics_factor()
Returns the current global relative dynamics factor of the robot.
Definition robot.cpp:123
Vector7d currentJointPositions()
Returns the current joint positions of the robot.
Definition robot.hpp:273
CartesianState currentCartesianState()
Returns the current cartesian state of the robot.
Definition robot.hpp:254
DynamicsLimit< Vector7d > joint_velocity_limit
Joint velocity limit [rad/s].
Definition robot.hpp:559
RobotState state()
Returns the current state of the robot.
Definition robot.cpp:60
void move(const std::shared_ptr< Motion< franka::Torques > > &motion, bool async=false, bool limit_rate=false, double cutoff_frequency=franka::kDefaultCutoffFrequency)
Execute the given motion.
Definition robot.hpp:463
DynamicsLimit< double > translation_jerk_limit
Translational jerk limit [m/s³].
Definition robot.hpp:544
DynamicsLimit< double > translation_velocity_limit
Translational velocity limit [m/s].
Definition robot.hpp:514
bool recoverFromErrors()
Calls the automatic error recovery of the robot and returns whether the recovery was successful.
Definition robot.cpp:55
void move(const std::shared_ptr< Motion< franka::JointPositions > > &motion, bool async=false, bool limit_rate=false, double cutoff_frequency=franka::kDefaultCutoffFrequency)
Execute the given motion.
Definition robot.hpp:423
DynamicsLimit< double > elbow_velocity_limit
Elbow velocity limit [rad/s].
Definition robot.hpp:524
DynamicsLimit< double > rotation_velocity_limit
Rotational velocity limit [rad/s].
Definition robot.hpp:519
DynamicsLimit< double > elbow_jerk_limit
Elbow jerk limit [rad/s³].
Definition robot.hpp:554
bool is_in_control()
Whether the robot is currently in control, i.e. a motion is being executed.
Definition robot.cpp:105
void move(const std::shared_ptr< Motion< franka::CartesianVelocities > > &motion, bool async=false, bool limit_rate=false, double cutoff_frequency=franka::kDefaultCutoffFrequency)
Execute the given motion.
Definition robot.hpp:403
DynamicsLimit< Vector7d > joint_jerk_limit
Joint jerk limit [rad/s³].
Definition robot.hpp:569
JointState currentJointState()
Returns the current joint state of the robot.
Definition robot.hpp:264
void setCollisionBehavior(const ScalarOrArray< 7 > &torque_threshold, const ScalarOrArray< 6 > &force_threshold)
Set the collision behavior of the robot.
Definition robot.cpp:72
static constexpr size_t degrees_of_freedoms
Definition robot.hpp:154
RobotVelocity currentCartesianVelocity()
Returns the current cartesian velocity of the robot.
Definition robot.hpp:248
DynamicsLimit< double > rotation_jerk_limit
Rotational jerk limit [rad/s³].
Definition robot.hpp:549
void setRelativeDynamicsFactor(const RelativeDynamicsFactor &relative_dynamics_factor)
Sets the global relative dynamics factor of the robot.
Definition robot.cpp:127
std::shared_ptr< const Model > model() const
The model of the robot.
Definition robot.hpp:327
DynamicsLimit< double > translation_acceleration_limit
Translational acceleration limit [m/s²].
Definition robot.hpp:529
bool hasErrors()
Returns whether the robot has errors.
Definition robot.cpp:53
RobotPose currentPose()
Returns the current pose of the robot.
Definition robot.hpp:242
DynamicsLimit< double > rotation_acceleration_limit
Rotational acceleration limit [rad/s²].
Definition robot.hpp:534
RelativeDynamicsFactor relative_dynamics_factor_rt()
Returns the current global relative dynamics factor of the robot (Real-Time safe).
Definition robot.cpp:125
void move(const std::shared_ptr< Motion< franka::JointVelocities > > &motion, bool async=false, bool limit_rate=false, double cutoff_frequency=franka::kDefaultCutoffFrequency)
Execute the given motion.
Definition robot.hpp:443
bool pollMotion()
Check whether the robot is still in motion. This function is non-blocking and returns immediately....
Definition robot.hpp:367
Cartesian pose of a robot.
Definition robot_pose.hpp:19
Cartesian velocity of a robot.
Definition robot_velocity.hpp:20
Wait-free, Single-Producer Single-Consumer (SPSC) triple buffer.
Definition wait_free_triple_buffer.hpp:17
void set(const T &value)
Publish new data.
Definition wait_free_triple_buffer.hpp:26
Definition dynamics_limit.cpp:8
std::array< double, dims > toStdD(const Eigen::Matrix< double, dims, 1 > &vector)
Definition util.hpp:18
Eigen::Vector< double, 7 > Vector7d
Definition types.hpp:11
ControlSignalType
Type of control signal.
Definition control_signal_type.hpp:8
std::variant< double, Array< dims > > ScalarOrArray
Definition types.hpp:22
Eigen::Affine3d Affine
Definition types.hpp:16
Exception thrown when an invalid motion type is used.
Definition robot.hpp:38
Exception thrown when a motion is started more than once.
Definition robot.hpp:48
Global parameters for the robot.
Definition robot.hpp:63
double kalman_dq_process_var
Kalman parameter: process noise variance of the velocity.
Definition robot.hpp:104
RelativeDynamicsFactor relative_dynamics_factor
Relative dynamics factor for the robot.
Definition robot.hpp:70
double default_force_threshold
Default force threshold for collision behavior.
Definition robot.hpp:80
double kalman_control_adaptation_rate
Kalman parameter: rate of adaptation of the robot state to the desired robot state.
Definition robot.hpp:150
double kalman_dq_obs_var
Kalman parameter: observation noise variance of measured joint velocities.
Definition robot.hpp:126
double kalman_q_obs_var
Kalman parameter: observation noise variance of measured joint positions.
Definition robot.hpp:120
double kalman_dq_d_obs_var
Kalman parameter: observation noise variance of desired joint velocities.
Definition robot.hpp:138
franka::ControllerMode controller_mode
Default controller mode for the robot.
Definition robot.hpp:87
double kalman_control_process_var
Kalman parameter: process noise variance of the control signal.
Definition robot.hpp:114
double kalman_q_d_obs_var
Kalman parameter: observation noise variance of desired joint positions.
Definition robot.hpp:132
franka::RealtimeConfig realtime_config
Default realtime configuration for the robot.
Definition robot.hpp:94
double kalman_ddq_d_obs_var
Kalman parameter: observation noise variance of desired joint accelerations.
Definition robot.hpp:144
double default_torque_threshold
Default torque threshold for collision behavior.
Definition robot.hpp:75
double kalman_ddq_process_var
Kalman parameter: process noise variance of the acceleration.
Definition robot.hpp:109
double kalman_q_process_var
Kalman parameter: process noise variance of the position.
Definition robot.hpp:99
Full state of the robot.
Definition robot_state.hpp:40