// Copyright (c) 2023 Franka Robotics GmbH
// Use of this source code is governed by the Apache-2.0 license, see LICENSE
#include <cmath>
#include <iostream>
#include <franka/exception.h>
#include <franka/robot.h>
#include "examples_common.h"
/**
* @example joint_point_to_point_motion.cpp
* An example that moves the robot to a target position by commanding joint positions.
*
* @warning Before executing this example, make sure there is enough space in front of the robot.
*/
/**
* @example joint_point_to_point_motion.cpp
* 一个通过命令关节位置将机器人移动到目标位置的示例。
*
* @warning 在执行这个示例之前,请确保机器人前方有足够的空间。
*/
int main(int argc, char** argv) {
if (argc != 10) {
std::cerr << "Usage: " << argv[0] << " <robot-hostname> "
<< "<joint0> <joint1> <joint2> <joint3> <joint4> <joint5> <joint6> "
<< "<speed-factor>" << std::endl
<< "joint0 to joint6 are joint angles in [rad]." << std::endl
<< "speed-factor must be between zero and one." << std::endl;
return -1;
}
try {
franka::Robot robot(argv[1]); // 连接机器人
setDefaultBehavior(robot); // 机器人默认行为
std::array<double, 7> q_goal; // 存储目标角度
for (size_t i = 0; i < 7; i++) {
q_goal[i] = std::stod(argv[i + 2]); // 解析角度
}
double speed_factor = std::stod(argv[9]); // 速度比例因子
// Set additional parameters always before the control loop, NEVER in the control loop!
// Set collision behavior. 设置碰撞行为
robot.setCollisionBehavior(
{{20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0}}, {{20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0}},
{{10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0}}, {{10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0}},
{{20.0, 20.0, 20.0, 20.0, 20.0, 20.0}}, {{20.0, 20.0, 20.0, 20.0, 20.0, 20.0}},
{{10.0, 10.0, 10.0, 10.0, 10.0, 10.0}}, {{10.0, 10.0, 10.0, 10.0, 10.0, 10.0}});
// 运动生成器
MotionGenerator motion_generator(speed_factor, q_goal); // 运动生成器
std::cout << "WARNING: This example will move the robot! "
<< "Please make sure to have the user stop button at hand!" << std::endl
<< "Press Enter to continue..." << std::endl;
std::cin.ignore();
robot.control(motion_generator);
std::cout << "Motion finished" << std::endl;
} catch (const franka::Exception& e) {
std::cout << e.what() << std::endl;
return -1;
}
return 0;
}
/**
* @file examples_common.h
* Contains common types and functions for the examples.
*/
/**
* Sets a default collision behavior, joint impedance and Cartesian impedance.
*
* @param[in] robot Robot instance to set behavior on.
*/
void setDefaultBehavior(franka::Robot& robot);
/**
* An example showing how to generate a joint pose motion to a goal position. Adapted from:
* Wisama Khalil and Etienne Dombre. 2002. Modeling, Identification and Control of Robots
* (Kogan Page Science Paper edition).
*/
class MotionGenerator {
public:
/**
* Creates a new MotionGenerator instance for a target q.
*
* @param[in] speed_factor General speed factor in range [0, 1].
* @param[in] q_goal Target joint positions.
*/
MotionGenerator(double speed_factor, const std::array<double, 7> q_goal);
/**
* Sends joint position calculations
*
* @param[in] robot_state Current state of the robot.
* @param[in] period Duration of execution.
*
* @return Joint positions for use inside a control loop.
*/
franka::JointPositions operator()(const franka::RobotState& robot_state, franka::Duration period);
private:
using Vector7d = Eigen::Matrix<double, 7, 1, Eigen::ColMajor>;
using Vector7i = Eigen::Matrix<int, 7, 1, Eigen::ColMajor>;
bool calculateDesiredValues(double t, Vector7d* delta_q_d) const;
void calculateSynchronizedValues();
static constexpr double kDeltaQMotionFinished = 1e-6;
const Vector7d q_goal_;
Vector7d q_start_;
Vector7d delta_q_;
Vector7d dq_max_sync_;
Vector7d t_1_sync_;
Vector7d t_2_sync_;
Vector7d t_f_sync_;
Vector7d q_1_;
double time_ = 0.0;
Vector7d dq_max_ = (Vector7d() << 2.0, 2.0, 2.0, 2.0, 2.5, 2.5, 2.5).finished();
Vector7d ddq_max_start_ = (Vector7d() << 5, 5, 5, 5, 5, 5, 5).finished();
Vector7d ddq_max_goal_ = (Vector7d() << 5, 5, 5, 5, 5, 5, 5).finished();
};
// Copyright (c) 2023 Franka Robotics GmbH
// Use of this source code is governed by the Apache-2.0 license, see LICENSE
#include "examples_common.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <franka/exception.h>
#include <franka/robot.h>
void setDefaultBehavior(franka::Robot& robot) {
robot.setCollisionBehavior(
{{20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0}}, {{20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0}},
{{10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0}}, {{10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0}},
{{20.0, 20.0, 20.0, 20.0, 20.0, 20.0}}, {{20.0, 20.0, 20.0, 20.0, 20.0, 20.0}},
{{10.0, 10.0, 10.0, 10.0, 10.0, 10.0}}, {{10.0, 10.0, 10.0, 10.0, 10.0, 10.0}});
robot.setJointImpedance({{3000, 3000, 3000, 2500, 2500, 2000, 2000}});
robot.setCartesianImpedance({{3000, 3000, 3000, 300, 300, 300}});
}
MotionGenerator::MotionGenerator(double speed_factor, const std::array<double, 7> q_goal)
: q_goal_(q_goal.data()) {
dq_max_ *= speed_factor;
ddq_max_start_ *= speed_factor;
ddq_max_goal_ *= speed_factor;
dq_max_sync_.setZero();
q_start_.setZero();
delta_q_.setZero();
t_1_sync_.setZero();
t_2_sync_.setZero();
t_f_sync_.setZero();
q_1_.setZero();
}
/**
* MotionGenerator::calculateDesiredValues 函数
*
* 这个函数计算在时间 t 时刻每个关节的目标位置变化量 delta_q_d。
*
* @param t 当前时间。
* @param delta_q_d 存储计算出的目标位置变化量的向量。
* @return 如果所有关节的运动都已完成,则返回 true,否则返回 false。
*/
bool MotionGenerator::calculateDesiredValues(double t, Vector7d* delta_q_d) const {
Vector7i sign_delta_q;
sign_delta_q << delta_q_.cwiseSign().cast<int>(); // 获取每个关节位置变化量的符号
Vector7d t_d = t_2_sync_ - t_1_sync_;
Vector7d delta_t_2_sync = t_f_sync_ - t_2_sync_;
std::array<bool, 7> joint_motion_finished{}; // 用于记录每个关节的运动是否完成
for (size_t i = 0; i < 7; i++) {
if (std::abs(delta_q_[i]) < kDeltaQMotionFinished) {// 如果关节位置变化量小于阈值,则认为运动已完成
(*delta_q_d)[i] = 0;
joint_motion_finished[i] = true;
} else {// 根据时间段计算目标位置变化量
if (t < t_1_sync_[i]) {//计算关节 i 的目标位置变化量,使用三次方计算公式
(*delta_q_d)[i] = -1.0 / std::pow(t_1_sync_[i], 3.0) * dq_max_sync_[i] * sign_delta_q[i] *
(0.5 * t - t_1_sync_[i]) * std::pow(t, 3.0);
} else if (t >= t_1_sync_[i] && t < t_2_sync_[i]) {//// 计算关节 i 的目标位置变化量,使用线性插值公式
(*delta_q_d)[i] = q_1_[i] + (t - t_1_sync_[i]) * dq_max_sync_[i] * sign_delta_q[i];
} else if (t >= t_2_sync_[i] && t < t_f_sync_[i]) {//// 计算关节 i 的目标位置变化量,使用三次方缓和曲线公式
(*delta_q_d)[i] =
delta_q_[i] + 0.5 *
(1.0 / std::pow(delta_t_2_sync[i], 3.0) *
(t - t_1_sync_[i] - 2.0 * delta_t_2_sync[i] - t_d[i]) *
std::pow((t - t_1_sync_[i] - t_d[i]), 3.0) +
(2.0 * t - 2.0 * t_1_sync_[i] - delta_t_2_sync[i] - 2.0 * t_d[i])) *
dq_max_sync_[i] * sign_delta_q[i];
} else {
(*delta_q_d)[i] = delta_q_[i];
joint_motion_finished[i] = true;
}
}
}
// 如果所有关节的运动都已完成,则返回 true
return std::all_of(joint_motion_finished.cbegin(), joint_motion_finished.cend(),
[](bool x) { return x; });
}
void MotionGenerator::calculateSynchronizedValues() {
Vector7d dq_max_reach(dq_max_);
Vector7d t_f = Vector7d::Zero();
Vector7d delta_t_2 = Vector7d::Zero();
Vector7d t_1 = Vector7d::Zero();
Vector7d delta_t_2_sync = Vector7d::Zero();
Vector7i sign_delta_q;
sign_delta_q << delta_q_.cwiseSign().cast<int>();
for (size_t i = 0; i < 7; i++) {
if (std::abs(delta_q_[i]) > kDeltaQMotionFinished) {
if (std::abs(delta_q_[i]) < (3.0 / 4.0 * (std::pow(dq_max_[i], 2.0) / ddq_max_start_[i]) +
3.0 / 4.0 * (std::pow(dq_max_[i], 2.0) / ddq_max_goal_[i]))) {
dq_max_reach[i] = std::sqrt(4.0 / 3.0 * delta_q_[i] * sign_delta_q[i] *
(ddq_max_start_[i] * ddq_max_goal_[i]) /
(ddq_max_start_[i] + ddq_max_goal_[i]));
}
t_1[i] = 1.5 * dq_max_reach[i] / ddq_max_start_[i];
delta_t_2[i] = 1.5 * dq_max_reach[i] / ddq_max_goal_[i];
t_f[i] = t_1[i] / 2.0 + delta_t_2[i] / 2.0 + std::abs(delta_q_[i]) / dq_max_reach[i];
}
}
double max_t_f = t_f.maxCoeff();
for (size_t i = 0; i < 7; i++) {
if (std::abs(delta_q_[i]) > kDeltaQMotionFinished) {
double a = 1.5 / 2.0 * (ddq_max_goal_[i] + ddq_max_start_[i]);
double b = -1.0 * max_t_f * ddq_max_goal_[i] * ddq_max_start_[i];
double c = std::abs(delta_q_[i]) * ddq_max_goal_[i] * ddq_max_start_[i];
double delta = b * b - 4.0 * a * c;
if (delta < 0.0) {
delta = 0.0;
}
dq_max_sync_[i] = (-1.0 * b - std::sqrt(delta)) / (2.0 * a);
t_1_sync_[i] = 1.5 * dq_max_sync_[i] / ddq_max_start_[i];
delta_t_2_sync[i] = 1.5 * dq_max_sync_[i] / ddq_max_goal_[i];
t_f_sync_[i] =
(t_1_sync_)[i] / 2.0 + delta_t_2_sync[i] / 2.0 + std::abs(delta_q_[i] / dq_max_sync_[i]);
t_2_sync_[i] = (t_f_sync_)[i] - delta_t_2_sync[i];
q_1_[i] = (dq_max_sync_)[i] * sign_delta_q[i] * (0.5 * (t_1_sync_)[i]);
}
}
}
franka::JointPositions MotionGenerator::operator()(const franka::RobotState& robot_state,
franka::Duration period) {
time_ += period.toSec();
if (time_ == 0.0) {
q_start_ = Vector7d(robot_state.q_d.data());
delta_q_ = q_goal_ - q_start_;
calculateSynchronizedValues();
}
Vector7d delta_q_d;
bool motion_finished = calculateDesiredValues(time_, &delta_q_d);
std::array<double, 7> joint_positions;
Eigen::VectorXd::Map(&joint_positions[0], 7) = (q_start_ + delta_q_d);
franka::JointPositions output(joint_positions);
output.motion_finished = motion_finished;
return output;
}