ik-geo 0.1.2

Geometric solutions to inverse kinematics, using the IK-Geo algorithm
Documentation
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
use {
    super::{
        irb6640, kuka_r800_fixed_q3, rrc_fixed_q6, spherical_bot, three_parallel_bot,
        two_parallel_bot, ur5, yumi_fixed_q3,
    },
    crate::{
        inverse_kinematics::{
            auxiliary::{Kinematics, Matrix3x7, Matrix3x8},
            setups::{calculate_ik_error, ik_write_output, SetupIk},
        },
        subproblems::{auxiliary::random_angle, setups::SetupStatic, Vector7},
    },
    nalgebra::{Matrix3, Matrix3x6, Vector3, Vector6},
    std::f64::{consts::PI, NAN},
};

macro_rules! define_struct {
    ($name:ident, $num_joints:expr) => {
        pub struct $name {
            kin: Kinematics<$num_joints, { $num_joints + 1 }>,
            r: Matrix3<f64>,
            t: Vector3<f64>,

            q: Vec<Vector6<f64>>,
            is_ls: Vec<bool>,
        }
    };
}

// Define all structures
define_struct!(Irb6640, 6);
define_struct!(KukaR800FixedQ3, 7);
define_struct!(RrcFixedQ6, 7);
define_struct!(YumiFixedQ3, 7);
define_struct!(Ur5, 6);
define_struct!(ThreeParallelBot, 6);
define_struct!(TwoParallelBot, 6);
define_struct!(SphericalBot, 6);

pub fn hardcoded_setup_from_string(raw: &str, r: &mut Matrix3<f64>, t: &mut Vector3<f64>) {
    let data: Vec<f64> = raw.split(',').map(|s| s.parse().unwrap()).collect();

    *r = Matrix3::new(
        data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8],
    );

    *t = Vector3::new(data[9], data[10], data[11]);
}

impl Irb6640 {
    pub fn get_kin() -> Kinematics<6, 7> {
        let mut kin = Kinematics::new();

        let zv = Vector3::zeros();
        let ex = Vector3::x();
        let ey = Vector3::y();
        let ez = Vector3::z();

        kin.h = Matrix3x6::from_columns(&[ez, ey, ey, ex, ey, ex]);
        kin.p = Matrix3x7::from_columns(&[
            zv,
            0.32 * ex + 0.78 * ez,
            1.075 * ez,
            1.1425 * ex + 0.2 * ez,
            zv,
            zv,
            0.2 * ex,
        ]);

        kin
    }
}

impl KukaR800FixedQ3 {
    const Q3: f64 = PI / 6.0;

    pub fn get_kin() -> Kinematics<7, 8> {
        let mut kin = Kinematics::new();

        let zv = Vector3::zeros();
        let ey = Vector3::y();
        let ez = Vector3::z();

        kin.h = Matrix3x7::from_columns(&[ez, ey, ez, -ey, ez, ey, ez]);
        kin.p = Matrix3x8::from_columns(&[
            (0.15 + 0.19) * ez,
            zv,
            0.21 * ez,
            0.19 * ez,
            (0.21 + 0.19) * ez,
            zv,
            zv,
            (0.081 + 0.045) * ez,
        ]);

        kin
    }

    pub fn get_kin_partial() -> (Kinematics<6, 7>, Matrix3<f64>) {
        let kin = Self::get_kin();
        kin.forward_kinematics_partial(Self::Q3, 2, &Matrix3::identity())
    }
}

impl RrcFixedQ6 {
    const Q6: f64 = PI / 6.0;

    pub fn get_kin() -> Kinematics<7, 8> {
        let mut kin = Kinematics::new();

        let zv = Vector3::zeros();
        let ex = Vector3::x();
        let ey = Vector3::y();
        let ez = Vector3::z();

        let p01 = zv;
        let p12 = 20.0 * ex - 4.0 * ey;
        let p23 = 4.0 * ey;
        let p34 = 21.5 * ex + 3.375 * ey;
        let p45 = -3.375 * ey;
        let p56 = 21.5 * ex + 3.325 * ey;
        let p67 = -3.325 * ey;
        let p7t = 7.0 * ex;

        kin.p = 0.0254 * Matrix3x8::from_columns(&[p01, p12, p23, p34, p45, p56, p67, p7t]);
        kin.h = Matrix3x7::from_columns(&[ex, ez, ex, ez, ex, ez, ex]);

        kin
    }

    pub fn get_kin_partial() -> (Kinematics<6, 7>, Matrix3<f64>) {
        let kin = Self::get_kin();
        let (mut kin_partial, r_6t) =
            kin.forward_kinematics_partial(Self::Q6, 5, &Matrix3::identity());

        let zv = Vector3::zeros();
        let alpha = (kin_partial.h.fixed_columns::<2>(4))
            .pseudo_inverse(1e-12)
            .unwrap()
            * kin_partial.p.column(5);
        let delta_p_45 = alpha[0] * kin_partial.h.column(4);
        let delta_p_6t = alpha[1] * kin_partial.h.column(5);

        kin_partial
            .p
            .set_column(4, &(kin_partial.p.column(4) + delta_p_45));
        kin_partial.p.set_column(5, &zv);
        kin_partial
            .p
            .set_column(6, &(kin_partial.p.column(6) + delta_p_6t));

        (kin_partial, r_6t)
    }
}

impl YumiFixedQ3 {
    const Q3: f64 = PI / 6.0;

    pub fn get_kin() -> Kinematics<7, 8> {
        let mut kin = Kinematics::new();

        kin.p = Matrix3x8::from_row_slice(&[
            0.0536, 0.0642, 0.1578, 0.0880, 0.1270, 0.0354, 0.0385, 0.0040, 0.0725, 0.0527, 0.0406,
            0.0011, -0.0877, -0.0712, -0.0087, -0.0043, 0.4149, 0.0632, 0.0650, 0.0143, -0.0700,
            -0.0670, -0.0030, -0.0038,
        ]);

        kin.h = Matrix3x7::from_row_slice(&[
            0.8138, 0.1048, 0.8138, 0.1048, 0.5716, 0.1048, 0.5716, 0.3420, 0.7088, 0.3420, 0.7088,
            -0.6170, 0.7088, -0.6170, 0.4698, -0.6976, 0.4698, -0.6976, -0.5410, -0.6976, -0.5410,
        ]);

        for i in 0..kin.h.ncols() {
            kin.h.set_column(i, &kin.h.column(i).normalize());
        }

        kin
    }

    pub fn get_kin_partial() -> (Kinematics<6, 7>, Matrix3<f64>) {
        let kin = Self::get_kin();
        kin.forward_kinematics_partial(Self::Q3, 2, &Matrix3::identity())
    }
}

impl Ur5 {
    pub fn get_kin() -> Kinematics<6, 7> {
        let mut kin = Kinematics::new();

        let ex = Vector3::x();
        let ey = Vector3::y();
        let ez = Vector3::z();

        kin.h = Matrix3x6::from_columns(&[ez, ey, ey, ey, -ez, ey]);
        kin.p = Matrix3x7::from_columns(&[
            0.089159 * ez,
            0.1358 * ey,
            -0.1197 * ey + 0.425 * ex,
            0.3922 * ex,
            0.093 * ey,
            -0.0946 * ez,
            0.0823 * ey,
        ]);

        kin
    }
}

impl ThreeParallelBot {
    pub fn get_kin() -> Kinematics<6, 7> {
        let mut kin = Kinematics::new();

        let ex = Vector3::x();
        let ey = Vector3::y();
        let ez = Vector3::z();

        kin.h = Matrix3x6::from_columns(&[ez, ex, ex, ex, ez, ex]);
        kin.p = Matrix3x7::from_columns(&[ez, ey, ey, ey, ey, ey + ex, ex]);

        kin
    }
}

impl TwoParallelBot {
    pub fn get_kin() -> Kinematics<6, 7> {
        let mut kin = Kinematics::new();

        let ex = Vector3::x();
        let ey = Vector3::y();
        let ez = Vector3::z();

        let es = (ex + ez).normalize();

        kin.h = Matrix3x6::from_columns(&[ez, ex, ex, ez, ex, es]);
        kin.p = Matrix3x7::from_columns(&[ez, ey, ey, ey, ey, ey, ez]);

        kin
    }
}

impl SphericalBot {
    pub fn get_kin() -> Kinematics<6, 7> {
        let mut kin = Kinematics::new();

        let zv = Vector3::zeros();
        let ex = Vector3::x();
        let ey = Vector3::y();
        let ez = Vector3::z();

        kin.h = Matrix3x6::from_columns(&[ey, ez, ey, ex, ey, ex]);
        kin.p = Matrix3x7::from_columns(&[zv, ez + ex, ez + ex, ez + ex, zv, zv, ex]);

        kin
    }
}

// Most of the implementations in SetupIk are the same, so we can use a macro to generate them.
macro_rules! impl_setup_ik {
    // Generate the function setup_from_str, write_output,ls_count, solution_count, name, and debug
    ($name:ident) => {
        fn setup_from_str(&mut self, raw: &str) {
            hardcoded_setup_from_string(raw, &mut self.r, &mut self.t);
        }

        fn write_output(&self) -> String {
            ik_write_output(&self.q)
        }

        fn ls_count(&self) -> usize {
            self.is_ls.iter().filter(|b| **b).count()
        }

        fn solution_count(&self) -> usize {
            self.is_ls.len()
        }

        fn name(&self) -> &'static str {
            <$name as SetupStatic>::name()
        }

        fn debug(&self, i: usize) {
            println!("{i}{}{}", self.r, self.t);
        }
    };
}

impl SetupIk for Irb6640 {
    fn setup(&mut self) {
        let q = Vector6::zeros().map(|_: f64| random_angle());
        (self.r, self.t) = self.kin.forward_kinematics(&q);
    }

    impl_setup_ik!(Irb6640);

    fn run(&mut self) {
        (self.q, self.is_ls) = irb6640(&self.r, &self.t)
    }

    fn error(&self) -> f64 {
        self.q
            .iter()
            .map(|q| calculate_ik_error(&self.kin, &self.r, &self.t, q))
            .reduce(f64::min)
            .unwrap_or(NAN)
    }
}

impl SetupIk for KukaR800FixedQ3 {
    fn setup(&mut self) {
        let mut q = Vector7::zeros().map(|_: f64| random_angle());
        q[2] = Self::Q3;
        (self.r, self.t) = self.kin.forward_kinematics(&q);
    }

    impl_setup_ik!(KukaR800FixedQ3);

    fn run(&mut self) {
        (self.q, self.is_ls) = kuka_r800_fixed_q3(&self.r, &self.t);
    }

    fn error(&self) -> f64 {
        self.q
            .iter()
            .map(|q| {
                let q_e =
                    Vector7::from_column_slice(&[q[0], q[1], Self::Q3, q[2], q[3], q[4], q[5]]);

                let (r_t, t_t) = self.kin.forward_kinematics(&q_e);
                (r_t - self.r).norm() + (t_t - self.t).norm()
            })
            .reduce(f64::min)
            .unwrap_or(NAN)
    }
}

impl SetupIk for RrcFixedQ6 {
    fn setup(&mut self) {
        let mut q = Vector7::zeros().map(|_: f64| random_angle());
        q[5] = Self::Q6;
        (self.r, self.t) = self.kin.forward_kinematics(&q);
    }

    impl_setup_ik!(RrcFixedQ6);

    fn run(&mut self) {
        (self.q, self.is_ls) = rrc_fixed_q6(&self.r, &self.t);
    }

    fn error(&self) -> f64 {
        self.q
            .iter()
            .map(|q| {
                let q_e =
                    Vector7::from_column_slice(&[q[0], q[1], q[2], q[3], q[4], Self::Q6, q[5]]);

                let (r_t, t_t) = self.kin.forward_kinematics(&q_e);
                (r_t - self.r).norm() + (t_t - self.t).norm()
            })
            .reduce(f64::min)
            .unwrap_or(NAN)
    }
}

impl SetupIk for YumiFixedQ3 {
    fn setup(&mut self) {
        let mut q = Vector7::zeros().map(|_: f64| random_angle());
        q[2] = Self::Q3;
        (self.r, self.t) = self.kin.forward_kinematics(&q);
    }

    impl_setup_ik!(YumiFixedQ3);

    fn run(&mut self) {
        (self.q, self.is_ls) = yumi_fixed_q3(&self.r, &self.t);
    }

    fn error(&self) -> f64 {
        self.q
            .iter()
            .map(|q| {
                let q_e =
                    Vector7::from_column_slice(&[q[0], q[1], Self::Q3, q[2], q[3], q[4], q[5]]);

                let (r_t, t_t) = self.kin.forward_kinematics(&q_e);
                (r_t - self.r).norm() + (t_t - self.t).norm()
            })
            .reduce(f64::min)
            .unwrap_or(NAN)
    }
}

impl SetupIk for Ur5 {
    fn setup(&mut self) {
        let q = Vector6::zeros().map(|_: f64| random_angle());
        (self.r, self.t) = self.kin.forward_kinematics(&q);
    }

    impl_setup_ik!(Ur5);

    fn run(&mut self) {
        (self.q, self.is_ls) = ur5(&self.r, &self.t);
    }

    fn error(&self) -> f64 {
        self.q
            .iter()
            .map(|q| calculate_ik_error(&self.kin, &self.r, &self.t, q))
            .reduce(f64::min)
            .unwrap_or(NAN)
    }
}

impl SetupIk for ThreeParallelBot {
    fn setup(&mut self) {
        let q = Vector6::zeros().map(|_: f64| random_angle());
        (self.r, self.t) = self.kin.forward_kinematics(&q);
    }

    impl_setup_ik!(ThreeParallelBot);

    fn run(&mut self) {
        (self.q, self.is_ls) = three_parallel_bot(&self.r, &self.t);
    }

    fn error(&self) -> f64 {
        self.q
            .iter()
            .map(|q| calculate_ik_error(&self.kin, &self.r, &self.t, q))
            .reduce(f64::min)
            .unwrap_or(NAN)
    }
}

impl SetupIk for TwoParallelBot {
    fn setup(&mut self) {
        let q = Vector6::zeros().map(|_: f64| random_angle());
        (self.r, self.t) = self.kin.forward_kinematics(&q);
    }

    impl_setup_ik!(TwoParallelBot);

    fn run(&mut self) {
        (self.q, self.is_ls) = two_parallel_bot(&self.r, &self.t);
    }

    fn error(&self) -> f64 {
        self.q
            .iter()
            .map(|q| calculate_ik_error(&self.kin, &self.r, &self.t, q))
            .reduce(f64::min)
            .unwrap_or(NAN)
    }
}

impl SetupIk for SphericalBot {
    fn setup(&mut self) {
        let q = Vector6::zeros().map(|_: f64| random_angle());
        (self.r, self.t) = self.kin.forward_kinematics(&q);
    }

    impl_setup_ik!(SphericalBot);

    fn run(&mut self) {
        (self.q, self.is_ls) = spherical_bot(&self.r, &self.t);
    }

    fn error(&self) -> f64 {
        self.q
            .iter()
            .map(|q| calculate_ik_error(&self.kin, &self.r, &self.t, q))
            .reduce(f64::min)
            .unwrap_or(NAN)
    }
}

// Do setup static as a macro to avoid repition
macro_rules! impl_setup_static {
    ($name:ident, $long_name:expr) => {
        impl SetupStatic for $name {
            fn new() -> Self {
                Self {
                    kin: Self::get_kin(),
                    r: Matrix3::zeros(),
                    t: Vector3::zeros(),

                    q: Vec::new(),
                    is_ls: Vec::new(),
                }
            }

            fn name() -> &'static str {
                $long_name
            }
        }
    };
}

// Implement static setup for all the robots
impl_setup_static!(Irb6640, "IRB 6640");
impl_setup_static!(KukaR800FixedQ3, "KUKA R800 Fixed Q3");
impl_setup_static!(RrcFixedQ6, "RRC Fixed Q6");
impl_setup_static!(YumiFixedQ3, "Yumi Fixed Q3");
impl_setup_static!(Ur5, "UR5");
impl_setup_static!(ThreeParallelBot, "Three Parallel Bot");
impl_setup_static!(TwoParallelBot, "Two Parallel Bot");
impl_setup_static!(SphericalBot, "Spherical Bot");