bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
Point to point motions

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.

What you need

bitsim built with Chrono, for the arm's kinematics; see Installing bitsim.

Time scaling

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) \]

where the time scaling \(s(t)\) goes from 0 at the start to 1 at the end, at time \(T\). The path is the same for every time scaling; only its timing changes. bitsim has three, in bitsim/trajectory/time_scaling.h:

Time scaling \(s(\tau)\), \(\tau = t / T\) starts and stops
CubicTimeScaling \(3\tau^2 - 2\tau^3\) at rest, but with a jump in acceleration
QuinticTimeScaling \(10\tau^3 - 15\tau^4 + 6\tau^5\) at rest and with no acceleration
TrapezoidalTimeScaling constant acceleration, cruise, constant braking at rest, with a jump in acceleration

PointToPoint moves between two points of any dimension, here the three joint angles, with one of them.

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 in the world

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.

Running the example

./point_to_point

Output

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.

Things to try

  1. Make the trapezoid's acceleration share 0.5, a triangle. How do its top speed and acceleration compare with the cubic's?
  2. Time the straight line in the world with the cubic instead of the quintic. What changes in the joint speeds?
  3. Move the end point closer to the base. How fast does the base joint have to turn on the straight line in the world, and why?

The code

// Point to point motions of the spatial arm of the kinematics examples. The arm moves from one
// joint configuration to another in 2 s, timed by a cubic, a quintic and a trapezoidal time
// scaling, and the example compares their speeds, accelerations and how they start. Then it
// compares a straight line in joint space with a straight line in the world.
//
// Usage: point_to_point
#include "bitsim/bitsim_config.h"
#ifdef BITSIM_CHRONO
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
namespace point_to_point
{
using chrono::ChVector3d;
using namespace bitsim::kinematics;
using namespace bitsim::trajectory;
using namespace bitsim::utils::io;
const real_t DURATION = 2.0; // [s]
const real_t DT = 0.001; // [s] to sample the motions
// The arm of the kinematics examples, from a pose on its right to one on its left
const std::vector<real_t> Q_START{-60.0 * DEG, 20.0 * DEG, -70.0 * DEG};
const std::vector<real_t> Q_END{70.0 * DEG, 50.0 * DEG, -30.0 * DEG};
SpatialJoints joints(const std::vector<real_t> &q) { return {q[0], q[1], q[2]}; }
real_t largest(const std::vector<real_t> &v)
{
real_t m = 0.0;
for (const real_t x : v)
{
m = std::max(m, std::abs(x));
}
return m;
}
{
print_title("1. Three ways to time the same motion");
const CubicTimeScaling cubic(DURATION);
const QuinticTimeScaling quintic(DURATION);
const auto trapezoid = TrapezoidalTimeScaling::with_accel_share(DURATION, 0.25);
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)
{
const real_t t = 0.25 * k;
std::cout << std::fixed << std::setprecision(2) << std::setw(9) << t;
for (const TimeScaling *timing :
{static_cast<const TimeScaling *>(&cubic), static_cast<const TimeScaling *>(&quintic),
static_cast<const TimeScaling *>(&trapezoid)})
{
std::cout << std::setprecision(3) << std::setw(timing == &cubic ? 10 : 12)
<< timing->s(t) << std::setw(8) << timing->ds(t) << std::setw(9)
<< clean(timing->dds(t));
}
std::cout << "\n";
}
}
{
print_title("2. On the arm");
struct Candidate
{
std::string name;
std::shared_ptr<const TimeScaling> timing;
};
const std::vector<Candidate> candidates{
{"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)
{
const PointToPoint motion(Q_START, Q_END, candidate.timing);
real_t top_speed = 0.0;
real_t top_accel = 0.0;
real_t top_jerk = 0.0;
real_t last_accel = largest(motion.acceleration(0.0));
for (real_t t = DT; t <= DURATION + 1e-9; t += DT)
{
top_speed = std::max(top_speed, largest(motion.velocity(t)));
const real_t accel = largest(motion.acceleration(t));
top_accel = std::max(top_accel, accel);
top_jerk = std::max(top_jerk, std::abs(accel - last_accel) / DT);
last_accel = accel;
}
// The jump from rest at t = 0 counts too
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");
// The same start and end, once with the joints moving together along a straight line in
// joint space, once with the tip moving along a straight line in the world, by inverse
// kinematics at every step
const auto timing = std::make_shared<QuinticTimeScaling>(DURATION);
const PointToPoint in_joints(Q_START, Q_END, timing);
const ChVector3d p_start = spatial_fk(ARM, joints(Q_START)).GetPos();
const ChVector3d p_end = spatial_fk(ARM, joints(Q_END)).GetPos();
print_vector("tip at the start", p_start, 30);
print_vector("tip at the end", p_end, 30);
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();
};
real_t joint_path = 0.0;
real_t joint_off = 0.0;
real_t world_off = 0.0;
real_t world_top_speed = 0.0;
ChVector3d last = p_start;
SpatialJoints q_last = q;
for (real_t t = DT; t <= DURATION + 1e-9; t += DT)
{
const ChVector3d p = spatial_fk(ARM, joints(in_joints.position(t))).GetPos();
joint_path += (p - last).Length();
joint_off = std::max(joint_off, off_line(p));
last = p;
// The tip on the straight line, at the share of the way the timing gives
const ChVector3d target = p_start + (p_end - p_start) * timing->s(t);
const auto result = numerical_ik(ARM, target, q, 0.05);
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);
print_error("world: off the line [m]", world_off, 30);
print_number("world: top joint speed [deg/s]", world_top_speed / DEG, 30);
}
} // namespace point_to_point
int main()
{
return 0;
}
#else
#include <iostream>
int main()
{
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