Franky 1.1.0
A High-Level Motion API for Franka
Loading...
Searching...
No Matches
robot.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <franka/control_types.h>
4#include <franka/duration.h>
5#include <franka/exception.h>
6#include <franka/model.h>
7#include <franka/robot.h>
8#include <franka/robot_state.h>
9
10#include <exception>
11#include <future>
12#include <optional>
13#include <stdexcept>
14#include <variant>
15
20#include "franky/model.hpp"
24#include "franky/robot_pose.hpp"
27#include "franky/types.hpp"
28#include "franky/util.hpp"
29
30namespace franky {
31
38struct InvalidMotionTypeException : std::runtime_error {
39 using std::runtime_error::runtime_error;
40};
41
48class Robot : public franka::Robot {
49 public:
53 struct Params {
61
66
71
77 franka::ControllerMode controller_mode{franka::ControllerMode::kJointImpedance};
78
84 franka::RealtimeConfig realtime_config{franka::RealtimeConfig::kEnforce};
85
89 double kalman_q_process_var = 0.0001;
90
94 double kalman_dq_process_var = 0.001;
95
100
105
110 double kalman_q_obs_var = 0.01;
111
116 double kalman_dq_obs_var = 0.1;
117
122 double kalman_q_d_obs_var = 0.0001;
123
128 double kalman_dq_d_obs_var = 0.0001;
129
134 double kalman_ddq_d_obs_var = 0.0001;
135
141 };
142
144 static constexpr size_t degrees_of_freedoms{7};
145
147 static constexpr double control_rate{0.001};
148
152 explicit Robot(const std::string &fci_hostname);
153
158 explicit Robot(const std::string &fci_hostname, const Params &params);
159
160 using franka::Robot::setCollisionBehavior;
161
170
186
214
220 bool recoverFromErrors();
221
226 [[nodiscard]] bool hasErrors();
227
233
239
245 auto s = state();
246 return {
247 {Affine(Eigen::Matrix4d::Map(s.O_T_EE.data())), ElbowState{s.elbow}}, RobotVelocity(s.O_dP_EE_c, s.delbow_c)};
248 }
249
255 auto s = state();
256 return {s.q, s.dq};
257 }
258
264
270
276
282
288
293 [[nodiscard]] bool is_in_control();
294
298 [[nodiscard]] std::string fci_hostname() const;
299
303 [[nodiscard]] std::optional<ControlSignalType> current_control_signal_type();
304
311 [[nodiscard]] std::shared_ptr<const Model> model() const { return model_; }
312
313#ifdef FRANKA_0_15
318 [[nodiscard]] std::string model_urdf() const { return model_urdf_; }
319#endif
320
325 bool joinMotion() {
326 std::unique_lock lock(*control_mutex_);
327 return joinMotionUnsafe(lock);
328 }
329
338 template <class Rep, class Period>
339 bool joinMotion(const std::chrono::duration<Rep, Period> &timeout) {
340 std::unique_lock lock(*control_mutex_);
342 }
343
350 [[nodiscard]]
351 bool pollMotion() {
352 return joinMotion(std::chrono::milliseconds(0));
353 }
354
355 // These helper functions are needed as the implicit template deduction does
356 // not work on subclasses of Motion
357
363 void move(const std::shared_ptr<Motion<franka::CartesianPose>> &motion, bool async = false) {
365 motion, [this](const ControlFunc<franka::CartesianPose> &m) { control(m, params_.controller_mode); }, async);
366 }
367
373 void move(const std::shared_ptr<Motion<franka::CartesianVelocities>> &motion, bool async = false) {
375 motion,
376 [this](const ControlFunc<franka::CartesianVelocities> &m) { control(m, params_.controller_mode); },
377 async);
378 }
379
385 void move(const std::shared_ptr<Motion<franka::JointPositions>> &motion, bool async = false) {
387 motion, [this](const ControlFunc<franka::JointPositions> &m) { control(m, params_.controller_mode); }, async);
388 }
389
395 void move(const std::shared_ptr<Motion<franka::JointVelocities>> &motion, bool async = false) {
397 motion, [this](const ControlFunc<franka::JointVelocities> &m) { control(m, params_.controller_mode); }, async);
398 }
399
405 void move(const std::shared_ptr<Motion<franka::Torques>> &motion, bool async = false) {
406 moveInternal<franka::Torques>(motion, [this](const ControlFunc<franka::Torques> &m) { control(m); }, async);
407 }
408
409 private:
410 std::shared_ptr<const Model> model_;
411#ifdef FRANKA_0_15
412 std::string model_urdf_;
413#endif
414
415 template <typename ControlSignalType>
416 using ControlFunc = std::function<ControlSignalType(const franka::RobotState &, franka::Duration)>;
417 using MotionGeneratorVariant = std::variant<
421
423 std::string fci_hostname_;
424 Params params_;
425 RobotState current_state_;
426 std::mutex state_mutex_;
427 std::shared_ptr<std::mutex> control_mutex_;
428 std::condition_variable control_finished_condition_;
429 std::exception_ptr control_exception_;
430 std::thread control_thread_;
431 MotionGeneratorVariant motion_generator_{std::nullopt};
432 bool motion_generator_running_{false};
433
434 [[nodiscard]] bool is_in_control_unsafe() const;
435
436 public:
437 // IMPORTANT: this has to come after control_mutex_ as otherwise
438 // control_mutex_ will be uninitialized when passed to the constructor of the
439 // DynamicsLimit class Limits provided by Franka for the FR3:
440 // https://frankaemika.github.io/docs/control_parameters.html
441 // clang-format off
446
451
456
461
466
471
476
481
486
491
496
501
502 // clang-format on
503
504 private:
505 template <class Rep = long, class Period = std::ratio<1>>
506 bool joinMotionUnsafe(
507 std::unique_lock<std::mutex> &lock,
508 const std::optional<std::chrono::duration<Rep, Period>> &timeout = std::nullopt) {
509 while (motion_generator_running_) {
510 if (timeout.has_value()) {
511 if (control_finished_condition_.wait_for(lock, timeout.value()) == std::cv_status::timeout) {
512 return false;
513 }
514 } else {
515 control_finished_condition_.wait(lock);
516 }
517 }
518 if (control_thread_.joinable()) control_thread_.join();
519 if (control_exception_ != nullptr) {
520 auto control_exception = control_exception_;
521 control_exception_ = nullptr;
522 std::rethrow_exception(control_exception);
523 }
524 return true;
525 }
526
527 template <typename ControlSignalType>
528 void moveInternal(
529 const std::shared_ptr<Motion<ControlSignalType>> &motion,
530 const std::function<void(const ControlFunc<ControlSignalType> &)> &control_func_executor, bool async) {
531 if (motion == nullptr) {
532 throw std::invalid_argument("The motion must not be null.");
533 }
534 { // Do not remove brace, it is needed to scope the lock
535 std::unique_lock lock(*control_mutex_);
536 if (is_in_control_unsafe() && motion_generator_running_) {
537 if (!std::holds_alternative<MotionGenerator<ControlSignalType>>(motion_generator_)) {
538 throw InvalidMotionTypeException(
539 "The type of motion cannot change during runtime. Please ensure "
540 "that the "
541 "previous motion finished before using a new type of motion.");
542 }
543 std::get<MotionGenerator<ControlSignalType>>(motion_generator_).updateMotion(motion);
544 } else {
545 joinMotionUnsafe(lock);
546
547 motion_generator_.emplace<MotionGenerator<ControlSignalType>>(this, motion);
548 auto motion_generator = &std::get<MotionGenerator<ControlSignalType>>(motion_generator_);
549 motion_generator->registerUpdateCallback(
550 [this](const RobotState &robot_state, franka::Duration duration, franka::Duration time) {
551 std::lock_guard lock(state_mutex_);
552 current_state_ = robot_state;
553 });
554 motion_generator_running_ = true;
555 control_thread_ = std::thread([this, control_func_executor, motion_generator]() {
556 try {
557 bool done = false;
558 RobotStateEstimator robot_state_estimator(
559 params_.kalman_q_process_var,
560 params_.kalman_dq_process_var,
561 params_.kalman_ddq_process_var,
562 params_.kalman_control_process_var,
563 params_.kalman_q_obs_var,
564 params_.kalman_dq_obs_var,
565 params_.kalman_q_d_obs_var,
566 params_.kalman_dq_d_obs_var,
567 params_.kalman_ddq_d_obs_var,
568 params_.kalman_control_adaptation_rate);
569 while (!done) {
571 [this, motion_generator, &robot_state_estimator](const franka::RobotState &rs, franka::Duration d) {
572 return (*motion_generator)(robot_state_estimator.update(rs, *model_), d);
573 });
574 std::unique_lock lock(*control_mutex_);
575
576 // This code is just for the case that a new motion is set just
577 // after the old one terminates. If this happens, we need to
578 // continue with this motion, unless an exception occurs.
579 done = !motion_generator->has_new_motion();
580 if (motion_generator->has_new_motion()) {
581 motion_generator->resetTimeUnsafe();
582 } else {
583 done = true;
584 motion_generator_running_ = false;
585 control_finished_condition_.notify_all();
586 }
587 }
588 } catch (...) {
589 std::unique_lock lock(*control_mutex_);
590 control_exception_ = std::current_exception();
591 motion_generator_running_ = false;
592 control_finished_condition_.notify_all();
593 }
594 });
595 }
596 }
597 if (!async) joinMotion();
598 }
599};
600
601} // namespace franky
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:24
Relative dynamics factors.
Definition relative_dynamics_factor.hpp:13
A class representing a Franka robot.
Definition robot.hpp:48
DynamicsLimit< Vector7d > joint_acceleration_limit
Joint acceleration limit [rad/s²].
Definition robot.hpp:495
void move(const std::shared_ptr< Motion< franka::JointPositions > > &motion, bool async=false)
Execute the given motion.
Definition robot.hpp:385
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:339
std::optional< ControlSignalType > current_control_signal_type()
The type of the current control signal.
Definition robot.cpp:113
void move(const std::shared_ptr< Motion< franka::CartesianPose > > &motion, bool async=false)
Execute the given motion.
Definition robot.hpp:363
std::string fci_hostname() const
The hostname of the robot.
Definition robot.cpp:111
Vector7d currentJointVelocities()
Returns the current joint velocities of the robot.
Definition robot.hpp:269
bool joinMotion()
Wait for the current motion to finish. Throw any exceptions that occurred during the motion.
Definition robot.hpp:325
static constexpr double control_rate
Definition robot.hpp:147
DynamicsLimit< double > elbow_acceleration_limit
Elbow acceleration limit [rad/s²].
Definition robot.hpp:470
RelativeDynamicsFactor relative_dynamics_factor()
Returns the current global relative dynamics factor of the robot.
Definition robot.cpp:124
Vector7d currentJointPositions()
Returns the current joint positions of the robot.
Definition robot.hpp:263
void move(const std::shared_ptr< Motion< franka::Torques > > &motion, bool async=false)
Execute the given motion.
Definition robot.hpp:405
CartesianState currentCartesianState()
Returns the current cartesian state of the robot.
Definition robot.hpp:244
DynamicsLimit< Vector7d > joint_velocity_limit
Joint velocity limit [rad/s].
Definition robot.hpp:490
RobotState state()
Returns the current state of the robot.
Definition robot.cpp:61
DynamicsLimit< double > translation_jerk_limit
Translational jerk limit [m/s³].
Definition robot.hpp:475
DynamicsLimit< double > translation_velocity_limit
Translational velocity limit [m/s].
Definition robot.hpp:445
bool recoverFromErrors()
Calls the automatic error recovery of the robot and returns whether the recovery was successful.
Definition robot.cpp:56
DynamicsLimit< double > elbow_velocity_limit
Elbow velocity limit [rad/s].
Definition robot.hpp:455
DynamicsLimit< double > rotation_velocity_limit
Rotational velocity limit [rad/s].
Definition robot.hpp:450
DynamicsLimit< double > elbow_jerk_limit
Elbow jerk limit [rad/s³].
Definition robot.hpp:485
bool is_in_control()
Whether the robot is currently in control, i.e. a motion is being executed.
Definition robot.cpp:106
void move(const std::shared_ptr< Motion< franka::JointVelocities > > &motion, bool async=false)
Execute the given motion.
Definition robot.hpp:395
DynamicsLimit< Vector7d > joint_jerk_limit
Joint jerk limit [rad/s³].
Definition robot.hpp:500
JointState currentJointState()
Returns the current joint state of the robot.
Definition robot.hpp:254
void setCollisionBehavior(const ScalarOrArray< 7 > &torque_threshold, const ScalarOrArray< 6 > &force_threshold)
Set the collision behavior of the robot.
Definition robot.cpp:73
static constexpr size_t degrees_of_freedoms
Definition robot.hpp:144
RobotVelocity currentCartesianVelocity()
Returns the current cartesian velocity of the robot.
Definition robot.hpp:238
DynamicsLimit< double > rotation_jerk_limit
Rotational jerk limit [rad/s³].
Definition robot.hpp:480
void move(const std::shared_ptr< Motion< franka::CartesianVelocities > > &motion, bool async=false)
Execute the given motion.
Definition robot.hpp:373
void setRelativeDynamicsFactor(const RelativeDynamicsFactor &relative_dynamics_factor)
Sets the global relative dynamics factor of the robot.
Definition robot.cpp:129
std::shared_ptr< const Model > model() const
The model of the robot.
Definition robot.hpp:311
DynamicsLimit< double > translation_acceleration_limit
Translational acceleration limit [m/s²].
Definition robot.hpp:460
bool hasErrors()
Returns whether the robot has errors.
Definition robot.cpp:54
RobotPose currentPose()
Returns the current pose of the robot.
Definition robot.hpp:232
DynamicsLimit< double > rotation_acceleration_limit
Rotational acceleration limit [rad/s²].
Definition robot.hpp:465
bool pollMotion()
Check whether the robot is still in motion. This function is non-blocking and returns immediately....
Definition robot.hpp:351
Cartesian pose of a robot.
Definition robot_pose.hpp:19
Cartesian velocity of a robot.
Definition robot_velocity.hpp:20
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:21
Eigen::Affine3d Affine
Definition types.hpp:15
Exception thrown when an invalid motion type is used.
Definition robot.hpp:38
Global parameters for the robot.
Definition robot.hpp:53
double kalman_dq_process_var
Kalman parameter: process noise variance of the velocity.
Definition robot.hpp:94
RelativeDynamicsFactor relative_dynamics_factor
Relative dynamics factor for the robot.
Definition robot.hpp:60
double default_force_threshold
Default force threshold for collision behavior.
Definition robot.hpp:70
double kalman_control_adaptation_rate
Kalman parameter: rate of adaptation of the robot state to the desired robot state.
Definition robot.hpp:140
double kalman_dq_obs_var
Kalman parameter: observation noise variance of measured joint velocities.
Definition robot.hpp:116
double kalman_q_obs_var
Kalman parameter: observation noise variance of measured joint positions.
Definition robot.hpp:110
double kalman_dq_d_obs_var
Kalman parameter: observation noise variance of desired joint velocities.
Definition robot.hpp:128
franka::ControllerMode controller_mode
Default controller mode for the robot.
Definition robot.hpp:77
double kalman_control_process_var
Kalman parameter: process noise variance of the control signal.
Definition robot.hpp:104
double kalman_q_d_obs_var
Kalman parameter: observation noise variance of desired joint positions.
Definition robot.hpp:122
franka::RealtimeConfig realtime_config
Default realtime configuration for the robot.
Definition robot.hpp:84
double kalman_ddq_d_obs_var
Kalman parameter: observation noise variance of desired joint accelerations.
Definition robot.hpp:134
double default_torque_threshold
Default torque threshold for collision behavior.
Definition robot.hpp:65
double kalman_ddq_process_var
Kalman parameter: process noise variance of the acceleration.
Definition robot.hpp:99
double kalman_q_process_var
Kalman parameter: process noise variance of the position.
Definition robot.hpp:89
Full state of the robot.
Definition robot_state.hpp:23