Inverse Kinematics using JavaFX for Robotics
As in many of my previous posts, you might have noticed that I've been building a robot. Yes before you say 'but you're taking ages to make it!', I know, there are many excuses I could use but I won't bother, I wouldn't be fooling anyone, I've just been putting Inverse Kinematics off because it's a beast that not many people can tame.
So (with the excuses out of the way) you can easily implement a basic setup in Java that works well enough for almost any implementation. See Mikes blog post about an Inverse Kinematics library he developed. If you copy the Bone and Skeleton .java files from the GitHub page and put them into your Java application, you'll be able to set up a basic bone structure for almost any type of robot.
The robot I'm developing happens to be a biped as seen on its project page and this means that I have to build two legs and control them as such. See m leg definitions below:
// Skeleton
public Skeleton skeleton;
// Torso
public final Bone torso = new Bone(80, 90);
// Left leg bones
Bone leftUpperLeg = new Bone(6.5d, 90);
Bone leftLowerLeg = new Bone(7.4d, 90);
Bone leftFoot = new Bone(7.4d, 90);
// Right leg bones
Bone rightUpperLeg = new Bone(6.5d, 90);
Bone rightLowerLeg = new Bone(7.4d, 90);
Bone rightFoot = new Bone(7.4d, 90);
// Definition of lower legs
leftLowerLeg.getChildren().add(leftFoot);
rightLowerLeg.getChildren().add(rightFoot);
leftUpperLeg.getChildren().add(leftLowerLeg);
rightUpperLeg.getChildren().add(rightLowerLeg);
// Connect legs to torso
torso.getChildren().add(leftUpperLeg);
torso.getChildren().add(rightUpperLeg);
// Creating the skeleton
skeleton = new Skeleton();
skeleton.setTranslateX(0);
skeleton.setTranslateY(0);
torso.setSkeleton(skeleton);
From then, it's simply a matter of getting the angle from the bone and setting that to the servo. Whilst I won't go into depths about the servo implementation, I will say that I have a function that converts an angle to a value within the servo range, so I can say setServoAngle(90) and it will set the servo to 1600. You can get the angle of the bone using the getRotate method, example implementation below:
leftHipX.setAngle(leftUpperLeg.getRotate(), 100);
The script mentioned above will also allow you to output a visual, something like the image below. Since my robot is only a pair of legs at the moment, it looks a bit odd, but it's useful, trust me.
If you have any questions/suggestions, please do post a comment and I'll reply as soon as I can!
Published at 7 Jul 2015, 22:23 PM
Tags: Java,Inverse Kinematics,Robot