Skip to content

Commit

Permalink
Improve Robot Kit blocks, unhide and modify servo blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
noqman committed Dec 1, 2024
1 parent e295676 commit 21d0fcb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 77 deletions.
129 changes: 59 additions & 70 deletions robot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,36 @@
* Email: [email protected]
*******************************************************************************/




enum SumobitCountdown {

//% block="3 seconds"
Three = 3,

//% block="5 seconds"
Five = 5,

};

enum SumobitDirection {

//% block="left"
Left = 0,

//% block="right"
Right = 1,

};

enum SumobitSearch {
//% block="normal"
Normal = 0,

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

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

//% block="defense"
Defense = 1,
};



namespace sumobit {

let initialSpeed: number;
let searchDirection: number = -1;
let searchMillis = control.millis();

/**
* Motor speed initialization
Expand Down Expand Up @@ -81,7 +72,6 @@ namespace sumobit {
basic.showNumber(second - Math.round((control.millis() - startTime) / 1000))
basic.pause(210)
}

}

/**
Expand All @@ -94,27 +84,27 @@ namespace sumobit {
//% weight=17
//% blockGap=8
//% blockId="sumobit_robot_backoff"
//% block="backoff %direction || speed:%speed acceleration:%acceleration"
//% 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
//% subcategory="Robot Kit"
export function backoff(direction: SumobitDirection, speed:number=120, acceleration:number=9): void {

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, acceleration)
basic.pause(350 - speed)

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

case SumobitDirection.Left:
Expand All @@ -125,7 +115,6 @@ namespace sumobit {
basic.pause(380 - speed)
sumobit.stopMotor(1000)
basic.pause(50)

}

/**
Expand All @@ -137,40 +126,35 @@ namespace sumobit {
//% weight=16
//% blockGap=8
//% blockId="sumobit_robot_attack"
//% block="attack || speed:%speed acceleration:%acceleration"
//% block="attack speed:%speed || acceleration:%acceleration"
//% expandableArgumentMode="toggle"
//% speed.min=0 speed.max=255
//% acceleration.min=1 acceleration.max=9
//% second.fieldOptions.decompileLiterals=true
//% 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)

}
// 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)

}

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)
}
// 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)
}
}

/**
Expand All @@ -183,32 +167,37 @@ namespace sumobit {
//% weight=15
//% blockGap=8
//% blockId="sumobit_robot_search"
//% block="search %mode || speed:%speed acceleration:%acceleration"
//% 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 {
export function search(mode: SumobitSearch, speed: number = 120, acceleration: number = 9): void {

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

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;
case SumobitSearch.Normal:
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;

case SumobitSearch.Defense:
if (control.millis() - searchMillis > 4500) {
sumobit.setMotorsSpeed(speed, speed, acceleration)
basic.pause(50)
searchMillis = control.millis()
}
else {
sumobit.stopMotor(1000)
}
break;
}
}



}// namespace
} // namespace
17 changes: 10 additions & 7 deletions servo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace sumobit{
//% blockGap=8
//% blockId=sumobit_servo_disable
//% block="disable servo %servo"
//% blockHidden=true

export function disableServo(servo: ServoChannel): void {
if (servo == ServoChannel.All) {
sumobit.i2cWrite(ServoChannel.Servo1, 0);
Expand All @@ -51,15 +51,18 @@ namespace sumobit{
//% weight=39
//% blockGap=8
//% blockId=sumobit_servo_set_position
//% block="set %servo position to %position degrees"
//% position.min=0 position.max=180
//% blockHidden = true
export function setServoPosition(servo: ServoChannel, position: number): void {
//% 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 {
position = sumobit.limit(position, 0, 180);

if (servo == ServoChannel.All) {
sumobit.i2cWrite(ServoChannel.Servo1, position);
sumobit.i2cWrite(ServoChannel.Servo1, position);
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);
Expand Down

0 comments on commit 21d0fcb

Please sign in to comment.