-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
trajgen.c
2648 lines (2472 loc) · 90 KB
/
trajgen.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* @file trajgen.c
* @author: Stefan Wilhelm (c [email protected])
* @license: BSD (see below)
* @version: 1.0b
* @standard C99
*
* Allows to move the machine tool tip to specified positions, velocities and
* accelerations by modifying a virtual machine position every generator cycle.
* The output is always an updated position for every joint. The closed loop
* controllers of the joints follow this position or, if the cannot, raise a
* trailing error fault.
*
* More details in the header file trajgen.h
*
* Annotations:
*
* - This file contains static variables pointing to your trajgen structure.
* That means it is assumed that you have only one trajectory generator
* in one process. This means as well that you instance of trajgen_t
* shall NOT be moved in memory. This disadvantage is the cost of much
* easier handling of the this piece of software. All static variables
* are assigned when you call trajgen_init(). The normal usage is to
* have a statically or dynamically allocated trajgen_t in the process
* heap or shared memory and then work with this memory block until the
* process quits.
* Interpolators, coordinated, manual and joint planer functions have
* the pointer to "this" as first function argument.
*
* - For performance reasons this file does not use volatile declarations,
* interfacing it to the realtime system is your part. The normal usage
* procedure is:
*
* - (Realtime (timed) thread function start (for the current sample interval)
* - Do pre-checks (motor states/estop/intermediate circuit/sync flags ...)
* - Conditionally trajgen_abort() on error conditions.
* - trajgen_tick();
* - Do post checks with your trajgen_t structure variable (errors etc)
* - Transfer new joint command positions into the motor control registers.
* - Transfer new digital I/O bits into the corresponding registers.
* - Process the real time FIFO/shared memory commands, e.g. to switch
* states, adapt override, add new motion commands, etc.
* - Leave thread function / wait for next sample in thread loop.
*
* -----------------------------------------------------------------------------
* License header: Copyright (c) 2008-2014, Stefan Wilhelm. All rights reserved.
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: (1) Redistributions
* of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer. (2) Redistributions in binary form must reproduce
* the above copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the distribution.
* (3) Neither the name of atwillys.de nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
* AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
******************************************************************************/
#include <stdio.h>
// <editor-fold defaultstate="collapsed" desc="Auxiliary functions/decls">
#include "trajgen.h"
/*******************************************************************************
*
* Auxiliary functions and declarations
*
******************************************************************************/
// Version for info
#define TG_VERSION "1.0b"
// Definable memset(0)
#ifndef ZERO_MEMORY
#define ZERO_MEMORY zero_memory
static void zero_memory(void *dst, size_t length)
{
char *p = (char*) dst;
char *end = ((char*) p) + length - sizeof (long);
while ((long*) p < (long*) end) {
*((long*) p) = (long) 0;
p += (unsigned long) sizeof (long);
}
end += sizeof (long);
while (p < end) { *(char*) p = (char) 0; p++; }
}
#endif
// Definable sincos
#ifndef SINCOS
#define SINCOS(x, sx, cx) do { *(sx)=sin((x)); *(cx)=cos((x)); } while(0);
#endif
// Definable square root function
#ifndef TG_SQRT_FN
#define sqrtf sqrt
#else
#define sqrtf TG_SQRT_FN
#endif
// Pointer checks
#ifdef ARG_POINTER_CHECKS
#define CHK_PTR(X) if((X))
#else
#define CHK_PTR(X) if(0)
#endif
#ifndef NO_MOTION_BLENDING
#define NO_MOTION_BLENDING (0)
#else
#undef NO_MOTION_BLENDING
#define NO_MOTION_BLENDING (1)
#endif
/* Max angle between end->start vectors where blending is still allowed */
#ifndef MAX_ALLOWED_BLENDING_ANGLE_DEG
#define MAX_ALLOWED_BLENDING_ANGLE_DEG 85
#endif
#ifndef STRAIGHT_BLENDING_ANGLE
#define STRAIGHT_BLENDING_ANGLE 1
#endif
/* Define it if you don't like inlining */
#ifndef TG_INLINE
#define TG_INLINE __inline__
#endif
// stfwi: redo this a bit nicer
#ifdef TG_DEBUG_LEVEL
#if TG_DEBUG_LEVEL > 0
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
static void tg_debug(const char* fmt, ...)
{ va_list args; va_start (args, fmt); fflush(stdout);
vfprintf(stderr, fmt, args); va_end(args); fflush(stderr); }
#define debug tg_debug
#else
#define debug tg_nodebug
#endif
#if TG_DEBUG_LEVEL < 3
static void tg_nodebug(const char* pattern, ...) { ; }
#endif
#if TG_DEBUG_LEVEL > 1
#define debug2 tg_debug
#else
#define debug2 tg_nodebug
#endif
#if TG_DEBUG_LEVEL > 2
#define debug3 tg_debug
#else
#define debug3 tg_nodebug
#endif
#else
TG_INLINE static void tg_nodebug(const char* pattern, ...) { ; }
#define debug tg_nodebug
#define debug2 tg_nodebug
#define debug3 tg_nodebug
#endif
/*
* Math "resolution", values define when a dimension or angle is treated
* as zero.
*/
#ifndef TG_D_RES
#define TG_D_RES TG_RESOLUTION
#endif
#ifndef TG_A_RES
#define TG_A_RES TG_RESOLUTION
#endif
/*
* Static switches, helps optimising the machine code
*/
#ifdef EXPORT_INTERPOLATORS
#define INTERPOLATION_STATIC
#else
#ifndef INTERPOLATION_STATIC
#define INTERPOLATION_STATIC static TG_INLINE
#endif
#endif
#ifdef EXPORT_JOINT_TG
#define JOINT_TG_STATIC
#else
#ifndef JOINT_TG_STATIC
#define JOINT_TG_STATIC static TG_INLINE
#endif
#endif
#ifdef EXPORT_MANUAL_TG
#define MANUAL_TG_STATIC
#else
#ifndef MANUAL_TG_STATIC
#define MANUAL_TG_STATIC static TG_INLINE
#endif
#endif
#ifdef EXPORT_COORD_TG
#define COORD_TG_STATIC
#define COORD_TG_STATICI
#else
#ifndef COORD_TG_STATIC
#define COORD_TG_STATIC static
#endif
#define COORD_TG_STATICI static TG_INLINE
#endif
static trajgen_error_t tg_err(unsigned code, unsigned joint);
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Math/vector auxiliaries">
/********************************************************************
*
* Vector / robotic auxiliary functions
*
********************************************************************/
#ifndef M_PI
#define M_PI (3.1415926535897932384626433832795029L) /* pi */
#endif
#ifndef M_SQRT2
#define M_SQRT2 (sqrt(2.)) /* sqrt(2) */
#endif
#define sqr(x) ((x)*(x))
#define sqrtz(X) ((X)>0?sqrtf(X):((X)>-TG_RESOLUTION?0:NAN))
#define v_cpy(vs,vd) (*(vd)=(vs))
#define v_magnitude_sq(v) (sqr(v.x)+sqr(v.y)+sqr(v.z))
#define v_magnitude(v) (sqrtz(sqr(v.x)+sqr(v.y)+sqr(v.z)))
#define v_distance(v1,v2) sqrtz(sqr((v2).x-(v1).x)+sqr((v2).y-(v1).y)+sqr((v2).z-(v1).z))
#define v_dot(v1,v2) ((v1).x*(v2).x+(v1).y*(v2).y+(v1).z*(v2).z) /* "dot" scalar product */
#define v_err_t int
#define v_isfinite(p) (isfinite((p).x) && isfinite((p).y) && isfinite((p).z))
#define v_invalidate(p) { (p).x = (p).y = (p).z = NAN; }
#define v_setzero(p) { (p).x=(p).y=(p).z=0; }
#define v_pose_split(pose, xyz, abc, uvw) { \
(xyz)->x=(pose).x; (xyz)->y=(pose).y; (xyz)->z=(pose).z; \
(uvw)->x=(pose).u; (uvw)->y=(pose).v; (uvw)->z=(pose).w; \
(abc)->x=(pose).a; (abc)->y=(pose).b; (abc)->z=(pose).c; \
}
#define v_pose_merge(xyz, abc, uvw, pose) { \
(pose)->x=(xyz).x; (pose)->y=(xyz).y; (pose)->z=(xyz).z; \
(pose)->u=(uvw).x; (pose)->v=(uvw).y; (pose)->w=(uvw).z; \
(pose)->a=(abc).x; (pose)->b=(abc).y; (pose)->c=(abc).z; \
}
TG_INLINE static v_err_t v_scale(pose_vector_t v1, real_t d, pose_vector_t * r)
{ CHK_PTR(!r) return 1; r->x=v1.x*d; r->y=v1.y*d; r->z=v1.z*d; return 0; }
TG_INLINE static v_err_t v_sum(pose_vector_t v1, pose_vector_t v2, pose_vector_t * r)
{ CHK_PTR(!r) return 1; r->x=v1.x+v2.x; r->y=v1.y+v2.y; r->z=v1.z+v2.z; return 0; }
TG_INLINE static v_err_t v_sub(pose_vector_t v1, pose_vector_t v2, pose_vector_t * r)
{ CHK_PTR(!r) return 1; r->x=v1.x-v2.x; r->y=v1.y-v2.y; r->z=v1.z-v2.z; return 0; }
TG_INLINE static v_err_t v_cross(pose_vector_t v1, pose_vector_t v2, pose_vector_t * r)
{ CHK_PTR(!r) return 1; r->x=v1.y*v2.z-v1.z*v2.y; r->y=v1.z*v2.x-v1.x*v2.z;
r->z=v1.x*v2.y-v1.y*v2.x; return 0; }
TG_INLINE static v_err_t v_unit(pose_vector_t v, pose_vector_t * r)
{ CHK_PTR(!r) return 1; real_t l; if ((l=v_magnitude(v)) == 0.0) {
r->x=r->y=r->z=0; return 1; } r->x = v.x/l; r->y = v.y/l; r->z = v.z/l; return 0; }
TG_INLINE static v_err_t v_project(pose_vector_t v1, pose_vector_t v2, pose_vector_t *r)
{ CHK_PTR(!r) return 1; if(v_unit(v2, &v2)) return 1; v_scale(v2, v_dot(v1,v2), r);
return 0; }
TG_INLINE static v_err_t v_plane_project(pose_vector_t v, pose_vector_t normal, pose_vector_t *r)
{ CHK_PTR(!r) return 1; pose_vector_t p; return v_project(v, normal, &p) || v_sub(v, p, r); }
/**
* Defines a line, precalculates characteristic values.
* @param pose_line_t *line
* @param pose_position_t start
* @param pose_position_t end
* @return int
*/
static v_err_t v_lin_define(pose_line_t *line, pose_position_t start, pose_position_t end)
{
CHK_PTR(!line) return 1;
line->start = start;
line->end = end;
v_sub(end, start, &line->u);
line->tm = v_magnitude(line->u);
if (line->tm < TG_D_RES) {
line->u.x = 1;
line->tm = line->u.y = line->u.z = 0;
} else if(v_unit(line->u, &line->u)) {
line->tm = line->u.x = line->u.y = line->u.z = 0;
return 1;
}
return 0;
}
/**
* Calculates a point for a line motion from a progress (current one-dim position)
* @param pose_line_t *line
* @param real_t progress
* @param pose_position_t *point
* @return int
*/
static v_err_t v_lin_get(pose_line_t *line, real_t progress, pose_position_t *point)
{
CHK_PTR(!line || !point) return 1;
if (line->tm <= 0 || progress >= line->tm) {
*point = line->end;
} else {
v_scale(line->u, progress, point);
v_sum(line->start, *point, point);
}
return 0;
}
/**
* Create a circle/arc/helix definition, precalculate characteristics.
* @param pose_circle_t *circle
* @param pose_position_t start
* @param pose_position_t end
* @param pose_vector_t center
* @param pose_vector_t normal
* @param int n_turns
* @return int
*/
static v_err_t v_arc_define(pose_circle_t * circle, pose_position_t start,
pose_position_t end, pose_vector_t center, pose_vector_t normal, unsigned n_turns)
{
CHK_PTR(!circle) return 1;
pose_vector_t v, e = { 0.0, 0.0, 0.0 }; // v:aux, e: rev. end vector
real_t d;
v_sub(start, center, &v); // determine projected centre point
if(v_project(v, normal, &v)) return 1;
v_sum(v, center, &circle->cp); // real center: point on normal in plane of the start point
v_unit(normal, &circle->nv); // unit normal vector (below: negative turns -> other direction)
circle->r = v_distance(start, circle->cp); // radius from start point
if(circle->r < TG_D_RES) return 1; // start radius 0
v_sub(end, center, &v); // determine projected centre point
if(v_project(v, normal, &v)) return 1;
v_sub(start, circle->cp, &circle->rt); // tangent normal vector
v_cross(circle->nv, circle->rt, &circle->rp); // vector in helix direction
v_sub(end, circle->cp, &circle->rh); // helix
v_plane_project(circle->rh, circle->nv, &e);
circle->s = v_magnitude(e) - circle->r;
v_sub(circle->rh, e, &circle->rh);
v_unit(e, &e);
v_scale(e, circle->r, &e);
if(v_magnitude(e) == 0) { v_scale(circle->nv, FLOAT_EPS, &v); v_sum(e, v, &e); }
d = v_dot(circle->rt, e)/sqr(circle->r); // determine angle
circle->a = d>1.0 ? 0.0 : (d<-1?M_PI : acos(d));
v_cross(circle->rt, e, &v); // Angle correction (0->PI --> PI->2PI)
if(v_dot(v, circle->nv) < 0) circle->a = (2.0*M_PI) - circle->a;
if(circle->a > -(TG_A_RES) && circle->a < (TG_A_RES)) circle->a = 0;
if(n_turns > 0) circle->a += n_turns * 2.0*M_PI; // add turns
circle->l = sqrtz(sqr(circle->a * circle->r) + sqr(v_magnitude(circle->rh))); // Segment length
return (circle->a == 0); // angle 0 -> error
}
/**
* Returns a point on a circle at a given angle (progress).
* @param pose_circle_t *circle
* @param real_t angle
* @param pose_position_t *point
* @return int
*/
static v_err_t v_arc_get(pose_circle_t * circle, real_t progress, pose_position_t *point)
{
CHK_PTR(!circle || !point) return 1;
pose_vector_t p, s;
real_t sx, cx;
progress /= circle->l; // length --> 0..1
SINCOS(progress * circle->a, &sx, &cx); // length --> angle
v_scale(circle->rp, sx, &s);
v_scale(circle->rt, cx, &p);
v_sum(p, s, point); // radius vector to center
v_unit(*point, &p); // add scaled radius vector
v_scale(p, progress * circle->s, &p);// parallel component
v_sum(*point, p, point);
v_scale(circle->rh, progress, &s); // perpendicular component
v_sum(*point, s, point);
v_sum(circle->cp, *point, point); // translate relative to center point
return 0;
}
#ifdef WITH_CURVES
/* Experimental, not places this code yet. */
#include "curve.inc.c"
#endif
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="interpolator_*: Interpolator">
/*******************************************************************************
*
* Interpolator (cubic spline)
*
******************************************************************************/
#ifndef NO_INTERPOLATION
INTERPOLATION_STATIC bool_t interpolator_need_update(interpolator_t *ci)
{
CHK_PTR(!ci) return INTERPOLATOR_ERROR_NULLPOINTER;
return ci->n<4;
}
INTERPOLATION_STATIC interpolator_error_t interpolator_push(interpolator_t * ip,
real_t point)
{
debug3("# ip: push %g\n", point);
// TG_CC_FN_CALL(TG_CC_interpolator_push);
CHK_PTR(!ip) return INTERPOLATOR_ERROR_NULLPOINTER;
if (ip->n >= 4) {
return INTERPOLATOR_ERROR_QUEUE_FULL;
} else if (ip->n == 0) {
return INTERPOLATOR_ERROR_NOT_RESET;
} else {
ip->x0 = ip->x1;
ip->x1 = ip->x2;
ip->x2 = ip->x3;
ip->x3 = point;
ip->n++;
}
real_t T = ip->T;
ip->w0 = (ip->x0 + 4.0*ip->x1 + ip->x2) / 6.0;
ip->w1 = (ip->x1 + 4.0*ip->x2 + ip->x3) / 6.0;
ip->v0 = ((ip->x2) - (ip->x0)) / (2.0 * (T));
ip->v1 = ((ip->x3) - (ip->x1)) / (2.0 * (T));
ip->d = ip->w0;
ip->c = ip->v0;
ip->b = 3 * (ip->w1 - ip->w0)/(T*T) - (2 * ip->v0 + ip->v1)/T;
ip->a = (ip->v1 - ip->v0)/(3.0*(T*T)) - (2.0 * ip->b)/(3.0*T);
ip->t = 0.0;
return INTERPOLATOR_OK;
}
INTERPOLATION_STATIC interpolator_error_t interpolator_reset(interpolator_t * ip,
real_t initial_position)
{
// TG_CC_FN_CALL(TG_CC_interpolator_reset);
debug3("# ip: reset\n");
CHK_PTR(!ip) return INTERPOLATOR_ERROR_NULLPOINTER;
ip->n = 1;
ip->t = 0.0;
ip->x0 = ip->x1 = ip->x2 = ip->x3 = ip->w0 = ip->w1 = ip->d = initial_position;
ip->v0 = ip->v1 = ip->a = ip->b = ip->c = 0.0;
return interpolator_push(ip, initial_position);
}
INTERPOLATION_STATIC interpolator_error_t interpolator_init(interpolator_t * ip,
real_t sample_interval, unsigned interpolation_rate)
{
debug3("# ip: init\n");
CHK_PTR(!ip) return INTERPOLATOR_ERROR_NULLPOINTER;
if (sample_interval <= 0.0 || interpolation_rate < 1) {
return INTERPOLATOR_ERROR_INIT_ARG_INVALID;
}
ip->T = sample_interval;
ip->Ti = ip->T / interpolation_rate;
interpolator_reset(ip, 0);
return INTERPOLATOR_OK;
}
INTERPOLATION_STATIC interpolator_error_t interpolator_interpolate(interpolator_t *ip,
real_t *x, real_t *v, real_t *a, real_t *j)
{
// TG_CC_FN_CALL(TG_CC_interpolator_interpolate);
CHK_PTR(!ip||!x||!v||!a||!j) return INTERPOLATOR_ERROR_INTERPOLATE_ARG_NULLPOINTER;
if (ip->n<4) interpolator_push(ip, ip->x3); // auto refill
if(ip->x3 == ip->x2 && ip->x1 == ip->x3) {
*x = ip->x3; *v = *a = *j = 0;
debug3("# ip: interpolate: positions equal: %g (v-0, a=0)\n", ip->x3);
} else {
real_t t = ip->t; // let the compiler optimise all this
*x = (((ip->a*t + ip->b)*t) + ip->c) * t + ip->d;
*v = ((3.0 * ip->a*t) + 2.0 * ip->b) * t + ip->c;
*a = 6.0 * ip->a*t + 2.0 * ip->b;
*j = 6.0 * ip->a;
debug3("# ip: interpolate: s%g v%g a%g\n", *x, *v, *a);
}
ip->t += ip->Ti;
if (fabs(ip->T - ip->t) < 0.5 * ip->Ti) ip->n--;
return INTERPOLATOR_OK;
}
#else
INTERPOLATION_STATIC bool_t interpolator_need_update(interpolator_t *ip)
{
CHK_PTR(!ip) return INTERPOLATOR_ERROR_NULLPOINTER;
return ip->n<4;
}
INTERPOLATION_STATIC interpolator_error_t interpolator_push(interpolator_t * ip,
real_t point)
{
CHK_PTR(!ip) return INTERPOLATOR_ERROR_NULLPOINTER;
if (ip->n >= 4) {
return INTERPOLATOR_ERROR_QUEUE_FULL;
} else if (ip->n == 0) {
return INTERPOLATOR_ERROR_NOT_RESET;
} else {
ip->x0 = ip->x1;
ip->x1 = ip->x2;
ip->x2 = ip->x3;
ip->x3 = point;
ip->n++;
}
return INTERPOLATOR_OK;
}
INTERPOLATION_STATIC interpolator_error_t interpolator_reset(interpolator_t * ip,
real_t initial_position)
{
CHK_PTR(!ip) return INTERPOLATOR_ERROR_NULLPOINTER;
ip->n = 2;
ip->t = 0.0;
ip->x0 = ip->x1 = ip->x2 = ip->x3 = ip->w0 = ip->w1 = ip->d = initial_position;
ip->v0 = ip->v1 = ip->a = ip->b = ip->c = 0.0;
return interpolator_push(ip, initial_position);
}
INTERPOLATION_STATIC interpolator_error_t interpolator_init(interpolator_t * ip,
real_t sample_interval, unsigned interpolation_rate)
{
CHK_PTR(!ip) return INTERPOLATOR_ERROR_NULLPOINTER;
if (sample_interval <= 0.0 || interpolation_rate != 1) {
return INTERPOLATOR_ERROR_INIT_ARG_INVALID;
}
ip->T = sample_interval;
ip->Ti = 1.0/ip->T;
interpolator_reset(ip, 0);
return INTERPOLATOR_OK;
}
INTERPOLATION_STATIC interpolator_error_t interpolator_interpolate(interpolator_t *ip,
real_t *x, real_t *v, real_t *a, real_t *j)
{
CHK_PTR(!ip || !x || !v || !a || !j) return INTERPOLATOR_ERROR_INTERPOLATE_ARG_NULLPOINTER;
if (ip->n<4) interpolator_push(ip, ip->x3);
if(ip->x3 == ip->x2 && ip->x1 == ip->x3) {
*x = ip->x3; *v = *a = *j = 0;
} else {
*x = ip->x3;
*v = (ip->x3 - ip->x2) * ip->Ti;
*a = (*v-ip->v0) * ip->Ti;
*j = (*a-ip->v1) * ip->Ti;
ip->v0 = *v;
ip->v1 = *a;
}
ip->n--;
return INTERPOLATOR_OK;
}
#endif
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="kinematics_*: Default identity kinematic model">
/*******************************************************************************
*
* Kinematics manager. Built-in default is an identity system.
* See header file for documentation about how to set your own kinematics.
*
******************************************************************************/
/**
* Default passthrough forward kinematics
* @param real_t *joint_positions
* @param pose_t *world
* @return kinematics_error_t
*/
static kinematics_error_t kinematics_identity_forward(real_t **joint_positions,
pose_t *world)
{
CHK_PTR(!joint_positions || !world) return KINEMATICS_ERR_NULL_POINTER;
world->x = *(joint_positions[0]);
world->y = *(joint_positions[1]);
world->z = *(joint_positions[2]);
world->a = *(joint_positions[3]);
world->b = *(joint_positions[4]);
world->c = *(joint_positions[5]);
world->u = *(joint_positions[6]);
world->v = *(joint_positions[7]);
world->w = *(joint_positions[8]);
return KINEMATICS_ERR_OK;
}
/**
* Default passthrough inverse kinematics
* @param pose_t *world
* @param real_t *joint_positions
* @return kinematics_error_t
*/
static kinematics_error_t kinematics_identity_inverse(pose_t *world, real_t **joint_positions)
{
CHK_PTR(!joint_positions || !world) return KINEMATICS_ERR_NULL_POINTER;
*(joint_positions[0]) = world->x;
*(joint_positions[1]) = world->y;
*(joint_positions[2]) = world->z;
*(joint_positions[3]) = world->a;
*(joint_positions[4]) = world->b;
*(joint_positions[5]) = world->c;
*(joint_positions[6]) = world->u;
*(joint_positions[7]) = world->v;
*(joint_positions[8]) = world->w;
return KINEMATICS_ERR_OK;
}
/**
* Default passthrough kinematics homeing
* @param pose_t *world
* @param real_t *joint_positions
* @return kinematics_error_t
*/
static kinematics_error_t kinematics_identity_home(pose_t *world, real_t **joint_positions)
{
CHK_PTR(!joint_positions || !world) return KINEMATICS_ERR_NULL_POINTER;
return kinematics_identity_forward(joint_positions, world);
}
/**
* Selects and initializes a machine type, sets the appropriate pointers to the
* associated functions.
* @param kinematics_t *kin
* @param kinematics_t *device_kinematics
* @return kinematics_error_t
*/
kinematics_error_t kinematics_initialize(kinematics_t *kin, kinematics_t device_kinematics)
{
if (!kin) return KINEMATICS_ERR_NULL_POINTER;
kin->forward = kinematics_identity_forward;
kin->inverse = kinematics_identity_inverse;
kin->reset = kinematics_identity_home;
if(device_kinematics.forward) kin->forward = device_kinematics.forward;
if(device_kinematics.inverse) kin->inverse = device_kinematics.inverse;
if(device_kinematics.reset) kin->reset = device_kinematics.reset;
if((!device_kinematics.forward) ^ (!device_kinematics.inverse)) {
return KINEMATICS_ERR_INIT_FUNCTION_NULL;
}
return KINEMATICS_ERR_OK;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="trajgen_coord_*: Coordinated motion planer">
/*******************************************************************************
*
*
* Coordinated motion planner.
*
*
******************************************************************************/
/**
* Error callback wrapper, passes through the error code without changing.
* @param trajgen_coord_error_t e
* @return trajgen_coord_error_t
*/
static TG_INLINE trajgen_coord_error_t trajgen_coord_raise_error(trajgen_coord_t* tp,
trajgen_coord_error_t e)
{
if (!tp) return e;
tg_err(e, 0);
return tp->last_error = e;
}
/**
* Calculates a unit vector for starting a segment.
* @param trajgen_sg_t *sg
* @param pose_vector_t* result
* @return trajgen_coord_error_t
*/
static trajgen_coord_error_t trajgen_sg_start_direction(trajgen_coord_t* tp,
trajgen_sg_t *sg, pose_vector_t *result)
{
CHK_PTR(!sg || !result) return TP_ERR_TP_NULL_POINTER;
switch (sg->motion_type) {
case SG_LINEAR:
{
v_sub(sg->coords.line.xyz.end, sg->coords.line.xyz.start, result);
v_unit(*result, result);
return TP_ERR_OK;
}
case SG_CIRCULAR:
{
pose_position_t startpoint;
pose_vector_t radius, tan, perp;
v_arc_get(&sg->coords.arc.xyz, 0.0, &startpoint);
v_sub(startpoint, sg->coords.arc.xyz.cp, &radius);
v_cross(sg->coords.arc.xyz.nv, radius, &tan);
v_unit(tan, &tan);
v_sub(sg->coords.arc.xyz.cp, startpoint, &perp);
v_unit(perp, &perp);
v_scale(tan, sg->a, &tan);
v_scale(perp, sqr(0.5 * sg->v) / sg->coords.arc.xyz.r, &perp);
v_sum(tan, perp, result);
v_unit(*result, result);
return TP_ERR_OK;
}
#ifdef WITH_CURVES
case SG_CURVE:
{
v_cpy(sg->coords.curve.xyz.suv, result);
return TP_ERR_OK;
}
#endif
}
return trajgen_coord_raise_error(tp, TP_ERR_INVALID_MOTION_TYPE);
}
/**
* Calculates a unit vector at the end of a segment.
* @param trajgen_sg_t *sg
* @param pose_vector_t* result
* @return trajgen_coord_error_t
*/
static trajgen_coord_error_t trajgen_sg_end_direction(trajgen_coord_t* tp,
trajgen_sg_t *sg, pose_vector_t *result)
{
CHK_PTR(!sg || !result) return TP_ERR_TP_NULL_POINTER;
switch (sg->motion_type) {
case SG_LINEAR:
{
v_sub(sg->coords.line.xyz.end, sg->coords.line.xyz.start, result);
v_unit(*result, result);
return TP_ERR_OK;
}
case SG_CIRCULAR:
{
pose_position_t endpoint;
pose_vector_t radius;
v_arc_get(&sg->coords.arc.xyz, sg->coords.arc.xyz.a, &endpoint);
v_sub(endpoint, sg->coords.arc.xyz.cp, &radius);
v_cross(sg->coords.arc.xyz.nv, radius, result);
v_unit(*result, result);
return TP_ERR_OK;
}
#ifdef WITH_CURVES
case SG_CURVE:
{
v_cpy(sg->coords.curve.xyz.euv, result);
return TP_ERR_OK;
}
#endif
}
return trajgen_coord_raise_error(tp, TP_ERR_INVALID_MOTION_TYPE);
}
/**
* Push a new segment into the queue.
*/
static TG_INLINE trajgen_coord_error_t trajgen_coord_queue_push_back(trajgen_coord_t* tp,
trajgen_sg_t *sg)
{
CHK_PTR(sg) return trajgen_coord_raise_error(tp, TP_ERR_TP_NULL_POINTER);
unsigned next = (tp->queue.end + 1) % tp->queue.size;
if (next == tp->queue.start) return trajgen_coord_raise_error(tp, TP_ERR_QUEUE_FULL);
tp->queue.queue[tp->queue.end] = *sg;
tp->queue.num_elements++;
tp->queue.end = next;
debug2("# tq: pushed segment %lu\n", (unsigned long)sg->id);
return TP_ERR_OK;
}
/**
* Removes n elements from the front of the queue
* @return trajgen_coord_error_t
*/
static TG_INLINE trajgen_coord_error_t trajgen_coord_queue_pop_front(trajgen_coord_t* tp,
int n)
{
if (n <= 0) return TP_ERR_OK;
if (n > tp->queue.num_elements) {
return trajgen_coord_raise_error(tp, TP_ERR_QUEUE_TO_MANY_ELEMENTS_TO_REMOVE);
}
debug2("# tq: popped segment %lu\n", (unsigned long)(tp->queue.queue[tp->queue.start].id));
tp->queue.start = (tp->queue.start + n) % tp->queue.size;
tp->queue.num_elements -= n;
return TP_ERR_OK;
}
/**
* Returns the head item of the queue
* @return trajgen_sg_t *
*/
static TG_INLINE trajgen_sg_t *trajgen_coord_queue_front(trajgen_coord_t* tp)
{ return (tp->queue.num_elements==0) ? (trajgen_sg_t*) 0 :
(&(tp->queue.queue[tp->queue.start])); }
/**
* Returns a queue item by value without modifying the queue
* @return trajgen_sg_t *
*/
static TG_INLINE trajgen_sg_t *trajgen_coord_queue_get(trajgen_coord_t* tp, unsigned n)
{ return (n>=tp->queue.num_elements) ? (trajgen_sg_t*) 0 :
&(tp->queue.queue[(tp->queue.start + n) % tp->queue.size]); }
/**
* Returns true if the queue is completely full. For margin checks, the function
* trajgen_coord_get_num_queued() can be used in combination with the known size.
* @return bool_t
*/
COORD_TG_STATICI bool_t trajgen_coord_is_queue_full(trajgen_coord_t* tp)
{ return tp->queue.num_elements >= tp->queue.size-1; }
//{ return (bool_t) (((tp->queue.end+1) % tp->queue.size) == tp->queue.start); }
/**
* Returns the size (maximum length) of the queue
* @return uint16_t
*/
COORD_TG_STATICI uint16_t trajgen_coord_get_queue_size(trajgen_coord_t* tp)
{ return tp->queue.size; }
/**
* Resets the planner without overwriting the configuration and actual position
* @return trajgen_coord_error_t
*/
COORD_TG_STATICI trajgen_coord_error_t trajgen_coord_reset(trajgen_coord_t* tp)
{
CHK_PTR(!tp) return TP_ERR_TP_NULL_POINTER;
tp->last_error = TP_ERR_OK;
tp->end_pose = tp->_i.last_position = tp->pose;
tp->queue.num_elements = tp->queue.start = tp->queue.end = 0;
tp->sg_id = tp->next_sg_id = tp->num_queued_active = 0;
tp->is_aborting = tp->is_pausing = 0;
tp->sync_dio_current = tp->sync_dio_command.clear = tp->sync_dio_command.set = 0;
tp->override = tp->_i.override = 1.0;
tp->override_enabled = 1;
tp->sync.type = TP_SYNC_NONE;
tp->sync.reference = 0;
tp->sync.i_offset = tp->sync.i_isset = 0;
tp->is_done = 1;
return TP_ERR_OK;
}
/**
* Initializes the planner
* @return trajgen_coord_error_t
*/
COORD_TG_STATICI trajgen_coord_error_t trajgen_coord_init(trajgen_coord_t *tp,
trajgen_coord_config_t config)
{
if (!tp) return TP_ERR_TP_NULL_POINTER;
ZERO_MEMORY(tp, sizeof (trajgen_coord_t)); // Set all values and pointers to 0
trajgen_coord_reset(tp);
tp->queue.size = TG_QUEUE_SIZE;
tp->config = config;
if (config.max_acceleration <= 0.0) {
return trajgen_coord_raise_error(tp, TP_ERR_INVALID_ACCEL);
} else if (config.max_velocity <= 0.0) {
return trajgen_coord_raise_error(tp, TP_ERR_INVALID_SPEED);
} else if (config.sample_interval <= 0.0) {
return trajgen_coord_raise_error(tp, TP_ERR_INVALID_SAMPLE_INTERVAL);
} else {
return TP_ERR_OK;
}
}
/**
* Set the current position of the planer. DO NOT TO THIS WHEN THE planer is
* running (done!=1).
* @param pose_t pos
* @return trajgen_coord_error_t
*/
COORD_TG_STATICI trajgen_coord_error_t trajgen_coord_set_position(trajgen_coord_t* tp,
pose_t p)
{ tp->pose = tp->end_pose = p; return TP_ERR_OK; }
/**
* Pause motion
* @return trajgen_coord_error_t
*/
COORD_TG_STATICI trajgen_coord_error_t trajgen_coord_pause(trajgen_coord_t* tp)
{ tp->is_pausing = 1; return TP_ERR_OK; }
/**
* Resume from pause state
* @return trajgen_coord_error_t
*/
COORD_TG_STATICI trajgen_coord_error_t trajgen_coord_resume(trajgen_coord_t* tp)
{ tp->is_pausing = 0; return TP_ERR_OK; }
/**
* Abort all coordinated motion
* @return trajgen_coord_error_t
*/
COORD_TG_STATICI trajgen_coord_error_t trajgen_coord_abort(trajgen_coord_t* tp)
{
CHK_PTR(!tp) return TP_ERR_TP_NULL_POINTER;
if (!tp->is_aborting) tp->is_pausing = tp->is_aborting = 1;
tp->sync_dio_command.clear = tp->sync_dio_command.set = 0;
return TP_ERR_OK;
}
/**
* Returns the id of the last motion that is currently executing.
* @return uint32_t
*/
COORD_TG_STATICI uint32_t trajgen_coord_get_current_id(trajgen_coord_t* tp)
{ return tp->sg_id; }
/**
* Returns the actual output position of the generator
* @return pose_t
*/
COORD_TG_STATICI pose_t trajgen_coord_get_position(trajgen_coord_t* tp)
{ return tp->pose; }
/**
* Returns nonzero if the generator finished processing the queue and
* reached the target position.
* @return bool_t
*/
COORD_TG_STATICI bool_t trajgen_coord_is_done(trajgen_coord_t* tp)
{ return tp->is_done; }
/**
* Returns the number of currently queued items
* @return uint16_t
*/
COORD_TG_STATICI uint16_t trajgen_coord_get_num_queued(trajgen_coord_t* tp)
{ return tp->queue.num_elements; }
/**
* Returns the number of currently processed items in the queue
* @return uint16_t
*/
COORD_TG_STATICI uint16_t trajgen_coord_get_num_queued_active(trajgen_coord_t* tp)
{ return tp->num_queued_active; }
/**
* Add a straight line into the queue. Current speed/accel/io settings apply.
* @param pose_t end_point
* @param real_t velocity
* @param real_t acceleration
* @return trajgen_coord_error_t
*/
COORD_TG_STATICI trajgen_coord_error_t trajgen_coord_add_line(trajgen_coord_t* tp,
pose_t end_point, real_t velocity, real_t acceleration)
{
trajgen_sg_t sg;
//pose_line_t line_xyz, line_uvw, line_abc;
pose_position_t start_xyz, end_xyz, start_uvw, end_uvw, start_abc, end_abc;
sg.sync_dio = tp->sync_dio_command;
tp->sync_dio_command.clear = tp->sync_dio_command.set = 0;
if (tp->is_aborting) return trajgen_coord_raise_error(tp, TP_ERR_ABORTING);
if (trajgen_coord_is_queue_full(tp)) return trajgen_coord_raise_error(tp, TP_ERR_QUEUE_FULL);
acceleration = acceleration == 0 ? tp->config.max_acceleration : acceleration;
if (velocity < TG_RESOLUTION || velocity > tp->config.max_velocity) {
return trajgen_coord_raise_error(tp, TP_ERR_INVALID_SPEED);
} else if (acceleration < TG_RESOLUTION || acceleration > tp->config.max_acceleration) {
return trajgen_coord_raise_error(tp, TP_ERR_INVALID_ACCEL);
} else if(!pose_isfinite(end_point)) {
return trajgen_coord_raise_error(tp, TP_ERR_INVALID_POSE);
}
v_pose_split(tp->end_pose, &start_xyz, &start_abc, &start_uvw);
v_pose_split(end_point, &end_xyz, &end_abc, &end_uvw);
if(
v_lin_define(&sg.coords.line.xyz, start_xyz, end_xyz)
#ifndef NO_UVW
|| v_lin_define(&sg.coords.line.uvw, start_uvw, end_uvw)
#endif
#ifndef NO_ABC
|| v_lin_define(&sg.coords.line.abc, start_abc, end_abc)
#endif
) {
return trajgen_coord_raise_error(tp, TP_ERR_INVALID_PARAM);
}
#ifdef NO_ABC
v_setzero(sg.coords.line.abc.start);
v_setzero(sg.coords.line.abc.end);
v_setzero(sg.coords.line.abc.u);
sg.coords.line.abc.tm = 0;
#endif
#ifdef NO_UVW
v_setzero(sg.coords.line.uvw.start);
v_setzero(sg.coords.line.uvw.end);
v_setzero(sg.coords.line.uvw.u);
sg.coords.line.uvw.tm = 0;
#endif
sg.length = (sg.coords.line.xyz.tm>TG_D_RES) ? sg.coords.line.xyz.tm :
(sg.coords.line.uvw.tm>TG_D_RES) ? (sg.coords.line.uvw.tm) : (sg.coords.line.abc.tm);
if(sg.length < TG_RESOLUTION) {
sg.length = 0;