getArmCoordinates method

ArmCoordinates getArmCoordinates(
  1. ArmAngles angles,
  2. Size size
)

Performs forward kinematics to get the coordinates of each joint from the angles.

Implementation

ArmCoordinates getArmCoordinates(ArmAngles angles, Size size) {
  // See: https://www.desmos.com/calculator/i8grld5pdu
  const shoulderX = 0.0;
  const shoulderY = 0.0;
  final a2 = angles.shoulder - pi + angles.elbow;
  final a3 = a2 + angles.lift;
  final length = min(size.width / 4, size.height / 2);
  final elbowX = length * shoulderLength * cos(angles.shoulder);
  final elbowY = length * shoulderLength * sin(angles.shoulder);
  final wristX = length * elbowLength * cos(a2) + elbowX;
  final wristY = length * elbowLength * sin(a2) + elbowY;
  final gripperX = length * gripperLength * cos(a3) + wristX;
  final gripperY = length * gripperLength * sin(a3) + wristY;

  final shoulderJoint = Offset(toAbsolute(shoulderX) + size.width / 2, -toAbsolute(shoulderY) + size.height);
  final elbowJoint = Offset(toAbsolute(elbowX) + size.width / 2, -toAbsolute(elbowY) + size.height);
  final wristJoint = Offset(toAbsolute(wristX) + size.width / 2, -toAbsolute(wristY) + size.height);
  final gripLocation = Offset(toAbsolute(gripperX) + size.width / 2, -toAbsolute(gripperY) + size.height);
  return (
    shoulder: shoulderJoint,
    elbow: elbowJoint,
    wrist: wristJoint,
    fingers: gripLocation,
  );
}