admin管理员组

文章数量:1406436

I have a project where I need to visualize the orientation of an IMU sensor as a 3D cube using VisPy, however, I keep getting the OSError: [Errno 5] Input/output error. I had another code using matplotlib to ensure the imu sensor was working and it worked for that. I have tried a lot of troubleshooting and I am thinking it could be a backend issue because I have never worked with vispy or backends. I will attach my code, please let me know what the issue could be.

import os
import time
import numpy as np
from vispy import app, gloo
from vispy.gloo import Program
from icm20948 import ICM20948

# **Force SDL2 Backend**
os.environ["VISPY_APP_BACKEND"] = "sdl2"

# Initialize IMU
imu = ICM20948()

# Initialize roll, pitch, yaw
roll, pitch, yaw = 0.0, 0.0, 0.0
prev_time = time.time()

# **VisPy Shader for Rendering a Cube**
vertex_shader = """
attribute vec3 a_position;
uniform mat4 u_model;
void main() {
    gl_Position = u_model * vec4(a_position, 1.0);
}
"""

fragment_shader = """
void main() {
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
"""

# **3D Cube Vertices**
cube_vertices = np.array([
    [-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1],
    [-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1]
], dtype=np.float32)

cube_indices = np.array([
    0, 1, 2, 2, 3, 0,
    1, 5, 6, 6, 2, 1,
    5, 4, 7, 7, 6, 5,
    4, 0, 3, 3, 7, 4,
    3, 2, 6, 6, 7, 3,
    4, 5, 1, 1, 0, 4
], dtype=np.uint32)

# **VisPy Visualization Class**
class IMUVisualizer(app.Canvas):
    def __init__(self):
        app.Canvas.__init__(self, title='IMU 3D Visualization (SDL2)', size=(600, 600))
        self.program = Program(vertex_shader, fragment_shader)

        # Attach buffers
        self.vertex_buffer = gloo.VertexBuffer(cube_vertices)
        self.index_buffer = gloo.IndexBuffer(cube_indices)
        self.program['a_position'] = self.vertex_buffer

        self.model_matrix = np.eye(4, dtype=np.float32)
        self.program['u_model'] = self.model_matrix

        self.timer = app.Timer(interval=0.05, connect=self.update_orientation, start=True)
        gloo.set_viewport(0, 0, *self.physical_size)
        self.show()

    def update_orientation(self, event):
        global roll, pitch, yaw, prev_time

        # Read IMU data (Gyroscope only)
        accel_gyro_data = imu.read_accelerometer_gyro_data()
        _, _, _, gyro_x, gyro_y, gyro_z = accel_gyro_data  # Extract gyro data

        # Get time difference (dt)
        current_time = time.time()
        dt = current_time - prev_time
        prev_time = current_time

        # Integrate gyroscope data to estimate orientation
        roll += gyro_x * dt
        pitch += gyro_y * dt
        yaw += gyro_z * dt

        # Print orientation values
        print(f"Roll: {roll:.2f}°, Pitch: {pitch:.2f}°, Yaw: {yaw:.2f}°")

        self.update()

    def on_draw(self, event):
        gloo.clear()
        self.program.draw('triangles', indices=self.index_buffer)

# **Run Visualization**
if __name__ == '__main__':
    canvas = IMUVisualizer()
    app.run()

'''

本文标签: