A path says where to go; a trajectory says when to be where. This example moves the spatial arm of Forward kinematics from one joint configuration to another in 2 s, with three ways of timing the motion, and compares what they ask of the motors. Then it compares a straight line in joint space with a straight line in the world.
A point to point motion from \(q_0\) to \(q_1\) follows the straight line between them,
\[q(t) = q_0 + s(t)\,(q_1 - q_0), \qquad \dot q = \dot s\,(q_1 - q_0), \qquad
\ddot q = \ddot s\,(q_1 - q_0)
\]
The cubic is the simplest polynomial that starts and ends at rest. Its acceleration jumps from 0 to \(6/T^2\) when the motion starts: an infinite jerk, which shakes an arm and what it carries. The quintic also starts and ends with no acceleration, so it is smooth, but it has to go faster in the middle, \(1.875/T\) against \(1.5/T\). The trapezoid is what most motor controllers do: they are given a top speed and an acceleration, and its top speed is the lowest of the three for a given time.
A straight line between two joint configurations is not a straight line for the tip of the arm: the tip swings along a curve. To move the tip along a straight line in the world, the example takes the point on the line at \(s(t)\) and finds the joint angles for it with the inverse kinematics of Inverse kinematics, at every step.
1. Three ways to time the same motion
t [s] cubic s ds/dt d2s/dt2 quintic s ds/dt d2s/dt2 trapezoid s ds/dt d2s/dt2
0.00 0.000 0.000 1.500 0.000 0.000 0.000 0.000 0.000 1.333
0.25 0.043 0.328 1.125 0.016 0.179 1.230 0.042 0.333 1.333
0.50 0.156 0.562 0.750 0.104 0.527 1.406 0.167 0.667 0.000
0.75 0.316 0.703 0.375 0.275 0.824 0.879 0.333 0.667 0.000
1.00 0.500 0.750 0.000 0.500 0.938 0.000 0.500 0.667 0.000
1.25 0.684 0.703 -0.375 0.725 0.824 -0.879 0.667 0.667 0.000
1.50 0.844 0.562 -0.750 0.896 0.527 -1.406 0.833 0.667 0.000
1.75 0.957 0.328 -1.125 0.984 0.179 -1.230 0.958 0.333 -1.333
2.00 1.000 0.000 -1.500 1.000 0.000 0.000 1.000 0.000 -1.333
2. On the arm
motion top joint speed top joint accel accel at t = 0 top jerk
[deg/s] [deg/s^2] [deg/s^2] [deg/s^3]
cubic 97.5 195.0 195.0 195000
quintic 121.9 187.6 0.0 974
trapezoid 86.7 173.3 173.3 173333
(the jerk is from steps of 1 ms: a jump in acceleration shows as a huge one)
3. A straight line in joint space is not one in the world
tip at the start (0.284, -0.493, 0.207)
tip at the end (0.184, 0.507, 0.709)
straight line [m] 1.123
joint space: tip travels [m] 1.451
joint space: off the line [m] 0.384
world: off the line [m] 4.5e-07
world: top joint speed [deg/s] 230.460
The base joint turns by 130 degrees, the most of the three, and sets the top speeds. The cubic and the trapezoid start with an acceleration of 195 and 173 deg/s² out of nothing; the quintic starts with none, at the price of a top speed a quarter higher. With the joints on a straight line the tip travels 1.45 m and passes 38 cm from the straight line between its ends. On the straight line in the world it stays within a micrometre of it, but the joints have to turn faster, up to 230 deg/s: the line passes closer to the base, where the base has to turn quickly to follow it.
#include "bitsim/bitsim_config.h"
#ifdef BITSIM_CHRONO
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
{
using chrono::ChVector3d;
{
{
m = std::max(m, std::abs(x));
}
return m;
}
{
std::cout << " t [s] cubic s ds/dt d2s/dt2 quintic s ds/dt d2s/dt2"
" trapezoid s ds/dt d2s/dt2\n";
for (int k = 0; k <= 8; ++k)
{
std::cout << std::fixed << std::setprecision(2) << std::setw(9) << t;
{
std::cout << std::setprecision(3) << std::setw(timing == &cubic ? 10 : 12)
<< timing->
s(t) << std::setw(8) << timing->
ds(t) << std::setw(9)
}
std::cout << "\n";
}
}
{
struct Candidate
{
std::string name;
std::shared_ptr<const TimeScaling> timing;
};
{
"cubic", std::make_shared<CubicTimeScaling>(
DURATION)},
{
"quintic", std::make_shared<QuinticTimeScaling>(
DURATION)},
{"trapezoid", std::make_shared<TrapezoidalTimeScaling>(
std::cout << " motion top joint speed top joint accel accel at t = 0 top jerk\n"
<< " [deg/s] [deg/s^2] [deg/s^2] [deg/s^3]\n";
for (const auto &candidate : candidates)
{
{
top_speed = std::max(top_speed,
largest(motion.velocity(t)));
top_accel = std::max(top_accel, accel);
top_jerk = std::max(top_jerk, std::abs(accel - last_accel) /
DT);
last_accel = accel;
}
top_jerk = std::max(top_jerk,
largest(motion.acceleration(0.0)) /
DT);
std::cout << " " << std::left << std::setw(11) << candidate.name << std::right
<< std::fixed << std::setprecision(1) << std::setw(17) << top_speed /
DEG
<< std::setw(18) << top_accel /
DEG << std::setw(17)
<<
largest(motion.acceleration(0.0)) /
DEG << std::setw(11)
<< std::setprecision(0) << top_jerk /
DEG <<
"\n";
}
std::cout <<
" (the jerk is from steps of " <<
DT * 1000 <<
" ms: a jump in acceleration "
<< "shows as a huge one)\n";
}
{
print_title(
"3. A straight line in joint space is not one in the world");
const auto timing = std::make_shared<QuinticTimeScaling>(
DURATION);
const auto off_line = [&](const ChVector3d &p)
{
const ChVector3d d = (p_end - p_start).GetNormalized();
const ChVector3d r = p - p_start;
return (r - d * r.Dot(d)).Length();
};
ChVector3d last = p_start;
{
joint_path += (p - last).Length();
joint_off = std::max(joint_off, off_line(p));
last = p;
const ChVector3d target = p_start + (p_end - p_start) * timing->
s(t);
q = result.q;
world_off = std::max(world_off, off_line(
spatial_fk(
ARM, q).GetPos()));
for (int j = 0; j < 3; ++j)
{
world_top_speed = std::max(world_top_speed, std::abs(q[j] - q_last[j]) /
DT);
}
q_last = q;
}
print_number(
"straight line [m]", (p_end - p_start).Length(), 30);
print_number(
"joint space: tip travels [m]", joint_path, 30);
print_number(
"joint space: off the line [m]", joint_off, 30);
}
}
{
return 0;
}
#else
#include <iostream>
{
std::cerr << "point_to_point needs bitsim configured with ENABLE_CHRONO=ON" << std::endl;
return 1;
}
#endif
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
The cubic polynomial that starts and ends at rest, with .
Definition time_scaling.h:66
A straight motion from one point of any dimension, such as the joint angles of an arm,...
Definition time_scaling.h:138
The quintic polynomial that starts and ends at rest with no acceleration, .
Definition time_scaling.h:84
How fast to go along a path: the share s(t) of the path done at time t, from s(0) = 0 to s(T) = 1,...
Definition time_scaling.h:24
virtual real_t dds(real_t t) const =0
[1/s^2]
virtual real_t s(real_t t) const =0
s(t); 0 before the start and 1 after the end
virtual real_t ds(real_t t) const =0
[1/s]
static TrapezoidalTimeScaling with_accel_share(real_t duration, real_t accel_share)
The trapezoid that takes duration and spends a share of it, accel_share, in each of the speeding up a...
Definition time_scaling.cpp:97
int main()
Definition discounted_returns.cpp:19
ChFrame spatial_fk(const SpatialArm &arm, const SpatialJoints &q)
The pose of the end-effector in the world, .
Definition kinematics.cpp:121
std::array< real_t, 3 > SpatialJoints
The angles of the three joints of a SpatialArm [rad].
Definition spatial_arm.h:41
const real_t PI
The PI constant.
Definition bitsim_consts.h:63
Definition denavit_hartenberg.h:18
IKResult numerical_ik(const SpatialArm &arm, const ChVector3d &target, SpatialJoints q0, real_t damping, int max_iterations, real_t tolerance, real_t max_step, const IKObserver &observer)
Definition kinematics.cpp:175
Definition line_segment_link.h:11
Definition csv_file_reader.cpp:12
void print_error(const std::string &label, real_t value, int width)
The label and the value in scientific notation with one decimal, for small errors.
Definition print_utils.cpp:33
void print_number(const std::string &label, real_t value, int width)
The label and the value, with 3 decimals.
Definition print_utils.cpp:21
std::ostream & print_vector(std::ostream &out, const std::vector< T > &obs)
Definition io_utils.h:21
real_t clean(real_t x, real_t zero)
x, or 0 when zero
Definition print_utils.cpp:15
void print_title(const std::string &title, std::ostream &out)
An empty line and then the title.
Definition print_utils.cpp:17
double real_t
real_t
Definition bitsim_types.h:20
std::vector< Candidate > candidates(const Solver &solver, const TrackModel &model)
Definition mcts_line_following.cpp:298
Definition point_to_point.cpp:27
void the_three_timings()
Definition point_to_point.cpp:56
void joint_space_or_world()
Definition point_to_point.cpp:127
SpatialJoints joints(const std::vector< real_t > &q)
Definition point_to_point.cpp:44
const SpatialArm ARM
Definition point_to_point.cpp:40
void compare_on_the_arm()
Definition point_to_point.cpp:82
const real_t DT
Definition point_to_point.cpp:37
const real_t DEG
Definition point_to_point.cpp:35
const real_t DURATION
Definition point_to_point.cpp:36
const std::vector< real_t > Q_END
Definition point_to_point.cpp:42
const std::vector< real_t > Q_START
Definition point_to_point.cpp:41
real_t largest(const std::vector< real_t > &v)
Definition point_to_point.cpp:46
Print labelled values, one per line, as the examples do: two spaces, the label padded to a width,...
An arm with three revolute joints. Joint 1 turns the arm about the vertical axis at height h0; joints...
Definition spatial_arm.h:31