Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions include/kinematics_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
17 changes: 17 additions & 0 deletions include/nml_intf/canon.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/emc/canterp/canterp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/emc/kinematics_userspace/kinematics_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
6 changes: 3 additions & 3 deletions src/emc/rs274ngc/Submakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
Expand All @@ -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++

Expand Down
90 changes: 90 additions & 0 deletions src/emc/rs274ngc/gcodemodule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
#include "nml_intf/nurbs_math.hh"
#include <axis_kinds.hh>
#include <kinematics.h> // KINEMATICS_IDENTITY
#include <kinematics_user.h>
#include <hal.h>
#include <unistd.h>

int _task = 0; // control preview behaviour when remapping

Expand Down Expand Up @@ -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*/) {}

Expand Down Expand Up @@ -1026,6 +1030,92 @@ bool GET_EXTERNAL_KINEMATICS_IDENTITY() {
return true;
}
}

// 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
// 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)
Expand Down
9 changes: 5 additions & 4 deletions src/emc/rs274ngc/interp_internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 9 additions & 64 deletions src/emc/rs274ngc/interp_namedparams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace bp = boost::python;
#include "rs274ngc_interp.hh"
#include <inifile.hh>

// for HAL pin variables
#include <hal.h>
// ENOENT, for a HAL name not found
#include <errno.h>

using namespace linuxcnc;

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/emc/rs274ngc/interp_setup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
Loading