Having Trouble with PicoSDK in Python

Post your Python discussions here.
Post Reply
semtex_willi
Newbie
Posts: 0
Joined: Tue Oct 01, 2024 8:49 am

Having Trouble with PicoSDK in Python

Post by semtex_willi »

I have a problem with the PicoLog 1216. I want to read the values from the PicoLog with Python with a sampling rate of about 250Hz and write them to a csv file, but I have the problem that the values always change, although they should not change
Here is the code:

Code: Select all

import ctypes
import time
import csv
import os
from picosdk.pl1000 import pl1000 as pl
from picosdk.functions import adc2mVpl1000, assert_pico_ok

output_dir = "output"
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

current_time = int(time.time() * 1000)
csv_filename = os.path.join(output_dir, f"{current_time}.csv")

def convert_voltage_to_g(voltage):
    return (voltage - 1.44) / (2.4 - 1.44)

def calculate_sampling_rate(start_time, num_samples):
    elapsed_time = (time.time() - start_time) * 1000
    return num_samples / (elapsed_time / 1000) if elapsed_time > 0 else 0

def update_data():
    try:
        chandle = ctypes.c_int16()
        status = {}
        status["openUnit"] = pl.pl1000OpenUnit(ctypes.byref(chandle))
        assert_pico_ok(status["openUnit"])
        print(f"Gerät geöffnet: {chandle.value}")

        usForBlock = ctypes.c_uint32(5000)
        noOfValues = ctypes.c_uint32(40)

        channels = (ctypes.c_int16 * 3)(1, 2, 3)

        status["setInterval"] = pl.pl1000SetInterval(chandle, ctypes.byref(usForBlock), noOfValues, channels, len(channels))
        assert_pico_ok(status["setInterval"])

        mode = pl.PL1000_BLOCK_METHOD["BM_SINGLE"]

        with open(csv_filename, mode="w", newline='') as file:
            writer = csv.writer(file)
            writer.writerow(["timestamp", "x", "y", "z", "on_position"])

            start_time = time.time()
            end_time = start_time + 120
            num_samples = 0

            while time.time() < end_time:
                status["run"] = pl.pl1000Run(chandle, noOfValues.value, mode)
                assert_pico_ok(status["run"])

                time.sleep(usForBlock.value * noOfValues.value / 1000000)

                values = (ctypes.c_uint16 * (noOfValues.value * 4))()
                overflow = ctypes.c_uint16()

                status["getValues"] = pl.pl1000GetValues(chandle, ctypes.byref(values), ctypes.byref(noOfValues), ctypes.byref(overflow), None)
                assert_pico_ok(status["getValues"])

                maxADC = ctypes.c_uint16()
                status["maxValue"] = pl.pl1000MaxValue(chandle, ctypes.byref(maxADC))
                assert_pico_ok(status["maxValue"])

                inputRange = 2500
                mVValues = adc2mVpl1000(values, inputRange, maxADC)

                block_start_time = time.time()
                timestamp_interval = (usForBlock.value / 1000) / noOfValues.value

                for i in range(noOfValues.value):
                    measurement_time = block_start_time + (i * timestamp_interval)
                    timestamp = int(measurement_time * 1000)

                    voltage_x = mVValues[i * 4] / 1000.0
                    voltage_y = mVValues[i * 4 + 1] / 1000.0
                    voltage_z = mVValues[i * 4 + 2] / 1000.0
                    voltage_on_position = mVValues[i * 4 + 3] / 1000.0

                    g_x = convert_voltage_to_g(voltage_x)
                    g_y = convert_voltage_to_g(voltage_y)
                    g_z = convert_voltage_to_g(voltage_z)

                    writer.writerow([timestamp, g_x, g_y, g_z, voltage_on_position])
                    num_samples += 1

        status["closeUnit"] = pl.pl1000CloseUnit(chandle)
        assert_pico_ok(status["closeUnit"])

        sampling_rate = calculate_sampling_rate(start_time, num_samples)
        print(f"Sampling Rate: {sampling_rate:.2f} Hz")

    except Exception as e:
        print(f"Ein Fehler ist aufgetreten: {e}")


if __name__ == "__main__":
    update_data()
When I execute the code, everything is written normally to a csv file but when I take a closer look at the csv file I realize that there are deviations that should not actually be there that cause problems when I want to display it in a diagram.

The diagram looks like this when I export it.
Image
Attachments
1727773278852_acceleration.png
Post Reply