Skip to content

Commit

Permalink
Improve edge sensor and robot kit blocks and add search function
Browse files Browse the repository at this point in the history
  • Loading branch information
noqman committed Nov 21, 2024
1 parent ce052ca commit 549bb61
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 73 deletions.
19 changes: 10 additions & 9 deletions edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,23 @@ namespace sumobit {

/**
* Edge threshold auto calibration.
* @param coefficient Threshold calibration ratio. eg: 5
*/
//% group="Edge Sensors"
//% weight=75
//% blockGap=8
//% blockId=sumobit_edge_calibrate_threshold
//% block="calibrate edge sensor"
export function calibrateEdgeThreshold(): void {
//% block="set edge sensor threshold|| - coefficient:%coefficient"
//% coefficient.min=1 coefficient.max=9


export function calibrateEdgeThreshold(coefficient:number=5): void {

let startTime1 = control.millis();

while (control.millis() - startTime1 < 1000) {
sumobitRightThreshold = pins.analogReadPin(EDGE_R_PIN) * (coefficient*0.1);
sumobitLeftThreshold = pins.analogReadPin(EDGE_L_PIN) * (coefficient *0.1);

sumobitRightThreshold = pins.analogReadPin(EDGE_R_PIN)*0.5;
sumobitLeftThreshold = pins.analogReadPin(EDGE_L_PIN)*0.5;

}
}

/**
Expand Down Expand Up @@ -183,13 +184,13 @@ namespace sumobit {
let result = false;
switch (edge) {
case SumobitEdgeSelection.Right:
if (sumobitRightEdgeValue < sumobitRightThreshold) {
if (readRightEdgeValue() < sumobitRightThreshold) {
result = true;
}
break;

case SumobitEdgeSelection.Left:
if (sumobitLeftEdgeValue < sumobitLeftThreshold) {
if (readLeftEdgeValue() < sumobitLeftThreshold) {
result = true;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const REG_ADD_RESET = 36;
*/

//% weight=10 color=#ff8000 icon="\uf091" block="SUMO:BIT"
//% groups=['DC Motors', 'Servos' , 'Mode', 'Motor Current','Opponent Sensors' , 'Edge Sensors', 'RGB LED', 'Robot Kit']
//% groups=['DC Motors', 'Servos' , 'Mode', 'Motor Current','Opponent Sensors' , 'Edge Sensors', 'RGB LED']
namespace sumobit {

// Stop all motor initially
Expand Down
168 changes: 106 additions & 62 deletions robot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Email: [email protected]
*******************************************************************************/

let sumobitInitialSpeed: number;



enum SumobitCountdown {
Expand All @@ -29,21 +29,26 @@ enum SumobitDirection {

};

enum SumobitAttack{
enum SumobitSearch {

//% block="testing"
Test = 0,
//% block="curve"
Curve = 0,

//% block="game"
Game = 1,
//% block="straight"
Straight = 1,

};



namespace sumobit {

let initialSpeed: number;
let searchDirection: number = -1;

/**
* Motor speed initialization
* * @param speed Intitial Robot Speed eg: 255
* @param speed Intitial Robot Speed eg: 255
*/
//% group="Robot Kit"
//% weight=19
Expand All @@ -52,23 +57,23 @@ namespace sumobit {
//% block="set robot speed %speed"
//% speed.min=0 speed.max=255
//% second.fieldOptions.decompileLiterals=true
//% advanced=true
//% subcategory="Robot Kit"
//% blockHidden=true
export function setSpeed(speed: number): void {
speed = sumobitInitialSpeed;
speed = initialSpeed;
}

/**
* Start LED matrix countdown (5 or 3 second)
* * @param second Countdown Second eg: SumobitCountdown.Five
* @param second Countdown Second eg: SumobitCountdown.Five
*/
//% group="Robot Kit"
//% weight=18
//% blockGap=8
//% blockId="sumobit_robot_countdown"
//% block="start countdown %second"

//% second.fieldOptions.decompileLiterals=true
//% advanced=true
//% subcategory="Robot Kit"
export function countdown(second: SumobitCountdown): void {
let startTime = control.millis();

Expand All @@ -81,29 +86,41 @@ namespace sumobit {

/**
* Preset backoff routine
* * @param direction Backoff turn direction eg: SumobitDirection.Right
* @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
*/
//% group="Robot Kit"
//% weight=17
//% blockGap=8
//% blockId="sumobit_robot_backoff"
//% block="backoff %direction"
//% block="backoff %direction || speed:%speed acceleration:%acceleration"
//% expandableArgumentMode="toggle"
//% speed.min=0 speed.max=255
//% acceleration.min=1 acceleration.max=9
//% second.fieldOptions.decompileLiterals=true
//% advanced=true
export function backoff(direction: SumobitDirection): void {
let speed = sumobitInitialSpeed
//% subcategory="Robot Kit"

export function backoff(direction: SumobitDirection, speed:number=120, acceleration:number=9): void {

//stop motor
sumobit.stopMotor(1000)
basic.pause(50)
//reverse
sumobit.setMotorsSpeed(speed / -1, speed / -1, 9)
//reverse
sumobit.setMotorsSpeed(speed * -1, speed * -1, acceleration)
basic.pause(350 - speed)


//rotate robot
switch (direction) {
case SumobitDirection.Right:
sumobit.setMotorsSpeed(speed / -1, speed / 1, 9);
searchDirection=1;
sumobit.setMotorsSpeed(speed * 1, speed * -1 , 9);
break;

case SumobitDirection.Left:
sumobit.setMotorsSpeed(speed / 1, speed / -1, 9);
searchDirection = -1;
sumobit.setMotorsSpeed(speed * -1, speed * 1, 9);
break;
}
basic.pause(380 - speed)
sumobit.stopMotor(1000)
Expand All @@ -112,59 +129,86 @@ namespace sumobit {
}

/**
* Attack routine for testing or game
* * @param
* Attack routine
* @param speed Motor speed when Front Cenre detects opponent (0-255). eg: 120
* @param acceleration Motor acceleration factor (1-9). eg: 9
*/
//% group="Robot Kit"
//% weight=16
//% blockGap=8
//% blockId="sumobit_robot_attack"
//% block="attack %mode"
//% block="attack || speed:%speed acceleration:%acceleration"
//% expandableArgumentMode="toggle"
//% speed.min=0 speed.max=255
//% acceleration.min=1 acceleration.max=9
//% second.fieldOptions.decompileLiterals=true
//% advanced=true
export function attack(mode: SumobitAttack): void {
let speed = sumobitInitialSpeed

switch(mode){
case SumobitAttack.Test:
//% subcategory="Robot Kit"

export function attack(speed: number=120, acceleration: number=9): void {

// Opponent in Front Centre
if (oppSensorDetection(2)) {
sumobit.setMotorsSpeed(speed, speed, acceleration)
}
// Opponent in Front Right
else if (oppSensorDetection(3)) {
sumobit.setMotorsSpeed(255, 0, 9)

} else if (oppSensorDetection(0)) {
sumobit.setMotorsSpeed(0, speed, 9)
basic.pause(300)
} else if (oppSensorDetection(4)) {
sumobit.setMotorsSpeed(speed, 0, 9)
basic.pause(300)
} else if (oppSensorDetection(1)) {
sumobit.setMotorsSpeed(speed * 0.5, speed, 9)
basic.pause(100)
} else if (oppSensorDetection(2)) {
sumobit.setMotorsSpeed(speed, speed * 0.5, 9)
basic.pause(100)
}


case SumobitAttack.Game:
if (oppSensorDetection(2)) {
sumobit.setMotorsSpeed(speed, speed,9)
} else if (oppSensorDetection(0)) {
sumobit.setMotorsSpeed(0, speed,9)
basic.pause(300)
} else if (oppSensorDetection(4)) {
sumobit.setMotorsSpeed(speed, 0, 9)
basic.pause(300)
} else if (oppSensorDetection(1)) {
sumobit.setMotorsSpeed(speed*0.5, speed, 9)
basic.pause(100)
} else if (oppSensorDetection(2)) {
sumobit.setMotorsSpeed(speed, speed*0.5, 9)
basic.pause(100)
// Opponent in Front Left
else if (oppSensorDetection(1)) {
sumobit.setMotorsSpeed(0, 255, 9)

}
// Opponent at Right
else if (oppSensorDetection(4)) {
sumobit.setMotorsSpeed(255, -255, 9)

}
// Opponent at Left
else if (oppSensorDetection(0)) {
sumobit.setMotorsSpeed(-255, 255, 9)

}

}
}

/**
* 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 acceleration Motor acceleration factor (1-9). eg: 9
*/
//% group="Robot Kit"
//% weight=15
//% blockGap=8
//% blockId="sumobit_robot_search"
//% block="search %mode || speed:%speed acceleration:%acceleration"
//% speed.min=0 speed.max=255
//% acceleration.min=1 acceleration.max=9
//% second.fieldOptions.decompileLiterals=true
//% subcategory="Robot Kit"
//% expandableArgumentMode="toggle"
export function search(mode:SumobitSearch, speed:number=120, acceleration:number= 9): void {

switch(mode){
case SumobitSearch.Straight:
sumobit.setMotorsSpeed(speed, speed , acceleration)
break;

case SumobitSearch.Curve:
if (searchDirection=1){
// curve to right
sumobit.setMotorsSpeed(speed , speed * 0.85, acceleration)
} else if (searchDirection = -1) {
// curve to left
sumobit.setMotorsSpeed(speed * 0.85, speed , acceleration)
}
break;

}
}



}
}// namespace
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ basic.forever(function () {

}
if (sumobit.checkMode(2)) {
sumobit.attack(SumobitAttack.Test)
sumobit.attack(0,9)
} else {

}
Expand Down

0 comments on commit 549bb61

Please sign in to comment.