Reputation: 11
I wanted to plot the raw sensor data like the accelerometer gyroscope and temperature data of MPU6050. For me MPU6050 is soldered with Pico and Pico is connected to my laptop via a USB connection.
Now, my problem is the frequency is quite low like 100 samples/second. I am using a serial library of python for this. And on Pico's side, I have used UART. I should get at least 1khz frequency as per the MPU6050 datasheet. But, I am seriously doing something wrong here. I am quite new and don’t have much clue how to increase the frequency in this case. Any help on this will be really helpful for me. Thanks in advance.
I am using baudrate 115200. I did some trial and error and find out that there is no problem between Pico and Python with pyserial. It’s like Pico is not able to read data from MPU6050 that fast because I am getting a lot of empty bytes with the use of serial.in_waiting(). I am uploading my code here. Just a bit of overview, my main.py uses two external modules imu.py and vector3d.py to read data. I am getting only 100 samples/second but It is a lot less. According to the MPU6050 datasheet, I should get at least 1khz frequency.
#PICO Code(main.py)
from imu import MPU6050
import utime
import time
from machine import Pin, I2C, UART
i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=400000)
imu = MPU6050(i2c)
uart = UART(0, baudrate=115200)
print("UART Info : ", uart)
while True:
ax = round(imu.accel.x, 2)
ay = round(imu.accel.y, 2)
az = round(imu.accel.z, 2)
gx = round(imu.gyro.x)
gy = round(imu.gyro.y)
gz = round(imu.gyro.z)
tem = round(imu.temperature, 2)
print(ax, ay, az, gx, gy, gz, tem)
#Python side code
import serial
import time
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
ser = serial.Serial('COM4', 115200)
print(ser)
file = open("serial_sample.csv", "w")
start_time = time.time()
curr_time = time.time()
output_list = []
header_string = "X_accel" + "," + "Y_accel" + "," + "Z_accel" + "," + "X_Pos" + "," + "Y_Pos" + "," + "Z_Pos" \
+ "," + "Temperature" + "\n"
output_list.append(header_string)
while curr_time - start_time < 10:
data = ser.readline()
data = str(data).split("\\")[0].split(" ")
output = data[0].split("'")[1] + "," + data[1] + "," + data[2] + "," + data[3] + "," + data[4] + ","
+\ data[5] + "," + data[6] + "\n"
output_list.append(output)
curr_time = time.time()
file.writelines(output_list)
file.close()
Upvotes: 1
Views: 584