<commit_before>#ifdef CMAKE
#include <cstring>
#include <cmath>
#endif
#include "Arduino.h"
#include "Machine.h"
#include "AnalogRead.h"
#include "version.h"
#include "IDuino.h"
#include "DeltaCalculator.h"

using namespace firestep;
using namespace ph5;

PH5TYPE DeltaCalculator::sqrt3 = sqrt(3.0);
PH5TYPE DeltaCalculator::sin120 = sqrt3 / 2.0;
PH5TYPE DeltaCalculator::cos120 = -0.5;
PH5TYPE DeltaCalculator::tan60 = sqrt3;
PH5TYPE DeltaCalculator::sin30 = 0.5;
PH5TYPE DeltaCalculator::tan30 = 1 / sqrt3;
PH5TYPE DeltaCalculator::tan30_half = tan30 / 2.0;
PH5TYPE DeltaCalculator::pi = PI;
PH5TYPE DeltaCalculator::dtr = pi / 180.0;

StepCoord DeltaCalculator::roundStep(PH5TYPE value) {
    return (StepCoord)(value + (value < 0 ? -0.5 : +0.5));
}

DeltaCalculator::DeltaCalculator()
    : e(131.636), // effector equilateral triangle side (mm)
      f(190.526), // base equilateral triangle side (mm)
      re(270.000), // effector arm length (mm)
      rf(90.000), // base arm length (mm)
      acr(24.15), // arm clearance radius (mm)
      steps360(200),
      microsteps(16),
      spAngle(FPD_SPE_ANGLE), // sliced pulley critical angle
      spRatio(0), // sliced pulley angular error per degree beyond critical angle (default is no SPE)
      dz(0)
{
    setGearRatio(FPD_GEAR_RATIO);
    homeAngle = getDefaultHomeAngle();
}

void DeltaCalculator::useEffectorOrigin() {
    dz = 0;
    XYZ3D xyz = calcXYZ(Angle3D());
    ASSERT(xyz.isValid());

    TESTCOUT1("xyz:", xyz.isValid());
    dz = -xyz.z; // use effector origin instead of base origin at zero degrees
    TESTCOUT3("DeltaCalculator.dx:", xyz.x, " dy:", xyz.y, " dz:", dz);
    TESTCOUT2("DeltaCalculator.pulseDegrees:", getDegreesPerPulse(), " minZ:", getMinZ());
}

PH5TYPE DeltaCalculator::getMinDegrees() {
    return getDefaultHomeAngle();
}

void DeltaCalculator::setMicrosteps(int16_t value) {
    PH5TYPE scale = ((PH5TYPE)microsteps)/value;
    microsteps = value;
    degreesPerPulse[0] *= scale;
    degreesPerPulse[1] *= scale;
    degreesPerPulse[2] *= scale;
}

void DeltaCalculator::setSteps360(int16_t value) {
    PH5TYPE scale = ((PH5TYPE)steps360)/value;
    steps360 = value;
    degreesPerPulse[0] *= scale;
    degreesPerPulse[1] *= scale;
    degreesPerPulse[2] *= scale;
}

void DeltaCalculator::setGearRatio(PH5TYPE value, DeltaAxis axis) {
    setDegreesPerPulse(360/(microsteps*steps360*value), axis);
	// NOTE: setting the gear ratio will affect the home pulses,
	// and client applications need to be aware of this. This
	// method should NOT set the home angle, which is an extrinsic
	// input to FPD kinematics.
}

void DeltaCalculator::setDegreesPerPulse(PH5TYPE value, DeltaAxis axis) {
    StepCoord pulses = getHomePulses();
    if (axis == DELTA_AXIS_ALL) {
        degreesPerPulse[0] =
            degreesPerPulse[1] =
                degreesPerPulse[2] = value;
    } else {
        degreesPerPulse[axis] = value;
    }
	// NOTE: setting the degrees per pulse ratio will affect the home pulses,
	// and client applications need to be aware of this. This
	// method should NOT set the home angle, which is an extrinsic
	// input to FPD kinematics.
}

PH5TYPE DeltaCalculator::getDefaultHomeAngle() {
    PH5TYPE armsParallel = 180*asin((f-e)/(re*sqrt3))/pi - 90;
    PH5TYPE clearanceAngle = 180*asin(acr/rf)/pi;
    PH5TYPE rawDegrees =  armsParallel + clearanceAngle;<commit_msg>home-pulses<commit_after>#ifdef CMAKE
#include <cstring>
#include <cmath>
#endif
#include "Arduino.h"
#include "Machine.h"
#include "AnalogRead.h"
#include "version.h"
#include "IDuino.h"
#include "DeltaCalculator.h"

using namespace firestep;
using namespace ph5;

PH5TYPE DeltaCalculator::sqrt3 = sqrt(3.0);
PH5TYPE DeltaCalculator::sin120 = sqrt3 / 2.0;
PH5TYPE DeltaCalculator::cos120 = -0.5;
PH5TYPE DeltaCalculator::tan60 = sqrt3;
PH5TYPE DeltaCalculator::sin30 = 0.5;
PH5TYPE DeltaCalculator::tan30 = 1 / sqrt3;
PH5TYPE DeltaCalculator::tan30_half = tan30 / 2.0;
PH5TYPE DeltaCalculator::pi = PI;
PH5TYPE DeltaCalculator::dtr = pi / 180.0;

StepCoord DeltaCalculator::roundStep(PH5TYPE value) {
    return (StepCoord)(value + (value < 0 ? -0.5 : +0.5));
}

DeltaCalculator::DeltaCalculator()
    : e(131.636), // effector equilateral triangle side (mm)
      f(190.526), // base equilateral triangle side (mm)
      re(270.000), // effector arm length (mm)
      rf(90.000), // base arm length (mm)
      acr(24.15), // arm clearance radius (mm)
      steps360(200),
      microsteps(16),
      spAngle(FPD_SPE_ANGLE), // sliced pulley critical angle
      spRatio(0), // sliced pulley angular error per degree beyond critical angle (default is no SPE)
      dz(0)
{
    setGearRatio(FPD_GEAR_RATIO);
    homeAngle = getDefaultHomeAngle();
}

void DeltaCalculator::useEffectorOrigin() {
    dz = 0;
    XYZ3D xyz = calcXYZ(Angle3D());
    ASSERT(xyz.isValid());

    TESTCOUT1("xyz:", xyz.isValid());
    dz = -xyz.z; // use effector origin instead of base origin at zero degrees
    TESTCOUT3("DeltaCalculator.dx:", xyz.x, " dy:", xyz.y, " dz:", dz);
    TESTCOUT2("DeltaCalculator.pulseDegrees:", getDegreesPerPulse(), " minZ:", getMinZ());
}

PH5TYPE DeltaCalculator::getMinDegrees() {
    return getDefaultHomeAngle();
}

void DeltaCalculator::setMicrosteps(int16_t value) {
    PH5TYPE scale = ((PH5TYPE)microsteps)/value;
    microsteps = value;
    degreesPerPulse[0] *= scale;
    degreesPerPulse[1] *= scale;
    degreesPerPulse[2] *= scale;
}

void DeltaCalculator::setSteps360(int16_t value) {
    PH5TYPE scale = ((PH5TYPE)steps360)/value;
    steps360 = value;
    degreesPerPulse[0] *= scale;
    degreesPerPulse[1] *= scale;
    degreesPerPulse[2] *= scale;
}

void DeltaCalculator::setGearRatio(PH5TYPE value, DeltaAxis axis) {
    setDegreesPerPulse(360/(microsteps*steps360*value), axis);
	// NOTE: setting the gear ratio will affect the home pulses,
	// and client applications need to be aware of this. This
	// method should NOT set the home angle, which is an extrinsic
	// input to FPD kinematics.
}

void DeltaCalculator::setDegreesPerPulse(PH5TYPE value, DeltaAxis axis) {
    if (axis == DELTA_AXIS_ALL) {
        degreesPerPulse[0] =
            degreesPerPulse[1] =
                degreesPerPulse[2] = value;
    } else {
        degreesPerPulse[axis] = value;
    }
	// NOTE: setting the degrees per pulse ratio will affect the home pulses,
	// and client applications need to be aware of this. This
	// method should NOT set the home angle, which is an extrinsic
	// input to FPD kinematics.
}

PH5TYPE DeltaCalculator::getDefaultHomeAngle() {
    PH5TYPE armsParallel = 180*asin((f-e)/(re*sqrt3))/pi - 90;
    PH5TYPE clearanceAngle = 180*asin(acr/rf)/pi;
    PH5TYPE rawDegrees =  armsParallel + clearanceAngle;
{'fraction_non_alphanumeric': 0.12618623962040332, 'fraction_numerical': 0.042111506524317915, 'mean_word_length': 3.901889534883721, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7192625', 'n_tokens_mistral': 2519, 'n_tokens_neox': 2283, 'n_words': 580}