PicoScope 7 Software
Available on Windows, Mac and Linux
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()