diff --git a/arduino/Arduino_ESP32_MLX90640/Arduino_ESP32_MLX90640.ino b/arduino/Arduino_ESP32_MLX90640/Arduino_ESP32_MLX90640.ino new file mode 100644 index 0000000..9fd2210 --- /dev/null +++ b/arduino/Arduino_ESP32_MLX90640/Arduino_ESP32_MLX90640.ino @@ -0,0 +1,312 @@ +#include +#include "MLX90640_API.h" +#include "MLX90640_I2C_Driver.h" +//#include "SPI.h" +//#include "Adafruit_GFX.h" +//#include "Adafruit_ILI9341.h" + +// For the ESP-WROVER_KIT, these are the default. +/*#define TFT_CS 15 +#define TFT_DC 2 +#define TFT_MOSI 13 +#define TFT_CLK 14 +#define TFT_RST 26 +#define TFT_MISO 12 +#define TFT_LED 27*/ + +/*Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);*/ + +const byte MLX90640_address = 0x33; //Default 7-bit unshifted address of the MLX90640 + +#define TA_SHIFT 8 //Default shift for MLX90640 in open air + +static float mlx90640To[768]; +paramsMLX90640 mlx90640; + +int xPos, yPos; // Abtastposition +int R_colour, G_colour, B_colour; // RGB-Farbwert +int i, j; // Zählvariable +float T_max, T_min; // maximale bzw. minimale gemessene Temperatur +float T_center; // Temperatur in der Bildschirmmitte + + + + +// *************************************** +// **************** SETUP **************** +// *************************************** + +void setup() + { + Serial.begin(115200); + + Wire.begin(); + Wire.setClock(400000); //Increase I2C clock speed to 400kHz + + while (!Serial); //Wait for user to open terminal + + Serial.println("MLX90640 IR Array Example"); + + if (isConnected() == false) + { + Serial.println("MLX90640 not detected at default I2C address. Please check wiring. Freezing."); + while (1); + } + + Serial.println("MLX90640 online!"); + + //Get device parameters - We only have to do this once + int status; + uint16_t eeMLX90640[832]; + + status = MLX90640_DumpEE(MLX90640_address, eeMLX90640); + + if (status != 0) + Serial.println("Failed to load system parameters"); + + status = MLX90640_ExtractParameters(eeMLX90640, &mlx90640); + + if (status != 0) + { + Serial.println("Parameter extraction failed"); + Serial.print(" status = "); + Serial.println(status); + } + + //Once params are extracted, we can release eeMLX90640 array + + MLX90640_I2CWrite(0x33, 0x800D, 6401); // writes the value 1901 (HEX) = 6401 (DEC) in the register at position 0x800D to enable reading out the temperatures!!! + // =============================================================================================================================================================== + + //MLX90640_SetRefreshRate(MLX90640_address, 0x00); //Set rate to 0.25Hz effective - Works + //MLX90640_SetRefreshRate(MLX90640_address, 0x01); //Set rate to 0.5Hz effective - Works + //MLX90640_SetRefreshRate(MLX90640_address, 0x02); //Set rate to 1Hz effective - Works + //MLX90640_SetRefreshRate(MLX90640_address, 0x03); //Set rate to 2Hz effective - Works + MLX90640_SetRefreshRate(MLX90640_address, 0x04); //Set rate to 4Hz effective - Works + //MLX90640_SetRefreshRate(MLX90640_address, 0x05); //Set rate to 8Hz effective - Works at 800kHz + //MLX90640_SetRefreshRate(MLX90640_address, 0x06); //Set rate to 16Hz effective - Works at 800kHz + //MLX90640_SetRefreshRate(MLX90640_address, 0x07); //Set rate to 32Hz effective - fails + + + //pinMode(TFT_LED, OUTPUT); + //digitalWrite(TFT_LED, HIGH); + + /*tft.begin(); + + tft.setRotation(1); + + tft.fillScreen(ILI9341_BLACK); + tft.fillRect(0, 0, 319, 13, tft.color565(255, 0, 10)); + tft.setCursor(100, 3); + tft.setTextSize(1); + tft.setTextColor(ILI9341_YELLOW, tft.color565(255, 0, 10)); + tft.print("Thermographie - stoppi"); + + tft.drawLine(250, 210 - 0, 258, 210 - 0, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 30, 258, 210 - 30, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 60, 258, 210 - 60, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 90, 258, 210 - 90, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 120, 258, 210 - 120, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 150, 258, 210 - 150, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 180, 258, 210 - 180, tft.color565(255, 255, 255)); + + tft.setCursor(80, 220); + tft.setTextColor(ILI9341_WHITE, tft.color565(0, 0, 0)); + tft.print("T+ = "); + + + // drawing the colour-scale + // ======================== + + for (i = 0; i < 181; i++) + { + //value = random(180); + + getColour(i); + tft.drawLine(240, 210 - i, 250, 210 - i, tft.color565(R_colour, G_colour, B_colour)); + } + + } + +*/ + +// ********************************** +// ************** LOOP ************** +// ********************************** + +void loop() + { + for (byte x = 0 ; x < 2 ; x++) //Read both subpages + { + uint16_t mlx90640Frame[834]; + int status = MLX90640_GetFrameData(MLX90640_address, mlx90640Frame); + + if (status < 0) + { + Serial.print("GetFrame Error: "); + Serial.println(status); + } + + float vdd = MLX90640_GetVdd(mlx90640Frame, &mlx90640); + float Ta = MLX90640_GetTa(mlx90640Frame, &mlx90640); + + float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature + float emissivity = 0.95; + + MLX90640_CalculateTo(mlx90640Frame, &mlx90640, emissivity, tr, mlx90640To); + } + + + // determine T_min and T_max and eliminate error pixels + // ==================================================== + + mlx90640To[1*32 + 21] = 0.5 * (mlx90640To[1*32 + 20] + mlx90640To[1*32 + 22]); // eliminate the error-pixels + mlx90640To[4*32 + 30] = 0.5 * (mlx90640To[4*32 + 29] + mlx90640To[4*32 + 31]); // eliminate the error-pixels + + T_min = mlx90640To[0]; + T_max = mlx90640To[0]; + + for (i = 1; i < 768; i++) + { + if((mlx90640To[i] > -41) && (mlx90640To[i] < 301)) + { + if(mlx90640To[i] < T_min) + { + T_min = mlx90640To[i]; + } + + if(mlx90640To[i] > T_max) + { + T_max = mlx90640To[i]; + } + } + else if(i > 0) // temperature out of range + { + mlx90640To[i] = mlx90640To[i-1]; + } + else + { + mlx90640To[i] = mlx90640To[i+1]; + } + } + + + // determine T_center + // ================== + + T_center = mlx90640To[11* 32 + 15]; + + // drawing the picture + // =================== + + for (i = 0 ; i < 24 ; i++) + { + for (j = 0; j < 32; j++) + { + mlx90640To[i*32 + j] = 180.0 * (mlx90640To[i*32 + j] - T_min) / (T_max - T_min); + + getColour(mlx90640To[i*32 + j]); + + // tft.fillRect(217 - j * 7, 35 + i * 7, 7, 7, tft.color565(R_colour, G_colour, B_colour)); + } + } + /* + tft.drawLine(217 - 15*7 + 3.5 - 5, 11*7 + 35 + 3.5, 217 - 15*7 + 3.5 + 5, 11*7 + 35 + 3.5, tft.color565(255, 255, 255)); + tft.drawLine(217 - 15*7 + 3.5, 11*7 + 35 + 3.5 - 5, 217 - 15*7 + 3.5, 11*7 + 35 + 3.5 + 5, tft.color565(255, 255, 255)); + + tft.fillRect(260, 25, 37, 10, tft.color565(0, 0, 0)); + tft.fillRect(260, 205, 37, 10, tft.color565(0, 0, 0)); + tft.fillRect(115, 220, 37, 10, tft.color565(0, 0, 0)); + + tft.setTextColor(ILI9341_WHITE, tft.color565(0, 0, 0)); + tft.setCursor(265, 25); + tft.print(T_max, 1); + tft.setCursor(265, 205); + tft.print(T_min, 1); + tft.setCursor(120, 220); + tft.print(T_center, 1); + + tft.setCursor(300, 25); + tft.print("C"); + tft.setCursor(300, 205); + tft.print("C"); + tft.setCursor(155, 220); + tft.print("C"); + */ + delay(20); + } + + + +// =============================== +// ===== determine the colour ==== +// =============================== + +void getColour(int j) + { + if (j >= 0 && j < 30) + { + R_colour = 0; + G_colour = 0; + B_colour = 20 + (120.0/30.0) * j; + } + + if (j >= 30 && j < 60) + { + R_colour = (120.0 / 30) * (j - 30.0); + G_colour = 0; + B_colour = 140 - (60.0/30.0) * (j - 30.0); + } + + if (j >= 60 && j < 90) + { + R_colour = 120 + (135.0/30.0) * (j - 60.0); + G_colour = 0; + B_colour = 80 - (70.0/30.0) * (j - 60.0); + } + + if (j >= 90 && j < 120) + { + R_colour = 255; + G_colour = 0 + (60.0/30.0) * (j - 90.0); + B_colour = 10 - (10.0/30.0) * (j - 90.0); + } + + if (j >= 120 && j < 150) + { + R_colour = 255; + G_colour = 60 + (175.0/30.0) * (j - 120.0); + B_colour = 0; + } + + if (j >= 150 && j <= 180) + { + R_colour = 255; + G_colour = 235 + (20.0/30.0) * (j - 150.0); + B_colour = 0 + 255.0/30.0 * (j - 150.0); + } + + } + + +//Returns true if the MLX90640 is detected on the I2C bus +boolean isConnected() + { + Wire.beginTransmission((uint8_t)MLX90640_address); + + if (Wire.endTransmission() != 0) + return (false); //Sensor did not ACK + + return (true); + } + + + + + + + + + + + diff --git a/arduino/SR04_Example/SR04_Example.ino b/arduino/SR04_Example/SR04_Example.ino index 3304f62..3503093 100644 --- a/arduino/SR04_Example/SR04_Example.ino +++ b/arduino/SR04_Example/SR04_Example.ino @@ -1,6 +1,6 @@ #include "SR04.h" -#define TRIG_PIN 3 -#define ECHO_PIN 4 +#define TRIG_PIN D3 +#define ECHO_PIN D4 SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); long a; diff --git a/arduino/ultraschall/2Ultrasonic_Sensor_an_rpi/2Ultrasonic_Sensor_an_rpi.ino b/arduino/ultraschall/2Ultrasonic_Sensor_an_rpi/2Ultrasonic_Sensor_an_rpi.ino new file mode 100644 index 0000000..fc05397 --- /dev/null +++ b/arduino/ultraschall/2Ultrasonic_Sensor_an_rpi/2Ultrasonic_Sensor_an_rpi.ino @@ -0,0 +1,157 @@ +#include +#include + +//Eigene zu trackende Entfernung festlegen +#define DISTANCE 155 + +const char* SSID = "smartroom"; +const char* PSK = "smarthome"; +const char* MQTT_BROKER = "192.168.252.1"; +WiFiClient espClient; +PubSubClient client(espClient); + +// defines pins numbers +const int trigPin = D3; //D4 +const int echoPin1 = D4; //D3 +const int echoPin2 = D2; //D2 + +long duration1; +long duration2; +int distance1; +int distance2; +int bool1 = 0; +int bool2 = 0; +char msg[50]; +int value = 0; +long lastMsg = 0; + +void setup() { + pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output + pinMode(echoPin1, INPUT); // Sets the echoPin as an Input + pinMode(echoPin2, INPUT); // Sets the echoPin as an Input + + Serial.begin(115200); + setup_wifi(); + client.setServer(MQTT_BROKER, 1883); +} + +void setup_wifi() { + delay(10); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(SSID); + WiFi.mode(WIFI_STA); + WiFi.begin(SSID, PSK); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void reconnect() { + while (!client.connected()) { + Serial.print("Reconnecting..."); + if (!client.connect("ESP8266Client")) { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" retrying in 5 seconds"); + delay(5000); + } + } +} + + +void loop() { + + if (!client.connected()) { + reconnect(); + } + client.loop(); + + // Clears the trigPin + digitalWrite(trigPin, LOW); + delayMicroseconds(2); + + // Sets the trigPin on HIGH state for 10 micro seconds + digitalWrite(trigPin, HIGH); + delayMicroseconds(10); + digitalWrite(trigPin, LOW); + + // Reads the echoPin, returns the sound wave travel time in microseconds + duration1 = pulseIn(echoPin1, HIGH); + duration2 = pulseIn(echoPin2, HIGH); + + // Calculating the distance + distance1 = getDistance(duration1); + distance2 = getDistance(duration2); + // Prints the distance1 on the Serial Monitor + /* if ((distance1!= (distance_alt - 1)) && (distance1!= (distance_alt)) && (distance1!= (distance_alt + 1))) { //+-1 um störungen herauszufiltern + snprintf (msg, 50, "%d", distance); + Serial.print("Publish Motion: "); + Serial.println(msg); + client.publish("/home/data", msg); + delay(200); + } */ + bool1 = presenceDetection(bool1, distance1); + bool2 = presenceDetection(bool2, distance2); + + delay(80); +} + +int presenceDetection(int bool1, int distance1){ + + if (bool1 == 0) { + //alternativ: if ((distance1!= (DISTANCE - 1)) && (distance1!= (DISTANCE)) && (distance1!= (DISTANCE + 1))) { //+-1 + + if (((distance1 > (DISTANCE + 2)) || (distance1 < (DISTANCE - 2))) && (distance1 < (DISTANCE + 2))) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern + //Meldung an PI, dass die Distanz gestört ist + snprintf (msg, 50, "%d", 1); + client.publish("/gso/bb/104/ultraschall/1", msg); + //Serieller Monitor + Serial.print("Motion detected! Distance: "); + Serial.println(distance1); + + //Flag auf 1 + bool1 = 1; + return bool1; + } + else { + snprintf (msg, 50, "%d", 0); + client.publish("/gso/bb/104/ultraschall/1", msg); + return bool1; + } + + } + else if (bool1 == 1) { + if (((distance1> (DISTANCE + 2)) || (distance1<(DISTANCE - 2)))&&(distance1<(DISTANCE + 2))) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern + Serial.print("Still motion detected! Distance: "); + Serial.println(distance1); + //Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird + snprintf (msg, 50, "%d", 1); + client.publish("/gso/bb/104/ultraschall/1", msg); + + return bool1; + } + else { + //Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird + snprintf (msg, 50, "%d", 0); + client.publish("/gso/bb/104/ultraschall/1", msg); + + //Flag wieder auf 0 + bool1 = 0; + return bool1; + + } + } +} + +int getDistance(int duration){ + int distance = duration * 0.034 / 2; + return distance; +} diff --git a/arduino/ultraschall/Ultrasonic_Sensor_optimiert/Ultrasonic_Sensor_optimiert.ino b/arduino/ultraschall/Ultrasonic_Sensor_optimiert/Ultrasonic_Sensor_optimiert.ino index 24b8928..e13355e 100644 --- a/arduino/ultraschall/Ultrasonic_Sensor_optimiert/Ultrasonic_Sensor_optimiert.ino +++ b/arduino/ultraschall/Ultrasonic_Sensor_optimiert/Ultrasonic_Sensor_optimiert.ino @@ -11,20 +11,24 @@ WiFiClient espClient; PubSubClient client(espClient); // defines pins numbers -const int trigPin = 2; //D4 -const int echoPin = 0; //D3 +const int trigPin = D3; //D4 +const int echoPin1 = D4; //D3 + +long duration1; + +int distance1; -long duration; -int distance; int bool1 = 0; + char msg[50]; int value = 0; long lastMsg = 0; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output - pinMode(echoPin, INPUT); // Sets the echoPin as an Input + pinMode(echoPin1, INPUT); // Sets the echoPin as an Input + Serial.begin(115200); setup_wifi(); client.setServer(MQTT_BROKER, 1883); @@ -79,45 +83,59 @@ void loop() { digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds - duration = pulseIn(echoPin, HIGH); - + duration1 = pulseIn(echoPin1, HIGH); + + // Calculating the distance - distance = duration * 0.034 / 2; - // Prints the distance on the Serial Monitor - /* if ((distance != (distance_alt - 1)) && (distance != (distance_alt)) && (distance != (distance_alt + 1))) { //+-1 um störungen herauszufiltern + distance1 = getDistance(duration1); + + // Prints the distance1 on the Serial Monitor + /* if ((distance1!= (distance_alt - 1)) && (distance1!= (distance_alt)) && (distance1!= (distance_alt + 1))) { //+-1 um störungen herauszufiltern snprintf (msg, 50, "%d", distance); Serial.print("Publish Motion: "); Serial.println(msg); client.publish("/home/data", msg); delay(200); } */ + bool1 = presenceDetection(bool1, distance1); + + + delay(80); +} + +int presenceDetection(int bool1, int distance1){ + if (bool1 == 0) { - //alternativ: if ((distance != (DISTANCE - 1)) && (distance != (DISTANCE)) && (distance != (DISTANCE + 1))) { //+-1 - if (((DISTANCE + 2) < distance) || ((DISTANCE - 2) > distance)) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern + //alternativ: if ((distance1!= (DISTANCE - 1)) && (distance1!= (DISTANCE)) && (distance1!= (DISTANCE + 1))) { //+-1 + + if (((distance1 > (DISTANCE + 2)) || (distance1 < (DISTANCE - 2))) && (distance1 < (DISTANCE + 2))) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern //Meldung an PI, dass die Distanz gestört ist snprintf (msg, 50, "%d", 1); client.publish("/gso/bb/104/ultraschall/1", msg); //Serieller Monitor Serial.print("Motion detected! Distance: "); - Serial.println(distance); + Serial.println(distance1); //Flag auf 1 - bool1 = 1; + bool1 = 1; + return bool1; } else { snprintf (msg, 50, "%d", 0); client.publish("/gso/bb/104/ultraschall/1", msg); - + return bool1; } } else if (bool1 == 1) { - if (((DISTANCE + 2) < distance) || ((DISTANCE - 2) > distance)) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern + if (((distance1> (DISTANCE + 2)) || (distance1<(DISTANCE - 2)))) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern Serial.print("Still motion detected! Distance: "); - Serial.println(distance); + Serial.println(distance1); //Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird snprintf (msg, 50, "%d", 1); client.publish("/gso/bb/104/ultraschall/1", msg); + + return bool1; } else { //Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird @@ -126,8 +144,13 @@ void loop() { //Flag wieder auf 0 bool1 = 0; + return bool1; } } - delay(100); +} + +int getDistance(int duration){ + int distance = duration * 0.034 / 2; + return distance; } diff --git a/arduino/wärmebildkamera/Arduino_ESP32_MLX90640/Arduino_ESP32_MLX90640.ino b/arduino/wärmebildkamera/Arduino_ESP32_MLX90640/Arduino_ESP32_MLX90640.ino new file mode 100644 index 0000000..b681df7 --- /dev/null +++ b/arduino/wärmebildkamera/Arduino_ESP32_MLX90640/Arduino_ESP32_MLX90640.ino @@ -0,0 +1,313 @@ +#include +#include "MLX90640_API.h" +#include "MLX90640_I2C_Driver.h" +#include "SPI.h" +//#include "Adafruit_GFX.h" +//#include "Adafruit_ILI9341.h" + +// For the ESP-WROVER_KIT, these are the default. +/*#define TFT_CS 15 +#define TFT_DC 2 +#define TFT_MOSI 13 +#define TFT_CLK 14 +#define TFT_RST 26 +#define TFT_MISO 12 +#define TFT_LED 27*/ + +/*Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);*/ + +const byte MLX90640_address = 0x33; //Default 7-bit unshifted address of the MLX90640 + +#define TA_SHIFT 8 //Default shift for MLX90640 in open air + +static float mlx90640To[768]; +paramsMLX90640 mlx90640; + +int xPos, yPos; // Abtastposition +int R_colour, G_colour, B_colour; // RGB-Farbwert +int i, j; // Zählvariable +float T_max, T_min; // maximale bzw. minimale gemessene Temperatur +float T_center; // Temperatur in der Bildschirmmitte + + + + +// *************************************** +// **************** SETUP **************** +// *************************************** + +void setup() + { + Serial.begin(115200); + + Wire.begin(); + Wire.setClock(400000); //Increase I2C clock speed to 400kHz + + while (!Serial); //Wait for user to open terminal + + Serial.println("MLX90640 IR Array Example"); + + if (isConnected() == false) + { + Serial.println("MLX90640 not detected at default I2C address. Please check wiring. Freezing."); + while (1); + } + + Serial.println("MLX90640 online!"); + + //Get device parameters - We only have to do this once + int status; + uint16_t eeMLX90640[832]; + + status = MLX90640_DumpEE(MLX90640_address, eeMLX90640); + + if (status != 0) + Serial.println("Failed to load system parameters"); + if (status == 0) + Serial.println("dumpee_okay"); + + status = MLX90640_ExtractParameters(eeMLX90640, &mlx90640); + + if (status != 0) + { + Serial.println("Parameter extraction failed"); + Serial.print(" status = "); + Serial.println(status); + } + + //Once params are extracted, we can release eeMLX90640 array + + MLX90640_I2CWrite(0x33, 0x800D, 6401); // writes the value 1901 (HEX) = 6401 (DEC) in the register at position 0x800D to enable reading out the temperatures!!! + // =============================================================================================================================================================== + + //MLX90640_SetRefreshRate(MLX90640_address, 0x00); //Set rate to 0.25Hz effective - Works + //MLX90640_SetRefreshRate(MLX90640_address, 0x01); //Set rate to 0.5Hz effective - Works + //MLX90640_SetRefreshRate(MLX90640_address, 0x02); //Set rate to 1Hz effective - Works + //MLX90640_SetRefreshRate(MLX90640_address, 0x03); //Set rate to 2Hz effective - Works + MLX90640_SetRefreshRate(MLX90640_address, 0x04); //Set rate to 4Hz effective - Works + //MLX90640_SetRefreshRate(MLX90640_address, 0x05); //Set rate to 8Hz effective - Works at 800kHz + //MLX90640_SetRefreshRate(MLX90640_address, 0x06); //Set rate to 16Hz effective - Works at 800kHz + //MLX90640_SetRefreshRate(MLX90640_address, 0x07); //Set rate to 32Hz effective - fails + + + //pinMode(TFT_LED, OUTPUT); + //digitalWrite(TFT_LED, HIGH); + + /*tft.begin(); + + tft.setRotation(1); + + tft.fillScreen(ILI9341_BLACK); + tft.fillRect(0, 0, 319, 13, tft.color565(255, 0, 10)); + tft.setCursor(100, 3); + tft.setTextSize(1); + tft.setTextColor(ILI9341_YELLOW, tft.color565(255, 0, 10)); + tft.print("Thermographie - stoppi"); + + tft.drawLine(250, 210 - 0, 258, 210 - 0, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 30, 258, 210 - 30, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 60, 258, 210 - 60, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 90, 258, 210 - 90, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 120, 258, 210 - 120, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 150, 258, 210 - 150, tft.color565(255, 255, 255)); + tft.drawLine(250, 210 - 180, 258, 210 - 180, tft.color565(255, 255, 255)); + + tft.setCursor(80, 220); + tft.setTextColor(ILI9341_WHITE, tft.color565(0, 0, 0)); + tft.print("T+ = "); + + + // drawing the colour-scale + // ======================== + + for (i = 0; i < 181; i++) + { + //value = random(180); + + getColour(i); + tft.drawLine(240, 210 - i, 250, 210 - i, tft.color565(R_colour, G_colour, B_colour)); + } + + + +*/ + } +// ********************************** +// ************** LOOP ************** +// ********************************** + +void loop(){ + for (byte x = 0 ; x < 2 ; x++) //Read both subpages + { + uint16_t mlx90640Frame[834]; + int status = MLX90640_GetFrameData(MLX90640_address, mlx90640Frame); + + if (status < 0) + { + Serial.print("GetFrame Error: "); + Serial.println(status); + } + + float vdd = MLX90640_GetVdd(mlx90640Frame, &mlx90640); + float Ta = MLX90640_GetTa(mlx90640Frame, &mlx90640); + + float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature + float emissivity = 0.95; + + MLX90640_CalculateTo(mlx90640Frame, &mlx90640, emissivity, tr, mlx90640To); + } + + + // determine T_min and T_max and eliminate error pixels + // ==================================================== + + mlx90640To[1*32 + 21] = 0.5 * (mlx90640To[1*32 + 20] + mlx90640To[1*32 + 22]); // eliminate the error-pixels + mlx90640To[4*32 + 30] = 0.5 * (mlx90640To[4*32 + 29] + mlx90640To[4*32 + 31]); // eliminate the error-pixels + + T_min = mlx90640To[0]; + T_max = mlx90640To[0]; + + for (i = 1; i < 768; i++) + { + if((mlx90640To[i] > -41) && (mlx90640To[i] < 301)) + { + if(mlx90640To[i] < T_min) + { + T_min = mlx90640To[i]; + } + + if(mlx90640To[i] > T_max) + { + T_max = mlx90640To[i]; + } + } + else if(i > 0) // temperature out of range + { + mlx90640To[i] = mlx90640To[i-1]; + } + else + { + mlx90640To[i] = mlx90640To[i+1]; + } + } + + + // determine T_center + // ================== + + T_center = mlx90640To[11* 32 + 15]; + + // drawing the picture + // =================== + + for (i = 0 ; i < 24 ; i++) + { + for (j = 0; j < 32; j++) + { + mlx90640To[i*32 + j] = 180.0 * (mlx90640To[i*32 + j] - T_min) / (T_max - T_min); + + getColour(mlx90640To[i*32 + j]); + + // tft.fillRect(217 - j * 7, 35 + i * 7, 7, 7, tft.color565(R_colour, G_colour, B_colour)); + } + } + /* + tft.drawLine(217 - 15*7 + 3.5 - 5, 11*7 + 35 + 3.5, 217 - 15*7 + 3.5 + 5, 11*7 + 35 + 3.5, tft.color565(255, 255, 255)); + tft.drawLine(217 - 15*7 + 3.5, 11*7 + 35 + 3.5 - 5, 217 - 15*7 + 3.5, 11*7 + 35 + 3.5 + 5, tft.color565(255, 255, 255)); + + tft.fillRect(260, 25, 37, 10, tft.color565(0, 0, 0)); + tft.fillRect(260, 205, 37, 10, tft.color565(0, 0, 0)); + tft.fillRect(115, 220, 37, 10, tft.color565(0, 0, 0)); + + tft.setTextColor(ILI9341_WHITE, tft.color565(0, 0, 0)); + tft.setCursor(265, 25); + tft.print(T_max, 1); + tft.setCursor(265, 205); + tft.print(T_min, 1); + tft.setCursor(120, 220); + tft.print(T_center, 1); + + tft.setCursor(300, 25); + tft.print("C"); + tft.setCursor(300, 205); + tft.print("C"); + tft.setCursor(155, 220); + tft.print("C"); + */ + delay(20); + } + + + +// =============================== +// ===== determine the colour ==== +// =============================== + +void getColour(int j) + { + if (j >= 0 && j < 30) + { + R_colour = 0; + G_colour = 0; + B_colour = 20 + (120.0/30.0) * j; + } + + if (j >= 30 && j < 60) + { + R_colour = (120.0 / 30) * (j - 30.0); + G_colour = 0; + B_colour = 140 - (60.0/30.0) * (j - 30.0); + } + + if (j >= 60 && j < 90) + { + R_colour = 120 + (135.0/30.0) * (j - 60.0); + G_colour = 0; + B_colour = 80 - (70.0/30.0) * (j - 60.0); + } + + if (j >= 90 && j < 120) + { + R_colour = 255; + G_colour = 0 + (60.0/30.0) * (j - 90.0); + B_colour = 10 - (10.0/30.0) * (j - 90.0); + } + + if (j >= 120 && j < 150) + { + R_colour = 255; + G_colour = 60 + (175.0/30.0) * (j - 120.0); + B_colour = 0; + } + + if (j >= 150 && j <= 180) + { + R_colour = 255; + G_colour = 235 + (20.0/30.0) * (j - 150.0); + B_colour = 0 + 255.0/30.0 * (j - 150.0); + } + + } + + +//Returns true if the MLX90640 is detected on the I2C bus +boolean isConnected() + { + Wire.beginTransmission((uint8_t)MLX90640_address); + + if (Wire.endTransmission() != 0) + return (false); //Sensor did not ACK + + return (true); + } + + + + + + + + + + + diff --git a/arduino/wärmebildkamera/Arduino_ESP32_MLX90640/MLX90640_API.zip b/arduino/wärmebildkamera/Arduino_ESP32_MLX90640/MLX90640_API.zip new file mode 100644 index 0000000..651310c Binary files /dev/null and b/arduino/wärmebildkamera/Arduino_ESP32_MLX90640/MLX90640_API.zip differ diff --git a/arduino/wärmebildkamera/Arduino_ESP32_MLX90640/MLX90640_I2C_Driver.zip b/arduino/wärmebildkamera/Arduino_ESP32_MLX90640/MLX90640_I2C_Driver.zip new file mode 100644 index 0000000..d5c281a Binary files /dev/null and b/arduino/wärmebildkamera/Arduino_ESP32_MLX90640/MLX90640_I2C_Driver.zip differ diff --git a/arduino/wärmebildkamera/Example1_BasicReadings/Example1_BasicReadings.ino b/arduino/wärmebildkamera/Example1_BasicReadings/Example1_BasicReadings.ino new file mode 100644 index 0000000..f1ce9f2 --- /dev/null +++ b/arduino/wärmebildkamera/Example1_BasicReadings/Example1_BasicReadings.ino @@ -0,0 +1,113 @@ + +/* + Read the temperature pixels from the MLX90640 IR array + By: Nathan Seidle + SparkFun Electronics + Date: May 22nd, 2018 + License: MIT. See license file for more information but you can + basically do whatever you want with this code. + + Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14769 + + This example initializes the MLX90640 and outputs the 768 temperature values + from the 768 pixels. + + This example will work with a Teensy 3.1 and above. The MLX90640 requires some + hefty calculations and larger arrays. You will need a microcontroller with 20,000 + bytes or more of RAM. + + This relies on the driver written by Melexis and can be found at: + https://github.com/melexis/mlx90640-library + + Hardware Connections: + Connect the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) + to the Qwiic board + Connect the male pins to the Teensy. The pinouts can be found here: https://www.pjrc.com/teensy/pinout.html + Open the serial monitor at 9600 baud to see the output +*/ + +#include + +#include "MLX90640_API.h" +#include "MLX90640_I2C_Driver.h" + +const byte MLX90640_address = 0x33; //Default 7-bit unshifted address of the MLX90640 + +#define TA_SHIFT 8 //Default shift for MLX90640 in open air + +static float mlx90640To[768]; +paramsMLX90640 mlx90640; + +void setup() +{ + Wire.begin(); + Wire.setClock(100000); //Increase I2C clock speed to 400kHz + + Serial.begin(9600); + while (!Serial); //Wait for user to open terminal + Serial.println("MLX90640 IR Array Example"); + + if (isConnected() == false) + { + Serial.println("MLX90640 not detected at default I2C address. Please check wiring. Freezing."); + while (1); + } + Serial.println("MLX90640 online!"); + + //Get device parameters - We only have to do this once + int status; + uint16_t eeMLX90640[832]; + status = MLX90640_DumpEE(MLX90640_address, eeMLX90640); + if (status != 0) + Serial.println("Failed to load system parameters"); + + status = MLX90640_ExtractParameters(eeMLX90640, &mlx90640); + if (status != 0) + Serial.println("Parameter extraction failed"); + + //Once params are extracted, we can release eeMLX90640 array +} + +void loop() +{ + for (byte x = 0 ; x < 2 ; x++) //Read both subpages + { + uint16_t mlx90640Frame[834]; + int status = MLX90640_GetFrameData(MLX90640_address, mlx90640Frame); + if (status < 0) + { + Serial.print("GetFrame Error: "); + Serial.println(status); + } + + float vdd = MLX90640_GetVdd(mlx90640Frame, &mlx90640); + float Ta = MLX90640_GetTa(mlx90640Frame, &mlx90640); + + float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature + float emissivity = 0.95; + + MLX90640_CalculateTo(mlx90640Frame, &mlx90640, emissivity, tr, mlx90640To); + } + + for (int x = 0 ; x < 10 ; x++) + { + Serial.print("Pixel "); + Serial.print(x); + Serial.print(": "); + Serial.print(mlx90640To[x], 2); + Serial.print("C"); + Serial.println(); + } + + delay(1000); +} + +//Returns true if the MLX90640 is detected on the I2C bus +boolean isConnected() +{ + Wire.beginTransmission((uint8_t)MLX90640_address); + if (Wire.endTransmission() != 0) + return (false); //Sensor did not ACK + return (true); +} diff --git a/arduino/wärmebildkamera/Example1_BasicReadings/MLX90640_API.zip b/arduino/wärmebildkamera/Example1_BasicReadings/MLX90640_API.zip new file mode 100644 index 0000000..651310c Binary files /dev/null and b/arduino/wärmebildkamera/Example1_BasicReadings/MLX90640_API.zip differ diff --git a/arduino/wärmebildkamera/Example1_BasicReadings/MLX90640_I2C_Driver.zip b/arduino/wärmebildkamera/Example1_BasicReadings/MLX90640_I2C_Driver.zip new file mode 100644 index 0000000..00e66cf Binary files /dev/null and b/arduino/wärmebildkamera/Example1_BasicReadings/MLX90640_I2C_Driver.zip differ diff --git a/arduino/wärmebildkamera/MLX90640_API.cpp b/arduino/wärmebildkamera/MLX90640_API.cpp new file mode 100644 index 0000000..c5358fd --- /dev/null +++ b/arduino/wärmebildkamera/MLX90640_API.cpp @@ -0,0 +1,1183 @@ +/** + * @copyright (C) 2017 Melexis N.V. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include "MLX90640_I2C_Driver.h" +#include "MLX90640_API.h" +#include + +void ExtractVDDParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractPTATParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractGainParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractTgcParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractResolutionParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractKsTaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractKsToParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractAlphaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractOffsetParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractKtaPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractKvPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractCPParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractCILCParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +int ExtractDeviatingPixels(uint16_t *eeData, paramsMLX90640 *mlx90640); +int CheckAdjacentPixels(uint16_t pix1, uint16_t pix2); +int CheckEEPROMValid(uint16_t *eeData); + + +int MLX90640_DumpEE(uint8_t slaveAddr, uint16_t *eeData) +{ + return MLX90640_I2CRead(slaveAddr, 0x2400, 832, eeData); +} + +int MLX90640_GetFrameData(uint8_t slaveAddr, uint16_t *frameData) +{ + uint16_t dataReady = 1; + uint16_t controlRegister1; + uint16_t statusRegister; + int error = 1; + uint8_t cnt = 0; + + dataReady = 0; + while(dataReady == 0) + { + error = MLX90640_I2CRead(slaveAddr, 0x8000, 1, &statusRegister); + if(error != 0) + { + return error; + } + dataReady = statusRegister & 0x0008; + } + + while(dataReady != 0 && cnt < 5) + { + error = MLX90640_I2CWrite(slaveAddr, 0x8000, 0x0030); + if(error == -1) + { + return error; + } + + error = MLX90640_I2CRead(slaveAddr, 0x0400, 832, frameData); + if(error != 0) + { + return error; + } + + error = MLX90640_I2CRead(slaveAddr, 0x8000, 1, &statusRegister); + if(error != 0) + { + return error; + } + dataReady = statusRegister & 0x0008; + cnt = cnt + 1; + } + + if(cnt > 4) + { + return -8; + } + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + frameData[832] = controlRegister1; + frameData[833] = statusRegister & 0x0001; + + if(error != 0) + { + return error; + } + + return frameData[833]; +} + +int MLX90640_ExtractParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int error = CheckEEPROMValid(eeData); + + if(error == 0) + { + ExtractVDDParameters(eeData, mlx90640); + ExtractPTATParameters(eeData, mlx90640); + ExtractGainParameters(eeData, mlx90640); + ExtractTgcParameters(eeData, mlx90640); + ExtractResolutionParameters(eeData, mlx90640); + ExtractKsTaParameters(eeData, mlx90640); + ExtractKsToParameters(eeData, mlx90640); + ExtractAlphaParameters(eeData, mlx90640); + ExtractOffsetParameters(eeData, mlx90640); + ExtractKtaPixelParameters(eeData, mlx90640); + ExtractKvPixelParameters(eeData, mlx90640); + ExtractCPParameters(eeData, mlx90640); + ExtractCILCParameters(eeData, mlx90640); + error = ExtractDeviatingPixels(eeData, mlx90640); + } + + return error; + +} + +//------------------------------------------------------------------------------ + +int MLX90640_SetResolution(uint8_t slaveAddr, uint8_t resolution) +{ + uint16_t controlRegister1; + int value; + int error; + + value = (resolution & 0x03) << 10; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + + if(error == 0) + { + value = (controlRegister1 & 0xF3FF) | value; + error = MLX90640_I2CWrite(slaveAddr, 0x800D, value); + } + + return error; +} + +//------------------------------------------------------------------------------ + +int MLX90640_GetCurResolution(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int resolutionRAM; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + if(error != 0) + { + return error; + } + resolutionRAM = (controlRegister1 & 0x0C00) >> 10; + + return resolutionRAM; +} + +//------------------------------------------------------------------------------ + +int MLX90640_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate) +{ + uint16_t controlRegister1; + int value; + int error; + + value = (refreshRate & 0x07)<<7; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + if(error == 0) + { + value = (controlRegister1 & 0xFC7F) | value; + error = MLX90640_I2CWrite(slaveAddr, 0x800D, value); + } + + return error; +} + +//------------------------------------------------------------------------------ + +int MLX90640_GetRefreshRate(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int refreshRate; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + if(error != 0) + { + return error; + } + refreshRate = (controlRegister1 & 0x0380) >> 7; + + return refreshRate; +} + +//------------------------------------------------------------------------------ + +int MLX90640_SetInterleavedMode(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int value; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + + if(error == 0) + { + value = (controlRegister1 & 0xEFFF); + error = MLX90640_I2CWrite(slaveAddr, 0x800D, value); + } + + return error; +} + +//------------------------------------------------------------------------------ + +int MLX90640_SetChessMode(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int value; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + + if(error == 0) + { + value = (controlRegister1 | 0x1000); + error = MLX90640_I2CWrite(slaveAddr, 0x800D, value); + } + + return error; +} + +//------------------------------------------------------------------------------ + +int MLX90640_GetCurMode(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int modeRAM; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + if(error != 0) + { + return error; + } + modeRAM = (controlRegister1 & 0x1000) >> 12; + + return modeRAM; +} + +//------------------------------------------------------------------------------ + +void MLX90640_CalculateTo(uint16_t *frameData, const paramsMLX90640 *params, float emissivity, float tr, float *result) +{ + float vdd; + float ta; + float ta4; + float tr4; + float taTr; + float gain; + float irDataCP[2]; + float irData; + float alphaCompensated; + uint8_t mode; + int8_t ilPattern; + int8_t chessPattern; + int8_t pattern; + int8_t conversionPattern; + float Sx; + float To; + float alphaCorrR[4]; + int8_t range; + uint16_t subPage; + + subPage = frameData[833]; + vdd = MLX90640_GetVdd(frameData, params); + ta = MLX90640_GetTa(frameData, params); + ta4 = pow((ta + 273.15), (double)4); + tr4 = pow((tr + 273.15), (double)4); + taTr = tr4 - (tr4-ta4)/emissivity; + + alphaCorrR[0] = 1 / (1 + params->ksTo[0] * 40); + alphaCorrR[1] = 1 ; + alphaCorrR[2] = (1 + params->ksTo[2] * params->ct[2]); + alphaCorrR[3] = alphaCorrR[2] * (1 + params->ksTo[3] * (params->ct[3] - params->ct[2])); + +//------------------------- Gain calculation ----------------------------------- + gain = frameData[778]; + if(gain > 32767) + { + gain = gain - 65536; + } + + gain = params->gainEE / gain; + +//------------------------- To calculation ------------------------------------- + mode = (frameData[832] & 0x1000) >> 5; + + irDataCP[0] = frameData[776]; + irDataCP[1] = frameData[808]; + for( int i = 0; i < 2; i++) + { + if(irDataCP[i] > 32767) + { + irDataCP[i] = irDataCP[i] - 65536; + } + irDataCP[i] = irDataCP[i] * gain; + } + irDataCP[0] = irDataCP[0] - params->cpOffset[0] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); + if( mode == params->calibrationModeEE) + { + irDataCP[1] = irDataCP[1] - params->cpOffset[1] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); + } + else + { + irDataCP[1] = irDataCP[1] - (params->cpOffset[1] + params->ilChessC[0]) * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); + } + + for( int pixelNumber = 0; pixelNumber < 768; pixelNumber++) + { + ilPattern = pixelNumber / 32 - (pixelNumber / 64) * 2; + chessPattern = ilPattern ^ (pixelNumber - (pixelNumber/2)*2); + conversionPattern = ((pixelNumber + 2) / 4 - (pixelNumber + 3) / 4 + (pixelNumber + 1) / 4 - pixelNumber / 4) * (1 - 2 * ilPattern); + + if(mode == 0) + { + pattern = ilPattern; + } + else + { + pattern = chessPattern; + } + + if(pattern == frameData[833]) + { + irData = frameData[pixelNumber]; + if(irData > 32767) + { + irData = irData - 65536; + } + irData = irData * gain; + + irData = irData - params->offset[pixelNumber]*(1 + params->kta[pixelNumber]*(ta - 25))*(1 + params->kv[pixelNumber]*(vdd - 3.3)); + if(mode != params->calibrationModeEE) + { + irData = irData + params->ilChessC[2] * (2 * ilPattern - 1) - params->ilChessC[1] * conversionPattern; + } + + irData = irData / emissivity; + + irData = irData - params->tgc * irDataCP[subPage]; + + alphaCompensated = (params->alpha[pixelNumber] - params->tgc * params->cpAlpha[subPage])*(1 + params->KsTa * (ta - 25)); + + Sx = pow((double)alphaCompensated, (double)3) * (irData + alphaCompensated * taTr); + Sx = sqrt(sqrt(Sx)) * params->ksTo[1]; + + To = sqrt(sqrt(irData/(alphaCompensated * (1 - params->ksTo[1] * 273.15) + Sx) + taTr)) - 273.15; + + if(To < params->ct[1]) + { + range = 0; + } + else if(To < params->ct[2]) + { + range = 1; + } + else if(To < params->ct[3]) + { + range = 2; + } + else + { + range = 3; + } + + To = sqrt(sqrt(irData / (alphaCompensated * alphaCorrR[range] * (1 + params->ksTo[range] * (To - params->ct[range]))) + taTr)) - 273.15; + + result[pixelNumber] = To; + } + } +} + +//------------------------------------------------------------------------------ + +void MLX90640_GetImage(uint16_t *frameData, const paramsMLX90640 *params, float *result) +{ + float vdd; + float ta; + float gain; + float irDataCP[2]; + float irData; + float alphaCompensated; + uint8_t mode; + int8_t ilPattern; + int8_t chessPattern; + int8_t pattern; + int8_t conversionPattern; + float image; + uint16_t subPage; + + subPage = frameData[833]; + vdd = MLX90640_GetVdd(frameData, params); + ta = MLX90640_GetTa(frameData, params); + +//------------------------- Gain calculation ----------------------------------- + gain = frameData[778]; + if(gain > 32767) + { + gain = gain - 65536; + } + + gain = params->gainEE / gain; + +//------------------------- Image calculation ------------------------------------- + mode = (frameData[832] & 0x1000) >> 5; + + irDataCP[0] = frameData[776]; + irDataCP[1] = frameData[808]; + for( int i = 0; i < 2; i++) + { + if(irDataCP[i] > 32767) + { + irDataCP[i] = irDataCP[i] - 65536; + } + irDataCP[i] = irDataCP[i] * gain; + } + irDataCP[0] = irDataCP[0] - params->cpOffset[0] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); + if( mode == params->calibrationModeEE) + { + irDataCP[1] = irDataCP[1] - params->cpOffset[1] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); + } + else + { + irDataCP[1] = irDataCP[1] - (params->cpOffset[1] + params->ilChessC[0]) * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); + } + + for( int pixelNumber = 0; pixelNumber < 768; pixelNumber++) + { + ilPattern = pixelNumber / 32 - (pixelNumber / 64) * 2; + chessPattern = ilPattern ^ (pixelNumber - (pixelNumber/2)*2); + conversionPattern = ((pixelNumber + 2) / 4 - (pixelNumber + 3) / 4 + (pixelNumber + 1) / 4 - pixelNumber / 4) * (1 - 2 * ilPattern); + + if(mode == 0) + { + pattern = ilPattern; + } + else + { + pattern = chessPattern; + } + + if(pattern == frameData[833]) + { + irData = frameData[pixelNumber]; + if(irData > 32767) + { + irData = irData - 65536; + } + irData = irData * gain; + + irData = irData - params->offset[pixelNumber]*(1 + params->kta[pixelNumber]*(ta - 25))*(1 + params->kv[pixelNumber]*(vdd - 3.3)); + if(mode != params->calibrationModeEE) + { + irData = irData + params->ilChessC[2] * (2 * ilPattern - 1) - params->ilChessC[1] * conversionPattern; + } + + irData = irData - params->tgc * irDataCP[subPage]; + + alphaCompensated = (params->alpha[pixelNumber] - params->tgc * params->cpAlpha[subPage])*(1 + params->KsTa * (ta - 25)); + + image = irData/alphaCompensated; + + result[pixelNumber] = image; + } + } +} + +//------------------------------------------------------------------------------ + +float MLX90640_GetVdd(uint16_t *frameData, const paramsMLX90640 *params) +{ + float vdd; + float resolutionCorrection; + + int resolutionRAM; + + vdd = frameData[810]; + if(vdd > 32767) + { + vdd = vdd - 65536; + } + resolutionRAM = (frameData[832] & 0x0C00) >> 10; + resolutionCorrection = pow(2, (double)params->resolutionEE) / pow(2, (double)resolutionRAM); + vdd = (resolutionCorrection * vdd - params->vdd25) / params->kVdd + 3.3; + + return vdd; +} + +//------------------------------------------------------------------------------ + +float MLX90640_GetTa(uint16_t *frameData, const paramsMLX90640 *params) +{ + float ptat; + float ptatArt; + float vdd; + float ta; + + vdd = MLX90640_GetVdd(frameData, params); + + ptat = frameData[800]; + if(ptat > 32767) + { + ptat = ptat - 65536; + } + + ptatArt = frameData[768]; + if(ptatArt > 32767) + { + ptatArt = ptatArt - 65536; + } + ptatArt = (ptat / (ptat * params->alphaPTAT + ptatArt)) * pow(2, (double)18); + + ta = (ptatArt / (1 + params->KvPTAT * (vdd - 3.3)) - params->vPTAT25); + ta = ta / params->KtPTAT + 25; + + return ta; +} + +//------------------------------------------------------------------------------ + +int MLX90640_GetSubPageNumber(uint16_t *frameData) +{ + return frameData[833]; + +} + +//------------------------------------------------------------------------------ + +void ExtractVDDParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int16_t kVdd; + int16_t vdd25; + + kVdd = eeData[51]; + + kVdd = (eeData[51] & 0xFF00) >> 8; + if(kVdd > 127) + { + kVdd = kVdd - 256; + } + kVdd = 32 * kVdd; + vdd25 = eeData[51] & 0x00FF; + vdd25 = ((vdd25 - 256) << 5) - 8192; + + mlx90640->kVdd = kVdd; + mlx90640->vdd25 = vdd25; +} + +//------------------------------------------------------------------------------ + +void ExtractPTATParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float KvPTAT; + float KtPTAT; + int16_t vPTAT25; + float alphaPTAT; + + KvPTAT = (eeData[50] & 0xFC00) >> 10; + if(KvPTAT > 31) + { + KvPTAT = KvPTAT - 64; + } + KvPTAT = KvPTAT/4096; + + KtPTAT = eeData[50] & 0x03FF; + if(KtPTAT > 511) + { + KtPTAT = KtPTAT - 1024; + } + KtPTAT = KtPTAT/8; + + vPTAT25 = eeData[49]; + + alphaPTAT = (eeData[16] & 0xF000) / pow(2, (double)14) + 8.0f; + + mlx90640->KvPTAT = KvPTAT; + mlx90640->KtPTAT = KtPTAT; + mlx90640->vPTAT25 = vPTAT25; + mlx90640->alphaPTAT = alphaPTAT; +} + +//------------------------------------------------------------------------------ + +void ExtractGainParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int16_t gainEE; + + gainEE = eeData[48]; + if(gainEE > 32767) + { + gainEE = gainEE -65536; + } + + mlx90640->gainEE = gainEE; +} + +//------------------------------------------------------------------------------ + +void ExtractTgcParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float tgc; + tgc = eeData[60] & 0x00FF; + if(tgc > 127) + { + tgc = tgc - 256; + } + tgc = tgc / 32.0f; + + mlx90640->tgc = tgc; +} + +//------------------------------------------------------------------------------ + +void ExtractResolutionParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + uint8_t resolutionEE; + resolutionEE = (eeData[56] & 0x3000) >> 12; + + mlx90640->resolutionEE = resolutionEE; +} + +//------------------------------------------------------------------------------ + +void ExtractKsTaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float KsTa; + KsTa = (eeData[60] & 0xFF00) >> 8; + if(KsTa > 127) + { + KsTa = KsTa -256; + } + KsTa = KsTa / 8192.0f; + + mlx90640->KsTa = KsTa; +} + +//------------------------------------------------------------------------------ + +void ExtractKsToParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int KsToScale; + int8_t step; + + step = ((eeData[63] & 0x3000) >> 12) * 10; + + mlx90640->ct[0] = -40; + mlx90640->ct[1] = 0; + mlx90640->ct[2] = (eeData[63] & 0x00F0) >> 4; + mlx90640->ct[3] = (eeData[63] & 0x0F00) >> 8; + + mlx90640->ct[2] = mlx90640->ct[2]*step; + mlx90640->ct[3] = mlx90640->ct[2] + mlx90640->ct[3]*step; + + KsToScale = (eeData[63] & 0x000F) + 8; + KsToScale = 1 << KsToScale; + + mlx90640->ksTo[0] = eeData[61] & 0x00FF; + mlx90640->ksTo[1] = (eeData[61] & 0xFF00) >> 8; + mlx90640->ksTo[2] = eeData[62] & 0x00FF; + mlx90640->ksTo[3] = (eeData[62] & 0xFF00) >> 8; + + + for(int i = 0; i < 4; i++) + { + if(mlx90640->ksTo[i] > 127) + { + mlx90640->ksTo[i] = mlx90640->ksTo[i] -256; + } + mlx90640->ksTo[i] = mlx90640->ksTo[i] / KsToScale; + } +} + +//------------------------------------------------------------------------------ + +void ExtractAlphaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int accRow[24]; + int accColumn[32]; + int p = 0; + int alphaRef; + uint8_t alphaScale; + uint8_t accRowScale; + uint8_t accColumnScale; + uint8_t accRemScale; + + + accRemScale = eeData[32] & 0x000F; + accColumnScale = (eeData[32] & 0x00F0) >> 4; + accRowScale = (eeData[32] & 0x0F00) >> 8; + alphaScale = ((eeData[32] & 0xF000) >> 12) + 30; + alphaRef = eeData[33]; + + for(int i = 0; i < 6; i++) + { + p = i * 4; + accRow[p + 0] = (eeData[34 + i] & 0x000F); + accRow[p + 1] = (eeData[34 + i] & 0x00F0) >> 4; + accRow[p + 2] = (eeData[34 + i] & 0x0F00) >> 8; + accRow[p + 3] = (eeData[34 + i] & 0xF000) >> 12; + } + + for(int i = 0; i < 24; i++) + { + if (accRow[i] > 7) + { + accRow[i] = accRow[i] - 16; + } + } + + for(int i = 0; i < 8; i++) + { + p = i * 4; + accColumn[p + 0] = (eeData[40 + i] & 0x000F); + accColumn[p + 1] = (eeData[40 + i] & 0x00F0) >> 4; + accColumn[p + 2] = (eeData[40 + i] & 0x0F00) >> 8; + accColumn[p + 3] = (eeData[40 + i] & 0xF000) >> 12; + } + + for(int i = 0; i < 32; i ++) + { + if (accColumn[i] > 7) + { + accColumn[i] = accColumn[i] - 16; + } + } + + for(int i = 0; i < 24; i++) + { + for(int j = 0; j < 32; j ++) + { + p = 32 * i +j; + mlx90640->alpha[p] = (eeData[64 + p] & 0x03F0) >> 4; + if (mlx90640->alpha[p] > 31) + { + mlx90640->alpha[p] = mlx90640->alpha[p] - 64; + } + mlx90640->alpha[p] = mlx90640->alpha[p]*(1 << accRemScale); + mlx90640->alpha[p] = (alphaRef + (accRow[i] << accRowScale) + (accColumn[j] << accColumnScale) + mlx90640->alpha[p]); + mlx90640->alpha[p] = mlx90640->alpha[p] / pow(2,(double)alphaScale); + } + } +} + +//------------------------------------------------------------------------------ + +void ExtractOffsetParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int occRow[24]; + int occColumn[32]; + int p = 0; + int16_t offsetRef; + uint8_t occRowScale; + uint8_t occColumnScale; + uint8_t occRemScale; + + + occRemScale = (eeData[16] & 0x000F); + occColumnScale = (eeData[16] & 0x00F0) >> 4; + occRowScale = (eeData[16] & 0x0F00) >> 8; + offsetRef = eeData[17]; + if (offsetRef > 32767) + { + offsetRef = offsetRef - 65536; + } + + for(int i = 0; i < 6; i++) + { + p = i * 4; + occRow[p + 0] = (eeData[18 + i] & 0x000F); + occRow[p + 1] = (eeData[18 + i] & 0x00F0) >> 4; + occRow[p + 2] = (eeData[18 + i] & 0x0F00) >> 8; + occRow[p + 3] = (eeData[18 + i] & 0xF000) >> 12; + } + + for(int i = 0; i < 24; i++) + { + if (occRow[i] > 7) + { + occRow[i] = occRow[i] - 16; + } + } + + for(int i = 0; i < 8; i++) + { + p = i * 4; + occColumn[p + 0] = (eeData[24 + i] & 0x000F); + occColumn[p + 1] = (eeData[24 + i] & 0x00F0) >> 4; + occColumn[p + 2] = (eeData[24 + i] & 0x0F00) >> 8; + occColumn[p + 3] = (eeData[24 + i] & 0xF000) >> 12; + } + + for(int i = 0; i < 32; i ++) + { + if (occColumn[i] > 7) + { + occColumn[i] = occColumn[i] - 16; + } + } + + for(int i = 0; i < 24; i++) + { + for(int j = 0; j < 32; j ++) + { + p = 32 * i +j; + mlx90640->offset[p] = (eeData[64 + p] & 0xFC00) >> 10; + if (mlx90640->offset[p] > 31) + { + mlx90640->offset[p] = mlx90640->offset[p] - 64; + } + mlx90640->offset[p] = mlx90640->offset[p]*(1 << occRemScale); + mlx90640->offset[p] = (offsetRef + (occRow[i] << occRowScale) + (occColumn[j] << occColumnScale) + mlx90640->offset[p]); + } + } +} + +//------------------------------------------------------------------------------ + +void ExtractKtaPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int p = 0; + int8_t KtaRC[4]; + int8_t KtaRoCo; + int8_t KtaRoCe; + int8_t KtaReCo; + int8_t KtaReCe; + uint8_t ktaScale1; + uint8_t ktaScale2; + uint8_t split; + + KtaRoCo = (eeData[54] & 0xFF00) >> 8; + if (KtaRoCo > 127) + { + KtaRoCo = KtaRoCo - 256; + } + KtaRC[0] = KtaRoCo; + + KtaReCo = (eeData[54] & 0x00FF); + if (KtaReCo > 127) + { + KtaReCo = KtaReCo - 256; + } + KtaRC[2] = KtaReCo; + + KtaRoCe = (eeData[55] & 0xFF00) >> 8; + if (KtaRoCe > 127) + { + KtaRoCe = KtaRoCe - 256; + } + KtaRC[1] = KtaRoCe; + + KtaReCe = (eeData[55] & 0x00FF); + if (KtaReCe > 127) + { + KtaReCe = KtaReCe - 256; + } + KtaRC[3] = KtaReCe; + + ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8; + ktaScale2 = (eeData[56] & 0x000F); + + for(int i = 0; i < 24; i++) + { + for(int j = 0; j < 32; j ++) + { + p = 32 * i +j; + split = 2*(p/32 - (p/64)*2) + p%2; + mlx90640->kta[p] = (eeData[64 + p] & 0x000E) >> 1; + if (mlx90640->kta[p] > 3) + { + mlx90640->kta[p] = mlx90640->kta[p] - 8; + } + mlx90640->kta[p] = mlx90640->kta[p] * (1 << ktaScale2); + mlx90640->kta[p] = KtaRC[split] + mlx90640->kta[p]; + mlx90640->kta[p] = mlx90640->kta[p] / pow(2,(double)ktaScale1); + } + } +} + +//------------------------------------------------------------------------------ + +void ExtractKvPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int p = 0; + int8_t KvT[4]; + int8_t KvRoCo; + int8_t KvRoCe; + int8_t KvReCo; + int8_t KvReCe; + uint8_t kvScale; + uint8_t split; + + KvRoCo = (eeData[52] & 0xF000) >> 12; + if (KvRoCo > 7) + { + KvRoCo = KvRoCo - 16; + } + KvT[0] = KvRoCo; + + KvReCo = (eeData[52] & 0x0F00) >> 8; + if (KvReCo > 7) + { + KvReCo = KvReCo - 16; + } + KvT[2] = KvReCo; + + KvRoCe = (eeData[52] & 0x00F0) >> 4; + if (KvRoCe > 7) + { + KvRoCe = KvRoCe - 16; + } + KvT[1] = KvRoCe; + + KvReCe = (eeData[52] & 0x000F); + if (KvReCe > 7) + { + KvReCe = KvReCe - 16; + } + KvT[3] = KvReCe; + + kvScale = (eeData[56] & 0x0F00) >> 8; + + + for(int i = 0; i < 24; i++) + { + for(int j = 0; j < 32; j ++) + { + p = 32 * i +j; + split = 2*(p/32 - (p/64)*2) + p%2; + mlx90640->kv[p] = KvT[split]; + mlx90640->kv[p] = mlx90640->kv[p] / pow(2,(double)kvScale); + } + } +} + +//------------------------------------------------------------------------------ + +void ExtractCPParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float alphaSP[2]; + int16_t offsetSP[2]; + float cpKv; + float cpKta; + uint8_t alphaScale; + uint8_t ktaScale1; + uint8_t kvScale; + + alphaScale = ((eeData[32] & 0xF000) >> 12) + 27; + + offsetSP[0] = (eeData[58] & 0x03FF); + if (offsetSP[0] > 511) + { + offsetSP[0] = offsetSP[0] - 1024; + } + + offsetSP[1] = (eeData[58] & 0xFC00) >> 10; + if (offsetSP[1] > 31) + { + offsetSP[1] = offsetSP[1] - 64; + } + offsetSP[1] = offsetSP[1] + offsetSP[0]; + + alphaSP[0] = (eeData[57] & 0x03FF); + if (alphaSP[0] > 511) + { + alphaSP[0] = alphaSP[0] - 1024; + } + alphaSP[0] = alphaSP[0] / pow(2,(double)alphaScale); + + alphaSP[1] = (eeData[57] & 0xFC00) >> 10; + if (alphaSP[1] > 31) + { + alphaSP[1] = alphaSP[1] - 64; + } + alphaSP[1] = (1 + alphaSP[1]/128) * alphaSP[0]; + + cpKta = (eeData[59] & 0x00FF); + if (cpKta > 127) + { + cpKta = cpKta - 256; + } + ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8; + mlx90640->cpKta = cpKta / pow(2,(double)ktaScale1); + + cpKv = (eeData[59] & 0xFF00) >> 8; + if (cpKv > 127) + { + cpKv = cpKv - 256; + } + kvScale = (eeData[56] & 0x0F00) >> 8; + mlx90640->cpKv = cpKv / pow(2,(double)kvScale); + + mlx90640->cpAlpha[0] = alphaSP[0]; + mlx90640->cpAlpha[1] = alphaSP[1]; + mlx90640->cpOffset[0] = offsetSP[0]; + mlx90640->cpOffset[1] = offsetSP[1]; +} + +//------------------------------------------------------------------------------ + +void ExtractCILCParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float ilChessC[3]; + uint8_t calibrationModeEE; + + calibrationModeEE = (eeData[10] & 0x0800) >> 4; + calibrationModeEE = calibrationModeEE ^ 0x80; + + ilChessC[0] = (eeData[53] & 0x003F); + if (ilChessC[0] > 31) + { + ilChessC[0] = ilChessC[0] - 64; + } + ilChessC[0] = ilChessC[0] / 16.0f; + + ilChessC[1] = (eeData[53] & 0x07C0) >> 6; + if (ilChessC[1] > 15) + { + ilChessC[1] = ilChessC[1] - 32; + } + ilChessC[1] = ilChessC[1] / 2.0f; + + ilChessC[2] = (eeData[53] & 0xF800) >> 11; + if (ilChessC[2] > 15) + { + ilChessC[2] = ilChessC[2] - 32; + } + ilChessC[2] = ilChessC[2] / 8.0f; + + mlx90640->calibrationModeEE = calibrationModeEE; + mlx90640->ilChessC[0] = ilChessC[0]; + mlx90640->ilChessC[1] = ilChessC[1]; + mlx90640->ilChessC[2] = ilChessC[2]; +} + +//------------------------------------------------------------------------------ + +int ExtractDeviatingPixels(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + uint16_t pixCnt = 0; + uint16_t brokenPixCnt = 0; + uint16_t outlierPixCnt = 0; + int warn = 0; + int i; + + for(pixCnt = 0; pixCnt<5; pixCnt++) + { + mlx90640->brokenPixels[pixCnt] = 0xFFFF; + mlx90640->outlierPixels[pixCnt] = 0xFFFF; + } + + pixCnt = 0; + while (pixCnt < 768 && brokenPixCnt < 5 && outlierPixCnt < 5) + { + if(eeData[pixCnt+64] == 0) + { + mlx90640->brokenPixels[brokenPixCnt] = pixCnt; + brokenPixCnt = brokenPixCnt + 1; + } + else if((eeData[pixCnt+64] & 0x0001) != 0) + { + mlx90640->outlierPixels[outlierPixCnt] = pixCnt; + outlierPixCnt = outlierPixCnt + 1; + } + + pixCnt = pixCnt + 1; + + } + + if(brokenPixCnt > 4) + { + warn = -3; + } + else if(outlierPixCnt > 4) + { + warn = -4; + } + else if((brokenPixCnt + outlierPixCnt) > 4) + { + warn = -5; + } + else + { + for(pixCnt=0; pixCntbrokenPixels[pixCnt],mlx90640->brokenPixels[i]); + if(warn != 0) + { + return warn; + } + } + } + + for(pixCnt=0; pixCntoutlierPixels[pixCnt],mlx90640->outlierPixels[i]); + if(warn != 0) + { + return warn; + } + } + } + + for(pixCnt=0; pixCntbrokenPixels[pixCnt],mlx90640->outlierPixels[i]); + if(warn != 0) + { + return warn; + } + } + } + + } + + + return warn; + +} + +//------------------------------------------------------------------------------ + + int CheckAdjacentPixels(uint16_t pix1, uint16_t pix2) + { + int pixPosDif; + + pixPosDif = pix1 - pix2; + if(pixPosDif > -34 && pixPosDif < -30) + { + return -6; + } + if(pixPosDif > -2 && pixPosDif < 2) + { + return -6; + } + if(pixPosDif > 30 && pixPosDif < 34) + { + return -6; + } + + return 0; + } + + //------------------------------------------------------------------------------ + + int CheckEEPROMValid(uint16_t *eeData) + { + int deviceSelect; + deviceSelect = eeData[10] & 0x0040; + if(deviceSelect == 0) + { + return 0; + } + + return -7; + } diff --git a/arduino/wärmebildkamera/MLX90640_API.h b/arduino/wärmebildkamera/MLX90640_API.h new file mode 100644 index 0000000..ea0f4bc --- /dev/null +++ b/arduino/wärmebildkamera/MLX90640_API.h @@ -0,0 +1,64 @@ +/** + * @copyright (C) 2017 Melexis N.V. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef _MLX640_API_H_ +#define _MLX640_API_H_ + + typedef struct + { + int16_t kVdd; + int16_t vdd25; + float KvPTAT; + float KtPTAT; + uint16_t vPTAT25; + float alphaPTAT; + int16_t gainEE; + float tgc; + float cpKv; + float cpKta; + uint8_t resolutionEE; + uint8_t calibrationModeEE; + float KsTa; + float ksTo[4]; + int16_t ct[4]; + float alpha[768]; + int16_t offset[768]; + float kta[768]; + float kv[768]; + float cpAlpha[2]; + int16_t cpOffset[2]; + float ilChessC[3]; + uint16_t brokenPixels[5]; + uint16_t outlierPixels[5]; + } paramsMLX90640; + + int MLX90640_DumpEE(uint8_t slaveAddr, uint16_t *eeData); + int MLX90640_GetFrameData(uint8_t slaveAddr, uint16_t *frameData); + int MLX90640_ExtractParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); + float MLX90640_GetVdd(uint16_t *frameData, const paramsMLX90640 *params); + float MLX90640_GetTa(uint16_t *frameData, const paramsMLX90640 *params); + void MLX90640_GetImage(uint16_t *frameData, const paramsMLX90640 *params, float *result); + void MLX90640_CalculateTo(uint16_t *frameData, const paramsMLX90640 *params, float emissivity, float tr, float *result); + int MLX90640_SetResolution(uint8_t slaveAddr, uint8_t resolution); + int MLX90640_GetCurResolution(uint8_t slaveAddr); + int MLX90640_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate); + int MLX90640_GetRefreshRate(uint8_t slaveAddr); + int MLX90640_GetSubPageNumber(uint16_t *frameData); + int MLX90640_GetCurMode(uint8_t slaveAddr); + int MLX90640_SetInterleavedMode(uint8_t slaveAddr); + int MLX90640_SetChessMode(uint8_t slaveAddr); + +#endif diff --git a/arduino/wärmebildkamera/MLX90640_I2C_Driver.cpp b/arduino/wärmebildkamera/MLX90640_I2C_Driver.cpp new file mode 100644 index 0000000..0689a49 --- /dev/null +++ b/arduino/wärmebildkamera/MLX90640_I2C_Driver.cpp @@ -0,0 +1,102 @@ +/** + @copyright (C) 2017 Melexis N.V. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +#include +#include +#include "MLX90640_I2C_Driver.h" + +void MLX90640_I2CInit() +{ + +} + +//Read a number of words from startAddress. Store into Data array. +//Returns 0 if successful, -1 if error +int MLX90640_I2CRead(uint8_t _deviceAddress, unsigned int startAddress, unsigned int nWordsRead, uint16_t *data) +{ + + //Caller passes number of 'unsigned ints to read', increase this to 'bytes to read' + uint16_t bytesRemaining = nWordsRead * 2; + + //It doesn't look like sequential read works. Do we need to re-issue the address command each time? + + uint16_t dataSpot = 0; //Start at beginning of array + + //Setup a series of chunked I2C_BUFFER_LENGTH byte reads + while (bytesRemaining > 0) + { + Wire.beginTransmission(_deviceAddress); + Wire.write(startAddress >> 8); //MSB + Wire.write(startAddress & 0xFF); //LSB + Wire.endTransmission(false) + uint16_t numberOfBytesToRead = bytesRemaining; + if (numberOfBytesToRead > I2C_BUFFER_LENGTH) numberOfBytesToRead = I2C_BUFFER_LENGTH; + + Wire.requestFrom((uint8_t)_deviceAddress, numberOfBytesToRead); + if (Wire.available()) + { + for (uint16_t x = 0 ; x < numberOfBytesToRead / 2; x++) + { + //Store data into array + data[dataSpot] = Wire.read() << 8; //MSB + data[dataSpot] |= Wire.read(); //LSB + + dataSpot++; + } + } + + bytesRemaining -= numberOfBytesToRead; + + startAddress += numberOfBytesToRead / 2; + } + + return (0); //Success +} + +//Set I2C Freq, in kHz +//MLX90640_I2CFreqSet(1000) sets frequency to 1MHz +void MLX90640_I2CFreqSet(int freq) +{ + //i2c.frequency(1000 * freq); + Wire.setClock((long)1000 * freq); +} + +//Write two bytes to a two byte address +int MLX90640_I2CWrite(uint8_t _deviceAddress, unsigned int writeAddress, uint16_t data) +{ + Wire.beginTransmission((uint8_t)_deviceAddress); + Wire.write(writeAddress >> 8); //MSB + Wire.write(writeAddress & 0xFF); //LSB + Wire.write(data >> 8); //MSB + Wire.write(data & 0xFF); //LSB + if (Wire.endTransmission() != 0) + { + //Sensor did not ACK + Serial.println("Error: Sensor did not ack"); + return (-1); + } + + uint16_t dataCheck; + MLX90640_I2CRead(_deviceAddress, writeAddress, 1, &dataCheck); + if (dataCheck != data) + { + //Serial.println("The write request didn't stick"); + return -2; + } + + return (0); //Success +} diff --git a/arduino/wärmebildkamera/MLX90640_I2C_Driver.h b/arduino/wärmebildkamera/MLX90640_I2C_Driver.h new file mode 100644 index 0000000..4de44d0 --- /dev/null +++ b/arduino/wärmebildkamera/MLX90640_I2C_Driver.h @@ -0,0 +1,51 @@ +/** + @copyright (C) 2017 Melexis N.V. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ +#ifndef _MLX90640_I2C_Driver_H_ +#define _MLX90640_I2C_Driver_H_ + +#include + +//Define the size of the I2C buffer based on the platform the user has +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) + +//I2C_BUFFER_LENGTH is defined in Wire.H +#define I2C_BUFFER_LENGTH BUFFER_LENGTH + +#elif defined(__SAMD21G18A__) + +//SAMD21 uses RingBuffer.h +#define I2C_BUFFER_LENGTH SERIAL_BUFFER_SIZE + +#elif __MK20DX256__ +//Teensy 3.2 +#define I2C_BUFFER_LENGTH 32 + +#else + +//The catch-all default is 32 +#define I2C_BUFFER_LENGTH 32 + +#endif +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + +void MLX90640_I2CInit(void); +int MLX90640_I2CRead(uint8_t slaveAddr, unsigned int startAddress, unsigned int nWordsRead, uint16_t *data); +int MLX90640_I2CWrite(uint8_t slaveAddr, unsigned int writeAddress, uint16_t data); +void MLX90640_I2CFreqSet(int freq); +#endif diff --git a/arduino/wärmebildkamera/mlx90640/mlx90640.ino b/arduino/wärmebildkamera/mlx90640/mlx90640.ino new file mode 100644 index 0000000..95c2b6e --- /dev/null +++ b/arduino/wärmebildkamera/mlx90640/mlx90640.ino @@ -0,0 +1,9 @@ +void setup() { + // put your setup code here, to run once: + +} + +void loop() { + // put your main code here, to run repeatedly: + +}