From ca10e663899ca41876c94fa6eecfb41502a8fcc0 Mon Sep 17 00:00:00 2001 From: Adam0Brien <20093460@mail.wit.ie> Date: Tue, 2 Apr 2024 15:23:18 +0100 Subject: [PATCH 1/2] feat: Added Sensor Pod to USV Comms. --- USV/Sensor-Pod-Comms.ts | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 USV/Sensor-Pod-Comms.ts diff --git a/USV/Sensor-Pod-Comms.ts b/USV/Sensor-Pod-Comms.ts new file mode 100644 index 0000000..394bdb0 --- /dev/null +++ b/USV/Sensor-Pod-Comms.ts @@ -0,0 +1,59 @@ +// Starter Implementation +// Change on button A and B pressed to physical circuit +input.onButtonPressed(Button.A, function () { + isSubmerged = 0 +}) +input.onButtonPressed(Button.B, function () { + isSubmerged = 1 +}) + + +let isSubmerged = 0 +radio.setGroup(213) +basic.showIcon(IconNames.Yes) +USVSensorPod.initLocalDisplay() +let ph_list: number[] = [] +let light_list: number[] = [] +let temp_list: number[] = [] +isSubmerged = 1 +basic.forever(function () { + USVSensorPod.ShowString("Temp:" + USVSensorPod.calculateTempC() + "°C", 0, 0) + USVSensorPod.ShowString("pH:" + USVSensorPod.calculatePh(), 0, 1) +}) +basic.forever(function () { + if (isSubmerged == 1) { + ph_list.push(USVSensorPod.calculatePh()) + temp_list.push(USVSensorPod.calculateTempC()) + light_list.push(USVSensorPod.getLight()) + basic.pause(100) + led.toggle(4, 0) + } +}) +basic.forever(function () { + if (isSubmerged == 0) { + for (let value of ph_list) { + radio.sendValue("pH", value) + basic.pause(1000) + ph_list.removeAt(ph_list.indexOf(value)) + } + } +}) +basic.forever(function () { + if (isSubmerged == 0) { + for (let value of light_list) { + radio.sendValue("l", value) + basic.pause(1000) + led.toggle(0, 0) + light_list.removeAt(light_list.indexOf(value)) + } + } +}) +basic.forever(function () { + if (isSubmerged == 0) { + for (let value of temp_list) { + radio.sendValue("tempC", value) + basic.pause(100) + temp_list.removeAt(temp_list.indexOf(value)) + } + } +}) From 8bf040ffa7cd4c56f11fb87d4704318041e375b4 Mon Sep 17 00:00:00 2001 From: Adam0Brien <20093460@mail.wit.ie> Date: Thu, 4 Apr 2024 14:26:59 +0100 Subject: [PATCH 2/2] feat: Added Listen for sensor pod data block --- USV/Sensor-Pod-Comms.ts | 4 +- USV/USV.ts | 105 +++++++++++++++++++++++++--------------- 2 files changed, 66 insertions(+), 43 deletions(-) diff --git a/USV/Sensor-Pod-Comms.ts b/USV/Sensor-Pod-Comms.ts index 394bdb0..84974c1 100644 --- a/USV/Sensor-Pod-Comms.ts +++ b/USV/Sensor-Pod-Comms.ts @@ -6,8 +6,6 @@ input.onButtonPressed(Button.A, function () { input.onButtonPressed(Button.B, function () { isSubmerged = 1 }) - - let isSubmerged = 0 radio.setGroup(213) basic.showIcon(IconNames.Yes) @@ -41,7 +39,7 @@ basic.forever(function () { basic.forever(function () { if (isSubmerged == 0) { for (let value of light_list) { - radio.sendValue("l", value) + radio.sendValue("light", value) basic.pause(1000) led.toggle(0, 0) light_list.removeAt(light_list.indexOf(value)) diff --git a/USV/USV.ts b/USV/USV.ts index 2d98800..29e77ee 100644 --- a/USV/USV.ts +++ b/USV/USV.ts @@ -11,7 +11,7 @@ namespace USV { let Arm = 1 } - //% blockId=1disarmUSV + //% blockId=disarmUSV //% block="Disarm USV" //% weight=50 //% color=#2bd9ad @@ -62,25 +62,29 @@ namespace USV { for (let index = 0; index <= sec; index++) { for (let index = 0; index <= 10; index++) { basic.pause(100) - radio.sendValue("left", speed) + radio.sendValue("left", 180 - speed) } } } } if (motor == 1) { if (Arm == 1) { - for (let index = 0; index < 100; index++) { - radio.sendValue("right", 100) - basic.pause(100) + for (let index = 0; index <= sec; index++) { + for (let index = 0; index <= 10; index++) { + basic.pause(100) + radio.sendValue("right", speed) + } } } } if (motor == 2) { if (Arm == 1) { for (let index = 0; index <= sec; index++) { - radio.sendValue("left", speed) + for (let index = 0; index <= 10; index++) { + radio.sendValue("left", 180 - speed) radio.sendValue("right", speed) basic.pause(100) + } } } } @@ -103,7 +107,8 @@ namespace USV { down = 0, //%blockId=sensor_up //% block="Up" - up = 1 + up = 1 + } /** @@ -158,52 +163,72 @@ namespace USV { //% color=#7F76AB //% subcategory=USV //% group="D. Auto Movement" - //% blockId=AUTO_USV block="Auto Drive |%Direction" + //% blockId=AUTO_USV block="Auto Drive |%Direction for %number seconds" //% speed.min=60 speed.max=120 //% index.fieldEditor="gridpicker" index.fieldOptions.columns=2 - export function AUTO_USV(direction: Direction): void { + export function AUTO_USV(direction: Direction, sec: number): void { + for (let index = 0; index <= sec; index++) { + for (let index = 0; index <= 10; index++) { radio.sendValue("dCompass", direction) + } + } } - - //% blockId=getTempC - //% block="Sensor Pod Temperature °C" - //% subcategory=Sensor Pod - //% color=#442FDE - //% group="Sensors" - export function getTempC(): number { - // Ask sensor pod for temperature through radio - return 0 + function directionToDegree(direction: string) { + if (direction == "N") { + return 0 + } else if (direction == "NE") { + return 45 + } else if (direction == "E") { + return 90 + } else if (direction == "SE") { + return 135 + } else if (direction == "S") { + return 180 + } else if (direction == "SW") { + return 225 + } else if (direction == "W") { + return 270 + } else if (direction == "NW") { + return 315 + } else { + // Default to North if unknown + return 0 + } } - //% blockId=getTempF - //% block="Sensor Pod Temperature °F" - //% subcategory=Sensor Pod - //% color=#442FDE - //% group="Sensors" - export function getTempF(): number { - // Ask sensor pod for temperature - return 0 + + export enum SensorType { + pH = 0, + tempC = 1, + light = 2, } - //% blockId=getPh - //% block="Sensor Pod pH" - //% subcategory=Sensor Pod - //% color=#442FDE - //% group="Sensors" - export function getPh(): number { - // Ask sensor pod for pH - return 0 + function sensorListener(sensorType: SensorType) { + let sensorName: string; + + if(sensorType == 0) { + sensorName = "pH"; + } else if (sensorType == 1){ + sensorName = "tempC"; + } else if (sensorType == 2) { + sensorName = "light"; + } + + radio.onReceivedValue(function (name, value) { + if (name == sensorName) { + serial.writeValue(name, value); + } + }); } - //% blockId=Sensor Pod Light - //% block="Sensor Pod Light" - //% subcategory=Sensor Pod + //% blockId="sensorListenerBlock" + //% block="Listen for Sensor Pod |%SensorType value" + //% subcategory="Sensors" //% color=#442FDE //% group="Sensors" - export function getLight(): number { - // Ask sensor pod for LDR reading - return 0 + export function sensorListenerBlock(sensorType: SensorType): void { + sensorListener(sensorType); }