Skip to content

Commit f3fd553

Browse files
committed
convert all kinematics modules to nonrt_attach architecture
Each module now exports nonrt_attach(shmem_base, offset, ops) instead of nonrt_kinematicsForward/Inverse. The planner calls nonrt_attach once at startup to get the shmem pointer and register forward/inverse ops. Modules with HAL-configurable params use a !haldata guard in the RT forward/inverse functions to detect the userspace context and read from shmem via KINS_SHMEM_READ. Modules with no configurable params (corexykins, rotatekins, scorbot-kins) register the RT functions directly. trivkins sets is_identity=1 and skips kinematics entirely. Also updates kinematics.adoc to describe the current architecture.
1 parent eba8e59 commit f3fd553

26 files changed

Lines changed: 1301 additions & 1083 deletions

docs/src/motion/kinematics.adoc

Lines changed: 169 additions & 109 deletions
Large diffs are not rendered by default.

src/emc/kinematics/5axiskins.c

Lines changed: 77 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
#include <kinematics.h>
6363

6464
#include "switchkins.h"
65+
#include "hal_priv.h"
66+
#include "kinematics_params.h"
6567

6668
/* ========================================================================
6769
* Internal math (was in 5axiskins_math.h)
@@ -162,6 +164,9 @@ struct haldata {
162164
} *haldata;
163165
static int fiveaxis_max_joints;
164166

167+
/* Userspace-accessible params in HAL shmem */
168+
static kinematics_params_t *uspace_params;
169+
165170
// Joint mapping struct
166171
static fiveaxis_joints_t jmap;
167172

@@ -182,35 +187,61 @@ static int fiveaxis_KinematicsForward(const double *joints,
182187
const KINEMATICS_FORWARD_FLAGS * fflags,
183188
KINEMATICS_INVERSE_FLAGS * iflags)
184189
{
185-
(void)fflags;
186-
(void)iflags;
190+
(void)fflags; (void)iflags;
187191
fiveaxis_params_t params;
192+
193+
if (!haldata) {
194+
/* userspace path: read params from HAL shmem set up by nonrt_attach */
195+
kinematics_params_t local;
196+
KINS_SHMEM_READ(uspace_params, local);
197+
params.pivot_length = local.params.fiveaxis.pivot_length;
198+
fiveaxis_joints_t jm = {
199+
local.axis_to_joint[0], local.axis_to_joint[1], local.axis_to_joint[2],
200+
local.axis_to_joint[3], local.axis_to_joint[4], local.axis_to_joint[5],
201+
local.axis_to_joint[6], local.axis_to_joint[7], local.axis_to_joint[8]
202+
};
203+
return fiveaxis_forward_impl(&params, &jm, joints, pos);
204+
}
205+
188206
params.pivot_length = *(haldata->pivot_length);
207+
if (uspace_params) {
208+
uspace_params->head++;
209+
uspace_params->params.fiveaxis.pivot_length = params.pivot_length;
210+
uspace_params->tail = uspace_params->head;
211+
}
189212
return fiveaxis_forward_impl(&params, &jmap, joints, pos);
190-
} //fiveaxis_KinematicsForward()
213+
}
191214

192215
static int fiveaxis_KinematicsInverse(const EmcPose * pos,
193216
double *joints,
194217
const KINEMATICS_INVERSE_FLAGS * iflags,
195218
KINEMATICS_FORWARD_FLAGS * fflags)
196219
{
197-
(void)iflags;
198-
(void)fflags;
220+
(void)iflags; (void)fflags;
199221
fiveaxis_params_t params;
200-
params.pivot_length = *(haldata->pivot_length);
201222

202-
// Compute axis values using pure math function
223+
if (!haldata) {
224+
/* userspace path: read params from HAL shmem set up by nonrt_attach */
225+
kinematics_params_t local;
226+
KINS_SHMEM_READ(uspace_params, local);
227+
params.pivot_length = local.params.fiveaxis.pivot_length;
228+
fiveaxis_joints_t jm = {
229+
local.axis_to_joint[0], local.axis_to_joint[1], local.axis_to_joint[2],
230+
local.axis_to_joint[3], local.axis_to_joint[4], local.axis_to_joint[5],
231+
local.axis_to_joint[6], local.axis_to_joint[7], local.axis_to_joint[8]
232+
};
233+
EmcPose axis_values;
234+
fiveaxis_inverse_impl(&params, pos, &axis_values);
235+
fiveaxis_axis_to_joints(&jm, &axis_values, joints);
236+
return 0;
237+
}
238+
239+
params.pivot_length = *(haldata->pivot_length);
203240
EmcPose P;
204241
fiveaxis_inverse_impl(&params, pos, &P);
205-
206-
// update joints with support for
207-
// multiple-joints per-coordinate letter:
208-
// based on computed position
209-
position_to_mapped_joints(fiveaxis_max_joints,
210-
&P,
211-
joints);
242+
position_to_mapped_joints(fiveaxis_max_joints, &P, joints);
212243
return 0;
213-
} // fiveaxis_kinematicsInverse()
244+
}
214245

215246
int fiveaxis_KinematicsSetup(const int comp_id,
216247
const char* coordinates,
@@ -278,6 +309,28 @@ int fiveaxis_KinematicsSetup(const int comp_id,
278309

279310
*haldata->pivot_length = DEFAULT_PIVOT_LENGTH;
280311

312+
/* Publish kinematics params to HAL shmem for userspace planner */
313+
uspace_params = (kinematics_params_t *)hal_malloc(sizeof(kinematics_params_t));
314+
if (!uspace_params) {
315+
rtapi_print_msg(RTAPI_MSG_ERR, "5axiskins: hal_malloc for uspace_params failed\n");
316+
goto error;
317+
}
318+
result = hal_param_s32_newf(HAL_RO, &uspace_params->self_offset, comp_id,
319+
"%s.uspace-params-offset", kp->halprefix);
320+
if (result < 0) goto error;
321+
322+
memset(uspace_params, 0, sizeof(*uspace_params));
323+
uspace_params->num_joints = fiveaxis_max_joints;
324+
uspace_params->axis_to_joint[0] = JX; uspace_params->axis_to_joint[1] = JY;
325+
uspace_params->axis_to_joint[2] = JZ; uspace_params->axis_to_joint[3] = JA;
326+
uspace_params->axis_to_joint[4] = JB; uspace_params->axis_to_joint[5] = JC;
327+
uspace_params->axis_to_joint[6] = JU; uspace_params->axis_to_joint[7] = JV;
328+
uspace_params->axis_to_joint[8] = JW;
329+
uspace_params->params.fiveaxis.pivot_length = DEFAULT_PIVOT_LENGTH;
330+
uspace_params->valid = 1;
331+
uspace_params->is_identity = 0;
332+
uspace_params->self_offset = (int)SHMOFF(uspace_params);
333+
281334
rtapi_print("Kinematics Module %s\n",__FILE__);
282335
rtapi_print(" module name = %s\n"
283336
" coordinates = %s Requires: [KINS]JOINTS>=%d\n"
@@ -333,66 +386,17 @@ int switchkinsSetup(kparms* kp,
333386

334387
/* ========================================================================
335388
* Non-RT interface for userspace trajectory planner
389+
*
390+
* nonrt_attach() is the only exported symbol. It sets uspace_params so the
391+
* !haldata branch in fiveaxis_KinematicsForward/Inverse reads live shmem,
392+
* then registers those functions directly — no separate nonrt functions.
336393
* ======================================================================== */
337-
#include "kinematics_params.h"
338-
339-
int nonrt_kinematicsForward(const void *params,
340-
const double *joints,
341-
EmcPose *pos)
342-
{
343-
const kinematics_params_t *kp = (const kinematics_params_t *)params;
344-
fiveaxis_params_t p;
345-
fiveaxis_joints_t jm;
346-
347-
p.pivot_length = kp->params.fiveaxis.pivot_length;
348-
jm.jx = kp->axis_to_joint[0]; jm.jy = kp->axis_to_joint[1];
349-
jm.jz = kp->axis_to_joint[2]; jm.ja = kp->axis_to_joint[3];
350-
jm.jb = kp->axis_to_joint[4]; jm.jc = kp->axis_to_joint[5];
351-
jm.ju = kp->axis_to_joint[6]; jm.jv = kp->axis_to_joint[7];
352-
jm.jw = kp->axis_to_joint[8];
353-
354-
return fiveaxis_forward_impl(&p, &jm, joints, pos);
355-
}
356-
357-
int nonrt_kinematicsInverse(const void *params,
358-
const EmcPose *pos,
359-
double *joints)
360-
{
361-
const kinematics_params_t *kp = (const kinematics_params_t *)params;
362-
fiveaxis_params_t p;
363-
fiveaxis_joints_t jm;
364-
EmcPose axis_values;
365-
366-
p.pivot_length = kp->params.fiveaxis.pivot_length;
367-
jm.jx = kp->axis_to_joint[0]; jm.jy = kp->axis_to_joint[1];
368-
jm.jz = kp->axis_to_joint[2]; jm.ja = kp->axis_to_joint[3];
369-
jm.jb = kp->axis_to_joint[4]; jm.jc = kp->axis_to_joint[5];
370-
jm.ju = kp->axis_to_joint[6]; jm.jv = kp->axis_to_joint[7];
371-
jm.jw = kp->axis_to_joint[8];
372-
373-
fiveaxis_inverse_impl(&p, pos, &axis_values);
374-
fiveaxis_axis_to_joints(&jm, &axis_values, joints);
375-
return 0;
376-
}
377394

378-
int nonrt_refresh(void *params,
379-
int (*read_float)(const char *, double *),
380-
int (*read_bit)(const char *, int *),
381-
int (*read_s32)(const char *, int *))
395+
void nonrt_attach(char *shmem_base, int offset, nonrt_ops_t *ops)
382396
{
383-
kinematics_params_t *kp = (kinematics_params_t *)params;
384-
(void)read_bit; (void)read_s32;
385-
386-
if (read_float("5axiskins.pivot-length",
387-
&kp->params.fiveaxis.pivot_length) != 0)
388-
return -1;
389-
390-
return 0;
397+
uspace_params = (kinematics_params_t *)(shmem_base + offset);
398+
ops->forward = fiveaxis_KinematicsForward;
399+
ops->inverse = fiveaxis_KinematicsInverse;
391400
}
392401

393-
int nonrt_is_identity(void) { return 0; }
394-
395-
EXPORT_SYMBOL(nonrt_kinematicsForward);
396-
EXPORT_SYMBOL(nonrt_kinematicsInverse);
397-
EXPORT_SYMBOL(nonrt_refresh);
398-
EXPORT_SYMBOL(nonrt_is_identity);
402+
EXPORT_SYMBOL(nonrt_attach);

src/emc/kinematics/corexykins.c

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "rtapi_app.h"
1111
#include "rtapi_string.h"
1212
#include "kinematics.h"
13+
#include "hal_priv.h"
14+
#include "kinematics_params.h"
1315

1416
/*
1517
* CoreXY kinematics - pure linear transformation, no parameters
@@ -51,6 +53,8 @@ static struct data {
5153
hal_s32_t joints[EMCMOT_MAX_JOINTS];
5254
} *data;
5355

56+
static kinematics_params_t *uspace_params;
57+
5458
int kinematicsForward(const double *joints
5559
,EmcPose *pos
5660
,const KINEMATICS_FORWARD_FLAGS *fflags
@@ -99,6 +103,20 @@ int rtapi_app_main(void) {
99103

100104
data = hal_malloc(sizeof(struct data));
101105

106+
uspace_params = (kinematics_params_t *)hal_malloc(sizeof(kinematics_params_t));
107+
if (!uspace_params) { hal_exit(comp_id); return -1; }
108+
if (hal_param_s32_newf(HAL_RO, &uspace_params->self_offset, comp_id,
109+
"corexykins.uspace-params-offset") < 0) {
110+
hal_exit(comp_id); return -1;
111+
}
112+
memset(uspace_params, 0, sizeof(*uspace_params));
113+
uspace_params->num_joints = 9;
114+
uspace_params->valid = 1;
115+
uspace_params->is_identity = 0;
116+
uspace_params->head = 1;
117+
uspace_params->tail = 1;
118+
uspace_params->self_offset = (int)SHMOFF(uspace_params);
119+
102120
hal_ready(comp_id);
103121
return 0;
104122
}
@@ -108,36 +126,12 @@ void rtapi_app_exit(void) { hal_exit(comp_id); }
108126
/* ========================================================================
109127
* Non-RT interface for userspace trajectory planner
110128
* ======================================================================== */
111-
#include "kinematics_params.h"
112-
113-
int nonrt_kinematicsForward(const void *params,
114-
const double *joints,
115-
EmcPose *pos)
116-
{
117-
(void)params;
118-
return corexy_forward_math(joints, pos);
119-
}
120129

121-
int nonrt_kinematicsInverse(const void *params,
122-
const EmcPose *pos,
123-
double *joints)
130+
void nonrt_attach(char *shmem_base, int offset, nonrt_ops_t *ops)
124131
{
125-
(void)params;
126-
return corexy_inverse_math(pos, joints);
132+
(void)shmem_base; (void)offset;
133+
ops->forward = kinematicsForward;
134+
ops->inverse = kinematicsInverse;
127135
}
128136

129-
int nonrt_refresh(void *params,
130-
int (*read_float)(const char *, double *),
131-
int (*read_bit)(const char *, int *),
132-
int (*read_s32)(const char *, int *))
133-
{
134-
(void)params; (void)read_float; (void)read_bit; (void)read_s32;
135-
return 0;
136-
}
137-
138-
int nonrt_is_identity(void) { return 0; }
139-
140-
EXPORT_SYMBOL(nonrt_kinematicsForward);
141-
EXPORT_SYMBOL(nonrt_kinematicsInverse);
142-
EXPORT_SYMBOL(nonrt_refresh);
143-
EXPORT_SYMBOL(nonrt_is_identity);
137+
EXPORT_SYMBOL(nonrt_attach);

0 commit comments

Comments
 (0)