Giter VIP home page Giter VIP logo

adafruit_circuitpython_mlx90640's Introduction

Introduction

Documentation Status Discord Build Status Code Style: Black

Driver for the MLX90640 thermal camera

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-mlx90640

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-mlx90640

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-mlx90640

Usage Example

import time
import board
import busio
import adafruit_mlx90640

i2c = busio.I2C(board.SCL, board.SDA, frequency=800000)

mlx = adafruit_mlx90640.MLX90640(i2c)
print("MLX addr detected on I2C", [hex(i) for i in mlx.serial_number])

# if using higher refresh rates yields a 'too many retries' exception,
# try decreasing this value to work with certain pi/camera combinations
mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ

frame = [0] * 768
while True:
    try:
        mlx.getFrame(frame)
    except ValueError:
        # these happen, no biggie - retry
        continue

    for h in range(24):
        for w in range(32):
            t = frame[h*32 + w]
            print("%0.1f, " % t, end="")
        print()
    print()

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

adafruit_circuitpython_mlx90640's People

Contributors

bliii avatar caternuson avatar dc-fgervais avatar dglaude avatar dhalbert avatar evaherrada avatar foamyguy avatar jepler avatar kattni avatar ladyada avatar lesamouraipourpre avatar prcutler avatar siddacious avatar sommersoft avatar tannewt avatar tcfranks avatar tekktrik avatar waspinator avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit_circuitpython_mlx90640's Issues

OSError: [Errno 5] Input/output error in raspberry pi 4

Hi. I try the sample code and modify it for measure process time
When it run 2833 sec after start ,I got the error 'OSError: [Errno 5] Input/output error '
And the second try to got the same error with 594 sec
Do you know how can I fix it?

here is my code:

import time
import board
import busio
import adafruit_mlx90640
t0=time.time()
i2c = busio.I2C(board.SCL, board.SDA, frequency=800000)

mlx = adafruit_mlx90640.MLX90640(i2c)
print("MLX addr detected on I2C", [hex(i) for i in mlx.serial_number])

# if using higher refresh rates yields a 'too many retries' exception,
# try decreasing this value to work with certain pi/camera combinations
mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ

frame = [0] * 768
while True:
    try:
        mlx.getFrame(frame)
    except ValueError:
        # these happen, no biggie - retry
        continue

    for h in range(24):
        for w in range(32):
            t = frame[h*32 + w]
    processtime = time.time()-t0
    print(round(frame[0],1))
    print('processtime: %d sec'%processtime)

and here is the error report:

Traceback (most recent call last):
File "", line 18, in
File "/usr/local/lib/python3.9/dist-packages/adafruit_mlx90640.py", line 126, in getFrame
status = self._GetFrameData(mlx90640Frame)
File "/usr/local/lib/python3.9/dist-packages/adafruit_mlx90640.py", line 140, in _GetFrameData
self._I2CReadWords(0x8000, statusRegister)
File "/usr/local/lib/python3.9/dist-packages/adafruit_mlx90640.py", line 828, in _I2CReadWords
i2c.write_then_readinto(
File "/usr/local/lib/python3.9/dist-packages/adafruit_bus_device/i2c_device.py", line 141, in write_then_readinto
self.i2c.writeto_then_readfrom(
File "/usr/local/lib/python3.9/dist-packages/busio.py", line 192, in writeto_then_readfrom
return self._i2c.writeto_then_readfrom(
File "/usr/local/lib/python3.9/dist-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 90, in writeto_then_readfrom
readin = self._i2c_bus.read_i2c_block_data(
File "/usr/local/lib/python3.9/dist-packages/Adafruit_PureIO/smbus.py", line 275, in read_i2c_block_data
ioctl(self._device.fileno(), I2C_RDWR, request)
OSError: [Errno 5] Input/output error

Missing Type Annotations

There are missing type annotations for some functions in this library.

The typing module does not exist on CircuitPython devices so the import needs to be wrapped in try/except to catch the error for missing import. There is an example of how that is done here:

try:
    from typing import List, Tuple
except ImportError:
    pass

Once imported the typing annotations for the argument type(s), and return type(s) can be added to the function signature. Here is an example of a function that has had this done already:

def wrap_text_to_pixels(
    string: str, max_width: int, font=None, indent0: str = "", indent1: str = ""
) -> List[str]:

If you are new to Git or Github we have a guide about contributing to our projects here: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

There is also a guide that covers our CI utilities and how to run them locally to ensure they will pass in Github Actions here: https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/check-your-code In particular the pages: Sharing docs on ReadTheDocs and Check your code with pre-commit contain the tools to install and commands to run locally to run the checks.

If you are attempting to resolve this issue and need help, you can post a comment on this issue and tag both @FoamyGuy and @kattni or reach out to us on Discord: https://adafru.it/discord in the #circuitpython-dev channel.

The following locations are reported by mypy to be missing type annotations:

  • adafruit_mlx90640.py:87
  • adafruit_mlx90640.py:110
  • adafruit_mlx90640.py:117
  • adafruit_mlx90640.py:133
  • adafruit_mlx90640.py:162
  • adafruit_mlx90640.py:178
  • adafruit_mlx90640.py:191
  • adafruit_mlx90640.py:771
  • adafruit_mlx90640.py:777
  • adafruit_mlx90640.py:790
  • adafruit_mlx90640.py:796
  • adafruit_mlx90640.py:813

Adafruit mlx90640_camtest.py example does a "black screen" in RPi4

The example from adafruit (mlx90640_camtest.py) crashes the RPi4 all the time! This means that when I run the example, the screen goes black and I need to shut down the RPi4. Don't even have time to get the error. The example mlx90640_simpletest.py and also mlx90640_pil.py works good though... any thoughts?

Why do i get value > 300 with function "getFrame" ?

Hi, I recently use your library to get data from the MLX90640.
When i get data with the function "getFrame" which say in description "...and calculate the temperature in C for each of 32x24 pixels...".
datasheet of MLX90640 say it can measure temperature from -40 to 300°C, so do you have any clue why i get lots of value > 300 ? (i am measuring a 3D printing process, max temperature should be around 200°)

Thanks in advance for your reply,
Jerevi.

Should use simpleio.map_range in mlx90640_pygamer example.

Talking with @CedarGroveStudios author of https://github.com/CedarGroveStudios/Thermal_Camera, I figured out that for mapping from temperature to colour palette index, I could use simpleio.map_range rather than a custom made function like I did:

This is not blocking, maybe just to put on my todo list, but anybody is welcomed to fix this before me.

The only additional feature that could make sense would be C° to F° conversion of the min and the max, that could be added. Anything else would be advance feature and not good for an example.

Any other code review is welcome.

i2c.write_then_readinto slows down frame rate after running the library for a couple seconds at 64Hz

I have been trying to get the sensor to run as close to 60hz as possible however I have recently run into a problem. After modifying the library a little bit using the code from chimajero in (#22), I have managed a frame rate of roughly 57 FPS, however, after a few seconds, this frame rate drops slowly down to the '30s. I narrowed the code down to the _I2CReadWrods function and the line, i2c.write_then_readinto(), taking the most time and slowing down the rest of the program. This can be seen in the Line Profiler data shown below.

I know next to nothing about the i2c bus or how to interact with it so if anyone has any idea what may be happening, or how to speed up this process, it would be greatly appreciated. If you need any more clarification or need the code for the slightly modified library I would be glad to provide it.

Thank you!

Edit: Another thing to note is that when the end value is changed in i2c.write_then_readinto(addrbuf, inbuf, in_end=read_words * 2) to a smaller value under 300 (I used 256 as its just 768/3, end=256) the frame rate stayed the same and did not lower leading me to believe the problem is the amount of data taken from the i2c bus. This is just my guess.

Line Profiler Data within the first 5 seconds.
before 5 seconds

Line Profiler Data after around 5 seconds has passed.
After 5 seconds

Unable to get pixels after some time until I reset program, using Raspberry Pi 3B+ (math domain error, More than 4 outlier pixels)

My Python code:

import board
import busio
import adafruit_mlx90640

class IR_Sensor():

      def get_pixels(self):
             try:
                  i2c = busio.I2C(board.SCL, board.SDA, frequency=400000)
                  mlx = adafruit_mlx90640.MLX90640(i2c)
                  mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_4_HZ
                  mlx.getFrame(pixels)
                  self.ir_connected = True
              except Exception as e:
                  self.ir_connected = False
                  pixels = None
                  self.controller.log_error('Could not read from IR Sensor: {}'.format(str(e)), ERR_IR_READING)

I have a SparkFun Qwiik IR Array MLX90640 attached to a Raspberry Pi 3B+ with 15 foot wiring. I have changed the I2C frequency in the Pi to 400,000 Hz. I have this code running in a continuous loop in a thread. The camera works, I'm able to read the pixels and display an image using its values. After some time (2 hours or so), mlx.getFrame() will fail and Exception e will show 'math domain error' and then 'More than 4 outlier pixels.' I can fix this simply by ending the program, then starting it again, and the camera will work again for some time, until I get this error again. I need to be able to have this program run for days/weeks at a time, and resetting the program every time getFrame() fails will not do.

I am unable to figure out if this is an issue with the camera, the Pi, or the code. I'm kind of hoping there is a way programmatically to mimic the actions of me restarting the program/camera without actually restarting.

I can also replicate the 'More than 4 outlier pixels' error by unplugging and plugging the camera back into the Pi. I can unplug and plug back in a handful of times, then after the 4th-8th plug in, the Exception occurs, fixed again by restarting the program.

Edit 1:

While re-reading my post, I may have come up with a programmatic solution that I'm going to test. Every time the exception occurs, I'm going to stop the thread that calls get_pixels(), and in my controller class (which keeps an instance of IR_Sensor) I'm going to assign a new instance of IR_Sensor.

Edit 2:

Edit 1 was not working how I expected, so in conjunction with Edit 1, I added the code below to reset I2C every time the exception occurs. Make sure I2C is currently not in use or the 'modprobe -r i2c_dev' command will not work. I don't think Edit 1 is necessary now, but I'm leaving it in because my program is working.

import os

       os.system('sudo modprobe -r i2c_dev')
       os.system('sudo modprobe -r i2c_bcm2708')

       os.system('sudo modprobe i2c_bcm2708')
       os.system('sudo modprobe i2c_dev')

I left my program on overnight and came back to the camera working correctly. In the span of 17 hours, getFrame() raised the exception 'math domain error' 9 times, once every 30 to 120 minutes. So the camera does not work properly for a few seconds once every 30 to 120 minutes. This is acceptable for my purposes, but I'd rather not have this happen at all.

Edit 3:

The previous method is no longer working and I cannot figure out why.

OSError: [Errno 5] Input/output error with Metro M7

Re this thread:
https://forums.adafruit.com/viewtopic.php?t=204722

Poster is using Airlift version.

Seeing same thing on non-Airlift (PID 5600) version:

Adafruit CircuitPython 8.2.6 on 2023-09-12; Metro MIMXRT1011 with IMXRT1011DAE5A
>>> import board
>>> import adafruit_mlx90640
>>> i2c = board.STEMMA_I2C()
>>> mlx = adafruit_mlx90640.MLX90640(i2c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_mlx90640.py", line 95, in __init__
  File "adafruit_mlx90640.py", line 839, in _I2CReadWords
OSError: [Errno 5] Input/output error
>>> 

More than 4 outlier pixels error

when I run the example on a Pi 4 I get this error

Traceback (most recent call last):
  File "test2.py", line 8, in <module>
    mlx = adafruit_mlx90640.MLX90640(i2c)
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_mlx90640.py", line 108, in __init__
    self._ExtractParameters()
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_mlx90640.py", line 370, in _ExtractParameters
    self._ExtractDeviatingPixels()
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_mlx90640.py", line 766, in _ExtractDeviatingPixels
    raise RuntimeError("More than 4 outlier pixels")
RuntimeError: More than 4 outlier pixels

MLX90641?

Is an update planned to support the MLX90641 sensor?

mlx.getFrame(frame) returns "math domain error"

Hello,

I am trying to run the basic example to stream data over i2c through the MCP2221. The Serial number is pulled from the MLC90640 easily but each time mlx.getFrame(frame) is called I get a "math domain error" if I print the exception. Any ideas as to why this may be happening? I am using Ubuntu 16.04 and python 3. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.