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
use na::{DVectorSliceMut, Isometry3, RealField, Unit, Vector3};

use crate::joint::{Joint, PrismaticJoint, RevoluteJoint};
use crate::math::{JacobianSliceMut, Velocity};
use crate::solver::{ConstraintSet, GenericNonlinearConstraint, IntegrationParameters};

/// A joint that allows one translational and one rotational degrees of freedom along a single axis.
#[derive(Copy, Clone, Debug)]
pub struct CylindricalJoint<N: RealField> {
prism: PrismaticJoint<N>,
revo: RevoluteJoint<N>,
}

impl<N: RealField> CylindricalJoint<N> {
/// Create a cylindrical joint with the given axis and initial position of angle.
///
/// The axis is expressed in the local space of the multibody links attached to this joint.
pub fn new(axis: Unit<Vector3<N>>, position: N, angle: N) -> Self {
let prism = PrismaticJoint::new(axis, position);
let revo = RevoluteJoint::new(axis, angle);

CylindricalJoint { prism, revo }
}
}

impl<N: RealField> Joint<N> for CylindricalJoint<N> {
#[inline]
fn ndofs(&self) -> usize {
2
}

fn body_to_parent(&self, parent_shift: &Vector3<N>, body_shift: &Vector3<N>) -> Isometry3<N> {
self.prism.translation() * self.revo.body_to_parent(parent_shift, body_shift)
}

fn update_jacobians(&mut self, body_shift: &Vector3<N>, vels: &[N]) {
self.prism.update_jacobians(body_shift, vels);
self.revo.update_jacobians(body_shift, &[vels[1]]);
}

fn jacobian(&self, transform: &Isometry3<N>, out: &mut JacobianSliceMut<N>) {
self.prism.jacobian(transform, &mut out.columns_mut(0, 1));
self.revo.jacobian(transform, &mut out.columns_mut(1, 1));
}

fn jacobian_dot(&self, transform: &Isometry3<N>, out: &mut JacobianSliceMut<N>) {
self.prism
.jacobian_dot(transform, &mut out.columns_mut(0, 1));
self.revo
.jacobian_dot(transform, &mut out.columns_mut(1, 1));
}

fn jacobian_dot_veldiff_mul_coordinates(
&self,
transform: &Isometry3<N>,
vels: &[N],
out: &mut JacobianSliceMut<N>,
) {
self.prism.jacobian_dot_veldiff_mul_coordinates(
transform,
vels,
&mut out.columns_mut(0, 1),
);
self.revo.jacobian_dot_veldiff_mul_coordinates(
transform,
&[vels[1]],
&mut out.columns_mut(1, 1),
);
}

fn jacobian_mul_coordinates(&self, vels: &[N]) -> Velocity<N> {
self.prism.jacobian_mul_coordinates(vels) + self.revo.jacobian_mul_coordinates(&[vels[1]])
}

fn jacobian_dot_mul_coordinates(&self, vels: &[N]) -> Velocity<N> {
// NOTE: The following is zero.
// self.prism.jacobian_dot_mul_coordinates(vels) +
self.revo.jacobian_dot_mul_coordinates(&[vels[1]])
}

fn default_damping(&self, out: &mut DVectorSliceMut<N>) {
self.prism.default_damping(&mut out.rows_mut(0, 1));
self.revo.default_damping(&mut out.rows_mut(1, 1));
}

fn integrate(&mut self, parameters: &IntegrationParameters<N>, vels: &[N]) {
self.prism.integrate(parameters, vels);
self.revo.integrate(parameters, &[vels[1]]);
}

fn apply_displacement(&mut self, disp: &[N]) {
self.prism.apply_displacement(disp);
self.revo.apply_displacement(&[disp[1]]);
}

#[inline]
fn clone(&self) -> Box<dyn Joint<N>> {
Box::new(*self)
}

fn num_velocity_constraints(&self) -> usize {
self.prism.num_velocity_constraints() + self.revo.num_velocity_constraints()
}

fn velocity_constraints(
&self,
parameters: &IntegrationParameters<N>,
multibody: &Multibody<N>,
assembly_id: usize,
dof_id: usize,
ext_vels: &[N],
ground_j_id: &mut usize,
jacobians: &mut [N],
constraints: &mut ConstraintSet<N, (), (), usize>,
) {
self.prism.velocity_constraints(
parameters,
multibody,
assembly_id,
dof_id,
ext_vels,
ground_j_id,
jacobians,
constraints,
);
self.revo.velocity_constraints(
parameters,
multibody,
assembly_id,
dof_id + 1,
ext_vels,
ground_j_id,
jacobians,
constraints,
);
}

fn num_position_constraints(&self) -> usize {
// NOTE: we don't test if constraints exist to simplify indexing.
2
}

fn position_constraint(
&self,
i: usize,
multibody: &Multibody<N>,
handle: BodyPartHandle<()>,
dof_id: usize,
jacobians: &mut [N],
) -> Option<GenericNonlinearConstraint<N, ()>> {
if i == 0 {
self.prism
.position_constraint(0, multibody, link, handle, dof_id, jacobians)
} else {
self.revo
.position_constraint(0, multibody, link, handle, dof_id + 1, jacobians)
}
}
}

prismatic_motor_limit_methods!(CylindricalJoint, prism);
revolute_motor_limit_methods!(CylindricalJoint, revo);