Getting Started¶
This page shows how to add KSL to a program and explains the conventions used in its data structures and function names. Installation covers building the library itself.
Using KSL in your project¶
Include ksl/ksl.h to get the data types and nearly all of the functions.
A few headers are not included by ksl/ksl.h and must be included
separately when they are needed:
ksl/trig.h:ksl_dc()(the{sin, cos}pair of an angle) and angle normalizationksl/eulerangles.h: Euler angle typesksl/memory.h: allocation of two-dimensional arrays
Most functions are static inline functions defined in the headers. The
rest (LU factorization, matrix inversion, printing, trigonometry utilities)
are in a small static library, so a program also needs to link against
libksl and the math library -lm.
With meson, add KSL as a subproject by placing a wrap file in
subprojects/ksl.wrap:
[wrap-git]
url = https://github.com/shift-dynamics/ksl.git
revision = master
[provide]
dependency_names = ksl
and use it as a dependency:
ksl_dep = dependency('ksl')
executable('myprogram', 'main.c', dependencies: ksl_dep)
Without meson, build KSL as described in Installation, then add the repository root to the include path and link the static library:
cc -std=c11 -O2 -I path/to/ksl main.c path/to/ksl/build/libksl.a -lm
KSL is written in C11 and can also be included from C++.
Data layout¶
All types are unions, so the same data can be read as named fields or as an array.
ksl_vec3_t:x,y,z, orat[3].ksl_mat3x3_t: a 3x3 matrix stored in column major order.at[j][i]is rowiof columnj,m01is row 0 of column 1, andv[j](orv0,v1,v2) is columnjas aksl_vec3_t.ksl_SE3_t: a rigid body transformation (displacement) \(D = (R, t)\), stored as a rotation matrixRfollowed by a translationt, 12 values in all. The same data isat[4][3]in column major order, so the translation isat[3](orv3). It acts on a point as \(D p = R p + t\).ksl_screw_t: a 6 element screw, such as a velocity, with a linear partlinfollowed by an angular partang. For a velocity screw,linis the velocity of the body point that is momentarily at the origin of the frame the screw is expressed in, andangis the angular velocity.ksl_coscrew_t: a 6 element coscrew, such as a force, with a linear partlin(the force) followed by an angular partang(the moment about the origin).ksl_quaternion_t:x,y,z,w, with the scalar partwlast.ksl_inertia_t: the mass, the first moment of mass and the 6 unique terms of the inertia matrix, 10 values in all.
Every type has a single precision version with an f suffix, for example
ksl_SE3f_t.
Naming conventions¶
Function names describe the operation and the operands. For example,
ksl_product_dv is the product of a displacement d and a vector v,
and ksl_product_Adsinv applies the adjoint Ad to a negated screw
sinv. The operand abbreviations are:
Abbreviation |
Argument type |
Meaning |
|---|---|---|
|
|
rigid body transformation (displacement), \(D = (R, t)\) |
|
|
rotation only, \((R, 0)\) |
|
|
rotation about the x, y or z axis, given as |
|
|
translation along the x, y or z axis |
|
|
quaternion for a rotation about the x, y or z axis, given as
|
|
|
vector |
|
|
screw |
|
|
coscrew |
|
|
quaternion |
|
|
scalar |
|
|
adjoint (for screws) and coadjoint (for coscrews) of a transformation, applied without forming a 6x6 matrix |
|
|
adjoint and coadjoint of a rotation only |
|
|
adjoint and coadjoint of a translation only |
|
|
the screw of a joint axis, or the coscrew of a force or moment along an axis, of a frame given by a transformation (see below) |
The suffix inv changes the meaning of an operand:
after a transformation (
dinv,drinv,Adinv) it means the inverse transformation. The inverse is applied directly; it is never computed and stored.after a vector, screw or coscrew (
vinv,sinv,cinv) it means the negative of that quantity.
Functions that end in ed (ksl_SE3_inverted, ksl_vec3_normalized)
write the result to a separate output. The same name without ed
(ksl_SE3_invert) works in place.
The hs and hc functions return the axis quantities of a frame
\(D\): ksl_hsrz(D) is the screw of a revolute joint about the frame’s
z axis, \(\mathrm{Ad}(D) \{0, e_z\}\); ksl_hstz(D) is the screw of a
prismatic joint along it, \(\mathrm{Ad}(D) \{e_z, 0\}\). Likewise
ksl_hctz(D) is a unit force along the z axis and ksl_hcrz(D) a unit
moment about it.
Axis rotations take {sin, cos}¶
Functions for rotations about a coordinate axis take the angle as a two
element array dc = {sin(theta), cos(theta)} rather than the angle itself.
A program often needs the same rotation several times, and this way the sine
and cosine are computed only once. Use ksl_dc() from ksl/trig.h to
fill the array:
double dc[2];
ksl_dc(theta, dc); /* dc = {sin(theta), cos(theta)} */
ksl_product_ddrz(&D, dc, &Do); /* Do = D * Rz(theta) */
The order is always sine first, then cosine.
Inputs and outputs¶
Inputs come first and outputs last. Function arguments are pointers; outputs
are written into memory provided by the caller and the library never
allocates memory, except for the functions in ksl/memory.h. Inputs and
outputs of a function must not overlap unless the documentation says
otherwise, because the functions are declared with restrict pointers. Use
the in-place version of a function, where one exists, to overwrite an input.
Example¶
This program computes the tip position and tip velocity of a planar arm with two revolute joints, and compares them with the closed-form result. It is built and run as part of the KSL test suite.
/* Position and velocity of the tip of a planar two-link arm.
Joint 1 rotates link 1 about the base z axis. Joint 2 is at the end of link 1,
a distance L1 along its x axis, and rotates link 2 about z. The tip is a
distance L2 along the x axis of link 2.
The program prints the tip position and velocity computed with KSL and with
the closed-form expressions, and returns 0 if they agree. */
#include <math.h>
#include <stdio.h>
#include "ksl/ksl.h"
#include "ksl/trig.h"
int main(void) {
const double L1 = 0.5, L2 = 0.3; /* link lengths, m */
const double q1 = 0.4, q2 = -1.1; /* joint angles, rad */
const double q1dot = 0.7, q2dot = 1.5; /* joint rates, rad/s */
/* Each axis rotation takes the {sin, cos} pair of its angle. */
double dc1[2], dc2[2];
ksl_dc(q1, dc1);
ksl_dc(q2, dc2);
/* Pose of link 1 in the base frame: D1 = drz(q1). */
const ksl_SE3_t I = ksl_SE3_identity();
ksl_SE3_t D1;
ksl_product_ddrz(&I, dc1, &D1);
/* Pose of link 2: D2 = D1 * dtx(L1) * drz(q2). The specialized products
update only the terms that a translation or an axis rotation changes. */
ksl_SE3_t D1t, D2;
ksl_product_ddtx(&D1, L1, &D1t);
ksl_product_ddrz(&D1t, dc2, &D2);
/* Tip position: p = D2 * {L2, 0, 0}. */
const ksl_vec3_t tip_local = {.x = L2, .y = 0.0, .z = 0.0};
ksl_vec3_t p;
ksl_product_dv(&D2, &tip_local, &p);
/* Joint axis screws in the base frame, hs = Ad(D) * {0, z}. Their linear
part is the velocity of the point at the base origin for a unit joint
rate. */
ksl_screw_t h1, h2;
ksl_hsrz(&D1, &h1);
ksl_hsrz(&D2, &h2);
/* Velocity screw of link 2: V = h1 * q1dot + h2 * q2dot. */
ksl_screw_t V;
ksl_product_as(q1dot, &h1, &V);
ksl_axpy_ss(q2dot, &h2, &V);
/* Velocity of the tip: v = V.lin + V.ang x p. */
ksl_vec3_t w_x_p, v;
ksl_cross_vv(&V.ang, &p, &w_x_p);
ksl_add_vv(&V.lin, &w_x_p, &v);
/* Closed-form results for comparison. */
const double q12 = q1 + q2, q12dot = q1dot + q2dot;
const double px = L1 * cos(q1) + L2 * cos(q12);
const double py = L1 * sin(q1) + L2 * sin(q12);
const double vx = -L1 * sin(q1) * q1dot - L2 * sin(q12) * q12dot;
const double vy = L1 * cos(q1) * q1dot + L2 * cos(q12) * q12dot;
printf("tip position ksl: % .6f % .6f % .6f\n", p.x, p.y, p.z);
printf(" closed form: % .6f % .6f % .6f\n", px, py, 0.0);
printf("tip velocity ksl: % .6f % .6f % .6f\n", v.x, v.y, v.z);
printf(" closed form: % .6f % .6f % .6f\n", vx, vy, 0.0);
const double tol = 1e-12;
const int ok = fabs(p.x - px) < tol && fabs(p.y - py) < tol &&
fabs(p.z) < tol && fabs(v.x - vx) < tol &&
fabs(v.y - vy) < tol && fabs(v.z) < tol;
return ok ? 0 : 1;
}
Its output is:
tip position ksl: 0.689983 0.001444 0.000000
closed form: 0.689983 0.001444 0.000000
tip velocity ksl: 0.288887 0.827167 0.000000
closed form: 0.288887 0.827167 0.000000