diff --git a/docs/reference/motors/motor/set-brake-settle-time.md b/docs/reference/motors/motor/set-brake-settle-time.md new file mode 100644 index 00000000..95e7813e --- /dev/null +++ b/docs/reference/motors/motor/set-brake-settle-time.md @@ -0,0 +1,30 @@ +# set Brake Settle Time + +Set the time to wait after a motor stopped to allow it settle +when brake is enabled. Default is 10ms. + +```sig +motors.largeA.setBrakeSettleTime(200) +``` + +When a the motor is stopped and brake is applied, it can still wiggle for a little while. You can use the settle time to automatically way after stopping and let the robot settle. + +## Parameters + +* **time**: a [number](/types/number) value which represents the number of milliseconds to wait after braking. + +## Example + +Set the brake mode and the settle time to 500ms. Run the motor connected to port **A** for 2 seconds at a speed of `30` and stop after 2s. + +```blocks +motors.largeA.setBrake(true) +motors.largeA.setBrakeSettleTime(500) +motors.largeA.run(30) +pause(2000) +motors.largeA.stop() +``` + +## See also + +[stop](/reference/motors/motor/stop) \ No newline at end of file diff --git a/docs/reference/motors/motor/set-brake.md b/docs/reference/motors/motor/set-brake.md index 6c215ebd..6ea5adb6 100644 --- a/docs/reference/motors/motor/set-brake.md +++ b/docs/reference/motors/motor/set-brake.md @@ -1,28 +1,28 @@ # set Brake -Set the brake on the motor so it won't turn when it has no power. +Set the brake on the motor so it will brake when it finishes a brake command. ```sig motors.largeA.setBrake(false) ``` -When a the motor is stopped, it can still rotate if an external force is applied to it. This can happen, for example, if your're tanking your brick on a inclined surface and stop the motors. Gravity will push down on the brick and might cause it to start rolling again. You can prevent this movement by setting the brake. +When a the motor is stopped, it can still rotate if an external force is applied to it. This can happen, for example, if you're tanking your brick on a inclined surface and stop the motors. Gravity will push down on the brick and might cause it to start rolling again. You can prevent this movement by setting the brake. Also, you can use the brake to do simple skid steering for your brick. -## Paramters +## Parameters * **brake**: a [boolean](/types/boolean) value which is either `true` to set the brake on or `false` to set the brake off. ## Example -Run the motor connected to port **A** for 2 seconds at a speed of `30`. Stop and set the brake. +Run the motor connected to port **A** for 2 seconds at a speed of `30` and stop after 2s. ```blocks +motors.largeA.setBrake(true) motors.largeA.run(30) pause(2000) motors.largeA.stop() -motors.largeA.setBrake(true) ``` ## See also diff --git a/docs/reference/motors/motor/set-run-acceleration-ramp.md b/docs/reference/motors/motor/set-run-acceleration-ramp.md deleted file mode 100644 index 03e6a358..00000000 --- a/docs/reference/motors/motor/set-run-acceleration-ramp.md +++ /dev/null @@ -1,20 +0,0 @@ -# Set Run Acceleration Ramp - -```sig -motors.largeD.setRunAccelerationRamp(1, MoveUnit.Seconds) -``` - -## Examples - -```blocks -brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () { - motors.largeB.run(50, 6, MoveUnit.Rotations) -}) -brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () { - motors.largeC.run(50, 6, MoveUnit.Seconds) -}) -motors.largeB.setRunAccelerationRamp(360, MoveUnit.Degrees) -motors.largeB.setRunDecelerationRamp(360, MoveUnit.Degrees) -motors.largeC.setRunAccelerationRamp(2, MoveUnit.Seconds) -motors.largeC.setRunDecelerationRamp(2, MoveUnit.Seconds) -``` \ No newline at end of file diff --git a/docs/reference/motors/motor/set-run-deceleration-ramp.md b/docs/reference/motors/motor/set-run-deceleration-ramp.md deleted file mode 100644 index 55442bc5..00000000 --- a/docs/reference/motors/motor/set-run-deceleration-ramp.md +++ /dev/null @@ -1,20 +0,0 @@ -# Set Run Deceleration Ramp - -```sig -motors.largeD.setRunDecelerationRamp(1, MoveUnit.Seconds) -``` - -## Examples - -```blocks -brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () { - motors.largeB.run(50, 6, MoveUnit.Rotations) -}) -brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () { - motors.largeC.run(50, 6, MoveUnit.Seconds) -}) -motors.largeB.setRunAccelerationRamp(360, MoveUnit.Degrees) -motors.largeB.setRunDecelerationRamp(360, MoveUnit.Degrees) -motors.largeC.setRunAccelerationRamp(2, MoveUnit.Seconds) -motors.largeC.setRunDecelerationRamp(2, MoveUnit.Seconds) -``` \ No newline at end of file diff --git a/docs/reference/motors/motor/set-run-phase.md b/docs/reference/motors/motor/set-run-phase.md new file mode 100644 index 00000000..c783cd2c --- /dev/null +++ b/docs/reference/motors/motor/set-run-phase.md @@ -0,0 +1,26 @@ +# Set Run Phase + +Allows to specify an acceleration or deceleration phases for run commands. + +```sig +motors.largeD.setRunPhase(MovePhase.Acceleration, 1, MoveUnit.Seconds) +``` + +Once the run phase is specified on a motor (or pair of motors), +it will be automatically applied to [run](/reference/motors/run) commands. + +## Time vs Rotation + +The phases specified for time units (seconds, milliseconds) only apply to run with time +moves. Similarly, the phases specified for rotation units (# rotation, degrees) only +apply to run with rotation units. + +## Examples + +```blocks +motors.largeB.setRunPhase(MovePhase.Acceleration, 0.5, MoveUnit.Seconds) +motors.largeB.setRunPhase(MovePhase.Deceleration, 0.2, MoveUnit.Seconds) +forever(function () { + motors.largeB.run(50, 1, MoveUnit.Seconds) +}) +``` \ No newline at end of file diff --git a/libs/core/output.ts b/libs/core/output.ts index 6a1ec461..f4d080df 100644 --- a/libs/core/output.ts +++ b/libs/core/output.ts @@ -36,6 +36,13 @@ enum MoveUnit { MilliSeconds } +enum MovePhase { + //% block="acceleration" + Acceleration, + //% block="deceleration" + Deceleration +} + namespace motors { let pwmMM: MMap let motorMM: MMap @@ -226,6 +233,7 @@ namespace motors { //% weight=1 blockGap=8 //% group="Properties" //% millis.defl=200 millis.min=0 millis.max=500 + //% help=motors/motor/set-brake-settle-time setBrakeSettleTime(millis: number) { this.init(); // ensure in [0,500] @@ -388,50 +396,41 @@ namespace motors { * Specifies the amount of rotation or time for the acceleration * of run commands. */ - //% blockId=outputMotorsetRunAcceleration block="set %motor|run acceleration ramp to $value||$unit" + //% blockId=outputMotorsetRunRamp block="set %motor|run %ramp to $value||$unit" //% motor.fieldEditor="motors" //% weight=21 blockGap=8 //% group="Properties" - //% help=motors/motor/set-run-acceleration-ramp - setRunAccelerationRamp(value: number, unit: MoveUnit = MoveUnit.MilliSeconds) { + //% help=motors/motor/set-run-phase + setRunPhase(phase: MovePhase, value: number, unit: MoveUnit = MoveUnit.MilliSeconds) { + let temp: number; switch (unit) { case MoveUnit.Rotations: - this._accelerationSteps = Math.max(0, (value * 360) | 0); + temp = Math.max(0, (value * 360) | 0); + if (phase == MovePhase.Acceleration) + this._accelerationSteps = temp; + else + this._decelerationSteps = temp; break; case MoveUnit.Degrees: - this._accelerationSteps = Math.max(0, value | 0); + temp = Math.max(0, value | 0); + if (phase == MovePhase.Acceleration) + this._accelerationSteps = temp; + else + this._decelerationSteps = temp; break; case MoveUnit.Seconds: - this._accelerationTime = Math.max(0, (value * 1000) | 0); + temp = Math.max(0, (value * 1000) | 0); + if (phase == MovePhase.Acceleration) + this._accelerationTime = temp; + else + this._decelerationTime = temp; break; case MoveUnit.MilliSeconds: - this._accelerationTime = Math.max(0, value | 0); - break; - } - } - - /** - * Specifies the amount of rotation or time for the acceleration - * of run commands. - */ - //% blockId=outputMotorsetRunDeceleration block="set %motor|run deceleration ramp to $value||$unit" - //% motor.fieldEditor="motors" - //% weight=20 blockGap=8 - //% group="Properties" - //% help=motors/motor/set-run-deceleration-ramp - setRunDecelerationRamp(value: number, unit: MoveUnit = MoveUnit.MilliSeconds) { - switch (unit) { - case MoveUnit.Rotations: - this._decelerationSteps = Math.max(0, (value * 360) | 0); - break; - case MoveUnit.Degrees: - this._decelerationSteps = Math.max(0, value | 0); - break; - case MoveUnit.Seconds: - this._decelerationTime = Math.max(0, (value * 1000) | 0); - break; - case MoveUnit.MilliSeconds: - this._decelerationTime = Math.max(0, value | 0); + temp = Math.max(0, value | 0); + if (phase == MovePhase.Acceleration) + this._accelerationTime = temp; + else + this._decelerationTime = temp; break; } }