-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Implement custom cached forward kinematics
- Loading branch information
Showing
3 changed files
with
249 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use nalgebra::{Isometry3, Matrix6xX, RealField, Translation3, Unit, UnitQuaternion, Vector3}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Joint<T: RealField> { | ||
offset: Isometry3<T>, | ||
typ: JointType<T>, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
enum JointType<T: RealField> { | ||
Revolute { axis: Unit<Vector3<T>> }, | ||
Prismatic { axis: Unit<Vector3<T>> }, | ||
} | ||
|
||
impl<T: RealField + Copy> Joint<T> { | ||
pub fn revolute(offset: Isometry3<T>, axis: Unit<Vector3<T>>) -> Joint<T> { | ||
Joint { | ||
offset, | ||
typ: JointType::Revolute { axis }, | ||
} | ||
} | ||
|
||
pub fn prismatic(offset: Isometry3<T>, axis: Unit<Vector3<T>>) -> Joint<T> { | ||
Joint { | ||
offset, | ||
typ: JointType::Prismatic { axis }, | ||
} | ||
} | ||
|
||
fn local_transform(&self, q: &[T]) -> Isometry3<T> { | ||
match self.typ { | ||
JointType::Revolute { axis } => { | ||
// let s = q[0].sin(); | ||
// let c = q[0].cos(); | ||
|
||
// let v = Vector3::x(); | ||
// let k = &axis; | ||
// let v_rot = | ||
// v * c + k.cross(&v) * s + k.scale(k.dot(&v) * (nalgebra::one::<T>() - c)); | ||
self.offset * UnitQuaternion::from_axis_angle(&axis, q[0]) | ||
} | ||
JointType::Prismatic { axis } => self.offset * Translation3::from(axis.scale(q[0])), | ||
} | ||
} | ||
|
||
pub fn nq(&self) -> usize { | ||
match self.typ { | ||
JointType::Revolute { .. } => 1, | ||
JointType::Prismatic { .. } => 1, | ||
} | ||
} | ||
} | ||
|
||
pub fn joint_kinematics<'a>( | ||
chain: &'a [Joint<f64>], | ||
q: &'a [f64], | ||
) -> impl Iterator<Item = Isometry3<f64>> + 'a { | ||
// let mut tfms = vec![]; | ||
|
||
// let mut tfm = Isometry3::identity(); | ||
// let mut qidx = 0; | ||
// for joint in chain.iter() { | ||
// tfm *= joint.local_transform(&q[qidx..(qidx + joint.nq())]); | ||
// tfms.push(tfm); | ||
|
||
// qidx += joint.nq(); | ||
// } | ||
|
||
struct State { | ||
qidx: usize, | ||
tfm: Isometry3<f64>, | ||
} | ||
|
||
// let mut qidx = 0; | ||
chain.iter().scan( | ||
State { | ||
tfm: Isometry3::identity(), | ||
qidx: 0, | ||
}, | ||
|state, joint| { | ||
state.tfm *= joint.local_transform(&q[state.qidx..(state.qidx + joint.nq())]); | ||
state.qidx += joint.nq(); | ||
|
||
Some(state.tfm) | ||
}, | ||
) | ||
|
||
// (tfms, tfm * ee_tfm) | ||
} | ||
|
||
pub fn jacobian( | ||
arm: &[Joint<f64>], | ||
ee_offset: &Isometry3<f64>, | ||
tfms: &[Isometry3<f64>], | ||
) -> Matrix6xX<f64> { | ||
let dof = arm.len(); | ||
let tfm_w_ee = tfms.last().unwrap() * ee_offset; | ||
|
||
let mut m = Matrix6xX::zeros(dof); | ||
for (col, joint) in arm.iter().enumerate() { | ||
let tfm_w_i = tfms[col]; | ||
|
||
match joint.typ { | ||
JointType::Revolute { axis, .. } => { | ||
let a_i = tfm_w_i.rotation * axis; | ||
let dp_i = a_i.cross(&(tfm_w_ee.translation.vector - tfm_w_i.translation.vector)); | ||
|
||
// in local frame | ||
let a_i = tfm_w_ee.inverse_transform_vector(&a_i); | ||
let dp_i = tfm_w_ee.inverse_transform_vector(&dp_i); | ||
|
||
m.fixed_slice_mut::<3, 1>(0, col).copy_from(&dp_i); | ||
m.fixed_slice_mut::<3, 1>(3, col).copy_from(&a_i); | ||
} | ||
JointType::Prismatic { .. } => todo!(), | ||
}; | ||
} | ||
|
||
m | ||
} |
Oops, something went wrong.