├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── custom.md │ └── feature_request.md └── workflows │ ├── run-ci-arduino.yml │ ├── stale.yml │ └── sync_issues.yml ├── .gitlab-ci.yml ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── examples ├── BasicReadings │ └── BasicReadings.ino ├── MaxRefreshRate │ └── MaxRefreshRate.ino └── Visualization │ └── Visualization.ino ├── keywords.txt ├── library.json ├── library.properties └── src ├── MLX90640_API.cpp ├── MLX90640_API.h ├── MLX90641_API.cpp ├── MLX90641_API.h ├── MLX9064X_I2C_Driver.cpp └── MLX9064X_I2C_Driver.h /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/custom.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Custom issue template 3 | about: Describe this issue template's purpose here. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/workflows/run-ci-arduino.yml: -------------------------------------------------------------------------------- 1 | name: Run Ci Arduino 2 | 3 | on: 4 | push: 5 | pull_request: 6 | repository_dispatch: 7 | types: [trigger-workflow] 8 | 9 | jobs: 10 | ci-arduino: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - name: Checkout repository 15 | uses: actions/checkout@v4 16 | 17 | - name: Checkout script repository 18 | uses: actions/checkout@v4 19 | with: 20 | repository: Seeed-Studio/ci-arduino 21 | path: ci 22 | 23 | - name: Setup arduino cli 24 | uses: arduino/setup-arduino-cli@v2.0.0 25 | 26 | - name: Create a depend.list file 27 | run: | 28 | # eg: echo "" >> depend.list 29 | 30 | - name: Create a ignore.list file 31 | run: | 32 | # eg: echo "," >> ignore.list 33 | 34 | - name: Build sketch 35 | run: ./ci/tools/compile.sh 36 | 37 | - name: Build result 38 | run: | 39 | cat build.log 40 | if [ ${{ github.event_name }} == 'pull_request' ] && [ -f compile.failed ]; then 41 | exit 1 42 | fi 43 | 44 | - name: Generate issue 45 | if: ${{ github.event_name != 'pull_request' }} 46 | run: ./ci/tools/issue.sh 47 | env: 48 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} 49 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | name: 'Close stale issues and PRs' 2 | 3 | on: 4 | workflow_dispatch: 5 | schedule: 6 | - cron: '0 4 * * *' 7 | 8 | jobs: 9 | stale: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - name: Checkout repository 14 | uses: actions/checkout@v4 15 | 16 | - name: Checkout script repository 17 | uses: actions/checkout@v4 18 | with: 19 | repository: Seeed-Studio/sync-github-all-issues 20 | path: ci 21 | 22 | - name: Run script 23 | run: ./ci/tools/stale.sh 24 | env: 25 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} 26 | -------------------------------------------------------------------------------- /.github/workflows/sync_issues.yml: -------------------------------------------------------------------------------- 1 | name: Automate Issue Management 2 | 3 | on: 4 | issues: 5 | types: 6 | - opened 7 | - edited 8 | - assigned 9 | - unassigned 10 | - labeled 11 | - unlabeled 12 | - reopened 13 | 14 | jobs: 15 | add_issue_to_project: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: Add issue to GitHub Project 19 | uses: actions/add-to-project@v1.0.2 20 | with: 21 | project-url: https://github.com/orgs/Seeed-Studio/projects/17 22 | github-token: ${{ secrets.ISSUE_ASSEMBLE }} 23 | labeled: bug 24 | label-operator: NOT -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | build: 2 | tags: 3 | - nas 4 | script: 5 | - wget -c https://files.seeedstudio.com/arduino/seeed-arduino-ci.sh 6 | - chmod +x seeed-arduino-ci.sh 7 | - bash $PWD/seeed-arduino-ci.sh BasicReadings:uno/MaxRefreshRate:uno/Visualization:uno 8 | 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | 2 | language: generic 3 | dist: bionic 4 | sudo: false 5 | cache: 6 | directories: 7 | - ~/arduino_ide 8 | - ~/.arduino15/packages/ 9 | 10 | before_install: 11 | - wget -c https://files.seeedstudio.com/arduino/seeed-arduino-ci.sh 12 | 13 | script: 14 | - chmod +x seeed-arduino-ci.sh 15 | - cat $PWD/seeed-arduino-ci.sh 16 | - bash $PWD/seeed-arduino-ci.sh BasicReadings:uno/MaxRefreshRate:uno/Visualization:uno 17 | 18 | notifications: 19 | email: 20 | on_success: change 21 | on_failure: change 22 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at zuobaozhu@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing guidelines 2 | 3 | All guidelines for contributing to the Seeed_Arduino_MLX9064x repository can be found at [`How to contribute guideline`](https://github.com/Seeed-Studio/Seeed_Arduino_MLX9064x/wiki/How_to_contribute). 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Seeed Studio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Seeed Arduino MLX90640 [![Build Status](https://travis-ci.com/Seeed-Studio/Seeed_Arduino_MLX90640.svg?branch=master)](https://travis-ci.com/Seeed-Studio/Seeed_Arduino_MLX90640) 2 | 3 | The inspiration for this product came from [Melexis's mlx90640](https://github.com/melexis/mlx90640-library/) and [Melexis's mlx90641](https://github.com/melexis/mlx90641-library/). This relies on the driver written by Melexis. 4 | 5 | ## Introduction 6 | 7 | [Grove - Thermal Imaging Camera](https://www.seeedstudio.com/Grove-Thermal-Imaging-Camera-IR-Array-MLX90640-110-degree-p-4334.html) is a thermal sensor which carries MLX90640 IR Array. The camera can present dynamic thermal images and detect the surrounding temperature from -40℃~300℃. The camera with narrow-angle/wide-angle has an FOV(Field of View) of 55°x35°/110°x75°. 8 | 9 | The IR thermal camera carries a 32x24 array of thermal sensors (MLX90640), it can detect the temperature of objects from feet away with the accuracy of ±1.5℃. In order to obtain the thermal image easily, I2C protocol is used to get the low-resolution image from the camera. 10 | 11 | This module connects the MCU with the I2C interface. However, it needs an MCU which has over 20000 bytes of RAM to drive the camera. As a matter of fact, Dev board like Arduino Uno can not be used with this Sensor camera due to its lower ability of calculation. We recommend you to choose Arch Mix as an MCU to control the camera because it really has a good performance to process the complex data from the IR sensor camera. 12 | ![](https://raw.githubusercontent.com/SeeedDocument/IR-thermal-imaging-sensor-MLX90640-/master/114020142-previewbig.jpg) 13 | 14 | ## Note: 15 | - **Visualization** The visual effect is as follows, you can use the following command to install his upper computer on the raspberry PI or PC 16 | ``` 17 | pip3 install seeed_python_ircamera 18 | 19 | #PortName is like COM1,COM2 in Windows system. 20 | #MLX90640 is an optional chip type , MLX90640 or MLX90641 can be supported for now. 21 | ircamera PortName MLX90640 22 | ``` 23 | 24 | ![](https://raw.githubusercontent.com/SeeedDocument/IR-thermal-imaging-sensor-MLX90640-/master/20191121_173909.gif) 25 | 26 | 27 | 28 | _______________ 29 | This software is written by Baozhu baozhu.zuo@gmail.com for seeed studio 30 | and is licensed under The MIT License. Check License.txt for more information. 31 | 32 | Contributing to this software is warmly welcomed. You can do this basically by 33 | forking, committing modifications and then pulling requests (follow the links above 34 | for operating guide). Adding change log and your contact into file header is encouraged. 35 | Thanks for your contribution. 36 | 37 | Seeed Studio is an open hardware facilitation company based in Shenzhen, China. 38 | Benefiting from local manufacture power and convenient global logistic system, 39 | we integrate resources to serve new era of innovation. Seeed also works with 40 | global distributors and partners to push open hardware movement. 41 | -------------------------------------------------------------------------------- /examples/BasicReadings/BasicReadings.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Read the temperature pixels from the MLX90640 IR array 3 | */ 4 | 5 | #include 6 | 7 | #include "MLX90640_API.h" 8 | #include "MLX9064X_I2C_Driver.h" 9 | 10 | #if defined(ARDUINO_ARCH_AVR) 11 | #define debug Serial 12 | 13 | #elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM) 14 | #define debug Serial 15 | #else 16 | #define debug Serial 17 | #endif 18 | 19 | 20 | 21 | 22 | const byte MLX90640_address = 0x33; //Default 7-bit unshifted address of the MLX90640 23 | 24 | #define TA_SHIFT 8 //Default shift for MLX90640 in open air 25 | 26 | static float mlx90640To[768]; 27 | paramsMLX90640 mlx90640; 28 | 29 | void setup() { 30 | Wire.begin(); 31 | Wire.setClock(400000); //Increase I2C clock speed to 400kHz 32 | 33 | debug.begin(9600); 34 | while (!debug); //Wait for user to open terminal 35 | debug.println("MLX90640 IR Array Example"); 36 | 37 | if (isConnected() == false) { 38 | debug.println("MLX90640 not detected at default I2C address. Please check wiring. Freezing."); 39 | while (1); 40 | } 41 | debug.println("MLX90640 online!"); 42 | 43 | //Get device parameters - We only have to do this once 44 | int status; 45 | uint16_t eeMLX90640[832]; 46 | status = MLX90640_DumpEE(MLX90640_address, eeMLX90640); 47 | if (status != 0) { 48 | debug.println("Failed to load system parameters"); 49 | } 50 | 51 | status = MLX90640_ExtractParameters(eeMLX90640, &mlx90640); 52 | if (status != 0) { 53 | debug.println("Parameter extraction failed"); 54 | } 55 | 56 | //Once params are extracted, we can release eeMLX90640 array 57 | } 58 | 59 | void loop() { 60 | for (byte x = 0 ; x < 2 ; x++) { //Read both subpages 61 | uint16_t mlx90640Frame[834]; 62 | int status = MLX90640_GetFrameData(MLX90640_address, mlx90640Frame); 63 | if (status < 0) { 64 | debug.print("GetFrame Error: "); 65 | debug.println(status); 66 | } 67 | 68 | float vdd = MLX90640_GetVdd(mlx90640Frame, &mlx90640); 69 | float Ta = MLX90640_GetTa(mlx90640Frame, &mlx90640); 70 | 71 | float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature 72 | float emissivity = 0.95; 73 | 74 | MLX90640_CalculateTo(mlx90640Frame, &mlx90640, emissivity, tr, mlx90640To); 75 | } 76 | 77 | for (int x = 0 ; x < 10 ; x++) { 78 | debug.print("Pixel "); 79 | debug.print(x); 80 | debug.print(": "); 81 | debug.print(mlx90640To[x], 2); 82 | debug.print("C"); 83 | debug.println(); 84 | } 85 | 86 | delay(1000); 87 | } 88 | 89 | //Returns true if the MLX90640 is detected on the I2C bus 90 | boolean isConnected() { 91 | Wire.beginTransmission((uint8_t)MLX90640_address); 92 | if (Wire.endTransmission() != 0) { 93 | return (false); //Sensor did not ACK 94 | } 95 | return (true); 96 | } 97 | 98 | -------------------------------------------------------------------------------- /examples/MaxRefreshRate/MaxRefreshRate.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Read the MLX90640 temperature readings as quickly as possible 3 | */ 4 | 5 | #include 6 | 7 | #include "MLX90640_API.h" 8 | #include "MLX9064X_I2C_Driver.h" 9 | 10 | 11 | 12 | #if defined(ARDUINO_ARCH_AVR) 13 | #define debug Serial 14 | 15 | #elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM) 16 | #define debug Serial 17 | #else 18 | #define debug Serial 19 | #endif 20 | 21 | 22 | 23 | const byte MLX90640_address = 0x33; //Default 7-bit unshifted address of the MLX90640 24 | 25 | #define TA_SHIFT 8 //Default shift for MLX90640 in open air 26 | 27 | float mlx90640To[768]; 28 | paramsMLX90640 mlx90640; 29 | 30 | const byte calcStart = 33; //Pin that goes high/low when calculations are complete 31 | //This makes the timing visible on the logic analyzer 32 | 33 | void setup() { 34 | pinMode(calcStart, OUTPUT); 35 | 36 | Wire.begin(); 37 | Wire.setClock(400000); //Increase I2C clock speed to 400kHz 38 | 39 | debug.begin(115200); //Fast debug as possible 40 | 41 | while (!debug); //Wait for user to open terminal 42 | //debug.println("MLX90640 IR Array Example"); 43 | 44 | if (isConnected() == false) { 45 | debug.println("MLX90640 not detected at default I2C address. Please check wiring. Freezing."); 46 | while (1); 47 | } 48 | 49 | //Get device parameters - We only have to do this once 50 | int status; 51 | uint16_t eeMLX90640[832]; 52 | status = MLX90640_DumpEE(MLX90640_address, eeMLX90640); 53 | if (status != 0) { 54 | debug.println("Failed to load system parameters"); 55 | } 56 | 57 | status = MLX90640_ExtractParameters(eeMLX90640, &mlx90640); 58 | if (status != 0) { 59 | debug.println("Parameter extraction failed"); 60 | } 61 | 62 | //Once params are extracted, we can release eeMLX90640 array 63 | 64 | //Set refresh rate 65 | //A rate of 0.5Hz takes 4Sec per reading because we have to read two frames to get complete picture 66 | //MLX90640_SetRefreshRate(MLX90640_address, 0x00); //Set rate to 0.5Hz effective - Works 67 | //MLX90640_SetRefreshRate(MLX90640_address, 0x01); //Set rate to 1Hz effective - Works 68 | //MLX90640_SetRefreshRate(MLX90640_address, 0x02); //Set rate to 2Hz effective - Works 69 | //MLX90640_SetRefreshRate(MLX90640_address, 0x03); //Set rate to 4Hz effective - Works 70 | MLX90640_SetRefreshRate(MLX90640_address, 0x04); //Set rate to 8Hz effective - Works 71 | //MLX90640_SetRefreshRate(MLX90640_address, 0x05); //Set rate to 16Hz effective - Works at 800kHz 72 | //MLX90640_SetRefreshRate(MLX90640_address, 0x06); //Set rate to 32Hz effective - Works at 800kHz 73 | //MLX90640_SetRefreshRate(MLX90640_address, 0x07); //Set rate to 64Hz effective - fails 74 | 75 | //Once EEPROM has been read at 400kHz we can increase to 1MHz 76 | Wire.setClock(1000000); //Teensy will now run I2C at 800kHz (because of clock division) 77 | } 78 | 79 | void loop() { 80 | long startTime = millis(); 81 | for (byte x = 0 ; x < 2 ; x++) { 82 | uint16_t mlx90640Frame[834]; 83 | int status = MLX90640_GetFrameData(MLX90640_address, mlx90640Frame); 84 | 85 | digitalWrite(calcStart, HIGH); 86 | float vdd = MLX90640_GetVdd(mlx90640Frame, &mlx90640); 87 | float Ta = MLX90640_GetTa(mlx90640Frame, &mlx90640); 88 | 89 | float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature 90 | float emissivity = 0.95; 91 | 92 | MLX90640_CalculateTo(mlx90640Frame, &mlx90640, emissivity, tr, mlx90640To); 93 | digitalWrite(calcStart, LOW); 94 | //Calculation time on a Teensy 3.5 is 71ms 95 | } 96 | long stopReadTime = millis(); 97 | 98 | for (int x = 0 ; x < 768 ; x++) { 99 | //if(x % 8 == 0) debug.println(); 100 | debug.print(mlx90640To[x], 2); 101 | debug.print(","); 102 | } 103 | debug.println(""); 104 | long stopPrintTime = millis(); 105 | 106 | debug.print("Read rate: "); 107 | debug.print(1000.0 / (stopReadTime - startTime), 2); 108 | debug.println(" Hz"); 109 | debug.print("Read plus print rate: "); 110 | debug.print(1000.0 / (stopPrintTime - startTime), 2); 111 | debug.println(" Hz"); 112 | } 113 | 114 | //Returns true if the MLX90640 is detected on the I2C bus 115 | boolean isConnected() { 116 | Wire.beginTransmission((uint8_t)MLX90640_address); 117 | if (Wire.endTransmission() != 0) { 118 | return (false); //Sensor did not ACK 119 | } 120 | return (true); 121 | } 122 | 123 | -------------------------------------------------------------------------------- /examples/Visualization/Visualization.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Output the temperature readings to all pixels to be read by a Processing visualizer 3 | */ 4 | 5 | #include 6 | 7 | #define USE_MLX90641 8 | 9 | #ifndef USE_MLX90641 10 | #include "MLX90640_API.h" 11 | #else 12 | #include "MLX90641_API.h" 13 | #endif 14 | 15 | #include "MLX9064X_I2C_Driver.h" 16 | 17 | #if defined(ARDUINO_ARCH_AVR) 18 | #define debug Serial 19 | 20 | #elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM) 21 | #define debug Serial 22 | #else 23 | #define debug Serial 24 | #endif 25 | 26 | #ifdef USE_MLX90641 27 | const byte MLX90641_address = 0x33; //Default 7-bit unshifted address of the MLX90641 28 | #define TA_SHIFT 8 //Default shift for MLX90641 in open air 29 | 30 | uint16_t eeMLX90641[832]; 31 | float MLX90641To[192]; 32 | uint16_t MLX90641Frame[242]; 33 | paramsMLX90641 MLX90641; 34 | int errorno = 0; 35 | #else 36 | const byte MLX90640_address = 0x33; //Default 7-bit unshifted address of the MLX90640 37 | 38 | #define TA_SHIFT 8 //Default shift for MLX90640 in open air 39 | 40 | float mlx90640To[768]; 41 | paramsMLX90640 mlx90640; 42 | #endif 43 | void setup() { 44 | Wire.begin(); 45 | Wire.setClock(400000); //Increase I2C clock speed to 400kHz 46 | 47 | debug.begin(115200); //Fast debug as possible 48 | 49 | while (!debug); //Wait for user to open terminal 50 | //debug.println("MLX90640 IR Array Example"); 51 | 52 | 53 | #ifndef USE_MLX90641 54 | if (isConnected() == false) { 55 | debug.println("MLX9064x not detected at default I2C address. Please check wiring. Freezing."); 56 | while (1); 57 | } 58 | //Get device parameters - We only have to do this once 59 | int status; 60 | uint16_t eeMLX90640[832]; 61 | status = MLX90640_DumpEE(MLX90640_address, eeMLX90640); 62 | if (status != 0) { 63 | debug.println("Failed to load system parameters"); 64 | } 65 | 66 | status = MLX90640_ExtractParameters(eeMLX90640, &mlx90640); 67 | if (status != 0) { 68 | debug.println("Parameter extraction failed"); 69 | } 70 | 71 | //Once params are extracted, we can release eeMLX90640 array 72 | 73 | //MLX90640_SetRefreshRate(MLX90640_address, 0x02); //Set rate to 2Hz 74 | MLX90640_SetRefreshRate(MLX90640_address, 0x03); //Set rate to 4Hz 75 | //MLX90640_SetRefreshRate(MLX90640_address, 0x07); //Set rate to 64H 76 | #else 77 | if (isConnected() == false) { 78 | debug.println("MLX90641 not detected at default I2C address. Please check wiring. Freezing."); 79 | while (1); 80 | } 81 | //Get device parameters - We only have to do this once 82 | int status; 83 | status = MLX90641_DumpEE(MLX90641_address, eeMLX90641); 84 | errorno = status;//MLX90641_CheckEEPROMValid(eeMLX90641);//eeMLX90641[10] & 0x0040;// 85 | 86 | if (status != 0) { 87 | debug.println("Failed to load system parameters"); 88 | while(1); 89 | } 90 | 91 | status = MLX90641_ExtractParameters(eeMLX90641, &MLX90641); 92 | //errorno = status; 93 | if (status != 0) { 94 | debug.println("Parameter extraction failed"); 95 | while(1); 96 | } 97 | 98 | //Once params are extracted, we can release eeMLX90641 array 99 | 100 | //MLX90641_SetRefreshRate(MLX90641_address, 0x02); //Set rate to 2Hz 101 | MLX90641_SetRefreshRate(MLX90641_address, 0x03); //Set rate to 4Hz 102 | //MLX90641_SetRefreshRate(MLX90641_address, 0x07); //Set rate to 64Hz 103 | #endif 104 | 105 | } 106 | 107 | void loop() { 108 | #ifndef USE_MLX90641 109 | long startTime = millis(); 110 | for (byte x = 0 ; x < 2 ; x++) { 111 | uint16_t mlx90640Frame[834]; 112 | int status = MLX90640_GetFrameData(MLX90640_address, mlx90640Frame); 113 | 114 | float vdd = MLX90640_GetVdd(mlx90640Frame, &mlx90640); 115 | float Ta = MLX90640_GetTa(mlx90640Frame, &mlx90640); 116 | 117 | float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature 118 | float emissivity = 0.95; 119 | 120 | MLX90640_CalculateTo(mlx90640Frame, &mlx90640, emissivity, tr, mlx90640To); 121 | } 122 | long stopTime = millis(); 123 | 124 | for (int x = 0 ; x < 768 ; x++) { 125 | //if(x % 8 == 0) debug.println(); 126 | debug.print(mlx90640To[x], 2); 127 | debug.print(","); 128 | } 129 | debug.println(""); 130 | #else 131 | long startTime = millis(); 132 | 133 | for (byte x = 0 ; x < 2 ; x++) { 134 | int status = MLX90641_GetFrameData(MLX90641_address, MLX90641Frame); 135 | 136 | float vdd = MLX90641_GetVdd(MLX90641Frame, &MLX90641); 137 | float Ta = MLX90641_GetTa(MLX90641Frame, &MLX90641); 138 | 139 | float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature 140 | float emissivity = 0.95; 141 | 142 | MLX90641_CalculateTo(MLX90641Frame, &MLX90641, emissivity, tr, MLX90641To); 143 | } 144 | long stopTime = millis(); 145 | /* 146 | debug.print("vdd="); 147 | debug.print(vdd,2); 148 | debug.print(",Ta="); 149 | debug.print(Ta,2); 150 | 151 | debug.print(",errorno="); 152 | debug.print(errorno,DEC); 153 | 154 | 155 | for (int x = 0 ; x < 64 ; x++) { 156 | debug.print(MLX90641Frame[x], HEX); 157 | debug.print(","); 158 | } 159 | 160 | delay(1000); 161 | */ 162 | for (int x = 0 ; x < 192 ; x++) { 163 | debug.print(MLX90641To[x], 2); 164 | debug.print(","); 165 | } 166 | debug.println(""); 167 | #endif 168 | } 169 | 170 | //Returns true if the MLX90640 is detected on the I2C bus 171 | boolean isConnected() { 172 | #ifndef USE_MLX90641 173 | Wire.beginTransmission((uint8_t)MLX90640_address); 174 | #else 175 | Wire.beginTransmission((uint8_t)MLX90641_address); 176 | #endif 177 | if (Wire.endTransmission() != 0) { 178 | return (false); //Sensor did not ACK 179 | } 180 | return (true); 181 | } 182 | 183 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | MLX90640 KEYWORD1 2 | 3 | MLX90640_DumpEE KEYWORD2 4 | MLX90640_GetFrameData KEYWORD2 5 | MLX90640_ExtractParameters KEYWORD2 6 | MLX90640_GetVdd KEYWORD2 7 | MLX90640_GetTa KEYWORD2 8 | MLX90640_GetImage KEYWORD2 9 | MLX90640_CalculateTo KEYWORD2 10 | MLX90640_SetResolution KEYWORD2 11 | MLX90640_GetCurResolution KEYWORD2 12 | MLX90640_SetRefreshRate KEYWORD2 13 | MLX90640_GetRefreshRate KEYWORD2 14 | MLX90640_GetSubPageNumber KEYWORD2 15 | MLX90640_GetCurMode KEYWORD2 16 | MLX90640_SetInterleavedMode KEYWORD2 17 | MLX90640_SetChessMod KEYWORD2 18 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Seeed_Arduino_MLX9064x", 3 | "version": "1.0.0", 4 | "keywords": "MLX90640, IR", 5 | "description": "A arduino library for MLX90640 sensor", 6 | "repository": 7 | { 8 | "type": "git", 9 | "url": "https://github.com/Seeed-Studio/Seeed_Arduino_MLX9064x" 10 | }, 11 | "authors": 12 | [ 13 | { 14 | "name": "Baozhu zuo", 15 | "email": "baozhu.zuo@gmail.com", 16 | "maintainer": true 17 | } 18 | ], 19 | "frameworks": "arduino", 20 | "platforms": "*2" 21 | } 22 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Seeed_Arduino_MLX9064X 2 | version=1.0.0 3 | author=Baozhu Zuo 4 | maintainer=baozhu.zuo@gmail.com 5 | sentence=A MLX9064X library for Arduino. 6 | paragraph=A MLX9064X library for Arduino. 7 | category=Sensors 8 | url=https://github.com/Seeed-Studio/Seeed_Arduino_MLX9064x 9 | architectures=* 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/MLX90640_API.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | @copyright (C) 2017 Melexis N.V. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | */ 17 | #include "MLX9064X_I2C_Driver.h" 18 | #include "MLX90640_API.h" 19 | #include 20 | 21 | void ExtractVDDParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 22 | void ExtractPTATParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 23 | void ExtractGainParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 24 | void ExtractTgcParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 25 | void ExtractResolutionParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 26 | void ExtractKsTaParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 27 | void ExtractKsToParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 28 | void ExtractAlphaParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 29 | void ExtractOffsetParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 30 | void ExtractKtaPixelParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 31 | void ExtractKvPixelParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 32 | void ExtractCPParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 33 | void ExtractCILCParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 34 | int ExtractDeviatingPixels(uint16_t* eeData, paramsMLX90640* mlx90640); 35 | int CheckAdjacentPixels(uint16_t pix1, uint16_t pix2); 36 | int CheckEEPROMValid(uint16_t* eeData); 37 | 38 | 39 | int MLX90640_DumpEE(uint8_t slaveAddr, uint16_t* eeData) { 40 | return MLX9064x_I2CRead(slaveAddr, 0x2400, 832, eeData); 41 | } 42 | 43 | int MLX90640_GetFrameData(uint8_t slaveAddr, uint16_t* frameData) { 44 | uint16_t dataReady = 1; 45 | uint16_t controlRegister1; 46 | uint16_t statusRegister; 47 | int error = 1; 48 | uint8_t cnt = 0; 49 | 50 | dataReady = 0; 51 | while (dataReady == 0) { 52 | error = MLX9064x_I2CRead(slaveAddr, 0x8000, 1, &statusRegister); 53 | if (error != 0) { 54 | return error; 55 | } 56 | dataReady = statusRegister & 0x0008; 57 | } 58 | 59 | while (dataReady != 0 && cnt < 5) { 60 | error = MLX9064x_I2CWrite(slaveAddr, 0x8000, 0x0030); 61 | if (error == -1) { 62 | return error; 63 | } 64 | 65 | error = MLX9064x_I2CRead(slaveAddr, 0x0400, 832, frameData); 66 | if (error != 0) { 67 | return error; 68 | } 69 | 70 | error = MLX9064x_I2CRead(slaveAddr, 0x8000, 1, &statusRegister); 71 | if (error != 0) { 72 | return error; 73 | } 74 | dataReady = statusRegister & 0x0008; 75 | cnt = cnt + 1; 76 | } 77 | 78 | if (cnt > 4) { 79 | return -8; 80 | } 81 | 82 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 83 | frameData[832] = controlRegister1; 84 | frameData[833] = statusRegister & 0x0001; 85 | 86 | if (error != 0) { 87 | return error; 88 | } 89 | 90 | return frameData[833]; 91 | } 92 | 93 | int MLX90640_ExtractParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 94 | int error = CheckEEPROMValid(eeData); 95 | 96 | if (error == 0) { 97 | ExtractVDDParameters(eeData, mlx90640); 98 | ExtractPTATParameters(eeData, mlx90640); 99 | ExtractGainParameters(eeData, mlx90640); 100 | ExtractTgcParameters(eeData, mlx90640); 101 | ExtractResolutionParameters(eeData, mlx90640); 102 | ExtractKsTaParameters(eeData, mlx90640); 103 | ExtractKsToParameters(eeData, mlx90640); 104 | ExtractAlphaParameters(eeData, mlx90640); 105 | ExtractOffsetParameters(eeData, mlx90640); 106 | ExtractKtaPixelParameters(eeData, mlx90640); 107 | ExtractKvPixelParameters(eeData, mlx90640); 108 | ExtractCPParameters(eeData, mlx90640); 109 | ExtractCILCParameters(eeData, mlx90640); 110 | error = ExtractDeviatingPixels(eeData, mlx90640); 111 | } 112 | 113 | return error; 114 | 115 | } 116 | 117 | //------------------------------------------------------------------------------ 118 | 119 | int MLX90640_SetResolution(uint8_t slaveAddr, uint8_t resolution) { 120 | uint16_t controlRegister1; 121 | int value; 122 | int error; 123 | 124 | value = (resolution & 0x03) << 10; 125 | 126 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 127 | 128 | if (error == 0) { 129 | value = (controlRegister1 & 0xF3FF) | value; 130 | error = MLX9064x_I2CWrite(slaveAddr, 0x800D, value); 131 | } 132 | 133 | return error; 134 | } 135 | 136 | //------------------------------------------------------------------------------ 137 | 138 | int MLX90640_GetCurResolution(uint8_t slaveAddr) { 139 | uint16_t controlRegister1; 140 | int resolutionRAM; 141 | int error; 142 | 143 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 144 | if (error != 0) { 145 | return error; 146 | } 147 | resolutionRAM = (controlRegister1 & 0x0C00) >> 10; 148 | 149 | return resolutionRAM; 150 | } 151 | 152 | //------------------------------------------------------------------------------ 153 | 154 | int MLX90640_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate) { 155 | uint16_t controlRegister1; 156 | int value; 157 | int error; 158 | 159 | value = (refreshRate & 0x07) << 7; 160 | 161 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 162 | if (error == 0) { 163 | value = (controlRegister1 & 0xFC7F) | value; 164 | error = MLX9064x_I2CWrite(slaveAddr, 0x800D, value); 165 | } 166 | 167 | return error; 168 | } 169 | 170 | //------------------------------------------------------------------------------ 171 | 172 | int MLX90640_GetRefreshRate(uint8_t slaveAddr) { 173 | uint16_t controlRegister1; 174 | int refreshRate; 175 | int error; 176 | 177 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 178 | if (error != 0) { 179 | return error; 180 | } 181 | refreshRate = (controlRegister1 & 0x0380) >> 7; 182 | 183 | return refreshRate; 184 | } 185 | 186 | //------------------------------------------------------------------------------ 187 | 188 | int MLX90640_SetInterleavedMode(uint8_t slaveAddr) { 189 | uint16_t controlRegister1; 190 | int value; 191 | int error; 192 | 193 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 194 | 195 | if (error == 0) { 196 | value = (controlRegister1 & 0xEFFF); 197 | error = MLX9064x_I2CWrite(slaveAddr, 0x800D, value); 198 | } 199 | 200 | return error; 201 | } 202 | 203 | //------------------------------------------------------------------------------ 204 | 205 | int MLX90640_SetChessMode(uint8_t slaveAddr) { 206 | uint16_t controlRegister1; 207 | int value; 208 | int error; 209 | 210 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 211 | 212 | if (error == 0) { 213 | value = (controlRegister1 | 0x1000); 214 | error = MLX9064x_I2CWrite(slaveAddr, 0x800D, value); 215 | } 216 | 217 | return error; 218 | } 219 | 220 | //------------------------------------------------------------------------------ 221 | 222 | int MLX90640_GetCurMode(uint8_t slaveAddr) { 223 | uint16_t controlRegister1; 224 | int modeRAM; 225 | int error; 226 | 227 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 228 | if (error != 0) { 229 | return error; 230 | } 231 | modeRAM = (controlRegister1 & 0x1000) >> 12; 232 | 233 | return modeRAM; 234 | } 235 | 236 | //------------------------------------------------------------------------------ 237 | 238 | void MLX90640_CalculateTo(uint16_t* frameData, const paramsMLX90640* params, float emissivity, float tr, 239 | float* result) { 240 | float vdd; 241 | float ta; 242 | float ta4; 243 | float tr4; 244 | float taTr; 245 | float gain; 246 | float irDataCP[2]; 247 | float irData; 248 | float alphaCompensated; 249 | uint8_t mode; 250 | int8_t ilPattern; 251 | int8_t chessPattern; 252 | int8_t pattern; 253 | int8_t conversionPattern; 254 | float Sx; 255 | float To; 256 | float alphaCorrR[4]; 257 | int8_t range; 258 | uint16_t subPage; 259 | 260 | subPage = frameData[833]; 261 | vdd = MLX90640_GetVdd(frameData, params); 262 | ta = MLX90640_GetTa(frameData, params); 263 | ta4 = pow((ta + 273.15), (double)4); 264 | tr4 = pow((tr + 273.15), (double)4); 265 | taTr = tr4 - (tr4 - ta4) / emissivity; 266 | 267 | alphaCorrR[0] = 1 / (1 + params->ksTo[0] * 40); 268 | alphaCorrR[1] = 1 ; 269 | alphaCorrR[2] = (1 + params->ksTo[2] * params->ct[2]); 270 | alphaCorrR[3] = alphaCorrR[2] * (1 + params->ksTo[3] * (params->ct[3] - params->ct[2])); 271 | 272 | //------------------------- Gain calculation ----------------------------------- 273 | gain = frameData[778]; 274 | if (gain > 32767) { 275 | gain = gain - 65536; 276 | } 277 | 278 | gain = params->gainEE / gain; 279 | 280 | //------------------------- To calculation ------------------------------------- 281 | mode = (frameData[832] & 0x1000) >> 5; 282 | 283 | irDataCP[0] = frameData[776]; 284 | irDataCP[1] = frameData[808]; 285 | for (int i = 0; i < 2; i++) { 286 | if (irDataCP[i] > 32767) { 287 | irDataCP[i] = irDataCP[i] - 65536; 288 | } 289 | irDataCP[i] = irDataCP[i] * gain; 290 | } 291 | irDataCP[0] = irDataCP[0] - params->cpOffset[0] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); 292 | if (mode == params->calibrationModeEE) { 293 | irDataCP[1] = irDataCP[1] - params->cpOffset[1] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); 294 | } else { 295 | irDataCP[1] = irDataCP[1] - (params->cpOffset[1] + params->ilChessC[0]) * (1 + params->cpKta * (ta - 25)) * 296 | (1 + params->cpKv * (vdd - 3.3)); 297 | } 298 | 299 | for (int pixelNumber = 0; pixelNumber < 768; pixelNumber++) { 300 | ilPattern = pixelNumber / 32 - (pixelNumber / 64) * 2; 301 | chessPattern = ilPattern ^ (pixelNumber - (pixelNumber / 2) * 2); 302 | conversionPattern = ((pixelNumber + 2) / 4 - (pixelNumber + 3) / 4 + (pixelNumber + 1) / 4 - pixelNumber / 4) * 303 | (1 - 2 * ilPattern); 304 | 305 | if (mode == 0) { 306 | pattern = ilPattern; 307 | } else { 308 | pattern = chessPattern; 309 | } 310 | 311 | if (pattern == frameData[833]) { 312 | irData = frameData[pixelNumber]; 313 | if (irData > 32767) { 314 | irData = irData - 65536; 315 | } 316 | irData = irData * gain; 317 | 318 | irData = irData - params->offset[pixelNumber] * (1 + params->kta[pixelNumber] * (ta - 25)) * 319 | (1 + params->kv[pixelNumber] * (vdd - 3.3)); 320 | if (mode != params->calibrationModeEE) { 321 | irData = irData + params->ilChessC[2] * (2 * ilPattern - 1) - params->ilChessC[1] * conversionPattern; 322 | } 323 | 324 | irData = irData / emissivity; 325 | 326 | irData = irData - params->tgc * irDataCP[subPage]; 327 | 328 | alphaCompensated = (params->alpha[pixelNumber] - params->tgc * params->cpAlpha[subPage]) * (1 + params->KsTa * 329 | (ta - 25)); 330 | 331 | Sx = pow((double)alphaCompensated, (double)3) * (irData + alphaCompensated * taTr); 332 | Sx = sqrt(sqrt(Sx)) * params->ksTo[1]; 333 | 334 | To = sqrt(sqrt(irData / (alphaCompensated * (1 - params->ksTo[1] * 273.15) + Sx) + taTr)) - 273.15; 335 | 336 | if (To < params->ct[1]) { 337 | range = 0; 338 | } else if (To < params->ct[2]) { 339 | range = 1; 340 | } else if (To < params->ct[3]) { 341 | range = 2; 342 | } else { 343 | range = 3; 344 | } 345 | 346 | To = sqrt(sqrt(irData / (alphaCompensated * alphaCorrR[range] * (1 + params->ksTo[range] * 347 | (To - params->ct[range]))) + taTr)) - 273.15; 348 | 349 | result[pixelNumber] = To; 350 | } 351 | } 352 | } 353 | 354 | //------------------------------------------------------------------------------ 355 | 356 | void MLX90640_GetImage(uint16_t* frameData, const paramsMLX90640* params, float* result) { 357 | float vdd; 358 | float ta; 359 | float gain; 360 | float irDataCP[2]; 361 | float irData; 362 | float alphaCompensated; 363 | uint8_t mode; 364 | int8_t ilPattern; 365 | int8_t chessPattern; 366 | int8_t pattern; 367 | int8_t conversionPattern; 368 | float image; 369 | uint16_t subPage; 370 | 371 | subPage = frameData[833]; 372 | vdd = MLX90640_GetVdd(frameData, params); 373 | ta = MLX90640_GetTa(frameData, params); 374 | 375 | //------------------------- Gain calculation ----------------------------------- 376 | gain = frameData[778]; 377 | if (gain > 32767) { 378 | gain = gain - 65536; 379 | } 380 | 381 | gain = params->gainEE / gain; 382 | 383 | //------------------------- Image calculation ------------------------------------- 384 | mode = (frameData[832] & 0x1000) >> 5; 385 | 386 | irDataCP[0] = frameData[776]; 387 | irDataCP[1] = frameData[808]; 388 | for (int i = 0; i < 2; i++) { 389 | if (irDataCP[i] > 32767) { 390 | irDataCP[i] = irDataCP[i] - 65536; 391 | } 392 | irDataCP[i] = irDataCP[i] * gain; 393 | } 394 | irDataCP[0] = irDataCP[0] - params->cpOffset[0] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); 395 | if (mode == params->calibrationModeEE) { 396 | irDataCP[1] = irDataCP[1] - params->cpOffset[1] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); 397 | } else { 398 | irDataCP[1] = irDataCP[1] - (params->cpOffset[1] + params->ilChessC[0]) * (1 + params->cpKta * (ta - 25)) * 399 | (1 + params->cpKv * (vdd - 3.3)); 400 | } 401 | 402 | for (int pixelNumber = 0; pixelNumber < 768; pixelNumber++) { 403 | ilPattern = pixelNumber / 32 - (pixelNumber / 64) * 2; 404 | chessPattern = ilPattern ^ (pixelNumber - (pixelNumber / 2) * 2); 405 | conversionPattern = ((pixelNumber + 2) / 4 - (pixelNumber + 3) / 4 + (pixelNumber + 1) / 4 - pixelNumber / 4) * 406 | (1 - 2 * ilPattern); 407 | 408 | if (mode == 0) { 409 | pattern = ilPattern; 410 | } else { 411 | pattern = chessPattern; 412 | } 413 | 414 | if (pattern == frameData[833]) { 415 | irData = frameData[pixelNumber]; 416 | if (irData > 32767) { 417 | irData = irData - 65536; 418 | } 419 | irData = irData * gain; 420 | 421 | irData = irData - params->offset[pixelNumber] * (1 + params->kta[pixelNumber] * (ta - 25)) * 422 | (1 + params->kv[pixelNumber] * (vdd - 3.3)); 423 | if (mode != params->calibrationModeEE) { 424 | irData = irData + params->ilChessC[2] * (2 * ilPattern - 1) - params->ilChessC[1] * conversionPattern; 425 | } 426 | 427 | irData = irData - params->tgc * irDataCP[subPage]; 428 | 429 | alphaCompensated = (params->alpha[pixelNumber] - params->tgc * params->cpAlpha[subPage]) * (1 + params->KsTa * 430 | (ta - 25)); 431 | 432 | image = irData / alphaCompensated; 433 | 434 | result[pixelNumber] = image; 435 | } 436 | } 437 | } 438 | 439 | //------------------------------------------------------------------------------ 440 | 441 | float MLX90640_GetVdd(uint16_t* frameData, const paramsMLX90640* params) { 442 | float vdd; 443 | float resolutionCorrection; 444 | 445 | int resolutionRAM; 446 | 447 | vdd = frameData[810]; 448 | if (vdd > 32767) { 449 | vdd = vdd - 65536; 450 | } 451 | resolutionRAM = (frameData[832] & 0x0C00) >> 10; 452 | resolutionCorrection = pow(2, (double)params->resolutionEE) / pow(2, (double)resolutionRAM); 453 | vdd = (resolutionCorrection * vdd - params->vdd25) / params->kVdd + 3.3; 454 | 455 | return vdd; 456 | } 457 | 458 | //------------------------------------------------------------------------------ 459 | 460 | float MLX90640_GetTa(uint16_t* frameData, const paramsMLX90640* params) { 461 | float ptat; 462 | float ptatArt; 463 | float vdd; 464 | float ta; 465 | 466 | vdd = MLX90640_GetVdd(frameData, params); 467 | 468 | ptat = frameData[800]; 469 | if (ptat > 32767) { 470 | ptat = ptat - 65536; 471 | } 472 | 473 | ptatArt = frameData[768]; 474 | if (ptatArt > 32767) { 475 | ptatArt = ptatArt - 65536; 476 | } 477 | ptatArt = (ptat / (ptat * params->alphaPTAT + ptatArt)) * pow(2, (double)18); 478 | 479 | ta = (ptatArt / (1 + params->KvPTAT * (vdd - 3.3)) - params->vPTAT25); 480 | ta = ta / params->KtPTAT + 25; 481 | 482 | return ta; 483 | } 484 | 485 | //------------------------------------------------------------------------------ 486 | 487 | int MLX90640_GetSubPageNumber(uint16_t* frameData) { 488 | return frameData[833]; 489 | 490 | } 491 | 492 | //------------------------------------------------------------------------------ 493 | 494 | void ExtractVDDParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 495 | int16_t kVdd; 496 | int16_t vdd25; 497 | 498 | kVdd = eeData[51]; 499 | 500 | kVdd = (eeData[51] & 0xFF00) >> 8; 501 | if (kVdd > 127) { 502 | kVdd = kVdd - 256; 503 | } 504 | kVdd = 32 * kVdd; 505 | vdd25 = eeData[51] & 0x00FF; 506 | vdd25 = ((vdd25 - 256) << 5) - 8192; 507 | 508 | mlx90640->kVdd = kVdd; 509 | mlx90640->vdd25 = vdd25; 510 | } 511 | 512 | //------------------------------------------------------------------------------ 513 | 514 | void ExtractPTATParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 515 | float KvPTAT; 516 | float KtPTAT; 517 | int16_t vPTAT25; 518 | float alphaPTAT; 519 | 520 | KvPTAT = (eeData[50] & 0xFC00) >> 10; 521 | if (KvPTAT > 31) { 522 | KvPTAT = KvPTAT - 64; 523 | } 524 | KvPTAT = KvPTAT / 4096; 525 | 526 | KtPTAT = eeData[50] & 0x03FF; 527 | if (KtPTAT > 511) { 528 | KtPTAT = KtPTAT - 1024; 529 | } 530 | KtPTAT = KtPTAT / 8; 531 | 532 | vPTAT25 = eeData[49]; 533 | 534 | alphaPTAT = (eeData[16] & 0xF000) / pow(2, (double)14) + 8.0f; 535 | 536 | mlx90640->KvPTAT = KvPTAT; 537 | mlx90640->KtPTAT = KtPTAT; 538 | mlx90640->vPTAT25 = vPTAT25; 539 | mlx90640->alphaPTAT = alphaPTAT; 540 | } 541 | 542 | //------------------------------------------------------------------------------ 543 | 544 | void ExtractGainParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 545 | int16_t gainEE; 546 | 547 | gainEE = eeData[48]; 548 | if (gainEE > 32767) { 549 | gainEE = gainEE - 65536; 550 | } 551 | 552 | mlx90640->gainEE = gainEE; 553 | } 554 | 555 | //------------------------------------------------------------------------------ 556 | 557 | void ExtractTgcParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 558 | float tgc; 559 | tgc = eeData[60] & 0x00FF; 560 | if (tgc > 127) { 561 | tgc = tgc - 256; 562 | } 563 | tgc = tgc / 32.0f; 564 | 565 | mlx90640->tgc = tgc; 566 | } 567 | 568 | //------------------------------------------------------------------------------ 569 | 570 | void ExtractResolutionParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 571 | uint8_t resolutionEE; 572 | resolutionEE = (eeData[56] & 0x3000) >> 12; 573 | 574 | mlx90640->resolutionEE = resolutionEE; 575 | } 576 | 577 | //------------------------------------------------------------------------------ 578 | 579 | void ExtractKsTaParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 580 | float KsTa; 581 | KsTa = (eeData[60] & 0xFF00) >> 8; 582 | if (KsTa > 127) { 583 | KsTa = KsTa - 256; 584 | } 585 | KsTa = KsTa / 8192.0f; 586 | 587 | mlx90640->KsTa = KsTa; 588 | } 589 | 590 | //------------------------------------------------------------------------------ 591 | 592 | void ExtractKsToParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 593 | int KsToScale; 594 | int8_t step; 595 | 596 | step = ((eeData[63] & 0x3000) >> 12) * 10; 597 | 598 | mlx90640->ct[0] = -40; 599 | mlx90640->ct[1] = 0; 600 | mlx90640->ct[2] = (eeData[63] & 0x00F0) >> 4; 601 | mlx90640->ct[3] = (eeData[63] & 0x0F00) >> 8; 602 | 603 | mlx90640->ct[2] = mlx90640->ct[2] * step; 604 | mlx90640->ct[3] = mlx90640->ct[2] + mlx90640->ct[3] * step; 605 | 606 | KsToScale = (eeData[63] & 0x000F) + 8; 607 | KsToScale = 1 << KsToScale; 608 | 609 | mlx90640->ksTo[0] = eeData[61] & 0x00FF; 610 | mlx90640->ksTo[1] = (eeData[61] & 0xFF00) >> 8; 611 | mlx90640->ksTo[2] = eeData[62] & 0x00FF; 612 | mlx90640->ksTo[3] = (eeData[62] & 0xFF00) >> 8; 613 | 614 | 615 | for (int i = 0; i < 4; i++) { 616 | if (mlx90640->ksTo[i] > 127) { 617 | mlx90640->ksTo[i] = mlx90640->ksTo[i] - 256; 618 | } 619 | mlx90640->ksTo[i] = mlx90640->ksTo[i] / KsToScale; 620 | } 621 | } 622 | 623 | //------------------------------------------------------------------------------ 624 | 625 | void ExtractAlphaParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 626 | int accRow[24]; 627 | int accColumn[32]; 628 | int p = 0; 629 | int alphaRef; 630 | uint8_t alphaScale; 631 | uint8_t accRowScale; 632 | uint8_t accColumnScale; 633 | uint8_t accRemScale; 634 | 635 | 636 | accRemScale = eeData[32] & 0x000F; 637 | accColumnScale = (eeData[32] & 0x00F0) >> 4; 638 | accRowScale = (eeData[32] & 0x0F00) >> 8; 639 | alphaScale = ((eeData[32] & 0xF000) >> 12) + 30; 640 | alphaRef = eeData[33]; 641 | 642 | for (int i = 0; i < 6; i++) { 643 | p = i * 4; 644 | accRow[p + 0] = (eeData[34 + i] & 0x000F); 645 | accRow[p + 1] = (eeData[34 + i] & 0x00F0) >> 4; 646 | accRow[p + 2] = (eeData[34 + i] & 0x0F00) >> 8; 647 | accRow[p + 3] = (eeData[34 + i] & 0xF000) >> 12; 648 | } 649 | 650 | for (int i = 0; i < 24; i++) { 651 | if (accRow[i] > 7) { 652 | accRow[i] = accRow[i] - 16; 653 | } 654 | } 655 | 656 | for (int i = 0; i < 8; i++) { 657 | p = i * 4; 658 | accColumn[p + 0] = (eeData[40 + i] & 0x000F); 659 | accColumn[p + 1] = (eeData[40 + i] & 0x00F0) >> 4; 660 | accColumn[p + 2] = (eeData[40 + i] & 0x0F00) >> 8; 661 | accColumn[p + 3] = (eeData[40 + i] & 0xF000) >> 12; 662 | } 663 | 664 | for (int i = 0; i < 32; i ++) { 665 | if (accColumn[i] > 7) { 666 | accColumn[i] = accColumn[i] - 16; 667 | } 668 | } 669 | 670 | for (int i = 0; i < 24; i++) { 671 | for (int j = 0; j < 32; j ++) { 672 | p = 32 * i + j; 673 | mlx90640->alpha[p] = (eeData[64 + p] & 0x03F0) >> 4; 674 | if (mlx90640->alpha[p] > 31) { 675 | mlx90640->alpha[p] = mlx90640->alpha[p] - 64; 676 | } 677 | mlx90640->alpha[p] = mlx90640->alpha[p] * (1 << accRemScale); 678 | mlx90640->alpha[p] = (alphaRef + (accRow[i] << accRowScale) + (accColumn[j] << accColumnScale) + mlx90640->alpha[p]); 679 | mlx90640->alpha[p] = mlx90640->alpha[p] / pow(2, (double)alphaScale); 680 | } 681 | } 682 | } 683 | 684 | //------------------------------------------------------------------------------ 685 | 686 | void ExtractOffsetParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 687 | int occRow[24]; 688 | int occColumn[32]; 689 | int p = 0; 690 | int16_t offsetRef; 691 | uint8_t occRowScale; 692 | uint8_t occColumnScale; 693 | uint8_t occRemScale; 694 | 695 | 696 | occRemScale = (eeData[16] & 0x000F); 697 | occColumnScale = (eeData[16] & 0x00F0) >> 4; 698 | occRowScale = (eeData[16] & 0x0F00) >> 8; 699 | offsetRef = eeData[17]; 700 | if (offsetRef > 32767) { 701 | offsetRef = offsetRef - 65536; 702 | } 703 | 704 | for (int i = 0; i < 6; i++) { 705 | p = i * 4; 706 | occRow[p + 0] = (eeData[18 + i] & 0x000F); 707 | occRow[p + 1] = (eeData[18 + i] & 0x00F0) >> 4; 708 | occRow[p + 2] = (eeData[18 + i] & 0x0F00) >> 8; 709 | occRow[p + 3] = (eeData[18 + i] & 0xF000) >> 12; 710 | } 711 | 712 | for (int i = 0; i < 24; i++) { 713 | if (occRow[i] > 7) { 714 | occRow[i] = occRow[i] - 16; 715 | } 716 | } 717 | 718 | for (int i = 0; i < 8; i++) { 719 | p = i * 4; 720 | occColumn[p + 0] = (eeData[24 + i] & 0x000F); 721 | occColumn[p + 1] = (eeData[24 + i] & 0x00F0) >> 4; 722 | occColumn[p + 2] = (eeData[24 + i] & 0x0F00) >> 8; 723 | occColumn[p + 3] = (eeData[24 + i] & 0xF000) >> 12; 724 | } 725 | 726 | for (int i = 0; i < 32; i ++) { 727 | if (occColumn[i] > 7) { 728 | occColumn[i] = occColumn[i] - 16; 729 | } 730 | } 731 | 732 | for (int i = 0; i < 24; i++) { 733 | for (int j = 0; j < 32; j ++) { 734 | p = 32 * i + j; 735 | mlx90640->offset[p] = (eeData[64 + p] & 0xFC00) >> 10; 736 | if (mlx90640->offset[p] > 31) { 737 | mlx90640->offset[p] = mlx90640->offset[p] - 64; 738 | } 739 | mlx90640->offset[p] = mlx90640->offset[p] * (1 << occRemScale); 740 | mlx90640->offset[p] = (offsetRef + (occRow[i] << occRowScale) + (occColumn[j] << occColumnScale) + mlx90640->offset[p]); 741 | } 742 | } 743 | } 744 | 745 | //------------------------------------------------------------------------------ 746 | 747 | void ExtractKtaPixelParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 748 | int p = 0; 749 | int8_t KtaRC[4]; 750 | int8_t KtaRoCo; 751 | int8_t KtaRoCe; 752 | int8_t KtaReCo; 753 | int8_t KtaReCe; 754 | uint8_t ktaScale1; 755 | uint8_t ktaScale2; 756 | uint8_t split; 757 | 758 | KtaRoCo = (eeData[54] & 0xFF00) >> 8; 759 | if (KtaRoCo > 127) { 760 | KtaRoCo = KtaRoCo - 256; 761 | } 762 | KtaRC[0] = KtaRoCo; 763 | 764 | KtaReCo = (eeData[54] & 0x00FF); 765 | if (KtaReCo > 127) { 766 | KtaReCo = KtaReCo - 256; 767 | } 768 | KtaRC[2] = KtaReCo; 769 | 770 | KtaRoCe = (eeData[55] & 0xFF00) >> 8; 771 | if (KtaRoCe > 127) { 772 | KtaRoCe = KtaRoCe - 256; 773 | } 774 | KtaRC[1] = KtaRoCe; 775 | 776 | KtaReCe = (eeData[55] & 0x00FF); 777 | if (KtaReCe > 127) { 778 | KtaReCe = KtaReCe - 256; 779 | } 780 | KtaRC[3] = KtaReCe; 781 | 782 | ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8; 783 | ktaScale2 = (eeData[56] & 0x000F); 784 | 785 | for (int i = 0; i < 24; i++) { 786 | for (int j = 0; j < 32; j ++) { 787 | p = 32 * i + j; 788 | split = 2 * (p / 32 - (p / 64) * 2) + p % 2; 789 | mlx90640->kta[p] = (eeData[64 + p] & 0x000E) >> 1; 790 | if (mlx90640->kta[p] > 3) { 791 | mlx90640->kta[p] = mlx90640->kta[p] - 8; 792 | } 793 | mlx90640->kta[p] = mlx90640->kta[p] * (1 << ktaScale2); 794 | mlx90640->kta[p] = KtaRC[split] + mlx90640->kta[p]; 795 | mlx90640->kta[p] = mlx90640->kta[p] / pow(2, (double)ktaScale1); 796 | } 797 | } 798 | } 799 | 800 | //------------------------------------------------------------------------------ 801 | 802 | void ExtractKvPixelParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 803 | int p = 0; 804 | int8_t KvT[4]; 805 | int8_t KvRoCo; 806 | int8_t KvRoCe; 807 | int8_t KvReCo; 808 | int8_t KvReCe; 809 | uint8_t kvScale; 810 | uint8_t split; 811 | 812 | KvRoCo = (eeData[52] & 0xF000) >> 12; 813 | if (KvRoCo > 7) { 814 | KvRoCo = KvRoCo - 16; 815 | } 816 | KvT[0] = KvRoCo; 817 | 818 | KvReCo = (eeData[52] & 0x0F00) >> 8; 819 | if (KvReCo > 7) { 820 | KvReCo = KvReCo - 16; 821 | } 822 | KvT[2] = KvReCo; 823 | 824 | KvRoCe = (eeData[52] & 0x00F0) >> 4; 825 | if (KvRoCe > 7) { 826 | KvRoCe = KvRoCe - 16; 827 | } 828 | KvT[1] = KvRoCe; 829 | 830 | KvReCe = (eeData[52] & 0x000F); 831 | if (KvReCe > 7) { 832 | KvReCe = KvReCe - 16; 833 | } 834 | KvT[3] = KvReCe; 835 | 836 | kvScale = (eeData[56] & 0x0F00) >> 8; 837 | 838 | 839 | for (int i = 0; i < 24; i++) { 840 | for (int j = 0; j < 32; j ++) { 841 | p = 32 * i + j; 842 | split = 2 * (p / 32 - (p / 64) * 2) + p % 2; 843 | mlx90640->kv[p] = KvT[split]; 844 | mlx90640->kv[p] = mlx90640->kv[p] / pow(2, (double)kvScale); 845 | } 846 | } 847 | } 848 | 849 | //------------------------------------------------------------------------------ 850 | 851 | void ExtractCPParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 852 | float alphaSP[2]; 853 | int16_t offsetSP[2]; 854 | float cpKv; 855 | float cpKta; 856 | uint8_t alphaScale; 857 | uint8_t ktaScale1; 858 | uint8_t kvScale; 859 | 860 | alphaScale = ((eeData[32] & 0xF000) >> 12) + 27; 861 | 862 | offsetSP[0] = (eeData[58] & 0x03FF); 863 | if (offsetSP[0] > 511) { 864 | offsetSP[0] = offsetSP[0] - 1024; 865 | } 866 | 867 | offsetSP[1] = (eeData[58] & 0xFC00) >> 10; 868 | if (offsetSP[1] > 31) { 869 | offsetSP[1] = offsetSP[1] - 64; 870 | } 871 | offsetSP[1] = offsetSP[1] + offsetSP[0]; 872 | 873 | alphaSP[0] = (eeData[57] & 0x03FF); 874 | if (alphaSP[0] > 511) { 875 | alphaSP[0] = alphaSP[0] - 1024; 876 | } 877 | alphaSP[0] = alphaSP[0] / pow(2, (double)alphaScale); 878 | 879 | alphaSP[1] = (eeData[57] & 0xFC00) >> 10; 880 | if (alphaSP[1] > 31) { 881 | alphaSP[1] = alphaSP[1] - 64; 882 | } 883 | alphaSP[1] = (1 + alphaSP[1] / 128) * alphaSP[0]; 884 | 885 | cpKta = (eeData[59] & 0x00FF); 886 | if (cpKta > 127) { 887 | cpKta = cpKta - 256; 888 | } 889 | ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8; 890 | mlx90640->cpKta = cpKta / pow(2, (double)ktaScale1); 891 | 892 | cpKv = (eeData[59] & 0xFF00) >> 8; 893 | if (cpKv > 127) { 894 | cpKv = cpKv - 256; 895 | } 896 | kvScale = (eeData[56] & 0x0F00) >> 8; 897 | mlx90640->cpKv = cpKv / pow(2, (double)kvScale); 898 | 899 | mlx90640->cpAlpha[0] = alphaSP[0]; 900 | mlx90640->cpAlpha[1] = alphaSP[1]; 901 | mlx90640->cpOffset[0] = offsetSP[0]; 902 | mlx90640->cpOffset[1] = offsetSP[1]; 903 | } 904 | 905 | //------------------------------------------------------------------------------ 906 | 907 | void ExtractCILCParameters(uint16_t* eeData, paramsMLX90640* mlx90640) { 908 | float ilChessC[3]; 909 | uint8_t calibrationModeEE; 910 | 911 | calibrationModeEE = (eeData[10] & 0x0800) >> 4; 912 | calibrationModeEE = calibrationModeEE ^ 0x80; 913 | 914 | ilChessC[0] = (eeData[53] & 0x003F); 915 | if (ilChessC[0] > 31) { 916 | ilChessC[0] = ilChessC[0] - 64; 917 | } 918 | ilChessC[0] = ilChessC[0] / 16.0f; 919 | 920 | ilChessC[1] = (eeData[53] & 0x07C0) >> 6; 921 | if (ilChessC[1] > 15) { 922 | ilChessC[1] = ilChessC[1] - 32; 923 | } 924 | ilChessC[1] = ilChessC[1] / 2.0f; 925 | 926 | ilChessC[2] = (eeData[53] & 0xF800) >> 11; 927 | if (ilChessC[2] > 15) { 928 | ilChessC[2] = ilChessC[2] - 32; 929 | } 930 | ilChessC[2] = ilChessC[2] / 8.0f; 931 | 932 | mlx90640->calibrationModeEE = calibrationModeEE; 933 | mlx90640->ilChessC[0] = ilChessC[0]; 934 | mlx90640->ilChessC[1] = ilChessC[1]; 935 | mlx90640->ilChessC[2] = ilChessC[2]; 936 | } 937 | 938 | //------------------------------------------------------------------------------ 939 | 940 | int ExtractDeviatingPixels(uint16_t* eeData, paramsMLX90640* mlx90640) { 941 | uint16_t pixCnt = 0; 942 | uint16_t brokenPixCnt = 0; 943 | uint16_t outlierPixCnt = 0; 944 | int warn = 0; 945 | int i; 946 | 947 | for (pixCnt = 0; pixCnt < 5; pixCnt++) { 948 | mlx90640->brokenPixels[pixCnt] = 0xFFFF; 949 | mlx90640->outlierPixels[pixCnt] = 0xFFFF; 950 | } 951 | 952 | pixCnt = 0; 953 | while (pixCnt < 768 && brokenPixCnt < 5 && outlierPixCnt < 5) { 954 | if (eeData[pixCnt + 64] == 0) { 955 | mlx90640->brokenPixels[brokenPixCnt] = pixCnt; 956 | brokenPixCnt = brokenPixCnt + 1; 957 | } else if ((eeData[pixCnt + 64] & 0x0001) != 0) { 958 | mlx90640->outlierPixels[outlierPixCnt] = pixCnt; 959 | outlierPixCnt = outlierPixCnt + 1; 960 | } 961 | 962 | pixCnt = pixCnt + 1; 963 | 964 | } 965 | 966 | if (brokenPixCnt > 4) { 967 | warn = -3; 968 | } else if (outlierPixCnt > 4) { 969 | warn = -4; 970 | } else if ((brokenPixCnt + outlierPixCnt) > 4) { 971 | warn = -5; 972 | } else { 973 | for (pixCnt = 0; pixCnt < brokenPixCnt; pixCnt++) { 974 | for (i = pixCnt + 1; i < brokenPixCnt; i++) { 975 | warn = CheckAdjacentPixels(mlx90640->brokenPixels[pixCnt], mlx90640->brokenPixels[i]); 976 | if (warn != 0) { 977 | return warn; 978 | } 979 | } 980 | } 981 | 982 | for (pixCnt = 0; pixCnt < outlierPixCnt; pixCnt++) { 983 | for (i = pixCnt + 1; i < outlierPixCnt; i++) { 984 | warn = CheckAdjacentPixels(mlx90640->outlierPixels[pixCnt], mlx90640->outlierPixels[i]); 985 | if (warn != 0) { 986 | return warn; 987 | } 988 | } 989 | } 990 | 991 | for (pixCnt = 0; pixCnt < brokenPixCnt; pixCnt++) { 992 | for (i = 0; i < outlierPixCnt; i++) { 993 | warn = CheckAdjacentPixels(mlx90640->brokenPixels[pixCnt], mlx90640->outlierPixels[i]); 994 | if (warn != 0) { 995 | return warn; 996 | } 997 | } 998 | } 999 | 1000 | } 1001 | 1002 | 1003 | return warn; 1004 | 1005 | } 1006 | 1007 | //------------------------------------------------------------------------------ 1008 | 1009 | int CheckAdjacentPixels(uint16_t pix1, uint16_t pix2) { 1010 | int pixPosDif; 1011 | 1012 | pixPosDif = pix1 - pix2; 1013 | if (pixPosDif > -34 && pixPosDif < -30) { 1014 | return -6; 1015 | } 1016 | if (pixPosDif > -2 && pixPosDif < 2) { 1017 | return -6; 1018 | } 1019 | if (pixPosDif > 30 && pixPosDif < 34) { 1020 | return -6; 1021 | } 1022 | 1023 | return 0; 1024 | } 1025 | 1026 | //------------------------------------------------------------------------------ 1027 | 1028 | int CheckEEPROMValid(uint16_t* eeData) { 1029 | int deviceSelect; 1030 | deviceSelect = eeData[10] & 0x0040; 1031 | if (deviceSelect == 0) { 1032 | return 0; 1033 | } 1034 | 1035 | return -7; 1036 | } 1037 | -------------------------------------------------------------------------------- /src/MLX90640_API.h: -------------------------------------------------------------------------------- 1 | /** 2 | @copyright (C) 2017 Melexis N.V. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | */ 17 | #ifndef _MLX640_API_H_ 18 | #define _MLX640_API_H_ 19 | 20 | typedef struct { 21 | int16_t kVdd; 22 | int16_t vdd25; 23 | float KvPTAT; 24 | float KtPTAT; 25 | uint16_t vPTAT25; 26 | float alphaPTAT; 27 | int16_t gainEE; 28 | float tgc; 29 | float cpKv; 30 | float cpKta; 31 | uint8_t resolutionEE; 32 | uint8_t calibrationModeEE; 33 | float KsTa; 34 | float ksTo[4]; 35 | int16_t ct[4]; 36 | float alpha[768]; 37 | int16_t offset[768]; 38 | float kta[768]; 39 | float kv[768]; 40 | float cpAlpha[2]; 41 | int16_t cpOffset[2]; 42 | float ilChessC[3]; 43 | uint16_t brokenPixels[5]; 44 | uint16_t outlierPixels[5]; 45 | } paramsMLX90640; 46 | 47 | int MLX90640_DumpEE(uint8_t slaveAddr, uint16_t* eeData); 48 | int MLX90640_GetFrameData(uint8_t slaveAddr, uint16_t* frameData); 49 | int MLX90640_ExtractParameters(uint16_t* eeData, paramsMLX90640* mlx90640); 50 | float MLX90640_GetVdd(uint16_t* frameData, const paramsMLX90640* params); 51 | float MLX90640_GetTa(uint16_t* frameData, const paramsMLX90640* params); 52 | void MLX90640_GetImage(uint16_t* frameData, const paramsMLX90640* params, float* result); 53 | void MLX90640_CalculateTo(uint16_t* frameData, const paramsMLX90640* params, float emissivity, float tr, float* result); 54 | int MLX90640_SetResolution(uint8_t slaveAddr, uint8_t resolution); 55 | int MLX90640_GetCurResolution(uint8_t slaveAddr); 56 | int MLX90640_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate); 57 | int MLX90640_GetRefreshRate(uint8_t slaveAddr); 58 | int MLX90640_GetSubPageNumber(uint16_t* frameData); 59 | int MLX90640_GetCurMode(uint8_t slaveAddr); 60 | int MLX90640_SetInterleavedMode(uint8_t slaveAddr); 61 | int MLX90640_SetChessMode(uint8_t slaveAddr); 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /src/MLX90641_API.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright (C) 2017 Melexis N.V. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | #include 18 | #include 19 | #include 20 | 21 | void MLX90641_ExtractVDDParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 22 | void MLX90641_ExtractPTATParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 23 | void MLX90641_ExtractGainParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 24 | void MLX90641_ExtractTgcParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 25 | void MLX90641_ExtractEmissivityParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 26 | void MLX90641_ExtractResolutionParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 27 | void MLX90641_ExtractKsTaParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 28 | void MLX90641_ExtractKsToParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 29 | void MLX90641_ExtractAlphaParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 30 | void MLX90641_ExtractOffsetParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 31 | void MLX90641_ExtractKtaPixelParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 32 | void MLX90641_ExtractKvPixelParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 33 | void MLX90641_ExtractCPParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 34 | int MLX90641_ExtractDeviatingPixels(uint16_t *eeData, paramsMLX90641 *mlx90641); 35 | int MLX90641_HammingDecode(uint16_t *eeData); 36 | 37 | 38 | //------------------------------------------------------------------------------ 39 | 40 | int MLX90641_DumpEE(uint8_t slaveAddr, uint16_t *eeData) 41 | { 42 | int error = 1; 43 | error = MLX9064x_I2CRead(slaveAddr, 0x2400, 832, eeData); 44 | if (error == 0) 45 | { 46 | error = MLX90641_HammingDecode(eeData); 47 | } 48 | 49 | return error; 50 | } 51 | 52 | //------------------------------------------------------------------------------ 53 | 54 | int MLX90641_HammingDecode(uint16_t *eeData) 55 | { 56 | int error = 0; 57 | int16_t parity[5]; 58 | int8_t D[16]; 59 | int16_t check; 60 | uint16_t data; 61 | uint16_t mask; 62 | 63 | for (int addr=16; addr<832; addr++) 64 | { 65 | parity[0] = -1; 66 | parity[1] = -1; 67 | parity[2] = -1; 68 | parity[3] = -1; 69 | parity[4] = -1; 70 | 71 | data = eeData[addr]; 72 | mask = 1; 73 | for( int i = 0; i < 16; i++) 74 | { 75 | D[i] = (data & mask) >> i; 76 | mask = mask << 1; 77 | } 78 | 79 | parity[0] = D[0]^D[1]^D[3]^D[4]^D[6]^D[8]^D[10]^D[11]; 80 | parity[1] = D[0]^D[2]^D[3]^D[5]^D[6]^D[9]^D[10]^D[12]; 81 | parity[2] = D[1]^D[2]^D[3]^D[7]^D[8]^D[9]^D[10]^D[13]; 82 | parity[3] = D[4]^D[5]^D[6]^D[7]^D[8]^D[9]^D[10]^D[14]; 83 | parity[4] = D[0]^D[1]^D[2]^D[3]^D[4]^D[5]^D[6]^D[7]^D[8]^D[9]^D[10]^D[11]^D[12]^D[13]^D[14]^D[15]; 84 | 85 | if ((parity[0]!=0) || (parity[1]!=0) || (parity[2]!=0) || (parity[3]!=0) || (parity[4]!=0)) 86 | { 87 | check = (parity[0]<<0) + (parity[1]<<1) + (parity[2]<<2) + (parity[3]<<3) + (parity[4]<<4); 88 | 89 | if ((check > 15)&&(check < 32)) 90 | { 91 | switch (check) 92 | { 93 | case 16: 94 | D[15] = 1 - D[15]; 95 | break; 96 | 97 | case 24: 98 | D[14] = 1 - D[14]; 99 | break; 100 | 101 | case 20: 102 | D[13] = 1 - D[13]; 103 | break; 104 | 105 | case 18: 106 | D[12] = 1 - D[12]; 107 | break; 108 | 109 | case 17: 110 | D[11] = 1 - D[11]; 111 | break; 112 | 113 | case 31: 114 | D[10] = 1 - D[10]; 115 | break; 116 | 117 | case 30: 118 | D[9] = 1 - D[9]; 119 | break; 120 | 121 | case 29: 122 | D[8] = 1 - D[8]; 123 | break; 124 | 125 | case 28: 126 | D[7] = 1 - D[7]; 127 | break; 128 | 129 | case 27: 130 | D[6] = 1 - D[6]; 131 | break; 132 | 133 | case 26: 134 | D[5] = 1 - D[5]; 135 | break; 136 | 137 | case 25: 138 | D[4] = 1 - D[4]; 139 | break; 140 | 141 | case 23: 142 | D[3] = 1 - D[3]; 143 | break; 144 | 145 | case 22: 146 | D[2] = 1 - D[2]; 147 | break; 148 | 149 | case 21: 150 | D[1] = 1 - D[1]; 151 | break; 152 | 153 | case 19: 154 | D[0] = 1 - D[0]; 155 | break; 156 | 157 | } 158 | 159 | if(error == 0) 160 | { 161 | error = -9; 162 | 163 | } 164 | 165 | data = 0; 166 | mask = 1; 167 | for( int i = 0; i < 16; i++) 168 | { 169 | data = data + D[i]*mask; 170 | mask = mask << 1; 171 | } 172 | 173 | } 174 | else 175 | { 176 | error = -10; 177 | } 178 | } 179 | 180 | eeData[addr] = data & 0x07FF; 181 | } 182 | 183 | return error; 184 | } 185 | 186 | //------------------------------------------------------------------------------ 187 | 188 | int MLX90641_GetFrameData(uint8_t slaveAddr, uint16_t *frameData) 189 | { 190 | uint16_t dataReady = 1; 191 | uint16_t controlRegister1; 192 | uint16_t statusRegister; 193 | int error = 1; 194 | uint8_t cnt = 0; 195 | uint8_t subPage = 0; 196 | 197 | dataReady = 0; 198 | while(dataReady == 0) 199 | { 200 | error = MLX9064x_I2CRead(slaveAddr, 0x8000, 1, &statusRegister); 201 | if(error != 0) 202 | { 203 | return error; 204 | } 205 | dataReady = statusRegister & 0x0008; 206 | } 207 | subPage = statusRegister & 0x0001; 208 | 209 | while(dataReady != 0 && cnt < 5) 210 | { 211 | error = MLX9064x_I2CWrite(slaveAddr, 0x8000, 0x0030); 212 | if(error == -1) 213 | { 214 | return error; 215 | } 216 | 217 | if(subPage == 0) 218 | { 219 | error = MLX9064x_I2CRead(slaveAddr, 0x0400, 32, frameData); 220 | if(error != 0) 221 | { 222 | return error; 223 | } 224 | error = MLX9064x_I2CRead(slaveAddr, 0x0440, 32, frameData+32); 225 | if(error != 0) 226 | { 227 | return error; 228 | } 229 | error = MLX9064x_I2CRead(slaveAddr, 0x0480, 32, frameData+64); 230 | if(error != 0) 231 | { 232 | return error; 233 | } 234 | error = MLX9064x_I2CRead(slaveAddr, 0x04C0, 32, frameData+96); 235 | if(error != 0) 236 | { 237 | return error; 238 | } 239 | error = MLX9064x_I2CRead(slaveAddr, 0x0500, 32, frameData+128); 240 | if(error != 0) 241 | { 242 | return error; 243 | } 244 | error = MLX9064x_I2CRead(slaveAddr, 0x0540, 32, frameData+160); 245 | if(error != 0) 246 | { 247 | return error; 248 | } 249 | } 250 | else 251 | { 252 | error = MLX9064x_I2CRead(slaveAddr, 0x0420, 32, frameData); 253 | if(error != 0) 254 | { 255 | return error; 256 | } 257 | error = MLX9064x_I2CRead(slaveAddr, 0x0460, 32, frameData+32); 258 | if(error != 0) 259 | { 260 | return error; 261 | } 262 | error = MLX9064x_I2CRead(slaveAddr, 0x04A0, 32, frameData+64); 263 | if(error != 0) 264 | { 265 | return error; 266 | } 267 | error = MLX9064x_I2CRead(slaveAddr, 0x04E0, 32, frameData+96); 268 | if(error != 0) 269 | { 270 | return error; 271 | } 272 | error = MLX9064x_I2CRead(slaveAddr, 0x0520, 32, frameData+128); 273 | if(error != 0) 274 | { 275 | return error; 276 | } 277 | error = MLX9064x_I2CRead(slaveAddr, 0x0560, 32, frameData+160); 278 | if(error != 0) 279 | { 280 | return error; 281 | } 282 | } 283 | 284 | error = MLX9064x_I2CRead(slaveAddr, 0x0580, 48, frameData+192); 285 | if(error != 0) 286 | { 287 | return error; 288 | } 289 | 290 | error = MLX9064x_I2CRead(slaveAddr, 0x8000, 1, &statusRegister); 291 | if(error != 0) 292 | { 293 | return error; 294 | } 295 | dataReady = statusRegister & 0x0008; 296 | subPage = statusRegister & 0x0001; 297 | cnt = cnt + 1; 298 | } 299 | 300 | if(cnt > 4) 301 | { 302 | return -8; 303 | } 304 | 305 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 306 | 307 | frameData[240] = controlRegister1; 308 | frameData[241] = statusRegister & 0x0001; 309 | 310 | if(error != 0) 311 | { 312 | return error; 313 | } 314 | 315 | return frameData[241]; 316 | } 317 | 318 | //------------------------------------------------------------------------------ 319 | 320 | int MLX90641_ExtractParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 321 | { 322 | int error = MLX90641_CheckEEPROMValid(eeData); 323 | 324 | if(error == 0) 325 | { 326 | MLX90641_ExtractVDDParameters(eeData, mlx90641); 327 | MLX90641_ExtractPTATParameters(eeData, mlx90641); 328 | MLX90641_ExtractGainParameters(eeData, mlx90641); 329 | MLX90641_ExtractTgcParameters(eeData, mlx90641); 330 | MLX90641_ExtractEmissivityParameters(eeData, mlx90641); 331 | MLX90641_ExtractResolutionParameters(eeData, mlx90641); 332 | MLX90641_ExtractKsTaParameters(eeData, mlx90641); 333 | MLX90641_ExtractKsToParameters(eeData, mlx90641); 334 | MLX90641_ExtractAlphaParameters(eeData, mlx90641); 335 | MLX90641_ExtractOffsetParameters(eeData, mlx90641); 336 | MLX90641_ExtractKtaPixelParameters(eeData, mlx90641); 337 | MLX90641_ExtractKvPixelParameters(eeData, mlx90641); 338 | MLX90641_ExtractCPParameters(eeData, mlx90641); 339 | error = MLX90641_ExtractDeviatingPixels(eeData, mlx90641); 340 | } 341 | 342 | return error; 343 | 344 | } 345 | 346 | //------------------------------------------------------------------------------ 347 | 348 | int MLX90641_SetResolution(uint8_t slaveAddr, uint8_t resolution) 349 | { 350 | uint16_t controlRegister1; 351 | int value; 352 | int error; 353 | 354 | value = (resolution & 0x03) << 10; 355 | 356 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 357 | 358 | if(error == 0) 359 | { 360 | value = (controlRegister1 & 0xF3FF) | value; 361 | error = MLX9064x_I2CWrite(slaveAddr, 0x800D, value); 362 | } 363 | 364 | return error; 365 | } 366 | 367 | //------------------------------------------------------------------------------ 368 | 369 | int MLX90641_GetCurResolution(uint8_t slaveAddr) 370 | { 371 | uint16_t controlRegister1; 372 | int resolutionRAM; 373 | int error; 374 | 375 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 376 | if(error != 0) 377 | { 378 | return error; 379 | } 380 | resolutionRAM = (controlRegister1 & 0x0C00) >> 10; 381 | 382 | return resolutionRAM; 383 | } 384 | 385 | //------------------------------------------------------------------------------ 386 | 387 | int MLX90641_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate) 388 | { 389 | uint16_t controlRegister1; 390 | int value; 391 | int error; 392 | 393 | value = (refreshRate & 0x07)<<7; 394 | 395 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 396 | if(error == 0) 397 | { 398 | value = (controlRegister1 & 0xFC7F) | value; 399 | error = MLX9064x_I2CWrite(slaveAddr, 0x800D, value); 400 | } 401 | 402 | return error; 403 | } 404 | 405 | //------------------------------------------------------------------------------ 406 | 407 | int MLX90641_GetRefreshRate(uint8_t slaveAddr) 408 | { 409 | uint16_t controlRegister1; 410 | int refreshRate; 411 | int error; 412 | 413 | error = MLX9064x_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); 414 | if(error != 0) 415 | { 416 | return error; 417 | } 418 | refreshRate = (controlRegister1 & 0x0380) >> 7; 419 | 420 | return refreshRate; 421 | } 422 | 423 | //------------------------------------------------------------------------------ 424 | 425 | void MLX90641_CalculateTo(uint16_t *frameData, const paramsMLX90641 *params, float emissivity, float tr, float *result) 426 | { 427 | float vdd; 428 | float ta; 429 | float ta4; 430 | float tr4; 431 | float taTr; 432 | float gain; 433 | float irDataCP; 434 | float irData; 435 | float alphaCompensated; 436 | float Sx; 437 | float To; 438 | float alphaCorrR[8]; 439 | int8_t range; 440 | uint16_t subPage; 441 | float ktaScale; 442 | float kvScale; 443 | float alphaScale; 444 | float kta; 445 | float kv; 446 | 447 | subPage = frameData[241]; 448 | vdd = MLX90641_GetVdd(frameData, params); 449 | ta = MLX90641_GetTa(frameData, params); 450 | ta4 = (ta + 273.15); 451 | ta4 = ta4 * ta4; 452 | ta4 = ta4 * ta4; 453 | tr4 = (tr + 273.15); 454 | tr4 = tr4 * tr4; 455 | tr4 = tr4 * tr4; 456 | 457 | taTr = tr4 - (tr4-ta4)/emissivity; 458 | 459 | ktaScale = pow(2,(double)params->ktaScale); 460 | kvScale = pow(2,(double)params->kvScale); 461 | alphaScale = pow(2,(double)params->alphaScale); 462 | 463 | alphaCorrR[1] = 1 / (1 + params->ksTo[1] * 20); 464 | alphaCorrR[0] = alphaCorrR[1] / (1 + params->ksTo[0] * 20); 465 | alphaCorrR[2] = 1 ; 466 | alphaCorrR[3] = (1 + params->ksTo[2] * params->ct[3]); 467 | alphaCorrR[4] = alphaCorrR[3] * (1 + params->ksTo[3] * (params->ct[4] - params->ct[3])); 468 | alphaCorrR[5] = alphaCorrR[4] * (1 + params->ksTo[4] * (params->ct[5] - params->ct[4])); 469 | alphaCorrR[6] = alphaCorrR[5] * (1 + params->ksTo[5] * (params->ct[6] - params->ct[5])); 470 | alphaCorrR[7] = alphaCorrR[6] * (1 + params->ksTo[6] * (params->ct[7] - params->ct[6])); 471 | 472 | //------------------------- Gain calculation ----------------------------------- 473 | gain = frameData[202]; 474 | if(gain > 32767) 475 | { 476 | gain = gain - 65536; 477 | } 478 | 479 | gain = params->gainEE / gain; 480 | 481 | //------------------------- To calculation ------------------------------------- 482 | irDataCP = frameData[200]; 483 | if(irDataCP > 32767) 484 | { 485 | irDataCP = irDataCP - 65536; 486 | } 487 | irDataCP = irDataCP * gain; 488 | 489 | irDataCP = irDataCP - params->cpOffset * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); 490 | 491 | for( int pixelNumber = 0; pixelNumber < 192; pixelNumber++) 492 | { 493 | irData = frameData[pixelNumber]; 494 | if(irData > 32767) 495 | { 496 | irData = irData - 65536; 497 | } 498 | irData = irData * gain; 499 | 500 | kta = (float)params->kta[pixelNumber]/ktaScale; 501 | kv = (float)params->kv[pixelNumber]/kvScale; 502 | 503 | irData = irData - params->offset[subPage][pixelNumber]*(1 + kta*(ta - 25))*(1 + kv*(vdd - 3.3)); 504 | 505 | irData = irData - params->tgc * irDataCP; 506 | 507 | irData = irData / emissivity; 508 | 509 | alphaCompensated = SCALEALPHA*alphaScale/params->alpha[pixelNumber]; 510 | alphaCompensated = alphaCompensated*(1 + params->KsTa * (ta - 25)); 511 | 512 | Sx = alphaCompensated * alphaCompensated * alphaCompensated * (irData + alphaCompensated * taTr); 513 | Sx = sqrt(sqrt(Sx)) * params->ksTo[2]; 514 | 515 | To = sqrt(sqrt(irData/(alphaCompensated * (1 - params->ksTo[2] * 273.15) + Sx) + taTr)) - 273.15; 516 | 517 | if(To < params->ct[1]) 518 | { 519 | range = 0; 520 | } 521 | else if(To < params->ct[2]) 522 | { 523 | range = 1; 524 | } 525 | else if(To < params->ct[3]) 526 | { 527 | range = 2; 528 | } 529 | else if(To < params->ct[4]) 530 | { 531 | range = 3; 532 | } 533 | else if(To < params->ct[5]) 534 | { 535 | range = 4; 536 | } 537 | else if(To < params->ct[6]) 538 | { 539 | range = 5; 540 | } 541 | else if(To < params->ct[7]) 542 | { 543 | range = 6; 544 | } 545 | else 546 | { 547 | range = 7; 548 | } 549 | 550 | To = sqrt(sqrt(irData / (alphaCompensated * alphaCorrR[range] * (1 + params->ksTo[range] * (To - params->ct[range]))) + taTr)) - 273.15; 551 | 552 | result[pixelNumber] = To; 553 | } 554 | } 555 | 556 | //------------------------------------------------------------------------------ 557 | 558 | void MLX90641_GetImage(uint16_t *frameData, const paramsMLX90641 *params, float *result) 559 | { 560 | float vdd; 561 | float ta; 562 | float gain; 563 | float irDataCP; 564 | float irData; 565 | float alphaCompensated; 566 | float image; 567 | uint16_t subPage; 568 | 569 | subPage = frameData[241]; 570 | 571 | vdd = MLX90641_GetVdd(frameData, params); 572 | ta = MLX90641_GetTa(frameData, params); 573 | 574 | //------------------------- Gain calculation ----------------------------------- 575 | gain = frameData[202]; 576 | if(gain > 32767) 577 | { 578 | gain = gain - 65536; 579 | } 580 | 581 | gain = params->gainEE / gain; 582 | 583 | //------------------------- Image calculation ------------------------------------- 584 | irDataCP = frameData[200]; 585 | if(irDataCP > 32767) 586 | { 587 | irDataCP = irDataCP - 65536; 588 | } 589 | irDataCP = irDataCP * gain; 590 | 591 | irDataCP = irDataCP - params->cpOffset * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); 592 | 593 | for( int pixelNumber = 0; pixelNumber < 192; pixelNumber++) 594 | { 595 | irData = frameData[pixelNumber]; 596 | if(irData > 32767) 597 | { 598 | irData = irData - 65536; 599 | } 600 | irData = irData * gain; 601 | 602 | irData = irData - params->offset[subPage][pixelNumber]*(1 + params->kta[pixelNumber]*(ta - 25))*(1 + params->kv[pixelNumber]*(vdd - 3.3)); 603 | 604 | irData = irData - params->tgc * irDataCP; 605 | 606 | alphaCompensated = (params->alpha[pixelNumber] - params->tgc * params->cpAlpha); 607 | 608 | image = irData*alphaCompensated; 609 | 610 | result[pixelNumber] = image; 611 | } 612 | } 613 | 614 | //------------------------------------------------------------------------------ 615 | 616 | float MLX90641_GetVdd(uint16_t *frameData, const paramsMLX90641 *params) 617 | { 618 | float vdd; 619 | float resolutionCorrection; 620 | 621 | int resolutionRAM; 622 | 623 | vdd = frameData[234]; 624 | if(vdd > 32767) 625 | { 626 | vdd = vdd - 65536; 627 | } 628 | resolutionRAM = (frameData[240] & 0x0C00) >> 10; 629 | resolutionCorrection = pow(2, (double)params->resolutionEE) / pow(2, (double)resolutionRAM); 630 | vdd = (resolutionCorrection * vdd - params->vdd25) / params->kVdd + 3.3; 631 | 632 | return vdd; 633 | } 634 | 635 | //------------------------------------------------------------------------------ 636 | 637 | float MLX90641_GetTa(uint16_t *frameData, const paramsMLX90641 *params) 638 | { 639 | float ptat; 640 | float ptatArt; 641 | float vdd; 642 | float ta; 643 | 644 | vdd = MLX90641_GetVdd(frameData, params); 645 | 646 | ptat = frameData[224]; 647 | if(ptat > 32767) 648 | { 649 | ptat = ptat - 65536; 650 | } 651 | 652 | ptatArt = frameData[192]; 653 | if(ptatArt > 32767) 654 | { 655 | ptatArt = ptatArt - 65536; 656 | } 657 | ptatArt = (ptat / (ptat * params->alphaPTAT + ptatArt)) * pow(2, (double)18); 658 | 659 | ta = (ptatArt / (1 + params->KvPTAT * (vdd - 3.3)) - params->vPTAT25); 660 | ta = ta / params->KtPTAT + 25; 661 | 662 | return ta; 663 | } 664 | 665 | //------------------------------------------------------------------------------ 666 | 667 | int MLX90641_GetSubPageNumber(uint16_t *frameData) 668 | { 669 | return frameData[241]; 670 | 671 | } 672 | 673 | //------------------------------------------------------------------------------ 674 | void MLX90641_BadPixelsCorrection(uint16_t *pixels, float *to, paramsMLX90641 *params) 675 | { 676 | float ap[2]; 677 | uint8_t pix; 678 | uint8_t line; 679 | uint8_t column; 680 | 681 | pix = 0; 682 | while(pixels[pix]< 65535) 683 | { 684 | line = pixels[pix]>>5; 685 | column = pixels[pix] - (line<<5); 686 | 687 | if(column == 0) 688 | { 689 | to[pixels[pix]] = to[pixels[pix]+1]; 690 | } 691 | else if(column == 1 || column == 14) 692 | { 693 | to[pixels[pix]] = (to[pixels[pix]-1]+to[pixels[pix]+1])/2.0; 694 | } 695 | else if(column == 15) 696 | { 697 | to[pixels[pix]] = to[pixels[pix]-1]; 698 | } 699 | else 700 | { 701 | ap[0] = to[pixels[pix]+1] - to[pixels[pix]+2]; 702 | ap[1] = to[pixels[pix]-1] - to[pixels[pix]-2]; 703 | if(fabs(ap[0]) > fabs(ap[1])) 704 | { 705 | to[pixels[pix]] = to[pixels[pix]-1] + ap[1]; 706 | } 707 | else 708 | { 709 | to[pixels[pix]] = to[pixels[pix]+1] + ap[0]; 710 | } 711 | 712 | } 713 | 714 | pix = pix + 1; 715 | } 716 | } 717 | 718 | //------------------------------------------------------------------------------ 719 | 720 | void MLX90641_ExtractVDDParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 721 | { 722 | int16_t kVdd; 723 | int16_t vdd25; 724 | 725 | kVdd = eeData[39]; 726 | if(kVdd > 1023) 727 | { 728 | kVdd = kVdd - 2048; 729 | } 730 | kVdd = 32 * kVdd; 731 | 732 | vdd25 = eeData[38]; 733 | if(vdd25 > 1023) 734 | { 735 | vdd25 = vdd25 - 2048; 736 | } 737 | vdd25 = 32 * vdd25; 738 | 739 | mlx90641->kVdd = kVdd; 740 | mlx90641->vdd25 = vdd25; 741 | } 742 | 743 | //------------------------------------------------------------------------------ 744 | 745 | void MLX90641_ExtractPTATParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 746 | { 747 | float KvPTAT; 748 | float KtPTAT; 749 | int16_t vPTAT25; 750 | float alphaPTAT; 751 | 752 | KvPTAT = eeData[43]; 753 | if(KvPTAT > 1023) 754 | { 755 | KvPTAT = KvPTAT - 2048; 756 | } 757 | KvPTAT = KvPTAT/4096; 758 | 759 | KtPTAT = eeData[42]; 760 | if(KtPTAT > 1023) 761 | { 762 | KtPTAT = KtPTAT - 2048; 763 | } 764 | KtPTAT = KtPTAT/8; 765 | 766 | vPTAT25 = 32 * eeData[40] + eeData[41]; 767 | 768 | alphaPTAT = eeData[44] / 128.0f; 769 | 770 | mlx90641->KvPTAT = KvPTAT; 771 | mlx90641->KtPTAT = KtPTAT; 772 | mlx90641->vPTAT25 = vPTAT25; 773 | mlx90641->alphaPTAT = alphaPTAT; 774 | } 775 | 776 | //------------------------------------------------------------------------------ 777 | 778 | void MLX90641_ExtractGainParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 779 | { 780 | int16_t gainEE; 781 | 782 | gainEE = 32 * eeData[36] + eeData[37]; 783 | 784 | mlx90641->gainEE = gainEE; 785 | } 786 | 787 | //------------------------------------------------------------------------------ 788 | 789 | void MLX90641_ExtractTgcParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 790 | { 791 | float tgc; 792 | tgc = eeData[51] & 0x01FF; 793 | if(tgc > 255) 794 | { 795 | tgc = tgc - 512; 796 | } 797 | tgc = tgc / 64.0f; 798 | 799 | mlx90641->tgc = tgc; 800 | } 801 | 802 | //------------------------------------------------------------------------------ 803 | 804 | void MLX90641_ExtractEmissivityParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 805 | { 806 | float emissivity; 807 | emissivity = eeData[35]; 808 | 809 | if(emissivity > 1023) 810 | { 811 | emissivity = emissivity - 2048; 812 | } 813 | emissivity = emissivity/512; 814 | 815 | mlx90641->emissivityEE = emissivity; 816 | } 817 | 818 | //------------------------------------------------------------------------------ 819 | 820 | void MLX90641_ExtractResolutionParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 821 | { 822 | uint8_t resolutionEE; 823 | resolutionEE = (eeData[51] & 0x0600) >> 9; 824 | 825 | mlx90641->resolutionEE = resolutionEE; 826 | } 827 | 828 | //------------------------------------------------------------------------------ 829 | 830 | void MLX90641_ExtractKsTaParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 831 | { 832 | float KsTa; 833 | KsTa = eeData[34]; 834 | if(KsTa > 1023) 835 | { 836 | KsTa = KsTa - 2048; 837 | } 838 | KsTa = KsTa / 32768.0f; 839 | 840 | mlx90641->KsTa = KsTa; 841 | } 842 | 843 | //------------------------------------------------------------------------------ 844 | 845 | void MLX90641_ExtractKsToParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 846 | { 847 | int KsToScale; 848 | 849 | mlx90641->ct[0] = -40; 850 | mlx90641->ct[1] = -20; 851 | mlx90641->ct[2] = 0; 852 | mlx90641->ct[3] = 80; 853 | mlx90641->ct[4] = 120; 854 | mlx90641->ct[5] = eeData[58]; 855 | mlx90641->ct[6] = eeData[60]; 856 | mlx90641->ct[7] = eeData[62]; 857 | 858 | KsToScale = eeData[52]; 859 | KsToScale = 1 << KsToScale; 860 | 861 | mlx90641->ksTo[0] = eeData[53]; 862 | mlx90641->ksTo[1] = eeData[54]; 863 | mlx90641->ksTo[2] = eeData[55]; 864 | mlx90641->ksTo[3] = eeData[56]; 865 | mlx90641->ksTo[4] = eeData[57]; 866 | mlx90641->ksTo[5] = eeData[59]; 867 | mlx90641->ksTo[6] = eeData[61]; 868 | mlx90641->ksTo[7] = eeData[63]; 869 | 870 | 871 | for(int i = 0; i < 8; i++) 872 | { 873 | if(mlx90641->ksTo[i] > 1023) 874 | { 875 | mlx90641->ksTo[i] = mlx90641->ksTo[i] - 2048; 876 | } 877 | mlx90641->ksTo[i] = mlx90641->ksTo[i] / KsToScale; 878 | } 879 | } 880 | 881 | //------------------------------------------------------------------------------ 882 | 883 | void MLX90641_ExtractAlphaParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 884 | { 885 | float rowMaxAlphaNorm[6]; 886 | uint16_t scaleRowAlpha[6]; 887 | uint8_t alphaScale; 888 | float alphaTemp[192]; 889 | float temp; 890 | int p = 0; 891 | 892 | scaleRowAlpha[0] = (eeData[25] >> 5) + 20; 893 | scaleRowAlpha[1] = (eeData[25] & 0x001F) + 20; 894 | scaleRowAlpha[2] = (eeData[26] >> 5) + 20; 895 | scaleRowAlpha[3] = (eeData[26] & 0x001F) + 20; 896 | scaleRowAlpha[4] = (eeData[27] >> 5) + 20; 897 | scaleRowAlpha[5] = (eeData[27] & 0x001F) + 20; 898 | 899 | 900 | for(int i = 0; i < 6; i++) 901 | { 902 | rowMaxAlphaNorm[i] = eeData[28 + i] / pow(2,(double)scaleRowAlpha[i]); 903 | rowMaxAlphaNorm[i] = rowMaxAlphaNorm[i] / 2047.0f; 904 | } 905 | 906 | for(int i = 0; i < 6; i++) 907 | { 908 | for(int j = 0; j < 32; j ++) 909 | { 910 | p = 32 * i +j; 911 | alphaTemp[p] = eeData[256 + p] * rowMaxAlphaNorm[i]; 912 | alphaTemp[p] = alphaTemp[p] - mlx90641->tgc * mlx90641->cpAlpha; 913 | alphaTemp[p] = SCALEALPHA/alphaTemp[p]; 914 | } 915 | } 916 | 917 | temp = alphaTemp[0]; 918 | for(int i = 1; i < 192; i++) 919 | { 920 | if (alphaTemp[i] > temp) 921 | { 922 | temp = alphaTemp[i]; 923 | } 924 | } 925 | 926 | alphaScale = 0; 927 | while(temp < 32768) 928 | { 929 | temp = temp*2; 930 | alphaScale = alphaScale + 1; 931 | } 932 | 933 | for(int i = 0; i < 192; i++) 934 | { 935 | temp = alphaTemp[i] * pow(2,(double)alphaScale); 936 | mlx90641->alpha[i] = (temp + 0.5); 937 | 938 | } 939 | 940 | mlx90641->alphaScale = alphaScale; 941 | } 942 | 943 | //------------------------------------------------------------------------------ 944 | 945 | void MLX90641_ExtractOffsetParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 946 | { 947 | int scaleOffset; 948 | int16_t offsetRef; 949 | int16_t tempOffset; 950 | 951 | scaleOffset = eeData[16] >> 5; 952 | scaleOffset = 1 << scaleOffset; 953 | 954 | offsetRef = 32 * eeData[17] + eeData[18]; 955 | if (offsetRef > 32767) 956 | { 957 | offsetRef = offsetRef - 65536; 958 | } 959 | 960 | for(int i = 0; i < 192; i++) 961 | { 962 | tempOffset = eeData[64 + i]; 963 | if(tempOffset > 1023) 964 | { 965 | tempOffset = eeData[64 + i] - 2048; 966 | } 967 | mlx90641->offset[0][i] = tempOffset * scaleOffset + offsetRef; 968 | 969 | tempOffset = eeData[640 + i]; 970 | if(tempOffset > 1023) 971 | { 972 | tempOffset = eeData[640 + i] - 2048; 973 | } 974 | mlx90641->offset[1][i] = tempOffset * scaleOffset + offsetRef; 975 | } 976 | } 977 | 978 | //------------------------------------------------------------------------------ 979 | 980 | void MLX90641_ExtractKtaPixelParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 981 | { 982 | uint8_t ktaScale1; 983 | uint8_t ktaScale2; 984 | int16_t ktaAvg; 985 | int16_t tempKta; 986 | float ktaTemp[192]; 987 | float temp; 988 | 989 | ktaAvg = eeData[21]; 990 | if (ktaAvg > 1023) 991 | { 992 | ktaAvg = ktaAvg - 2048; 993 | } 994 | 995 | ktaScale1 = eeData[22] >> 5; 996 | ktaScale2 = eeData[22] & 0x001F; 997 | 998 | for(int i = 0; i < 192; i++) 999 | { 1000 | tempKta = (eeData[448 + i] >> 5); 1001 | if (tempKta > 31) 1002 | { 1003 | tempKta = tempKta - 64; 1004 | } 1005 | 1006 | ktaTemp[i] = tempKta * pow(2,(double)ktaScale2); 1007 | ktaTemp[i] = ktaTemp[i] + ktaAvg; 1008 | ktaTemp[i] = ktaTemp[i] / pow(2,(double)ktaScale1); 1009 | } 1010 | 1011 | temp = fabs(ktaTemp[0]); 1012 | for(int i = 1; i < 192; i++) 1013 | { 1014 | if (fabs(ktaTemp[i]) > temp) 1015 | { 1016 | temp = fabs(ktaTemp[i]); 1017 | } 1018 | } 1019 | 1020 | ktaScale1 = 0; 1021 | while(temp < 64) 1022 | { 1023 | temp = temp*2; 1024 | ktaScale1 = ktaScale1 + 1; 1025 | } 1026 | 1027 | for(int i = 0; i < 192; i++) 1028 | { 1029 | temp = ktaTemp[i] * pow(2,(double)ktaScale1); 1030 | if (temp < 0) 1031 | { 1032 | mlx90641->kta[i] = (temp - 0.5); 1033 | } 1034 | else 1035 | { 1036 | mlx90641->kta[i] = (temp + 0.5); 1037 | } 1038 | 1039 | } 1040 | 1041 | mlx90641->ktaScale = ktaScale1; 1042 | } 1043 | 1044 | //------------------------------------------------------------------------------ 1045 | 1046 | void MLX90641_ExtractKvPixelParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 1047 | { 1048 | uint8_t kvScale1; 1049 | uint8_t kvScale2; 1050 | int16_t kvAvg; 1051 | int16_t tempKv; 1052 | float kvTemp[192]; 1053 | float temp; 1054 | 1055 | kvAvg = eeData[23]; 1056 | if (kvAvg > 1023) 1057 | { 1058 | kvAvg = kvAvg - 2048; 1059 | } 1060 | 1061 | kvScale1 = eeData[24] >> 5; 1062 | kvScale2 = eeData[24] & 0x001F; 1063 | 1064 | for(int i = 0; i < 192; i++) 1065 | { 1066 | tempKv = (eeData[448 + i] & 0x001F); 1067 | if (tempKv > 15) 1068 | { 1069 | tempKv = tempKv - 32; 1070 | } 1071 | 1072 | kvTemp[i] = tempKv * pow(2,(double)kvScale2); 1073 | kvTemp[i] = kvTemp[i] + kvAvg; 1074 | kvTemp[i] = kvTemp[i] / pow(2,(double)kvScale1); 1075 | } 1076 | 1077 | temp = fabs(kvTemp[0]); 1078 | for(int i = 1; i < 192; i++) 1079 | { 1080 | if (fabs(kvTemp[i]) > temp) 1081 | { 1082 | temp = fabs(kvTemp[i]); 1083 | } 1084 | } 1085 | 1086 | kvScale1 = 0; 1087 | while(temp < 64) 1088 | { 1089 | temp = temp*2; 1090 | kvScale1 = kvScale1 + 1; 1091 | } 1092 | 1093 | for(int i = 0; i < 192; i++) 1094 | { 1095 | temp = kvTemp[i] * pow(2,(double)kvScale1); 1096 | if (temp < 0) 1097 | { 1098 | mlx90641->kv[i] = (temp - 0.5); 1099 | } 1100 | else 1101 | { 1102 | mlx90641->kv[i] = (temp + 0.5); 1103 | } 1104 | 1105 | } 1106 | 1107 | mlx90641->kvScale = kvScale1; 1108 | } 1109 | 1110 | //------------------------------------------------------------------------------ 1111 | 1112 | void MLX90641_ExtractCPParameters(uint16_t *eeData, paramsMLX90641 *mlx90641) 1113 | { 1114 | float alphaCP; 1115 | int16_t offsetCP; 1116 | float cpKv; 1117 | float cpKta; 1118 | uint8_t alphaScale; 1119 | uint8_t ktaScale1; 1120 | uint8_t kvScale; 1121 | 1122 | alphaScale = eeData[46]; 1123 | 1124 | offsetCP = 32 * eeData[47] + eeData[48]; 1125 | if (offsetCP > 32767) 1126 | { 1127 | offsetCP = offsetCP - 65536; 1128 | } 1129 | 1130 | alphaCP = eeData[45]; 1131 | if (alphaCP > 1023) 1132 | { 1133 | alphaCP = alphaCP - 2048; 1134 | } 1135 | 1136 | alphaCP = alphaCP / pow(2,(double)alphaScale); 1137 | 1138 | 1139 | cpKta = eeData[49] & 0x001F; 1140 | if (cpKta > 31) 1141 | { 1142 | cpKta = cpKta - 64; 1143 | } 1144 | ktaScale1 = eeData[49] >> 6; 1145 | mlx90641->cpKta = cpKta / pow(2,(double)ktaScale1); 1146 | 1147 | cpKv = eeData[50] & 0x001F; 1148 | if (cpKv > 31) 1149 | { 1150 | cpKv = cpKv - 64; 1151 | } 1152 | kvScale = eeData[50] >> 6; 1153 | mlx90641->cpKv = cpKv / pow(2,(double)kvScale); 1154 | 1155 | mlx90641->cpAlpha = alphaCP; 1156 | mlx90641->cpOffset = offsetCP; 1157 | } 1158 | 1159 | //------------------------------------------------------------------------------ 1160 | 1161 | float MLX90641_GetEmissivity(const paramsMLX90641 *mlx90641) 1162 | { 1163 | return mlx90641->emissivityEE; 1164 | } 1165 | 1166 | //------------------------------------------------------------------------------ 1167 | 1168 | int MLX90641_ExtractDeviatingPixels(uint16_t *eeData, paramsMLX90641 *mlx90641) 1169 | { 1170 | uint16_t pixCnt = 0; 1171 | uint16_t brokenPixCnt = 0; 1172 | 1173 | int warn = 0; 1174 | 1175 | for(pixCnt = 0; pixCnt<3; pixCnt++) 1176 | { 1177 | mlx90641->brokenPixels[pixCnt] = 0xFFFF; 1178 | } 1179 | 1180 | pixCnt = 0; 1181 | while (pixCnt < 192 && brokenPixCnt < 3) 1182 | { 1183 | if((eeData[pixCnt+64] == 0) && (eeData[pixCnt+256] == 0) && (eeData[pixCnt+448] == 0) && (eeData[pixCnt+640] == 0)) 1184 | { 1185 | mlx90641->brokenPixels[brokenPixCnt] = pixCnt; 1186 | brokenPixCnt = brokenPixCnt + 1; 1187 | } 1188 | 1189 | pixCnt = pixCnt + 1; 1190 | 1191 | } 1192 | 1193 | if(brokenPixCnt > 1) 1194 | { 1195 | warn = -3; 1196 | } 1197 | 1198 | return warn; 1199 | 1200 | } 1201 | 1202 | //------------------------------------------------------------------------------ 1203 | 1204 | int MLX90641_CheckEEPROMValid(uint16_t *eeData) 1205 | { 1206 | int deviceSelect; 1207 | deviceSelect = ((uint16_t)eeData[10]) & 0x0040; 1208 | 1209 | if(deviceSelect != 0) 1210 | { 1211 | return 0; 1212 | } 1213 | 1214 | return -7; 1215 | } -------------------------------------------------------------------------------- /src/MLX90641_API.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright (C) 2017 Melexis N.V. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | #ifndef _MLX641_API_H_ 18 | #define _MLX641_API_H_ 19 | 20 | #define SCALEALPHA 0.000001 21 | 22 | typedef struct 23 | { 24 | int16_t kVdd; 25 | int16_t vdd25; 26 | float KvPTAT; 27 | float KtPTAT; 28 | uint16_t vPTAT25; 29 | float alphaPTAT; 30 | int16_t gainEE; 31 | float tgc; 32 | float cpKv; 33 | float cpKta; 34 | uint8_t resolutionEE; 35 | uint8_t calibrationModeEE; 36 | float KsTa; 37 | float ksTo[8]; 38 | int16_t ct[8]; 39 | uint16_t alpha[192]; 40 | uint8_t alphaScale; 41 | int16_t offset[2][192]; 42 | int8_t kta[192]; 43 | uint8_t ktaScale; 44 | int8_t kv[192]; 45 | uint8_t kvScale; 46 | float cpAlpha; 47 | int16_t cpOffset; 48 | float emissivityEE; 49 | uint16_t brokenPixels[2]; 50 | } paramsMLX90641; 51 | 52 | int MLX90641_DumpEE(uint8_t slaveAddr, uint16_t *eeData); 53 | int MLX90641_CheckEEPROMValid(uint16_t *eeData); 54 | 55 | int MLX90641_GetFrameData(uint8_t slaveAddr, uint16_t *frameData); 56 | int MLX90641_ExtractParameters(uint16_t *eeData, paramsMLX90641 *mlx90641); 57 | float MLX90641_GetVdd(uint16_t *frameData, const paramsMLX90641 *params); 58 | float MLX90641_GetTa(uint16_t *frameData, const paramsMLX90641 *params); 59 | void MLX90641_GetImage(uint16_t *frameData, const paramsMLX90641 *params, float *result); 60 | void MLX90641_CalculateTo(uint16_t *frameData, const paramsMLX90641 *params, float emissivity, float tr, float *result); 61 | int MLX90641_SetResolution(uint8_t slaveAddr, uint8_t resolution); 62 | int MLX90641_GetCurResolution(uint8_t slaveAddr); 63 | int MLX90641_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate); 64 | int MLX90641_GetRefreshRate(uint8_t slaveAddr); 65 | int MLX90641_GetSubPageNumber(uint16_t *frameData); 66 | float MLX90641_GetEmissivity(const paramsMLX90641 *mlx90641); 67 | void MLX90641_BadPixelsCorrection(uint16_t *pixels, float *to, paramsMLX90641 *params); 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /src/MLX9064X_I2C_Driver.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | @copyright (C) 2017 Melexis N.V. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | */ 17 | 18 | 19 | #include 20 | #include 21 | 22 | #include "MLX9064X_I2C_Driver.h" 23 | 24 | void MLX9064x_I2CInit() { 25 | 26 | } 27 | 28 | //Read a number of words from startAddress. Store into Data array. 29 | //Returns 0 if successful, -1 if error 30 | int MLX9064x_I2CRead(uint8_t _deviceAddress, unsigned int startAddress, unsigned int nWordsRead, uint16_t* data) { 31 | 32 | //Caller passes number of 'unsigned ints to read', increase this to 'bytes to read' 33 | uint16_t bytesRemaining = nWordsRead * 2; 34 | 35 | //It doesn't look like sequential read works. Do we need to re-issue the address command each time? 36 | 37 | uint16_t dataSpot = 0; //Start at beginning of array 38 | 39 | //Setup a series of chunked I2C_BUFFER_LENGTH byte reads 40 | while (bytesRemaining > 0) { 41 | Wire.beginTransmission(_deviceAddress); 42 | Wire.write(startAddress >> 8); //MSB 43 | Wire.write(startAddress & 0xFF); //LSB 44 | if (Wire.endTransmission(false) != 0) { //Do not release bus 45 | //Serial.println("No ack read"); 46 | return (0); //Sensor did not ACK 47 | } 48 | 49 | uint16_t numberOfBytesToRead = bytesRemaining; 50 | if (numberOfBytesToRead > I2C_BUFFER_LENGTH) { 51 | numberOfBytesToRead = I2C_BUFFER_LENGTH; 52 | } 53 | 54 | Wire.requestFrom((uint8_t)_deviceAddress, numberOfBytesToRead); 55 | if (Wire.available()) { 56 | for (uint16_t x = 0 ; x < numberOfBytesToRead / 2; x++) { 57 | //Store data into array 58 | data[dataSpot] = Wire.read() << 8; //MSB 59 | data[dataSpot] |= Wire.read(); //LSB 60 | 61 | dataSpot++; 62 | } 63 | } 64 | 65 | bytesRemaining -= numberOfBytesToRead; 66 | 67 | startAddress += numberOfBytesToRead / 2; 68 | } 69 | 70 | return (0); //Success 71 | } 72 | 73 | //Set I2C Freq, in kHz 74 | //MLX9064x_I2CFreqSet(1000) sets frequency to 1MHz 75 | void MLX9064x_I2CFreqSet(int freq) { 76 | //i2c.frequency(1000 * freq); 77 | Wire.setClock((long)1000 * freq); 78 | } 79 | 80 | //Write two bytes to a two byte address 81 | int MLX9064x_I2CWrite(uint8_t _deviceAddress, unsigned int writeAddress, uint16_t data) { 82 | Wire.beginTransmission((uint8_t)_deviceAddress); 83 | Wire.write(writeAddress >> 8); //MSB 84 | Wire.write(writeAddress & 0xFF); //LSB 85 | Wire.write(data >> 8); //MSB 86 | Wire.write(data & 0xFF); //LSB 87 | if (Wire.endTransmission() != 0) { 88 | //Sensor did not ACK 89 | //Serial.println("Error: Sensor did not ack"); 90 | return (-1); 91 | } 92 | 93 | uint16_t dataCheck; 94 | MLX9064x_I2CRead(_deviceAddress, writeAddress, 1, &dataCheck); 95 | if (dataCheck != data) { 96 | //Serial.println("The write request didn't stick"); 97 | return -2; 98 | } 99 | 100 | return (0); //Success 101 | } 102 | 103 | -------------------------------------------------------------------------------- /src/MLX9064X_I2C_Driver.h: -------------------------------------------------------------------------------- 1 | /** 2 | @copyright (C) 2017 Melexis N.V. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | */ 17 | #ifndef _MLX9064X_I2C_Driver_H_ 18 | #define _MLX9064X_I2C_Driver_H_ 19 | 20 | #include 21 | 22 | //Define the size of the I2C buffer based on the platform the user has 23 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 24 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) 25 | 26 | //I2C_BUFFER_LENGTH is defined in Wire.H 27 | #define I2C_BUFFER_LENGTH BUFFER_LENGTH 28 | 29 | #elif defined(__SAMD21G18A__) 30 | 31 | //SAMD21 uses RingBuffer.h 32 | #define I2C_BUFFER_LENGTH SERIAL_BUFFER_SIZE 33 | 34 | #elif __MK20DX256__ 35 | //Teensy 3.2 36 | #define I2C_BUFFER_LENGTH 32 37 | 38 | #else 39 | 40 | //The catch-all default is 32 41 | #define I2C_BUFFER_LENGTH 32 42 | 43 | #endif 44 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 45 | 46 | 47 | void MLX9064x_I2CInit(void); 48 | int MLX9064x_I2CRead(uint8_t slaveAddr, unsigned int startAddress, unsigned int nWordsRead, uint16_t* data); 49 | int MLX9064x_I2CWrite(uint8_t slaveAddr, unsigned int writeAddress, uint16_t data); 50 | void MLX9064x_I2CFreqSet(int freq); 51 | #endif 52 | --------------------------------------------------------------------------------