diff --git a/main/espFeatures/motorFeature.h b/main/espFeatures/motorFeature.h index 6a9e393..8ef95f3 100644 --- a/main/espFeatures/motorFeature.h +++ b/main/espFeatures/motorFeature.h @@ -86,12 +86,12 @@ struct JSDCMotor { std::optional motor; std::optional pendingPromise; int encTicks; - double diameter; + double circumference; template - JSDCMotor(MotorPins pins, int regP, LedcConfig ledcConf, int encTicks, double diameter): + JSDCMotor(MotorPins pins, int regP, LedcConfig ledcConf, int encTicks, double circumference): motor(std::in_place, pins.motA, pins.motB, pins.encA, pins.encB, regP, ledcConf.timer, ledcConf.channelA, ledcConf.channelB), - encTicks(encTicks), diameter(diameter) + encTicks(encTicks), circumference(circumference) { motor->startTicker(); } @@ -115,12 +115,12 @@ class MotorProtoBuilder : public jac::ProtoBuilder::Opaque, public ja auto options = args[0].to(); auto pins = options.get("pins").to>(); auto encTicks = options.get("encTicks").to(); - auto diameter = options.get("diameter").to(); + auto circumference = options.get("circumference").to(); auto ledcConf = options.get("ledc").to>(); auto machine = reinterpret_cast(JS_GetContextOpaque(ctx)); - std::unique_ptr mot = std::make_unique(pins, 50, ledcConf, encTicks, diameter); + std::unique_ptr mot = std::make_unique(pins, 50, ledcConf, encTicks, circumference); mot->motor->onTarget([mot = mot.get(), machine]() { static constexpr auto resolve = +[](void* data) { JSDCMotor* mot = reinterpret_cast(data); @@ -149,7 +149,7 @@ class MotorProtoBuilder : public jac::ProtoBuilder::Opaque, public ja throw jac::Exception::create(jac::Exception::Type::InternalError, "Motor is closed"); } - int ticksPerSec = motor.encTicks * speed / (motor.diameter * std::numbers::pi); + int ticksPerSec = motor.encTicks * speed / (motor.circumference); motor.motor->setSpeed(ticksPerSec); }), jac::PropFlags::Enumerable); @@ -171,7 +171,7 @@ class MotorProtoBuilder : public jac::ProtoBuilder::Opaque, public ja if (duration.hasProperty("distance")) { double distance = duration.get("distance").to(); - int64_t ticks = motor.encTicks * distance / (motor.diameter * std::numbers::pi); + int64_t ticks = motor.encTicks * distance / (motor.circumference); motor.motor->moveDistance(ticks); } else if (duration.hasProperty("time")) { @@ -214,7 +214,7 @@ class MotorProtoBuilder : public jac::ProtoBuilder::Opaque, public ja } int64_t ticks = motor.motor->getPosition(); - return jac::Value::from(ctx, static_cast(ticks) * motor.diameter * std::numbers::pi / motor.encTicks); + return jac::Value::from(ctx, static_cast(ticks) * motor.circumference / motor.encTicks); }), jac::PropFlags::Enumerable); proto.defineProperty("close", ff.newFunctionThis([](jac::ContextRef ctx, jac::ValueWeak thisVal) { diff --git a/ts-examples/@types/motor.d.ts b/ts-examples/@types/motor.d.ts index 2ec3139..c629c45 100644 --- a/ts-examples/@types/motor.d.ts +++ b/ts-examples/@types/motor.d.ts @@ -22,14 +22,14 @@ declare module "motor" { /** * Construc a new Motor instance * @param options Motor configuration - * @note Units used in the diameter parameter determines the units used in the other methods + * @note Units used in the circumference parameter determines the units used in the other methods */ - constructor(options: { pins: MotorPins, ledc: LedcConfig, encTicks: number, diameter: number }); + constructor(options: { pins: MotorPins, ledc: LedcConfig, encTicks: number, circumference: number }); /** * Set the speed of the motor * @param speed Speed in units per second - * @note The units are the same as used in the diameter parameter + * @note The units are the same as used in the circumference parameter */ setSpeed(speed: number): void; // setRamp(ramp: number): void; @@ -37,7 +37,7 @@ declare module "motor" { /** * Move the motor * @param duration Duration of the movement - * @note The units are the same as used in the diameter parameter + * @note The units are the same as used in the circumference parameter * @note If the duration is not provided, the motor will move indefinitely */ move(duration?: MoveDuration): Promise; @@ -50,7 +50,7 @@ declare module "motor" { /** * Get the position of the motor - * @note The units are the same as used in the diameter parameter + * @note The units are the same as used in the circumference parameter * @returns The position of the motor */ getPosition(): number; diff --git a/ts-examples/src/motor.ts b/ts-examples/src/motor.ts index 385b97a..bca6009 100644 --- a/ts-examples/src/motor.ts +++ b/ts-examples/src/motor.ts @@ -16,7 +16,7 @@ let left = new Motor({ channelB: 1 }, encTicks: 406, // ticks per revolution - diameter: 34 // mm + circumference: 34 * Math.PI // mm }); let right = new Motor({ @@ -32,7 +32,7 @@ let right = new Motor({ channelB: 3 }, encTicks: 406, // ticks per revolution - diameter: 34 // mm + circumference: 34 * Math.PI // mm }); setInterval(() => {