copp 0.2.2

Convex-objective path parameterization for robotic trajectory planning.
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
//! Python wrappers for robot and constraint-buffer construction.
//!
//! The Rust [`Robot`](crate::robot::Robot) type is generic over a robot model.
//! Python exposes one owning [`PyRobot`] class backed by [`PyRobotModel`], plus
//! a lightweight [`PyConstraints`] proxy. A [`PyConstraints`] value can either
//! be returned from `robot.constraints` or independently constructed around a
//! hidden point-mass robot when only raw TOPP constraints are needed.

use crate::copp::constraints::ModePopConstraints;
use crate::diag::{CoppError, RobotDynamicsError};
use crate::ffi::python::array::{array_like_to_vec_f64, array_like_to_vec2_f64};
use crate::ffi::python::error::to_py_err;
use crate::ffi::python::path::{PyMatrixLayout, PyPath};
use crate::robot::{Robot as RustRobot, RobotBasic, RobotTorque};
use nalgebra::DMatrix;
use numpy::{PyArray1, PyReadonlyArray1};
use pyo3::Borrowed;
use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::PyAnyMethods;
use std::sync::{Arc, Mutex, MutexGuard};

/// Shared robot state used by [`PyRobot`] and [`PyConstraints`] proxies.
pub(crate) type SharedRobot = Arc<Mutex<RustRobot<PyRobotModel>>>;

/// Register robot-related classes on the native [`PyModule`].
pub(crate) fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<PyRobot>()?;
    m.add_class::<PyConstraints>()?;
    Ok(())
}

/// Internal parser for `MatrixLayout | str | None` robot arguments.
#[derive(Clone, Copy)]
struct MatrixLayoutArg(
    /// Parsed matrix layout for Python robot inputs.
    PyMatrixLayout,
);

impl FromPyObject<'_, '_> for MatrixLayoutArg {
    /// [`PyErr`] returned when parsing fails.
    type Error = PyErr;

    /// Parse enum values, accepted strings, or `None` into a matrix layout.
    fn extract(obj: Borrowed<'_, '_, PyAny>) -> Result<Self, Self::Error> {
        if obj.is_none() {
            return Ok(Self(PyMatrixLayout::SampleMajor));
        }

        if let Ok(layout) = obj.extract::<PyMatrixLayout>() {
            return Ok(Self(layout));
        }

        if let Ok(text) = obj.extract::<&str>() {
            return match normalize_token(text).as_str() {
                "sample_major" | "samplemajor" => Ok(Self(PyMatrixLayout::SampleMajor)),
                "dim_major" | "dimmajor" => Ok(Self(PyMatrixLayout::DimMajor)),
                _ => Err(PyValueError::new_err(
                    "`layout` must be MatrixLayout.SAMPLE_MAJOR, MatrixLayout.DIM_MAJOR, \"sample_major\", or \"dim_major\"",
                )),
            };
        }

        Err(PyValueError::new_err(
            "`layout` must be a MatrixLayout value or a string",
        ))
    }
}

/// Python-owned robot wrapper backed by [`RustRobot`].
#[pyclass(name = "Robot", module = "copp_py._native")]
pub(crate) struct PyRobot {
    /// Shared robot model and constraint storage.
    inner: SharedRobot,
}

#[pymethods]
impl PyRobot {
    /// Construct a robot wrapper.
    ///
    /// When ``inverse_dynamics`` is ``None``, torque evaluation uses point-mass
    /// dynamics, i.e. ``tau = ddq``. Passing a callable or object with an
    /// ``inverse_dynamics(q, dq, ddq)`` method enables Python inverse dynamics.
    #[new]
    #[pyo3(
        signature = (dim, *, capacity = None, inverse_dynamics = None),
        text_signature = "(dim, *, capacity=None, inverse_dynamics=None)"
    )]
    fn new(
        py: Python<'_>,
        dim: usize,
        capacity: Option<usize>,
        inverse_dynamics: Option<Py<PyAny>>,
    ) -> PyResult<Self> {
        Self::from_parts(py, dim, capacity, inverse_dynamics)
    }

    /// Construct an explicit point-mass robot, where ``tau = ddq``.
    #[staticmethod]
    #[pyo3(
        signature = (dim, *, capacity = None),
        text_signature = "(dim, *, capacity=None)"
    )]
    fn point_mass(py: Python<'_>, dim: usize, capacity: Option<usize>) -> PyResult<Self> {
        Self::from_parts(py, dim, capacity, None)
    }

    /// Return the robot dimension.
    #[getter]
    fn dim(&self) -> PyResult<usize> {
        Ok(lock_robot(&self.inner)?.dim())
    }

    /// Return the number of station samples currently stored.
    #[getter]
    fn len(&self) -> PyResult<usize> {
        Ok(lock_robot(&self.inner)?.constraints.len())
    }

    /// Return the allocated station-buffer capacity.
    #[getter]
    fn capacity(&self) -> PyResult<usize> {
        Ok(lock_robot(&self.inner)?.constraints.capacity())
    }

    /// Return whether the constraint buffer contains no stations.
    #[getter]
    fn is_empty(&self) -> PyResult<bool> {
        Ok(lock_robot(&self.inner)?.constraints.is_empty())
    }

    /// Return the active global station-id range ``(idx_s_start, idx_s_end)``.
    #[getter]
    fn idx_s_range(&self) -> PyResult<(usize, usize)> {
        let robot = lock_robot(&self.inner)?;
        Ok((
            robot.constraints.idx_s_start(),
            robot.constraints.idx_s_end(),
        ))
    }

    /// Return whether a Python inverse-dynamics callback is installed.
    #[getter]
    fn has_inverse_dynamics(&self) -> PyResult<bool> {
        Ok(lock_robot(&self.inner)?.model().has_inverse_dynamics())
    }

    /// Return a lightweight proxy for raw constraint-buffer operations.
    #[getter]
    fn constraints(&self) -> PyConstraints {
        PyConstraints {
            inner: Arc::clone(&self.inner),
        }
    }

    /// Return ``len(robot)`` as the current number of station samples.
    fn __len__(&self) -> PyResult<usize> {
        self.len()
    }

    /// Return a compact representation for interactive Python sessions.
    fn __repr__(&self) -> PyResult<String> {
        let robot = lock_robot(&self.inner)?;
        Ok(format!(
            "Robot(dim={}, len={}, capacity={}, has_inverse_dynamics={})",
            robot.dim(),
            robot.constraints.len(),
            robot.constraints.capacity(),
            robot.model().has_inverse_dynamics()
        ))
    }

    /// Replace the Python inverse-dynamics callback.
    ///
    /// The callback may be a plain callable ``f(q, dq, ddq)`` or an object with
    /// a callable ``inverse_dynamics(q, dq, ddq)`` method. It must return a
    /// vector convertible to contiguous ``float64`` with shape ``(dim,)``.
    fn set_inverse_dynamics(&self, py: Python<'_>, inverse_dynamics: Py<PyAny>) -> PyResult<()> {
        validate_inverse_dynamics(py, &inverse_dynamics)?;
        lock_robot(&self.inner)?
            .model_mut()
            .set_inverse_dynamics(Some(inverse_dynamics));
        Ok(())
    }

    /// Clear the Python inverse-dynamics callback and restore point dynamics.
    fn clear_inverse_dynamics(&self) -> PyResult<()> {
        lock_robot(&self.inner)?
            .model_mut()
            .set_inverse_dynamics(None);
        Ok(())
    }

    /// Append strictly increasing station samples to the robot.
    fn append_s(&self, s: &Bound<'_, PyAny>) -> PyResult<()> {
        let s = array_like_to_vec_f64("s", s)?;
        let mut robot = lock_robot(&self.inner)?;
        robot
            .with_s(&s)
            .map(|_| ())
            .map_err(|error| to_py_err(error.into()))
    }

    /// Store discrete path derivatives over an existing station interval.
    #[pyo3(
        signature = (q, dq, ddq, idx_s, *, dddq = None, layout = MatrixLayoutArg(PyMatrixLayout::SampleMajor)),
        text_signature = "(q, dq, ddq, idx_s, *, dddq=None, layout='sample_major')"
    )]
    fn set_q(
        &self,
        q: &Bound<'_, PyAny>,
        dq: &Bound<'_, PyAny>,
        ddq: &Bound<'_, PyAny>,
        idx_s: usize,
        dddq: Option<&Bound<'_, PyAny>>,
        layout: MatrixLayoutArg,
    ) -> PyResult<()> {
        let dim = lock_robot(&self.inner)?.dim();
        let q = matrix_to_dmatrix("q", q, layout.0, Some(dim))?;
        let dq = matrix_to_dmatrix("dq", dq, layout.0, Some(dim))?;
        let ddq = matrix_to_dmatrix("ddq", ddq, layout.0, Some(dim))?;
        let dddq = dddq
            .map(|array| matrix_to_dmatrix("dddq", array, layout.0, Some(dim)))
            .transpose()?;

        let dddq_view = dddq.as_ref().map(|matrix| matrix.as_view());
        let mut robot = lock_robot(&self.inner)?;
        robot
            .with_q(
                &q.as_view(),
                &dq.as_view(),
                &ddq.as_view(),
                dddq_view.as_ref(),
                idx_s,
            )
            .map(|_| ())
            .map_err(|error| to_py_err(error.into()))
    }

    /// Sample a path and store derivatives up to second order.
    fn set_q_from_path_2nd(
        &self,
        path: PyRef<'_, PyPath>,
        idx_s_from: usize,
        idx_s_to: usize,
    ) -> PyResult<()> {
        let s = {
            let robot = lock_robot(&self.inner)?;
            robot
                .constraints
                .s_vec(idx_s_from, idx_s_to)
                .map_err(|error| to_py_err(error.into()))?
        };
        let derivs = path.evaluate_up_to_2nd_rust(&s)?;
        let mut robot = lock_robot(&self.inner)?;
        robot
            .with_q(
                &derivs.q.as_view(),
                &derivs.dq.as_ref().expect("dq must exist").as_view(),
                &derivs.ddq.as_ref().expect("ddq must exist").as_view(),
                None,
                idx_s_from,
            )
            .map(|_| ())
            .map_err(|error| to_py_err(error.into()))
    }

    /// Sample a path and store derivatives up to third order.
    fn set_q_from_path_3rd(
        &self,
        path: PyRef<'_, PyPath>,
        idx_s_from: usize,
        idx_s_to: usize,
    ) -> PyResult<()> {
        let s = {
            let robot = lock_robot(&self.inner)?;
            robot
                .constraints
                .s_vec(idx_s_from, idx_s_to)
                .map_err(|error| to_py_err(error.into()))?
        };
        let derivs = path.evaluate_up_to_3rd_rust(&s)?;
        let dddq_view = derivs.dddq.as_ref().map(|matrix| matrix.as_view());
        let mut robot = lock_robot(&self.inner)?;
        robot
            .with_q(
                &derivs.q.as_view(),
                &derivs.dq.as_ref().expect("dq must exist").as_view(),
                &derivs.ddq.as_ref().expect("ddq must exist").as_view(),
                dddq_view.as_ref(),
                idx_s_from,
            )
            .map(|_| ())
            .map_err(|error| to_py_err(error.into()))
    }

    /// Add axial velocity limits over an existing station interval.
    #[pyo3(
        signature = (upper, lower, *, start_idx_s, length = None, layout = MatrixLayoutArg(PyMatrixLayout::SampleMajor)),
        text_signature = "(upper, lower, *, start_idx_s, length=None, layout='sample_major')"
    )]
    fn add_velocity_limits(
        &self,
        upper: &Bound<'_, PyAny>,
        lower: &Bound<'_, PyAny>,
        start_idx_s: usize,
        length: Option<usize>,
        layout: MatrixLayoutArg,
    ) -> PyResult<()> {
        self.add_limits(
            upper,
            lower,
            start_idx_s,
            length,
            layout.0,
            AxialLimitKind::Velocity,
        )
    }

    /// Add axial acceleration limits over an existing station interval.
    #[pyo3(
        signature = (upper, lower, *, start_idx_s, length = None, layout = MatrixLayoutArg(PyMatrixLayout::SampleMajor)),
        text_signature = "(upper, lower, *, start_idx_s, length=None, layout='sample_major')"
    )]
    fn add_acceleration_limits(
        &self,
        upper: &Bound<'_, PyAny>,
        lower: &Bound<'_, PyAny>,
        start_idx_s: usize,
        length: Option<usize>,
        layout: MatrixLayoutArg,
    ) -> PyResult<()> {
        self.add_limits(
            upper,
            lower,
            start_idx_s,
            length,
            layout.0,
            AxialLimitKind::Acceleration,
        )
    }

    /// Add axial jerk limits over an existing station interval.
    #[pyo3(
        signature = (upper, lower, *, start_idx_s, length = None, layout = MatrixLayoutArg(PyMatrixLayout::SampleMajor)),
        text_signature = "(upper, lower, *, start_idx_s, length=None, layout='sample_major')"
    )]
    fn add_jerk_limits(
        &self,
        upper: &Bound<'_, PyAny>,
        lower: &Bound<'_, PyAny>,
        start_idx_s: usize,
        length: Option<usize>,
        layout: MatrixLayoutArg,
    ) -> PyResult<()> {
        self.add_limits(
            upper,
            lower,
            start_idx_s,
            length,
            layout.0,
            AxialLimitKind::Jerk,
        )
    }

    /// Add axial torque limits using point or Python inverse dynamics.
    #[pyo3(
        signature = (upper, lower, *, start_idx_s, length = None, layout = MatrixLayoutArg(PyMatrixLayout::SampleMajor)),
        text_signature = "(upper, lower, *, start_idx_s, length=None, layout='sample_major')"
    )]
    fn add_torque_limits(
        &self,
        upper: &Bound<'_, PyAny>,
        lower: &Bound<'_, PyAny>,
        start_idx_s: usize,
        length: Option<usize>,
        layout: MatrixLayoutArg,
    ) -> PyResult<()> {
        self.add_limits(
            upper,
            lower,
            start_idx_s,
            length,
            layout.0,
            AxialLimitKind::Torque,
        )
    }
}

impl PyRobot {
    /// Clone the shared robot handle for solver descriptors.
    pub(crate) fn shared_robot(&self) -> SharedRobot {
        Arc::clone(&self.inner)
    }

    /// Construct a [`PyRobot`] from validated constructor parts.
    fn from_parts(
        py: Python<'_>,
        dim: usize,
        capacity: Option<usize>,
        inverse_dynamics: Option<Py<PyAny>>,
    ) -> PyResult<Self> {
        if dim == 0 {
            return Err(PyValueError::new_err("`dim` must be a positive integer"));
        }
        if capacity.is_some_and(|capacity| capacity == 0) {
            return Err(PyValueError::new_err(
                "`capacity` must be a positive integer or None",
            ));
        }
        if let Some(callback) = inverse_dynamics.as_ref() {
            validate_inverse_dynamics(py, callback)?;
        }

        let model = PyRobotModel::new(dim, inverse_dynamics);
        let robot = match capacity {
            Some(capacity) => RustRobot::with_capacity(model, capacity),
            None => RustRobot::new(model),
        };

        Ok(Self {
            inner: Arc::new(Mutex::new(robot)),
        })
    }

    /// Add one family of axial limits.
    fn add_limits(
        &self,
        upper: &Bound<'_, PyAny>,
        lower: &Bound<'_, PyAny>,
        start_idx_s: usize,
        length: Option<usize>,
        layout: PyMatrixLayout,
        kind: AxialLimitKind,
    ) -> PyResult<()> {
        let mut robot = lock_robot(&self.inner)?;
        let default_length = inferred_station_length(&robot, start_idx_s);
        let upper = limit_to_dmatrix("upper", upper, robot.dim(), default_length, length, layout)?;
        let lower = limit_to_dmatrix("lower", lower, robot.dim(), default_length, length, layout)?;

        match kind {
            AxialLimitKind::Velocity => robot
                .with_axial_velocity(&upper.as_view(), &lower.as_view(), start_idx_s)
                .map(|_| ())
                .map_err(|error| to_py_err(error.into())),
            AxialLimitKind::Acceleration => robot
                .with_axial_acceleration(&upper.as_view(), &lower.as_view(), start_idx_s)
                .map(|_| ())
                .map_err(|error| to_py_err(error.into())),
            AxialLimitKind::Jerk => robot
                .with_axial_jerk(&upper.as_view(), &lower.as_view(), start_idx_s)
                .map(|_| ())
                .map_err(|error| to_py_err(error.into())),
            AxialLimitKind::Torque => {
                robot.model().clear_pending_error();
                let result = robot
                    .with_axial_torque(&upper.as_view(), &lower.as_view(), start_idx_s)
                    .map(|_| ());
                map_robot_copp_result(&robot, result)
            }
        }
    }
}

/// Raw constraint-buffer proxy returned by [`PyRobot::constraints`].
#[pyclass(name = "Constraints", module = "copp_py._native")]
pub(crate) struct PyConstraints {
    /// Shared robot state whose constraint buffer is mutated by this proxy.
    inner: SharedRobot,
}

impl PyConstraints {
    /// Clone the shared robot handle for solver descriptors.
    pub(crate) fn shared_robot(&self) -> SharedRobot {
        Arc::clone(&self.inner)
    }
}

#[pymethods]
impl PyConstraints {
    /// Construct an independent raw constraint buffer.
    ///
    /// This constructor is intended for TOPP-only workflows that do not need a
    /// full Python [`PyRobot`] object. Internally it owns a hidden point-mass
    /// robot so the same storage and validation path is shared with
    /// `robot.constraints`.
    #[new]
    #[pyo3(
        signature = (dim, *, capacity = None),
        text_signature = "(dim, *, capacity=None)"
    )]
    fn new(py: Python<'_>, dim: usize, capacity: Option<usize>) -> PyResult<Self> {
        let robot = PyRobot::from_parts(py, dim, capacity, None)?;
        Ok(Self { inner: robot.inner })
    }

    /// Return the robot/path dimension.
    #[getter]
    fn dim(&self) -> PyResult<usize> {
        Ok(lock_robot(&self.inner)?.dim())
    }

    /// Return the number of station samples currently stored.
    #[getter]
    fn len(&self) -> PyResult<usize> {
        Ok(lock_robot(&self.inner)?.constraints.len())
    }

    /// Return the allocated station-buffer capacity.
    #[getter]
    fn capacity(&self) -> PyResult<usize> {
        Ok(lock_robot(&self.inner)?.constraints.capacity())
    }

    /// Return whether the constraint buffer contains no stations.
    #[getter]
    fn is_empty(&self) -> PyResult<bool> {
        Ok(lock_robot(&self.inner)?.constraints.is_empty())
    }

    /// Return the active global station-id range ``(idx_s_start, idx_s_end)``.
    #[getter]
    fn idx_s_range(&self) -> PyResult<(usize, usize)> {
        let robot = lock_robot(&self.inner)?;
        Ok((
            robot.constraints.idx_s_start(),
            robot.constraints.idx_s_end(),
        ))
    }

    /// Return a compact proxy representation.
    fn __repr__(&self) -> PyResult<String> {
        let robot = lock_robot(&self.inner)?;
        Ok(format!(
            "Constraints(dim={}, len={}, capacity={})",
            robot.dim(),
            robot.constraints.len(),
            robot.constraints.capacity()
        ))
    }

    /// Append strictly increasing station samples to the constraint buffer.
    fn append_s(&self, s: &Bound<'_, PyAny>) -> PyResult<()> {
        let s = array_like_to_vec_f64("s", s)?;
        lock_robot(&self.inner)?
            .constraints
            .with_s(&s)
            .map(|_| ())
            .map_err(|error| to_py_err(error.into()))
    }

    /// Export station samples over a half-open station-id interval.
    #[pyo3(
        signature = (idx_s_from = None, idx_s_to = None),
        text_signature = "(idx_s_from=None, idx_s_to=None)"
    )]
    fn s_values<'py>(
        &self,
        py: Python<'py>,
        idx_s_from: Option<usize>,
        idx_s_to: Option<usize>,
    ) -> PyResult<Bound<'py, PyArray1<f64>>> {
        let robot = lock_robot(&self.inner)?;
        let (idx_s_from, idx_s_to) = default_interval(&robot, idx_s_from, idx_s_to);
        let values = robot
            .constraints
            .s_vec(idx_s_from, idx_s_to)
            .map_err(|error| to_py_err(error.into()))?;
        Ok(PyArray1::from_vec(py, values))
    }

    /// Export first-order upper bounds over a half-open station-id interval.
    #[pyo3(
        signature = (idx_s_from = None, idx_s_to = None),
        text_signature = "(idx_s_from=None, idx_s_to=None)"
    )]
    fn amax_values<'py>(
        &self,
        py: Python<'py>,
        idx_s_from: Option<usize>,
        idx_s_to: Option<usize>,
    ) -> PyResult<Bound<'py, PyArray1<f64>>> {
        let robot = lock_robot(&self.inner)?;
        let (idx_s_from, idx_s_to) = default_interval(&robot, idx_s_from, idx_s_to);
        let values = robot
            .constraints
            .amax_vec(idx_s_from, idx_s_to)
            .map_err(|error| to_py_err(error.into()))?;
        Ok(PyArray1::from_vec(py, values))
    }

    /// Overwrite first-order bounds over a station interval.
    fn amax_substitute(&self, amax: &Bound<'_, PyAny>, idx_s: usize) -> PyResult<()> {
        let amax = array_like_to_vec_f64("amax", amax)?;
        lock_robot(&self.inner)?
            .constraints
            .amax_substitute(&amax, idx_s)
            .map_err(|error| to_py_err(error.into()))
    }

    /// Clear all stored constraints and station data.
    #[pyo3(signature = (*, keep_idx_s = false), text_signature = "(*, keep_idx_s=False)")]
    fn clear(&self, keep_idx_s: bool) -> PyResult<()> {
        lock_robot(&self.inner)?.constraints.clear(keep_idx_s);
        Ok(())
    }

    /// Remove ``n_cols`` station samples from the front.
    fn pop_front_n(&self, n_cols: usize) -> PyResult<()> {
        lock_robot(&self.inner)?
            .constraints
            .pop_front(ModePopConstraints::PopNCols(n_cols));
        Ok(())
    }

    /// Remove ``n_cols`` station samples from the back.
    fn pop_back_n(&self, n_cols: usize) -> PyResult<()> {
        lock_robot(&self.inner)?
            .constraints
            .pop_back(ModePopConstraints::PopNCols(n_cols));
        Ok(())
    }

    /// Remove front samples until the kept window starts at ``idx_s_cut``.
    fn pop_front_until(&self, idx_s_cut: usize) -> PyResult<()> {
        lock_robot(&self.inner)?
            .constraints
            .pop_front(ModePopConstraints::CutAtIdxS(idx_s_cut));
        Ok(())
    }

    /// Remove back samples until the kept window ends before ``idx_s_cut``.
    fn pop_back_until(&self, idx_s_cut: usize) -> PyResult<()> {
        lock_robot(&self.inner)?
            .constraints
            .pop_back(ModePopConstraints::CutAtIdxS(idx_s_cut));
        Ok(())
    }

    /// Add or tighten first-order raw constraints.
    #[pyo3(
        signature = (amax, idx_s, *, layout = MatrixLayoutArg(PyMatrixLayout::SampleMajor)),
        text_signature = "(amax, idx_s, *, layout='sample_major')"
    )]
    fn add_constraint_1st(
        &self,
        amax: &Bound<'_, PyAny>,
        idx_s: usize,
        layout: MatrixLayoutArg,
    ) -> PyResult<()> {
        let amax = matrix_to_dmatrix("amax", amax, layout.0, None)?;
        lock_robot(&self.inner)?
            .constraints
            .with_constraint_1order(&amax.as_view(), idx_s)
            .map(|_| ())
            .map_err(|error| to_py_err(error.into()))
    }

    /// Add second-order raw constraints.
    #[pyo3(
        signature = (acc_a, acc_b, acc_max, idx_s, *, is_negative = false, layout = MatrixLayoutArg(PyMatrixLayout::SampleMajor)),
        text_signature = "(acc_a, acc_b, acc_max, idx_s, *, is_negative=False, layout='sample_major')"
    )]
    fn add_constraint_2nd(
        &self,
        acc_a: &Bound<'_, PyAny>,
        acc_b: &Bound<'_, PyAny>,
        acc_max: &Bound<'_, PyAny>,
        idx_s: usize,
        is_negative: bool,
        layout: MatrixLayoutArg,
    ) -> PyResult<()> {
        let acc_a = matrix_to_dmatrix("acc_a", acc_a, layout.0, None)?;
        let acc_b = matrix_to_dmatrix("acc_b", acc_b, layout.0, None)?;
        let acc_max = matrix_to_dmatrix("acc_max", acc_max, layout.0, None)?;
        lock_robot(&self.inner)?
            .constraints
            .with_constraint_2order(
                &acc_a.as_view(),
                &acc_b.as_view(),
                &acc_max.as_view(),
                idx_s,
                is_negative,
            )
            .map(|_| ())
            .map_err(|error| to_py_err(error.into()))
    }

    /// Add third-order raw constraints.
    #[pyo3(
        signature = (jerk_a, jerk_b, jerk_c, jerk_d, jerk_max, idx_s, *, is_negative = false, layout = MatrixLayoutArg(PyMatrixLayout::SampleMajor)),
        text_signature = "(jerk_a, jerk_b, jerk_c, jerk_d, jerk_max, idx_s, *, is_negative=False, layout='sample_major')"
    )]
    fn add_constraint_3rd(
        &self,
        jerk_a: &Bound<'_, PyAny>,
        jerk_b: &Bound<'_, PyAny>,
        jerk_c: &Bound<'_, PyAny>,
        jerk_d: &Bound<'_, PyAny>,
        jerk_max: &Bound<'_, PyAny>,
        idx_s: usize,
        is_negative: bool,
        layout: MatrixLayoutArg,
    ) -> PyResult<()> {
        let jerk_a = matrix_to_dmatrix("jerk_a", jerk_a, layout.0, None)?;
        let jerk_b = matrix_to_dmatrix("jerk_b", jerk_b, layout.0, None)?;
        let jerk_c = matrix_to_dmatrix("jerk_c", jerk_c, layout.0, None)?;
        let jerk_d = matrix_to_dmatrix("jerk_d", jerk_d, layout.0, None)?;
        let jerk_max = matrix_to_dmatrix("jerk_max", jerk_max, layout.0, None)?;
        lock_robot(&self.inner)?
            .constraints
            .with_constraint_3order(
                &jerk_a.as_view(),
                &jerk_b.as_view(),
                &jerk_c.as_view(),
                &jerk_d.as_view(),
                &jerk_max.as_view(),
                idx_s,
                is_negative,
            )
            .map(|_| ())
            .map_err(|error| to_py_err(error.into()))
    }
}

/// Python-capable robot model implementing [`RobotBasic`] and [`RobotTorque`].
pub(crate) struct PyRobotModel {
    /// Robot/path dimension.
    dim: usize,
    /// Optional Python inverse-dynamics callable or protocol object.
    inverse_dynamics: Option<Py<PyAny>>,
    /// Pending Python exception raised inside [`RobotTorque::inverse_dynamics`].
    pending_error: Mutex<Option<PyErr>>,
}

impl PyRobotModel {
    /// Construct a model with optional Python inverse dynamics.
    fn new(dim: usize, inverse_dynamics: Option<Py<PyAny>>) -> Self {
        Self {
            dim,
            inverse_dynamics,
            pending_error: Mutex::new(None),
        }
    }

    /// Return whether a Python dynamics callback is installed.
    fn has_inverse_dynamics(&self) -> bool {
        self.inverse_dynamics.is_some()
    }

    /// Replace the Python inverse-dynamics callback.
    fn set_inverse_dynamics(&mut self, inverse_dynamics: Option<Py<PyAny>>) {
        self.inverse_dynamics = inverse_dynamics;
    }

    /// Store a Python exception and return a Rust dynamics error sentinel.
    fn store_error(&self, error: PyErr) -> RobotDynamicsError {
        *self
            .pending_error
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(error);
        RobotDynamicsError::new("python inverse_dynamics callback failed")
    }

    /// Clear any stale Python callback exception.
    fn clear_pending_error(&self) {
        self.pending_error
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .take();
    }

    /// Take the pending Python callback exception, if present.
    fn take_pending_error(&self) -> Option<PyErr> {
        self.pending_error
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .take()
    }
}

impl RobotBasic for PyRobotModel {
    /// Return robot/path dimension.
    fn dim(&self) -> usize {
        self.dim
    }
}

impl RobotTorque for PyRobotModel {
    /// Evaluate point dynamics or delegate to a Python inverse-dynamics callback.
    fn inverse_dynamics(
        &self,
        q: &[f64],
        dq: &[f64],
        ddq: &[f64],
        tau: &mut [f64],
    ) -> Result<(), RobotDynamicsError> {
        let Some(callback) = &self.inverse_dynamics else {
            tau.copy_from_slice(ddq);
            return Ok(());
        };

        Python::attach(|py| -> PyResult<()> {
            let q = PyArray1::from_slice(py, q);
            let dq = PyArray1::from_slice(py, dq);
            let ddq = PyArray1::from_slice(py, ddq);
            let callback = callback.bind(py);
            let result = if callback.is_callable() {
                callback.call1((&q, &dq, &ddq))?
            } else {
                callback.call_method1("inverse_dynamics", (&q, &dq, &ddq))?
            };

            let numpy = py.import("numpy")?;
            let dtype = numpy.getattr("float64")?;
            let array_obj = numpy
                .getattr("ascontiguousarray")?
                .call1((&result, dtype))?;
            let array = array_obj.extract::<PyReadonlyArray1<'_, f64>>()?;
            let values = array.as_slice().map_err(|_| {
                PyValueError::new_err(
                    "`inverse_dynamics` must return a contiguous one-dimensional float64 array",
                )
            })?;
            if values.len() != self.dim {
                return Err(PyValueError::new_err(format!(
                    "`inverse_dynamics` returned {} values; expected {}",
                    values.len(),
                    self.dim
                )));
            }
            tau.copy_from_slice(values);
            Ok(())
        })
        .map_err(|error| self.store_error(error))
    }
}

/// Axial limit family selected by Python convenience methods.
enum AxialLimitKind {
    /// Velocity limits.
    Velocity,
    /// Acceleration limits.
    Acceleration,
    /// Jerk limits.
    Jerk,
    /// Torque limits.
    Torque,
}

/// Validate an inverse-dynamics callback or protocol object.
fn validate_inverse_dynamics(py: Python<'_>, callback: &Py<PyAny>) -> PyResult<()> {
    let callback = callback.bind(py);
    if callback.is_callable() {
        return Ok(());
    }
    if callback.hasattr("inverse_dynamics")? && callback.getattr("inverse_dynamics")?.is_callable()
    {
        return Ok(());
    }
    Err(PyValueError::new_err(
        "`inverse_dynamics` must be a callable or define an `inverse_dynamics(q, dq, ddq)` method",
    ))
}

/// Lock shared robot state with a Python-facing poisoning error.
fn lock_robot(shared: &SharedRobot) -> PyResult<MutexGuard<'_, RustRobot<PyRobotModel>>> {
    shared
        .lock()
        .map_err(|_| PyRuntimeError::new_err("internal robot state lock was poisoned"))
}

/// Return whether a shared robot has a Python inverse-dynamics callback.
pub(crate) fn shared_has_inverse_dynamics(shared: &SharedRobot) -> PyResult<bool> {
    Ok(lock_robot(shared)?.model().has_inverse_dynamics())
}

/// Borrow a shared Rust robot while holding its lock.
///
/// Solver wrappers call this directly from a detached Python thread when the
/// robot has no Python callback installed. If a callback is installed, callers
/// keep the GIL and this function preserves any pending Python exception.
pub(crate) fn with_shared_robot_result<R>(
    shared: &SharedRobot,
    f: impl FnOnce(&RustRobot<PyRobotModel>) -> Result<R, CoppError>,
) -> PyResult<R> {
    let robot = lock_robot(shared)?;
    robot.model().clear_pending_error();
    let result = f(&robot);
    map_robot_copp_result(&robot, result)
}

/// Mutably borrow a shared Rust robot while holding its lock.
pub(crate) fn with_shared_robot_mut_result<R>(
    shared: &SharedRobot,
    f: impl FnOnce(&mut RustRobot<PyRobotModel>) -> Result<R, CoppError>,
) -> PyResult<R> {
    let mut robot = lock_robot(shared)?;
    robot.model().clear_pending_error();
    let result = f(&mut robot);
    map_robot_copp_result(&robot, result)
}

/// Borrow shared raw constraints while holding their robot lock.
pub(crate) fn with_shared_constraints_result<R>(
    shared: &SharedRobot,
    f: impl FnOnce(&crate::copp::constraints::Constraints) -> PyResult<R>,
) -> PyResult<R> {
    let robot = lock_robot(shared)?;
    f(&robot.constraints)
}

/// Mutably borrow shared raw constraints while holding their robot lock.
pub(crate) fn with_shared_constraints_mut_result<R>(
    shared: &SharedRobot,
    f: impl FnOnce(&mut crate::copp::constraints::Constraints) -> PyResult<R>,
) -> PyResult<R> {
    let mut robot = lock_robot(shared)?;
    f(&mut robot.constraints)
}

/// Prefer a pending Python dynamics exception over a Rust sentinel error.
fn map_robot_copp_result<T>(
    robot: &RustRobot<PyRobotModel>,
    result: Result<T, CoppError>,
) -> PyResult<T> {
    result.map_err(|error| {
        if let Some(py_err) = robot.model().take_pending_error() {
            return py_err;
        }
        to_py_err(error)
    })
}

/// Return the default station length for a broadcast limit.
fn inferred_station_length(robot: &RustRobot<PyRobotModel>, start_idx_s: usize) -> usize {
    robot.constraints.idx_s_end().saturating_sub(start_idx_s)
}

/// Return interval defaults from the current constraint window.
fn default_interval(
    robot: &RustRobot<PyRobotModel>,
    idx_s_from: Option<usize>,
    idx_s_to: Option<usize>,
) -> (usize, usize) {
    (
        idx_s_from.unwrap_or_else(|| robot.constraints.idx_s_start()),
        idx_s_to.unwrap_or_else(|| robot.constraints.idx_s_end()),
    )
}

/// Convert a Python limit input into a Rust `(dim, n_samples)` matrix.
fn limit_to_dmatrix(
    name: &str,
    value: &Bound<'_, PyAny>,
    dim: usize,
    default_length: usize,
    length: Option<usize>,
    layout: PyMatrixLayout,
) -> PyResult<DMatrix<f64>> {
    if let Ok(values) = array_like_to_vec_f64(name, value) {
        if values.len() != dim {
            return Err(PyValueError::new_err(format!(
                "`{name}` must have length {dim}; got {}",
                values.len()
            )));
        }
        let ncols = length.unwrap_or(default_length);
        return Ok(DMatrix::from_fn(dim, ncols, |row, _| values[row]));
    }

    if let Ok(matrix) = matrix_to_dmatrix(name, value, layout, Some(dim)) {
        if let Some(length) = length
            && matrix.ncols() != length
        {
            return Err(PyValueError::new_err(format!(
                "`length` is {length}, but `{name}` contains {} station samples",
                matrix.ncols()
            )));
        }
        return Ok(matrix);
    }

    Err(PyValueError::new_err(format!(
        "`{name}` must be convertible to a one- or two-dimensional float64 array"
    )))
}

/// Convert a Python ArrayLike matrix to a Rust `(rows, n_samples)` [`DMatrix`].
fn matrix_to_dmatrix(
    name: &str,
    value: &Bound<'_, PyAny>,
    layout: PyMatrixLayout,
    expected_rows: Option<usize>,
) -> PyResult<DMatrix<f64>> {
    let (rows, cols, data) = array_like_to_vec2_f64(name, value)?;

    let matrix = match layout {
        PyMatrixLayout::SampleMajor => {
            let n_samples = rows;
            let out_rows = cols;
            let mut matrix = DMatrix::<f64>::zeros(out_rows, n_samples);
            for sample in 0..n_samples {
                for row in 0..out_rows {
                    matrix[(row, sample)] = data[sample * out_rows + row];
                }
            }
            matrix
        }
        PyMatrixLayout::DimMajor => DMatrix::from_row_slice(rows, cols, &data),
    };

    if let Some(expected_rows) = expected_rows
        && matrix.nrows() != expected_rows
    {
        return Err(PyValueError::new_err(format!(
            "`{name}` has {} rows after layout conversion; expected {expected_rows}",
            matrix.nrows()
        )));
    }

    Ok(matrix)
}

/// Normalize user-facing option strings for lenient matching.
fn normalize_token(text: &str) -> String {
    text.trim().to_ascii_lowercase().replace('-', "_")
}