#ifndef LINUXCNCLINEARDELTAKINS_COMMON_H
#define LINUXCNCLINEARDELTAKINS_COMMON_H
#include "emcpos.h"
static double L, R;
static double Ax, Ay, Bx, By, Cx, Cy, L2;
#define SQ3 (sqrt(3))
#define SIN_60 (SQ3/2)
#define COS_60 (.5)
static double sq(double x) { return x*x; }
static void set_geometry(double r_, double l_)
{
if(L == l_ && R == r_) return;
L = l_;
R = r_;
L2 = sq(L);
Ax = 0.0;
Ay = R;
Bx = -SIN_60 * R;
By = -COS_60 * R;
Cx = SIN_60 * R;
Cy = -COS_60 * R;
}
static int kinematics_inverse(const EmcPose *pos, double *joints)
{
double x = pos->tran.x, y = pos->tran.y, z = pos->tran.z;
joints[0] = z + sqrt(L2 - sq(Ax-x) - sq(Ay-y));
joints[1] = z + sqrt(L2 - sq(Bx-x) - sq(By-y));
joints[2] = z + sqrt(L2 - sq(Cx-x) - sq(Cy-y));
joints[3] = pos->a;
joints[4] = pos->b;
joints[5] = pos->c;
joints[6] = pos->u;
joints[7] = pos->v;
joints[8] = pos->w;
return isnan(joints[0]) || isnan(joints[1]) || isnan(joints[2])
? -1 : 0;
}
static int kinematics_forward(const double *joints, EmcPose *pos)
{
double q1 = joints[0];
double q2 = joints[1];
double q3 = joints[2];
double den = (By-Ay)*Cx-(Cy-Ay)*Bx;
double w1 = Ay*Ay + q1*q1; double w2 = Bx*Bx + By*By + q2*q2;
double w3 = Cx*Cx + Cy*Cy + q3*q3;
double a1 = (q2-q1)*(Cy-Ay)-(q3-q1)*(By-Ay);
double b1 = -((w2-w1)*(Cy-Ay)-(w3-w1)*(By-Ay))/2.0;
double a2 = -(q2-q1)*Cx+(q3-q1)*Bx;
double b2 = ((w2-w1)*Cx - (w3-w1)*Bx)/2.0;
double a = a1*a1 + a2*a2 + den*den;
double b = 2*(a1*b1 + a2*(b2-Ay*den) - q1*den*den);
double c = (b2-Ay*den)*(b2-Ay*den) + b1*b1 + den*den*(q1*q1 - L*L);
double discr = b*b - 4.0*a*c;
if (discr < 0) return -1;
double z = -0.5*(b+sqrt(discr))/a;
pos->tran.z = z;
pos->tran.x = (a1*z + b1)/den;
pos->tran.y = (a2*z + b2)/den;
pos->a = joints[3];
pos->b = joints[4];
pos->c = joints[5];
pos->u = joints[6];
pos->v = joints[7];
pos->w = joints[8];
return 0;
}
#define DELTA_DIAGONAL_ROD 269.0
#define DELTA_SMOOTH_ROD_OFFSET 198.25
#define DELTA_EFFECTOR_OFFSET 33.0
#define DELTA_CARRIAGE_OFFSET 35.0
#define DELTA_RADIUS (DELTA_SMOOTH_ROD_OFFSET-DELTA_EFFECTOR_OFFSET-DELTA_CARRIAGE_OFFSET)
#endif