From a421cbe2eb1bdf9609b69beb4cb19d0b3123044e Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 5 Oct 2022 13:46:01 +0200 Subject: [PATCH 01/68] everything working --- Devices/Arduino/HallSensor/HallSensor.ino | 63 ++++++++++++++--------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 9d07247a..c60714d4 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,13 +1,13 @@ #define ARDUINO_TYPE "HALLSENS" #define VERSION "1" #define POSITION 1 +#define size 1024 #define BAUDRATE 9600 -#include +#include // Tlv493d Opject -Tlv493d Tlv493dMagnetic3DSensor = Tlv493d(); - - +Tle493d_w2b6 sensor = Tle493d_w2b6(Tle493d::MASTERCONTROLLEDMODE); +int called = 0; // Communication #define INPUT_BUFFER_SIZE 256 char input_buffer[INPUT_BUFFER_SIZE]; @@ -35,11 +35,11 @@ commandHandler_t cmdHandler[] = { }; int getCommands(char*) { - Serial.println("Valid Commands (without quotes):"); - Serial.println("'!DATA*#' "); - Serial.println("'!POS*#' "); - Serial.println("'!VERSION*#' "); - Serial.println("'!COMMANDS*#' "); + Serial.print("Valid Commands (without quotes):"); + Serial.print("'!DATA*#' "); + Serial.print("'!POS*#' "); + Serial.print("'!VERSION*#' "); + Serial.print("'!COMMANDS*#' "); Serial.println("#"); } @@ -118,34 +118,47 @@ void serialCommand() { void setup() { Serial.begin(BAUDRATE); while(!Serial); - - //For the Evalkit "TLV493D-A1B6 MS2GO" uncommend following 3 lines: - pinMode(LED2, OUTPUT); //Sensor-VDD as output - digitalWrite(LED2, HIGH); //Power on the sensor + + //If using the MS2Go-Kit: Enable following lines to switch on the sensor + // *** + pinMode(LED2, OUTPUT); + digitalWrite(LED2, HIGH); delay(50); + // *** - Tlv493dMagnetic3DSensor.begin(); - Tlv493dMagnetic3DSensor.setAccessMode(Tlv493dMagnetic3DSensor.MASTERCONTROLLEDMODE); - Tlv493dMagnetic3DSensor.disableTemp(); + sensor.begin(); } int getData(char*) { - Tlv493dMagnetic3DSensor.updateData(); - unsigned long measDelay = Tlv493dMagnetic3DSensor.getMeasurementDelay(); + // updateData reads values from sensor and reading triggers next measurement + int ret = sensor.updateData(); // Throw away first old data + delay(10); + ret = sensor.updateData(); // Could check ret after each measurement + uint16_t measDelay = 10; unsigned long start = millis(); - // TODO perform measurement - Serial.write(Tlv493dMagnetic3DSensor.getRawX()); - Serial.write(Tlv493dMagnetic3DSensor.getRawY()); - Serial.write(Tlv493dMagnetic3DSensor.getRawZ()); - Serial.println("#"); - Serial.flush(); + // TODO perform measurement + + int16_t mX = 4352; + int16_t mY = sensor.getRawY(); + int16_t mZ = sensor.getRawZ(); + int16_t sX=0,sY=0,sZ =0; + + Serial.write((byte *)&mX,2); + Serial.write((byte *)&mY,2); + Serial.write((byte *)&mZ,2); + //Serial.write((byte *)&sX,2); + //Serial.write((byte *)&sY,2); + //Serial.write((byte *)&sZ,2); + Serial.print("#"); + Serial.flush(); + unsigned long end = millis(); if (end - start < measDelay) { - delay(end - start); + delay(measDelay-(end - start)); } } From 0ee5c86d1b2f71e75ef74d4178c3918a1dea7a3a Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 5 Oct 2022 14:50:54 +0200 Subject: [PATCH 02/68] reset hallsensor --- Devices/Arduino/HallSensor/HallSensor.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index c60714d4..f399d08b 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -141,7 +141,7 @@ int getData(char*) { // TODO perform measurement - int16_t mX = 4352; + int16_t mX = sensor.getRawX(); int16_t mY = sensor.getRawY(); int16_t mZ = sensor.getRawZ(); From 822529631ecb7beed6d70c5952804ccb2af7c09f Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 6 Oct 2022 15:19:47 +0200 Subject: [PATCH 03/68] sending char instead of bin --- Devices/Arduino/HallSensor/HallSensor.ino | 16 +++++++++------- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 5 ++--- src/Devices/Utils/SerialDevices/SerialDevice.jl | 5 ++++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index f399d08b..beef98ef 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -141,15 +141,16 @@ int getData(char*) { // TODO perform measurement - int16_t mX = sensor.getRawX(); - int16_t mY = sensor.getRawY(); - int16_t mZ = sensor.getRawZ(); + int16_t mX = 1; + int16_t mY = 4352; + int16_t mZ = 3; int16_t sX=0,sY=0,sZ =0; - - Serial.write((byte *)&mX,2); - Serial.write((byte *)&mY,2); - Serial.write((byte *)&mZ,2); + Serial.print(mX); + Serial.print(","); + Serial.print(mY); + Serial.print(","); + Serial.print(mZ); //Serial.write((byte *)&sX,2); //Serial.write((byte *)&sY,2); //Serial.write((byte *)&sZ,2); @@ -162,6 +163,7 @@ int getData(char*) { } } + int getPosition(char*) { Serial.print(POSITION); Serial.println("#"); diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index eacab696..2ceb178b 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -78,9 +78,8 @@ function checkSerialDevice(gauss::ArduinoGaussMeter, sd::SerialDevice) end function getXYZValues(gauss::ArduinoGaussMeter) - data = zeros(Int16, 3) - sendCommand(gauss.ard, "DATA", data) - #TODO + data_strings = split(sendCommand(gauss.ard, "DATA"), ",") + data = [parse(Int16,str) for str in data_strings] return data end diff --git a/src/Devices/Utils/SerialDevices/SerialDevice.jl b/src/Devices/Utils/SerialDevices/SerialDevice.jl index 1edc6562..2545f0b0 100644 --- a/src/Devices/Utils/SerialDevices/SerialDevice.jl +++ b/src/Devices/Utils/SerialDevices/SerialDevice.jl @@ -144,12 +144,15 @@ end function receiveDelimited(sd::SerialDevice, array::AbstractArray) lock(sd.sdLock) try + println("----------------------------") set_read_timeout(sd.sp, sd.timeout_ms/1000) buf = IOBuffer() done = false while bytesavailable(sd.sp) > 0 || !done c = read(sd.sp, 1) - if c[1] == UInt8(sd.delim_read) + println(c[1]) + if c[1] == UInt8(sd.delim_read) && buf.size == sizeof(array) + println("we did it") done = true break end From b7819c1ad304a1d0f67cfffa9491c174b928ab28 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 6 Oct 2022 15:21:10 +0200 Subject: [PATCH 04/68] fixing hallsensor to use reale Data --- Devices/Arduino/HallSensor/HallSensor.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index beef98ef..d3052cb0 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -141,9 +141,9 @@ int getData(char*) { // TODO perform measurement - int16_t mX = 1; - int16_t mY = 4352; - int16_t mZ = 3; + int16_t mX = sensor.getRawX(); + int16_t mY = sensor.getRawY(); + int16_t mZ = sensor.getRawZ(); int16_t sX=0,sY=0,sZ =0; Serial.print(mX); From de95d05a905a352b5d774448e8d9226dc6d767e7 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 6 Oct 2022 17:21:46 +0200 Subject: [PATCH 05/68] WIP: one way textbook aproch but var is quite off --- Devices/Arduino/HallSensor/HallSensor.ino | 67 ++++++++++++++++------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index d3052cb0..aed8e7a2 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,15 +1,14 @@ #define ARDUINO_TYPE "HALLSENS" #define VERSION "1" #define POSITION 1 -#define size 1024 #define BAUDRATE 9600 #include // Tlv493d Opject Tle493d_w2b6 sensor = Tle493d_w2b6(Tle493d::MASTERCONTROLLEDMODE); -int called = 0; +int sample_size= 30; // Communication -#define INPUT_BUFFER_SIZE 256 +#define INPUT_BUFFER_SIZE 3000 char input_buffer[INPUT_BUFFER_SIZE]; unsigned int input_pos = 0; @@ -134,33 +133,61 @@ int getData(char*) { // updateData reads values from sensor and reading triggers next measurement int ret = sensor.updateData(); // Throw away first old data delay(10); - ret = sensor.updateData(); // Could check ret after each measurement uint16_t measDelay = 10; - unsigned long start = millis(); + unsigned long start, end; // TODO perform measurement + + int32_t mX=0,mY=0,mZ=0,sX=0,sY=0,sZ=0,x=0,y=0,z=0; + for (int i =0 ;i< sample_size ; i++){ + sensor.updateData(); + start = millis(); + + x = sensor.getRawX(); + y = sensor.getRawY(); + z = sensor.getRawZ(); + + mX+=x; + mY+=y; + mZ+=z; - int16_t mX = sensor.getRawX(); - int16_t mY = sensor.getRawY(); - int16_t mZ = sensor.getRawZ(); + Serial.print(x); + Serial.print(","); + + sX += x*x; + sY += y*y; + sZ += z*z; + + end = millis(); + if (end - start < measDelay) { + delay(measDelay-(end - start)); + } + } + + + //sX -= mX*mX/sample_size; + sY -= mY*mY/sample_size; + sZ -= mZ*mZ/sample_size; + + mX = mX/sample_size; + mY = mY/sample_size; + mZ = mZ/sample_size; - int16_t sX=0,sY=0,sZ =0; Serial.print(mX); + //Serial.print(","); + //Serial.print(mY); + //Serial.print(","); + //Serial.print(mZ); Serial.print(","); - Serial.print(mY); - Serial.print(","); - Serial.print(mZ); - //Serial.write((byte *)&sX,2); - //Serial.write((byte *)&sY,2); - //Serial.write((byte *)&sZ,2); + Serial.print(sX); + //Serial.print(","); + //Serial.print(sY); + //Serial.print(","); + //Serial.print(sZ); + //Serial.print(","); Serial.print("#"); Serial.flush(); - - unsigned long end = millis(); - if (end - start < measDelay) { - delay(measDelay-(end - start)); - } } From a9074ff93c644ba4744f13ef2801675bfbbe332c Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 12 Oct 2022 15:34:54 +0200 Subject: [PATCH 06/68] Using one pass textbook --- Devices/Arduino/HallSensor/HallSensor.ino | 61 +++++++++---------- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 2 +- .../Utils/SerialDevices/SerialDevice.jl | 1 + 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index aed8e7a2..3399d060 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -6,7 +6,7 @@ // Tlv493d Opject Tle493d_w2b6 sensor = Tle493d_w2b6(Tle493d::MASTERCONTROLLEDMODE); -int sample_size= 30; +int sample_size= 500; // Communication #define INPUT_BUFFER_SIZE 3000 char input_buffer[INPUT_BUFFER_SIZE]; @@ -139,7 +139,8 @@ int getData(char*) { // TODO perform measurement - int32_t mX=0,mY=0,mZ=0,sX=0,sY=0,sZ=0,x=0,y=0,z=0; + int32_t sumX=0,sumY=0,sumZ=0,sumXX=0,sumYY=0,sumZZ=0,x=0,y=0,z=0; + float varX=0, varY = 0, varZ=0, meanX =0, meanY =0, meanZ =0; for (int i =0 ;i< sample_size ; i++){ sensor.updateData(); start = millis(); @@ -148,44 +149,40 @@ int getData(char*) { y = sensor.getRawY(); z = sensor.getRawZ(); - mX+=x; - mY+=y; - mZ+=z; - - Serial.print(x); - Serial.print(","); + sumX+=x; + sumY+=y; + sumZ+=z; - sX += x*x; - sY += y*y; - sZ += z*z; + sumXX += x*x; + sumYY += y*y; + sumZZ += z*z; end = millis(); if (end - start < measDelay) { delay(measDelay-(end - start)); } } - - - //sX -= mX*mX/sample_size; - sY -= mY*mY/sample_size; - sZ -= mZ*mZ/sample_size; - - mX = mX/sample_size; - mY = mY/sample_size; - mZ = mZ/sample_size; - - Serial.print(mX); - //Serial.print(","); - //Serial.print(mY); - //Serial.print(","); - //Serial.print(mZ); + meanX = (float)sumX/sample_size; + varX = (float) sumXX- (meanX*meanX)*sample_size; + varX = varX/(sample_size-1); + meanY = (float)sumY/sample_size; + varY = (float) sumYY- (meanY*meanY)*sample_size; + varY = varY/(sample_size-1); + meanZ = (float)sumX/sample_size; + varZ = (float) sumZZ- (meanZ*meanZ)*sample_size; + varZ = varZ/(sample_size-1); + + Serial.print(meanX,7); + Serial.print(","); + Serial.print(meanY,7); + Serial.print(","); + Serial.print(meanZ,7); + Serial.print(","); + Serial.print(varX,7); + Serial.print(","); + Serial.print(varY,7); Serial.print(","); - Serial.print(sX); - //Serial.print(","); - //Serial.print(sY); - //Serial.print(","); - //Serial.print(sZ); - //Serial.print(","); + Serial.print(varZ,7); Serial.print("#"); Serial.flush(); } diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 2ceb178b..9bcdb7f2 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -79,7 +79,7 @@ end function getXYZValues(gauss::ArduinoGaussMeter) data_strings = split(sendCommand(gauss.ard, "DATA"), ",") - data = [parse(Int16,str) for str in data_strings] + data = [parse(Float32,str) for str in data_strings] return data end diff --git a/src/Devices/Utils/SerialDevices/SerialDevice.jl b/src/Devices/Utils/SerialDevices/SerialDevice.jl index 2545f0b0..930fed70 100644 --- a/src/Devices/Utils/SerialDevices/SerialDevice.jl +++ b/src/Devices/Utils/SerialDevices/SerialDevice.jl @@ -178,6 +178,7 @@ function query(sd::SerialDevice,cmd) sp_flush(sd.sp, SP_BUF_INPUT) return out finally + sp_flush(sd.sp, SP_BUF_INPUT) unlock(sd.sdLock) end end From 4cb8a1fbc86f4fd618a5a1f9ee27b3530120887b Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 12 Oct 2022 15:38:18 +0200 Subject: [PATCH 07/68] sending each step --- Devices/Arduino/HallSensor/HallSensor.ino | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 3399d060..601b80da 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -146,6 +146,8 @@ int getData(char*) { start = millis(); x = sensor.getRawX(); + print(x); + print(",") y = sensor.getRawY(); z = sensor.getRawZ(); @@ -174,15 +176,15 @@ int getData(char*) { Serial.print(meanX,7); Serial.print(","); - Serial.print(meanY,7); - Serial.print(","); - Serial.print(meanZ,7); - Serial.print(","); + //Serial.print(meanY,7); + //Serial.print(","); + //Serial.print(meanZ,7); + //Serial.print(","); Serial.print(varX,7); - Serial.print(","); - Serial.print(varY,7); - Serial.print(","); - Serial.print(varZ,7); + //Serial.print(","); + //Serial.print(varY,7); + //Serial.print(","); + //Serial.print(varZ,7); Serial.print("#"); Serial.flush(); } From d53d07fe50686a7ced5f818ffbc67b3c3cfc016e Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 12 Oct 2022 16:15:11 +0200 Subject: [PATCH 08/68] Sende jede messung zwecks valdierung --- Devices/Arduino/HallSensor/HallSensor.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 601b80da..e965d419 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -146,8 +146,8 @@ int getData(char*) { start = millis(); x = sensor.getRawX(); - print(x); - print(",") + Serial.print(x); + Serial.print(","); y = sensor.getRawY(); z = sensor.getRawZ(); From 1cdfc0a6cf3aa8709e76d3ba10c7d8431bf08e2a Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 3 Nov 2022 15:17:25 +0100 Subject: [PATCH 09/68] adding funktion to set sample size --- Devices/Arduino/HallSensor/HallSensor.ino | 35 ++++++++++++++++------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index e965d419..40f8be8f 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,5 +1,5 @@ #define ARDUINO_TYPE "HALLSENS" -#define VERSION "1" +#define VERSION "1.2" #define POSITION 1 #define BAUDRATE 9600 #include @@ -17,6 +17,7 @@ int getPosition(char*); int getTemp(char*); int getVersion(char*); int getCommands(char*); +int setSampleSize(char*); //int setFoo(char*) typedef struct { @@ -30,6 +31,7 @@ commandHandler_t cmdHandler[] = { {"TEMP", getTemp}, {"VERSION", getVersion}, {"COMMANDS", getCommands}, + {"SAMPLES", setSampleSize} //{"FOO", setFoo} }; @@ -39,6 +41,7 @@ int getCommands(char*) { Serial.print("'!POS*#' "); Serial.print("'!VERSION*#' "); Serial.print("'!COMMANDS*#' "); + Serial.print("'!SAMPLES*500#' "); Serial.println("#"); } @@ -95,7 +98,7 @@ void serialCommand() { //check for known commands for (int i = 0; i < sizeof(cmdHandler)/sizeof(*cmdHandler); i++) { if (strncmp(cmdHandler[i].id, command, strlen(cmdHandler[i].id)) == 0) { - cmdHandler[i].handler(command); + cmdHandler[i].handler(endCmd+1); unknown = false; input_buffer[0] = '\0'; // "Empty" input buffer break; @@ -176,16 +179,16 @@ int getData(char*) { Serial.print(meanX,7); Serial.print(","); - //Serial.print(meanY,7); - //Serial.print(","); - //Serial.print(meanZ,7); - //Serial.print(","); + Serial.print(meanY,7); + Serial.print(","); + Serial.print(meanZ,7); + Serial.print(","); Serial.print(varX,7); - //Serial.print(","); - //Serial.print(varY,7); - //Serial.print(","); - //Serial.print(varZ,7); - Serial.print("#"); + Serial.print(","); + Serial.print(varY,7); + Serial.print(","); + Serial.print(varZ,7); + Serial.println("#"); Serial.flush(); } @@ -209,6 +212,16 @@ int getVersion(char*) { Serial.flush(); } +int setSampleSize(char* value){ + int value_int = atoi(value); + if (value_int>0){ + sample_size=value_int; + } + Serial.print("Set sample size to:"); + Serial.print(sample_size); + Serial.println("#"); +} + void loop() { From 05e25fc7cbb1c0231c4c5fa98e59a3a7246ac650 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 3 Nov 2022 15:20:14 +0100 Subject: [PATCH 10/68] removing artefaks --- Devices/Arduino/HallSensor/HallSensor.ino | 2 -- 1 file changed, 2 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 40f8be8f..82d071e0 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -149,8 +149,6 @@ int getData(char*) { start = millis(); x = sensor.getRawX(); - Serial.print(x); - Serial.print(","); y = sensor.getRawY(); z = sensor.getRawZ(); From 96d885d96fe832bc4ac237797cd8de02ec4f9274 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 3 Nov 2022 16:49:14 +0100 Subject: [PATCH 11/68] adding julia side to modify sample size --- Devices/Arduino/HallSensor/HallSensor.ino | 9 ++++----- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 5 +++++ src/Devices/Utils/Arduino.jl | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 82d071e0..8674eb6d 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -41,7 +41,7 @@ int getCommands(char*) { Serial.print("'!POS*#' "); Serial.print("'!VERSION*#' "); Serial.print("'!COMMANDS*#' "); - Serial.print("'!SAMPLES*500#' "); + Serial.print("'!SAMPLES500*#' "); Serial.println("#"); } @@ -98,7 +98,7 @@ void serialCommand() { //check for known commands for (int i = 0; i < sizeof(cmdHandler)/sizeof(*cmdHandler); i++) { if (strncmp(cmdHandler[i].id, command, strlen(cmdHandler[i].id)) == 0) { - cmdHandler[i].handler(endCmd+1); + cmdHandler[i].handler(command); unknown = false; input_buffer[0] = '\0'; // "Empty" input buffer break; @@ -210,12 +210,11 @@ int getVersion(char*) { Serial.flush(); } -int setSampleSize(char* value){ - int value_int = atoi(value); +int setSampleSize(char* command){ + int value_int = atoi(command+7); if (value_int>0){ sample_size=value_int; } - Serial.print("Set sample size to:"); Serial.print(sample_size); Serial.println("#"); } diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 9bcdb7f2..cb9f8be0 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -82,5 +82,10 @@ function getXYZValues(gauss::ArduinoGaussMeter) data = [parse(Float32,str) for str in data_strings] return data end +export setSampleSize +function setSampleSize(gauss::ArduinoGaussMeter, samplesize::Int) + data_string = sendCommand(gauss.ard, "SAMPLES" * string(samplesize)) + return parse(Int, data_string) +end close(gauss::ArduinoGaussMeter) = close(gauss.ard) \ No newline at end of file diff --git a/src/Devices/Utils/Arduino.jl b/src/Devices/Utils/Arduino.jl index 331f7a78..f067ae91 100644 --- a/src/Devices/Utils/Arduino.jl +++ b/src/Devices/Utils/Arduino.jl @@ -11,7 +11,7 @@ function sendCommand(ard::Arduino, cmdString::String) cmd = cmdStart(ard) * cmdString * cmdEnd(ard) return query(serialDevice(ard), cmd) end - + function sendCommand(ard::Arduino, cmdString::String, data::AbstractArray) cmd = cmdStart(ard) * cmdString * cmdEnd(ard) return query!(serialDevice(ard), cmd, data, delimited = true) From 0e7804dfba5c1132652c1ceb719835c0fefab27d Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 8 Nov 2022 13:45:32 +0100 Subject: [PATCH 12/68] 2pass for hall sensor --- Devices/Arduino/HallSensor/HallSensor.ino | 45 ++++++++++++------- .../Utils/SerialDevices/SerialDevice.jl | 2 +- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 8674eb6d..ac415953 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -9,7 +9,9 @@ Tle493d_w2b6 sensor = Tle493d_w2b6(Tle493d::MASTERCONTROLLEDMODE); int sample_size= 500; // Communication #define INPUT_BUFFER_SIZE 3000 +#define VALUE_BUFFER_SIZE 1000 char input_buffer[INPUT_BUFFER_SIZE]; +int16_t value_buffer[VALUE_BUFFER_SIZE*3]; unsigned int input_pos = 0; int getData(char*); @@ -137,14 +139,15 @@ int getData(char*) { int ret = sensor.updateData(); // Throw away first old data delay(10); uint16_t measDelay = 10; - unsigned long start, end; + unsigned long start, end,startFP,startSP,endA; // TODO perform measurement - - int32_t sumX=0,sumY=0,sumZ=0,sumXX=0,sumYY=0,sumZZ=0,x=0,y=0,z=0; + int16_t x=0,y=0,z=0; + int32_t sumX=0,sumY=0,sumZ=0,sumXX=0,sumYY=0,sumZZ=0; float varX=0, varY = 0, varZ=0, meanX =0, meanY =0, meanZ =0; - for (int i =0 ;i< sample_size ; i++){ + startFP=millis(); + for (int i =0 ;i< VALUE_BUFFER_SIZE*3 ; i+=3){ sensor.updateData(); start = millis(); @@ -152,29 +155,33 @@ int getData(char*) { y = sensor.getRawY(); z = sensor.getRawZ(); + value_buffer[i] = x; + value_buffer[i+1] = y; + value_buffer[i+2] = z; + sumX+=x; sumY+=y; sumZ+=z; - sumXX += x*x; - sumYY += y*y; - sumZZ += z*z; - end = millis(); if (end - start < measDelay) { delay(measDelay-(end - start)); } } - meanX = (float)sumX/sample_size; - varX = (float) sumXX- (meanX*meanX)*sample_size; - varX = varX/(sample_size-1); - meanY = (float)sumY/sample_size; - varY = (float) sumYY- (meanY*meanY)*sample_size; - varY = varY/(sample_size-1); - meanZ = (float)sumX/sample_size; - varZ = (float) sumZZ- (meanZ*meanZ)*sample_size; - varZ = varZ/(sample_size-1); + meanX = (float)sumX/VALUE_BUFFER_SIZE; + meanY = (float)sumY/VALUE_BUFFER_SIZE; + meanZ = (float)sumX/VALUE_BUFFER_SIZE; + startSP= millis(); + for(int i=0; i Date: Tue, 8 Nov 2022 14:00:58 +0100 Subject: [PATCH 13/68] fixing var calculation --- Devices/Arduino/HallSensor/HallSensor.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index ac415953..d31b1ccd 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -174,9 +174,9 @@ int getData(char*) { meanZ = (float)sumX/VALUE_BUFFER_SIZE; startSP= millis(); for(int i=0; i Date: Tue, 8 Nov 2022 15:23:02 +0100 Subject: [PATCH 14/68] using variable sample size --- Devices/Arduino/HallSensor/HallSensor.ino | 32 +++++++++++------------ 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index d31b1ccd..0c3f5c84 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -6,10 +6,10 @@ // Tlv493d Opject Tle493d_w2b6 sensor = Tle493d_w2b6(Tle493d::MASTERCONTROLLEDMODE); -int sample_size= 500; +int sample_size= 1000; // Communication #define INPUT_BUFFER_SIZE 3000 -#define VALUE_BUFFER_SIZE 1000 +#define VALUE_BUFFER_SIZE 1024 char input_buffer[INPUT_BUFFER_SIZE]; int16_t value_buffer[VALUE_BUFFER_SIZE*3]; unsigned int input_pos = 0; @@ -43,7 +43,7 @@ int getCommands(char*) { Serial.print("'!POS*#' "); Serial.print("'!VERSION*#' "); Serial.print("'!COMMANDS*#' "); - Serial.print("'!SAMPLES500*#' "); + Serial.print("'!SAMPLESx*# 1>=x>=1024' "); Serial.println("#"); } @@ -139,7 +139,7 @@ int getData(char*) { int ret = sensor.updateData(); // Throw away first old data delay(10); uint16_t measDelay = 10; - unsigned long start, end,startFP,startSP,endA; + unsigned long start, end,startFP,endA; // TODO perform measurement @@ -147,7 +147,7 @@ int getData(char*) { int32_t sumX=0,sumY=0,sumZ=0,sumXX=0,sumYY=0,sumZZ=0; float varX=0, varY = 0, varZ=0, meanX =0, meanY =0, meanZ =0; startFP=millis(); - for (int i =0 ;i< VALUE_BUFFER_SIZE*3 ; i+=3){ + for (int i =0 ;i< sample_size*3 ; i+=3){ sensor.updateData(); start = millis(); @@ -169,18 +169,18 @@ int getData(char*) { } } - meanX = (float)sumX/VALUE_BUFFER_SIZE; - meanY = (float)sumY/VALUE_BUFFER_SIZE; - meanZ = (float)sumX/VALUE_BUFFER_SIZE; - startSP= millis(); - for(int i=0; i0){ + if (value_int>0 && value_int<=2048){ sample_size=value_int; } Serial.print(sample_size); From 04b546be650d22a137cd06aefd1c1c8479249606 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 15 Nov 2022 18:23:34 +0100 Subject: [PATCH 15/68] fixing missing float --- Devices/Arduino/HallSensor/HallSensor.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 0c3f5c84..abe7933d 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -174,9 +174,9 @@ int getData(char*) { meanZ = (float)sumX/sample_size; for(int i=0; i Date: Tue, 15 Nov 2022 18:25:14 +0100 Subject: [PATCH 16/68] fixing version number --- Devices/Arduino/HallSensor/HallSensor.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index abe7933d..5918a54f 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,5 +1,5 @@ #define ARDUINO_TYPE "HALLSENS" -#define VERSION "1.2" +#define VERSION "1.3" #define POSITION 1 #define BAUDRATE 9600 #include From 1e6800842a3bfae4ce4cb40526e07d2641aa0696 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 17 Nov 2022 14:45:58 +0100 Subject: [PATCH 17/68] adding tem function --- Devices/Arduino/HallSensor/HallSensor.ino | 22 +++++++++++++++------ src/Devices/GaussMeter/ArduinoGaussMeter.jl | 6 ++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 5918a54f..3c344e87 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -42,6 +42,7 @@ int getCommands(char*) { Serial.print("'!DATA*#' "); Serial.print("'!POS*#' "); Serial.print("'!VERSION*#' "); + Serial.print("'!TEMP*#' "); Serial.print("'!COMMANDS*#' "); Serial.print("'!SAMPLESx*# 1>=x>=1024' "); Serial.println("#"); @@ -131,12 +132,13 @@ void setup() { // *** sensor.begin(); + sensor.enableTemp(); } int getData(char*) { // updateData reads values from sensor and reading triggers next measurement - int ret = sensor.updateData(); // Throw away first old data + sensor.updateData(); // Throw away first old data delay(10); uint16_t measDelay = 10; unsigned long start, end,startFP,endA; @@ -146,7 +148,6 @@ int getData(char*) { int16_t x=0,y=0,z=0; int32_t sumX=0,sumY=0,sumZ=0,sumXX=0,sumYY=0,sumZZ=0; float varX=0, varY = 0, varZ=0, meanX =0, meanY =0, meanZ =0; - startFP=millis(); for (int i =0 ;i< sample_size*3 ; i+=3){ sensor.updateData(); start = millis(); @@ -181,7 +182,7 @@ int getData(char*) { varX =(float)sumXX/sample_size; varY =(float)sumYY/sample_size; varZ =(float)sumXX/sample_size; - endA=millis(); + Serial.print(meanX,7); Serial.print(","); Serial.print(meanY,7); @@ -193,8 +194,6 @@ int getData(char*) { Serial.print(varY,7); Serial.print(","); Serial.print(varZ,7); - Serial.print(","); - Serial.print(endA-startFP); Serial.println("#"); Serial.flush(); } @@ -206,7 +205,18 @@ int getPosition(char*) { } int getTemp(char*) { - // Enable Temp, measure, then disable again + sensor.enableTemp(); + delay(10); + sensor.updateData(); + delay(10); + sensor.updateData(); + delay(10); + float temp = sensor.getTemp(); + Serial.print(temp); + Serial.println("#"); + Serial.flush(); + sensor.disableTemp(); + delay(10); } int getVersion(char*) { diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index cb9f8be0..54f1724e 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -88,4 +88,10 @@ function setSampleSize(gauss::ArduinoGaussMeter, samplesize::Int) return parse(Int, data_string) end +export getTemperature +function getTemperature(gauss::ArduinoGaussMeter) + temp_str = sendCommand(gauss.ard, "TEMP") + return parse(Float32,temp_str) +end + close(gauss::ArduinoGaussMeter) = close(gauss.ard) \ No newline at end of file From 66174dc72178ab650816684ab83b94532a482bb7 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 22 Nov 2022 15:03:06 +0100 Subject: [PATCH 18/68] removed bug in sensor and versiun bump --- Devices/Arduino/HallSensor/HallSensor.ino | 10 +++++----- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 3c344e87..6d8786e1 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,5 +1,5 @@ #define ARDUINO_TYPE "HALLSENS" -#define VERSION "1.3" +#define VERSION "2.1" #define POSITION 1 #define BAUDRATE 9600 #include @@ -132,7 +132,7 @@ void setup() { // *** sensor.begin(); - sensor.enableTemp(); + sensor.disableTemp(); } @@ -172,7 +172,7 @@ int getData(char*) { meanX = (float)sumX/sample_size; meanY = (float)sumY/sample_size; - meanZ = (float)sumX/sample_size; + meanZ = (float)sumZ/sample_size; for(int i=0; i Date: Thu, 24 Nov 2022 11:46:19 +0100 Subject: [PATCH 19/68] removing prints --- src/Devices/Utils/SerialDevices/SerialDevice.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Devices/Utils/SerialDevices/SerialDevice.jl b/src/Devices/Utils/SerialDevices/SerialDevice.jl index a7948672..08da6242 100644 --- a/src/Devices/Utils/SerialDevices/SerialDevice.jl +++ b/src/Devices/Utils/SerialDevices/SerialDevice.jl @@ -144,15 +144,12 @@ end function receiveDelimited(sd::SerialDevice, array::AbstractArray) lock(sd.sdLock) try - println("----------------------------") set_read_timeout(sd.sp, sd.timeout_ms/1000) buf = IOBuffer() done = false while bytesavailable(sd.sp) > 0 || !done c = read(sd.sp, 1) - println(c[1]) if c[1] == UInt8(sd.delim_read) && buf.size == sizeof(array) - println("we did it") done = true break end From 0c4a231911683bd91caf2174caa55d70f80df915 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 24 Nov 2022 11:52:14 +0100 Subject: [PATCH 20/68] enable timeout --- src/Devices/Utils/SerialDevices/SerialDevice.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Devices/Utils/SerialDevices/SerialDevice.jl b/src/Devices/Utils/SerialDevices/SerialDevice.jl index a46cc801..f4be4b2f 100644 --- a/src/Devices/Utils/SerialDevices/SerialDevice.jl +++ b/src/Devices/Utils/SerialDevices/SerialDevice.jl @@ -122,7 +122,7 @@ Read out current content of the output buffer of the serial devive. Returns a St function receive(sd::SerialDevice) lock(sd.sdLock) try - #set_read_timeout(sd.sp, sd.timeout_ms/1000) + set_read_timeout(sd.sp, sd.timeout_ms/1000) reply = readuntil(sd.sp, sd.delim_read) @debug "$(sd.portName) received: $reply" return reply From c48e6b153229d885654a080cb3418ac913ab0ca4 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 29 Nov 2022 16:09:11 +0100 Subject: [PATCH 21/68] fixing timeouts --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 7 +++++++ src/Devices/Utils/Arduino.jl | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 53b77868..6183030b 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -82,8 +82,15 @@ function getXYZValues(gauss::ArduinoGaussMeter) data = [parse(Float32,str) for str in data_strings] return data end + export setSampleSize function setSampleSize(gauss::ArduinoGaussMeter, samplesize::Int) + if(samplesize>1024 || samplesize<1) + throw(error("no valid sample size, pick size from 1 to 1024")) + end + timeout_ms = max(1000,floor(Int,samplesize*10*1.1)+1) + @info("debug"*string(timeout_ms)) + set_timeout_ms(serialDevice(gauss.ard),timeout_ms) data_string = sendCommand(gauss.ard, "SAMPLES" * string(samplesize)) return parse(Int, data_string) end diff --git a/src/Devices/Utils/Arduino.jl b/src/Devices/Utils/Arduino.jl index f067ae91..59fca7d0 100644 --- a/src/Devices/Utils/Arduino.jl +++ b/src/Devices/Utils/Arduino.jl @@ -9,7 +9,8 @@ abstract type Arduino <: Device end function sendCommand(ard::Arduino, cmdString::String) cmd = cmdStart(ard) * cmdString * cmdEnd(ard) - return query(serialDevice(ard), cmd) + sd = serialDevice(ard) + return query(sd, cmd) end function sendCommand(ard::Arduino, cmdString::String, data::AbstractArray) From 0fcd508ffb0f4ac618cd26e7a52061b39f0721e2 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 29 Nov 2022 16:11:46 +0100 Subject: [PATCH 22/68] removing @info from debugging --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 6183030b..6d5f1b2e 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -89,7 +89,6 @@ function setSampleSize(gauss::ArduinoGaussMeter, samplesize::Int) throw(error("no valid sample size, pick size from 1 to 1024")) end timeout_ms = max(1000,floor(Int,samplesize*10*1.1)+1) - @info("debug"*string(timeout_ms)) set_timeout_ms(serialDevice(gauss.ard),timeout_ms) data_string = sendCommand(gauss.ard, "SAMPLES" * string(samplesize)) return parse(Int, data_string) From a0220845d166fcd0daf4a4a9c4465edec6f9fd2d Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 29 Nov 2022 18:32:39 +0100 Subject: [PATCH 23/68] Keeping it abstract, no direkt access on serial device --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 31 +++++++++++++++---- src/Devices/Utils/Arduino.jl | 8 +++++ .../Utils/SerialDevices/SerialDevice.jl | 4 +++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 6d5f1b2e..f4a84672 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -25,16 +25,23 @@ Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams description::String coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) calibration::Vector{Float64} = [0.098, 0.098, 0.098] + sampleSize:: Int @add_serial_device_fields "#" @add_arduino_fields "!" "*" end -ArduinoGaussMeterDescriptionParams(dict::Dict) = params_from_dict(ArduinoGaussMeterDescriptionParams, dict) +function ArduinoGaussMeterDescriptionParams(dict::Dict) + if haskey(dict, "coordinateTransformation") + dict["coordinateTransformation"] = reshape(dict["coordinateTransformation"], 3, 3) + end + params_from_dict(ArduinoGaussMeterDescriptionParams, dict) +end Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter @add_device_fields ArduinoGaussMeterParams ard::Union{SimpleArduino, Nothing} = nothing + sampleSize::Int = 0 end neededDependencies(::ArduinoGaussMeter) = [] @@ -46,6 +53,7 @@ function _init(gauss::ArduinoGaussMeter) @info "Connection to ArduinoGaussMeter established." ard = SimpleArduino(;commandStart = params.commandStart, commandEnd = params.commandEnd, sd = sd) gauss.ard = ard + setSampleSize(gauss, params.sampleSize) end function initSerialDevice(gauss::ArduinoGaussMeter, params::ArduinoGaussMeterDirectParams) @@ -78,22 +86,33 @@ function checkSerialDevice(gauss::ArduinoGaussMeter, sd::SerialDevice) end function getXYZValues(gauss::ArduinoGaussMeter) + temp = get_timeout(gauss.ard) + timeout_ms = max(1000,floor(Int,gauss.sampleSize*10*1.2)+1) + @info(timeout_ms) + set_timeout(gauss.ard, timeout_ms) data_strings = split(sendCommand(gauss.ard, "DATA"), ",") data = [parse(Float32,str) for str in data_strings] + set_timeout(gauss.ard, temp) return data end export setSampleSize -function setSampleSize(gauss::ArduinoGaussMeter, samplesize::Int) - if(samplesize>1024 || samplesize<1) +function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) + if(sampleSize>1024 || sampleSize<1) throw(error("no valid sample size, pick size from 1 to 1024")) end - timeout_ms = max(1000,floor(Int,samplesize*10*1.1)+1) - set_timeout_ms(serialDevice(gauss.ard),timeout_ms) - data_string = sendCommand(gauss.ard, "SAMPLES" * string(samplesize)) + gauss.sampleSize = sampleSize + data_string = sendCommand(gauss.ard, "SAMPLES" * string(sampleSize)) return parse(Int, data_string) end +export getSampleSize +function getSampleSize(gauss::ArduinoGaussMeter) + return gauss.sampleSize +end + + + export getTemperature function getTemperature(gauss::ArduinoGaussMeter) temp_str = sendCommand(gauss.ard, "TEMP") diff --git a/src/Devices/Utils/Arduino.jl b/src/Devices/Utils/Arduino.jl index 59fca7d0..17572181 100644 --- a/src/Devices/Utils/Arduino.jl +++ b/src/Devices/Utils/Arduino.jl @@ -18,6 +18,14 @@ function sendCommand(ard::Arduino, cmdString::String, data::AbstractArray) return query!(serialDevice(ard), cmd, data, delimited = true) end +function set_timeout(ard::Arduino,timeout_ms::Int) + set_timeout_ms(ard.sd,timeout_ms) +end + +function get_timeout(ard::Arduino) + return get_timeout_ms(ard.sd) +end + Base.@kwdef struct SimpleArduino <: Arduino commandStart::String = "!" commandEnd::String = "*" diff --git a/src/Devices/Utils/SerialDevices/SerialDevice.jl b/src/Devices/Utils/SerialDevices/SerialDevice.jl index f4be4b2f..d42fe50f 100644 --- a/src/Devices/Utils/SerialDevices/SerialDevice.jl +++ b/src/Devices/Utils/SerialDevices/SerialDevice.jl @@ -67,6 +67,10 @@ function set_timeout_ms(sd::SerialDevice,timeout_ms::Int) return nothing end +function get_timeout_ms(sd::SerialDevice) + return sd.timeout_ms +end + """ Set character which terminates query. """ From d7ec15b7f01065b6842d39928cfb66ea3973c0b0 Mon Sep 17 00:00:00 2001 From: nHackel Date: Wed, 30 Nov 2022 09:30:07 +0100 Subject: [PATCH 24/68] Added TDesignCube skeleton --- src/Devices/GaussMeter/GaussMeter.jl | 1 + src/Devices/GaussMeter/TDesignCube.jl | 40 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/Devices/GaussMeter/TDesignCube.jl diff --git a/src/Devices/GaussMeter/GaussMeter.jl b/src/Devices/GaussMeter/GaussMeter.jl index f8df01fe..d4bc38c2 100644 --- a/src/Devices/GaussMeter/GaussMeter.jl +++ b/src/Devices/GaussMeter/GaussMeter.jl @@ -40,3 +40,4 @@ include("DummyGaussMeter.jl") include("SimulatedGaussMeter.jl") include("LakeShore.jl") include("ArduinoGaussMeter.jl") +include("TDesginCube.jl") diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl new file mode 100644 index 00000000..114ebd5c --- /dev/null +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -0,0 +1,40 @@ +export + +Base.@kwdef struct TDesignCubeParams <: DeviceParams + T::Int64 + N::Int64 + radius::typeof(1.0u"mm") = 0.0u"mm" + samplesSize::Union{Nothing, Int64} = nothing # Optional overwrite +end + +Base.@kwdef mutable struct TDesignCube + @add_device_fields TDesignCubeParams + sensors::Union{Vector{ArduinoGaussMeter}, Nothing} = nothing +end + +neededDependencies(::ArduinoGaussMeter) = [ArduinoGaussMeter] +optionalDependencies(::ArduinoGaussMeter) = [] + +function _init(cube::TDesignCube) + sensors = dependencies(cube, ArduinoGaussMeter) + # TODO Check if all needed sensors are there + # TODO Sort sensors according to position + cube.sensors = sensors + if !isnothing(cube.params.sampleSize) + # TODO overwrite samplesize of sensors + end +end + +# TODO get/setSampleSize + +function getXYZValues(cube::TDesignCube) + measurement = zeros(typeof(u"T"), 3, cube.params.T) + # TODO Implement this with start/receive from sensors + return measurement +end + +# TODO implement start and receive like with sensor as (maybe async) for loops + +function close(cube::TDesignCube) + # NOP +end \ No newline at end of file From 0ae9dfaf0acb6bf27415689f41118c5e02a9366c Mon Sep 17 00:00:00 2001 From: nHackel Date: Wed, 30 Nov 2022 10:56:47 +0100 Subject: [PATCH 25/68] Added inital skeleton for TDesignProtocol --- src/Devices/GaussMeter/TDesignCube.jl | 3 +- src/Protocols/Protocol.jl | 3 +- src/Protocols/TDesignCubeProtocol.jl | 174 ++++++++++++++++++++++++++ 3 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 src/Protocols/TDesignCubeProtocol.jl diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 114ebd5c..be58a8d3 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -1,4 +1,4 @@ -export +export TDesignCubeParams, TDesignCube Base.@kwdef struct TDesignCubeParams <: DeviceParams T::Int64 @@ -34,6 +34,7 @@ function getXYZValues(cube::TDesignCube) end # TODO implement start and receive like with sensor as (maybe async) for loops +# TODO implement "getters" for T, N, radius function close(cube::TDesignCube) # NOP diff --git a/src/Protocols/Protocol.jl b/src/Protocols/Protocol.jl index afcca78a..7bda4838 100644 --- a/src/Protocols/Protocol.jl +++ b/src/Protocols/Protocol.jl @@ -319,4 +319,5 @@ include("MPIMeasurementProtocol.jl") include("RobotMPIMeasurementProtocol.jl") include("RobotBasedProtocol.jl") include("ContinousMeasurementProtocol.jl") -#include("TransferFunctionProtocol.jl") \ No newline at end of file +#include("TransferFunctionProtocol.jl") +include("TDesignCubeProtocol.jl") \ No newline at end of file diff --git a/src/Protocols/TDesignCubeProtocol.jl b/src/Protocols/TDesignCubeProtocol.jl new file mode 100644 index 00000000..4d360a01 --- /dev/null +++ b/src/Protocols/TDesignCubeProtocol.jl @@ -0,0 +1,174 @@ +export TDesignCubeProtocolParams, TDesignCubeProtocol + +Base.@kwdef mutable struct TDesignCubeProtocolParams <: ProtocolParams + sequence::Union{Sequence, Nothing} = nothing + center::ScannerCoords = ScannerCoords([[0.0u"mm", 0.0u"mm", 0.0u"mm"]]) +end +function TDesignCubeProtocolParams(dict::Dict, scanner::MPIScanner) + sequence = nothing + if haskey(dict, "sequence") + sequence = Sequence(scanner, dict["sequence"]) + dict["sequence"] = sequence + delete!(dict, "sequence") + end + + params = params_from_dict(TDesignCubeProtocolParams, dict) + params.sequence = sequence + return params + end +end + +Base.@kwdef mutable struct TDesignCubeProtocol <: Protocol + finishAcknowledged::Bool = false + measurement::Union{Matrix{Float64}, Nothing} = nothing + tDesign::Union{SphericalTDesign, Nothing} = nothing +end + +requiredDevices(protocol::TDesignCubeProtocol) = [TDesignCube, AbstractDAQ] + +function _init(protocol::TDesignCubeProtocol) + if isnothing(protocol.params.sequence) + throw(IllegalStateException("Protocol requires a sequence")) + end + cube = getDevice(protocol.scanner, TDesignCube) + # TODO get T, N, radius from TDesignCube + protocol.tDesign = loadTDesign(T, N, radius, protocol.params.center.data) + protocol.measurement = zeros(Float64, 3, length(protocol.tDesign)) + end +end + +function enterExecute(protocol::TDesignCubeProtocol) + protocol.done = false + protocol.cancelled = false + protocol.finishAcknowledged = false + protocol.unit = "" +end + +function _execute(protocol::TDesignCubeProtocol) + @debug "TDesignCube protocol started" + + performMeasurement(protocol) + + put!(protocol.biChannel, FinishedNotificationEvent()) + + debugCount = 0 + + while !(protocol.finishAcknowledged) + handleEvents(protocol) + protocol.cancelled && throw(CancelException()) + end + + @info "Protocol finished." + close(protocol.biChannel) + @debug "Protocol channel closed after execution." +end + +function performMeasurement(protocol::TDesignCubeProtocol) + cube = getDevice(scanner(protocol), TDesignCube) + producer = @tspawnat protocol.scanner.generalParams.producerThreadID measurement(protocol) + while !istaskdone(producer) + handleEvents(protocol) + # Dont want to throw cancel here + sleep(0.05) + end + + if Base.istaskfailed(producer) + currExceptions = current_exceptions(producer) + @error "Measurement failed" exception = (currExceptions[end][:exception], stacktrace(currExceptions[end][:backtrace])) + for i in 1:length(currExceptions) - 1 + stack = currExceptions[i] + @error stack[:exception] trace = stacktrace(stack[:backtrace]) + end + ex = currExceptions[1][:exception] + throw(ex) + end +end + +function startMeasurement(protocol::RobotBasedTDesignFieldProtocol) + daq = getDAQ(protocol.scanner) + su = getSurveillanceUnit(protocol.scanner) + tempControl = getTemperatureController(protocol.scanner) + amps = getDevices(protocol.scanner, Amplifier) + if !isempty(amps) + # Only enable amps that amplify a channel of the current sequence + channelIdx = id.(vcat(acyclicElectricalTxChannels(protocol.params.sequence), periodicElectricalTxChannels(protocol.params.sequence))) + amps = filter(amp -> in(channelId(amp), channelIdx), amps) + end + if !isnothing(su) + enableACPower(su) + end + if !isnothing(tempControl) + disableControl(tempControl) + end + @sync for amp in amps + @async turnOn(amp) + end + startTx(daq) + current = 0 + # Wait for measurement proper frame to start + while current < timing.start + current = currentWP(daq.rpc) + sleep(0.01) + end +end + +function stopMeasurement(protocol::RobotBasedTDesignFieldProtocol) + daq = getDAQ(protocol.scanner) + su = getSurveillanceUnit(protocol.scanner) + tempControl = getTemperatureController(protocol.scanner) + amps = getDevices(protocol.scanner, Amplifier) + if !isempty(amps) + # Only disable amps that amplify a channel of the current sequence + channelIdx = id.(vcat(acyclicElectricalTxChannels(protocol.params.sequence), periodicElectricalTxChannels(protocol.params.sequence))) + amps = filter(amp -> in(channelId(amp), channelIdx), amps) + end + timing = getTiming(daq) + @show timing + endSequence(daq, timing.finish) + @sync for amp in amps + @async turnOff(amp) + end + if !isnothing(tempControl) + enableControl(tempControl) + end + if !isnothing(su) + disableACPower(su) + end +end + +function measurement(protocol::RobotBasedTDesignFieldProtocol) + daq = getDAQ(protocol.scanner) + startMeasurement(protocol) + cube = getDevice(scanner(protocol), TDesignCube) + + field = getXYZValues(protocol, cube) + timing = getTiming(daq) + current = currentWP(daq.rpc) + if current > timing.down + @warn current + @warn "Magnetic field was measured too late" + end + stopMeasurement(protocol) + protocol.measurement = ustrip.(u"T", field) +end + +handleEvent(protocol::RobotBasedTDesignFieldProtocol, event::FinishedAckEvent) = protocol.finishAcknowledged = true + +function handleEvent(protocol::RobotBasedTDesignFieldProtocol, event::FileStorageRequestEvent) + filename = event.filename + h5open(filename, "w") do file + write(file,"/fields", protocol.measurement) # measured field (size: 3 x #points x #patches) + # TODO get T, N, radius from TDesignCube + write(file,"/positions/tDesign/radius", ustrip(u"m", radius)) # radius of the measured ball + write(file,"/positions/tDesign/N", N) # number of points of the t-design + write(file,"/positions/tDesign/t", T) # t of the t-design + write(file,"/positions/tDesign/center", ustrip.(u"m", protocol.params.center.data)) # center of the measured ball + #write(file, "/sensor/correctionTranslation", getGaussMeter(protocol.scanner).params.sensorCorrectionTranslation) # TODO How to handle this for the cube? + end + put!(protocol.biChannel, StorageSuccessEvent(filename)) +end + +function cleanup(protocol::RobotBasedTDesignFieldProtocol) + # NOP +end + From c2042e3ed22aef41e0c6cd4cabffe0df623ffe92 Mon Sep 17 00:00:00 2001 From: nHackel Date: Wed, 30 Nov 2022 12:08:43 +0100 Subject: [PATCH 26/68] More todos --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 14 ++++++++++++++ src/Devices/GaussMeter/TDesignCube.jl | 11 +++-------- src/Protocols/TDesignCubeProtocol.jl | 9 ++++----- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index f4a84672..c0688a91 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -1,8 +1,19 @@ export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams abstract type ArduinoGaussMeterParams <: DeviceParams end +# Params need more paramters +# TODO three matrices: +# calibration: 3x3 <- from calibration measurement (in hall sensor coords) +# rotation: 3x3 <- how the sensor is rotated in the cube +# translation: 3x3 <- spatial offset of each sensor (for now 0) +# TODO each params needs a position value +# TODO "getter" for rotated translation + Base.@kwdef struct ArduinoGaussMeterDirectParams <: ArduinoGaussMeterParams portAddress::String + position::Int64 = 1 + calibration::Matrix{Float64} + rotation::Matrix{Float64} coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) calibration::Vector{Float64} = [0.098, 0.098, 0.098] @@ -41,6 +52,7 @@ end Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter @add_device_fields ArduinoGaussMeterParams ard::Union{SimpleArduino, Nothing} = nothing + rotatedCalibration::Matrix{Float64} sampleSize::Int = 0 end @@ -53,6 +65,7 @@ function _init(gauss::ArduinoGaussMeter) @info "Connection to ArduinoGaussMeter established." ard = SimpleArduino(;commandStart = params.commandStart, commandEnd = params.commandEnd, sd = sd) gauss.ard = ard + gauss.rotatedCalibration = params.rotation * params.calibration setSampleSize(gauss, params.sampleSize) end @@ -91,6 +104,7 @@ function getXYZValues(gauss::ArduinoGaussMeter) @info(timeout_ms) set_timeout(gauss.ard, timeout_ms) data_strings = split(sendCommand(gauss.ard, "DATA"), ",") + # TODO use rotatedCalibration * data data = [parse(Float32,str) for str in data_strings] set_timeout(gauss.ard, temp) return data diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index be58a8d3..14e771a7 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -4,10 +4,9 @@ Base.@kwdef struct TDesignCubeParams <: DeviceParams T::Int64 N::Int64 radius::typeof(1.0u"mm") = 0.0u"mm" - samplesSize::Union{Nothing, Int64} = nothing # Optional overwrite end -Base.@kwdef mutable struct TDesignCube +Base.@kwdef mutable struct TDesignCube <: Device @add_device_fields TDesignCubeParams sensors::Union{Vector{ArduinoGaussMeter}, Nothing} = nothing end @@ -17,15 +16,12 @@ optionalDependencies(::ArduinoGaussMeter) = [] function _init(cube::TDesignCube) sensors = dependencies(cube, ArduinoGaussMeter) - # TODO Check if all needed sensors are there + # TODO Check if all needed sensors are there if not throw ScannerConfigurationError # TODO Sort sensors according to position cube.sensors = sensors - if !isnothing(cube.params.sampleSize) - # TODO overwrite samplesize of sensors - end end -# TODO get/setSampleSize +# TODO get/setSampleSizes function getXYZValues(cube::TDesignCube) measurement = zeros(typeof(u"T"), 3, cube.params.T) @@ -33,7 +29,6 @@ function getXYZValues(cube::TDesignCube) return measurement end -# TODO implement start and receive like with sensor as (maybe async) for loops # TODO implement "getters" for T, N, radius function close(cube::TDesignCube) diff --git a/src/Protocols/TDesignCubeProtocol.jl b/src/Protocols/TDesignCubeProtocol.jl index 4d360a01..6e84a9a3 100644 --- a/src/Protocols/TDesignCubeProtocol.jl +++ b/src/Protocols/TDesignCubeProtocol.jl @@ -3,6 +3,7 @@ export TDesignCubeProtocolParams, TDesignCubeProtocol Base.@kwdef mutable struct TDesignCubeProtocolParams <: ProtocolParams sequence::Union{Sequence, Nothing} = nothing center::ScannerCoords = ScannerCoords([[0.0u"mm", 0.0u"mm", 0.0u"mm"]]) + samplesSize::Union{Nothing, Int64} = nothing # Optional overwrite end function TDesignCubeProtocolParams(dict::Dict, scanner::MPIScanner) sequence = nothing @@ -19,6 +20,7 @@ function TDesignCubeProtocolParams(dict::Dict, scanner::MPIScanner) end Base.@kwdef mutable struct TDesignCubeProtocol <: Protocol + @add_protocol_fields TDesignCubeProtocolParams finishAcknowledged::Bool = false measurement::Union{Matrix{Float64}, Nothing} = nothing tDesign::Union{SphericalTDesign, Nothing} = nothing @@ -38,10 +40,7 @@ function _init(protocol::TDesignCubeProtocol) end function enterExecute(protocol::TDesignCubeProtocol) - protocol.done = false - protocol.cancelled = false protocol.finishAcknowledged = false - protocol.unit = "" end function _execute(protocol::TDesignCubeProtocol) @@ -140,7 +139,7 @@ function measurement(protocol::RobotBasedTDesignFieldProtocol) daq = getDAQ(protocol.scanner) startMeasurement(protocol) cube = getDevice(scanner(protocol), TDesignCube) - + # TODO overwrite samplesSize if given field = getXYZValues(protocol, cube) timing = getTiming(daq) current = currentWP(daq.rpc) @@ -163,7 +162,7 @@ function handleEvent(protocol::RobotBasedTDesignFieldProtocol, event::FileStorag write(file,"/positions/tDesign/N", N) # number of points of the t-design write(file,"/positions/tDesign/t", T) # t of the t-design write(file,"/positions/tDesign/center", ustrip.(u"m", protocol.params.center.data)) # center of the measured ball - #write(file, "/sensor/correctionTranslation", getGaussMeter(protocol.scanner).params.sensorCorrectionTranslation) # TODO How to handle this for the cube? + #write(file, "/sensor/correctionTranslation", getGaussMeter(protocol.scanner).params.sensorCorrectionTranslation) # TODO Write 3x3xN rotated translations end put!(protocol.biChannel, StorageSuccessEvent(filename)) end From 8094dfcd649bb49a8269fe8aaac178d1a296ff47 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 30 Nov 2022 14:29:39 +0100 Subject: [PATCH 27/68] appying caliration adding function for raw data --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 25 ++++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index f4a84672..c83a0602 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -4,7 +4,7 @@ abstract type ArduinoGaussMeterParams <: DeviceParams end Base.@kwdef struct ArduinoGaussMeterDirectParams <: ArduinoGaussMeterParams portAddress::String coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - calibration::Vector{Float64} = [0.098, 0.098, 0.098] + biasCalibration = Vector{Float64} = [0.098, 0.098, 0.098] @add_serial_device_fields "#" @add_arduino_fields "!" "*" @@ -14,7 +14,7 @@ ArduinoGaussMeterDirectParams(dict::Dict) = params_from_dict(ArduinoGaussMeterDi Base.@kwdef struct ArduinoGaussMeterPoolParams <: ArduinoGaussMeterParams position::Int64 coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - calibration::Vector{Float64} = [0.098, 0.098, 0.098] + biasCalibration = Vector{Float64} = [0.098, 0.098, 0.098] @add_serial_device_fields "#" @add_arduino_fields "!" "*" @@ -24,16 +24,19 @@ ArduinoGaussMeterPoolParams(dict::Dict) = params_from_dict(ArduinoGaussMeterPool Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams description::String coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - calibration::Vector{Float64} = [0.098, 0.098, 0.098] + biasCalibration = Vector{Float64} = [0.098, 0.098, 0.098] sampleSize:: Int @add_serial_device_fields "#" @add_arduino_fields "!" "*" end + function ArduinoGaussMeterDescriptionParams(dict::Dict) if haskey(dict, "coordinateTransformation") dict["coordinateTransformation"] = reshape(dict["coordinateTransformation"], 3, 3) - end + elseif haskey(dict,"biasCalibration") + dict["coordinateTransformation"] = reshape(dict["biasCalibration"], 3, 1) + end params_from_dict(ArduinoGaussMeterDescriptionParams, dict) end @@ -85,10 +88,10 @@ function checkSerialDevice(gauss::ArduinoGaussMeter, sd::SerialDevice) end end -function getXYZValues(gauss::ArduinoGaussMeter) +export getRawXYZValues +function getRawXYZValues(gauss::ArduinoGaussMeter) temp = get_timeout(gauss.ard) timeout_ms = max(1000,floor(Int,gauss.sampleSize*10*1.2)+1) - @info(timeout_ms) set_timeout(gauss.ard, timeout_ms) data_strings = split(sendCommand(gauss.ard, "DATA"), ",") data = [parse(Float32,str) for str in data_strings] @@ -96,6 +99,16 @@ function getXYZValues(gauss::ArduinoGaussMeter) return data end +function getXYZValues(gauss::ArduinoGaussMeter) + data = getRawXYZValues(gauss) + means = data[1:3] + calibrated_data = gauss.params.coordinateTransformation * means + gauss.params.biasCalibration + data = vcat(calibrated_data,data[4:6]) + return data + +end + + export setSampleSize function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) if(sampleSize>1024 || sampleSize<1) From 815f63318ada77cc3d3e3d1a0e467c3cb3645f19 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 30 Nov 2022 16:46:28 +0100 Subject: [PATCH 28/68] comment out code --- src/Protocols/TDesignCubeProtocol.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Protocols/TDesignCubeProtocol.jl b/src/Protocols/TDesignCubeProtocol.jl index 6e84a9a3..37f813c5 100644 --- a/src/Protocols/TDesignCubeProtocol.jl +++ b/src/Protocols/TDesignCubeProtocol.jl @@ -1,3 +1,4 @@ +#= export TDesignCubeProtocolParams, TDesignCubeProtocol Base.@kwdef mutable struct TDesignCubeProtocolParams <: ProtocolParams @@ -171,3 +172,4 @@ function cleanup(protocol::RobotBasedTDesignFieldProtocol) # NOP end +=# \ No newline at end of file From 0e7225472039d8c9996a9ec2b5b3628c6ca2057d Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 6 Dec 2022 13:31:21 +0100 Subject: [PATCH 29/68] adding new prameter --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 47 ++++++++------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 69f14efd..ae71a3c5 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -1,41 +1,30 @@ export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams abstract type ArduinoGaussMeterParams <: DeviceParams end -# Params need more paramters -# TODO three matrices: -# calibration: 3x3 <- from calibration measurement (in hall sensor coords) -# rotation: 3x3 <- how the sensor is rotated in the cube -# translation: 3x3 <- spatial offset of each sensor (for now 0) -# TODO each params needs a position value -# TODO "getter" for rotated translation Base.@kwdef struct ArduinoGaussMeterDirectParams <: ArduinoGaussMeterParams portAddress::String position::Int64 = 1 - calibration::Matrix{Float64} - rotation::Matrix{Float64} + calibration::Matrix{Float64} = Matrix{Float64}(I,(3,3)) + rotation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) + translation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) biasCalibration = Vector{Float64} = [0.098, 0.098, 0.098] - + sampleSize::Int @add_serial_device_fields "#" @add_arduino_fields "!" "*" end ArduinoGaussMeterDirectParams(dict::Dict) = params_from_dict(ArduinoGaussMeterDirectParams, dict) -Base.@kwdef struct ArduinoGaussMeterPoolParams <: ArduinoGaussMeterParams - position::Int64 - coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - biasCalibration = Vector{Float64} = [0.098, 0.098, 0.098] - - @add_serial_device_fields "#" - @add_arduino_fields "!" "*" -end -ArduinoGaussMeterPoolParams(dict::Dict) = params_from_dict(ArduinoGaussMeterPoolParams, dict) Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams description::String + position::Int64 = 1 + calibration::Matrix{Float64} = Matrix{Float64}(I,(3,3)) + rotation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) + translation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - biasCalibration = Vector{Float64} = [0.098, 0.098, 0.098] + biasCalibration::Vector{Float64} = [0.098, 0.098, 0.098] sampleSize:: Int @add_serial_device_fields "#" @@ -43,10 +32,14 @@ Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams end function ArduinoGaussMeterDescriptionParams(dict::Dict) - if haskey(dict, "coordinateTransformation") - dict["coordinateTransformation"] = reshape(dict["coordinateTransformation"], 3, 3) - elseif haskey(dict,"biasCalibration") - dict["coordinateTransformation"] = reshape(dict["biasCalibration"], 3, 1) + if haskey(dict, "translation") + dict["translation"] = Float64.(reshape(dict["translation"], 3, 3)) + end + if haskey(dict, "rotation") + dict["rotation"] = Float64.(reshape(dict["rotation"], 3, 3)) + end + if haskey(dict, "calibration") + dict["calibration"] = Float64.(reshape(dict["calibration"], 3, 3)) end params_from_dict(ArduinoGaussMeterDescriptionParams, dict) end @@ -55,7 +48,7 @@ end Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter @add_device_fields ArduinoGaussMeterParams ard::Union{SimpleArduino, Nothing} = nothing - rotatedCalibration::Matrix{Float64} + rotatedCalibration::Matrix{Float64} = Matrix{Float64}(I,(3,3)) sampleSize::Int = 0 end @@ -78,10 +71,6 @@ function initSerialDevice(gauss::ArduinoGaussMeter, params::ArduinoGaussMeterDir return sd end -function initSerialDevice(gauss::ArduinoGaussMeter, params::ArduinoGaussMeterPoolParams) - return initSerialDevice(gauss, "!VERSION*", "HALLSENS:1:$(params.position)") -end - function initSerialDevice(gauss::ArduinoGaussMeter, params::ArduinoGaussMeterDescriptionParams) sd = initSerialDevice(gauss, params.description) checkSerialDevice(gauss, sd) From 6f85260751098da0b3c1cdfb248d822f645360d9 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 6 Dec 2022 13:32:04 +0100 Subject: [PATCH 30/68] commit out unused files --- src/Devices/GaussMeter/GaussMeter.jl | 2 +- src/Devices/GaussMeter/TDesignCube.jl | 6 ++++-- src/Protocols/Protocol.jl | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Devices/GaussMeter/GaussMeter.jl b/src/Devices/GaussMeter/GaussMeter.jl index d4bc38c2..33f686b5 100644 --- a/src/Devices/GaussMeter/GaussMeter.jl +++ b/src/Devices/GaussMeter/GaussMeter.jl @@ -40,4 +40,4 @@ include("DummyGaussMeter.jl") include("SimulatedGaussMeter.jl") include("LakeShore.jl") include("ArduinoGaussMeter.jl") -include("TDesginCube.jl") +#include("TDesginCube.jl") diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 14e771a7..18beffc4 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -1,4 +1,5 @@ -export TDesignCubeParams, TDesignCube + +#=export TDesignCubeParams, TDesignCube Base.@kwdef struct TDesignCubeParams <: DeviceParams T::Int64 @@ -33,4 +34,5 @@ end function close(cube::TDesignCube) # NOP -end \ No newline at end of file +end +=# \ No newline at end of file diff --git a/src/Protocols/Protocol.jl b/src/Protocols/Protocol.jl index 7bda4838..36b6d4dc 100644 --- a/src/Protocols/Protocol.jl +++ b/src/Protocols/Protocol.jl @@ -320,4 +320,4 @@ include("RobotMPIMeasurementProtocol.jl") include("RobotBasedProtocol.jl") include("ContinousMeasurementProtocol.jl") #include("TransferFunctionProtocol.jl") -include("TDesignCubeProtocol.jl") \ No newline at end of file +#include("TDesignCubeProtocol.jl") \ No newline at end of file From 03f920d3c5892f3d8461f6faa3f7e2e6a63df8ac Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 7 Dec 2022 12:20:45 +0100 Subject: [PATCH 31/68] using calibration for variance --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 6 ++++-- src/Devices/GaussMeter/GaussMeter.jl | 2 +- src/Devices/GaussMeter/TDesignCube.jl | 17 ++++++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index ae71a3c5..30ad13a6 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -105,8 +105,10 @@ end function getXYZValues(gauss::ArduinoGaussMeter) data = getRawXYZValues(gauss) means = data[1:3] - calibrated_data = gauss.params.coordinateTransformation * means + gauss.params.biasCalibration - data = vcat(calibrated_data,data[4:6]) + var = data[4:6] + calibrated_means = gauss.params.coordinateTransformation * means + gauss.params.biasCalibration + calibrated_var = gauss.params.coordinateTransformation*gauss.params.coordinateTransformation*var + data = vcat(calibrated_means,calibrated_var) return data end diff --git a/src/Devices/GaussMeter/GaussMeter.jl b/src/Devices/GaussMeter/GaussMeter.jl index 33f686b5..2f408fd4 100644 --- a/src/Devices/GaussMeter/GaussMeter.jl +++ b/src/Devices/GaussMeter/GaussMeter.jl @@ -40,4 +40,4 @@ include("DummyGaussMeter.jl") include("SimulatedGaussMeter.jl") include("LakeShore.jl") include("ArduinoGaussMeter.jl") -#include("TDesginCube.jl") +include("TDesignCube.jl") diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 18beffc4..1fb1403a 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -1,24 +1,28 @@ - -#=export TDesignCubeParams, TDesignCube +export TDesignCubeParams, TDesignCube Base.@kwdef struct TDesignCubeParams <: DeviceParams T::Int64 N::Int64 radius::typeof(1.0u"mm") = 0.0u"mm" end +TDesignCubeParams(dict::Dict) = params_from_dict(TDesignCubeParams, dict) + Base.@kwdef mutable struct TDesignCube <: Device @add_device_fields TDesignCubeParams sensors::Union{Vector{ArduinoGaussMeter}, Nothing} = nothing end -neededDependencies(::ArduinoGaussMeter) = [ArduinoGaussMeter] -optionalDependencies(::ArduinoGaussMeter) = [] +neededDependencies(::TDesignCube) = [ArduinoGaussMeter] +optionalDependencies(::TDesignCube) = [] function _init(cube::TDesignCube) sensors = dependencies(cube, ArduinoGaussMeter) - # TODO Check if all needed sensors are there if not throw ScannerConfigurationError - # TODO Sort sensors according to position + if length(sensors) != 4 + throw("missing Sensors") + end + + sort!(sensors,by=x-> x.parms.position) cube.sensors = sensors end @@ -35,4 +39,3 @@ end function close(cube::TDesignCube) # NOP end -=# \ No newline at end of file From b2104dc264231f61fdd02977f518656cfd665bc2 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 7 Dec 2022 14:00:20 +0100 Subject: [PATCH 32/68] getGaussMeter for Scanner --- src/Devices/GaussMeter/GaussMeter.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Devices/GaussMeter/GaussMeter.jl b/src/Devices/GaussMeter/GaussMeter.jl index 2f408fd4..45f32fa8 100644 --- a/src/Devices/GaussMeter/GaussMeter.jl +++ b/src/Devices/GaussMeter/GaussMeter.jl @@ -9,6 +9,9 @@ getGaussMeters(scanner::MPIScanner) = getDevices(scanner, GaussMeter) export getGaussMeter getGaussMeter(scanner::MPIScanner) = getDevice(scanner, GaussMeter) +export getCube +getCube(scanner::MPIScanner) = getDevice(scanner,TDesignCube) + export getXValue @mustimplement getXValue(gauss::GaussMeter) From bfabb0659989432f6ceb181f9ee22976df86ba7d Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 7 Dec 2022 14:00:37 +0100 Subject: [PATCH 33/68] Working Cube --- src/Devices/GaussMeter/TDesignCube.jl | 38 ++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 1fb1403a..572a253c 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -1,9 +1,10 @@ -export TDesignCubeParams, TDesignCube +export TDesignCubeParams, TDesignCube, setSampleSize, getSampleSize, getT, getN, getRadius Base.@kwdef struct TDesignCubeParams <: DeviceParams T::Int64 N::Int64 radius::typeof(1.0u"mm") = 0.0u"mm" + sampleSize:: Int64 = 100 end TDesignCubeParams(dict::Dict) = params_from_dict(TDesignCubeParams, dict) @@ -11,31 +12,50 @@ TDesignCubeParams(dict::Dict) = params_from_dict(TDesignCubeParams, dict) Base.@kwdef mutable struct TDesignCube <: Device @add_device_fields TDesignCubeParams sensors::Union{Vector{ArduinoGaussMeter}, Nothing} = nothing + sampleSize:: Int64 = 100 end neededDependencies(::TDesignCube) = [ArduinoGaussMeter] optionalDependencies(::TDesignCube) = [] function _init(cube::TDesignCube) + sampleSize = cube.params.sampleSize sensors = dependencies(cube, ArduinoGaussMeter) - if length(sensors) != 4 + if length(sensors) != cube.params.T throw("missing Sensors") end - - sort!(sensors,by=x-> x.parms.position) + sort!(sensors,by=x-> x.params.position) cube.sensors = sensors + println(cube.params) + setSampleSize(cube,cube.sampleSize) end -# TODO get/setSampleSizes +export setSampleSize +function setSampleSize(cube::TDesignCube,sampleSize::Int) + if sampleSize>2000 || sampleSize<1 + throw("sampleSize must be in 1:2000") + end + for sensor in cube.sensors + setSampleSize(sensor,sampleSize) + end + cube.sampleSize = sampleSize +end + +export getSampleSize +getSampleSize(cube::TDesignCube) = cube.params.sampleSize function getXYZValues(cube::TDesignCube) - measurement = zeros(typeof(u"T"), 3, cube.params.T) - # TODO Implement this with start/receive from sensors + measurement = zeros(cube.params.T,6) + for (i,sensor) in enumerate(cube.sensors) + measurement[i,:] = getXYZValues(sensor) + end return measurement end -# TODO implement "getters" for T, N, radius +getT(cube::TDesignCube) = cube.params.T +getN(cube::TDesignCube) = cube.params.N +getRadius(cube::TDesignCube) = cube.params.radius function close(cube::TDesignCube) # NOP -end +end \ No newline at end of file From a59aaa0a8a75c5b8229dc5df53a38cade39fe82a Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 7 Dec 2022 14:35:17 +0100 Subject: [PATCH 34/68] fixing limits on sample size --- src/Devices/GaussMeter/TDesignCube.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 572a253c..0de140ba 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -32,8 +32,8 @@ end export setSampleSize function setSampleSize(cube::TDesignCube,sampleSize::Int) - if sampleSize>2000 || sampleSize<1 - throw("sampleSize must be in 1:2000") + if sampleSize>1024 || sampleSize<1 + throw("sampleSize must be in 1:1024") end for sensor in cube.sensors setSampleSize(sensor,sampleSize) From 15a38a7c4023589cbf95f81ab28b35c2d814f319 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 7 Dec 2022 15:29:53 +0100 Subject: [PATCH 35/68] WIP: spliting sending and reciveing --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 30ad13a6..c0cc0a03 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -63,6 +63,7 @@ function _init(gauss::ArduinoGaussMeter) gauss.ard = ard gauss.rotatedCalibration = params.rotation * params.calibration setSampleSize(gauss, params.sampleSize) + measurementTriggered =false end function initSerialDevice(gauss::ArduinoGaussMeter, params::ArduinoGaussMeterDirectParams) @@ -113,7 +114,54 @@ function getXYZValues(gauss::ArduinoGaussMeter) end +function query(sd::SerialDevice,cmd) + lock(sd.sdLock) + try + sp_flush(sd.sp, SP_BUF_INPUT) + send(sd,cmd) + out = receive(sd) + # Discard remaining data + sp_flush(sd.sp, SP_BUF_INPUT) + return out + finally + sp_flush(sd.sp, SP_BUF_INPUT) + unlock(sd.sdLock) + end +end + +#todo move to Arduino.jl +function triggerMeasurment(gauss::ArduinoGaussMeter) + cmd = cmdStart(gauss.ard) * cmdString * cmdEnd(gauss.ard) + sd = gauss.sd + lock(sd.sdLock) + try + if gauss.measurementTriggered + throw("measurement already triggered") + end + sp_flush(sd.sp,SP_BUF_INPUT) + send(sd,cmd) + gauss.measurementTriggered = true + finally + unlock(sd.sdLock) + end + +end +function reciveMeasurmentRaw(gauss::ArduinoGaussMeter) + #todo use lock + if !gauss.measurementTriggered + throw("startMeasurment has to be called first") + else + lock(sd.sdLock) + try + data = receive(gauss.sd) + sp_flush(sd.sp, SP_BUF_INPUT) + finally + sp_flush(sd.sp, SP_BUF_INPUT) + unlock(sd.sdLock) + end +end +function applyCalibration(gauss::ArduinoGaussMeter, data::) export setSampleSize function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) if(sampleSize>1024 || sampleSize<1) From 6fd67b455cb72f0dc10ddc4922fb0ac3af2421b6 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 7 Dec 2022 16:52:44 +0100 Subject: [PATCH 36/68] cube uses splitted send recive --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 66 +++++++++------------ src/Devices/GaussMeter/TDesignCube.jl | 7 ++- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index c0cc0a03..8fd0e149 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -1,4 +1,4 @@ -export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams +export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams,getRawXYZValues, getXValue, triggerMeasurment, reciveMeasurmentRaw,reciveMeasurment,setSampleSize,getSampleSize,getTemperature abstract type ArduinoGaussMeterParams <: DeviceParams end @@ -50,6 +50,7 @@ Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter ard::Union{SimpleArduino, Nothing} = nothing rotatedCalibration::Matrix{Float64} = Matrix{Float64}(I,(3,3)) sampleSize::Int = 0 + measurementTriggered::Bool =false end neededDependencies(::ArduinoGaussMeter) = [] @@ -63,7 +64,6 @@ function _init(gauss::ArduinoGaussMeter) gauss.ard = ard gauss.rotatedCalibration = params.rotation * params.calibration setSampleSize(gauss, params.sampleSize) - measurementTriggered =false end function initSerialDevice(gauss::ArduinoGaussMeter, params::ArduinoGaussMeterDirectParams) @@ -91,48 +91,25 @@ function checkSerialDevice(gauss::ArduinoGaussMeter, sd::SerialDevice) end end -export getRawXYZValues function getRawXYZValues(gauss::ArduinoGaussMeter) temp = get_timeout(gauss.ard) timeout_ms = max(1000,floor(Int,gauss.sampleSize*10*1.2)+1) set_timeout(gauss.ard, timeout_ms) data_strings = split(sendCommand(gauss.ard, "DATA"), ",") - # TODO use rotatedCalibration * data - data = [parse(Float32,str) for str in data_strings] + data = [parse(Float64,str) for str in data_strings] set_timeout(gauss.ard, temp) return data end function getXYZValues(gauss::ArduinoGaussMeter) data = getRawXYZValues(gauss) - means = data[1:3] - var = data[4:6] - calibrated_means = gauss.params.coordinateTransformation * means + gauss.params.biasCalibration - calibrated_var = gauss.params.coordinateTransformation*gauss.params.coordinateTransformation*var - data = vcat(calibrated_means,calibrated_var) - return data - -end - -function query(sd::SerialDevice,cmd) - lock(sd.sdLock) - try - sp_flush(sd.sp, SP_BUF_INPUT) - send(sd,cmd) - out = receive(sd) - # Discard remaining data - sp_flush(sd.sp, SP_BUF_INPUT) - return out - finally - sp_flush(sd.sp, SP_BUF_INPUT) - unlock(sd.sdLock) - end + return applyCalibration(gauss,data) end #todo move to Arduino.jl function triggerMeasurment(gauss::ArduinoGaussMeter) - cmd = cmdStart(gauss.ard) * cmdString * cmdEnd(gauss.ard) - sd = gauss.sd + cmd = cmdStart(gauss.ard) * "DATA" * cmdEnd(gauss.ard) + sd = gauss.ard.sd lock(sd.sdLock) try if gauss.measurementTriggered @@ -152,17 +129,36 @@ function reciveMeasurmentRaw(gauss::ArduinoGaussMeter) if !gauss.measurementTriggered throw("startMeasurment has to be called first") else + sd = gauss.ard.sd lock(sd.sdLock) + temp = get_timeout(gauss.ard) + timeout_ms = max(1000,floor(Int,gauss.sampleSize*10*1.2)+1) + set_timeout(gauss.ard, timeout_ms) try - data = receive(gauss.sd) - sp_flush(sd.sp, SP_BUF_INPUT) + data_strings = split(receive(gauss.ard.sd), ",") + data = [parse(Float64,str) for str in data_strings] + return data finally sp_flush(sd.sp, SP_BUF_INPUT) unlock(sd.sdLock) + set_timeout(gauss.ard, temp) + gauss.measurementTriggered = false end + end end -function applyCalibration(gauss::ArduinoGaussMeter, data::) -export setSampleSize + + +reciveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss,reciveMeasurmentRaw(gauss)) + + +function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) + means = data[1:3] + var = data[4:6] + calibrated_means = gauss.params.coordinateTransformation * means + gauss.params.biasCalibration + calibrated_var = gauss.params.coordinateTransformation*gauss.params.coordinateTransformation*var + return vcat(calibrated_means,calibrated_var) +end + function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) if(sampleSize>1024 || sampleSize<1) throw(error("no valid sample size, pick size from 1 to 1024")) @@ -172,14 +168,10 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) return parse(Int, data_string) end -export getSampleSize function getSampleSize(gauss::ArduinoGaussMeter) return gauss.sampleSize end - - -export getTemperature function getTemperature(gauss::ArduinoGaussMeter) temp_str = sendCommand(gauss.ard, "TEMP") return parse(Float32,temp_str) diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 0de140ba..91d9e1a5 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -46,8 +46,13 @@ getSampleSize(cube::TDesignCube) = cube.params.sampleSize function getXYZValues(cube::TDesignCube) measurement = zeros(cube.params.T,6) + #triggerMeasurment + for sensor in cube.sensors + triggerMeasurment(sensor) + end + #readmeasurement for (i,sensor) in enumerate(cube.sensors) - measurement[i,:] = getXYZValues(sensor) + measurement[i,:] = reciveMeasurment(sensor) end return measurement end From c204ff5245449d31c9260689915f6a50f0294991 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 16 Jan 2023 15:15:45 +0100 Subject: [PATCH 37/68] fixing calibration --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 8fd0e149..95d857c6 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -154,7 +154,7 @@ reciveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss,reciveMeasur function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] var = data[4:6] - calibrated_means = gauss.params.coordinateTransformation * means + gauss.params.biasCalibration + calibrated_means = gauss.rotatedCalibration * means + gauss.params.biasCalibration calibrated_var = gauss.params.coordinateTransformation*gauss.params.coordinateTransformation*var return vcat(calibrated_means,calibrated_var) end From 38f3ba314ecc218580b17cf97d2bf2f687f830c7 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 16 Jan 2023 15:16:13 +0100 Subject: [PATCH 38/68] closing diveces when loading another fails --- src/Devices/GaussMeter/TDesignCube.jl | 3 ++- src/Scanner.jl | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 91d9e1a5..28cc149c 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -21,7 +21,8 @@ optionalDependencies(::TDesignCube) = [] function _init(cube::TDesignCube) sampleSize = cube.params.sampleSize sensors = dependencies(cube, ArduinoGaussMeter) - if length(sensors) != cube.params.T + if length(sensors) != cube.params.N + close.(sensors) throw("missing Sensors") end sort!(sensors,by=x-> x.params.position) diff --git a/src/Scanner.jl b/src/Scanner.jl index 10b971e2..c33d6d65 100644 --- a/src/Scanner.jl +++ b/src/Scanner.jl @@ -108,6 +108,11 @@ function initiateDevices(configDir::AbstractString, devicesParams::Dict{String, end catch e if !robust + for (deviceId, device) in devices + if device.present + close(device) + end + end rethrow() else @warn e @@ -239,7 +244,7 @@ Close the devices when closing the scanner. """ function Base.close(scanner::MPIScanner) for device in getDevices(scanner, Device) - close(device) + close(device) end end From de339e5327fb68e56fab283c03b60d682205e079 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 17 Jan 2023 12:46:38 +0100 Subject: [PATCH 39/68] adding documentation --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 129 ++++++++++++++------ 1 file changed, 93 insertions(+), 36 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 95d857c6..e39feeea 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -1,14 +1,14 @@ -export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams,getRawXYZValues, getXValue, triggerMeasurment, reciveMeasurmentRaw,reciveMeasurment,setSampleSize,getSampleSize,getTemperature +export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams, getRawXYZValues, getXValue, triggerMeasurment, reciveMeasurmentRaw, reciveMeasurment, setSampleSize, getSampleSize, getTemperature abstract type ArduinoGaussMeterParams <: DeviceParams end Base.@kwdef struct ArduinoGaussMeterDirectParams <: ArduinoGaussMeterParams portAddress::String position::Int64 = 1 - calibration::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - rotation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - translation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) + calibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) + rotation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) + translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) + coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration = Vector{Float64} = [0.098, 0.098, 0.098] sampleSize::Int @add_serial_device_fields "#" @@ -20,12 +20,12 @@ ArduinoGaussMeterDirectParams(dict::Dict) = params_from_dict(ArduinoGaussMeterDi Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams description::String position::Int64 = 1 - calibration::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - rotation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - translation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) - coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) + calibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) + rotation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) + translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) + coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration::Vector{Float64} = [0.098, 0.098, 0.098] - sampleSize:: Int + sampleSize::Int @add_serial_device_fields "#" @add_arduino_fields "!" "*" @@ -47,10 +47,10 @@ end Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter @add_device_fields ArduinoGaussMeterParams - ard::Union{SimpleArduino, Nothing} = nothing - rotatedCalibration::Matrix{Float64} = Matrix{Float64}(I,(3,3)) + ard::Union{SimpleArduino,Nothing} = nothing + rotatedCalibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) sampleSize::Int = 0 - measurementTriggered::Bool =false + measurementTriggered::Bool = false end neededDependencies(::ArduinoGaussMeter) = [] @@ -59,8 +59,8 @@ optionalDependencies(::ArduinoGaussMeter) = [SerialPortPool] function _init(gauss::ArduinoGaussMeter) params = gauss.params sd = initSerialDevice(gauss, params) - @info "Connection to ArduinoGaussMeter established." - ard = SimpleArduino(;commandStart = params.commandStart, commandEnd = params.commandEnd, sd = sd) + @info "Connection to ArduinoGaussMeter established." + ard = SimpleArduino(; commandStart=params.commandStart, commandEnd=params.commandEnd, sd=sd) gauss.ard = ard gauss.rotatedCalibration = params.rotation * params.calibration setSampleSize(gauss, params.sampleSize) @@ -82,8 +82,8 @@ function checkSerialDevice(gauss::ArduinoGaussMeter, sd::SerialDevice) try reply = query(sd, "!VERSION*") if !(startswith(reply, "HALLSENS:2")) - close(sd) - throw(ScannerConfigurationError(string("Connected to wrong Device", reply))) + close(sd) + throw(ScannerConfigurationError(string("Connected to wrong Device", reply))) end return sd catch e @@ -91,22 +91,41 @@ function checkSerialDevice(gauss::ArduinoGaussMeter, sd::SerialDevice) end end +""" + getRawXYZValues(gauss::ArduinoGaussMeter)::Array{Float64,1} + + start measurment in sensor 'gauss' and returns raw measurment + + #returns + [x_raw_mean,y_raw_mean,z_raw_mean, x_raw_var,y_raw_var,z_raw_var] +""" function getRawXYZValues(gauss::ArduinoGaussMeter) temp = get_timeout(gauss.ard) - timeout_ms = max(1000,floor(Int,gauss.sampleSize*10*1.2)+1) + timeout_ms = max(1000, floor(Int, gauss.sampleSize * 10 * 1.2) + 1) set_timeout(gauss.ard, timeout_ms) data_strings = split(sendCommand(gauss.ard, "DATA"), ",") - data = [parse(Float64,str) for str in data_strings] + data = [parse(Float64, str) for str in data_strings] set_timeout(gauss.ard, temp) return data end +""" + getXYZValues(gauss::ArduinoGaussMeter)::Array{Float64,1} + start and returns calibrated measurment for 'gauss'-sensor in mT + + #returns + [x_mean,y_mean,z_mean, x_var,y_var,z_var] +""" function getXYZValues(gauss::ArduinoGaussMeter) - data = getRawXYZValues(gauss) - return applyCalibration(gauss,data) + data = getRawXYZValues(gauss) + return applyCalibration(gauss, data) end #todo move to Arduino.jl +""" + triggerMeasurment(gauss::ArduinoGaussMeter) + start measurment in sensor 'gauss' +""" function triggerMeasurment(gauss::ArduinoGaussMeter) cmd = cmdStart(gauss.ard) * "DATA" * cmdEnd(gauss.ard) sd = gauss.ard.sd @@ -115,28 +134,37 @@ function triggerMeasurment(gauss::ArduinoGaussMeter) if gauss.measurementTriggered throw("measurement already triggered") end - sp_flush(sd.sp,SP_BUF_INPUT) - send(sd,cmd) + sp_flush(sd.sp, SP_BUF_INPUT) + send(sd, cmd) gauss.measurementTriggered = true finally - unlock(sd.sdLock) + unlock(sd.sdLock) end - end + +""" + reciveMeasurmentRaw(gauss::ArduinoGaussMeter)::Array{Float64,1} + + collecting the measurment data for sensor 'gauss' + triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first. + + #returns + [x_raw_mean,y_raw_mean,z_raw_mean, x_raw_var,y_raw_var,z_raw_var] +""" function reciveMeasurmentRaw(gauss::ArduinoGaussMeter) #todo use lock if !gauss.measurementTriggered - throw("startMeasurment has to be called first") + throw("triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first") else sd = gauss.ard.sd lock(sd.sdLock) temp = get_timeout(gauss.ard) - timeout_ms = max(1000,floor(Int,gauss.sampleSize*10*1.2)+1) + timeout_ms = max(1000, floor(Int, gauss.sampleSize * 10 * 1.2) + 1) set_timeout(gauss.ard, timeout_ms) try data_strings = split(receive(gauss.ard.sd), ",") - data = [parse(Float64,str) for str in data_strings] + data = [parse(Float64, str) for str in data_strings] return data finally sp_flush(sd.sp, SP_BUF_INPUT) @@ -147,20 +175,44 @@ function reciveMeasurmentRaw(gauss::ArduinoGaussMeter) end end +""" +reciveMeasurment(gauss::ArduinoGaussMeter)::Array{Float64,1} + collecting, calibrating and returning measurment for 'gauss'-sensor in mT + triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first. -reciveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss,reciveMeasurmentRaw(gauss)) + + #return + [x_mean,y_mean,z_mean, x_var,y_var,z_var] +""" +reciveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss, reciveMeasurmentRaw(gauss)) +""" +applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64})::Array{Float64,1} +calibrate and rotated raw data to mT +""" function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] var = data[4:6] - calibrated_means = gauss.rotatedCalibration * means + gauss.params.biasCalibration - calibrated_var = gauss.params.coordinateTransformation*gauss.params.coordinateTransformation*var - return vcat(calibrated_means,calibrated_var) + calibrated_means = gauss.rotatedCalibration * means + gauss.params.biasCalibration + calibrated_var = gauss.params.coordinateTransformation * gauss.params.coordinateTransformation * var + return vcat(calibrated_means, calibrated_var) end - + +""" + setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int)::Int = sample_size + + sets sample size for measurment + + #Arguments + -`sampleSize` number of mesurments done by the sensor 1=>sample_size<=1024 + -`gauss` sensor + + #return + -sampleSize()::Int +""" function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) - if(sampleSize>1024 || sampleSize<1) + if (sampleSize > 1024 || sampleSize < 1) throw(error("no valid sample size, pick size from 1 to 1024")) end gauss.sampleSize = sampleSize @@ -168,13 +220,18 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) return parse(Int, data_string) end -function getSampleSize(gauss::ArduinoGaussMeter) +function getSampleSize(gauss::ArduinoGaussMeter) return gauss.sampleSize end +""" +getTemperature(gauss::ArduinoGaussMeter)::Float32= tempreture + +returns tempreture of the sensor, do not expect a high tempreture resolution +""" function getTemperature(gauss::ArduinoGaussMeter) temp_str = sendCommand(gauss.ard, "TEMP") - return parse(Float32,temp_str) + return parse(Float32, temp_str) end close(gauss::ArduinoGaussMeter) = close(gauss.ard) \ No newline at end of file From 47e1844a30859226db415d36a3fd39224924705a Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 18 Jan 2023 12:05:12 +0100 Subject: [PATCH 40/68] updating doku --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index e39feeea..9e6b7474 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -225,7 +225,7 @@ function getSampleSize(gauss::ArduinoGaussMeter) end """ -getTemperature(gauss::ArduinoGaussMeter)::Float32= tempreture +getTemperature(gauss::ArduinoGaussMeter)::Float32 returns tempreture of the sensor, do not expect a high tempreture resolution """ From c50999ec3fb6747a0a6a53f46eea80b1e0ebd611 Mon Sep 17 00:00:00 2001 From: nHackel Date: Mon, 23 Jan 2023 14:24:36 +0100 Subject: [PATCH 41/68] Added TODOs --- Devices/Arduino/HallSensor/HallSensor.ino | 21 +++++++++++---------- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 18 ++++++++++++++---- src/Devices/GaussMeter/TDesignCube.jl | 5 +++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 6d8786e1..bb6d130f 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,6 +1,6 @@ #define ARDUINO_TYPE "HALLSENS" #define VERSION "2.1" -#define POSITION 1 +#define POSITION 1 // TODO Remove POSITION and position related code #define BAUDRATE 9600 #include @@ -9,7 +9,7 @@ Tle493d_w2b6 sensor = Tle493d_w2b6(Tle493d::MASTERCONTROLLEDMODE); int sample_size= 1000; // Communication #define INPUT_BUFFER_SIZE 3000 -#define VALUE_BUFFER_SIZE 1024 +#define VALUE_BUFFER_SIZE 1024 // TODO Add function querying VALUE_BUFFER_SIZE char input_buffer[INPUT_BUFFER_SIZE]; int16_t value_buffer[VALUE_BUFFER_SIZE*3]; unsigned int input_pos = 0; @@ -139,12 +139,12 @@ void setup() { int getData(char*) { // updateData reads values from sensor and reading triggers next measurement sensor.updateData(); // Throw away first old data - delay(10); + delay(10); // TODO Magic number 10 -> #define MEAS_DELAY 10 uint16_t measDelay = 10; unsigned long start, end,startFP,endA; - // TODO perform measurement + // TODO consistent formatting int16_t x=0,y=0,z=0; int32_t sumX=0,sumY=0,sumZ=0,sumXX=0,sumYY=0,sumZZ=0; float varX=0, varY = 0, varZ=0, meanX =0, meanY =0, meanZ =0; @@ -179,9 +179,10 @@ int getData(char*) { sumYY += ((float)value_buffer[i+1]-meanY)*((float)value_buffer[i+1]-meanY); sumZZ += ((float)value_buffer[i+2]-meanZ) *((float)value_buffer[i+2]-meanZ); } - varX =(float)sumXX/sample_size; - varY =(float)sumYY/sample_size; - varZ =(float)sumZZ/sample_size; + + varX =(float)sumXX/sample_size; + varY =(float)sumYY/sample_size; + varZ =(float)sumZZ/sample_size; Serial.print(meanX,7); Serial.print(","); @@ -206,7 +207,7 @@ int getPosition(char*) { int getTemp(char*) { sensor.enableTemp(); - delay(10); + delay(10); // TODO See meas delay sensor.updateData(); delay(10); sensor.updateData(); @@ -230,8 +231,8 @@ int getVersion(char*) { } int setSampleSize(char* command){ - int value_int = atoi(command+7); - if (value_int>0 && value_int<=2048){ + int value_int = atoi(command+7); // Could point to end of char, value_int = 0. But in theory can not differentiate between unintended string and 0 samples set + if (value_int>0 && value_int<=2048){ // 2048 > VALUE_BUFFER_SIZE -> TODO Use VALUE_BUFFER_SIZE here sample_size=value_int; } Serial.print(sample_size); diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 95d857c6..327df2c1 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -5,6 +5,7 @@ abstract type ArduinoGaussMeterParams <: DeviceParams end Base.@kwdef struct ArduinoGaussMeterDirectParams <: ArduinoGaussMeterParams portAddress::String position::Int64 = 1 + # TODO Sensible defaults for translation and bias for both structs calibration::Matrix{Float64} = Matrix{Float64}(I,(3,3)) rotation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) translation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) @@ -93,7 +94,8 @@ end function getRawXYZValues(gauss::ArduinoGaussMeter) temp = get_timeout(gauss.ard) - timeout_ms = max(1000,floor(Int,gauss.sampleSize*10*1.2)+1) + # TODO use triggerMeasurment and receiveMeasurement + timeout_ms = max(1000,floor(Int,gauss.sampleSize*10*1.2)+1) # TODO Timeout has magic number -> query MEAS_DELAY from Arduino in init() set_timeout(gauss.ard, timeout_ms) data_strings = split(sendCommand(gauss.ard, "DATA"), ",") data = [parse(Float64,str) for str in data_strings] @@ -110,6 +112,9 @@ end function triggerMeasurment(gauss::ArduinoGaussMeter) cmd = cmdStart(gauss.ard) * "DATA" * cmdEnd(gauss.ard) sd = gauss.ard.sd + # TODO Lock synchronization like this does not have the desired effect: + # For example triggerMeasurement(gauss); getTemperature(gauss); receiveMeasurement(gauss) would result in wrong values + # Instead use an atomic or lock concept on ArduinoGaussMeter level and restrict all communication during measurement lock(sd.sdLock) try if gauss.measurementTriggered @@ -124,6 +129,7 @@ function triggerMeasurment(gauss::ArduinoGaussMeter) end +# TODO Rename to receive, dont export as not intended as public interface function reciveMeasurmentRaw(gauss::ArduinoGaussMeter) #todo use lock if !gauss.measurementTriggered @@ -134,12 +140,13 @@ function reciveMeasurmentRaw(gauss::ArduinoGaussMeter) temp = get_timeout(gauss.ard) timeout_ms = max(1000,floor(Int,gauss.sampleSize*10*1.2)+1) set_timeout(gauss.ard, timeout_ms) + # TODO Function has no "defined" return value if receive or parse fail try data_strings = split(receive(gauss.ard.sd), ",") data = [parse(Float64,str) for str in data_strings] return data finally - sp_flush(sd.sp, SP_BUF_INPUT) + sp_flush(sd.sp, SP_BUF_INPUT) # TODO Why flush? unlock(sd.sdLock) set_timeout(gauss.ard, temp) gauss.measurementTriggered = false @@ -154,6 +161,8 @@ reciveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss,reciveMeasur function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] var = data[4:6] + # TODO Sanity checks on data, does it have the expected size + # TODO Is bias not rotated? Why is variance multiplied with coordinateTransformation twice calibrated_means = gauss.rotatedCalibration * means + gauss.params.biasCalibration calibrated_var = gauss.params.coordinateTransformation*gauss.params.coordinateTransformation*var return vcat(calibrated_means,calibrated_var) @@ -163,12 +172,13 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) if(sampleSize>1024 || sampleSize<1) throw(error("no valid sample size, pick size from 1 to 1024")) end + # TODO only set sampleSize if it was confirmed by Arduino, see next comment gauss.sampleSize = sampleSize - data_string = sendCommand(gauss.ard, "SAMPLES" * string(sampleSize)) + data_string = sendCommand(gauss.ard, "SAMPLES" * string(sampleSize)) # TODO Check if wanted value was set, otherwise throw error and there query HallSensor for valid > 0 values return parse(Int, data_string) end -function getSampleSize(gauss::ArduinoGaussMeter) +function getSampleSize(gauss::ArduinoGaussMeter) return gauss.sampleSize end diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 28cc149c..d0c72a20 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -22,17 +22,18 @@ function _init(cube::TDesignCube) sampleSize = cube.params.sampleSize sensors = dependencies(cube, ArduinoGaussMeter) if length(sensors) != cube.params.N - close.(sensors) + close.(sensors) # TODO @NH Should not close devices here throw("missing Sensors") end sort!(sensors,by=x-> x.params.position) cube.sensors = sensors - println(cube.params) + println(cube.params) # TODO remove setSampleSize(cube,cube.sampleSize) end export setSampleSize function setSampleSize(cube::TDesignCube,sampleSize::Int) + # TODO This check should happen in the sensors, here it should be checked if values could be successfully set, ideally with a bool reply from setSampleSize if sampleSize>1024 || sampleSize<1 throw("sampleSize must be in 1:1024") end From 9c8c99277372ec497d2605ac1c9a86cd69cfdc00 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 23 Jan 2023 14:58:31 +0100 Subject: [PATCH 42/68] reducing waittime in sensor --- Devices/Arduino/HallSensor/HallSensor.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 6d8786e1..c7ed51f2 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -140,7 +140,7 @@ int getData(char*) { // updateData reads values from sensor and reading triggers next measurement sensor.updateData(); // Throw away first old data delay(10); - uint16_t measDelay = 10; + uint16_t measDelay = 2; unsigned long start, end,startFP,endA; From 4278f8a95e6f246aa5e86fc034f8a44ca2e529ee Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 26 Jan 2023 11:39:48 +0100 Subject: [PATCH 43/68] fixing calibration and removing varianz from calibrated getXYZValues --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 14 +++++++------- src/Devices/GaussMeter/TDesignCube.jl | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 7a9edc79..fb045ec9 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -60,7 +60,6 @@ function _init(gauss::ArduinoGaussMeter) @info "Connection to ArduinoGaussMeter established." ard = SimpleArduino(; commandStart=params.commandStart, commandEnd=params.commandEnd, sd=sd) gauss.ard = ard - gauss.rotatedCalibration = params.rotation * params.calibration gaus.measdelay = query(sd,"!DELAY*") setSampleSize(gauss, params.sampleSize) end @@ -191,17 +190,18 @@ reciveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss, recive(gaus """ applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64})::Array{Float64,1} -calibrate and rotated raw data to mT +calibrate and rotate raw data to mT + +#returns +[x_mean_c,y_mean_c,z_mean_c] """ function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] - var = data[4:6] - + # TODO Sanity checks on data, does it have the expected size # TODO Is bias not rotated? Why is variance multiplied with coordinateTransformation twice - calibrated_means = gauss.rotatedCalibration * means + gauss.params.biasCalibration - calibrated_var = gauss.rotatedCalibration*gauss.rotatedCalibration*var - return vcat(calibrated_means,calibrated_var) + calibrated_means = gaus.params.rotation*(gauss.params.calibrate * means + gauss.params.biasCalibration) + return calibrated_means end """ diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 5edd4221..804f59c2 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -44,7 +44,7 @@ export getSampleSize getSampleSize(cube::TDesignCube) = cube.params.sampleSize function getXYZValues(cube::TDesignCube) - measurement = zeros(cube.params.T,6) + measurement = zeros(cube.params.N,3) #triggerMeasurment for sensor in cube.sensors triggerMeasurment(sensor) From af01e3179315b63c30b95140be8c3d4c62e8000e Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 26 Jan 2023 11:54:58 +0100 Subject: [PATCH 44/68] added comment to varianze --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index fb045ec9..96e0d789 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -192,6 +192,8 @@ applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64})::Array{Float64 calibrate and rotate raw data to mT +Varianz can't be calibrated + #returns [x_mean_c,y_mean_c,z_mean_c] """ @@ -199,7 +201,6 @@ function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] # TODO Sanity checks on data, does it have the expected size - # TODO Is bias not rotated? Why is variance multiplied with coordinateTransformation twice calibrated_means = gaus.params.rotation*(gauss.params.calibrate * means + gauss.params.biasCalibration) return calibrated_means end @@ -220,10 +221,13 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) if (sampleSize > 1024 || sampleSize < 1) throw(error("no valid sample size, pick size from 1 to 1024")) end - # TODO only set sampleSize if it was confirmed by Arduino, see next comment - gauss.sampleSize = sampleSize + # TODO problem on time out probabil wrong value on device data_string = sendCommand(gauss.ard, "SAMPLES" * string(sampleSize)) # TODO Check if wanted value was set, otherwise throw error and there query HallSensor for valid > 0 values - return parse(Int, data_string) + new_size = parse(Int, data_string) + if(new_size !== sampleSize) + gauss.sampleSize = new_size + return new_size + end function getSampleSize(gauss::ArduinoGaussMeter) From af3387be894b5d6f89ba2fed0eb381a1b12438df Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 26 Jan 2023 11:57:34 +0100 Subject: [PATCH 45/68] renaming new to updatedSampleSize --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 96e0d789..70adbca7 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -206,7 +206,7 @@ function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) end """ - setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int)::Int = sample_size + setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int)::Int sets sample size for measurment @@ -215,7 +215,7 @@ end -`gauss` sensor #return - -sampleSize()::Int + -updatedSampleSize """ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) if (sampleSize > 1024 || sampleSize < 1) @@ -223,10 +223,10 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) end # TODO problem on time out probabil wrong value on device data_string = sendCommand(gauss.ard, "SAMPLES" * string(sampleSize)) # TODO Check if wanted value was set, otherwise throw error and there query HallSensor for valid > 0 values - new_size = parse(Int, data_string) - if(new_size !== sampleSize) - gauss.sampleSize = new_size - return new_size + updatedSampleSize = parse(Int, data_string) + if(updatedSampleSize !== sampleSize) + gauss.sampleSize = updatedSampleSize + return updatedSampleSize end From 0b8dbf8d1f1b56fd830d5db00df3a20601ad0296 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 30 Jan 2023 14:57:19 +0100 Subject: [PATCH 46/68] Updated SEnsor software to 3.0 removing position fixing varianz to n-1 adding comments --- Devices/Arduino/HallSensor/HallSensor.ino | 268 ++++++++++++---------- 1 file changed, 149 insertions(+), 119 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 74b04e0c..8a985075 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,5 +1,5 @@ #define ARDUINO_TYPE "HALLSENS" -#define VERSION "2.1" +#define VERSION "3.0" #define BAUDRATE 9600 #define MEASDELAY 2 @@ -7,39 +7,40 @@ // Tlv493d Opject Tle493d_w2b6 sensor = Tle493d_w2b6(Tle493d::MASTERCONTROLLEDMODE); -int sample_size= 1000; +int sample_size = 1000; // Communication #define INPUT_BUFFER_SIZE 3000 #define VALUE_BUFFER_SIZE 1024 char input_buffer[INPUT_BUFFER_SIZE]; -int16_t value_buffer[VALUE_BUFFER_SIZE*3]; +int16_t value_buffer[VALUE_BUFFER_SIZE * 3]; unsigned int input_pos = 0; -int getData(char*); -int getPosition(char*); -int getTemp(char*); -int getVersion(char*); -int getCommands(char*); -int setSampleSize(char*); -//int setFoo(char*) - -typedef struct { - const char * id; - int (*handler)(char*); +int getDelay(char *); +int getData(char *); +int getPosition(char *); +int getTemp(char *); +int getVersion(char *); +int getCommands(char *); +int setSampleSize(char *); + +typedef struct +{ + const char *id; + int (*handler)(char *); } commandHandler_t; commandHandler_t cmdHandler[] = { - {"DELAY",getDelay} - {"DATA", getData}, - {"TEMP", getTemp}, - {"VERSION", getVersion}, - {"COMMANDS", getCommands}, - {"SAMPLES", setSampleSize} - //{"FOO", setFoo} + {"DELAY", getDelay}, + {"DATA", getData}, + {"TEMP", getTemp}, + {"VERSION", getVersion}, + {"COMMANDS", getCommands}, + {"SAMPLES", setSampleSize} }; -int getCommands(char*) { +int getCommands(char *) +{ Serial.print("Valid Commands (without quotes):"); Serial.print("'!DATA*#' "); Serial.print("'!DELAY*#' "); @@ -50,60 +51,72 @@ int getCommands(char*) { Serial.println("#"); } - -bool updateBufferUntilDelim(char delim) { +bool updateBufferUntilDelim(char delim) +{ char nextChar; - while (Serial.available() > 0) { - nextChar = Serial.read(); - if (nextChar == delim) { - input_buffer[input_pos] = '\0'; - input_pos = 0; - return true; - } else if (nextChar != '\n') { - input_buffer[input_pos % (INPUT_BUFFER_SIZE - 1)] = nextChar; // Size - 1 to always leave room for \0 - input_pos++; - } + while (Serial.available() > 0) + { + nextChar = Serial.read(); + if (nextChar == delim) + { + input_buffer[input_pos] = '\0'; + input_pos = 0; + return true; + } + else if (nextChar != '\n') + { + input_buffer[input_pos % (INPUT_BUFFER_SIZE - 1)] = nextChar; // Size - 1 to always leave room for \0 + input_pos++; + } } return false; } -void serialCommand() { - int s=-1; - int e=-1; - char beginDelim='!'; +void serialCommand() +{ + int s = -1; + int e = -1; + char beginDelim = '!'; char *beginCmd; - char endDelim='*'; + char endDelim = '*'; char *endCmd; char *command; bool done = false; bool unknown = false; - + // determin substring - if(Serial.available()>0) { - done = updateBufferUntilDelim('#'); + if (Serial.available() > 0) + { + done = updateBufferUntilDelim('#'); } - if (done) { + if (done) + { s = -1; - if ((beginCmd = strchr(input_buffer, beginDelim)) != NULL) { + if ((beginCmd = strchr(input_buffer, beginDelim)) != NULL) + { s = beginCmd - input_buffer; } e = -1; - if ((endCmd = strchr(input_buffer, endDelim)) != NULL) { + if ((endCmd = strchr(input_buffer, endDelim)) != NULL) + { e = endCmd - input_buffer; - } + } unknown = true; - //check if valid command - if (e!=-1 && s!=-1) { + // check if valid command + if (e != -1 && s != -1) + { command = beginCmd + 1; *endCmd = '\0'; Serial.flush(); - //check for known commands - for (int i = 0; i < sizeof(cmdHandler)/sizeof(*cmdHandler); i++) { - if (strncmp(cmdHandler[i].id, command, strlen(cmdHandler[i].id)) == 0) { + // check for known commands + for (int i = 0; i < sizeof(cmdHandler) / sizeof(*cmdHandler); i++) + { + if (strncmp(cmdHandler[i].id, command, strlen(cmdHandler[i].id)) == 0) + { cmdHandler[i].handler(command); unknown = false; input_buffer[0] = '\0'; // "Empty" input buffer @@ -111,133 +124,150 @@ void serialCommand() { } } } - - if (unknown) { + + if (unknown) + { Serial.println("UNKNOWN"); Serial.println("#"); - Serial.flush(); + Serial.flush(); } - } - } //////////////////////////////////////////////////////////////////////////////// -void setup() { +void setup() +{ Serial.begin(BAUDRATE); - while(!Serial); - - //If using the MS2Go-Kit: Enable following lines to switch on the sensor - // *** + while (!Serial) + ; + + // If using the MS2Go-Kit: Enable following lines to switch on the sensor + // *** pinMode(LED2, OUTPUT); digitalWrite(LED2, HIGH); delay(50); // *** - + sensor.begin(); sensor.disableTemp(); } -int getDelay(char*) { - +int getDelay(char *) +{ + Serial.print(MEASDELAY, 7); + Serial.println("#"); + Serial.flush(); } -int getData(char*) { +int getData(char *) +{ + unsigned long start, end, startFP, endA; + int16_t x = 0, y = 0, z = 0; + int32_t sumX = 0, sumY = 0, sumZ = 0; + float sumXX = 0, sumYY = 0, sumZZ = 0; + float varX = 0, varY = 0, varZ = 0; + float meanX = 0, meanY = 0, meanZ = 0; + // updateData reads values from sensor and reading triggers next measurement - sensor.updateData(); - delay(MEASDELAY); - unsigned long start, end,startFP,endA; - // TODO consistent formatting - int16_t x=0,y=0,z=0; - int32_t sumX=0,sumY=0,sumZ=0,sumXX=0,sumYY=0,sumZZ=0; - float varX=0, varY = 0, varZ=0, meanX =0, meanY =0, meanZ =0; - for (int i =0 ;i< sample_size*3 ; i+=3){ - sensor.updateData(); + sensor.updateData(); + delay(MEASDELAY); + + // calculating mean values Mean =sum(x[i])/sample_size + // measurement is stored for calculating the variance in a second go + for (int i = 0; i < sample_size * 3; i += 3) + { + sensor.updateData(); start = millis(); - + x = sensor.getRawX(); y = sensor.getRawY(); z = sensor.getRawZ(); - + value_buffer[i] = x; - value_buffer[i+1] = y; - value_buffer[i+2] = z; - - sumX+=x; - sumY+=y; - sumZ+=z; - + value_buffer[i + 1] = y; + value_buffer[i + 2] = z; + + sumX += x; + sumY += y; + sumZ += z; + + //waiting until new data is ready in the sensor end = millis(); - if (end - start < measDelay) { - delay(MEASDELAY-(end - start)); + if (end - start < MEASDELAY) + { + delay(MEASDELAY - (end - start)); } } - - meanX = (float)sumX/sample_size; - meanY = (float)sumY/sample_size; - meanZ = (float)sumZ/sample_size; - - for(int i=0; i 1) + { + for (int i = 0; i < sample_size * 3; i += 3) + { + sumXX += ((float)value_buffer[i] - meanX) * ((float)value_buffer[i] - meanX); + sumYY += ((float)value_buffer[i + 1] - meanY) * ((float)value_buffer[i + 1] - meanY); + sumZZ += ((float)value_buffer[i + 2] - meanZ) * ((float)value_buffer[i + 2] - meanZ); + } - Serial.print(meanX,7); + varX = sumXX / (sample_size - 1); + varY = sumYY / (sample_size - 1); + varZ = sumZZ / (sample_size - 1); + } + Serial.print(meanX, 7); Serial.print(","); - Serial.print(meanY,7); + Serial.print(meanY, 7); Serial.print(","); - Serial.print(meanZ,7); + Serial.print(meanZ, 7); Serial.print(","); - Serial.print(varX,7); + Serial.print(varX, 7); Serial.print(","); - Serial.print(varY,7); + Serial.print(varY, 7); Serial.print(","); - Serial.print(varZ,7); + Serial.print(varZ, 7); Serial.println("#"); Serial.flush(); } - -int getTemp(char*) { +int getTemp(char *) +{ sensor.enableTemp(); - delay(10); + delay(10); sensor.updateData(); delay(MEASDELAY); sensor.updateData(); delay(MEASDELAY); float temp = sensor.getTemp(); - Serial.print(temp,7); + Serial.print(temp, 7); Serial.println("#"); Serial.flush(); sensor.disableTemp(); delay(10); } -int getVersion(char*) { +int getVersion(char *) +{ Serial.print(ARDUINO_TYPE); Serial.print(":"); Serial.print(VERSION); - Serial.print(":"); - Serial.print(POSITION); Serial.print("#"); - Serial.flush(); + Serial.flush(); } -int setSampleSize(char* command){ - int value_int = atoi(command+7); // Could point to end of char, value_int = 0. But in theory can not differentiate between unintended string and 0 samples set - if (value_int>0 && value_int<=VALUE_BUFFER_SIZE){ - sample_size=value_int; +int setSampleSize(char *command) +{ + int value_int = atoi(command + 7); // Could point to end of char, value_int = 0. But in theory can not differentiate between unintended string and 0 samples set + if (value_int > 0 && value_int <= VALUE_BUFFER_SIZE) + { + sample_size = value_int; } Serial.print(sample_size); Serial.println("#"); } - - -void loop() { +void loop() +{ serialCommand(); } From 23041adc10b61e0071bc3ef7fc82aa86bc6a398b Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 30 Jan 2023 15:04:47 +0100 Subject: [PATCH 47/68] removing comment on set sample size, as the value only set if in valid range --- Devices/Arduino/HallSensor/HallSensor.ino | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 8a985075..dde9d272 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -36,8 +36,7 @@ commandHandler_t cmdHandler[] = { {"TEMP", getTemp}, {"VERSION", getVersion}, {"COMMANDS", getCommands}, - {"SAMPLES", setSampleSize} -}; + {"SAMPLES", setSampleSize}}; int getCommands(char *) { @@ -190,7 +189,7 @@ int getData(char *) sumY += y; sumZ += z; - //waiting until new data is ready in the sensor + // waiting until new data is ready in the sensor end = millis(); if (end - start < MEASDELAY) { @@ -258,7 +257,7 @@ int getVersion(char *) int setSampleSize(char *command) { - int value_int = atoi(command + 7); // Could point to end of char, value_int = 0. But in theory can not differentiate between unintended string and 0 samples set + int value_int = atoi(command + 7); if (value_int > 0 && value_int <= VALUE_BUFFER_SIZE) { sample_size = value_int; From 9d1dba4aa7aeaaa56ba26b98f5da2475a088ce49 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 30 Jan 2023 16:09:13 +0100 Subject: [PATCH 48/68] adding missing ends --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 14 +++++++------- src/Devices/GaussMeter/TDesignCube.jl | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 70adbca7..b75a8d3c 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -5,7 +5,7 @@ abstract type ArduinoGaussMeterParams <: DeviceParams end Base.@kwdef struct ArduinoGaussMeterDirectParams <: ArduinoGaussMeterParams portAddress::String position::Int64 = 1 - calibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3))*0.125 + calibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) * 0.125 rotation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration = Vector{Float64} = [0.0, 0.0, 0.0] @@ -19,7 +19,7 @@ ArduinoGaussMeterDirectParams(dict::Dict) = params_from_dict(ArduinoGaussMeterDi Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams description::String position::Int64 = 1 - calibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3))*0.125 + calibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) * 0.125 rotation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration::Vector{Float64} = [0.0, 0.0, 0.0] @@ -60,7 +60,7 @@ function _init(gauss::ArduinoGaussMeter) @info "Connection to ArduinoGaussMeter established." ard = SimpleArduino(; commandStart=params.commandStart, commandEnd=params.commandEnd, sd=sd) gauss.ard = ard - gaus.measdelay = query(sd,"!DELAY*") + gaus.measdelay = query(sd, "!DELAY*") setSampleSize(gauss, params.sampleSize) end @@ -199,9 +199,8 @@ Varianz can't be calibrated """ function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] - # TODO Sanity checks on data, does it have the expected size - calibrated_means = gaus.params.rotation*(gauss.params.calibrate * means + gauss.params.biasCalibration) + calibrated_means = gaus.params.rotation * (gauss.params.calibrate * means + gauss.params.biasCalibration) return calibrated_means end @@ -224,10 +223,11 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) # TODO problem on time out probabil wrong value on device data_string = sendCommand(gauss.ard, "SAMPLES" * string(sampleSize)) # TODO Check if wanted value was set, otherwise throw error and there query HallSensor for valid > 0 values updatedSampleSize = parse(Int, data_string) - if(updatedSampleSize !== sampleSize) + if (updatedSampleSize !== sampleSize) + throw(error("wrong sample size set")) + end gauss.sampleSize = updatedSampleSize return updatedSampleSize - end function getSampleSize(gauss::ArduinoGaussMeter) diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 804f59c2..881f50cd 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -36,6 +36,7 @@ function setSampleSize(cube::TDesignCube,sampleSize::Int) returnSampleSize = setSampleSize(sensor,sampleSize) if returnSampleSize != sampleSize throw("sensors coud not be updated") + end end cube.sampleSize = sampleSize end From 8596f08328612de1c56a57b512552009ce94798d Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 31 Jan 2023 14:32:23 +0100 Subject: [PATCH 49/68] using lock correct in recive and trigger --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 30 +++++++-------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index b75a8d3c..ef19d0f7 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -115,7 +115,6 @@ function getXYZValues(gauss::ArduinoGaussMeter) return applyCalibration(gauss, data) end -#todo move to Arduino.jl """ triggerMeasurment(gauss::ArduinoGaussMeter) start measurment in sensor 'gauss' @@ -123,20 +122,15 @@ end function triggerMeasurment(gauss::ArduinoGaussMeter) cmd = cmdStart(gauss.ard) * "DATA" * cmdEnd(gauss.ard) sd = gauss.ard.sd - # TODO Lock synchronization like this does not have the desired effect: - # For example triggerMeasurement(gauss); getTemperature(gauss); receiveMeasurement(gauss) would result in wrong values - # Instead use an atomic or lock concept on ArduinoGaussMeter level and restrict all communication during measurement - lock(sd.sdLock) - try - if gauss.measurementTriggered - throw("measurement already triggered") - end - sp_flush(sd.sp, SP_BUF_INPUT) - send(sd, cmd) - gauss.measurementTriggered = true - finally - unlock(sd.sdLock) + lock(sd.sdLock) + + if gauss.measurementTriggered + throw("measurement already triggered") end + sp_flush(sd.sp, SP_BUF_INPUT) + send(sd, cmd) + gauss.measurementTriggered = true + end @@ -151,23 +145,19 @@ end """ function recive(gauss::ArduinoGaussMeter) - #todo use lock if !gauss.measurementTriggered throw("triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first") else - sd = gauss.ard.sd - lock(sd.sdLock) + temp = get_timeout(gauss.ard) timeout_ms = max(1000, floor(Int, gauss.sampleSize * 10 * 1.2) + 1) set_timeout(gauss.ard, timeout_ms) - # TODO Function has no "defined" return value if receive or parse fail try data_strings = split(receive(gauss.ard.sd), ",") data = [parse(Float64, str) for str in data_strings] return data - catch e - throw(e) finally + sd = gauss.ard.sd sp_flush(sd.sp, SP_BUF_INPUT) # TODO Why flush? unlock(sd.sdLock) set_timeout(gauss.ard, temp) From 455d6d605d6c9985d6649586aed894c7a1597b43 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 31 Jan 2023 14:33:58 +0100 Subject: [PATCH 50/68] renaming recive to receive --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 12 ++++++------ src/Devices/GaussMeter/TDesignCube.jl | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index ef19d0f7..5e98e131 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -1,4 +1,4 @@ -export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams, getRawXYZValues, getXValue, triggerMeasurment, recive, reciveMeasurment, setSampleSize, getSampleSize, getTemperature +export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams, getRawXYZValues, getXValue, triggerMeasurment, receive, receiveMeasurment, setSampleSize, getSampleSize, getTemperature abstract type ArduinoGaussMeterParams <: DeviceParams end @@ -99,7 +99,7 @@ end """ function getRawXYZValues(gauss::ArduinoGaussMeter) triggerMeasurment(gauss) - data = recive(gauss) + data = receive(gauss) return data end @@ -135,7 +135,7 @@ end """ - recive(gauss::ArduinoGaussMeter)::Array{Float64,1} + receive(gauss::ArduinoGaussMeter)::Array{Float64,1} collecting the measurment data for sensor 'gauss' triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first. @@ -144,7 +144,7 @@ end [x_raw_mean,y_raw_mean,z_raw_mean, x_raw_var,y_raw_var,z_raw_var] """ -function recive(gauss::ArduinoGaussMeter) +function receive(gauss::ArduinoGaussMeter) if !gauss.measurementTriggered throw("triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first") else @@ -167,7 +167,7 @@ function recive(gauss::ArduinoGaussMeter) end """ -reciveMeasurment(gauss::ArduinoGaussMeter)::Array{Float64,1} +receiveMeasurment(gauss::ArduinoGaussMeter)::Array{Float64,1} collecting, calibrating and returning measurment for 'gauss'-sensor in mT triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first. @@ -175,7 +175,7 @@ reciveMeasurment(gauss::ArduinoGaussMeter)::Array{Float64,1} #return [x_mean,y_mean,z_mean, x_var,y_var,z_var] """ -reciveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss, recive(gauss)) +receiveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss, receive(gauss)) """ applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64})::Array{Float64,1} diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 881f50cd..8b9762c4 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -52,7 +52,7 @@ function getXYZValues(cube::TDesignCube) end #readmeasurement for (i,sensor) in enumerate(cube.sensors) - measurement[i,:] = reciveMeasurment(sensor) + measurement[i,:] = receiveMeasurment(sensor) end return measurement end From fda38ff85ec62e795c299dc4f2425142311bd0af Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 31 Jan 2023 14:55:50 +0100 Subject: [PATCH 51/68] renaming send to query Comand --- .../ArduinoTemperatureController.jl | 14 ++++---- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 4 +-- src/Devices/Robots/BrukerRobot.jl | 10 +++--- .../Temperature/ArduinoTemperatureSensor.jl | 6 ++-- .../ArduinoSurveillanceUnit.jl | 36 +++++++++---------- .../ArduinoSurveillanceUnitExternalTemp.jl | 6 ++-- .../ArduinoSurveillanceUnitInternalTemp.jl | 4 +-- src/Devices/Utils/Arduino.jl | 6 ++-- 8 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/Devices/Control/Temperature/ArduinoTemperatureController.jl b/src/Devices/Control/Temperature/ArduinoTemperatureController.jl index f210b20b..8f67a10e 100644 --- a/src/Devices/Control/Temperature/ArduinoTemperatureController.jl +++ b/src/Devices/Control/Temperature/ArduinoTemperatureController.jl @@ -117,7 +117,7 @@ end function retrieveTemps(controller::ArduinoTemperatureController, query::AbstractString = "GET:ALLTEMPS") TempDelim = "," # TODO Retrieve temp properly - Temps = sendCommand(controller.ard, query) + Temps = queryCommand(controller.ard, query) result = tryparse.(Float64,split(Temps,TempDelim)) result = map(x -> isnothing(x) ? 0.0 : x, result) return result @@ -127,7 +127,7 @@ export setMaximumTemps function setMaximumTemps(controller::ArduinoTemperatureController, maxTemps::Array) if length(maxTemps) == controller.params.numSensors maxTempString= join(maxTemps, ",") - ack = sendCommand(controller.ard, "SET:TMAX:<"*maxTempString*">") + ack = queryCommand(controller.ard, "SET:TMAX:<"*maxTempString*">") if parse(Bool, ack) @info "Set max temp for ArduinoTemperatureController $(deviceID(controller))." return true @@ -160,7 +160,7 @@ export setTargetTemps function setTargetTemps(controller::ArduinoTemperatureController, targetTemps::Vector{Int64}) if length(targetTemps) == controller.params.numSensors targetTempString= join(targetTemps, ",") - ack = sendCommand(controller.ard, "SET:TSET:<"*targetTempString*">") + ack = queryCommand(controller.ard, "SET:TSET:<"*targetTempString*">") if parse(Bool, ack) @info "Set target temp for ArduinoTemperatureController $(deviceID(controller))." return true @@ -193,7 +193,7 @@ function setControlMode(controller::ArduinoTemperatureController, mode::Temperat else throw(ScannerConfigurationError("Temp Controller does not support mode $mode")) end - return sendCommand(controller.ard, cmd) + return queryCommand(controller.ard, cmd) end #function getControlMode(controller::ArduinoTemperatureController) @@ -202,13 +202,13 @@ end export resetOvertemp function resetOvertemp(controller::ArduinoTemperatureController) - return parse(Bool, sendCommand(controller.ard, "RESET:OVERTEMP")) + return parse(Bool, queryCommand(controller.ard, "RESET:OVERTEMP")) end function enableControl(controller::ArduinoTemperatureController) - return sendCommand(controller.ard, "SET:ENABLE_HEATING:<1>") + return queryCommand(controller.ard, "SET:ENABLE_HEATING:<1>") end function disableControl(controller::ArduinoTemperatureController) - return sendCommand(controller.ard, "SET:ENABLE_HEATING:<0>") + return queryCommand(controller.ard, "SET:ENABLE_HEATING:<0>") end \ No newline at end of file diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 5e98e131..12ac3740 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -211,7 +211,7 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) throw(error("no valid sample size, pick size from 1 to 1024")) end # TODO problem on time out probabil wrong value on device - data_string = sendCommand(gauss.ard, "SAMPLES" * string(sampleSize)) # TODO Check if wanted value was set, otherwise throw error and there query HallSensor for valid > 0 values + data_string = queryCommand(gauss.ard, "SAMPLES" * string(sampleSize)) # TODO Check if wanted value was set, otherwise throw error and there query HallSensor for valid > 0 values updatedSampleSize = parse(Int, data_string) if (updatedSampleSize !== sampleSize) throw(error("wrong sample size set")) @@ -230,7 +230,7 @@ getTemperature(gauss::ArduinoGaussMeter)::Float32 returns tempreture of the sensor, do not expect a high tempreture resolution """ function getTemperature(gauss::ArduinoGaussMeter) - temp_str = sendCommand(gauss.ard, "TEMP") + temp_str = queryCommand(gauss.ard, "TEMP") return parse(Float32, temp_str) end diff --git a/src/Devices/Robots/BrukerRobot.jl b/src/Devices/Robots/BrukerRobot.jl index 35457a00..289d2730 100644 --- a/src/Devices/Robots/BrukerRobot.jl +++ b/src/Devices/Robots/BrukerRobot.jl @@ -42,7 +42,7 @@ const err = "err?\n" dof(rob::BrukerRobot) = 3 axisRange(rob::BrukerRobot) = rob.params.axisRange defaultVelocity(rob::BrukerRobot) = nothing -_getPosition(rob::BrukerRobot) = sendCommand(rob, BrukerCommand(pos)) +_getPosition(rob::BrukerRobot) = queryCommand(rob, BrukerCommand(pos)) _isReferenced(rob::BrukerRobot) = true _enable(rob::BrukerRobot) = nothing _disable(rob::BrukerRobot) = nothing @@ -53,12 +53,12 @@ _doReferenceDrive(rob::BrukerRobot) = nothing """ Move Bruker Robot to center""" function moveCenter(sd::BrukerRobot) - sendCommand(sd, BrukerCommand(center)) + queryCommand(sd, BrukerCommand(center)) end """ Move Bruker Robot to park""" function movePark(sd::BrukerRobot) - sendCommand(sd, BrukerCommand(park)) + queryCommand(sd, BrukerCommand(park)) end function _moveAbs(rob::BrukerRobot, pos::Vector{<:Unitful.Length}, speed::Union{Vector{<:Unitful.Velocity},Nothing}) @@ -66,7 +66,7 @@ function _moveAbs(rob::BrukerRobot, pos::Vector{<:Unitful.Length}, speed::Union{ @warn "BrukerRobot does not support setting velocities!" end cmd = BrukerCommand("goto $(ustrip(Float64, u"mm", pos[1])),$(ustrip(Float64, u"mm", pos[2])),$(ustrip(Float64, u"mm", pos[3]))\n") - res = sendCommand(rob, cmd) + res = queryCommand(rob, cmd) end """ Not Implemented """ @@ -76,7 +76,7 @@ end """ Send Command `sendCommand(sd::BrukerRobot, brukercmd::BrukerCommand)`""" -function sendCommand(sd::BrukerRobot, brukercmd::BrukerCommand) +function queryCommand(sd::BrukerRobot, brukercmd::BrukerCommand) (result, startmovetime, endmovetime) = _sendCommand(sd, brukercmd) diff --git a/src/Devices/Sensors/Temperature/ArduinoTemperatureSensor.jl b/src/Devices/Sensors/Temperature/ArduinoTemperatureSensor.jl index db346565..beb96bed 100644 --- a/src/Devices/Sensors/Temperature/ArduinoTemperatureSensor.jl +++ b/src/Devices/Sensors/Temperature/ArduinoTemperatureSensor.jl @@ -112,7 +112,7 @@ end function retrieveTemps(sensor::ArduinoTemperatureSensor) TempDelim = "," - Temps = sendCommand(sensor.ard, "GET:ALLTEMPS") + Temps = queryCommand(sensor.ard, "GET:ALLTEMPS") Temps = Temps[7:end] #filter out "TEMPS:" at beginning of answer result = tryparse.(Float64,split(Temps,TempDelim)) @@ -123,7 +123,7 @@ end function setMaximumTemps(sensor::ArduinoTemperatureSensor, maxTemps::Array) if length(maxTemps) == sensor.params.numSensors maxTempString= join(maxTemps, ",") - ack = sendCommand(sensor.ard, "SET:MAXTEMPS:<"*maxTempString*">") + ack = queryCommand(sensor.ard, "SET:MAXTEMPS:<"*maxTempString*">") # TODO check ack? @info "acknowledge of MaxTemps from TempUnit." else @@ -132,7 +132,7 @@ function setMaximumTemps(sensor::ArduinoTemperatureSensor, maxTemps::Array) end function getMaximumTemps(sensor::ArduinoTemperatureSensor) - println(sendCommand(sensor.ard, "GET:MAXTEMPS")) + println(queryCommand(sensor.ard, "GET:MAXTEMPS")) end close(sensor::ArduinoTemperatureSensor) = close(sensor.ard) diff --git a/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnit.jl b/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnit.jl index 60dfbbc5..2b6e7c36 100644 --- a/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnit.jl +++ b/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnit.jl @@ -28,94 +28,94 @@ function parseBool(ard::ArduinoSurveillanceUnit, s::Char) end function arEnableWatchDog(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "ENABLE:WD") + ACQ = queryCommand(Arduino, "ENABLE:WD") checkACQ(Arduino, ACQ) end function arDisableWatchDog(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "DISABLE:WD") + ACQ = queryCommand(Arduino, "DISABLE:WD") checkACQ(Arduino, ACQ) end function getDigital(Arduino::ArduinoSurveillanceUnit, DIO::Int) - DIO = sendCommand(Arduino, "GET:DIGITAL:" * string(DIO)) + DIO = queryCommand(Arduino, "GET:DIGITAL:" * string(DIO)) return DIO; end function getAnalog(Arduino::ArduinoSurveillanceUnit, ADC::Int) - ADC = sendCommand(Arduino, "GET:ANALOG:A" * string(ADC)) + ADC = queryCommand(Arduino, "GET:ANALOG:A" * string(ADC)) return ADC; end function getErrorStatus(Arduino::ArduinoSurveillanceUnit) - Errorcode = sendCommand(Arduino, "GET:STATUS"); + Errorcode = queryCommand(Arduino, "GET:STATUS"); ErrorcodeBool = [parseBool(Arduino, x) for x in Errorcode] return ErrorcodeBool end function resetWatchDog(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "RESET:WD") + ACQ = queryCommand(Arduino, "RESET:WD") checkACQ(Arduino, ACQ) end function enableWatchDog(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "ENABLE:WD") + ACQ = queryCommand(Arduino, "ENABLE:WD") checkACQ(Arduino, ACQ) end function disableWatchDog(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "DISABLE:WD") + ACQ = queryCommand(Arduino, "DISABLE:WD") checkACQ(Arduino, ACQ) end function resetFail(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "RESET:FAIL") + ACQ = queryCommand(Arduino, "RESET:FAIL") checkACQ(Arduino, ACQ) end function disableSurveillance(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "DISABLE:SURVEILLANCE") + ACQ = queryCommand(Arduino, "DISABLE:SURVEILLANCE") checkACQ(Arduino, ACQ) end function enableSurveillance(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "ENABLE:SURVEILLANCE") + ACQ = queryCommand(Arduino, "ENABLE:SURVEILLANCE") checkACQ(Arduino, ACQ) end function getCycletime(Arduino::ArduinoSurveillanceUnit) - tcycle = sendCommand(Arduino, "GET:CYCLETIME") + tcycle = queryCommand(Arduino, "GET:CYCLETIME") return tcycle; end function resetArduino(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "RESET:ARDUINO") + ACQ = queryCommand(Arduino, "RESET:ARDUINO") checkACQ(Arduino, ACQ) end function enableACPower(Arduino::ArduinoSurveillanceUnit) - reply = sendCommand(Arduino, "ENABLE:AC"); + reply = queryCommand(Arduino, "ENABLE:AC"); if !parse(Bool, reply) error("AC could not be enabled. Check SU fail") end end function disableACPower(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "DISABLE:AC"); + ACQ = queryCommand(Arduino, "DISABLE:AC"); checkACQ(Arduino, ACQ) end function enableHeating(ard::ArduinoSurveillanceUnit) - ACQ = sendCommand(ard, "ENABLE:HEATING"); + ACQ = queryCommand(ard, "ENABLE:HEATING"); checkACQ(Arduino, ACQ) end function disableHeating(ard::ArduinoSurveillanceUnit) - ACQ = sendCommand(ard, "DISABLE:HEATING"); + ACQ = queryCommand(ard, "DISABLE:HEATING"); checkACQ(Arduino, ACQ) end # TODO this does not seem to be implemented in external client, check server code function NOTAUS(Arduino::ArduinoSurveillanceUnit) - ACQ = sendCommand(Arduino, "NOTAUS"); + ACQ = queryCommand(Arduino, "NOTAUS"); checkACQ(Arduino, ACQ) end diff --git a/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnitExternalTemp.jl b/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnitExternalTemp.jl index 5b16dcbd..fb9ccb4c 100644 --- a/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnitExternalTemp.jl +++ b/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnitExternalTemp.jl @@ -26,7 +26,7 @@ end Base.close(su::ArduinoSurveillanceUnitExternalTemp) = close(su.ard) -sendCommand(su::ArduinoSurveillanceUnitExternalTemp, cmdString::String) = sendCommand(su.ard, cmdString) +queryCommand(su::ArduinoSurveillanceUnitExternalTemp, cmdString::String) = queryCommand(su.ard, cmdString) neededDependencies(::ArduinoSurveillanceUnitExternalTemp) = [ArduinoTemperatureSensor] # could in theory be generic temp sensor optionalDependencies(::ArduinoSurveillanceUnitExternalTemp) = [SerialPortPool] @@ -73,12 +73,12 @@ function getTemperatures(su::ArduinoSurveillanceUnitExternalTemp; names::Bool=fa end function getStatus(su::ArduinoSurveillanceUnitExternalTemp) - status = sendCommand(su,"GET:STATS") + status = queryCommand(su,"GET:STATS") return status end function resetDAQ(su::ArduinoSurveillanceUnitExternalTemp) - ACQ = sendCommand(su,"RESET:RP") + ACQ = queryCommand(su,"RESET:RP") checkACQ(su, ACQ) end diff --git a/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnitInternalTemp.jl b/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnitInternalTemp.jl index fe00fd30..166c57b7 100644 --- a/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnitInternalTemp.jl +++ b/src/Devices/SurveillanceUnit/ArduinoSurveillanceUnitInternalTemp.jl @@ -55,10 +55,10 @@ function _init(su::ArduinoSurveillanceUnitInternalTemp) #end end -sendCommand(su::ArduinoSurveillanceUnitInternalTemp, cmdString::String) = sendCommand(su.ard, cmdString) +queryCommand(su::ArduinoSurveillanceUnitInternalTemp, cmdString::String) = queryCommand(su.ard, cmdString) function getTemperatures(Arduino::ArduinoSurveillanceUnitInternalTemp) # toDo: deprecated - Temps = sendCommand(Arduino, "GET:TEMP") + Temps = queryCommand(Arduino, "GET:TEMP") TempDelim = "T" temp = tryparse.(Float64, split(Temps, TempDelim)) diff --git a/src/Devices/Utils/Arduino.jl b/src/Devices/Utils/Arduino.jl index 17572181..f42cf4fe 100644 --- a/src/Devices/Utils/Arduino.jl +++ b/src/Devices/Utils/Arduino.jl @@ -1,4 +1,4 @@ -export Arduino, sendCommand +export Arduino, queryCommand abstract type Arduino <: Device end @@ -7,13 +7,13 @@ abstract type Arduino <: Device end @mustimplement cmdEnd(ard::Arduino) @mustimplement serialDevice(ard::Arduino) -function sendCommand(ard::Arduino, cmdString::String) +function queryCommand(ard::Arduino, cmdString::String) cmd = cmdStart(ard) * cmdString * cmdEnd(ard) sd = serialDevice(ard) return query(sd, cmd) end -function sendCommand(ard::Arduino, cmdString::String, data::AbstractArray) +function queryCommand(ard::Arduino, cmdString::String, data::AbstractArray) cmd = cmdStart(ard) * cmdString * cmdEnd(ard) return query!(serialDevice(ard), cmd, data, delimited = true) end From bd9bbcc9cf24fe7fb5e49d5fe73ba7a36b0f2bed Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 31 Jan 2023 15:09:59 +0100 Subject: [PATCH 52/68] Adding send and recive to Arduino.jl and using it in Gaussmeter --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 21 ++++++--------------- src/Devices/Utils/Arduino.jl | 12 ++++++++++++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 12ac3740..9f57282c 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -48,6 +48,7 @@ Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter ard::Union{SimpleArduino,Nothing} = nothing rotatedCalibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) sampleSize::Int = 0 + gauss.measdelay = 1000 measurementTriggered::Bool = false end @@ -60,7 +61,7 @@ function _init(gauss::ArduinoGaussMeter) @info "Connection to ArduinoGaussMeter established." ard = SimpleArduino(; commandStart=params.commandStart, commandEnd=params.commandEnd, sd=sd) gauss.ard = ard - gaus.measdelay = query(sd, "!DELAY*") + gauss.measdelay = query(sd, "!DELAY*") setSampleSize(gauss, params.sampleSize) end @@ -120,17 +121,11 @@ end start measurment in sensor 'gauss' """ function triggerMeasurment(gauss::ArduinoGaussMeter) - cmd = cmdStart(gauss.ard) * "DATA" * cmdEnd(gauss.ard) - sd = gauss.ard.sd - lock(sd.sdLock) - if gauss.measurementTriggered throw("measurement already triggered") - end - sp_flush(sd.sp, SP_BUF_INPUT) - send(sd, cmd) + end + send(gauss.ard, "DATA") gauss.measurementTriggered = true - end @@ -148,18 +143,14 @@ function receive(gauss::ArduinoGaussMeter) if !gauss.measurementTriggered throw("triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first") else - temp = get_timeout(gauss.ard) - timeout_ms = max(1000, floor(Int, gauss.sampleSize * 10 * 1.2) + 1) + timeout_ms = max(1000, floor(Int, gauss.sampleSize * gauss.measdelay* 1.2) + 1) set_timeout(gauss.ard, timeout_ms) try - data_strings = split(receive(gauss.ard.sd), ",") + data_strings = split(receive(gauss.ard), ",") data = [parse(Float64, str) for str in data_strings] return data finally - sd = gauss.ard.sd - sp_flush(sd.sp, SP_BUF_INPUT) # TODO Why flush? - unlock(sd.sdLock) set_timeout(gauss.ard, temp) gauss.measurementTriggered = false end diff --git a/src/Devices/Utils/Arduino.jl b/src/Devices/Utils/Arduino.jl index f42cf4fe..35c661f8 100644 --- a/src/Devices/Utils/Arduino.jl +++ b/src/Devices/Utils/Arduino.jl @@ -7,6 +7,18 @@ abstract type Arduino <: Device end @mustimplement cmdEnd(ard::Arduino) @mustimplement serialDevice(ard::Arduino) +function sendCommand(ard::Arduino,cmdString::String) + cmd = cmdStart(ard) * cmdString * cmdEnd(ard) + sd = serialDevice(ard) + send(sd, cmd) + return nothing +end + +function receive(ard::Arduino) + sd = serialDevice(ard) + return receive(sd) +end + function queryCommand(ard::Arduino, cmdString::String) cmd = cmdStart(ard) * cmdString * cmdEnd(ard) sd = serialDevice(ard) From 3fd3248479409b37ede0c5247f1f08d4889a0589 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 31 Jan 2023 16:14:26 +0100 Subject: [PATCH 53/68] parsing delay --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 9f57282c..15f7951c 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -48,7 +48,7 @@ Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter ard::Union{SimpleArduino,Nothing} = nothing rotatedCalibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) sampleSize::Int = 0 - gauss.measdelay = 1000 + measdelay = 1000 measurementTriggered::Bool = false end @@ -61,7 +61,7 @@ function _init(gauss::ArduinoGaussMeter) @info "Connection to ArduinoGaussMeter established." ard = SimpleArduino(; commandStart=params.commandStart, commandEnd=params.commandEnd, sd=sd) gauss.ard = ard - gauss.measdelay = query(sd, "!DELAY*") + gauss.measdelay = parse(Int64,query(sd, "!DELAY*")) setSampleSize(gauss, params.sampleSize) end @@ -124,7 +124,7 @@ function triggerMeasurment(gauss::ArduinoGaussMeter) if gauss.measurementTriggered throw("measurement already triggered") end - send(gauss.ard, "DATA") + sendCommand(gauss.ard, "DATA") gauss.measurementTriggered = true end From c601e9170a9ebd35a262a227c7446376f1ff9d2c Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 31 Jan 2023 16:18:49 +0100 Subject: [PATCH 54/68] fixing spelling wrong variable names spelling --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 15f7951c..33c1144e 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -181,7 +181,7 @@ Varianz can't be calibrated function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] # TODO Sanity checks on data, does it have the expected size - calibrated_means = gaus.params.rotation * (gauss.params.calibrate * means + gauss.params.biasCalibration) + calibrated_means = gauss.params.rotation * (gauss.params.calibrate * means + gauss.params.biasCalibration) return calibrated_means end From ea4c2d58ed7fe5f6212a52f71e8d24ba35c5084d Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 31 Jan 2023 16:24:41 +0100 Subject: [PATCH 55/68] bias has to be subtrakted not added due to new calibration --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 33c1144e..47de3159 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -181,7 +181,7 @@ Varianz can't be calibrated function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] # TODO Sanity checks on data, does it have the expected size - calibrated_means = gauss.params.rotation * (gauss.params.calibrate * means + gauss.params.biasCalibration) + calibrated_means = gauss.params.rotation * (gauss.params.calibration * means - gauss.params.biasCalibration) return calibrated_means end From ebdf4501279faef9b738903988e4659ae709ce93 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 15 Mar 2023 16:21:30 +0100 Subject: [PATCH 56/68] Applying bias correction on raw data --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 47de3159..088975b9 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -181,7 +181,7 @@ Varianz can't be calibrated function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] # TODO Sanity checks on data, does it have the expected size - calibrated_means = gauss.params.rotation * (gauss.params.calibration * means - gauss.params.biasCalibration) + calibrated_means = gauss.params.rotation * (gauss.params.calibration * (means - gauss.params.biasCalibration)) return calibrated_means end From 2196206292070d6c2b450a1068d73c87aa6f2c54 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 6 Apr 2023 17:28:07 +0200 Subject: [PATCH 57/68] WIP not testet --- Devices/Arduino/HallSensor/HallSensor.ino | 135 ++++++++++++++------ src/Devices/GaussMeter/ArduinoGaussMeter.jl | 18 ++- 2 files changed, 108 insertions(+), 45 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index dde9d272..ed1e3873 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -8,7 +8,7 @@ // Tlv493d Opject Tle493d_w2b6 sensor = Tle493d_w2b6(Tle493d::MASTERCONTROLLEDMODE); int sample_size = 1000; - +bool fast_mode_on = false; // Communication #define INPUT_BUFFER_SIZE 3000 #define VALUE_BUFFER_SIZE 1024 @@ -23,6 +23,7 @@ int getTemp(char *); int getVersion(char *); int getCommands(char *); int setSampleSize(char *); +int setFastMode(char *); typedef struct { @@ -36,7 +37,8 @@ commandHandler_t cmdHandler[] = { {"TEMP", getTemp}, {"VERSION", getVersion}, {"COMMANDS", getCommands}, - {"SAMPLES", setSampleSize}}; + {"SAMPLES", setSampleSize}, + {"FASTMODE", setFastMode}}; int getCommands(char *) { @@ -47,7 +49,8 @@ int getCommands(char *) Serial.print("'!TEMP*#' "); Serial.print("'!COMMANDS*#' "); Serial.print("'!SAMPLESx*# 1>=x>=1024' "); - Serial.println("#"); + Serial.print("'!FASTMODE*#x =1|0'") + Serial.println("#"); } bool updateBufferUntilDelim(char delim) @@ -172,60 +175,90 @@ int getData(char *) // calculating mean values Mean =sum(x[i])/sample_size // measurement is stored for calculating the variance in a second go - for (int i = 0; i < sample_size * 3; i += 3) + if fast_mode_on { - sensor.updateData(); - start = millis(); - - x = sensor.getRawX(); - y = sensor.getRawY(); - z = sensor.getRawZ(); + for (int i = 0; i < sample_size * 3; i += 3) + { + sensor.updateData(); + start = millis(); - value_buffer[i] = x; - value_buffer[i + 1] = y; - value_buffer[i + 2] = z; + x = sensor.getRawX(); + y = sensor.getRawY(); + z = sensor.getRawZ(); - sumX += x; - sumY += y; - sumZ += z; + sumX += x; + sumY += y; + sumZ += z; - // waiting until new data is ready in the sensor - end = millis(); - if (end - start < MEASDELAY) - { - delay(MEASDELAY - (end - start)); + // waiting until new data is ready in the sensor + end = millis(); + if (end - start < MEASDELAY) + { + delay(MEASDELAY - (end - start)); + } } } - meanX = (float)sumX / sample_size; - meanY = (float)sumY / sample_size; - meanZ = (float)sumZ / sample_size; - - // calculating var var(x) = sum((x[i]-x_mean)^2)/(sample_size-1) - // for sample_size = 1 the variance is zero - if (sample_size > 1) + else { for (int i = 0; i < sample_size * 3; i += 3) { - sumXX += ((float)value_buffer[i] - meanX) * ((float)value_buffer[i] - meanX); - sumYY += ((float)value_buffer[i + 1] - meanY) * ((float)value_buffer[i + 1] - meanY); - sumZZ += ((float)value_buffer[i + 2] - meanZ) * ((float)value_buffer[i + 2] - meanZ); - } + sensor.updateData(); + start = millis(); + + x = sensor.getRawX(); + y = sensor.getRawY(); + z = sensor.getRawZ(); + + value_buffer[i] = x; + value_buffer[i + 1] = y; + value_buffer[i + 2] = z; - varX = sumXX / (sample_size - 1); - varY = sumYY / (sample_size - 1); - varZ = sumZZ / (sample_size - 1); + sumX += x; + sumY += y; + sumZ += z; + + // waiting until new data is ready in the sensor + end = millis(); + if (end - start < MEASDELAY) + { + delay(MEASDELAY - (end - start)); + } + } } + meanX = (float)sumX / sample_size; + meanY = (float)sumY / sample_size; + meanZ = (float)sumZ / sample_size; + Serial.print(meanX, 7); Serial.print(","); Serial.print(meanY, 7); Serial.print(","); Serial.print(meanZ, 7); - Serial.print(","); - Serial.print(varX, 7); - Serial.print(","); - Serial.print(varY, 7); - Serial.print(","); - Serial.print(varZ, 7); + + if !fast_mode_on + { + // calculating var var(x) = sum((x[i]-x_mean)^2)/(sample_size-1) + // for sample_size = 1 the variance is zero + if (sample_size > 1) + { + for (int i = 0; i < sample_size * 3; i += 3) + { + sumXX += ((float)value_buffer[i] - meanX) * ((float)value_buffer[i] - meanX); + sumYY += ((float)value_buffer[i + 1] - meanY) * ((float)value_buffer[i + 1] - meanY); + sumZZ += ((float)value_buffer[i + 2] - meanZ) * ((float)value_buffer[i + 2] - meanZ); + } + + varX = sumXX / (sample_size - 1); + varY = sumYY / (sample_size - 1); + varZ = sumZZ / (sample_size - 1); + } + Serial.print(","); + Serial.print(varX, 7); + Serial.print(","); + Serial.print(varY, 7); + Serial.print(","); + Serial.print(varZ, 7); + } Serial.println("#"); Serial.flush(); } @@ -257,7 +290,7 @@ int getVersion(char *) int setSampleSize(char *command) { - int value_int = atoi(command + 7); + int value_int = atoi(command + 7); if (value_int > 0 && value_int <= VALUE_BUFFER_SIZE) { sample_size = value_int; @@ -266,6 +299,24 @@ int setSampleSize(char *command) Serial.println("#"); } +int SetFastMode(char *command) +{ + int value_int = atoi(command+5) + if ( value_int ==0){ + fast_mode_on = false; + } + else if(value_int == 1){ + fast_mode_on = true; + } + else{ + Serial.print("value must be 0 or 1"); + Serial.println("#"); + return + } + Serial.print(fast_mode_on); + Serial.println("#"); +} + void loop() { serialCommand(); diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 088975b9..ceca5f82 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -10,6 +10,7 @@ Base.@kwdef struct ArduinoGaussMeterDirectParams <: ArduinoGaussMeterParams translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration = Vector{Float64} = [0.0, 0.0, 0.0] sampleSize::Int + fastModeOn::Bool @add_serial_device_fields "#" @add_arduino_fields "!" "*" end @@ -24,6 +25,7 @@ Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration::Vector{Float64} = [0.0, 0.0, 0.0] sampleSize::Int + fastModeOn::Bool @add_serial_device_fields "#" @add_arduino_fields "!" "*" @@ -50,6 +52,7 @@ Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter sampleSize::Int = 0 measdelay = 1000 measurementTriggered::Bool = false + fastModeOn:: Bool = false end neededDependencies(::ArduinoGaussMeter) = [] @@ -61,8 +64,9 @@ function _init(gauss::ArduinoGaussMeter) @info "Connection to ArduinoGaussMeter established." ard = SimpleArduino(; commandStart=params.commandStart, commandEnd=params.commandEnd, sd=sd) gauss.ard = ard - gauss.measdelay = parse(Int64,query(sd, "!DELAY*")) + gauss.measdelay = parse(Int64, query(sd, "!DELAY*")) setSampleSize(gauss, params.sampleSize) + setFast(gauss,params.fastModeOn) end function initSerialDevice(gauss::ArduinoGaussMeter, params::ArduinoGaussMeterDirectParams) @@ -123,7 +127,7 @@ end function triggerMeasurment(gauss::ArduinoGaussMeter) if gauss.measurementTriggered throw("measurement already triggered") - end + end sendCommand(gauss.ard, "DATA") gauss.measurementTriggered = true end @@ -144,7 +148,7 @@ function receive(gauss::ArduinoGaussMeter) throw("triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first") else temp = get_timeout(gauss.ard) - timeout_ms = max(1000, floor(Int, gauss.sampleSize * gauss.measdelay* 1.2) + 1) + timeout_ms = max(1000, floor(Int, gauss.sampleSize * gauss.measdelay * 1.2) + 1) set_timeout(gauss.ard, timeout_ms) try data_strings = split(receive(gauss.ard), ",") @@ -211,6 +215,14 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) return updatedSampleSize end +function setFast(gauss:ArduinoGaussMeter, on::Bool) + data_string = queryCommand(gauss.ard, "FAST" * string(Int(on))) + updatedSampleSize = parse(Bool, data_string) + gauss.FastModeOn = updatedSampleSize + return gauss.FastModeOn +end + + function getSampleSize(gauss::ArduinoGaussMeter) return gauss.sampleSize end From e9b2a327ca0298697e63669e4cf8085bd725f0b9 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 12 Apr 2023 13:43:15 +0200 Subject: [PATCH 58/68] fixing fast mode --- Devices/Arduino/HallSensor/HallSensor.ino | 30 ++++++++++++--------- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 21 ++++++++++----- src/Devices/GaussMeter/TDesignCube.jl | 16 ++++++++++- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index ed1e3873..09083a8a 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,5 +1,5 @@ #define ARDUINO_TYPE "HALLSENS" -#define VERSION "3.0" +#define VERSION "4.0" #define BAUDRATE 9600 #define MEASDELAY 2 @@ -49,8 +49,8 @@ int getCommands(char *) Serial.print("'!TEMP*#' "); Serial.print("'!COMMANDS*#' "); Serial.print("'!SAMPLESx*# 1>=x>=1024' "); - Serial.print("'!FASTMODE*#x =1|0'") - Serial.println("#"); + Serial.print("'!FASTMODEx*# x =1|0'"); + Serial.println("#"); } bool updateBufferUntilDelim(char delim) @@ -175,7 +175,7 @@ int getData(char *) // calculating mean values Mean =sum(x[i])/sample_size // measurement is stored for calculating the variance in a second go - if fast_mode_on + if (fast_mode_on) { for (int i = 0; i < sample_size * 3; i += 3) { @@ -235,7 +235,7 @@ int getData(char *) Serial.print(","); Serial.print(meanZ, 7); - if !fast_mode_on + if (!fast_mode_on) { // calculating var var(x) = sum((x[i]-x_mean)^2)/(sample_size-1) // for sample_size = 1 the variance is zero @@ -297,24 +297,30 @@ int setSampleSize(char *command) } Serial.print(sample_size); Serial.println("#"); + Serial.flush(); } -int SetFastMode(char *command) +int setFastMode(char *command) { - int value_int = atoi(command+5) - if ( value_int ==0){ + int value_int = atoi(command + 8); + if (value_int == 0) + { fast_mode_on = false; } - else if(value_int == 1){ + else if (value_int == 1) + { fast_mode_on = true; } - else{ + else + { Serial.print("value must be 0 or 1"); - Serial.println("#"); - return + Serial.println("#"); + Serial.flush(); + return 0; } Serial.print(fast_mode_on); Serial.println("#"); + Serial.flush(); } void loop() diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index ceca5f82..321cfa2f 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -84,7 +84,7 @@ end function checkSerialDevice(gauss::ArduinoGaussMeter, sd::SerialDevice) try reply = query(sd, "!VERSION*") - if !(startswith(reply, "HALLSENS:3")) + if !(startswith(reply, "HALLSENS:4")) close(sd) throw(ScannerConfigurationError(string("Connected to wrong Device", reply))) end @@ -215,11 +215,20 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) return updatedSampleSize end -function setFast(gauss:ArduinoGaussMeter, on::Bool) - data_string = queryCommand(gauss.ard, "FAST" * string(Int(on))) - updatedSampleSize = parse(Bool, data_string) - gauss.FastModeOn = updatedSampleSize - return gauss.FastModeOn +function setFast(gauss::ArduinoGaussMeter, fastModeOn::Bool) + data_string = queryCommand(gauss.ard, "FASTMODE" * string(Int(fastModeOn))) + @info(fastModeOn) + @info(data_string) + updatedFastMode = parse(Int, data_string) + println(updatedFastMode) + println(typeof(updatedFastMode)) + println(updatedFastMode != fastModeOn) + if updatedFastMode != fastModeOn + throw(error("FastMode not set right")) + end + gauss.fastModeOn = updatedFastMode + + return gauss.fastModeOn end diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 8b9762c4..75f0ba07 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -5,6 +5,7 @@ Base.@kwdef struct TDesignCubeParams <: DeviceParams N::Int64 radius::typeof(1.0u"mm") = 0.0u"mm" sampleSize:: Int64 = 100 + fastModeOn:: Bool = false end TDesignCubeParams(dict::Dict) = params_from_dict(TDesignCubeParams, dict) @@ -20,6 +21,7 @@ optionalDependencies(::TDesignCube) = [] function _init(cube::TDesignCube) sampleSize = cube.params.sampleSize + fastModeOn = cube.params.fastModeOn sensors = dependencies(cube, ArduinoGaussMeter) if length(sensors) != cube.params.N close.(sensors) # TODO @NH Should not close devices here @@ -27,7 +29,8 @@ function _init(cube::TDesignCube) end sort!(sensors,by=x-> x.params.position) cube.sensors = sensors - setSampleSize(cube,cube.sampleSize) + setSampleSize(cube,sampleSize) + setFast(cube,fastModeOn) end export setSampleSize @@ -41,6 +44,17 @@ function setSampleSize(cube::TDesignCube,sampleSize::Int) cube.sampleSize = sampleSize end +export setFast +function setFast(cube::TDesignCube,fastModeOn::Bool) + for sensor in cube.sensors + returnFastModeOn = setFast(sensor,fastModeOn) + if returnFastModeOn != fastModeOn + throw("sensors coud not be updated") + end + end + cube.sampleSize = fastModeOn +end + export getSampleSize getSampleSize(cube::TDesignCube) = cube.params.sampleSize From 9fceb7742f2714b4698bc283eeffd73a8e44914c Mon Sep 17 00:00:00 2001 From: vincent Date: Fri, 14 Apr 2023 15:36:21 +0200 Subject: [PATCH 59/68] changed positon to position in design --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 321cfa2f..d9793999 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -19,7 +19,7 @@ ArduinoGaussMeterDirectParams(dict::Dict) = params_from_dict(ArduinoGaussMeterDi Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams description::String - position::Int64 = 1 + position::Vector{Float64} = [0.0, 0.0, 0.0] calibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) * 0.125 rotation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) From 9009745a65b81b66b09ba1160ef89c39fa2ba24b Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 31 May 2023 14:15:07 +0200 Subject: [PATCH 60/68] adding positions --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 23 ++++++++++++--------- src/Devices/GaussMeter/TDesignCube.jl | 15 ++++++++++++-- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index d9793999..c0949609 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -1,4 +1,4 @@ -export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams, getRawXYZValues, getXValue, triggerMeasurment, receive, receiveMeasurment, setSampleSize, getSampleSize, getTemperature +export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams, getRawXYZValues, getXYZValues,triggerMeasurment, receive, receiveMeasurment, setSampleSize, getSampleSize, getTemperature abstract type ArduinoGaussMeterParams <: DeviceParams end @@ -64,7 +64,7 @@ function _init(gauss::ArduinoGaussMeter) @info "Connection to ArduinoGaussMeter established." ard = SimpleArduino(; commandStart=params.commandStart, commandEnd=params.commandEnd, sd=sd) gauss.ard = ard - gauss.measdelay = parse(Int64, query(sd, "!DELAY*")) + gauss.measdelay = parse(Int64, queryCommand(ard, "DELAY*")) setSampleSize(gauss, params.sampleSize) setFast(gauss,params.fastModeOn) end @@ -120,10 +120,11 @@ function getXYZValues(gauss::ArduinoGaussMeter) return applyCalibration(gauss, data) end + """ triggerMeasurment(gauss::ArduinoGaussMeter) start measurment in sensor 'gauss' -""" + """ function triggerMeasurment(gauss::ArduinoGaussMeter) if gauss.measurementTriggered throw("measurement already triggered") @@ -189,10 +190,11 @@ function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) return calibrated_means end + """ - setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int)::Int +setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int)::Int - sets sample size for measurment +sets sample size for measurment #Arguments -`sampleSize` number of mesurments done by the sensor 1=>sample_size<=1024 @@ -217,12 +219,7 @@ end function setFast(gauss::ArduinoGaussMeter, fastModeOn::Bool) data_string = queryCommand(gauss.ard, "FASTMODE" * string(Int(fastModeOn))) - @info(fastModeOn) - @info(data_string) updatedFastMode = parse(Int, data_string) - println(updatedFastMode) - println(typeof(updatedFastMode)) - println(updatedFastMode != fastModeOn) if updatedFastMode != fastModeOn throw(error("FastMode not set right")) end @@ -246,4 +243,10 @@ function getTemperature(gauss::ArduinoGaussMeter) return parse(Float32, temp_str) end + +getPosition(gauss::ArduinoGaussMeter) = gauss.params.position + +getXValue(gauss::ArduinoGaussMeter) = getXYZValues(gauss)[1] +getYValue(gauss::ArduinoGaussMeter) = getXYZValues(gauss)[2] +getZValue(gauss::ArduinoGaussMeter) = getXYZValues(gauss)[3] close(gauss::ArduinoGaussMeter) = close(gauss.ard) \ No newline at end of file diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 75f0ba07..c8fd4e93 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -1,4 +1,4 @@ -export TDesignCubeParams, TDesignCube, setSampleSize, getSampleSize, getT, getN, getRadius +export TDesignCubeParams, TDesignCube, setSampleSize, getSampleSize, getT, getN, getRadius, getPositions Base.@kwdef struct TDesignCubeParams <: DeviceParams T::Int64 @@ -27,6 +27,7 @@ function _init(cube::TDesignCube) close.(sensors) # TODO @NH Should not close devices here throw("missing Sensors") end + #TODO chang sorting to sensor sort!(sensors,by=x-> x.params.position) cube.sensors = sensors setSampleSize(cube,sampleSize) @@ -75,6 +76,16 @@ getT(cube::TDesignCube) = cube.params.T getN(cube::TDesignCube) = cube.params.N getRadius(cube::TDesignCube) = cube.params.radius +function getPositions(cube) + positions = zeros(cube.params.N,3) + for (i,sensor) in cube.sensorss + positions[i,:] = getPositions(sensor) + end + return positions +end + function close(cube::TDesignCube) - # NOP + for sensor in cube.sensors + close(sensor) + end end \ No newline at end of file From 08670ec8e7f7bb9450a3fdb31db9624c081b7e11 Mon Sep 17 00:00:00 2001 From: vincent Date: Fri, 2 Jun 2023 13:21:10 +0200 Subject: [PATCH 61/68] fixing hallsensor to singlepass --- Devices/Arduino/HallSensor/HallSensor.ino | 165 ++++++-------------- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 26 +-- src/Devices/GaussMeter/TDesignCube.jl | 14 -- 3 files changed, 53 insertions(+), 152 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 09083a8a..17a1067e 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,21 +1,21 @@ #define ARDUINO_TYPE "HALLSENS" -#define VERSION "4.0" +#define VERSION "5.0" #define BAUDRATE 9600 -#define MEASDELAY 2 +#define MEASDELAY 1 +#define INPUT_BUFFER_SIZE 3000 +#define MAX_SAMPLE_SIZE 10000 #include // Tlv493d Opject Tle493d_w2b6 sensor = Tle493d_w2b6(Tle493d::MASTERCONTROLLEDMODE); -int sample_size = 1000; -bool fast_mode_on = false; + // Communication -#define INPUT_BUFFER_SIZE 3000 -#define VALUE_BUFFER_SIZE 1024 char input_buffer[INPUT_BUFFER_SIZE]; -int16_t value_buffer[VALUE_BUFFER_SIZE * 3]; unsigned int input_pos = 0; +int sample_size = 1000; + int getDelay(char *); int getData(char *); int getPosition(char *); @@ -23,7 +23,6 @@ int getTemp(char *); int getVersion(char *); int getCommands(char *); int setSampleSize(char *); -int setFastMode(char *); typedef struct { @@ -37,8 +36,7 @@ commandHandler_t cmdHandler[] = { {"TEMP", getTemp}, {"VERSION", getVersion}, {"COMMANDS", getCommands}, - {"SAMPLES", setSampleSize}, - {"FASTMODE", setFastMode}}; + {"SAMPLES", setSampleSize}}; int getCommands(char *) { @@ -48,8 +46,7 @@ int getCommands(char *) Serial.print("'!VERSION*#' "); Serial.print("'!TEMP*#' "); Serial.print("'!COMMANDS*#' "); - Serial.print("'!SAMPLESx*# 1>=x>=1024' "); - Serial.print("'!FASTMODEx*# x =1|0'"); + Serial.print("'!SAMPLESx*# 1>=x>=10000' "); Serial.println("#"); } @@ -162,103 +159,62 @@ int getDelay(char *) int getData(char *) { - unsigned long start, end, startFP, endA; + unsigned long start; int16_t x = 0, y = 0, z = 0; + int16_t x_1 = 0, y_1 = 0, z_1 = 0; int32_t sumX = 0, sumY = 0, sumZ = 0; - float sumXX = 0, sumYY = 0, sumZZ = 0; + int32_t sumXX = 0, sumYY = 0, sumZZ = 0; float varX = 0, varY = 0, varZ = 0; float meanX = 0, meanY = 0, meanZ = 0; // updateData reads values from sensor and reading triggers next measurement sensor.updateData(); delay(MEASDELAY); - - // calculating mean values Mean =sum(x[i])/sample_size - // measurement is stored for calculating the variance in a second go - if (fast_mode_on) + + //storing first measurement as approximation of mean value + x_1 = sensor.getRawX(); + y_1 = sensor.getRawY(); + z_1 = sensor.getRawZ(); + + for (int i = 0; i < sample_size; i += 1) { - for (int i = 0; i < sample_size * 3; i += 3) - { - sensor.updateData(); - start = millis(); - - x = sensor.getRawX(); - y = sensor.getRawY(); - z = sensor.getRawZ(); - - sumX += x; - sumY += y; - sumZ += z; - - // waiting until new data is ready in the sensor - end = millis(); - if (end - start < MEASDELAY) - { - delay(MEASDELAY - (end - start)); - } - } + sensor.updateData(); + start = millis(); + + x = sensor.getRawX()-x_1; + y = sensor.getRawY()-y_1; + z = sensor.getRawZ()-z_1; + + sumX += x; + sumY += y; + sumZ += z; + + sumXX += x * x; + sumYY += y * y; + sumZZ += z * z; + //waiting until new data is ready in the sensor + while( millis() 1) - { - for (int i = 0; i < sample_size * 3; i += 3) - { - sumXX += ((float)value_buffer[i] - meanX) * ((float)value_buffer[i] - meanX); - sumYY += ((float)value_buffer[i + 1] - meanY) * ((float)value_buffer[i + 1] - meanY); - sumZZ += ((float)value_buffer[i + 2] - meanZ) * ((float)value_buffer[i + 2] - meanZ); - } - - varX = sumXX / (sample_size - 1); - varY = sumYY / (sample_size - 1); - varZ = sumZZ / (sample_size - 1); - } - Serial.print(","); - Serial.print(varX, 7); - Serial.print(","); - Serial.print(varY, 7); - Serial.print(","); - Serial.print(varZ, 7); - } + Serial.print(","); + Serial.print(varX, 7); + Serial.print(","); + Serial.print(varY, 7); + Serial.print(","); + Serial.print(varZ, 7); Serial.println("#"); Serial.flush(); } @@ -291,7 +247,7 @@ int getVersion(char *) int setSampleSize(char *command) { int value_int = atoi(command + 7); - if (value_int > 0 && value_int <= VALUE_BUFFER_SIZE) + if (value_int > 0 && value_int <= MAX_SAMPLE_SIZE) { sample_size = value_int; } @@ -300,29 +256,6 @@ int setSampleSize(char *command) Serial.flush(); } -int setFastMode(char *command) -{ - int value_int = atoi(command + 8); - if (value_int == 0) - { - fast_mode_on = false; - } - else if (value_int == 1) - { - fast_mode_on = true; - } - else - { - Serial.print("value must be 0 or 1"); - Serial.println("#"); - Serial.flush(); - return 0; - } - Serial.print(fast_mode_on); - Serial.println("#"); - Serial.flush(); -} - void loop() { serialCommand(); diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index c0949609..19400203 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -10,7 +10,6 @@ Base.@kwdef struct ArduinoGaussMeterDirectParams <: ArduinoGaussMeterParams translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration = Vector{Float64} = [0.0, 0.0, 0.0] sampleSize::Int - fastModeOn::Bool @add_serial_device_fields "#" @add_arduino_fields "!" "*" end @@ -25,7 +24,6 @@ Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration::Vector{Float64} = [0.0, 0.0, 0.0] sampleSize::Int - fastModeOn::Bool @add_serial_device_fields "#" @add_arduino_fields "!" "*" @@ -52,7 +50,6 @@ Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter sampleSize::Int = 0 measdelay = 1000 measurementTriggered::Bool = false - fastModeOn:: Bool = false end neededDependencies(::ArduinoGaussMeter) = [] @@ -66,7 +63,6 @@ function _init(gauss::ArduinoGaussMeter) gauss.ard = ard gauss.measdelay = parse(Int64, queryCommand(ard, "DELAY*")) setSampleSize(gauss, params.sampleSize) - setFast(gauss,params.fastModeOn) end function initSerialDevice(gauss::ArduinoGaussMeter, params::ArduinoGaussMeterDirectParams) @@ -84,7 +80,8 @@ end function checkSerialDevice(gauss::ArduinoGaussMeter, sd::SerialDevice) try reply = query(sd, "!VERSION*") - if !(startswith(reply, "HALLSENS:4")) + """todo fix number""" + if !(startswith(reply, "HALLSENS:")) close(sd) throw(ScannerConfigurationError(string("Connected to wrong Device", reply))) end @@ -149,7 +146,7 @@ function receive(gauss::ArduinoGaussMeter) throw("triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first") else temp = get_timeout(gauss.ard) - timeout_ms = max(1000, floor(Int, gauss.sampleSize * gauss.measdelay * 1.2) + 1) + timeout_ms = max(1000, floor(Int, gauss.sampleSize * gauss.measdelay * 10) + 1) set_timeout(gauss.ard, timeout_ms) try data_strings = split(receive(gauss.ard), ",") @@ -204,11 +201,7 @@ sets sample size for measurment -updatedSampleSize """ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) - if (sampleSize > 1024 || sampleSize < 1) - throw(error("no valid sample size, pick size from 1 to 1024")) - end - # TODO problem on time out probabil wrong value on device - data_string = queryCommand(gauss.ard, "SAMPLES" * string(sampleSize)) # TODO Check if wanted value was set, otherwise throw error and there query HallSensor for valid > 0 values + data_string = queryCommand(gauss.ard, "SAMPLES" * string(sampleSize)) updatedSampleSize = parse(Int, data_string) if (updatedSampleSize !== sampleSize) throw(error("wrong sample size set")) @@ -217,17 +210,6 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) return updatedSampleSize end -function setFast(gauss::ArduinoGaussMeter, fastModeOn::Bool) - data_string = queryCommand(gauss.ard, "FASTMODE" * string(Int(fastModeOn))) - updatedFastMode = parse(Int, data_string) - if updatedFastMode != fastModeOn - throw(error("FastMode not set right")) - end - gauss.fastModeOn = updatedFastMode - - return gauss.fastModeOn -end - function getSampleSize(gauss::ArduinoGaussMeter) return gauss.sampleSize diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index c8fd4e93..ed11abf3 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -5,7 +5,6 @@ Base.@kwdef struct TDesignCubeParams <: DeviceParams N::Int64 radius::typeof(1.0u"mm") = 0.0u"mm" sampleSize:: Int64 = 100 - fastModeOn:: Bool = false end TDesignCubeParams(dict::Dict) = params_from_dict(TDesignCubeParams, dict) @@ -21,7 +20,6 @@ optionalDependencies(::TDesignCube) = [] function _init(cube::TDesignCube) sampleSize = cube.params.sampleSize - fastModeOn = cube.params.fastModeOn sensors = dependencies(cube, ArduinoGaussMeter) if length(sensors) != cube.params.N close.(sensors) # TODO @NH Should not close devices here @@ -31,7 +29,6 @@ function _init(cube::TDesignCube) sort!(sensors,by=x-> x.params.position) cube.sensors = sensors setSampleSize(cube,sampleSize) - setFast(cube,fastModeOn) end export setSampleSize @@ -45,17 +42,6 @@ function setSampleSize(cube::TDesignCube,sampleSize::Int) cube.sampleSize = sampleSize end -export setFast -function setFast(cube::TDesignCube,fastModeOn::Bool) - for sensor in cube.sensors - returnFastModeOn = setFast(sensor,fastModeOn) - if returnFastModeOn != fastModeOn - throw("sensors coud not be updated") - end - end - cube.sampleSize = fastModeOn -end - export getSampleSize getSampleSize(cube::TDesignCube) = cube.params.sampleSize From 54e223e519b9d79e296b9ffdbdb1d970a1a17751 Mon Sep 17 00:00:00 2001 From: vincent Date: Fri, 2 Jun 2023 13:37:27 +0200 Subject: [PATCH 62/68] remove fast mode --- Devices/Arduino/HallSensor/HallSensor.ino | 2 +- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 16 ---------------- src/Devices/GaussMeter/TDesignCube.jl | 13 ------------- 3 files changed, 1 insertion(+), 30 deletions(-) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 09083a8a..d30ec8a4 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -1,7 +1,7 @@ #define ARDUINO_TYPE "HALLSENS" #define VERSION "4.0" #define BAUDRATE 9600 -#define MEASDELAY 2 +#define MEASDELAY 1 #include diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index c0949609..f504a910 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -10,7 +10,6 @@ Base.@kwdef struct ArduinoGaussMeterDirectParams <: ArduinoGaussMeterParams translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration = Vector{Float64} = [0.0, 0.0, 0.0] sampleSize::Int - fastModeOn::Bool @add_serial_device_fields "#" @add_arduino_fields "!" "*" end @@ -25,7 +24,6 @@ Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams translation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) biasCalibration::Vector{Float64} = [0.0, 0.0, 0.0] sampleSize::Int - fastModeOn::Bool @add_serial_device_fields "#" @add_arduino_fields "!" "*" @@ -52,7 +50,6 @@ Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter sampleSize::Int = 0 measdelay = 1000 measurementTriggered::Bool = false - fastModeOn:: Bool = false end neededDependencies(::ArduinoGaussMeter) = [] @@ -66,7 +63,6 @@ function _init(gauss::ArduinoGaussMeter) gauss.ard = ard gauss.measdelay = parse(Int64, queryCommand(ard, "DELAY*")) setSampleSize(gauss, params.sampleSize) - setFast(gauss,params.fastModeOn) end function initSerialDevice(gauss::ArduinoGaussMeter, params::ArduinoGaussMeterDirectParams) @@ -217,18 +213,6 @@ function setSampleSize(gauss::ArduinoGaussMeter, sampleSize::Int) return updatedSampleSize end -function setFast(gauss::ArduinoGaussMeter, fastModeOn::Bool) - data_string = queryCommand(gauss.ard, "FASTMODE" * string(Int(fastModeOn))) - updatedFastMode = parse(Int, data_string) - if updatedFastMode != fastModeOn - throw(error("FastMode not set right")) - end - gauss.fastModeOn = updatedFastMode - - return gauss.fastModeOn -end - - function getSampleSize(gauss::ArduinoGaussMeter) return gauss.sampleSize end diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index c8fd4e93..4755aed4 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -5,7 +5,6 @@ Base.@kwdef struct TDesignCubeParams <: DeviceParams N::Int64 radius::typeof(1.0u"mm") = 0.0u"mm" sampleSize:: Int64 = 100 - fastModeOn:: Bool = false end TDesignCubeParams(dict::Dict) = params_from_dict(TDesignCubeParams, dict) @@ -21,7 +20,6 @@ optionalDependencies(::TDesignCube) = [] function _init(cube::TDesignCube) sampleSize = cube.params.sampleSize - fastModeOn = cube.params.fastModeOn sensors = dependencies(cube, ArduinoGaussMeter) if length(sensors) != cube.params.N close.(sensors) # TODO @NH Should not close devices here @@ -45,17 +43,6 @@ function setSampleSize(cube::TDesignCube,sampleSize::Int) cube.sampleSize = sampleSize end -export setFast -function setFast(cube::TDesignCube,fastModeOn::Bool) - for sensor in cube.sensors - returnFastModeOn = setFast(sensor,fastModeOn) - if returnFastModeOn != fastModeOn - throw("sensors coud not be updated") - end - end - cube.sampleSize = fastModeOn -end - export getSampleSize getSampleSize(cube::TDesignCube) = cube.params.sampleSize From a38431ffc53573b5ab72a9cde2c5af44d4bf515c Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 6 Jun 2023 10:17:13 +0200 Subject: [PATCH 63/68] sorting by position id --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 16 +++---- src/Devices/GaussMeter/TDesignCube.jl | 47 +++++++++++++++------ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index 4ee6bc5e..cec911f5 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -1,4 +1,6 @@ export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams, getRawXYZValues, getXYZValues,triggerMeasurment, receive, receiveMeasurment, setSampleSize, getSampleSize, getTemperature + + abstract type ArduinoGaussMeterParams <: DeviceParams end @@ -18,6 +20,7 @@ ArduinoGaussMeterDirectParams(dict::Dict) = params_from_dict(ArduinoGaussMeterDi Base.@kwdef struct ArduinoGaussMeterDescriptionParams <: ArduinoGaussMeterParams description::String + positionID::Int position::Vector{Float64} = [0.0, 0.0, 0.0] calibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) * 0.125 rotation::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) @@ -46,7 +49,6 @@ end Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter @add_device_fields ArduinoGaussMeterParams ard::Union{SimpleArduino,Nothing} = nothing - rotatedCalibration::Matrix{Float64} = Matrix{Float64}(I, (3, 3)) sampleSize::Int = 0 measdelay = 1000 measurementTriggered::Bool = false @@ -100,6 +102,7 @@ end [x_raw_mean,y_raw_mean,z_raw_mean, x_raw_var,y_raw_var,z_raw_var] """ function getRawXYZValues(gauss::ArduinoGaussMeter) + triggerMeasurment(gauss) data = receive(gauss) return data @@ -146,7 +149,7 @@ function receive(gauss::ArduinoGaussMeter) throw("triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first") else temp = get_timeout(gauss.ard) - timeout_ms = max(1000, floor(Int, gauss.sampleSize * gauss.measdelay * 10) + 1) + timeout_ms = max(1000, floor(Int, gauss.sampleSize * gauss.measdelay * 1.2) + 1) set_timeout(gauss.ard, timeout_ms) try data_strings = split(receive(gauss.ard), ",") @@ -154,7 +157,7 @@ function receive(gauss::ArduinoGaussMeter) return data finally set_timeout(gauss.ard, temp) - gauss.measurementTriggered = false + gauss.measurementTriggered = false end end end @@ -183,7 +186,7 @@ Varianz can't be calibrated function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] # TODO Sanity checks on data, does it have the expected size - calibrated_means = gauss.params.rotation * (gauss.params.calibration * (means - gauss.params.biasCalibration)) + calibrated_means = gauss.params.rotation * ((gauss.params.calibration * means) - gauss.params.biasCalibration) return calibrated_means end @@ -224,10 +227,5 @@ function getTemperature(gauss::ArduinoGaussMeter) return parse(Float32, temp_str) end - getPosition(gauss::ArduinoGaussMeter) = gauss.params.position - -getXValue(gauss::ArduinoGaussMeter) = getXYZValues(gauss)[1] -getYValue(gauss::ArduinoGaussMeter) = getXYZValues(gauss)[2] -getZValue(gauss::ArduinoGaussMeter) = getXYZValues(gauss)[3] close(gauss::ArduinoGaussMeter) = close(gauss.ard) \ No newline at end of file diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index ed11abf3..079564c0 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -1,4 +1,4 @@ -export TDesignCubeParams, TDesignCube, setSampleSize, getSampleSize, getT, getN, getRadius, getPositions +export TDesignCubeParams, TDesignCube, setSampleSize, getSampleSize, getT, getN, getRadius, getPositions, getTemperature, measurment Base.@kwdef struct TDesignCubeParams <: DeviceParams T::Int64 @@ -25,8 +25,7 @@ function _init(cube::TDesignCube) close.(sensors) # TODO @NH Should not close devices here throw("missing Sensors") end - #TODO chang sorting to sensor - sort!(sensors,by=x-> x.params.position) + sort!(sensors,by=x-> x.params.positionID) cube.sensors = sensors setSampleSize(cube,sampleSize) end @@ -43,17 +42,17 @@ function setSampleSize(cube::TDesignCube,sampleSize::Int) end export getSampleSize -getSampleSize(cube::TDesignCube) = cube.params.sampleSize +getSampleSize(cube::TDesignCube) = cube.sampleSize function getXYZValues(cube::TDesignCube) - measurement = zeros(cube.params.N,3) + measurement = zeros(3,cube.params.N) #triggerMeasurment for sensor in cube.sensors triggerMeasurment(sensor) end #readmeasurement for (i,sensor) in enumerate(cube.sensors) - measurement[i,:] = receiveMeasurment(sensor) + measurement[:,i] = receiveMeasurment(sensor) end return measurement end @@ -62,16 +61,40 @@ getT(cube::TDesignCube) = cube.params.T getN(cube::TDesignCube) = cube.params.N getRadius(cube::TDesignCube) = cube.params.radius -function getPositions(cube) +function getPositions(cube::TDesignCube) positions = zeros(cube.params.N,3) - for (i,sensor) in cube.sensorss - positions[i,:] = getPositions(sensor) + for (i,sensor) in enumerate(cube.sensors) + positions[i,:] = getPosition(sensor) end return positions end -function close(cube::TDesignCube) - for sensor in cube.sensors - close(sensor) +function getTemperatures(cube::TDesignCube) + temperatures = zeros(cube.params.N) + for (i,sensor) in enumerate(cube.sensors) + temperatures[i] = getTemperature(sensor) end + return temperatures +end + +#starts measument and stores it into a hdf5 file +function measurment(cube,filename,center_position=[0,0,0],sampleSize=1000) + setSampleSize(cube,1000) + data = getXYZValues(cube) + println(data) + h5open(filename, "w") do file + println("hear") + write(file,"/fields", data) # measured field (size: 3 x #points x #patches) + println("hear2") + println(cube.params.radius) + write(file,"/positions/tDesign/radius", ustrip(u"m",cube.params.radius)) # radius of the measured ball + write(file,"/positions/tDesign/N", cube.params.N) # number of points of the t-design + write(file,"/positions/tDesign/t", cube.params.T) # t of the t-design + write(file,"/positions/tDesign/center", center_position) # center of the measured ball + end + return measurement + end + +function close(cube::TDesignCube) + #NOP end \ No newline at end of file From e9fdc37d5dd56267feb05bb0375713a9125c5c7b Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 6 Jun 2023 10:17:25 +0200 Subject: [PATCH 64/68] WIP PRotikol --- src/Protocols/TDesignCubeProtocol.jl | 289 ++++++++++++++------------- 1 file changed, 146 insertions(+), 143 deletions(-) diff --git a/src/Protocols/TDesignCubeProtocol.jl b/src/Protocols/TDesignCubeProtocol.jl index 37f813c5..dddb2cba 100644 --- a/src/Protocols/TDesignCubeProtocol.jl +++ b/src/Protocols/TDesignCubeProtocol.jl @@ -1,175 +1,178 @@ -#= + export TDesignCubeProtocolParams, TDesignCubeProtocol Base.@kwdef mutable struct TDesignCubeProtocolParams <: ProtocolParams - sequence::Union{Sequence, Nothing} = nothing - center::ScannerCoords = ScannerCoords([[0.0u"mm", 0.0u"mm", 0.0u"mm"]]) - samplesSize::Union{Nothing, Int64} = nothing # Optional overwrite + sequence::Union{Sequence,Nothing} = nothing + center::ScannerCoords = ScannerCoords([[0.0u"mm", 0.0u"mm", 0.0u"mm"]]) + samplesSize::Union{Nothing,Int64} = nothing # Optional overwrite end function TDesignCubeProtocolParams(dict::Dict, scanner::MPIScanner) - sequence = nothing - if haskey(dict, "sequence") - sequence = Sequence(scanner, dict["sequence"]) - dict["sequence"] = sequence - delete!(dict, "sequence") - end - - params = params_from_dict(TDesignCubeProtocolParams, dict) - params.sequence = sequence - return params + sequence = nothing + if haskey(dict, "sequence") + sequence = Sequence(scanner, dict["sequence"]) + dict["sequence"] = sequence + delete!(dict, "sequence") end + + params = params_from_dict(TDesignCubeProtocolParams, dict) + params.sequence = sequence + return params + end Base.@kwdef mutable struct TDesignCubeProtocol <: Protocol - @add_protocol_fields TDesignCubeProtocolParams - finishAcknowledged::Bool = false - measurement::Union{Matrix{Float64}, Nothing} = nothing - tDesign::Union{SphericalTDesign, Nothing} = nothing + @add_protocol_fields TDesignCubeProtocolParams + finishAcknowledged::Bool = false + measurement::Union{Matrix{Float64},Nothing} = nothing + tDesign::Union{SphericalTDesign,Nothing} = nothing end requiredDevices(protocol::TDesignCubeProtocol) = [TDesignCube, AbstractDAQ] function _init(protocol::TDesignCubeProtocol) - if isnothing(protocol.params.sequence) - throw(IllegalStateException("Protocol requires a sequence")) - end - cube = getDevice(protocol.scanner, TDesignCube) - # TODO get T, N, radius from TDesignCube - protocol.tDesign = loadTDesign(T, N, radius, protocol.params.center.data) - protocol.measurement = zeros(Float64, 3, length(protocol.tDesign)) + if isnothing(protocol.params.sequence) + throw(IllegalStateException("Protocol requires a sequence")) end + cube = getDevice(protocol.scanner, TDesignCube) + # TODO get T, N, radius from TDesignCube + N = getN(cube) + T = getT(cube) + radius = getRadius(cube) + protocol.tDesign = loadTDesign(T, N, radius, protocol.params.center.data) + protocol.measurement = zeros(Float64, 3, length(protocol.tDesign)) end + function enterExecute(protocol::TDesignCubeProtocol) - protocol.finishAcknowledged = false + protocol.finishAcknowledged = false end function _execute(protocol::TDesignCubeProtocol) - @debug "TDesignCube protocol started" - - performMeasurement(protocol) - - put!(protocol.biChannel, FinishedNotificationEvent()) - - debugCount = 0 - - while !(protocol.finishAcknowledged) - handleEvents(protocol) - protocol.cancelled && throw(CancelException()) - end - - @info "Protocol finished." - close(protocol.biChannel) - @debug "Protocol channel closed after execution." + @debug "TDesignCube protocol started" + + performMeasurement(protocol) + + put!(protocol.biChannel, FinishedNotificationEvent()) + + debugCount = 0 + + while !(protocol.finishAcknowledged) + handleEvents(protocol) + protocol.cancelled && throw(CancelException()) + end + + @info "Protocol finished." + close(protocol.biChannel) + @debug "Protocol channel closed after execution." end function performMeasurement(protocol::TDesignCubeProtocol) - cube = getDevice(scanner(protocol), TDesignCube) - producer = @tspawnat protocol.scanner.generalParams.producerThreadID measurement(protocol) - while !istaskdone(producer) - handleEvents(protocol) - # Dont want to throw cancel here - sleep(0.05) - end - - if Base.istaskfailed(producer) - currExceptions = current_exceptions(producer) - @error "Measurement failed" exception = (currExceptions[end][:exception], stacktrace(currExceptions[end][:backtrace])) - for i in 1:length(currExceptions) - 1 - stack = currExceptions[i] - @error stack[:exception] trace = stacktrace(stack[:backtrace]) - end - ex = currExceptions[1][:exception] - throw(ex) - end + cube = getDevice(scanner(protocol), TDesignCube) + producer = @tspawnat protocol.scanner.generalParams.producerThreadID measurement(protocol) + while !istaskdone(producer) + handleEvents(protocol) + # Dont want to throw cancel here + sleep(0.05) + end + + if Base.istaskfailed(producer) + currExceptions = current_exceptions(producer) + @error "Measurement failed" exception = (currExceptions[end][:exception], stacktrace(currExceptions[end][:backtrace])) + for i in 1:length(currExceptions)-1 + stack = currExceptions[i] + @error stack[:exception] trace = stacktrace(stack[:backtrace]) + end + ex = currExceptions[1][:exception] + throw(ex) + end end -function startMeasurement(protocol::RobotBasedTDesignFieldProtocol) - daq = getDAQ(protocol.scanner) - su = getSurveillanceUnit(protocol.scanner) - tempControl = getTemperatureController(protocol.scanner) - amps = getDevices(protocol.scanner, Amplifier) - if !isempty(amps) - # Only enable amps that amplify a channel of the current sequence - channelIdx = id.(vcat(acyclicElectricalTxChannels(protocol.params.sequence), periodicElectricalTxChannels(protocol.params.sequence))) - amps = filter(amp -> in(channelId(amp), channelIdx), amps) - end - if !isnothing(su) - enableACPower(su) - end - if !isnothing(tempControl) - disableControl(tempControl) - end - @sync for amp in amps - @async turnOn(amp) - end - startTx(daq) - current = 0 - # Wait for measurement proper frame to start - while current < timing.start - current = currentWP(daq.rpc) - sleep(0.01) - end +function startMeasurement(protocol::TDesignCubeProtocol) + daq = getDAQ(protocol.scanner) + su = getSurveillanceUnit(protocol.scanner) + tempControl = getTemperatureController(protocol.scanner) + amps = getDevices(protocol.scanner, Amplifier) + if !isempty(amps) + # Only enable amps that amplify a channel of the current sequence + channelIdx = id.(vcat(acyclicElectricalTxChannels(protocol.params.sequence), periodicElectricalTxChannels(protocol.params.sequence))) + amps = filter(amp -> in(channelId(amp), channelIdx), amps) + end + if !isnothing(su) + enableACPower(su) + end + if !isnothing(tempControl) + disableControl(tempControl) + end + @sync for amp in amps + @async turnOn(amp) + end + startTx(daq) + current = 0 + # Wait for measurement proper frame to start + while current < timing.start + current = currentWP(daq.rpc) + sleep(0.01) + end end - -function stopMeasurement(protocol::RobotBasedTDesignFieldProtocol) - daq = getDAQ(protocol.scanner) - su = getSurveillanceUnit(protocol.scanner) - tempControl = getTemperatureController(protocol.scanner) - amps = getDevices(protocol.scanner, Amplifier) - if !isempty(amps) - # Only disable amps that amplify a channel of the current sequence - channelIdx = id.(vcat(acyclicElectricalTxChannels(protocol.params.sequence), periodicElectricalTxChannels(protocol.params.sequence))) - amps = filter(amp -> in(channelId(amp), channelIdx), amps) - end - timing = getTiming(daq) - @show timing - endSequence(daq, timing.finish) - @sync for amp in amps - @async turnOff(amp) - end - if !isnothing(tempControl) - enableControl(tempControl) - end - if !isnothing(su) - disableACPower(su) - end + +function stopMeasurement(protocol::TDesignCubeProtocol) + daq = getDAQ(protocol.scanner) + su = getSurveillanceUnit(protocol.scanner) + tempControl = getTemperatureController(protocol.scanner) + amps = getDevices(protocol.scanner, Amplifier) + if !isempty(amps) + # Only disable amps that amplify a channel of the current sequence + channelIdx = id.(vcat(acyclicElectricalTxChannels(protocol.params.sequence), periodicElectricalTxChannels(protocol.params.sequence))) + amps = filter(amp -> in(channelId(amp), channelIdx), amps) + end + timing = getTiming(daq) + @show timing + endSequence(daq, timing.finish) + @sync for amp in amps + @async turnOff(amp) + end + if !isnothing(tempControl) + enableControl(tempControl) + end + if !isnothing(su) + disableACPower(su) + end end - -function measurement(protocol::RobotBasedTDesignFieldProtocol) - daq = getDAQ(protocol.scanner) - startMeasurement(protocol) - cube = getDevice(scanner(protocol), TDesignCube) - # TODO overwrite samplesSize if given - field = getXYZValues(protocol, cube) - timing = getTiming(daq) - current = currentWP(daq.rpc) - if current > timing.down - @warn current - @warn "Magnetic field was measured too late" - end - stopMeasurement(protocol) - protocol.measurement = ustrip.(u"T", field) + +function measurement(protocol::TDesignCubeProtocol) + daq = getDAQ(protocol.scanner) + startMeasurement(protocol) + cube = getDevice(scanner(protocol), TDesignCube) + if sample_size + setSampleSize(cube) + end + field = getXYZValues(protocol, cube) + timing = getTiming(daq) + current = currentWP(daq.rpc) + if current > timing.down + @warn current + @warn "Magnetic field was measured too late" + end + stopMeasurement(protocol) + protocol.measurement = ustrip.(u"T", field) end -handleEvent(protocol::RobotBasedTDesignFieldProtocol, event::FinishedAckEvent) = protocol.finishAcknowledged = true - -function handleEvent(protocol::RobotBasedTDesignFieldProtocol, event::FileStorageRequestEvent) - filename = event.filename - h5open(filename, "w") do file - write(file,"/fields", protocol.measurement) # measured field (size: 3 x #points x #patches) - # TODO get T, N, radius from TDesignCube - write(file,"/positions/tDesign/radius", ustrip(u"m", radius)) # radius of the measured ball - write(file,"/positions/tDesign/N", N) # number of points of the t-design - write(file,"/positions/tDesign/t", T) # t of the t-design - write(file,"/positions/tDesign/center", ustrip.(u"m", protocol.params.center.data)) # center of the measured ball - #write(file, "/sensor/correctionTranslation", getGaussMeter(protocol.scanner).params.sensorCorrectionTranslation) # TODO Write 3x3xN rotated translations - end - put!(protocol.biChannel, StorageSuccessEvent(filename)) +handleEvent(protocol::TDesignCubeProtocol, event::FinishedAckEvent) = protocol.finishAcknowledged = true + +function handleEvent(protocol::TDesignCubeProtocol, event::FileStorageRequestEvent) + filename = event.filename + h5open(filename, "w") do file + write(file, "/fields", protocol.measurement) # measured field (size: 3 x #points x #patches) + # TODO get T, N, radius from TDesignCube + write(file, "/positions/tDesign/radius", ustrip(u"m", radius))# radius of the measured ball + write(file, "/positions/tDesign/N", N)# number of points of the t-design + write(file, "/positions/tDesign/t", T)# t of the t-design + write(file, "/positions/tDesign/center", ustrip.(u"m", protocol.params.center.data))# center of the measured ball + #write(file, "/sensor/correctionTranslation", getGaussMeter(protocol.scanner).params.sensorCorrectionTranslation) # TODO Write 3x3xN rotated translations + end + put!(protocol.biChannel, StorageSuccessEvent(filename)) end - -function cleanup(protocol::RobotBasedTDesignFieldProtocol) - # NOP + +function cleanup(protocol::TDesignCubeProtocol) + # NOP end - -=# \ No newline at end of file From b075fd6d55ae66435a7c3f1aa1842f97e69728d2 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 14 Jun 2023 12:56:04 +0200 Subject: [PATCH 65/68] adding missing update for mean approximation --- Devices/Arduino/HallSensor/HallSensor.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Devices/Arduino/HallSensor/HallSensor.ino b/Devices/Arduino/HallSensor/HallSensor.ino index 17a1067e..36aa8ca5 100644 --- a/Devices/Arduino/HallSensor/HallSensor.ino +++ b/Devices/Arduino/HallSensor/HallSensor.ino @@ -150,6 +150,7 @@ void setup() sensor.begin(); sensor.disableTemp(); } + int getDelay(char *) { Serial.print(MEASDELAY, 7); @@ -170,6 +171,7 @@ int getData(char *) // updateData reads values from sensor and reading triggers next measurement sensor.updateData(); delay(MEASDELAY); + sensor.updateData(); //storing first measurement as approximation of mean value x_1 = sensor.getRawX(); From 18c2f0df4f21294e6ae10b1f73937c4434a7bde8 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 14 Jun 2023 12:57:24 +0200 Subject: [PATCH 66/68] fixing wrong transformation of transformation --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 28 ++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index cec911f5..ab6e5b19 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -1,4 +1,4 @@ -export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams, getRawXYZValues, getXYZValues,triggerMeasurment, receive, receiveMeasurment, setSampleSize, getSampleSize, getTemperature +export ArduinoGaussMeter, ArduinoGaussMeterParams, ArduinoGaussMeterDirectParams, ArduinoGaussMeterPoolParams, ArduinoGaussMeterDescriptionParams, getRawXYZValues, getXYZValues, triggerMeasurment, receive, receiveMeasurment, setSampleSize, getSampleSize, getTemperature,reset abstract type ArduinoGaussMeterParams <: DeviceParams end @@ -40,7 +40,7 @@ function ArduinoGaussMeterDescriptionParams(dict::Dict) dict["rotation"] = Float64.(reshape(dict["rotation"], 3, 3)) end if haskey(dict, "calibration") - dict["calibration"] = Float64.(reshape(dict["calibration"], 3, 3)) + dict["calibration"] = Float64.(reshape(dict["calibration"], 3, 3))' end params_from_dict(ArduinoGaussMeterDescriptionParams, dict) end @@ -50,7 +50,7 @@ Base.@kwdef mutable struct ArduinoGaussMeter <: GaussMeter @add_device_fields ArduinoGaussMeterParams ard::Union{SimpleArduino,Nothing} = nothing sampleSize::Int = 0 - measdelay = 1000 + measdelay = 1 measurementTriggered::Bool = false end @@ -102,7 +102,7 @@ end [x_raw_mean,y_raw_mean,z_raw_mean, x_raw_var,y_raw_var,z_raw_var] """ function getRawXYZValues(gauss::ArduinoGaussMeter) - + triggerMeasurment(gauss) data = receive(gauss) return data @@ -127,7 +127,7 @@ end """ function triggerMeasurment(gauss::ArduinoGaussMeter) if gauss.measurementTriggered - throw("measurement already triggered") + throw("measurement already triggered, you have to reset the sensor, use reset()") end sendCommand(gauss.ard, "DATA") gauss.measurementTriggered = true @@ -149,7 +149,8 @@ function receive(gauss::ArduinoGaussMeter) throw("triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first") else temp = get_timeout(gauss.ard) - timeout_ms = max(1000, floor(Int, gauss.sampleSize * gauss.measdelay * 1.2) + 1) + timeout_ms = max(1000, floor(Int, gauss.sampleSize * gauss.measdelay * 4) + 1) + (timeout_ms) set_timeout(gauss.ard, timeout_ms) try data_strings = split(receive(gauss.ard), ",") @@ -157,11 +158,20 @@ function receive(gauss::ArduinoGaussMeter) return data finally set_timeout(gauss.ard, temp) - gauss.measurementTriggered = false + gauss.measurementTriggered = false end end end +function reset(gauss::ArduinoGaussMeter) + if gauss.measurementTriggered + try + receive(gauss) + catch + gauss.measurementTriggered = false + end + end +end """ receiveMeasurment(gauss::ArduinoGaussMeter)::Array{Float64,1} collecting, calibrating and returning measurment for 'gauss'-sensor in mT @@ -186,7 +196,7 @@ Varianz can't be calibrated function applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64}) means = data[1:3] # TODO Sanity checks on data, does it have the expected size - calibrated_means = gauss.params.rotation * ((gauss.params.calibration * means) - gauss.params.biasCalibration) + calibrated_means = gauss.params.rotation * ((gauss.params.calibration * means) + gauss.params.biasCalibration) .* 1.0u"mT" return calibrated_means end @@ -224,7 +234,7 @@ returns tempreture of the sensor, do not expect a high tempreture resolution """ function getTemperature(gauss::ArduinoGaussMeter) temp_str = queryCommand(gauss.ard, "TEMP") - return parse(Float32, temp_str) + return parse(Float32, temp_str)u"°C" end getPosition(gauss::ArduinoGaussMeter) = gauss.params.position From 6ec6bee1281afaab927aeef1bae04416272382e5 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 14 Jun 2023 12:58:42 +0200 Subject: [PATCH 67/68] Begin to work on protocol, adding a dummy cube for testing --- .../Protocols/CalibCubeProtocol.toml | 9 + config/CalibCube/Scanner.toml | 839 ++++++++++++++++++ config/DummyCalibCube/Scanner.toml | 244 +++++ src/Devices/GaussMeter/DummyGaussMeter.jl | 9 +- src/Devices/GaussMeter/TDesignCube.jl | 71 +- src/Scanner.jl | 2 +- 6 files changed, 1129 insertions(+), 45 deletions(-) create mode 100644 config/CalibCube/Protocols/CalibCubeProtocol.toml create mode 100644 config/CalibCube/Scanner.toml create mode 100644 config/DummyCalibCube/Scanner.toml diff --git a/config/CalibCube/Protocols/CalibCubeProtocol.toml b/config/CalibCube/Protocols/CalibCubeProtocol.toml new file mode 100644 index 00000000..44c83dd4 --- /dev/null +++ b/config/CalibCube/Protocols/CalibCubeProtocol.toml @@ -0,0 +1,9 @@ +name = "Du" +type = "TDesignCubeProtocol" +description = "protocol for measuring fields with the cube." +targetScanner = "CalibCube" +sequence = "DummySequence" + +controlTx = false +fgFrames = 1 +bgFrames = 1 \ No newline at end of file diff --git a/config/CalibCube/Scanner.toml b/config/CalibCube/Scanner.toml new file mode 100644 index 00000000..d66d2624 --- /dev/null +++ b/config/CalibCube/Scanner.toml @@ -0,0 +1,839 @@ +[General] +boreSize = "70mm" +facility = "Institute for Biomedical Imaging" +manufacturer = "IBI" +name = "CalibCube" +topology = "FFP" +gradient = "42T/m" +datasetStore = "/home/v/Dokumente/BA/datastore" +defaultSequence = "1DSequence" +defaultProtocol = "MPIMeasurement" +producerThreadID = 2 +consumerThreadID = 3 +protocolThreadID = 4 + +[Devices] +initializationOrder = [ + "ard_pool", + "sensor1", + "sensor2", + "sensor3", + "sensor4", + "sensor5", + "sensor6", + "sensor7", + "sensor8", + "sensor9", + "sensor10", + "sensor11", + "sensor12", + "sensor13", + "sensor14", + "sensor15", + "sensor16", + "sensor17", + "sensor18", + "sensor19", + "sensor20", + "sensor21", + "sensor22", + "sensor23", + "sensor24", + "sensor25", + "sensor26", + "cube", +] + +[Devices.ard_pool] +deviceType = "SerialPortPool" +blacklist = [] + +[Devices.cube] +dependencies = [ + "sensor1", + "sensor2", + "sensor3", + "sensor4", + "sensor5", + "sensor6", + "sensor7", + "sensor8", + "sensor9", + "sensor10", + "sensor11", + "sensor12", + "sensor13", + "sensor14", + "sensor15", + "sensor16", + "sensor17", + "sensor18", + "sensor19", + "sensor20", + "sensor21", + "sensor22", + "sensor23", + "sensor24", + "sensor25", + "sensor26", +] +deviceType = "TDesignCube" +T = 6 +N = 26 +radius = "45.00mm" +sampleSize = 500 + +[Devices.sensor1] +dependencies = ["ard_pool"] +positionID = 1 +position = [-27.7, 4.54, 35.17] +deviceType = "ArduinoGaussMeter" +description = "000591152818" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + 0.016267661649074543, + 0.05983558797138986, + -0.05846225315440825, +] +calibration = [ + 0.1274596772714161, + 0.004583083681582869, + -0.002515841133667178, + 0.004677729944059288, + -0.12717276995766122, + 0.004620833525539426, + 0.0014622628826238583, + 0.004458485814247349, + 0.12447605890744025, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor2] +position = [17.78, 21.72, 35.17] +dependencies = ["ard_pool"] +positionID = 2 +deviceType = "ArduinoGaussMeter" +description = "000591153050" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.06251699474741296, + 0.08473138995432242, + 0.14004803136698254, +] +calibration = [ + 0.12445353735332422, + 0.0040478498508808155, + -0.0025769904588185365, + 0.003572688637932503, + -0.12404959211982101, + 0.003933475380712132, + 0.0013572013971591141, + 0.004478584245655492, + 0.11928058441671796, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor3] +dependencies = ["ard_pool"] +positionID = 3 +position = [9.92, -26.26, 35.17] +deviceType = "ArduinoGaussMeter" +description = "000591152804" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.10360856703701277, + -0.04966559042213319, + 0.1196129626330971, +] +calibration = [ + 0.12145628753860568, + 0.002894788252528038, + -0.0012550766961374217, + 0.006884740470811177, + -0.12183204388417378, + 0.000835873700428676, + -0.0006454471112704357, + 0.0019053631411839011, + 0.12512228201334183, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor4] +dependencies = ["ard_pool"] +positionID = 4 +position = [-18.74, 29.96, 27.85] +deviceType = "ArduinoGaussMeter" +description = "000591153036" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +calibration = [ + 0.1193881221604956, + 0.0038959080794159877, + -0.002405824422574261, + 0.004017260430757977, + -0.11954412242536172, + 0.005007750045816211, + 0.003207682610386789, + 0.003968429113480924, + 0.11861587322331456, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor5] +dependencies = ["ard_pool"] +positionID = 5 +position = [16.58, -31.21, 27.85] +deviceType = "ArduinoGaussMeter" +description = "000591152900" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.11175066548695481, + 0.0006928170744047621, + 0.2556224942119227, +] +calibration = [ + 0.11409187878874064, + 0.004029216479061814, + -0.0023818621736909386, + 0.0040765688865262465, + -0.11430970556171086, + 0.0037508327497473935, + 0.0012294671783964315, + 0.006376335041233221, + 0.11473792920006712, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor6] +dependencies = ["ard_pool"] +positionID = 6 +position = [35.32, 1.25, 27.85] +deviceType = "ArduinoGaussMeter" +description = "000591152773" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.07811756516536089, + 0.06052058566672786, + -0.02254532031918051, +] +calibration = [ + 0.12462818380081012, + 0.0014440760989957709, + -0.0007426855311287432, + 0.001600522847027823, + -0.12463218582629801, + 0.003399491752575622, + 0.0005283740187647149, + 0.004944213131627913, + 0.12194107123430963, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor7] +dependencies = ["ard_pool"] +positionID = 7 +position = [-43.89, 9.8, 1.64] +deviceType = "ArduinoGaussMeter" +description = "000591152905" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.03228670338789678, + 0.16282712751388917, + 0.08755835508613954, +] +calibration = [ + 0.126111275647657, + 0.004107199861945852, + -0.0035815161368010647, + 0.004211010316022731, + -0.12523287516529574, + 0.003937637826459097, + 0.0016586019387210603, + 0.005031549241089919, + 0.12031537280905144, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor8] +dependencies = ["ard_pool"] +positionID = 8 +position = [30.43, 33.11, 1.64] +deviceType = "ArduinoGaussMeter" +description = "000591152786" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.050301288735078956, + 0.007242721671125767, + 0.05134387841903545, +] +calibration = [ + 0.123576730357644, + 0.003988330168575955, + -0.0031380314190677747, + 0.0038521766776687025, + -0.12350115321197448, + 0.00363048271780163, + 0.0009221069107641911, + 0.004869635623593624, + 0.11815824410919311, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor9] +dependencies = ["ard_pool"] +positionID = 9 +position = [13.46, -42.91, 1.64] +deviceType = "ArduinoGaussMeter" +description = "000591106144" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.08063851341355473, + -0.09457656907113253, + 0.08909220361954753, +] + + +calibration = [ + 0.12244391471572519, + 0.0029256498113505525, + -0.00022167416408131835, + 0.003097402097904784, + -0.12244105705036513, + 0.004692789310972198, + -0.00037636118491695715, + 0.005000546026764026, + 0.12202206388737671, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor10] +dependencies = ["ard_pool"] +positionID = 10 +position = [33.78, -25.48, 15.31] +deviceType = "ArduinoGaussMeter" +description = "000591152925" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 + + +calibration = [ + 0.12390559353861216, + 0.004581956020932778, + -0.0031437334736553065, + 0.004087690068975834, + -0.12444335130047646, + 0.004286696244503667, + 0.0022119654788205064, + 0.005275299244033051, + 0.11731632250969505, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor11] +dependencies = ["ard_pool"] +positionID = 11 +position = [5.18, 42.0, 15.31] +deviceType = "ArduinoGaussMeter" +description = "000591152752" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + 0.0076351062938645525, + 0.05254555526380801, + -0.09420822368210524, +] + +calibration = [ + 0.11774821988265959, + 0.0024425729269259436, + -0.0018912320622383315, + 0.0029534267545576723, + -0.11799978886980497, + 0.003950839239519023, + 0.001203904842851989, + 0.003375869444462285, + 0.1182349580911541, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor12] +dependencies = ["ard_pool"] +positionID = 12 +position = [-38.96, -16.51, 15.31] +deviceType = "ArduinoGaussMeter" +description = "000591152836" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + 0.040216124945037623, + 0.010318151687317197, + 0.3902923000930719, +] +calibration = [ + 0.12784639522312896, + 0.005860231879385202, + -0.0024065830570815998, + 0.005832152993380663, + -0.12763591649437453, + 0.004513838599449157, + 0.0003537124562931949, + 0.005421453109422933, + 0.1277597911361169, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor13] +dependencies = ["ard_pool"] +positionID = 13 +position = [0, 0, 45.0] +deviceType = "ArduinoGaussMeter" +description = "000591153208" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + 0.05787168087787163, + 0.007782026420615045, + -0.174936145990533, +] +calibration = [ + 0.12569098195219294, + 0.005233531681438331, + -0.0022562058299639195, + 0.0047181079779809775, + -0.12529562541963896, + 0.004980659585153636, + 0.0018033364559025217, + 0.004001198368571654, + 0.12797773298852042, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor14] +dependencies = ["ard_pool"] +positionID = 14 +position = [0, 0, -45.0] +deviceType = "ArduinoGaussMeter" +description = "000591153053" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.09408679131693455, + 0.09262779647023484, + 0.10250268720363576, +] + +calibration = [ + 0.12390788420587295, + 0.00429244965132327, + -0.0014021136965981487, + 0.004145760929457313, + -0.12397944754199161, + 0.003201866497177015, + -0.00020880295332797763, + 0.0038073858276590575, + 0.12736931384524286, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor15] +dependencies = ["ard_pool"] +positionID = 15 +position = [38.96, -16.51, -15.31] +deviceType = "ArduinoGaussMeter" +description = "000591153000" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + 0.11192198667290201, + 0.05873463612139693, + 0.13051974163192132, +] +calibration = [ + 0.1248272449279698, + 0.004237326334326998, + -0.002162055189338484, + 0.004400230004560484, + -0.12490382439755691, + 0.004786338222452498, + 0.0029200969178976, + 0.0031202007013232513, + 0.12505013960877226, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor16] +dependencies = ["ard_pool"] +positionID = 16 +position = [-5.18, 42.0, -15.31] +deviceType = "ArduinoGaussMeter" +description = "000591153086" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.07173692062403533, + -0.10378175361573969, + 0.097268874404342, +] + +calibration = [ + 0.12353271863668144, + 0.003804023770068385, + -0.0007725229368607597, + 0.00479721810049492, + -0.12389040127121184, + 0.0046117109853350624, + 0.0005371490690878517, + 0.004186216311391714, + 0.12677553101043268, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor17] +dependencies = ["ard_pool"] +positionID = 17 +position = [-33.78, -25.48, -15.31] +deviceType = "ArduinoGaussMeter" +description = "000591152858" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.1266129628874156, + 0.03167140604876576, + -0.13144707745193573, +] +calibration = [ + 0.12270692514840652, + 0.004445179696612989, + -0.0025042201916963217, + 0.004950615953220393, + -0.12226284432233685, + 0.003244271456907889, + 0.0005597997123740271, + 0.0048048138251897896, + 0.11926031137762858, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor18] +dependencies = ["ard_pool"] +positionID = 18 +position = [-13.46, -42.91, -1.64] +deviceType = "ArduinoGaussMeter" +description = "000591152745" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.11094011894524748, + 0.04741175004255708, + 0.1164625238871334, +] +calibration = [ + 0.12041932702569226, + 0.0024481330321771888, + -0.000682491637228647, + 0.0031058747870189862, + -0.12006669578003344, + 0.0042715043163949066, + -0.0006503311957971996, + 0.00523488015666569, + 0.1220312806847921, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor19] +dependencies = ["ard_pool"] +positionID = 19 +position = [-30.43, 33.11, -1.64] +deviceType = "ArduinoGaussMeter" +description = "000591153163" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.0005077088500865267, + 0.007655077618018224, + 0.08752565665751846, +] +calibration = [ + 0.1177684010365977, + 0.0044199856753150855, + -0.0014037463632525064, + 0.0047866493162295135, + -0.11810754084387778, + 0.004763458223521106, + 0.0011860103418805592, + 0.004664678798574635, + 0.11566904970266322, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor20] +dependencies = ["ard_pool"] +positionID = 20 +position = [43.89, 9.8, -1.64] +deviceType = "ArduinoGaussMeter" +description = "000591152865" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.009222994401552144, + 0.1162432436837035, + 0.11483458976344611, +] +calibration = [ + 0.12466952768825788, + 0.004985170608510563, + -0.003663669897485981, + 0.005235246102733066, + -0.12483181193487261, + 0.004323413410213953, + 0.0019777288062626844, + 0.005362389732119592, + 0.11662291695309487, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor21] +dependencies = ["ard_pool"] +positionID = 21 +position = [-35.32, 1.25, -27.85] +deviceType = "ArduinoGaussMeter" +description = "000591152814" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.09623373875156094, + -0.017571198598100926, + 0.10795049731508409, +] +calibration = [ + 0.12804902776435978, + 0.006064737083108405, + -0.003059716433604519, + 0.004404445403647393, + -0.12787317745357418, + 0.004804013689090996, + 0.0015268475325486946, + 0.0053093256307858785, + 0.12712766185830032, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor22] +dependencies = ["ard_pool"] +positionID = 22 +position = [16.58, -31.21, -27.85] +deviceType = "ArduinoGaussMeter" +description = "000591153184" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.016120927805560212, + 0.029415934057913852, + 0.09925101870228746, +] +calibration = [ + 0.12265690001211238, + 0.0018839788599497817, + -0.004233899483951015, + 0.0022116137078271466, + -0.1221229452570502, + 0.00251519728469533, + 0.0019562815646702275, + 0.0035158496326256053, + 0.1237546035502813, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor23] +dependencies = ["ard_pool"] +positionID = 23 +position = [18.74, 29.96, -27.85] +deviceType = "ArduinoGaussMeter" +description = "000591152914" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.11860001687513623, + -0.07604163332416954, + 0.10745277327895555, +] +calibration = [ + 0.12021451682605735, + 0.004982661671389753, + -0.003367054241397645, + 0.005134749924187959, + -0.12069178101028209, + 0.0031151188762121042, + 0.002565004060018465, + 0.004370947811472259, + 0.1158821244292794, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor24] +dependencies = ["ard_pool"] +positionID = 24 +position = [-9.92, -26.26, -35.17] +deviceType = "ArduinoGaussMeter" +description = "000591152820" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.14967982391304302, + -0.020045288539361418, + 0.11583690253273501, +] +calibration = [ + 0.117404061511546, + 0.003162778177148975, + -0.0027539062251364646, + 0.0035986026769903004, + -0.11788251648311379, + 0.003369453596198593, + 0.0011353214325678777, + 0.005356662139827337, + 0.117581616474412, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor25] +dependencies = ["ard_pool"] +positionID = 25 +position = [-17.78, 21.72, -35.17] +deviceType = "ArduinoGaussMeter" +description = "000591152996" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +biasCalibration = [ + -0.03639818354549921, + -0.052990255942443824, + 0.06446197954475656, +] +calibration = [ + 0.1219427952740972, + 0.0031995346119920768, + -0.0009550787108900678, + 0.0031151411386121986, + -0.12176984400544262, + 0.0038424014855834, + 0.0006075012327269654, + 0.004017172755673142, + 0.11906107995918964, +] +rotation = [-1, 0, 0, 0, 0, 1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +[Devices.sensor26] +dependencies = ["ard_pool"] +positionID = 26 +position = [27.7, 4.54, -35.17] +deviceType = "ArduinoGaussMeter" +description = "000591153059" +baudrate = 9600 +timeout_ms = 5000 +sampleSize = 100 +##CALIBRATION IS ONLY BASED ON MEAN OF THE OTHER SENSORS +##SENSOR 26 MUST REMEASURED +biasCalibration = [ + -0.03639818354549921, + -0.052990255942443824, + 0.06446197954475656, +] +calibration = [ + 0.12723618674758644, + 0.003612691174133794, + -0.0021100796862250355, + 0.004152317594997159, + -0.12418959384892034, + 0.0036313583295693886, + 0.0012341975010427154, + 0.004690560170016473, + 0.12520873498746155, +] +rotation = [1, 0, 0, 0, 0, -1, 0, 1, 0] +translation = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +#POS - ID +#1 - 000591152818 +#2 - 000591153050 +#3 - 000591152804 +#4 - 000591153036 +#5 - 000591152900 +#6 - 000591152773 +#7 - 000591152905 +#8 - 000591152786 +#9 - 000591106144 +#10 - 000591152925 +#11 - 000591152752 +#12 - 000591152836 +#13 - 000591153208 +#14 - 000591153053 +#15 - 000591153000 +#16 - 000591153086 +#17 - 000591152858 +#18 - 000591152745 +#19 - 000591153163 +#20 - 000591152865 +#21 - 000591152814 +#22 - 000591153184 +#23 - 000591152914 +#24 - 000591152820 +#25 - 000591152996 +#26 - 000591153059 diff --git a/config/DummyCalibCube/Scanner.toml b/config/DummyCalibCube/Scanner.toml new file mode 100644 index 00000000..be575937 --- /dev/null +++ b/config/DummyCalibCube/Scanner.toml @@ -0,0 +1,244 @@ +[General] +boreSize = "70mm" +facility = "Institute for Biomedical Imaging" +manufacturer = "IBI" +name = "DummyCalibCube" +topology = "FFP" +gradient = "42T/m" +datasetStore = "" +defaultSequence = "1DSequence" +defaultProtocol = "MPIMeasurement" +producerThreadID = 2 +consumerThreadID = 3 +protocolThreadID = 4 + +[Devices] +initializationOrder = [ + "ard_pool", + "sensor1", + "sensor2", + "sensor3", + "sensor4", + "sensor5", + "sensor6", + "sensor7", + "sensor8", + "sensor9", + "sensor10", + "sensor11", + "sensor12", + "sensor13", + "sensor14", + "sensor15", + "sensor16", + "sensor17", + "sensor18", + "sensor19", + "sensor20", + "sensor21", + "sensor22", + "sensor23", + "sensor24", + "sensor25", + "sensor26", + "cube", +] + +[Devices.ard_pool] +deviceType = "SerialPortPool" +blacklist = [] + +[Devices.cube] +dependencies = [ + "sensor1", + "sensor2", + "sensor3", + "sensor4", + "sensor5", + "sensor6", + "sensor7", + "sensor8", + "sensor9", + "sensor10", + "sensor11", + "sensor12", + "sensor13", + "sensor14", + "sensor15", + "sensor16", + "sensor17", + "sensor18", + "sensor19", + "sensor20", + "sensor21", + "sensor22", + "sensor23", + "sensor24", + "sensor25", + "sensor26" +] +deviceType = "TDesignCube" +T = 6 +N = 26 +radius = "45.00mm" +sampleSize = 500 + +[Devices.sensor1] +dependencies = ["ard_pool"] +positionID = 1 +deviceType = "DummyGaussMeter" + +[Devices.sensor2] +dependencies = ["ard_pool"] +positionID = 2 +deviceType = "DummyGaussMeter" + + +[Devices.sensor3] +dependencies = ["ard_pool"] +positionID = 3 +deviceType = "DummyGaussMeter" + +[Devices.sensor4] +dependencies = ["ard_pool"] +positionID = 4 +deviceType = "DummyGaussMeter" + +[Devices.sensor5] +dependencies = ["ard_pool"] +positionID = 5 +deviceType = "DummyGaussMeter" + +[Devices.sensor6] +dependencies = ["ard_pool"] +positionID = 6 +deviceType = "DummyGaussMeter" + +[Devices.sensor7] +dependencies = ["ard_pool"] +positionID = 7 +deviceType = "DummyGaussMeter" + +[Devices.sensor8] +dependencies = ["ard_pool"] +positionID = 8 +deviceType = "DummyGaussMeter" + +[Devices.sensor9] +dependencies = ["ard_pool"] +positionID = 9 +deviceType = "DummyGaussMeter" + +[Devices.sensor10] +dependencies = ["ard_pool"] +positionID = 10 +deviceType = "DummyGaussMeter" + +[Devices.sensor11] +dependencies = ["ard_pool"] +positionID = 11 +deviceType = "DummyGaussMeter" + +[Devices.sensor12] +dependencies = ["ard_pool"] +positionID = 12 +deviceType = "DummyGaussMeter" + +[Devices.sensor13] +dependencies = ["ard_pool"] +positionID = 13 +deviceType = "DummyGaussMeter" + +[Devices.sensor14] +dependencies = ["ard_pool"] +positionID = 14 +deviceType = "DummyGaussMeter" + +[Devices.sensor15] +dependencies = ["ard_pool"] +positionID = 15 +deviceType = "DummyGaussMeter" + +[Devices.sensor16] +dependencies = ["ard_pool"] +positionID = 16 +deviceType = "DummyGaussMeter" + +[Devices.sensor17] +dependencies = ["ard_pool"] +positionID = 17 +deviceType = "DummyGaussMeter" + +[Devices.sensor18] +dependencies = ["ard_pool"] +positionID = 18 +deviceType = "DummyGaussMeter" + +[Devices.sensor19] +dependencies = ["ard_pool"] +positionID = 19 +deviceType = "DummyGaussMeter" + +[Devices.sensor20] +dependencies = ["ard_pool"] +positionID = 20 +deviceType = "DummyGaussMeter" + +[Devices.sensor21] +dependencies = ["ard_pool"] +positionID = 21 +deviceType = "DummyGaussMeter" + +[Devices.sensor22] +dependencies = ["ard_pool"] +positionID = 22 +deviceType = "DummyGaussMeter" + +[Devices.sensor23] +dependencies = ["ard_pool"] +positionID = 23 +deviceType = "DummyGaussMeter" + +[Devices.sensor24] +dependencies = ["ard_pool"] +positionID = 24 +deviceType = "DummyGaussMeter" + +[Devices.sensor25] +dependencies = ["ard_pool"] +positionID = 25 +deviceType = "DummyGaussMeter" + +[Devices.sensor26] +dependencies = ["ard_pool"] +positionID = 26 +deviceType = "DummyGaussMeter" + + +#POS - ID +#1 - 000591152818 +#2 - 000591153050 +#3 - 000591152804 +#4 - 000591153036 +#5 - 000591152900 +#6 - 000591152773 +#7 - 000591152905 +#8 - 000591152786 +#9 - 000591106144 +#10 - 000591152925 +#11 - 000591152752 +#12 - 000591152836 +#13 - 000591153208 +#14 - 000591153053 +#15 - 000591153000 +#16 - 000591153086 +#17 - 000591152858 +#18 - 000591152745 +#19 - 000591153163 +#20 - 000591152865 +#21 - 000591152814 +#22 - 000591153184 +#23 - 000591152914 +#24 - 000591152820 +#25 - 000591152996 +#26 - 000591153059 diff --git a/src/Devices/GaussMeter/DummyGaussMeter.jl b/src/Devices/GaussMeter/DummyGaussMeter.jl index bb8c03ff..7f1a103a 100644 --- a/src/Devices/GaussMeter/DummyGaussMeter.jl +++ b/src/Devices/GaussMeter/DummyGaussMeter.jl @@ -1,7 +1,7 @@ export DummyGaussMeter, DummyGaussMeterParams Base.@kwdef struct DummyGaussMeterParams <: DeviceParams - coordinateTransformation::Matrix{Float64} = Matrix{Float64}(I,(3,3)) + positionID = 0 end DummyGaussMeterParams(dict::Dict) = params_from_dict(DummyGaussMeterParams, dict) @@ -17,7 +17,12 @@ neededDependencies(::DummyGaussMeter) = [] optionalDependencies(::DummyGaussMeter) = [] Base.close(gauss::DummyGaussMeter) = nothing - +getXYZValues(gauss::DummyGaussMeter) = [getXValue(gauss::DummyGaussMeter) ,getYValue(gauss::DummyGaussMeter) ,getZValue(gauss::DummyGaussMeter) ] +function triggerMeasurment(gauss::DummyGaussMeter) + #NOP +end +receiveMeasurment(gauss::DummyGaussMeter) =getXYZValues(gauss) +setSampleSize(gauss::DummyGaussMeter, sampleSize) = sampleSize getXValue(gauss::DummyGaussMeter) = 1.0u"mT" getYValue(gauss::DummyGaussMeter) = 2.0u"mT" getZValue(gauss::DummyGaussMeter) = 3.0u"mT" diff --git a/src/Devices/GaussMeter/TDesignCube.jl b/src/Devices/GaussMeter/TDesignCube.jl index 079564c0..3246fc8b 100644 --- a/src/Devices/GaussMeter/TDesignCube.jl +++ b/src/Devices/GaussMeter/TDesignCube.jl @@ -1,41 +1,41 @@ -export TDesignCubeParams, TDesignCube, setSampleSize, getSampleSize, getT, getN, getRadius, getPositions, getTemperature, measurment +export TDesignCubeParams, TDesignCube, setSampleSize, getSampleSize, getT, getN, getRadius, getPositions, getTemperature, measurment, reset Base.@kwdef struct TDesignCubeParams <: DeviceParams T::Int64 N::Int64 radius::typeof(1.0u"mm") = 0.0u"mm" - sampleSize:: Int64 = 100 + sampleSize::Int64 = 100 end TDesignCubeParams(dict::Dict) = params_from_dict(TDesignCubeParams, dict) Base.@kwdef mutable struct TDesignCube <: Device @add_device_fields TDesignCubeParams - sensors::Union{Vector{ArduinoGaussMeter}, Nothing} = nothing - sampleSize:: Int64 = 100 + sensors::Union{Vector{GaussMeter},Nothing} = nothing + sampleSize::Int64 = 100 end -neededDependencies(::TDesignCube) = [ArduinoGaussMeter] +neededDependencies(::TDesignCube) = [GaussMeter] optionalDependencies(::TDesignCube) = [] function _init(cube::TDesignCube) sampleSize = cube.params.sampleSize - sensors = dependencies(cube, ArduinoGaussMeter) + sensors = dependencies(cube, GaussMeter) if length(sensors) != cube.params.N close.(sensors) # TODO @NH Should not close devices here throw("missing Sensors") end - sort!(sensors,by=x-> x.params.positionID) + sort!(sensors, by=x -> x.params.positionID) cube.sensors = sensors - setSampleSize(cube,sampleSize) + setSampleSize(cube, sampleSize) end export setSampleSize -function setSampleSize(cube::TDesignCube,sampleSize::Int) +function setSampleSize(cube::TDesignCube, sampleSize::Int) for sensor in cube.sensors - returnSampleSize = setSampleSize(sensor,sampleSize) + returnSampleSize = setSampleSize(sensor, sampleSize) if returnSampleSize != sampleSize - throw("sensors coud not be updated") + throw("sensors coud not be updated") end end cube.sampleSize = sampleSize @@ -45,14 +45,12 @@ export getSampleSize getSampleSize(cube::TDesignCube) = cube.sampleSize function getXYZValues(cube::TDesignCube) - measurement = zeros(3,cube.params.N) + measurement = zeros(3, cube.params.N) .* 1.0u"mT" #triggerMeasurment - for sensor in cube.sensors - triggerMeasurment(sensor) - end + triggerMeasurment.(cube.sensors) #readmeasurement - for (i,sensor) in enumerate(cube.sensors) - measurement[:,i] = receiveMeasurment(sensor) + for (i, sensor) in enumerate(cube.sensors) + measurement[:, i] = receiveMeasurment(sensor) end return measurement end @@ -61,40 +59,29 @@ getT(cube::TDesignCube) = cube.params.T getN(cube::TDesignCube) = cube.params.N getRadius(cube::TDesignCube) = cube.params.radius -function getPositions(cube::TDesignCube) - positions = zeros(cube.params.N,3) - for (i,sensor) in enumerate(cube.sensors) - positions[i,:] = getPosition(sensor) - end - return positions -end - -function getTemperatures(cube::TDesignCube) - temperatures = zeros(cube.params.N) - for (i,sensor) in enumerate(cube.sensors) - temperatures[i] = getTemperature(sensor) - end - return temperatures -end +getPositions(cube::TDesignCube) = getPosition.(cube.sensors) +getTemperatures(cube::TDesignCube) = getTemperature.(cube.sensors) #starts measument and stores it into a hdf5 file -function measurment(cube,filename,center_position=[0,0,0],sampleSize=1000) - setSampleSize(cube,1000) +function measurment(cube, filename, center_position=[0, 0, 0], sampleSize=1000) + setSampleSize(cube, 1000) data = getXYZValues(cube) println(data) h5open(filename, "w") do file println("hear") - write(file,"/fields", data) # measured field (size: 3 x #points x #patches) + write(file, "/fields", ustrip.(u"T", data))# measured field (size: 3 x #points x #patches) println("hear2") println(cube.params.radius) - write(file,"/positions/tDesign/radius", ustrip(u"m",cube.params.radius)) # radius of the measured ball - write(file,"/positions/tDesign/N", cube.params.N) # number of points of the t-design - write(file,"/positions/tDesign/t", cube.params.T) # t of the t-design - write(file,"/positions/tDesign/center", center_position) # center of the measured ball - end - return measurement + write(file, "/positions/tDesign/radius", ustrip(u"m", cube.params.radius))# radius of the measured ball + write(file, "/positions/tDesign/N", cube.params.N)# number of points of the t-design + write(file, "/positions/tDesign/t", cube.params.T)# t of the t-design + write(file, "/positions/tDesign/center", center_position)# center of the measured ball + write(file, "/sensor/correctionTranslation", zeros(3, 3)) end + return measurement +end +reset(cube::TDesignCube) = reset.(cube.sensors) function close(cube::TDesignCube) #NOP -end \ No newline at end of file +end \ No newline at end of file diff --git a/src/Scanner.jl b/src/Scanner.jl index 94d1c97a..f0ac9eb2 100644 --- a/src/Scanner.jl +++ b/src/Scanner.jl @@ -48,7 +48,7 @@ device configuration struct. """ function initiateDevices(configDir::AbstractString, devicesParams::Dict{String, Any}; robust = false) devices = Dict{String, Device}() - + println(DeviceParams) # Get implementations for all devices in the specified order for deviceID in devicesParams["initializationOrder"] params = nothing From ca92a96b1a34e18473ba368dbcd744635a4b7a86 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 14 Jun 2023 14:16:01 +0200 Subject: [PATCH 68/68] switching abstrackt functino --- src/Devices/GaussMeter/ArduinoGaussMeter.jl | 8 +++--- src/Devices/GaussMeter/DummyGaussMeter.jl | 5 +--- src/Devices/GaussMeter/GaussMeter.jl | 27 +++++++++---------- src/Devices/GaussMeter/LakeShoreF71.jl | 3 --- src/Devices/GaussMeter/SimulatedGaussMeter.jl | 4 +-- 5 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/Devices/GaussMeter/ArduinoGaussMeter.jl b/src/Devices/GaussMeter/ArduinoGaussMeter.jl index ab6e5b19..6a17d2b4 100644 --- a/src/Devices/GaussMeter/ArduinoGaussMeter.jl +++ b/src/Devices/GaussMeter/ArduinoGaussMeter.jl @@ -135,7 +135,7 @@ end """ - receive(gauss::ArduinoGaussMeter)::Array{Float64,1} + receiveRawMeasument(gauss::ArduinoGaussMeter)::Array{Float64,1} collecting the measurment data for sensor 'gauss' triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first. @@ -144,7 +144,7 @@ end [x_raw_mean,y_raw_mean,z_raw_mean, x_raw_var,y_raw_var,z_raw_var] """ -function receive(gauss::ArduinoGaussMeter) +function receiveRaWMeasurment(gauss::ArduinoGaussMeter) if !gauss.measurementTriggered throw("triggerMeasurment(gauss::ArduinoGaussMeter) has to be called first") else @@ -166,7 +166,7 @@ end function reset(gauss::ArduinoGaussMeter) if gauss.measurementTriggered try - receive(gauss) + receiveRawMeasurment(gauss) catch gauss.measurementTriggered = false end @@ -181,7 +181,7 @@ receiveMeasurment(gauss::ArduinoGaussMeter)::Array{Float64,1} #return [x_mean,y_mean,z_mean, x_var,y_var,z_var] """ -receiveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss, receive(gauss)) +receiveMeasurment(gauss::ArduinoGaussMeter) = applyCalibration(gauss, receiveRawMeasurment(gauss)) """ applyCalibration(gauss::ArduinoGaussMeter, data::Vector{Float64})::Array{Float64,1} diff --git a/src/Devices/GaussMeter/DummyGaussMeter.jl b/src/Devices/GaussMeter/DummyGaussMeter.jl index 7f1a103a..e731a18b 100644 --- a/src/Devices/GaussMeter/DummyGaussMeter.jl +++ b/src/Devices/GaussMeter/DummyGaussMeter.jl @@ -17,15 +17,12 @@ neededDependencies(::DummyGaussMeter) = [] optionalDependencies(::DummyGaussMeter) = [] Base.close(gauss::DummyGaussMeter) = nothing -getXYZValues(gauss::DummyGaussMeter) = [getXValue(gauss::DummyGaussMeter) ,getYValue(gauss::DummyGaussMeter) ,getZValue(gauss::DummyGaussMeter) ] function triggerMeasurment(gauss::DummyGaussMeter) #NOP end receiveMeasurment(gauss::DummyGaussMeter) =getXYZValues(gauss) setSampleSize(gauss::DummyGaussMeter, sampleSize) = sampleSize -getXValue(gauss::DummyGaussMeter) = 1.0u"mT" -getYValue(gauss::DummyGaussMeter) = 2.0u"mT" -getZValue(gauss::DummyGaussMeter) = 3.0u"mT" +getXYZValue(gauss::DummyGaussMeter) = [1.0,u"mT",2.0u"mT",3.0u"mT"] getTemperature(gauss::DummyGaussMeter) = 20.0u"°C" getFrequency(gauss::DummyGaussMeter) = 0.0u"Hz" calculateFieldError(gauss::DummyGaussMeter, magneticField::Vector{<:Unitful.BField}) = 1.0u"mT" \ No newline at end of file diff --git a/src/Devices/GaussMeter/GaussMeter.jl b/src/Devices/GaussMeter/GaussMeter.jl index 45f32fa8..78b0c85b 100644 --- a/src/Devices/GaussMeter/GaussMeter.jl +++ b/src/Devices/GaussMeter/GaussMeter.jl @@ -12,14 +12,6 @@ getGaussMeter(scanner::MPIScanner) = getDevice(scanner, GaussMeter) export getCube getCube(scanner::MPIScanner) = getDevice(scanner,TDesignCube) -export getXValue -@mustimplement getXValue(gauss::GaussMeter) - -export getYValue -@mustimplement getYValue(gauss::GaussMeter) - -export getZValue -@mustimplement getZValue(gauss::GaussMeter) export getTemperature @mustimplement getTemperature(gauss::GaussMeter) @@ -31,13 +23,18 @@ export calculateFieldError @mustimplement calculateFieldError(gauss::GaussMeter, magneticField::Vector{<:Unitful.BField}) export getXYZValues -""" -Returns x,y, and z values and applies a coordinate transformation -""" -function getXYZValues(gauss::GaussMeter) - values = [getXValue(gauss), getYValue(gauss), getZValue(gauss)] - return gauss.params.coordinateTransformation*values -end +@mustimplement getXYZValues(gauss::GaussMeter) + + +export getXValue +getXValue(gauss::GaussMeter)=getXYZValues(gauss)[1] + +export getYValue +getYValue(gauss::GaussMeter)=getXYZValues(gauss)[2] + +export getZValue +getZValue(gauss::GaussMeter)=getXYZValues(gauss)[3] + include("DummyGaussMeter.jl") include("SimulatedGaussMeter.jl") diff --git a/src/Devices/GaussMeter/LakeShoreF71.jl b/src/Devices/GaussMeter/LakeShoreF71.jl index a81c0ab8..f8cb8665 100644 --- a/src/Devices/GaussMeter/LakeShoreF71.jl +++ b/src/Devices/GaussMeter/LakeShoreF71.jl @@ -90,9 +90,6 @@ function setMeasurementMode(gauss::LakeShoreF71GaussMeter, mode::LakeShoreF71Gau end end -getXValue(gauss::LakeShoreF71GaussMeter) = getXYZValues(gauss)[1] -getYValue(gauss::LakeShoreF71GaussMeter) = getXYZValues(gauss)[2] -getZValue(gauss::LakeShoreF71GaussMeter) = getXYZValues(gauss)[3] function getXYZValues(gauss::LakeShoreF71GaussMeter) if gauss.params.measurementMode == F71_MM_DC diff --git a/src/Devices/GaussMeter/SimulatedGaussMeter.jl b/src/Devices/GaussMeter/SimulatedGaussMeter.jl index 8728cd46..23ba5bf7 100644 --- a/src/Devices/GaussMeter/SimulatedGaussMeter.jl +++ b/src/Devices/GaussMeter/SimulatedGaussMeter.jl @@ -18,9 +18,7 @@ optionalDependencies(::SimulatedGaussMeter) = [] Base.close(gauss::SimulatedGaussMeter) = nothing -getXValue(gauss::SimulatedGaussMeter) = 1.0u"mT" -getYValue(gauss::SimulatedGaussMeter) = 2.0u"mT" -getZValue(gauss::SimulatedGaussMeter) = 3.0u"mT" +getXYZValue(gauss::SimulatedGaussMeter) = [1.0u"mT",2.0u"mT",3.0u"mT"] getTemperature(gauss::SimulatedGaussMeter) = 20.0u"°C" getFrequency(gauss::SimulatedGaussMeter) = 0.0u"Hz" calculateFieldError(gauss::SimulatedGaussMeter, magneticField::Vector{<:Unitful.BField}) = 1.0u"mT" \ No newline at end of file