Buffersize to small

38 views Asked by At

Hi!

The below problem we guess has something to do with the uart object buffersize, but not sure. We would be thankful for any insights as to how to solve the problem.

The problem appears in this line of code:

condition2 = 31 <= data_as_float <= 100

We are using a Raspberry Pi Pico, with:

  • ADXL345 accelerometer
  • Radar sensor (MR24HPB1)

If human movement is detected and a specific object is not moving, an action will be taken in the code.

This is the problem we are encountering:

MPY: soft reboot buffer too small Traceback (most recent call last): File "<stdin>", line 63, in <module> TypeError: unsupported types for le: 'int', 'NoneType'

Here is the code:

from machine import UART, Pin, I2C
from struct import unpack
from neopixel import NeoPixel


def low_pass_filter(prev_value, new_value, alpha):
    """This function applies a low pass filter """
    return alpha * prev_value + (1 - alpha) * new_value

def get_data_as_float(data_as_hex):
    """This function takes a hexadecimal string and returns it as a float"""

    try:
        byte_array = bytes.fromhex(data_as_hex)
        float_value = unpack('f', byte_array)[0]
        return float_value

    except Exception as e:
        print(e)


# Constants
ADXL345_ADDRESS = 0x53
ADXL345_POWER_CTL = 0x2D
ADXL345_DATA_FORMAT = 0x31
ADXL345_DATAX0 = 0x32

# Instantiate sensors
uart = UART(1, 9600, bits=8, parity=None, stop=1, tx=Pin(4), rx=Pin(5))
i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
i2c.writeto_mem(ADXL345_ADDRESS, ADXL345_POWER_CTL, bytearray([0x08]))
i2c.writeto_mem(ADXL345_ADDRESS, ADXL345_DATA_FORMAT, bytearray([0x0B]))
led = NeoPixel(Pin("GP0"), 8)

# Variables for low pass
start_value = 0
alpha = 0.99

# Thresholds for accelerometer
x_thresh, y_thresh, z_thresh = 0.05, 0.5, 2.5

while True:
    # Get and process message
    message = uart.read()
    if message is not None:
        message = message.hex()
        function_code = message[6:8]
        address1 = message[8:10]
        address2 = message[10:12]
        data = message[12:-4]
        data_as_float = get_data_as_float(data)
        
        # Get and process xyz
        xyz = i2c.readfrom_mem(ADXL345_ADDRESS, ADXL345_DATAX0, 6)
        x, y, z = unpack('<3h', xyz)
        x, y, z = abs(x), abs(y), abs(z)
        filtered_x = low_pass_filter(start_value, x, alpha)
        filtered_y = low_pass_filter(start_value, y, alpha)
        filtered_z = low_pass_filter(start_value, z, alpha)
        
        # Conditions
        condition1 = function_code == "04" and address1 == "03" and address2 == "06"
        condition2 = 31 <= data_as_float <= 100
        condition3 = x < x_thresh and y < y_thresh and z < z_thresh
        
        if condition1 and condition2 and condition3:
            print("Yay")

We tried to increase txbuf and rxbuf parameters when initializing the radar sensor, but to no effect, same problem.

We are new to working this close to the hardware and it's a bit complicated for us at the moment.

0

There are 0 answers