Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

having issue when creating a chain that is less than 5 Nodes long #8

Closed
chitalian opened this issue Nov 23, 2019 · 2 comments
Closed

Comments

@chitalian
Copy link

chitalian commented Nov 23, 2019

Hi,

I am still new to robotics and rust. I am trying to make a simple chain with 2 segments that are connected via three nodes. I am modifying the example code. I can't really seem to figure out what I am doing wrong. Sorry if this is a silly question. Is what I am trying to do even possible?

fn create_joint_with_link_array() -> k::Node<f32> {
    let fixed: k::Node<f32> = JointBuilder::new()
        .name("fixed")
        .joint_type(JointType::Fixed)
        .translation(Translation3::new(0.0, 0.0, 0.6))
        .finalize()
        .into();
    let l0: k::Node<f32> = JointBuilder::new()
        .name("shoulder_pitch")
        .joint_type(JointType::Rotational {
            axis: Vector3::y_axis(),
        })
        .translation(Translation3::new(0.0, 0.1, 0.0))
        .finalize()
        .into();
    let l1: k::Node<f32> = JointBuilder::new()
        .name("shoulder_roll")
        .joint_type(JointType::Rotational {
            axis: Vector3::x_axis(),
        })
        .translation(Translation3::new(0.0, 0.1, 0.0))
        .finalize()
        .into();

    connect![fixed => l0 => l1];
    fixed
}

fn create_cubes(window: &mut Window) -> Vec<SceneNode> {
    let mut c_fixed = window.add_cube(0.05, 0.05, 0.05);
    c_fixed.set_color(0.2, 0.2, 0.2);
    let mut c0 = window.add_cube(0.1, 0.1, 0.1);
    c0.set_color(1.0, 0.0, 1.0);
    let mut c1 = window.add_cube(0.1, 0.1, 0.1);
    c1.set_color(1.0, 0.0, 0.0);

    vec![c_fixed, c0, c1]
}

fn main() {
    let root = create_joint_with_link_array();
    let arm = k::SerialChain::new_unchecked(k::Chain::from_root(root));

    let mut window = Window::new("k ui");
    window.set_light(Light::StickToCamera);
    let mut cubes = create_cubes(&mut window);
    let angles = vec![0.2, 0.2];
    arm.set_joint_positions(&angles).unwrap();
    let base_rot = Isometry3::from_parts(
        Translation3::new(0.0, 0.0, -0.6),
        UnitQuaternion::from_euler_angles(0.0, -1.57, -1.57),
    );
    arm.iter().next().unwrap().set_origin(
        base_rot
            * Isometry3::from_parts(
                Translation3::new(1.0, 0.0, 0.6),
                UnitQuaternion::from_euler_angles(0.5, 0.0, 0.0),
            ),
    );
    arm.update_transforms();
    let end = arm.find("shoulder_roll").unwrap();
    let mut target = end.world_transform().unwrap().clone();
    let mut c_t = window.add_sphere(0.05);
    c_t.set_color(1.0, 0.2, 0.2);
    let eye = Point3::new(0.0f32, 1.0, 3.0);
    let at = Point3::new(0.0f32, 0.2, 0.0);
    let mut arc_ball = ArcBall::new(eye, at);

    let solver = JacobianIKSolver::default();
    let _ = create_ground(&mut window);

    while window.render_with_camera(&mut arc_ball) {

        for mut event in window.events().iter() {
            match event.value {
                WindowEvent::Key(code, Action::Release, _) => {
                    match code {
                        
                        Key::R => {
                            // reset
                            arm.set_joint_positions(&angles).unwrap();
                            arm.update_transforms();
                            target = end.world_transform().unwrap().clone();
                        }
                        Key::Z => target.translation.vector[2] += 0.1,
                        Key::X => target.translation.vector[2] -= 0.1,
                        Key::Right => target.translation.vector[0] -= 0.1,
                        Key::Left => target.translation.vector[0] += 0.1,
                        Key::Up => target.translation.vector[1] += 0.1,
                        Key::Down => target.translation.vector[1] -= 0.1,
                        Key::S => {
                            // arm.set_joint_positions(&angles).unwrap();
                            println!("{:?}", arm.joint_positions());
                        },
                        _ => {}
                    }
                    event.inhibited = true // override the default keyboard handler
                }
                _ => {}
            }
        }
        let mut constraints = k::Constraints::default();
        constraints.rotation_x = false;
        solver.solve(&arm, &target)
            .unwrap_or_else(|err| {
                println!("Err: {}", err);
            });
        // solver
        //     .solve_with_constraints(&arm, &target, &constraints)
        //     
        c_t.set_local_transformation(target.clone());
        for (i, trans) in arm.update_transforms().iter().enumerate() {
            cubes[i].set_local_transformation(trans.clone());
        }
   

    }
}

error:

Err: ik precondition error "Input Dof=2, must be greater than 6"

Thanks!

@OTL
Copy link
Collaborator

OTL commented Nov 26, 2019

If you want to use inverse kinematics, you need 6 joints at least normally.
Do you need inverse kinematics?

There is a 4 DoF example, and it uses constraints settings to reduce the DoF to 4.
https://github.com/OTL/k/blob/master/examples/interactive_ik_scara.rs#L188-L190

What do you want to do?

@chitalian
Copy link
Author

Closing old issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants