import serial import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np # Open the serial port. Replace 'COM3' with your serial port name. serial_port = '/dev/ttyUSB0' baud_rate = 115200 # Adjust as per your device configuration ser = serial.Serial(serial_port, baud_rate, timeout=1) # Setup the matplotlib figure and axes for polar plot. fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) line, = ax.plot([], [], 'b-', lw=2) # Line object to update the angle ax.set_ylim(0, 1) # Set the radius range (fixed in this case) # Adjust the orientation so that 0 is up and the angle proceeds clockwise. ax.set_theta_zero_location('N') ax.set_theta_direction(-1) # Initialization function to clear the data. def init(): line.set_data([], []) return (line,) # Update function that reads from the serial port # and updates the plot with the new angle. def update(frame): line.set_data([], []) raw_data = ser.readline().strip() angle = 0 if raw_data: try: angle = float(raw_data) except: angle = 0 angle_rad = np.deg2rad(angle) # Convert to radians # Update the plot with the new angle. line.set_data([angle_rad, angle_rad], [0, 1]) return (line,) # Create the animation object with a suitable update interval (ms). ani = animation.FuncAnimation(fig, update, init_func=init, blit=True, interval=1) # Start the plot animation plt.show() # Closing the serial port after the plot is closed. ser.close()