diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 3b1aa9e7..b2bdb5dc 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -63,6 +63,9 @@ "motors.Motor.speed": "Gets motor actual speed.", "motors.Motor.stop": "Stops the motor", "motors.Motor.tachoCount": "Gets motor tacho count.", + "motors.setSyncSpeed": "Synchronizes this motor with another motor.", + "motors.setSyncSpeed|param|speed": "the power applied to the motor, eg: 50", + "motors.setSyncSpeed|param|turnRatio": "the ratio of the master power applied to this motor, eg: 100", "motors.stopAllMotors": "Stops all motors", "output.createBuffer": "Create a new zero-initialized buffer.", "output.createBuffer|param|size": "number of bytes in the buffer", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 132ddeca..f3e8cf34 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -14,6 +14,7 @@ "LightsPattern.Red|block": "Red", "Output.ALL|block": "All", "Output.A|block": "A", + "Output.BC|block": "B+C", "Output.B|block": "B", "Output.C|block": "C", "Output.D|block": "D", @@ -51,6 +52,7 @@ "motors.mediumMotorB|block": "medium B", "motors.mediumMotorC|block": "medium C", "motors.mediumMotorD|block": "medium D", + "motors.setSyncSpeed|block": "set sync speed B+C with %turnRatio|turn ratio at %speed|% speed", "motors.stopAllMotors|block": "stop all `icons.motorLarge`", "motors|block": "motors", "output|block": "output", diff --git a/libs/core/output.ts b/libs/core/output.ts index fe605900..0ffdb8e2 100644 --- a/libs/core/output.ts +++ b/libs/core/output.ts @@ -7,6 +7,8 @@ enum Output { C = 0x04, //% block="D" D = 0x08, + //% block="B+C" + BC = 0x06, //% block="All" ALL = 0x0f } @@ -75,7 +77,7 @@ namespace motors { //% fixedInstances export class Motor extends control.Component { - public port: Output; + private port: Output; private large: boolean; private brake: boolean; @@ -102,8 +104,7 @@ namespace motors { b.setNumber(NumberFormat.Int8LE, 2, speed) writePWM(b) if (speed) { - const b = mkCmd(this.port, DAL.opOutputStart, 0) - writePWM(b); + start(this.port); } else { this.stop(); } @@ -241,6 +242,28 @@ namespace motors { //% whenUsed fixedInstance block="medium D" export const mediumMotorD = new Motor(Output.D, false); + + /** + * Synchronizes this motor with another motor. + * @param motor the controlled motor, eg: motors.largeB + * @param other the motor that will control this motor, eg: motors.largeC + * @param turnRatio the ratio of the master power applied to this motor, eg: 100 + * @param speed the power applied to the motor, eg: 50 + */ + //% blockId=motorSync block="set sync speed B+C with %turnRatio|turn ratio at %speed|% speed" + //% turnRatio.min=-200 turnRatio.max=200 + //% speed.min=-100 speed.max=100 + export function setSyncSpeed(turnRatio: number, speed: number) { + syncMotors(Output.BC, { + useSteps: true, + speed: speed, + turnRatio: turnRatio, + stepsOrTime: 0, + useBrake: false + }) + start(Output.BC); + } + function reset(out: Output) { let b = mkCmd(out, DAL.opOutputReset, 0) writePWM(b) @@ -303,6 +326,11 @@ namespace motors { useBrake?: boolean; } + function start(out: Output) { + const b = mkCmd(out, DAL.opOutputStart, 0) + writePWM(b); + } + function step(out: Output, opts: StepOptions) { let op = opts.useSteps ? DAL.opOutputStepSpeed : DAL.opOutputTimeSpeed let speed = opts.speed