-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added edge.ts, opponent.ts, motor.ts, servo.ts, mode.ts, motorcurrent…
….ts and rgb.ts
- Loading branch information
Showing
10 changed files
with
1,126 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/******************************************************************************* | ||
* Functions for SUMO:BIT battery input | ||
* | ||
* Company: Cytron Technologies Sdn Bhd | ||
* Website: http://www.cytron.io | ||
* Email: [email protected] | ||
*******************************************************************************/ | ||
|
||
namespace sumobit { | ||
|
||
/** | ||
* Return the current battery voltage. | ||
*/ | ||
//% group="Battery" | ||
//% weight=49 | ||
//% blockGap=8 | ||
//% blockId=sumobit_read_VIN | ||
//% block="battery voltage" | ||
export function readBatteryValue(): number { | ||
let highByte: number; | ||
let lowByte: number; | ||
highByte = sumobit.i2cRead(REG_ADD_VIN_HIGH); | ||
lowByte = sumobit.i2cRead(REG_ADD_VIN_LOW); | ||
return ((highByte << 8) | lowByte) / 100; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/******************************************************************************* | ||
* Functions for SUMO:BIT edge sensors | ||
* | ||
* Company: Cytron Technologies Sdn Bhd | ||
* Website: http://www.cytron.io | ||
* Email: [email protected] | ||
*******************************************************************************/ | ||
|
||
// Default pin. | ||
const EDGE_R_PIN = AnalogPin.P0; | ||
const EDGE_L_PIN = AnalogPin.P1; | ||
|
||
// Event source. | ||
const EDGE_SENSOR_EVENT_SOURCE = 0x03; | ||
|
||
// Edge sensor side | ||
enum EdgeSide { | ||
|
||
//% block="right" | ||
Right = 0, | ||
//% block="left" | ||
Left = 1, | ||
|
||
//% block="both" | ||
Both = 1000 | ||
} | ||
|
||
// Comparison type. | ||
enum EdgeCompareType { | ||
//% block=">" | ||
MoreThan = 0, | ||
|
||
//% block="<" | ||
LessThan = 1 | ||
} | ||
|
||
namespace sumobit { | ||
|
||
/** | ||
* Return the right edge sensor value (0-1023). | ||
*/ | ||
//% group="Edge Sensors" | ||
//% weight=79 | ||
//% blockGap=8 | ||
//% blockId=sumobit_read_edge_R_value | ||
//% block="right edge sensor" | ||
export function readEdgeRValue(): number { | ||
return pins.analogReadPin(EDGE_R_PIN); | ||
} | ||
|
||
/** | ||
* Return the left edge sensor value (0-1023). | ||
*/ | ||
//% group="Edge Sensors" | ||
//% weight=78 | ||
//% blockGap=8 | ||
//% blockId=sumobit_read_edge_L_value | ||
//% block="left edge sensor" | ||
export function readEdgeLValue(): number { | ||
return pins.analogReadPin(EDGE_L_PIN); | ||
} | ||
|
||
|
||
/** | ||
* Compare the edge sensor(s) 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=77 | ||
//% blockGap=40 | ||
//% blockId=sumobit_compare_edge_value | ||
//% block="%side edge sensor %compareType %threshold" | ||
//% threshold.min=0 threshold.max=1023 | ||
export function compareEdge(side: EdgeSide, compareType: EdgeCompareType, threshold: number,): boolean { | ||
|
||
let result = false; | ||
let R = readEdgeRValue(); | ||
let L = readEdgeLValue(); | ||
|
||
|
||
switch (side) { | ||
case EdgeSide.Right: | ||
if ((R > threshold && EdgeCompareType.MoreThan) || (R < threshold && EdgeCompareType.LessThan)) { | ||
result = true; | ||
} | ||
break; | ||
|
||
case EdgeSide.Left: | ||
if ((L > threshold && EdgeCompareType.MoreThan) || (L < threshold && EdgeCompareType.LessThan)) { | ||
result = true; | ||
} | ||
break; | ||
|
||
case EdgeSide.Both: | ||
if (((R > threshold && L > threshold) && EdgeCompareType.MoreThan) || ((R > threshold && L > threshold) && EdgeCompareType.LessThan)) { | ||
result = true; | ||
} | ||
break; | ||
} | ||
|
||
return result; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,110 @@ | ||
basic.forever(function () { | ||
|
||
}) | ||
// I2C Adress | ||
const I2C_ADDRESS = 0x09; | ||
|
||
// Register addresses. | ||
const REG_ADD_REVISION = 0; | ||
const REG_ADD_SRV1_POS = 1; | ||
const REG_ADD_SRV1_SPEED = 2; | ||
const REG_ADD_SRV2_POS = 3; | ||
const REG_ADD_SRV2_SPEED = 4; | ||
const REG_SERVO_R1 = 5; | ||
const REG_SERVO_R2 = 6; | ||
const REG_SERVO_R3 = 7; | ||
const REG_SERVO_R4 = 8; | ||
const REG_ADD_PWM1 = 9; | ||
const REG_ADD_DIR1 = 10; | ||
const REG_ADD_ACCEL1 = 11; | ||
const REG_ADD_PWM2 = 12; | ||
const REG_ADD_DIR2 = 13; | ||
const REG_ADD_ACCEL2 = 14; | ||
const REG_MOTOR_R1 = 15; | ||
const REG_MOTOR_R2 = 16; | ||
const REG_MOTOR_R3 = 17; | ||
const REG_MOTOR_R4 = 18; | ||
const REG_ADD_R0 = 19; | ||
const REG_ADD_G0 = 20; | ||
const REG_ADD_B0 = 21; | ||
const REG_ADD_R1 = 22; | ||
const REG_ADD_G1 = 23; | ||
const REG_ADD_B1 = 24; | ||
const REG_ADD_AN1_HIGH = 25; | ||
const REG_ADD_AN1_LOW = 26; | ||
const REG_ADD_AN2_HIGH = 27; | ||
const REG_ADD_AN2_LOW = 28; | ||
const REG_CURRENT_R1 = 29; | ||
const REG_CURRENT_R2 = 30; | ||
const REG_ADD_VIN_HIGH = 31; | ||
const REG_ADD_VIN_LOW = 32; | ||
const REG_VIN_R1 = 33; | ||
const REG_VIN_R2 = 34; | ||
const REG_ADD_DIP = 35; | ||
const REG_ADD_RESET = 36; | ||
|
||
|
||
|
||
/** | ||
* Blocks for SUMO:BIT. | ||
*/ | ||
|
||
//% weight=10 color=#ff8000 icon="\uf091" block="SUMO:BIT" | ||
//% groups=['DC Motors', 'Servos' , 'Mode', 'Motor Current','Opponent Sensors' , 'Edge Sensors', 'RGB LED'] | ||
namespace sumobit { | ||
|
||
// Stop all motor at start | ||
brakeMotor(MotorChannel.MR); | ||
brakeMotor(MotorChannel.ML); | ||
|
||
// Disable the servos. | ||
disableServo(ServoChannel.S1); | ||
disableServo(ServoChannel.S2); | ||
|
||
|
||
/** | ||
* Limit the range of a number. | ||
* @param value The number we want to limit. | ||
* @param min Minimum value of the number. | ||
* @param max Maximum value of the number. | ||
*/ | ||
//% blockHidden=true | ||
//% blockId=sumobit_limit | ||
export function limit(value: number, min: number, max: number): number { | ||
if (value < min) { | ||
value = min; | ||
} | ||
else if (value > max) { | ||
value = max; | ||
} | ||
return value; | ||
} | ||
|
||
|
||
/** | ||
* I2C read from the register of RP2040. | ||
* @param register Register address. | ||
*/ | ||
//% blockHidden=true | ||
//% blockId=sumobit_i2c_read | ||
export function i2cRead(register: number): number { | ||
let value = 0; | ||
pins.i2cWriteNumber(I2C_ADDRESS, register, NumberFormat.UInt8LE, true); | ||
value = pins.i2cReadNumber(I2C_ADDRESS, NumberFormat.UInt8LE); | ||
return value; | ||
} | ||
|
||
|
||
/** | ||
* I2C write to the register of RP2040. | ||
* @param register Register address. | ||
* @param data Data to write. | ||
*/ | ||
//% blockHidden=true | ||
//% blockId=sumobit_i2c_write | ||
export function i2cWrite(register: number, data: number): void { | ||
let buffer = pins.createBuffer(2); | ||
buffer[0] = register; | ||
buffer[1] = data; | ||
pins.i2cWriteBuffer(I2C_ADDRESS, buffer); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/******************************************************************************* | ||
* Functions for sumo:bit mode. | ||
* | ||
* Company: Cytron Technologies Sdn Bhd | ||
* Website: http://www.cytron.io | ||
* Email: [email protected] | ||
*******************************************************************************/ | ||
const MODE_EVENT_SOURCE = 0x01; | ||
|
||
namespace sumobit { | ||
|
||
let bgFunctionCreated = false; | ||
|
||
// Event type. | ||
let eventType = 0; | ||
|
||
// Array for mode value. | ||
let modevalueArray: number[] = []; | ||
|
||
// Array for old compare result. | ||
let oldCompareResult: boolean[] = []; | ||
|
||
|
||
/** | ||
* Return the current mode number(0-15). | ||
*/ | ||
//% group="Mode" | ||
//% weight=69 | ||
//% blockGap=8 | ||
//% blockId=sumobit_read_mode_value | ||
//% block="mode" | ||
export function readModeValue(): number { | ||
return sumobit.i2cRead(REG_ADD_DIP); | ||
} | ||
|
||
/** | ||
* Check the current mode number (0-15) and returns the result if true. | ||
* @param modevalue The current DIP position. eg: 7 | ||
*/ | ||
//% group="Mode" | ||
//% weight=68 | ||
//% blockGap=40 | ||
//% blockId=sumobit_check_mode_value | ||
//% block="mode %modevalue" | ||
//% modevalue.min=0 modevalue.max=15 | ||
export function checkMode(modevalue: number): boolean { | ||
let result = false; | ||
|
||
if (readModeValue() === modevalue) { | ||
result = true; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Check current mode value and do something when true. | ||
* @param modevalue The current mode value. eg: 7 | ||
* @param handler Code to run when the event is raised. | ||
*/ | ||
//% group="Mode" | ||
//% weight=67 | ||
//% blockGap=8 | ||
//% blockId=sumobit_mode_event | ||
//% block="on mode value %modevalue" | ||
//% modevalue.min=0 modevalue.max=15 | ||
//% blockHidden=true | ||
export function onModeEvent(modevalue: number, handler: Action): void { | ||
// Use a new event type everytime a new event is create. | ||
eventType++; | ||
|
||
// Add the event info to the arrays. | ||
modevalueArray.push(modevalue); | ||
|
||
// Create a placeholder for the old compare result. | ||
oldCompareResult.push(false); | ||
|
||
// Register the event. | ||
control.onEvent(MODE_EVENT_SOURCE, eventType, handler); | ||
|
||
// Create a function in background if haven't done so. | ||
// This function will check for pot value and raise the event if the condition is met. | ||
if (bgFunctionCreated == false) { | ||
control.inBackground(function () { | ||
|
||
while (true) { | ||
// Loop for all the event created. | ||
for (let i = 0; i < eventType; i++) { | ||
|
||
// Check if the condition is met. | ||
if (checkMode(modevalueArray[i]) == true) { | ||
// Raise the event if the compare result changed from false to true. | ||
if (oldCompareResult[i] == false) { | ||
control.raiseEvent(MODE_EVENT_SOURCE, i + 1); | ||
} | ||
|
||
// Save old compare result. | ||
oldCompareResult[i] = true; | ||
} | ||
else { | ||
// Save old compare result. | ||
oldCompareResult[i] = false; | ||
} | ||
basic.pause(20) | ||
} | ||
} | ||
|
||
}); | ||
|
||
bgFunctionCreated = true; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.