Skip to content

Commit

Permalink
documenting
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Dec 21, 2023
1 parent 47f9b5c commit 4c40314
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import javax.swing.*;
import javax.swing.text.NumberFormatter;
import javax.vecmath.Matrix3d;
import javax.vecmath.Matrix4d;
import java.awt.*;
import java.text.NumberFormat;
import java.util.List;

/**
* {@link HingeJoint} is a joint that can rotate around the local Z axis.
* <p>a {@link HingeJoint} is a joint that can rotate around the local Z axis.</p>
* <p>a {@link HingeJoint} should be attached to a child {@link Pose} referenced as the axle. In this way the axle's
* parent {@link Pose} can be thought of as the initial pose at zero degrees. This helps prevent drift over time.</p>
*/
public class HingeJoint extends Node {
private double angle = 0; // degrees
Expand Down Expand Up @@ -101,11 +101,7 @@ public void update(double dt) {

if(axle!=null) {
// set the axle's location in space.
Matrix3d rotZ = new Matrix3d();
rotZ.rotZ(Math.toRadians(angle));
Matrix4d local = new Matrix4d(axle.getLocal());
local.set(rotZ);
axle.setLocal(local);
axle.getLocal().rotZ(Math.toRadians(angle));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/marginallyclever/ro3/node/nodes/Motor.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import java.util.List;

/**
* a {@link Motor} is a {@link Node} that can be attached to a {@link HingeJoint}.
* A {@link Motor} is a {@link Node} that can be attached to a {@link HingeJoint}. It will then drive the joint
* according to the motor's settings.
*/
public class Motor extends Node {
private HingeJoint hinge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public void moveEndEffectorInCartesianDirection(double[] cartesianVelocity) {
double sum = sumCartesianVelocityComponents(cartesianVelocity);
if(sum<0.0001) return;
if(sum <= 1) {
moveEndEffectorInCartesianDirectionALittle(cartesianVelocity);
setMotorVelocitiesFromCartesianVelocity(cartesianVelocity);
return;
}

Expand All @@ -382,45 +382,21 @@ public void moveEndEffectorInCartesianDirection(double[] cartesianVelocity) {
double[] cartesianVelocityUnit = Arrays.stream(cartesianVelocity)
.map(v -> v / total)
.toArray();
/*
// option 1, set joints directly.
for (int i = 0; i < total; ++i) {
moveEndEffectorInCartesianDirectionALittle(cartesianVelocityUnit);
}
*/
// option 2, set joint velocities.
setJointVelocitiesFromCartesianVelocity(cartesianVelocityUnit);
// set motor velocities.
setMotorVelocitiesFromCartesianVelocity(cartesianVelocityUnit);
}

/**
* <p>Attempts to move the robot arm such that the end effector travels in the direction of the cartesian
* velocity.</p>
* <p>This method will only move the robot arm a small amount. It is intended to be called repeatedly to move the
* arm in an iterative fashion.</p>
* <p>Attempts to move the robot arm such that the end effector travels in the cartesian direction. This is
* achieved by setting the velocity of the motors.</p>
* @param cartesianVelocity three linear forces (mm) and three angular forces (degrees).
* @throws RuntimeException if the robot cannot be moved in the direction of the cartesian force.
*/
private void moveEndEffectorInCartesianDirectionALittle(double[] cartesianVelocity) {
private void setMotorVelocitiesFromCartesianVelocity(double[] cartesianVelocity) {
ApproximateJacobian aj = getJacobian();
try {
double[] jointVelocity = aj.getJointForceFromCartesianForce(cartesianVelocity); // uses inverse jacobian
// do not make moves for impossible velocities
double[] jointVelocity = aj.getJointFromCartesian(cartesianVelocity); // uses inverse jacobian
if(impossibleVelocity(jointVelocity)) return; // TODO: throw exception instead?

double[] angles = getAllJointAngles(); // # dof long
for (int i = 0; i < angles.length; ++i) {
angles[i] += jointVelocity[i];
}
setAllJointAngles(angles);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private void setJointVelocitiesFromCartesianVelocity(double[] cartesianVelocity) {
ApproximateJacobian aj = getJacobian();
try {
double[] jointVelocity = aj.getJointForceFromCartesianForce(cartesianVelocity); // uses inverse jacobian
setAllJointVelocities(jointVelocity);
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/marginallyclever/ro3/render/Viewport.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.vecmath.Vector3d;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -187,9 +188,10 @@ private void renderAllPasses() {
}

private void updateAllNodes(double dt) {
// option 1, recursively
Registry.getScene().update(dt);
/*
// linear way, which is less recursive.
// option 2, in a linear way.
List<Node> toScan = new ArrayList<>(Registry.getScene().getChildren());
while(!toScan.isEmpty()) {
Node node = toScan.remove(0);
Expand Down Expand Up @@ -229,6 +231,12 @@ public void mouseDragged(MouseEvent e) {
}
}

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
super.mouseWheelMoved(e);
int dz = e.getWheelRotation();
}

private void panTiltCamera(int dx, int dy) {
Matrix4d local = camera.getLocal();
double [] panTiltAngles = getPanTiltFromMatrix(local);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ protected ApproximateJacobian(int DOF) {
jacobian = new double[6][DOF];
}

/**
* Use the jacobian to get the cartesian velocity from the joint velocity.
* @param jointForce joint velocity in degrees.
* @return 6 doubles containing the XYZ translation and UVW rotation forces on the end effector.
*/
public double[] getCartesianForceFromJointForce(final double[] jointForce) {
// vector-matrix multiplication (y = x^T A)
double[] cartesianVelocity = new double[DOF];
for (int j = 0; j < DOF; ++j) {
double sum = 0;
for (int k = 0; k < 6; ++k) {
sum += jacobian[k][j] * Math.toRadians(jointForce[j]);
}
cartesianVelocity[j] = sum;
}
return cartesianVelocity;
}

/**
* See <a href="https://stackoverflow.com/a/53028167/1159440">5 DOF Inverse kinematics for Jacobian Matrices</a>.
* @return the inverse Jacobian matrix.
Expand Down Expand Up @@ -94,10 +76,11 @@ private double[][] getInverseDampedLeastSquares(double lambda) {
/**
* Use the Jacobian to get the joint velocity from the cartesian velocity.
* @param cartesianVelocity 6 doubles - the XYZ translation and UVW rotation forces on the end effector.
* @return jointVelocity joint velocity in degrees. Will be filled with the new velocity.
* The rotation component is in radians.
* @return joint velocity in degrees. Will be filled with the new velocity.
* @throws Exception if joint velocities have NaN values
*/
public double[] getJointForceFromCartesianForce(final double[] cartesianVelocity) throws Exception {
public double[] getJointFromCartesian(final double[] cartesianVelocity) throws Exception {
double[][] inverseJacobian = getInverseJacobian();
double[] jointVelocity = new double[DOF];

Expand All @@ -116,6 +99,25 @@ public double[] getJointForceFromCartesianForce(final double[] cartesianVelocity
return jointVelocity;
}

/**
* Use the jacobian to convert joint velocity to cartesian velocity.
* @param joint joint velocity in degrees.
* @return 6 doubles containing the XYZ translation and UVW rotation forces on the end effector.
* The rotation component is in radians.
*/
public double[] getCartesianFromJoint(final double[] joint) {
// vector-matrix multiplication (y = x^T A)
double[] cartesianVelocity = new double[DOF];
for (int j = 0; j < DOF; ++j) {
double sum = 0;
for (int k = 0; k < 6; ++k) {
sum += jacobian[k][j] * Math.toRadians(joint[j]);
}
cartesianVelocity[j] = sum;
}
return cartesianVelocity;
}

public double[][] getJacobian() {
return jacobian;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private void applySmallCartesianForceToEndEffector(RobotComponent robotComponent
ApproximateJacobian aj = new ApproximateJacobianFiniteDifferences(robotComponent);
//ApproximateJacobian aj = new ApproximateJacobianScrewTheory(robotComponent);
try {
double[] jointVelocity = aj.getJointForceFromCartesianForce(cartesianVelocity); // uses inverse jacobian
double[] jointVelocity = aj.getJointFromCartesian(cartesianVelocity); // uses inverse jacobian
// do not make moves for impossible velocities
if(impossibleVelocity(robotComponent,jointVelocity)) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public void compare() throws Exception {
}

double [] v = new double[] {1,2,3,4,5,6};
double [] vFinite = finite.getJointForceFromCartesianForce(v);
double [] vScrew = screw.getJointForceFromCartesianForce(v);
double [] vFinite = finite.getJointFromCartesian(v);
double [] vScrew = screw.getJointFromCartesian(v);
System.out.println(Arrays.toString(vFinite));
System.out.println(Arrays.toString(vScrew));
for(int i=0;i<vFinite.length;++i) {
Expand Down

0 comments on commit 4c40314

Please sign in to comment.