From 933ec16a6bff08b0f9bb9652c764be9143105fb8 Mon Sep 17 00:00:00 2001 From: noqman <140384051+noqman@users.noreply.github.com> Date: Tue, 29 Oct 2024 12:52:01 +0800 Subject: [PATCH] Added a block to set motors speed individually and revised acceleration value --- motor.ts | 63 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/motor.ts b/motor.ts index 8ff493c..96cc10c 100644 --- a/motor.ts +++ b/motor.ts @@ -66,37 +66,37 @@ export enum MotorChannel { /** * Run the motor forward or backward. - * (Speed = 0-255) (Acceleration = 0-9). + * (Speed = 0-255) (Acceleration = 1-9). * @param motor Motor channel. * @param direction Motor direction. * @param speed Motor speed (PWM). eg: 128 - * @param accelaration Acceleration factor (0-9). eg: 0 + * @param acceleration Acceleration factor (1-9). eg: 4 */ //% group="DC Motors" //% weight=99 //% blockGap=8 //% blockId=sumobit_motor_run //% accel.fieldOptions.label="Acceleration Factor" - //% block="run %motor motor %direction at %speed speed || with %accelaration acceleration factor" + //% block="run %motor motor %direction at %speed speed || with %acceleration acceleration factor" //% inlineInputMode=inline //% speed.min=0 speed.max=255 - //% accelaration.min=0 accelaration.max=9 + //% acceleration.min=1 acceleration.max=9 //% expandableArgumentMode="enabled" - export function runMotor(motor: MotorChannel, direction: MotorDirection, speed: number, accelaration: number = 0): void { + export function runMotor(motor: MotorChannel, direction: MotorDirection, speed: number, acceleration: number = 4): void { speed = sumobit.limit(speed, 0, 255); - accelaration = sumobit.limit(accelaration, 0, 9); + acceleration = sumobit.limit(acceleration, 1, 9); switch (motor) { case MotorChannel.MR: if (direction == MotorDirection.Forward) { sumobit.i2cWrite(REG_ADD_PWM1, speed); sumobit.i2cWrite(REG_ADD_DIR1, 0); - sumobit.i2cWrite(REG_ADD_ACCEL1, accelaration); + sumobit.i2cWrite(REG_ADD_ACCEL1, acceleration); } else { sumobit.i2cWrite(REG_ADD_PWM1, speed); sumobit.i2cWrite(REG_ADD_DIR1, 1); - sumobit.i2cWrite(REG_ADD_ACCEL1, accelaration); + sumobit.i2cWrite(REG_ADD_ACCEL1, acceleration); } break; @@ -104,12 +104,12 @@ export enum MotorChannel { if (direction == MotorDirection.Forward) { sumobit.i2cWrite(REG_ADD_PWM2, speed); sumobit.i2cWrite(REG_ADD_DIR2, 0); - sumobit.i2cWrite(REG_ADD_ACCEL2, accelaration); + sumobit.i2cWrite(REG_ADD_ACCEL2, acceleration); } else { sumobit.i2cWrite(REG_ADD_PWM2, speed); sumobit.i2cWrite(REG_ADD_DIR2, 1); - sumobit.i2cWrite(REG_ADD_ACCEL2, accelaration); + sumobit.i2cWrite(REG_ADD_ACCEL2, acceleration); } break; @@ -117,22 +117,57 @@ export enum MotorChannel { if (direction == MotorDirection.Forward) { sumobit.i2cWrite(REG_ADD_PWM1, speed); sumobit.i2cWrite(REG_ADD_DIR1, 0); - sumobit.i2cWrite(REG_ADD_ACCEL1, accelaration); + sumobit.i2cWrite(REG_ADD_ACCEL1, acceleration); sumobit.i2cWrite(REG_ADD_PWM2, speed); sumobit.i2cWrite(REG_ADD_DIR2, 0); - sumobit.i2cWrite(REG_ADD_ACCEL2, accelaration); + sumobit.i2cWrite(REG_ADD_ACCEL2, acceleration); } else { sumobit.i2cWrite(REG_ADD_PWM1, speed); sumobit.i2cWrite(REG_ADD_DIR1, 1); - sumobit.i2cWrite(REG_ADD_ACCEL1, accelaration); + sumobit.i2cWrite(REG_ADD_ACCEL1, acceleration); sumobit.i2cWrite(REG_ADD_PWM2, speed); sumobit.i2cWrite(REG_ADD_DIR2, 1); - sumobit.i2cWrite(REG_ADD_ACCEL2, accelaration); + sumobit.i2cWrite(REG_ADD_ACCEL2, acceleration); } break; } } + /** + * Set individual motors speed (speed = -255 to 255, negative value = reverse). + * @param leftSpeed Speed for left motor. eg: 0 + * @param rightSpeed Speed for right motor. eg: 0 + */ + //% group="DC Motors" + //% weight=97 + //% blockGap=8 + //% blockId=sumobit_motor_set_speed + //% block="set motors speed: left %leftSpeed right %rightSpeed || acceleration %acceleration " + //% leftSpeed.min=-255 leftSpeed.max=255 + //% rightSpeed.min=-255 rightSpeed.max=255 + //% acceleration.min=1 acceleration.max=9 + //% expandableArgumentMode="enabled" + export function setMotorsSpeed(leftSpeed: number, rightSpeed: number, acceleration: number = 4): void { + let leftDir = MotorDirection.Forward; + let rightDir = MotorDirection.Forward; + + if (leftSpeed < 0) { + leftSpeed = -leftSpeed; + leftDir = MotorDirection.Backward; + } + + if (rightSpeed < 0) { + rightSpeed = -rightSpeed; + rightDir = MotorDirection.Backward; + } + sumobit.runMotor(0, rightDir, rightSpeed, acceleration); + sumobit.runMotor(1, leftDir, leftSpeed, acceleration); + } + + + + + }