From b67d3305aec7a9bb9a7473d1d79b894662c02243 Mon Sep 17 00:00:00 2001 From: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:10:49 +1100 Subject: [PATCH 1/3] interp: reach HAL only through the canon The interpreter loaded the machine's kinematics module itself, on a HAL component of its own, and #<_hal[name]> read HAL directly, so librs274 linked liblinuxcnchal and libkinslimits and the interpreter owned a HAL component's lifetime. The canon now answers both. GET_EXTERNAL_KINEMATICS() loads a module for each caller on the canon's HAL component and hands it out with a table of the calls on it, KinematicsUserOps; the interpreter keeps both in its setup and frees the module through the table at exit, before the new EXIT_CANON() lets the component go. GET_EXTERNAL_HAL_VALUE() reads a pin, signal or param through a new hal_get_number(). Task and the preview module answer both; sai has no machine and no HAL. librs274 no longer links liblinuxcnchal or libkinslimits. --- include/hal.h | 4 + include/kinematics_user.h | 28 ++++ include/nml_intf/canon.hh | 17 +++ .../kinematics_userspace/kinematics_user.c | 17 +++ src/emc/rs274ngc/Submakefile | 6 +- src/emc/rs274ngc/gcodemodule.cc | 57 ++++++++ src/emc/rs274ngc/interp_internal.hh | 9 +- src/emc/rs274ngc/interp_namedparams.cc | 73 ++-------- src/emc/rs274ngc/interp_setup.cc | 5 +- src/emc/rs274ngc/interp_workplane.cc | 133 ++++++++---------- src/emc/rs274ngc/rs274ngc_pre.cc | 7 +- src/emc/sai/saicanon.cc | 16 +++ src/emc/task/emccanon.cc | 113 ++++++++++++--- src/emc/task/emctask.cc | 1 + src/hal/hal_lib_query.c | 45 ++++++ 15 files changed, 355 insertions(+), 176 deletions(-) diff --git a/include/hal.h b/include/hal.h index 04b0dad1ce1..090f34c5e14 100644 --- a/include/hal.h +++ b/include/hal.h @@ -1358,6 +1358,10 @@ int hal_getref_s(hal_query_t *query); int hal_get_s(hal_query_t *query, hal_query_cb cb, void *arg); int hal_set_s(hal_query_t *query, hal_query_cb cb, void *arg); +// hal_get_number: the pin, signal or param named, in that order, as a number; +// 0, or -ENOENT when nothing has the name +int hal_get_number(const char *name, double *value); + // // *** HAL structure iteration functions *** // diff --git a/include/kinematics_user.h b/include/kinematics_user.h index 6052f9e51ff..273e28f8abb 100644 --- a/include/kinematics_user.h +++ b/include/kinematics_user.h @@ -287,6 +287,34 @@ KinematicsUserContext* kinematicsUserInitString(const char* kinematics, */ void kinematicsUserFree(KinematicsUserContext* ctx); +/** + * The calls the interpreter makes, as a table: it is handed a context and + * this table by the canon (GET_EXTERNAL_KINEMATICS) and so does not link + * this library, nor HAL through it. + */ +typedef struct KinematicsUserOps { + int (*setType)(KinematicsUserContext* ctx, int ktype); + int (*setTool)(KinematicsUserContext* ctx, const EmcPose* tool); + int (*inverse)(KinematicsUserContext* ctx, const EmcPose* world, double* joints); + int (*forward)(KinematicsUserContext* ctx, const double* joints, EmcPose* world); + const kins_params* (*params)(KinematicsUserContext* ctx); + int (*isIdentity)(KinematicsUserContext* ctx); + int (*getNumJoints)(KinematicsUserContext* ctx); + int (*workFrame)(KinematicsUserContext* ctx, const double* joints, PmRotationMatrix* rot); + int (*toolFrame)(KinematicsUserContext* ctx, const double* joints, PmRotationMatrix* rot); + int (*toolFrameInverse)(KinematicsUserContext* ctx, const PmCartesian* axis_in_work, + const PmCartesian* x_in_work, const double* seed, + unsigned int held, double* solutions, int max_solutions, + int* free_directions, double* tool_spin); + int (*workJoints)(KinematicsUserContext* ctx, const double* seed, unsigned int* mask); + int (*poseJoint)(KinematicsUserContext* ctx, const double* seed, int* joint); + int (*toolFrameInWork)(const PmRotationMatrix* work, const PmRotationMatrix* tool, + PmRotationMatrix* in_work); /* kins_module.h */ + void (*free)(KinematicsUserContext* ctx); +} KinematicsUserOps; + +extern const KinematicsUserOps kinematicsUserOps; + /** * Get axis value from EmcPose by index * diff --git a/include/nml_intf/canon.hh b/include/nml_intf/canon.hh index 798c6f2f837..a89772c224f 100644 --- a/include/nml_intf/canon.hh +++ b/include/nml_intf/canon.hh @@ -241,6 +241,10 @@ typedef struct CanonConfig_t { /* reads world model data into the canonical interface */ extern void INIT_CANON(); +/* lets go of what the canonical interface holds outside the process, + the HAL component of the kinematics, before the process exits */ +extern void EXIT_CANON(); + /* Representation */ extern void SET_G5X_OFFSET(int origin, @@ -891,6 +895,19 @@ extern int GET_EXTERNAL_KINS_TYPE_FLAGS(int ktype); world, so that no tool offset is applied by the kinematics; true where no motion controller is attached (sai, a preview without a machine) */ extern bool GET_EXTERNAL_KINEMATICS_IDENTITY(); +/* The machine's kinematics module for the interpreter to evaluate: a new + KinematicsUserContext (kinematics_user.h) for each call, which the + caller owns and frees with (*ops)->free before EXIT_CANON(), the calls + on it in *ops; NULL with *why set where it cannot be evaluated here: + no machine attached (sai, a preview without one), no HAL, or a module + that runs only in realtime. */ +struct KinematicsUserOps; +extern void *GET_EXTERNAL_KINEMATICS(const struct KinematicsUserOps **ops, const char **why); + +/* The HAL pin, signal or param named, in that order, for #<_hal[name]>: + 0 with *value set, -ENOENT when nothing has the name, another negative + errno where HAL cannot be read (sai). */ +extern int GET_EXTERNAL_HAL_VALUE(const char *name, double *value); /* The tilted work plane the machine last executed, in the form SET_G68_FRAME takes: 1 with origin and rotation filled in, 0 when there diff --git a/src/emc/kinematics_userspace/kinematics_user.c b/src/emc/kinematics_userspace/kinematics_user.c index a734fbcc467..b0358fc6c08 100644 --- a/src/emc/kinematics_userspace/kinematics_user.c +++ b/src/emc/kinematics_userspace/kinematics_user.c @@ -801,3 +801,20 @@ void kinematicsUserFree(KinematicsUserContext* ctx) unload_module(ctx); free(ctx); } + +const KinematicsUserOps kinematicsUserOps = { + kinematicsUserSetType, + kinematicsUserSetTool, + kinematicsUserInverse, + kinematicsUserForward, + kinematicsUserParams, + kinematicsUserIsIdentity, + kinematicsUserGetNumJoints, + kinematicsUserWorkFrame, + kinematicsUserToolFrame, + kinematicsUserToolFrameInverse, + kinematicsUserWorkJoints, + kinematicsUserPoseJoint, + toolFrameInWork, + kinematicsUserFree, +}; diff --git a/src/emc/rs274ngc/Submakefile b/src/emc/rs274ngc/Submakefile index aba5dc9933d..8e601076224 100644 --- a/src/emc/rs274ngc/Submakefile +++ b/src/emc/rs274ngc/Submakefile @@ -41,8 +41,7 @@ $(call TOOBJSDEPS, emc/rs274ngc/pyinterp1.cc emc/rs274ngc/pyparamclass.cc emc/rs TARGETS += ../lib/librs274.so ../lib/librs274.so.0 ../lib/librs274.so.0: $(patsubst %.cc,objects/%.o,$(LIBRS274SRCS)) \ - ../lib/liblinuxcncini.so ../lib/libpyplugin.so ../lib/liblinuxcnchal.so.0 \ - ../lib/libkinslimits.so.0 ../lib/libposemath.so.0 + ../lib/liblinuxcncini.so ../lib/libpyplugin.so ../lib/libposemath.so.0 $(ECHO) Linking $(notdir $@) @mkdir -p ../lib @rm -f $@ @@ -53,7 +52,8 @@ GCODEMODULESRCS := emc/rs274ngc/gcodemodule.cc emc/rs274ngc/gcode_renderer.cc \ PYSRCS += $(GCODEMODULESRCS) GCODEMODULE := ../lib/python/gcode.so -$(GCODEMODULE): $(call TOOBJS, $(GCODEMODULESRCS)) ../lib/librs274.so.0 +$(GCODEMODULE): $(call TOOBJS, $(GCODEMODULESRCS)) ../lib/librs274.so.0 \ + ../lib/libkinslimits.so.0 ../lib/liblinuxcnchal.so.0 $(ECHO) Linking python module $(notdir $@) $(CXX) $(LDFLAGS) -shared -o $@ $^ -lstdc++ diff --git a/src/emc/rs274ngc/gcodemodule.cc b/src/emc/rs274ngc/gcodemodule.cc index bc92c5be644..b51ce6dca5f 100644 --- a/src/emc/rs274ngc/gcodemodule.cc +++ b/src/emc/rs274ngc/gcodemodule.cc @@ -61,6 +61,9 @@ #include "nml_intf/nurbs_math.hh" #include #include // KINEMATICS_IDENTITY +#include +#include +#include int _task = 0; // control preview behaviour when remapping @@ -820,6 +823,7 @@ int GET_EXTERNAL_JOINT_POSITIONS(double *joints, int max) { return n; } void INIT_CANON() {} +void EXIT_CANON() {} void SET_PARAMETER_FILE_NAME(std::string_view /*name*/) {} @@ -1026,6 +1030,59 @@ bool GET_EXTERNAL_KINEMATICS_IDENTITY() { return true; } } + +int GET_EXTERNAL_HAL_VALUE(const char *name, double *value) { + return hal_get_number(name, value); +} + +// a module of the caller's own, on a HAL component this process attaches +// the first time a program asks and keeps for its life +void *GET_EXTERNAL_KINEMATICS(const KinematicsUserOps **ops, const char **why) { + static int comp = -1; + static bool tried; + static const char *comp_why = "no INI file is given (INI_FILE_NAME)"; + static std::string module; + static int joints; + KinematicsUserContext *ctx = NULL; + const char *reason; + + if(!tried) { + tried = true; + const char *ini_name = getenv("INI_FILE_NAME"); + if(ini_name) { + linuxcnc::IniFile ini(ini_name); + auto kins = ini ? ini.findString("KINEMATICS", "KINS") : std::nullopt; + joints = ini ? ini.findIntV("JOINTS", "KINS", 0) : 0; + char name[HAL_NAME_LEN + 1]; + snprintf(name, sizeof(name), "gcode.%d", (int)getpid()); + if(!kins) { + comp_why = "the INI file names no [KINS] KINEMATICS"; + } else if(joints < 1) { + comp_why = "the INI file gives no [KINS] JOINTS"; + } else if((comp = hal_init(name)) < 0) { + comp_why = "HAL cannot be connected to (is realtime running?)"; + } else { + hal_ready(comp); + module = *kins; + } + } + } + reason = comp_why; + if(comp >= 0) { + reason = NULL; + ctx = kinematicsUserInitString(module.c_str(), joints, comp, "gcode"); + if(!ctx) { + reason = "the module cannot be loaded here"; + } else if(kinematicsUserIsRtOnly(ctx)) { + kinematicsUserFree(ctx); + ctx = NULL; + reason = "the module runs only in realtime"; + } + } + if(ops) *ops = &kinematicsUserOps; + if(why) *why = reason; + return ctx; +} void SET_NAIVECAM_TOLERANCE(double /*tolerance*/) { } #define RESULT_OK (result == INTERP_OK || result == INTERP_EXECUTE_FINISH) diff --git a/src/emc/rs274ngc/interp_internal.hh b/src/emc/rs274ngc/interp_internal.hh index 2ca9f34898b..57e14446c5a 100644 --- a/src/emc/rs274ngc/interp_internal.hh +++ b/src/emc/rs274ngc/interp_internal.hh @@ -810,11 +810,12 @@ struct setup // the pose G53.2 last solved, in program words, for #<_orient_a> and kin bool orient_valid; double orient_pose[6]; // x y z a b c - // the kinematics, for G68.3 and the orientation moves: loaded on first - // use through the non-realtime loader, on a HAL component of our own + // the kinematics, for G68.3 and the orientation moves: a module of this + // interpreter's own from the canon, GET_EXTERNAL_KINEMATICS(), on first use void *kins_ctx; // KinematicsUserContext - int kins_comp_id; - char kins_module[LINELEN]; // [KINS] KINEMATICS, as loadrt gets it + const struct KinematicsUserOps *kins_ops; + bool kins_tried; + const char *kins_why; // why there is none int kins_joints; // [KINS] JOINTS int kins_angular_joints; // bit per joint, [JOINT_n] TYPE = ANGULAR double kins_joint_min[EMCMOT_MAX_JOINTS]; // [JOINT_n] MIN_LIMIT diff --git a/src/emc/rs274ngc/interp_namedparams.cc b/src/emc/rs274ngc/interp_namedparams.cc index 9c7b3bcf49d..55ab2cc7130 100644 --- a/src/emc/rs274ngc/interp_namedparams.cc +++ b/src/emc/rs274ngc/interp_namedparams.cc @@ -45,8 +45,8 @@ namespace bp = boost::python; #include "rs274ngc_interp.hh" #include -// for HAL pin variables -#include +// ENOENT, for a HAL name not found +#include using namespace linuxcnc; @@ -260,24 +260,6 @@ int Interp::fetch_hal_param( const char *nameBuf, int *status, double *value) return INTERP_OK; } - // Make sure we can query pins/params/signals - // Calling hal_lib_init() here would make it impossible for us to determine - // when to call hal_lib_exit() to satisfy the init/exit reference counting. - // Neither do we want to create a temporary component. The overhead is - // huge. An alternative would be wrapping this code in hal_lib_init() and - // hal_lib_exit(), but that also creates a large overhead. - // - // The old code just created a component (using a persistent static comp_id - // check in this function), but that never gets cleaned up, which is also - // bad. If the change should result in a program to fail the below test, - // then we should fix the program and not hack this library. - // Now it is the responsibility of the program using the rs274ngc library - // to attach to HAL shared memory before running programs that use this. - retval = hal_is_init(); - CHKS(retval == 0, "fetch_hal_param: HAL is not initialized (hal_is_init() returned zero)"); - - hal_type_t type = HAL_TYPE_UNINITIALIZED; - hal_query_value_u qval; std::string hal_name = nameBuf + 5; // skip the '_hal[' part size_t i = hal_name.find(']'); if(std::string::npos == i) { @@ -291,51 +273,14 @@ int Interp::fetch_hal_param( const char *nameBuf, int *status, double *value) } hal_name.erase(i); // Remove everything from ']'and after - // Retrieve the active value from the pin/signal/param (in that order) - hal_query_t q = {}; - q.name = hal_name.c_str(); - q.qtype = HAL_QTYPE_PIN; - if(0 == hal_get_p(&q, NULL, NULL)) { - // FIXME: It is not always logical to complain on an pin that is - // not attached to a signal. HAL_OUT pins should always give a real - // result. HAL_IO pins could be fine, but that depends on whether - // there has been written proper data on it yet. - if(!q.pp.signal) - logOword("%s: no signal connected", hal_name.c_str()); - qval = q.pp.value; - type = q.pp.type; - goto assign; - } - q = {}; - q.name = hal_name.c_str(); - if(0 == hal_get_s(&q, NULL, NULL)) { - if(q.sig.writers <= 0) - logOword("%s: signal has no writer", hal_name.c_str()); - qval = q.sig.value; - type = q.sig.type; - goto assign; - } - q = {}; - q.name = hal_name.c_str(); - q.qtype = HAL_QTYPE_PARAM; - if(0 == hal_get_p(&q, NULL, NULL)) { - qval = q.pp.value; - type = q.pp.type; - goto assign; - } - *status = 0; - ERS(_("Named hal parameter #<%s> not found"), nameBuf); - - return INTERP_OK; - - assign: - switch (type) { - case HAL_BOOL: *value = (double)qval.b; break; - case HAL_UINT: *value = (double)qval.u; break; - case HAL_SINT: *value = (double)qval.s; break; - case HAL_REAL: *value = (double)qval.r; break; - default: return -1; + // the pin, signal or param (in that order), from the canon: the + // program that runs the interpreter is the one attached to HAL + retval = GET_EXTERNAL_HAL_VALUE(hal_name.c_str(), value); + if(retval == -ENOENT) { + *status = 0; + ERS(_("Named hal parameter #<%s> not found"), nameBuf); } + CHKS(retval != 0, _("#<%s>: HAL cannot be read here"), nameBuf); logOword("%s: value=%f", hal_name.c_str(), *value); *status = 1; return INTERP_OK; diff --git a/src/emc/rs274ngc/interp_setup.cc b/src/emc/rs274ngc/interp_setup.cc index 223dfd50756..90ec43913bc 100644 --- a/src/emc/rs274ngc/interp_setup.cc +++ b/src/emc/rs274ngc/interp_setup.cc @@ -117,8 +117,9 @@ setup::setup() : orient_valid(false), orient_pose{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, kins_ctx(nullptr), - kins_comp_id(-1), - kins_module{}, + kins_ops(nullptr), + kins_tried(false), + kins_why(nullptr), kins_joints(0), kins_angular_joints(0), kins_joint_min{}, diff --git a/src/emc/rs274ngc/interp_workplane.cc b/src/emc/rs274ngc/interp_workplane.cc index 21cb5a57585..efb6657189e 100644 --- a/src/emc/rs274ngc/interp_workplane.cc +++ b/src/emc/rs274ngc/interp_workplane.cc @@ -574,42 +574,25 @@ int Interp::convert_work_plane(int g_code, block_pointer block, setup_pointer s) // its process, the first time it is asked. //---------------------------------------------------------------------- -#include #include #include "units.h" -#include #include #define KINS_CTX(s) ((KinematicsUserContext *)(s)->kins_ctx) -// the module loaded, once +// this interpreter's own module from the canon, once int Interp::kins_load(setup_pointer s) { - KinematicsUserContext *ctx; - - if (!s->kins_ctx) { - char name[HAL_NAME_LEN + 1]; - int comp; - - CHKS((!s->kins_module[0]), - _("the INI file names no [KINS] KINEMATICS, so the kinematics cannot be evaluated here")); - CHKS((s->kins_joints < 1), - _("the INI file gives no [KINS] JOINTS, so the kinematics cannot be evaluated here")); - snprintf(name, sizeof(name), "interp.%d", (int)getpid()); - comp = hal_init(name); - CHKS((comp < 0), - _("cannot connect to HAL to evaluate the kinematics (is realtime running?)")); - s->kins_comp_id = comp; - ctx = kinematicsUserInitString(s->kins_module, s->kins_joints, comp, name); - hal_ready(comp); - CHKS((!ctx), _("kinematics module %s cannot be loaded here"), s->kins_module); - s->kins_ctx = ctx; + if (!s->kins_tried) { + s->kins_tried = true; + s->kins_why = NULL; + s->kins_ctx = GET_EXTERNAL_KINEMATICS(&s->kins_ops, &s->kins_why); for (int i = 0; i < EMCMOT_MAX_JOINTS; i++) { s->kins_seed[i] = 0.0; } } return INTERP_OK; } -// the loaded module, on the kinematics type the program is in +// the module, on the kinematics type the program is in int Interp::kins_context(setup_pointer s, void **out) { KinematicsUserContext *ctx; @@ -617,9 +600,9 @@ int Interp::kins_context(setup_pointer s, void **out) *out = NULL; CHP(kins_load(s)); ctx = KINS_CTX(s); - CHKS((kinematicsUserIsRtOnly(ctx)), - _("kinematics module %s cannot be evaluated outside realtime"), s->kins_module); - CHKS((kinematicsUserSetType(ctx, s->kins_type) != 0), + CHKS((!ctx), _("the kinematics cannot be evaluated here: %s"), + s->kins_why ? s->kins_why : _("no machine is attached")); + CHKS((s->kins_ops->setType(ctx, s->kins_type) != 0), _("kinematics type %d is not available outside realtime"), s->kins_type); // with the tool offset the program is under here, not the one motion // has reached: the interpreter runs ahead of motion @@ -643,19 +626,7 @@ void Interp::kins_set_tool(void *vctx, const EmcPose *offset) tool.u = PROGRAM_TO_USER_LEN(offset->u); tool.v = PROGRAM_TO_USER_LEN(offset->v); tool.w = PROGRAM_TO_USER_LEN(offset->w); - kinematicsUserSetTool((KinematicsUserContext *)vctx, &tool); -} - -void Interp::kins_release(setup_pointer s) -{ - if (s->kins_ctx) { - kinematicsUserFree(KINS_CTX(s)); - s->kins_ctx = NULL; - } - if (s->kins_comp_id >= 0) { - hal_exit(s->kins_comp_id); - s->kins_comp_id = -1; - } + _setup.kins_ops->setTool((KinematicsUserContext *)vctx, &tool); } // the current point as the machine sees it: the absolute frame, in the @@ -716,14 +687,14 @@ int Interp::current_joints(setup_pointer s, void *vctx, double *joints) current_machine_pose(s, &pose); for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { joints[i] = s->kins_seed[i]; } seeded = pose; - if (kinematicsUserForward(ctx, joints, &seeded) == 0 && same_pose(&pose, &seeded)) { + if (s->kins_ops->forward(ctx, joints, &seeded) == 0 && same_pose(&pose, &seeded)) { return INTERP_OK; } n = GET_EXTERNAL_JOINT_POSITIONS(standing, EMCMOT_MAX_JOINTS); for (i = 0; i < n; i++) { joints[i] = standing[i]; } if (n > 0) { seeded = pose; - if (kinematicsUserForward(ctx, joints, &seeded) == 0 && same_pose(&pose, &seeded)) { + if (s->kins_ops->forward(ctx, joints, &seeded) == 0 && same_pose(&pose, &seeded)) { for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { s->kins_seed[i] = joints[i]; } return INTERP_OK; } @@ -731,7 +702,7 @@ int Interp::current_joints(setup_pointer s, void *vctx, double *joints) for (pass = 0; pass < 8; pass++) { double prev[EMCMOT_MAX_JOINTS], worst = 0.0; for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { prev[i] = joints[i]; } - CHKS((kinematicsUserInverse(ctx, &pose, joints) != 0), + CHKS((s->kins_ops->inverse(ctx, &pose, joints) != 0), _("the kinematics cannot invert the current position")); for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { worst = fmax(worst, fabs(joints[i] - prev[i])); } if (worst < 1e-9) { break; } @@ -740,6 +711,14 @@ int Interp::current_joints(setup_pointer s, void *vctx, double *joints) return INTERP_OK; } +// give the module back; the next use asks the canon again +void Interp::kins_release(setup_pointer s) +{ + if (s->kins_ctx && s->kins_ops) { s->kins_ops->free(KINS_CTX(s)); } + s->kins_ctx = NULL; + s->kins_tried = false; +} + // The kinematics as far as it can be evaluated here: the context on the // type the program is in, or NULL where there is nothing to evaluate, // with no machine attached (sai, a preview without one) or a module that @@ -749,7 +728,7 @@ int Interp::kins_here(setup_pointer s, void **out) *out = NULL; if (GET_EXTERNAL_KINEMATICS_IDENTITY()) { return INTERP_OK; } CHP(kins_load(s)); - if (kinematicsUserIsRtOnly(KINS_CTX(s))) { return INTERP_OK; } + if (!s->kins_ctx) { return INTERP_OK; } return kins_context(s, out); } @@ -771,14 +750,14 @@ int Interp::tool_offset_point(setup_pointer s, const EmcPose *offset, const doub if (flags >= 0 && (flags & KINSTYPE_IDENTITY)) { return INTERP_OK; } CHP(kins_here(s, &vctx)); ctx = (KinematicsUserContext *)vctx; - if (!ctx || kinematicsUserIsIdentity(ctx)) { return INTERP_OK; } + if (!ctx || s->kins_ops->isIdentity(ctx)) { return INTERP_OK; } if (standing) { for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { joints[i] = standing[i]; } } else { CHP(current_joints(s, ctx, joints)); } kins_set_tool(ctx, offset); - CHKS((kinematicsUserForward(ctx, joints, point) != 0), + CHKS((s->kins_ops->forward(ctx, joints, point) != 0), _("the kinematics cannot place the tool from the joints after the tool offset change")); // the machine stays on these joints for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { s->kins_seed[i] = joints[i]; } @@ -842,13 +821,13 @@ int Interp::convert_work_plane_from_tool(block_pointer block, setup_pointer s) _("Cancel the G52 in the tilted work plane (G52 X0 Y0 Z0) before G68.3")); CHP(kins_context(s, &vctx)); ctx = (KinematicsUserContext *)vctx; - CHKS((kinematicsUserIsIdentity(ctx)), + CHKS((s->kins_ops->isIdentity(ctx)), _("G68.3 needs a kinematics type that describes the machine; select it with G12.1 first")); CHP(current_joints(s, ctx, joints)); - CHKS((kinematicsUserWorkFrame(ctx, joints, &work) != 0 - || kinematicsUserToolFrame(ctx, joints, &tool) != 0), + CHKS((s->kins_ops->workFrame(ctx, joints, &work) != 0 + || s->kins_ops->toolFrame(ctx, joints, &tool) != 0), _("the kinematics reports no tool frame, so G68.3 cannot read the tool direction")); - toolFrameInWork(&work, &tool, &in_work); + s->kins_ops->toolFrameInWork(&work, &tool, &in_work); zt = in_work.z; xt = in_work.x; yt = in_work.y; @@ -925,7 +904,7 @@ int Interp::convert_orient_tool(int code, block_pointer block, setup_pointer s) CHP(kins_context(s, &vctx)); ctx = (KinematicsUserContext *)vctx; - CHKS((kinematicsUserIsIdentity(ctx)), + CHKS((s->kins_ops->isIdentity(ctx)), _("%s needs a kinematics type that describes the machine; select it with G12.1 first"), name); CHP(current_joints(s, ctx, now)); @@ -936,7 +915,7 @@ int Interp::convert_orient_tool(int code, block_pointer block, setup_pointer s) // where that puts the machine, and what the program calls it end_pose = (EmcPose){}; current_machine_pose(s, &end_pose); - CHKS((kinematicsUserForward(ctx, sol, &end_pose) != 0), + CHKS((s->kins_ops->forward(ctx, sol, &end_pose) != 0), _("%s: the kinematics cannot place the orientation it found"), name); for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { s->kins_seed[i] = sol[i]; } machine_pose_to_program(s, &end_pose, end_prog); @@ -1023,7 +1002,7 @@ static int orient_fit(setup_pointer s, KinematicsUserContext *ctx, const EmcPose bool inside = true, placed; for (j = 0; j < njoints; j++) { q[j] = pose[j]; } - placed = (kinematicsUserForward(ctx, q, &base) == 0); + placed = (s->kins_ops->forward(ctx, q, &base) == 0); for (j = 0; j < njoints; j++) { if (!(s->kins_angular_joints & (1 << j))) { continue; } double turns = floor((now[j] - pose[j]) / 360.0 + 0.5); @@ -1036,7 +1015,7 @@ static int orient_fit(setup_pointer s, KinematicsUserContext *ctx, const EmcPose // along, if any, and the turns its own travel leaves turned = base; q[j] += 360.0; - if (placed && kinematicsUserForward(ctx, q, &turned) == 0) { + if (placed && s->kins_ops->forward(ctx, q, &turned) == 0) { for (l = 3; l < 9; l++) { double step = pose_letter(&turned, l) - pose_letter(&base, l); if (fabs(fabs(step) - 360.0) > 1e-6) { continue; } @@ -1062,7 +1041,7 @@ static int orient_fit(setup_pointer s, KinematicsUserContext *ctx, const EmcPose pose[j] += 360.0 * turns; q[j] = pose[j]; } - if (inside && placed && kinematicsUserForward(ctx, q, &base) == 0) { + if (inside && placed && s->kins_ops->forward(ctx, q, &base) == 0) { for (l = 3; l < 9; l++) { double at = pose_letter(&base, l); if (at < s->axis_min[l] - 1e-9 || at > s->axis_max[l] + 1e-9) { inside = false; } @@ -1094,13 +1073,13 @@ int Interp::orient_solve(setup_pointer s, void *vctx, const PmCartesian *axis, c int n, i, j, chosen, njoints; bool reached; - njoints = kinematicsUserGetNumJoints(ctx); + njoints = s->kins_ops->getNumJoints(ctx); EmcPose seed = {}; current_machine_pose(s, &seed); if (q == 0) { - if (kinematicsUserWorkJoints(ctx, now, &held) != 0) { held = 0; } + if (s->kins_ops->workJoints(ctx, now, &held) != 0) { held = 0; } } - n = kinematicsUserToolFrameInverse(ctx, axis, xdir, now, held, + n = s->kins_ops->toolFrameInverse(ctx, axis, xdir, now, held, solutions, TOOL_FRAME_MAX_SOLUTIONS, free_dirs, spin); reached = (n > 0); if (n > 0) { n = orient_fit(s, ctx, &seed, solutions, n, njoints, now); } @@ -1109,7 +1088,7 @@ int Interp::orient_solve(setup_pointer s, void *vctx, const PmCartesian *axis, c // leaving the plane's X to the coordinate system, or a table that // can place it pins the pose and P has nothing to choose from held = 0; - n = kinematicsUserToolFrameInverse(ctx, axis, NULL, now, held, + n = s->kins_ops->toolFrameInverse(ctx, axis, NULL, now, held, solutions, TOOL_FRAME_MAX_SOLUTIONS, free_dirs, spin); reached = reached || (n > 0); if (n > 0) { n = orient_fit(s, ctx, &seed, solutions, n, njoints, now); } @@ -1136,7 +1115,7 @@ int Interp::orient_solve(setup_pointer s, void *vctx, const PmCartesian *axis, c // P names the pose rather than its rank, so that the same program // reaches the same pose from wherever the machine is standing int sign_joint; - CHKS((kinematicsUserPoseJoint(ctx, now, &sign_joint) != 0), + CHKS((s->kins_ops->poseJoint(ctx, now, &sign_joint) != 0), _("%s P%d: the poses of this machine cannot be told apart by a tilting" " joint, so leave P out and take the nearest"), name, p); chosen = -1; @@ -1178,7 +1157,7 @@ int Interp::tool_vector_ends(block_pointer block, setup_pointer s, double *a, do CHKS((vec_norm(v) < 1e-9), _("G43.5: the tool vector I J K is zero")); CHP(kins_context(s, &vctx)); ctx = (KinematicsUserContext *)vctx; - CHKS((kinematicsUserIsIdentity(ctx)), + CHKS((s->kins_ops->isIdentity(ctx)), _("G43.5: a tool vector needs a kinematics type that describes the machine; select it with G12.1 first")); CHP(current_joints(s, ctx, now)); if (block->g_modes[GM_MODAL_0] == G_53) { @@ -1193,7 +1172,7 @@ int Interp::tool_vector_ends(block_pointer block, setup_pointer s, double *a, do // where that puts the rotaries, and what the program calls it pose = (EmcPose){}; current_machine_pose(s, &pose); - CHKS((kinematicsUserForward(ctx, sol, &pose) != 0), + CHKS((s->kins_ops->forward(ctx, sol, &pose) != 0), _("G43.5: the kinematics cannot place the orientation it found")); for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { s->kins_seed[i] = sol[i]; } machine_pose_to_program(s, &pose, prog); @@ -1274,11 +1253,11 @@ int Interp::slide_joints(const char *name, setup_pointer s, void *vctx, const st if (machine < 0) { return slide_words(name, s, ctx, p, njoints, 1, flags, words, joints); } - CHKS((kinematicsUserSetType(ctx, machine) != 0), + CHKS((s->kins_ops->setType(ctx, machine) != 0), _("%s: kinematics type %d is not available outside realtime"), name, machine); status = slide_words(name, s, ctx, p, njoints, 0, flags, words, joints); // back on the type in force whatever happened: the caller's context - kinematicsUserSetType(ctx, s->kins_type); + s->kins_ops->setType(ctx, s->kins_type); return status; } @@ -1318,7 +1297,7 @@ int Interp::slide_words(const char *name, setup_pointer s, void *vctx, const str static const char letters[9] = { 'X', 'Y', 'Z', 'A', 'B', 'C', 'U', 'V', 'W' }; int a, j; - if (undeclared || kinematicsUserIsIdentity(ctx)) { + if (undeclared || s->kins_ops->isIdentity(ctx)) { CHP(letters_name_joints(name, s, p, njoints)); } if (!undeclared) { @@ -1352,7 +1331,7 @@ int Interp::machine_frame_joints(const char *name, setup_pointer s, void *vctx, int a, i, pass; current_machine_pose(s, &pose); - CHKS((kinematicsUserForward(ctx, joints, &pose) != 0), + CHKS((s->kins_ops->forward(ctx, joints, &pose) != 0), _("%s: the machine frame kinematics cannot place the current joints"), name); coord[0] = &pose.tran.x; coord[1] = &pose.tran.y; coord[2] = &pose.tran.z; coord[3] = &pose.a; coord[4] = &pose.b; coord[5] = &pose.c; @@ -1365,7 +1344,7 @@ int Interp::machine_frame_joints(const char *name, setup_pointer s, void *vctx, for (pass = 0; pass < 8; pass++) { double prev[EMCMOT_MAX_JOINTS], worst = 0.0; for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { prev[i] = joints[i]; } - CHKS((kinematicsUserInverse(ctx, &pose, joints) != 0), + CHKS((s->kins_ops->inverse(ctx, &pose, joints) != 0), _("%s: the machine frame kinematics cannot reach that point"), name); for (i = 0; i < EMCMOT_MAX_JOINTS; i++) { worst = fmax(worst, fabs(joints[i] - prev[i])); } if (worst < 1e-9) { break; } @@ -1384,7 +1363,7 @@ int Interp::ptp_joint_move(const char *name, int move, block_pointer block, setu int j; current_machine_pose(s, &pose); - CHKS((kinematicsUserForward(ctx, joints, &pose) != 0), + CHKS((s->kins_ops->forward(ctx, joints, &pose) != 0), _("%s: the kinematics cannot place those joints"), name); for (j = 0; j < EMCMOT_MAX_JOINTS; j++) { s->kins_seed[j] = joints[j]; } machine_pose_to_program(s, &pose, prog); @@ -1438,8 +1417,8 @@ int Interp::convert_ptp_joints(int code, int move, block_pointer block, setup_po _("Cannot use %s with cutter radius compensation on"), name); CHP(kins_context(s, &vctx)); ctx = (KinematicsUserContext *)vctx; - njoints = kinematicsUserGetNumJoints(ctx); - p = kinematicsUserParams(ctx); + njoints = s->kins_ops->getNumJoints(ctx); + p = s->kins_ops->params(ctx); CHP(current_joints(s, ctx, joints)); if (code == G_53_7) { @@ -1500,8 +1479,8 @@ int Interp::convert_home_slides(int code, block_pointer block, setup_pointer s) _("Cannot use %s with cutter radius compensation on"), name); CHP(kins_context(s, &vctx)); ctx = (KinematicsUserContext *)vctx; - njoints = kinematicsUserGetNumJoints(ctx); - p = kinematicsUserParams(ctx); + njoints = s->kins_ops->getNumJoints(ctx); + p = s->kins_ops->params(ctx); CHP(current_joints(s, ctx, joints)); for (a = 0; a < 9; a++) { @@ -1538,20 +1517,20 @@ int Interp::convert_savehome_slides(int code, setup_pointer s) CHP(kins_context(s, &vctx)); ctx = (KinematicsUserContext *)vctx; - njoints = kinematicsUserGetNumJoints(ctx); - p = kinematicsUserParams(ctx); + njoints = s->kins_ops->getNumJoints(ctx); + p = s->kins_ops->params(ctx); CHP(current_joints(s, ctx, joints)); if (machine >= 0) { EmcPose pose; int identity, placed; - CHKS((kinematicsUserSetType(ctx, machine) != 0), + CHKS((s->kins_ops->setType(ctx, machine) != 0), _("%s: kinematics type %d is not available outside realtime"), name, machine); - identity = kinematicsUserIsIdentity(ctx); + identity = s->kins_ops->isIdentity(ctx); current_machine_pose(s, &pose); - placed = (kinematicsUserForward(ctx, joints, &pose) == 0); + placed = (s->kins_ops->forward(ctx, joints, &pose) == 0); // back on the type in force: the caller's context - kinematicsUserSetType(ctx, s->kins_type); + s->kins_ops->setType(ctx, s->kins_type); if (identity) { CHP(letters_name_joints(name, s, p, njoints)); } CHKS((!placed), _("%s: the machine frame kinematics cannot place the current joints"), name); s->parameters[base + 0] = pose.tran.x; diff --git a/src/emc/rs274ngc/rs274ngc_pre.cc b/src/emc/rs274ngc/rs274ngc_pre.cc index 3d16e990e14..ede92140bfc 100644 --- a/src/emc/rs274ngc/rs274ngc_pre.cc +++ b/src/emc/rs274ngc/rs274ngc_pre.cc @@ -812,9 +812,7 @@ int Interp::exit() } } - // task exits without destroying the interpreter: let go of the HAL - // component the kinematics was evaluated through, or it outlives task - // and the shutdown waits for it; the next use loads it again + // before the canon lets go of what the module is loaded on (EXIT_CANON) kins_release(&_setup); return INTERP_OK; } @@ -958,9 +956,6 @@ int Interp::init() _setup.num_spindles = inifile.findIntV("SPINDLES", "TRAJ", 1); // the kinematics, for the codes that ask it something - if (auto kins = inifile.findString("KINEMATICS", "KINS")) { - snprintf(_setup.kins_module, sizeof(_setup.kins_module), "%s", kins->c_str()); - } _setup.kins_joints = inifile.findIntV("JOINTS", "KINS", 0); // which joints turn rather than slide, so that an axis letter is // refused where it would name a joint of the other kind, and the diff --git a/src/emc/sai/saicanon.cc b/src/emc/sai/saicanon.cc index 5935680c5e4..052c6e4e5ca 100644 --- a/src/emc/sai/saicanon.cc +++ b/src/emc/sai/saicanon.cc @@ -671,6 +671,10 @@ void FLOOD_ON() _sai._flood = 1; } +void EXIT_CANON() +{ +} + void INIT_CANON() { } @@ -841,6 +845,18 @@ extern bool GET_EXTERNAL_KINEMATICS_IDENTITY() return true; } +extern int GET_EXTERNAL_HAL_VALUE(const char * /*name*/, double * /*value*/) +{ + return -EFAULT; +} + +extern void *GET_EXTERNAL_KINEMATICS(const struct KinematicsUserOps **ops, const char **why) +{ + if (ops) { *ops = NULL; } + if (why) { *why = "the standalone interpreter has no machine"; } + return NULL; +} + void SET_PARAMETER_FILE_NAME(std::string_view name) { _parameter_file_name = name; diff --git a/src/emc/task/emccanon.cc b/src/emc/task/emccanon.cc index 1e53c34d3b7..30e2aa7d5fa 100644 --- a/src/emc/task/emccanon.cc +++ b/src/emc/task/emccanon.cc @@ -870,9 +870,79 @@ static double getStraightJerk(double x, double y, double z, // and a kinematics type that is the identity, cap nothing. //---------------------------------------------------------------------- +// The HAL component the module is loaded on, canon's and the +// interpreter's alike: the module reads the geometry from the pins of +// the one motion runs, so HAL has to be mapped. static struct { - KinematicsUserContext *ctx; + bool tried; // the component was asked for, once int comp_id; + char name[HAL_NAME_LEN + 1]; + char module[LINELEN]; // [KINS] KINEMATICS + int joints; // [KINS] JOINTS + const char *why; // why there is no component +} khal = { false, -1, {}, {}, 0, NULL }; + +static bool kins_hal(void) +{ + if (khal.tried) { return khal.comp_id >= 0; } + khal.tried = true; + linuxcnc::IniFile ini(emc_inifile); + if (!ini) { + khal.why = "the INI file cannot be read"; + return false; + } + auto module = ini.findString("KINEMATICS", "KINS"); + khal.joints = ini.findIntV("JOINTS", "KINS", 0); + if (!module) { + khal.why = "the INI file names no [KINS] KINEMATICS"; + return false; + } + if (khal.joints < 1) { + khal.why = "the INI file gives no [KINS] JOINTS"; + return false; + } + snprintf(khal.module, sizeof(khal.module), "%s", module->c_str()); + snprintf(khal.name, sizeof(khal.name), "canon.%d", (int)getpid()); + khal.comp_id = hal_init(khal.name); + if (khal.comp_id < 0) { + khal.why = "HAL cannot be connected to (is realtime running?)"; + return false; + } + hal_ready(khal.comp_id); + return true; +} + +// a module of the caller's own, apart from canon's: each interpreter sets +// its own kinematics type and tool offset, running ahead of canon +void *GET_EXTERNAL_KINEMATICS(const KinematicsUserOps **ops, const char **why) +{ + KinematicsUserContext *ctx = NULL; + const char *reason = NULL; + + if (!kins_hal()) { + reason = khal.why; + } else { + ctx = kinematicsUserInitString(khal.module, khal.joints, khal.comp_id, khal.name); + if (!ctx) { + reason = "the module cannot be loaded here"; + } else if (kinematicsUserIsRtOnly(ctx)) { + kinematicsUserFree(ctx); + ctx = NULL; + reason = "the module runs only in realtime"; + } + } + if (ops) { *ops = &kinematicsUserOps; } + if (why) { *why = reason; } + return ctx; +} + +int GET_EXTERNAL_HAL_VALUE(const char *name, double *value) +{ + return hal_get_number(name, value); +} + +static struct { + KinematicsUserContext *ctx; bool tried; // loading was attempted, once int type; // the kinematics type the program is in, -1 for the machine's double joints[EMCMOT_MAX_JOINTS]; // the joints at canon.endPoint, as far as canon knows @@ -887,35 +957,22 @@ static struct { bool capped; double vel, acc; } memo; -} kins = { NULL, -1, false, -1, {}, false, false, {}, { false, {}, {}, false, 0.0, 0.0 } }; +} kins = { NULL, false, -1, {}, false, false, {}, { false, {}, {}, false, 0.0, 0.0 } }; // the machine's own module, once static bool kins_load(void) { - char name[HAL_NAME_LEN + 1]; - int joints, j; + int j; if (kins.tried) { return kins.ctx != NULL; } kins.tried = true; - linuxcnc::IniFile ini(emc_inifile); - if (!ini) { return false; } - auto module = ini.findString("KINEMATICS", "KINS"); - joints = ini.findIntV("JOINTS", "KINS", 0); - if (!module || joints < 1) { return false; } - snprintf(name, sizeof(name), "canon.%d", (int)getpid()); - kins.comp_id = hal_init(name); - if (kins.comp_id < 0) { return false; } - kins.ctx = kinematicsUserInitString(module->c_str(), joints, kins.comp_id, name); - hal_ready(kins.comp_id); + if (!kins_hal()) { return false; } + kins.ctx = kinematicsUserInitString(khal.module, khal.joints, khal.comp_id, khal.name); if (kins.ctx && kinematicsUserIsRtOnly(kins.ctx)) { kinematicsUserFree(kins.ctx); kins.ctx = NULL; } - if (!kins.ctx) { - hal_exit(kins.comp_id); - kins.comp_id = -1; - return false; - } + if (!kins.ctx) { return false; } // the joint limits as the INI gives them; one left out binds nothing kins.limits.joints = kinematicsUserGetNumJoints(kins.ctx); for (j = 0; j < KINEMATICS_USER_MAX_JOINTS; j++) { @@ -927,7 +984,7 @@ static bool kins_load(void) for (j = 0; j < EMCMOT_MAX_JOINTS; j++) { kins.joints[j] = 0.0; } kins.debug = getenv("EMCCANON_KINS_DEBUG") != NULL; if (kins.debug) { - fprintf(stderr, "canon kins: %s, %d joints, limits", module->c_str(), kins.limits.joints); + fprintf(stderr, "canon kins: %s, %d joints, limits", khal.module, kins.limits.joints); for (j = 0; j < kins.limits.joints; j++) { fprintf(stderr, " %g/%g", kins.limits.vel[j], kins.limits.acc[j]); } @@ -3843,6 +3900,22 @@ EmcPose GET_EXTERNAL_TOOL_LENGTH_OFFSET() INIT_CANON() Initialize canonical local variables to defaults */ +// task exits without unloading this library: a HAL component left +// behind outlives task, and the shutdown waits for it; the interpreter +// has freed its module by now +void EXIT_CANON() +{ + if (kins.ctx) { + kinematicsUserFree(kins.ctx); + kins.ctx = NULL; + } + if (khal.comp_id >= 0) { + hal_exit(khal.comp_id); + khal.comp_id = -1; + } + kins.tried = khal.tried = false; +} + void INIT_CANON() { double units; diff --git a/src/emc/task/emctask.cc b/src/emc/task/emctask.cc index 9b240e1a597..acefe47c704 100644 --- a/src/emc/task/emctask.cc +++ b/src/emc/task/emctask.cc @@ -537,6 +537,7 @@ void emcTaskPlanExit() if (pinterp != NULL) { interp.exit(); } + EXIT_CANON(); } int emcTaskPlanOpen(const char *file) diff --git a/src/hal/hal_lib_query.c b/src/hal/hal_lib_query.c index 79d80d46a21..36cbad2ffb9 100644 --- a/src/hal/hal_lib_query.c +++ b/src/hal/hal_lib_query.c @@ -446,6 +446,51 @@ int hal_get_p(hal_query_t *q, hal_query_cb cb, void *arg) return rv; } +// +// hal_get_number() - Read a pin, signal or param by name, in that order, as a +// number, the way the interpreter's #<_hal[name]> does +// +// Returns 0 with *value set, -ENOENT when nothing has that name, -EFAULT when +// HAL is not mapped and -EINVAL for a port. +// +int hal_get_number(const char *name, double *value) +{ + hal_query_t q = {}; + hal_query_value_u v; + hal_type_t t; + + if(NULL == hal_data) + return -EFAULT; + q.name = name; + q.qtype = HAL_QTYPE_PIN; + if(0 == hal_get_p(&q, NULL, NULL)) { + v = q.pp.value; + t = q.pp.type; + } else { + q = (hal_query_t){}; + q.name = name; + if(0 == hal_get_s(&q, NULL, NULL)) { + v = q.sig.value; + t = q.sig.type; + } else { + q = (hal_query_t){}; + q.name = name; + q.qtype = HAL_QTYPE_PARAM; + if(0 != hal_get_p(&q, NULL, NULL)) + return -ENOENT; + v = q.pp.value; + t = q.pp.type; + } + } + switch(t) { + case HAL_BOOL: *value = (double)v.b; return 0; + case HAL_UINT: *value = (double)v.u; return 0; + case HAL_SINT: *value = (double)v.s; return 0; + case HAL_REAL: *value = (double)v.r; return 0; + default: return -EINVAL; + } +} + // // hal_getref_s() - Retrieve the pin reference to read/write the signal // From 87c90f673980f501e7ad599827070336903fc072 Mon Sep 17 00:00:00 2001 From: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:10:49 +1100 Subject: [PATCH 2/3] tests/interp/compile: bring the stub canon up to the current interface The stub lacked 17 canon calls added since the test was disabled, plus the two GET_EXTERNAL_ calls added here. Built against the internal headers it links -lrs274 alone, runs under LD_BIND_NOW and prints what expected holds. It stays skipped until the interpreter headers are exported to include/. --- tests/interp/compile/use-rs274.cc | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/interp/compile/use-rs274.cc b/tests/interp/compile/use-rs274.cc index b3403845580..9d6bdfdc0d1 100644 --- a/tests/interp/compile/use-rs274.cc +++ b/tests/interp/compile/use-rs274.cc @@ -19,6 +19,7 @@ #include "rs274ngc.hh" #include #include "nurbs_math.hh" +#include static void read_execute(InterpBase *b, const char *line) { fprintf(stderr, "> %s\n", line); @@ -82,7 +83,7 @@ void STRAIGHT_TRAVERSE(int lineno, void SET_FEED_RATE(double rate) {} void SET_FEED_REFERENCE(CANON_FEED_REFERENCE reference) {} void SET_FEED_MODE(int spindle, int mode) {} -void SET_MOTION_CONTROL_MODE(CANON_MOTION_MODE mode, double tolerance) {} +void SET_MOTION_CONTROL_MODE(CANON_MOTION_MODE mode, double tolerance, int planner_type, double scurve_peak_scale) {} void SET_NAIVECAM_TOLERANCE(double tolerance) {} void SET_CUTTER_RADIUS_COMPENSATION(double radius) {} void START_CUTTER_RADIUS_COMPENSATION(int direction) {} @@ -147,6 +148,7 @@ void USE_NO_SPINDLE_FORCE() {} void SET_TOOL_TABLE_ENTRY(int toolno, const EmcPose& offset, double diameter, double frontangle, double backangle, int orientation) {} void USE_TOOL_LENGTH_OFFSET(const EmcPose& offset) {} +void USE_TOOL_LENGTH_OFFSET(const EmcPose& offset, const EmcPose& point) {} void CHANGE_TOOL() {} void SELECT_TOOL(int tool) {} void CHANGE_TOOL_NUMBER(int toolno) {} @@ -239,3 +241,27 @@ USER_DEFINED_FUNCTION_TYPE USER_DEFINED_FUNCTION[USER_DEFINED_FUNCTION_NUM]; int GET_EXTERNAL_OFFSET_APPLIED() { return 0; }; EmcPose GET_EXTERNAL_OFFSETS(){ EmcPose retval; ZERO_EMC_POSE(retval); return retval; }; +double GET_EXTERNAL_AXIS_MAX_VELOCITY(int axis) { return 0.0; } +double GET_EXTERNAL_SPINDLE_MAX_VELOCITY(int spindle) { return 0.0; } +int GET_EXTERNAL_JOINT_POSITIONS(double *joints, int max) { return -1; } +int GET_EXTERNAL_KINS_TYPE() { return 0; } +int GET_EXTERNAL_KINS_TYPE_FLAGS(int ktype) { return -1; } +bool GET_EXTERNAL_KINEMATICS_IDENTITY() { return true; } +void *GET_EXTERNAL_KINEMATICS(const struct KinematicsUserOps **ops, const char **why) { + if (ops) *ops = NULL; + if (why) *why = "no machine"; + return NULL; +} +int GET_EXTERNAL_HAL_VALUE(const char *name, double *value) { return -ENOENT; } +int GET_EXTERNAL_G68_FRAME(double origin[3], double rotation[9]) { return -1; } +void SET_G68_FRAME(double x, double y, double z, const double rotation[9], int active) {} +void HOME_CYCLE(void) {} +void HOME_CYCLE_JOINT(int joint) {} +void JOINT_TRAVERSE(int lineno, const double *joints, int have_joints, + double x, double y, double z, double a, double b, double c, + double u, double v, double w) {} +void JOINT_FEED(int lineno, const double *joints, int have_joints, + double x, double y, double z, double a, double b, double c, + double u, double v, double w, double seconds) {} +void LOCK_SPEED_OVERRIDE(int spindle) {} +void SELECT_KINS_TYPE(int switchkins_type) {} From 9c59e59cc11f962053af9b3303120f2be91c73c1 Mon Sep 17 00:00:00 2001 From: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:09:45 +1100 Subject: [PATCH 3/3] canon: read _hal[] in the canons, replay EXIT_CANON in canterp hal_get_number() put an interpreter-specific lookup in the HAL query API. Task and the preview module now read the pin, signal or param themselves with hal_get_p() and hal_get_s(). canterp replays EXIT_CANON as it does INIT_CANON. --- include/hal.h | 4 --- src/emc/canterp/canterp.cc | 5 ++++ src/emc/rs274ngc/gcodemodule.cc | 37 +++++++++++++++++++++++++-- src/emc/task/emccanon.cc | 34 ++++++++++++++++++++++++- src/hal/hal_lib_query.c | 45 --------------------------------- 5 files changed, 73 insertions(+), 52 deletions(-) diff --git a/include/hal.h b/include/hal.h index 090f34c5e14..04b0dad1ce1 100644 --- a/include/hal.h +++ b/include/hal.h @@ -1358,10 +1358,6 @@ int hal_getref_s(hal_query_t *query); int hal_get_s(hal_query_t *query, hal_query_cb cb, void *arg); int hal_set_s(hal_query_t *query, hal_query_cb cb, void *arg); -// hal_get_number: the pin, signal or param named, in that order, as a number; -// 0, or -ENOENT when nothing has the name -int hal_get_number(const char *name, double *value); - // // *** HAL structure iteration functions *** // diff --git a/src/emc/canterp/canterp.cc b/src/emc/canterp/canterp.cc index 7826e6fb9df..e9bf99bb566 100644 --- a/src/emc/canterp/canterp.cc +++ b/src/emc/canterp/canterp.cc @@ -669,6 +669,11 @@ int Canterp::execute(const char *line) { return 0; } + if (!strcmp(the_command_name, "EXIT_CANON")) { + EXIT_CANON(); + return 0; + } + if (!strcmp(the_command_name, "TURN_PROBE_OFF")) { TURN_PROBE_OFF(); return 0; diff --git a/src/emc/rs274ngc/gcodemodule.cc b/src/emc/rs274ngc/gcodemodule.cc index b51ce6dca5f..ebea84128ce 100644 --- a/src/emc/rs274ngc/gcodemodule.cc +++ b/src/emc/rs274ngc/gcodemodule.cc @@ -1031,8 +1031,41 @@ bool GET_EXTERNAL_KINEMATICS_IDENTITY() { } } -int GET_EXTERNAL_HAL_VALUE(const char *name, double *value) { - return hal_get_number(name, value); +// the pin, signal or param named, in that order, as a number +int GET_EXTERNAL_HAL_VALUE(const char *name, double *value) +{ + hal_query_t q = {}; + hal_query_value_u v; + hal_type_t t; + + q.name = name; + q.qtype = HAL_QTYPE_PIN; + if (hal_get_p(&q, NULL, NULL) == 0) { + v = q.pp.value; + t = q.pp.type; + } else { + q = {}; + q.name = name; + if (hal_get_s(&q, NULL, NULL) == 0) { + v = q.sig.value; + t = q.sig.type; + } else { + q = {}; + q.name = name; + q.qtype = HAL_QTYPE_PARAM; + int rv = hal_get_p(&q, NULL, NULL); + if (rv != 0) { return rv == -EFAULT ? rv : -ENOENT; } + v = q.pp.value; + t = q.pp.type; + } + } + switch (t) { + case HAL_BOOL: *value = (double)v.b; return 0; + case HAL_UINT: *value = (double)v.u; return 0; + case HAL_SINT: *value = (double)v.s; return 0; + case HAL_REAL: *value = (double)v.r; return 0; + default: return -EINVAL; + } } // a module of the caller's own, on a HAL component this process attaches diff --git a/src/emc/task/emccanon.cc b/src/emc/task/emccanon.cc index 30e2aa7d5fa..c6132cbe56b 100644 --- a/src/emc/task/emccanon.cc +++ b/src/emc/task/emccanon.cc @@ -936,9 +936,41 @@ void *GET_EXTERNAL_KINEMATICS(const KinematicsUserOps **ops, const char **why) return ctx; } +// the pin, signal or param named, in that order, as a number int GET_EXTERNAL_HAL_VALUE(const char *name, double *value) { - return hal_get_number(name, value); + hal_query_t q = {}; + hal_query_value_u v; + hal_type_t t; + + q.name = name; + q.qtype = HAL_QTYPE_PIN; + if (hal_get_p(&q, NULL, NULL) == 0) { + v = q.pp.value; + t = q.pp.type; + } else { + q = {}; + q.name = name; + if (hal_get_s(&q, NULL, NULL) == 0) { + v = q.sig.value; + t = q.sig.type; + } else { + q = {}; + q.name = name; + q.qtype = HAL_QTYPE_PARAM; + int rv = hal_get_p(&q, NULL, NULL); + if (rv != 0) { return rv == -EFAULT ? rv : -ENOENT; } + v = q.pp.value; + t = q.pp.type; + } + } + switch (t) { + case HAL_BOOL: *value = (double)v.b; return 0; + case HAL_UINT: *value = (double)v.u; return 0; + case HAL_SINT: *value = (double)v.s; return 0; + case HAL_REAL: *value = (double)v.r; return 0; + default: return -EINVAL; + } } static struct { diff --git a/src/hal/hal_lib_query.c b/src/hal/hal_lib_query.c index 36cbad2ffb9..79d80d46a21 100644 --- a/src/hal/hal_lib_query.c +++ b/src/hal/hal_lib_query.c @@ -446,51 +446,6 @@ int hal_get_p(hal_query_t *q, hal_query_cb cb, void *arg) return rv; } -// -// hal_get_number() - Read a pin, signal or param by name, in that order, as a -// number, the way the interpreter's #<_hal[name]> does -// -// Returns 0 with *value set, -ENOENT when nothing has that name, -EFAULT when -// HAL is not mapped and -EINVAL for a port. -// -int hal_get_number(const char *name, double *value) -{ - hal_query_t q = {}; - hal_query_value_u v; - hal_type_t t; - - if(NULL == hal_data) - return -EFAULT; - q.name = name; - q.qtype = HAL_QTYPE_PIN; - if(0 == hal_get_p(&q, NULL, NULL)) { - v = q.pp.value; - t = q.pp.type; - } else { - q = (hal_query_t){}; - q.name = name; - if(0 == hal_get_s(&q, NULL, NULL)) { - v = q.sig.value; - t = q.sig.type; - } else { - q = (hal_query_t){}; - q.name = name; - q.qtype = HAL_QTYPE_PARAM; - if(0 != hal_get_p(&q, NULL, NULL)) - return -ENOENT; - v = q.pp.value; - t = q.pp.type; - } - } - switch(t) { - case HAL_BOOL: *value = (double)v.b; return 0; - case HAL_UINT: *value = (double)v.u; return 0; - case HAL_SINT: *value = (double)v.s; return 0; - case HAL_REAL: *value = (double)v.r; return 0; - default: return -EINVAL; - } -} - // // hal_getref_s() - Retrieve the pin reference to read/write the signal //