aboutsummaryrefslogtreecommitdiffstats
path: root/src/hal/components/userkins.comp
blob: f2bdafeab7213f86de9ac0074299832e0bbf4244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
component userkins"Template for user-built kinematics";

description
"""
.if \\n[.g] .mso www.tmac

The userkins.comp file is a template for creating
kinematics that can be user-built using halcompile.

The unmodified userkins component can be used
as a kinematics file for a machine with identity
kinematics for an xyz machine employing 3 joints
(motors).

\\fBUSAGE:\\fR

  1) Copy the userkins.comp file to a user-owned
     directory (\\fBmydir\\fR).

     Note: The userkins.comp file can be downloaded from:
.URL https://github.com/LinuxCNC/linuxcnc/raw/2.8/src/hal/components/userkins.comp
     where '2.8' is the branch name (use 'master' for
     the master branch)

     For a RIP (run-in-place) build, the file is located in
     the git tree as:
       src/hal/components/userkins.comp

  2) Edit the functions kinematicsForward() and
     kinematicsInverse() as required
  3) If required, add hal pins following examples in
     the template code
  4) Build and install the component using halcompile:
     $ cd \\fBmydir\\fR
     $ [sudo] halcompile --install userkins.comp
     # Note:
     #      sudo is required when using a deb install
     #      sudo is \\fBnot\\fR required for run-in-place builds
     # $ man halcompile for more info
  5) Specify userkins in an ini file as:
     \\fB[KINS]\\fR
     \\fBKINEMATICS=userkins\\fR
     \\fBJOINTS=3\\fR
     # the number of JOINTS must agree with the
     # number of joints used in your modified userkins.comp
  6) Note: the manpage for userkins is not updated by
     halcompile --install
  7) To use a different component name, rename the file
     (example mykins.comp) and change all instances of
     'userkins' to 'mykins'

\\fBNOTES:\\fR
  1  The \\fBfpin\\fR pin is included to satisfy the requirements of
     the halcompile utility but it is not accessible to kinematics
     functions.

  2  Hal pins and parameters needed in kinematics functions
     (kinematicsForward(), kinematicsInverse()) must
     be setup in a function (\\fBuserkins_setup()\\fR) invoked
     by the initial motion module call to kinematicsType().
""";
// The fpin pin is not accessible in kinematics functions.
// Use the *_setup() function for pins and params used by kinematics.
pin out s32 fpin=0"pin to demonstrate use of a conventional (non-kinematics) function fdemo";
function fdemo;
license "GPL";
author "Dewey Garrett";
;;

#include "rtapi_math.h"
#include "kinematics.h"

static struct haldata {
  // Example pin pointers
  hal_u32_t *in;
  hal_u32_t *out;
  // Example parameters
  hal_float_t param_rw;
  hal_float_t param_ro;
} *haldata;
// hal pin/param types:
// hal_bit_t   bit
// hal_u32_t   unsigned 32bit integer
// hal_s32_t   signed 32bit integer
// hal_float_t float (double precision)

FUNCTION(fdemo) {
   // This function can be added to a thread (addf) for
   // purposes not related to the kinematics functions.
   if (fpin == 0) {
       rtapi_print("fdemo function added to thread\n");
   }
   fpin++;
}

static int userkins_setup(void) {
#define HAL_PREFIX "userkins"
    int res=0;

    // inherit comp_id from rtapi_main()
    if (comp_id < 0) goto error;
    // set unready to allow creation of pins
    if (hal_set_unready(comp_id)) goto error;

    haldata = hal_malloc(sizeof(struct haldata));
    if (!haldata) goto error;

    // hal pin examples:
    res += hal_pin_u32_newf(HAL_IN ,&(haldata->in) ,comp_id,"%s.in" ,HAL_PREFIX);
    res += hal_pin_u32_newf(HAL_OUT,&(haldata->out),comp_id,"%s.out",HAL_PREFIX);

    // hal parameter examples:
    res += hal_param_float_newf(HAL_RW, &haldata->param_rw,comp_id,"%s.param-rw",HAL_PREFIX);
    res += hal_param_float_newf(HAL_RO, &haldata->param_ro,comp_id,"%s.param-ro",HAL_PREFIX);

    if (res) goto error;
    hal_ready(comp_id);
    rtapi_print("*** %s setup ok\n",__FILE__);
    return 0;
error:
    rtapi_print("\n!!! %s setup failed res=%d\n\n",__FILE__,res);
    return -1;
#undef HAL_PREFIX
}

KINS_NOT_SWITCHABLE
// see millturn.comp for example of switchable kinematics

EXPORT_SYMBOL(kinematicsType);
EXPORT_SYMBOL(kinematicsInverse);
EXPORT_SYMBOL(kinematicsForward);

KINEMATICS_TYPE kinematicsType()
{
static bool is_setup=0;
    if (!is_setup) userkins_setup();
    return KINEMATICS_IDENTITY; // set as required
           // Note: If kinematics are identity, using KINEMATICS_BOTH
           //       may be used in order to allow a gui to display
           //       joint values in preview prior to homing
} // kinematicsType()

static bool is_ready=0;
int kinematicsForward(const double *j,
                      EmcPose * pos,
                      const KINEMATICS_FORWARD_FLAGS * fflags,
                      KINEMATICS_INVERSE_FLAGS * iflags)
{
    static bool gave_msg;
    // [KINS]JOINTS=3
    pos->tran.x = j[0]; // X coordinate
    pos->tran.y = j[1]; // Y coordinate
    pos->tran.z = j[2]; // Z coordinate
    // unused coordinates:
    pos->a = 0;
    pos->b = 0;
    pos->c = 0;
    pos->u = 0;
    pos->v = 0;
    pos->w = 0;

    if (*haldata->in && !is_ready && !gave_msg) {
       rtapi_print_msg(RTAPI_MSG_ERR,
                       "%s The 'in' pin not echoed until Inverse called\n",
                      __FILE__);
       gave_msg=1;
    }
    return 0;
} // kinematicsForward()

int kinematicsInverse(const EmcPose * pos,
                      double *j,
                      const KINEMATICS_INVERSE_FLAGS * iflags,
                      KINEMATICS_FORWARD_FLAGS * fflags)
{
    is_ready = 1; // Inverse is not called until homed for KINEMATICS_BOTH

    // Update the kinematic joints specified by the
    // [KINS]JOINTS setting (3 required for this template).
    // Maximum number of joints is defined in include file:
    //         emcmotcfg:EMCMOT_MAX_JOINTS (typ 16)
    // Some joints may be claimed as 'extrajoints' and
    // do not participate in kinematics calculations --
    // $ man motion for more info
    j[0] = pos->tran.x; // joint 0
    j[1] = pos->tran.y; // joint 1
    j[2] = pos->tran.z; // joint 2

    //example hal pin update (homing reqd before kinematicsInverse)
    *haldata->out = *haldata->in; //dereference
    //read from param example: *haldata->out = haldata->param_rw;

    return 0;
} // kinematicsInverse()
bues.ch cgit interface