Skip to content

Commit db1d070

Browse files
committed
replace hal_priv.h/SHMOFF/self_offset with hal_struct_newf/attach API
Add hal_struct_newf() / hal_struct_attach() / hal_struct_detach() to hal.h and hal_lib.c as a clean public API for naming opaque blobs in HAL shmem without exposing hal_priv.h internals. Migrate all 17 kinematics modules and kinematics_user.c: - remove #include "hal_priv.h" from every kins module - replace hal_malloc + hal_param_s32_newf(...uspace-params-offset...) + SHMOFF(ptr) with hal_struct_newf(comp_id, sizeof(kinematics_params_t), NULL, "<module>.params") + hal_struct_attach() - remove self_offset field from kinematics_params_t - simplify nonrt_attach signature from (char *shmem_base, int offset, nonrt_ops_t *ops) to (nonrt_ops_t *ops); each module calls hal_struct_attach() internally to obtain its uspace_params pointer - simplify kinematics_user.c: drop hal_pin_reader dependency for the offset lookup, call hal_struct_attach() directly in find_shmem_params() - update kinematics.adoc custom module guide to reflect new API
1 parent f3fd553 commit db1d070

29 files changed

Lines changed: 421 additions & 593 deletions

docs/src/motion/kinematics.adoc

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -427,19 +427,18 @@ no per-call overhead.
427427
The userspace planner resolves a single symbol from the RT `.so`:
428428

429429
----
430-
void nonrt_attach(char *shmem_base, int offset, nonrt_ops_t *ops);
430+
void nonrt_attach(nonrt_ops_t *ops);
431431
----
432432

433-
`nonrt_attach` is called once at planner startup. It receives a
434-
pointer to the HAL shared memory base and the byte offset of the
435-
module's `kinematics_params_t` structure. It stores the pointer
436-
for later use and registers the forward and inverse functions that
437-
the planner will call:
433+
`nonrt_attach` is called once at planner startup. It calls
434+
`hal_struct_attach()` to map the module's `kinematics_params_t`
435+
from HAL shared memory, then registers the forward and inverse
436+
functions that the planner will call:
438437

439438
----
440-
void nonrt_attach(char *shmem_base, int offset, nonrt_ops_t *ops)
439+
void nonrt_attach(nonrt_ops_t *ops)
441440
{
442-
uspace_params = (kinematics_params_t *)(shmem_base + offset);
441+
hal_struct_attach("mykins.params", (void **)&uspace_params);
443442
ops->forward = myKinematicsForward; /* same function as RT */
444443
ops->inverse = myKinematicsInverse;
445444
}
@@ -483,24 +482,29 @@ any `!haldata` guard -- there are no HAL pins to dereference.
483482
==== Shared memory setup (required for planner 2)
484483

485484
`rtapi_app_main()` (or `XxxKinematicsSetup()` for switchkins modules)
486-
allocates the `kinematics_params_t` in HAL shmem and publishes its
487-
byte offset so the userspace planner can locate it:
485+
registers the `kinematics_params_t` in HAL shmem using
486+
`hal_struct_newf()`, then attaches to it with `hal_struct_attach()`:
488487

489488
----
490-
uspace_params = (kinematics_params_t *)hal_malloc(sizeof(kinematics_params_t));
491-
if (hal_param_s32_newf(HAL_RO, &uspace_params->self_offset, comp_id,
492-
"%s.uspace-params-offset", modname) < 0) goto error;
493-
memset(uspace_params, 0, sizeof(*uspace_params));
489+
if (hal_struct_newf(comp_id, sizeof(kinematics_params_t), NULL,
490+
"%s.params", modname) < 0) goto error;
491+
if (hal_struct_attach("mykins.params", (void **)&uspace_params) < 0)
492+
goto error;
494493
uspace_params->num_joints = num_joints;
495494
uspace_params->is_identity = 0; /* 1 for trivial/identity kinematics */
496495
/* fill initial parameter values */
497496
uspace_params->valid = 1;
498497
uspace_params->head = 1;
499498
uspace_params->tail = 1;
500-
uspace_params->self_offset = (int)SHMOFF(uspace_params);
501499
----
502500

503-
If the `uspace-params-offset` param is absent (old external module),
501+
`hal_struct_newf()` allocates the blob inside HAL's existing shared
502+
memory segment and registers a `HAL_RO hal_s32` parameter named
503+
`"<module>.params"` whose value is the shmem byte offset. The
504+
userspace planner calls `hal_struct_attach()` with the same name to
505+
obtain a direct pointer -- no `hal_priv.h`, no raw offsets.
506+
507+
If the `<module>.params` struct is absent (old external module),
504508
the userspace planner detects this at init time and disables planner 2
505509
with a logged warning. Planner 0 and 1 continue to work normally.
506510

@@ -561,13 +565,11 @@ the math, the RT interface (with shmem push), and the non-RT interface.
561565
562566
/* ---- RT headers ---- */
563567
#include "kinematics.h"
564-
#include "hal.h"
565-
#include "hal_priv.h" /* for SHMOFF() */
568+
#include <hal.h> /* hal_struct_newf/attach, hal_malloc */
566569
#include "kinematics_params.h" /* for kinematics_params_t */
567570
#include "rtapi.h"
568571
#include "rtapi_app.h"
569572
#include "rtapi_math.h"
570-
#include <string.h> /* for memset() */
571573
572574
/* ================================================================
573575
* Layer 1: Pure math (shared by RT and non-RT)
@@ -670,19 +672,17 @@ int rtapi_app_main(void)
670672
goto error;
671673
*(haldata->arm_length) = 100.0; /* default value */
672674
673-
/* Allocate kinematics_params_t in HAL shmem and publish its offset */
674-
uspace_params = (kinematics_params_t *)hal_malloc(sizeof(kinematics_params_t));
675-
if (!uspace_params) goto error;
676-
if (hal_param_s32_newf(HAL_RO, &uspace_params->self_offset, comp_id,
677-
"mykins.uspace-params-offset") < 0) goto error;
678-
memset(uspace_params, 0, sizeof(*uspace_params));
675+
/* Register kinematics parameters in HAL shmem via hal_struct_newf() */
676+
if (hal_struct_newf(comp_id, sizeof(kinematics_params_t), NULL,
677+
"mykins.params") < 0) goto error;
678+
if (hal_struct_attach("mykins.params", (void **)&uspace_params) < 0)
679+
goto error;
679680
uspace_params->num_joints = 3;
680681
uspace_params->is_identity = 0;
681682
uspace_params->params.mykins.arm_length = 100.0;
682683
uspace_params->valid = 1;
683684
uspace_params->head = 1;
684685
uspace_params->tail = 1;
685-
uspace_params->self_offset = (int)SHMOFF(uspace_params);
686686
687687
hal_ready(comp_id);
688688
return 0;
@@ -696,15 +696,16 @@ void rtapi_app_exit(void) { hal_exit(comp_id); }
696696
/* ================================================================
697697
* Layer 3: Non-RT entry point (for userspace trajectory planner)
698698
*
699-
* nonrt_attach is called once at planner startup. It maps the
700-
* shmem params pointer and registers the forward/inverse functions.
699+
* nonrt_attach is called once at planner startup. It attaches to
700+
* the kinematics_params_t registered by rtapi_app_main() via
701+
* hal_struct_newf() and registers the forward/inverse functions.
701702
* The same kinematicsForward/Inverse functions are used -- they
702703
* detect the userspace context via !haldata and read from shmem.
703704
* ================================================================ */
704705
705-
void nonrt_attach(char *shmem_base, int offset, nonrt_ops_t *ops)
706+
void nonrt_attach(nonrt_ops_t *ops)
706707
{
707-
uspace_params = (kinematics_params_t *)(shmem_base + offset);
708+
hal_struct_attach("mykins.params", (void **)&uspace_params);
708709
ops->forward = kinematicsForward;
709710
ops->inverse = kinematicsInverse;
710711
}
@@ -731,7 +732,8 @@ kinematics modules follow:
731732
is never called.
732733

733734
. **Layer 3 -- Non-RT entry point**: The `nonrt_attach()` function,
734-
called once at planner startup. It stores the shmem pointer and
735+
called once at planner startup. It calls `hal_struct_attach()` to
736+
map the module's `kinematics_params_t` from HAL shmem, then
735737
registers the forward/inverse function pointers. No separate
736738
non-RT math functions are needed -- the same RT functions handle
737739
both contexts via the `!haldata` guard.
@@ -791,8 +793,8 @@ RT `.so` to load and resolves `nonrt_attach` from it at startup.
791793
- Use `rtapi_snprintf()` instead of `snprintf()` in RT code.
792794
Standard C library functions are not available in the RT
793795
environment.
794-
- Always include `hal_priv.h` (for `SHMOFF()`) and `<string.h>`
795-
(for `memset()`) in modules that set up `uspace_params`.
796+
- Include `<hal.h>` (angle brackets — it is a public header) and
797+
`"kinematics_params.h"` in modules that set up `uspace_params`.
796798
- For modules with no runtime parameters (pure geometric transforms),
797799
register the RT functions directly in `nonrt_attach` with no
798800
`!haldata` guard -- there are no HAL pin pointers to dereference.
@@ -805,7 +807,7 @@ RT `.so` to load and resolves `nonrt_attach` from it at startup.
805807
- The shmem push uses `head`/`tail` guards to protect against torn
806808
reads: increment `head` before any write, set `tail = head` after
807809
all writes. The userspace side retries the read when `head != tail`.
808-
- Modules without the `uspace-params-offset` param fall back
810+
- Modules without the `<module>.params` HAL struct fall back
809811
gracefully: planner 0 and 1 work normally, planner 2 is disabled
810812
with a logged warning.
811813
- Look at the existing modules in `src/emc/kinematics/` for

src/emc/kinematics/5axiskins.c

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

6464
#include "switchkins.h"
65-
#include "hal_priv.h"
6665
#include "kinematics_params.h"
6766

6867
/* ========================================================================
@@ -164,7 +163,8 @@ struct haldata {
164163
} *haldata;
165164
static int fiveaxis_max_joints;
166165

167-
/* Userspace-accessible params in HAL shmem */
166+
/* Pointer into HAL shmem — set by hal_struct_newf/attach in setup,
167+
* re-attached by nonrt_attach() in the userspace dlopen path. */
168168
static kinematics_params_t *uspace_params;
169169

170170
// Joint mapping struct
@@ -309,17 +309,16 @@ int fiveaxis_KinematicsSetup(const int comp_id,
309309

310310
*haldata->pivot_length = DEFAULT_PIVOT_LENGTH;
311311

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);
312+
/* Register kinematics parameters in HAL shmem via hal_struct_newf() */
313+
result = hal_struct_newf(comp_id, sizeof(kinematics_params_t), NULL,
314+
"%s.params", kp->halprefix);
320315
if (result < 0) goto error;
316+
{
317+
char _n[HAL_NAME_LEN + 1];
318+
rtapi_snprintf(_n, sizeof(_n), "%s.params", kp->halprefix);
319+
if (hal_struct_attach(_n, (void **)&uspace_params) < 0) goto error;
320+
}
321321

322-
memset(uspace_params, 0, sizeof(*uspace_params));
323322
uspace_params->num_joints = fiveaxis_max_joints;
324323
uspace_params->axis_to_joint[0] = JX; uspace_params->axis_to_joint[1] = JY;
325324
uspace_params->axis_to_joint[2] = JZ; uspace_params->axis_to_joint[3] = JA;
@@ -329,7 +328,6 @@ int fiveaxis_KinematicsSetup(const int comp_id,
329328
uspace_params->params.fiveaxis.pivot_length = DEFAULT_PIVOT_LENGTH;
330329
uspace_params->valid = 1;
331330
uspace_params->is_identity = 0;
332-
uspace_params->self_offset = (int)SHMOFF(uspace_params);
333331

334332
rtapi_print("Kinematics Module %s\n",__FILE__);
335333
rtapi_print(" module name = %s\n"
@@ -385,16 +383,17 @@ int switchkinsSetup(kparms* kp,
385383
} // switchkinsSetup()
386384

387385
/* ========================================================================
388-
* Non-RT interface for userspace trajectory planner
386+
* Non-RT interface
389387
*
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.
388+
* nonrt_attach() is called by kinematics_user.c after dlopen().
389+
* It attaches to the kinematics_params_t registered in HAL shmem by the
390+
* RT side (hal_struct_newf in fiveaxis_KinematicsSetup) and returns
391+
* forward/inverse function pointers. No hal_priv.h or raw offsets needed.
393392
* ======================================================================== */
394393

395-
void nonrt_attach(char *shmem_base, int offset, nonrt_ops_t *ops)
394+
void nonrt_attach(nonrt_ops_t *ops)
396395
{
397-
uspace_params = (kinematics_params_t *)(shmem_base + offset);
396+
hal_struct_attach("5axiskins.params", (void **)&uspace_params);
398397
ops->forward = fiveaxis_KinematicsForward;
399398
ops->inverse = fiveaxis_KinematicsInverse;
400399
}

src/emc/kinematics/corexykins.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "rtapi_app.h"
1111
#include "rtapi_string.h"
1212
#include "kinematics.h"
13-
#include "hal_priv.h"
1413
#include "kinematics_params.h"
1514

1615
/*
@@ -103,19 +102,18 @@ int rtapi_app_main(void) {
103102

104103
data = hal_malloc(sizeof(struct data));
105104

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) {
105+
if (hal_struct_newf(comp_id, sizeof(kinematics_params_t), NULL,
106+
"corexykins.params") < 0) {
110107
hal_exit(comp_id); return -1;
111108
}
112-
memset(uspace_params, 0, sizeof(*uspace_params));
113-
uspace_params->num_joints = 9;
109+
if (hal_struct_attach("corexykins.params", (void **)&uspace_params) < 0) {
110+
hal_exit(comp_id); return -1;
111+
}
112+
uspace_params->num_joints = 9;
114113
uspace_params->valid = 1;
115114
uspace_params->is_identity = 0;
116115
uspace_params->head = 1;
117116
uspace_params->tail = 1;
118-
uspace_params->self_offset = (int)SHMOFF(uspace_params);
119117

120118
hal_ready(comp_id);
121119
return 0;
@@ -124,12 +122,14 @@ int rtapi_app_main(void) {
124122
void rtapi_app_exit(void) { hal_exit(comp_id); }
125123

126124
/* ========================================================================
127-
* Non-RT interface for userspace trajectory planner
125+
* Non-RT interface
126+
*
127+
* Called by kinematics_user.c after dlopen(). Returns forward/inverse
128+
* function pointers. (No kinematics-specific parameters to attach.)
128129
* ======================================================================== */
129130

130-
void nonrt_attach(char *shmem_base, int offset, nonrt_ops_t *ops)
131+
void nonrt_attach(nonrt_ops_t *ops)
131132
{
132-
(void)shmem_base; (void)offset;
133133
ops->forward = kinematicsForward;
134134
ops->inverse = kinematicsInverse;
135135
}

src/emc/kinematics/genhexkins.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@
116116
#include "motion.h"
117117
#include "kinematics.h" /* these decls, KINEMATICS_FORWARD_FLAGS */
118118
#include "switchkins.h"
119-
#include "hal_priv.h"
120119
#include "kinematics_params.h"
121120
#include <string.h>
122121

@@ -676,13 +675,15 @@ int genhexKinematicsSetup(const int comp_id,
676675

677676
if (res) goto error;
678677

679-
uspace_params = (kinematics_params_t *)hal_malloc(sizeof(kinematics_params_t));
680-
if (!uspace_params) goto error;
681-
if (hal_param_s32_newf(HAL_RO, &uspace_params->self_offset, comp_id,
682-
"%s.uspace-params-offset", kp->halprefix) < 0) goto error;
678+
if (hal_struct_newf(comp_id, sizeof(kinematics_params_t), NULL,
679+
"%s.params", kp->halprefix) < 0) goto error;
680+
{
681+
char _n[HAL_NAME_LEN + 1];
682+
rtapi_snprintf(_n, sizeof(_n), "%s.params", kp->halprefix);
683+
if (hal_struct_attach(_n, (void **)&uspace_params) < 0) goto error;
684+
}
683685
{
684686
int _i;
685-
memset(uspace_params, 0, sizeof(*uspace_params));
686687
uspace_params->num_joints = kp->max_joints;
687688
uspace_params->is_identity = 0;
688689
for (_i = 0; _i < GENHEX_NUM_STRUTS; _i++) {
@@ -708,7 +709,6 @@ int genhexKinematicsSetup(const int comp_id,
708709
uspace_params->valid = 1;
709710
uspace_params->head = 1;
710711
uspace_params->tail = 1;
711-
uspace_params->self_offset = (int)SHMOFF(uspace_params);
712712
}
713713
return 0;
714714

@@ -748,7 +748,11 @@ int switchkinsSetup(kparms* kp,
748748
} //switchkinsSetup()
749749

750750
/* ========================================================================
751-
* Non-RT interface for userspace trajectory planner
751+
* Non-RT interface
752+
*
753+
* Called by kinematics_user.c after dlopen(). Attaches to the
754+
* kinematics_params_t registered in HAL shmem by genhexKinematicsSetup()
755+
* via hal_struct_newf() and returns forward/inverse function pointers.
752756
* ======================================================================== */
753757

754758
static void nonrt_build_genhex(const kinematics_params_t *kp, genhex_params_t *p)
@@ -803,9 +807,9 @@ static int nonrt_genhex_inverse(const EmcPose *pos, double *joints,
803807
return genhex_inv(&p, pos, joints);
804808
}
805809

806-
void nonrt_attach(char *shmem_base, int offset, nonrt_ops_t *ops)
810+
void nonrt_attach(nonrt_ops_t *ops)
807811
{
808-
uspace_params = (kinematics_params_t *)(shmem_base + offset);
812+
hal_struct_attach("genhexkins.params", (void **)&uspace_params);
809813
ops->forward = nonrt_genhex_forward;
810814
ops->inverse = nonrt_genhex_inverse;
811815
}

src/emc/kinematics/genserfuncs.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "kinematics.h"
3838
#include "hal.h"
3939
#ifdef RTAPI
40-
#include "hal_priv.h"
4140
#include "kinematics_params.h"
4241
#include <string.h>
4342
#endif
@@ -688,13 +687,15 @@ int genserKinematicsSetup(const int comp_id,
688687
D(5) = DEFAULT_D6;
689688

690689
#ifdef RTAPI
691-
uspace_params = (kinematics_params_t *)hal_malloc(sizeof(kinematics_params_t));
692-
if (!uspace_params) goto error;
693-
if (hal_param_s32_newf(HAL_RO, &uspace_params->self_offset, comp_id,
694-
"%s.uspace-params-offset", kp->halprefix) < 0) goto error;
690+
if (hal_struct_newf(comp_id, sizeof(kinematics_params_t), NULL,
691+
"%s.params", kp->halprefix) < 0) goto error;
692+
{
693+
char _n[HAL_NAME_LEN + 1];
694+
rtapi_snprintf(_n, sizeof(_n), "%s.params", kp->halprefix);
695+
if (hal_struct_attach(_n, (void **)&uspace_params) < 0) goto error;
696+
}
695697
{
696698
int _i;
697-
memset(uspace_params, 0, sizeof(*uspace_params));
698699
uspace_params->num_joints = total_joints;
699700
uspace_params->is_identity = 0;
700701
for (_i = 0; _i < 6; _i++) {
@@ -708,7 +709,6 @@ int genserKinematicsSetup(const int comp_id,
708709
uspace_params->valid = 1;
709710
uspace_params->head = 1;
710711
uspace_params->tail = 1;
711-
uspace_params->self_offset = (int)SHMOFF(uspace_params);
712712
}
713713
#endif
714714

0 commit comments

Comments
 (0)