diff --git a/battery.ts b/battery.ts index 5e8b883..3052f8f 100644 --- a/battery.ts +++ b/battery.ts @@ -9,7 +9,7 @@ namespace sumobit { /** - * Read the current battery voltage (2 d.p.). + * Get the current battery voltage (2 d.p.). */ //% group="Battery" //% weight=49 diff --git a/edge.ts b/edge.ts index 6ddf8b9..e434dbf 100644 --- a/edge.ts +++ b/edge.ts @@ -38,6 +38,7 @@ namespace sumobit { let leftThreshold: number; let rightEdgeValue: number; let leftEdgeValue: number; + /** * Returns the right edge sensor value (0-1023). */ @@ -64,15 +65,17 @@ namespace sumobit { leftEdgeValue = pins.analogReadPin(EDGE_L_PIN); return leftEdgeValue; } + /** * Returns the edge sensor value (0-1023). - * @param edge Left or right sensor. eg: SumobitEdgeSelection.Right + * @param edge Sensor to read (Left or Right). eg: SumobitEdgeSelection.Right */ //% group="Edge Sensors" //% weight=77 //% blockGap=8 //% blockId=sumobit_edge_return_value //% block="%edge edge sensor" + //% edge.defl=0 export function fetchEdgeValue(edge: SumobitEdgeSelection): number { switch (edge) { case SumobitEdgeSelection.Right: @@ -83,13 +86,13 @@ namespace sumobit { return readRightEdgeValue(); } } + /** - * Compare the edge sensor value (0-1023) with certain value and return true -if condition is meet. - * @param EdgeSide Which side of edge sensors are to compare to. eg: 0 - * @param compareType More than or less than. eg: 0 - * @param threshold The value to compare with. eg: 512 - */ + * Compare the edge sensor value (0-1023) with certain value and return true if condition is meet. + * @param EdgeSide Which side of edge sensors are to compare to. eg: 0 + * @param compareType More than or less than. eg: 0 + * @param threshold The value to compare with. eg: 512 + */ //% group="Edge Sensors" //% weight=76 //% blockGap=40 @@ -126,22 +129,23 @@ if condition is meet. return result; } /** - * Edge threshold auto calibration. - * @param coefficient Threshold calibration ratio. eg: 5 + * Calibrate edge sensor threshold. + * @param coefficient Threshold calibration ratio (Actual value is coefficient times 0.1). eg: 5 */ //% group="Edge Sensors" //% weight=75 //% blockGap=8 //% blockId=sumobit_edge_calibrate_threshold //% block="set edge sensor threshold|| coefficient:%coefficient" - //% coefficient.min=1 coefficient.max=9 + //% coefficient.min=1 coefficient.max=9 + //% coefficient.defl=5 export function calibrateEdgeThreshold(coefficient: number = 5): void { rightThreshold = pins.analogReadPin(EDGE_R_PIN) * (coefficient * 0.1); leftThreshold = pins.analogReadPin(EDGE_L_PIN) * (coefficient * 0.1); } + /** - * Return the calibrated edge threshold in seriel monitor for troubleshooting - purpose. + * Return the calibrated edge threshold in serial monitor for troubleshooting purpose. */ //% group="Edge Sensors" //% weight=74 @@ -157,13 +161,15 @@ if condition is meet. serial.writeLine("") } /** - * Compare. + * Returns true if sensor detects edge. + * @param edge Sensor to read (Left or Right). eg: SumobitEdgeSelection.Right */ //% group="Edge Sensors" //% weight=73 //% blockGap=8 //% blockId=sumobit_edge_compare_calibrated_value //% block="%edge sensor detect edge" + //% edge.defl=0 export function compareEdgeCalibrated(edge: SumobitEdgeSelection): boolean { let result = false; switch (edge) { diff --git a/main.ts b/main.ts index 6d7f4bc..9af027f 100644 --- a/main.ts +++ b/main.ts @@ -58,8 +58,8 @@ namespace sumobit { sumobit.i2cWrite(REG_ADD_PWM2, 0); // Disable the servos initially - sumobit.i2cWrite(ServoChannel.Servo1, 0); - sumobit.i2cWrite(ServoChannel.Servo2, 0); + sumobit.i2cWrite(SumobitServoChannel.Servo1, 0); + sumobit.i2cWrite(SumobitServoChannel.Servo2, 0); /** diff --git a/mode.ts b/mode.ts index 6776061..360f459 100644 --- a/mode.ts +++ b/mode.ts @@ -10,8 +10,10 @@ namespace sumobit { +// Mode event source flag. const MODE_EVENT_SOURCE = 0x01; +// Flag to indicate whether the background function has been created. let bgFunctionCreated = false; // Event type. @@ -25,7 +27,7 @@ let oldCompareResult: boolean[] = []; /** - * Read the current mode number(0-15). + * Get the current mode number(0-15). */ //% group="Mode" //% weight=69 @@ -37,22 +39,22 @@ let oldCompareResult: boolean[] = []; } /** - * Check the current mode number (0-15) and returns the result if true. - * @param modevalue The current DIP position. eg: 7 + * Returns true if the mode matches the specified value. + * @param mode The mode number to compare. eg: 7 */ //% group="Mode" //% weight=68 //% blockGap=40 //% blockId=sumobit_mode_compare_value - //% block="mode %modevalue" - //% modevalue.min=0 modevalue.max=15 + //% block="mode %mode" + //% mode.min=0 mode.max=15 + //% mode.defl=7 export function checkMode(modevalue: number): boolean { let result = false; if (readModeValue() === modevalue) { result = true; } - return result; } diff --git a/motor.ts b/motor.ts index dc4b34b..0ce5ef3 100644 --- a/motor.ts +++ b/motor.ts @@ -10,7 +10,6 @@ // Motor direction. enum SumobitMotorDirection { - //% block="forward" Forward = 0, @@ -20,7 +19,6 @@ enum SumobitMotorDirection { // Motor channel. enum SumobitMotorChannel { - //% block="right" RightMotor = 0, @@ -36,13 +34,14 @@ namespace sumobit { /** * Stop motor - * @param motor Motorchannel eg: SumobitMotorChannel.RightMotor + * @param motor The motor to control. eg: SumobitMotorChannel.Both */ //% group="DC Motors" //% weight=98 //% blockGap=8 //% blockId=sumobit_motor_stop //% block="stop %motor motor" + //% motor.defl=1000 export function stopMotor(motor: SumobitMotorChannel): void { switch (motor) { case SumobitMotorChannel.RightMotor: @@ -66,12 +65,11 @@ namespace sumobit { /** - * Run the motor forward or backward. - * (Speed = 0-255) (Acceleration = 1-9). - * @param motor Motor channel. - * @param direction Motor direction. - * @param speed Motor speed (PWM). eg: 128 - * @param acceleration Acceleration factor (1-9). eg: 9 + * Run the motor forward or backward (Speed = 0-255) (Acceleration = 1-9). + * @param motor The motor to control. eg: SumobitMotorChannel.Both + * @param direction The direction of rotation (Forward or Backward). eg: SumobitMotorDirection.Forward + * @param speed The Motor speed (PWM). eg: 120 + * @param acceleration Acceleration factor, higher value results in a faster rate of speed change (1-9). eg: 9 */ //% group="DC Motors" //% weight=99 @@ -80,8 +78,12 @@ namespace sumobit { //% accel.fieldOptions.label="Acceleration Factor" //% block="run %motor motor %direction at %speed speed || with %acceleration acceleration factor" //% inlineInputMode=inline + //% motor.defl=0 + //% direction.defl=1000 //% speed.min=0 speed.max=255 + //% speed.defl=120 //% acceleration.min=1 acceleration.max=9 + //% acceleration.defl=9 //% expandableArgumentMode="enabled" export function runMotor(motor: SumobitMotorChannel, direction: SumobitMotorDirection, speed: number, acceleration: number = 9): void { @@ -136,10 +138,10 @@ namespace sumobit { } /** - * Set individual motors speed (speed = -255 to 255, negative value = reverse). - * @param leftSpeed Speed for left motor. eg: 100 - * @param rightSpeed Speed for right motor. eg: 100 - * @param acceleration Speed for right motor. eg: 9 + * Set the speed and direction of both motors independently (speed = -255 to 255, negative value = reverse). + * @param leftSpeed The desired speed of the left motor. eg: 120 + * @param rightSpeed The desired speed of the right motor. eg: 120 + * @param acceleration Acceleration factor, higher value results in a faster rate of speed change (1-9). eg: 9 */ //% group="DC Motors" //% weight=97 @@ -147,8 +149,11 @@ namespace sumobit { //% blockId=sumobit_motor_set_speed //% block="set motors speed: left %leftSpeed right %rightSpeed || acceleration %acceleration " //% leftSpeed.min=-255 leftSpeed.max=255 + //% leftSpeed.defl=120 //% rightSpeed.min=-255 rightSpeed.max=255 + //% rightSpeed.defl=120 //% acceleration.min=1 acceleration.max=9 + //% acceleration.defl=9 //% expandableArgumentMode="enabled" export function setMotorsSpeed(leftSpeed: number, rightSpeed: number, acceleration: number = 9): void { let leftDir = SumobitMotorDirection.Forward; diff --git a/motorcurrent.ts b/motorcurrent.ts index 3261786..c1bad90 100644 --- a/motorcurrent.ts +++ b/motorcurrent.ts @@ -21,8 +21,10 @@ enum SumobitMotorChannel2 { namespace sumobit { +// Mode event source flag. const MOTORCURRENT_EVENT_SOURCE = 0x02; +// Flag for background function creation let bgFunctionCreated = false; // Event type. diff --git a/opponent.ts b/opponent.ts index 3ffb6dd..58c4546 100644 --- a/opponent.ts +++ b/opponent.ts @@ -56,13 +56,15 @@ namespace sumobit { /** - * Read the opponent sensor value. + * Returns the opponent sensor value. + * @param sensor The opponent sensor to read (Left, Front Left, Center, Front Right, Right). eg: SumobitSensorSelection1.Right */ //% group="Opponent Sensors" //% weight=89 //% blockGap=8 //% blockId=sumobit_maker_object_read_digital //% block="%sensor opponent sensor" + //% sensor.defl=4 //% position.fieldEditor="gridpicker" position.fieldOptions.columns=5 export function oppSensorValue(sensor: SumobitSensorSelection1): number { @@ -90,14 +92,15 @@ namespace sumobit { } /** - * Return true if the opponent sensor is low (Obstacle detected). + * Returns true if the opponent sensor is low (Obstacle detected). + * @param sensor The opponent sensor to read (Left, Front Left, Center, Front Right, Right). eg: SumobitSensorSelection2.Right */ //% group="Opponent Sensors" //% weight=88 //% blockGap=8 //% blockId=sumobit_maker_object_detect_opponent //% block="%position sensor detect opponent" - + //% position.defl = 4 export function oppSensorDetection(position: SumobitSensorSelection2): boolean { @@ -159,7 +162,6 @@ namespace sumobit { //% TogL.shadow="toggleHighLow" TogFL.shadow="toggleHighLow" TogFC.shadow="toggleHighLow" TogFR.shadow="toggleHighLow" TogR.shadow="toggleHighLow" //% inlineInputMode=inline //% blockHidden=true - export function oppSensorCombinationBoolean(TogL: boolean, TogFL: boolean, TogFC: boolean, TogFR: boolean, TogR: boolean): boolean { let sensorValues = [ diff --git a/rgb.ts b/rgb.ts index fce76cc..bbcdc81 100644 --- a/rgb.ts +++ b/rgb.ts @@ -71,7 +71,6 @@ namespace sumobit { } - /** * Set all RGB pixels to the specified color. * @param color RGB color of the pixel. @@ -104,7 +103,7 @@ namespace sumobit { /** * Set individual RGB pixel to a certain colour * @param pixel The pixel number we want to change the color. - * @param color RGB color of the pixel. + * @param color The desired color in RGB format. */ //% group="RGB LED" //% weight=26 @@ -114,7 +113,7 @@ namespace sumobit { //% color.shadow="colorNumberPicker" //% pixel.min=0 pixel.max=1 //% pixel.fieldEditor="numberdropdown" - //% pixel.fieldOptions.decompileLiterals=true + //% pixel.fieldOptions.decompileLiterals=false //% pixel.fieldOptions.values='0,1' //% pixel.defl='0' @@ -158,7 +157,7 @@ namespace sumobit { /** - * Convert the specified red, green, and blue channel values into an RGB color value. + * Convert the specified red, green, and blue channel values into an RGB color value. * @param red Value of the red channel (0 - 255). eg: 255 * @param green Value of the green channel (0 - 255). eg: 255 * @param blue Value of the blue channel (0 - 255). eg: 255 diff --git a/robot.ts b/robot.ts index 28869b4..6a7d493 100644 --- a/robot.ts +++ b/robot.ts @@ -35,9 +35,9 @@ enum SumobitSearch { namespace sumobit { - let initialSpeed: number; - let searchDirection: number = -1; - let searchMillis = control.millis(); +let initialSpeed: number; +let searchDirection: number = -1; +let searchMillis = control.millis(); /** * Motor speed initialization @@ -57,14 +57,15 @@ namespace sumobit { } /** - * Start LED matrix countdown (5 or 3 second) - * @param second Countdown Second eg: SumobitCountdown.Five + * Start a countdown timer on LED (5 or 3 second). + * @param second The duration of the countdown. eg: SumobitCountdown.Five */ //% group="Robot Kit" //% weight=18 //% blockGap=8 //% blockId="sumobit_robot_countdown" //% block="start countdown %second" + //% second.defl=5 //% second.fieldOptions.decompileLiterals=true //% subcategory="Robot Kit" export function countdown(second: SumobitCountdown): void { @@ -77,7 +78,7 @@ namespace sumobit { } /** - * Preset backoff routine + * Backoff routine * @param direction Backoff turn direction eg: SumobitDirection.Right * @param speed Motor speed when reverse and turn (0-255). eg: 120 * @param acceleration Motor acceleration factor (1-9). eg: 9 @@ -88,8 +89,11 @@ namespace sumobit { //% blockId="sumobit_robot_backoff" //% block="backoff %direction speed:%speed || acceleration:%acceleration" //% expandableArgumentMode="toggle" + //% direction.defl=1 //% speed.min=0 speed.max=255 + //% speed.defl=120 //% acceleration.min=1 acceleration.max=9 + //% acceleration.defl=9 //% second.fieldOptions.decompileLiterals=true //% subcategory="Robot Kit" @@ -121,7 +125,7 @@ namespace sumobit { /** * Attack routine - * @param speed Motor speed when Front Cenre detects opponent (0-255). eg: 120 + * @param speed Motor speed when Front Centre sensor detects opponent (0-255). eg: 120 * @param acceleration Motor acceleration factor (1-9). eg: 9 */ //% group="Robot Kit" @@ -131,7 +135,9 @@ namespace sumobit { //% block="attack speed:%speed || acceleration:%acceleration" //% expandableArgumentMode="toggle" //% speed.min=0 speed.max=255 + //% speed.defl=120 //% acceleration.min=1 acceleration.max=9 + //% acceleration.defl=9 //% second.fieldOptions.decompileLiterals=true //% subcategory="Robot Kit" @@ -161,8 +167,8 @@ namespace sumobit { /** * Robot search routine - * @param mode Robot search movement when no opponent is detected eg: SumobitSearch(0) - * @param speed Motor speed when Front Cenre detects opponent (0-255). eg: 120 + * @param mode The search mode. eg: SumobitSearch.0 + * @param speed Motor speed when Front Centre sensor detects opponent (0-255). eg: 120 * @param acceleration Motor acceleration factor (1-9). eg: 9 */ //% group="Robot Kit" @@ -170,9 +176,11 @@ namespace sumobit { //% blockGap=8 //% blockId="sumobit_robot_search" //% block="search %mode speed:%speed || acceleration:%acceleration" + //% mode.defl=0 //% speed.min=0 speed.max=255 + //% speed.defl=120 //% acceleration.min=1 acceleration.max=9 - //% second.fieldOptions.decompileLiterals=true + //% acceleration.defl=9 //% subcategory="Robot Kit" //% expandableArgumentMode="toggle" export function search(mode: SumobitSearch, speed: number = 120, acceleration: number = 9): void { @@ -202,4 +210,6 @@ namespace sumobit { break; } } -} // namespace \ No newline at end of file + + +} \ No newline at end of file diff --git a/servo.ts b/servo.ts index 169e5b4..a31b84a 100644 --- a/servo.ts +++ b/servo.ts @@ -9,13 +9,13 @@ // Servo Channel. -enum ServoChannel { +enum SumobitServoChannel { //% block="servo 1" - Servo1 = REG_ADD_SRV1_POS, + Servo1 = 1, //% block="servo 2" - Servo2 = REG_ADD_SRV2_POS, + Servo2 = 2, //% block="all" All = 1000, @@ -26,49 +26,70 @@ namespace sumobit{ /** * Disable servo. - * @param servo Servo channel. + * @param servo The servo to control. eg:SumobitServoChannel.Servo1 */ //% group="Servos" //% weight=38 //% blockGap=8 //% blockId=sumobit_servo_disable //% block="disable servo %servo" - - export function disableServo(servo: ServoChannel): void { - if (servo == ServoChannel.All) { - sumobit.i2cWrite(ServoChannel.Servo1, 0); - sumobit.i2cWrite(ServoChannel.Servo2, 0); - } - else { - sumobit.i2cWrite(servo, 0); + //% servo.defl=1 + export function disableServo(servo: SumobitServoChannel): void { + + switch (servo) { + case SumobitServoChannel.Servo1: + setServoPosition(1, 0, 9); + break; + + case SumobitServoChannel.Servo2: + setServoPosition(2, 0, 9); + break; + + case SumobitServoChannel.All: + setServoPosition(1000, 0, 9); + break; } } /** * Set the position for servo (0-180 degrees). - * @param servo Servo channel. eg: ServoChannel.Servo1 - * @param position Servo positon. eg: 90 + * @param servo The servo to control. eg: SumobitServoChannel.Servo1 + * @param position The desired angle of the servo. eg: 90 + * @param speed Servo movement speed. Higher values result in faster movement. eg:9 */ //% group="Servos" //% weight=39 //% blockGap=8 //% blockId=sumobit_servo_set_position //% block="set %servo position to %position \\° || at speed:%speed" - //% position.min=0 position.max=180 - - export function setServoPosition(servo: ServoChannel, position: number, speed:number=9): void { + //% servo.defl=1 + //% position.min=0 position.max=180 + //% position.defl=90 + //% speed.min=0 position.max=180 + //% speed.defl=9 + export function setServoPosition(servo: SumobitServoChannel, position: number, speed:number=9): void { position = sumobit.limit(position, 0, 180); - if (servo == ServoChannel.All) { - sumobit.i2cWrite(REG_ADD_SRV1_POS, position); - sumobit.i2cWrite(REG_ADD_SRV2_POS, position); - sumobit.i2cWrite(REG_ADD_SRV1_SPEED, position); - sumobit.i2cWrite(REG_ADD_SRV1_SPEED, position); - - } - else { - sumobit.i2cWrite(servo, position); + switch (servo) { + case SumobitServoChannel.Servo1: + sumobit.i2cWrite(REG_ADD_SRV1_POS, position); + sumobit.i2cWrite(REG_ADD_SRV1_SPEED, speed); + break; + + case SumobitServoChannel.Servo2: + sumobit.i2cWrite(REG_ADD_SRV2_POS, position); + sumobit.i2cWrite(REG_ADD_SRV2_SPEED, speed); + break; + + case SumobitServoChannel.All: + sumobit.i2cWrite(REG_ADD_SRV1_POS, position); + sumobit.i2cWrite(REG_ADD_SRV2_POS, position); + sumobit.i2cWrite(REG_ADD_SRV1_SPEED, speed); + sumobit.i2cWrite(REG_ADD_SRV2_SPEED, speed); + break; } } + + }