Skip to content

Commit

Permalink
change diameter parameter to circumference
Browse files Browse the repository at this point in the history
  • Loading branch information
cubicap committed Jul 8, 2024
1 parent 709444a commit 5b2020c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
16 changes: 8 additions & 8 deletions main/espFeatures/motorFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ struct JSDCMotor {
std::optional<DCMotor> motor;
std::optional<PromiseFunctions> pendingPromise;
int encTicks;
double diameter;
double circumference;

template<typename Feature>
JSDCMotor(MotorPins<Feature> pins, int regP, LedcConfig<Feature> ledcConf, int encTicks, double diameter):
JSDCMotor(MotorPins<Feature> pins, int regP, LedcConfig<Feature> 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();
}
Expand All @@ -115,12 +115,12 @@ class MotorProtoBuilder : public jac::ProtoBuilder::Opaque<JSDCMotor>, public ja
auto options = args[0].to<jac::Object>();
auto pins = options.get("pins").to<MotorPins<Feature>>();
auto encTicks = options.get("encTicks").to<int>();
auto diameter = options.get("diameter").to<double>();
auto circumference = options.get("circumference").to<double>();

auto ledcConf = options.get("ledc").to<LedcConfig<Feature>>();

auto machine = reinterpret_cast<Feature*>(JS_GetContextOpaque(ctx));
std::unique_ptr<JSDCMotor> mot = std::make_unique<JSDCMotor>(pins, 50, ledcConf, encTicks, diameter);
std::unique_ptr<JSDCMotor> mot = std::make_unique<JSDCMotor>(pins, 50, ledcConf, encTicks, circumference);
mot->motor->onTarget([mot = mot.get(), machine]() {
static constexpr auto resolve = +[](void* data) {
JSDCMotor* mot = reinterpret_cast<JSDCMotor*>(data);
Expand Down Expand Up @@ -149,7 +149,7 @@ class MotorProtoBuilder : public jac::ProtoBuilder::Opaque<JSDCMotor>, 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);

Expand All @@ -171,7 +171,7 @@ class MotorProtoBuilder : public jac::ProtoBuilder::Opaque<JSDCMotor>, public ja

if (duration.hasProperty("distance")) {
double distance = duration.get("distance").to<double>();
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")) {
Expand Down Expand Up @@ -214,7 +214,7 @@ class MotorProtoBuilder : public jac::ProtoBuilder::Opaque<JSDCMotor>, public ja
}

int64_t ticks = motor.motor->getPosition();
return jac::Value::from(ctx, static_cast<double>(ticks) * motor.diameter * std::numbers::pi / motor.encTicks);
return jac::Value::from(ctx, static_cast<double>(ticks) * motor.circumference / motor.encTicks);
}), jac::PropFlags::Enumerable);

proto.defineProperty("close", ff.newFunctionThis([](jac::ContextRef ctx, jac::ValueWeak thisVal) {
Expand Down
10 changes: 5 additions & 5 deletions ts-examples/@types/motor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ 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;

/**
* 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<void>;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions ts-examples/src/motor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -32,7 +32,7 @@ let right = new Motor({
channelB: 3
},
encTicks: 406, // ticks per revolution
diameter: 34 // mm
circumference: 34 * Math.PI // mm
});

setInterval(() => {
Expand Down

0 comments on commit 5b2020c

Please sign in to comment.