Untitled
unknown
python
2 years ago
2.9 kB
4
Indexable
from HIMUServer import HIMUServer import pandas as pd import turtle import numpy import serial # getting change in orientation from gyroscope value def get_o_i(gyro): return gyro * 0.1 # drawing the route wn = turtle.Screen() wn.bgcolor("light green") wn.title("Turtle") skk = turtle.Turtle() skk.shape('turtle') port = 'COM8' # Replace with the port name or device path baud_rate = 115200 # Replace with the appropriate baud rate # creating dataframe for collecting sensor data data = pd.DataFrame(columns=['acc_x', 'acc_y', 'acc_z', 'gyro_x', 'gyro_y', 'gyro_z']) # Open the serial connection ser = serial.Serial('COM8', baudrate=115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1) # Create an empty Pandas Series to store the distances distances = pd.Series(dtype=float) # Read data from the port while True: if ser.in_waiting > 0: line = ser.readline().decode().strip() # Parse the line and extract distance values if line.startswith("Distance"): distance_str = line.split("=")[1].strip() distance = float(distance_str) # Append the distance to the Pandas Series distances = distances.append(pd.Series(distance)) # Break the loop if Position line is encountered elif line.startswith("Position"): break # listener implementation for collecting accelerometer and gyroscope data class MyCustomListener: def __init__(self, server_instance): pass def notify(self, sensor_data): # Customize the notify method in order to elaborate data # sensorData contains String values (see HIMUServer.__extractSensorData()) for sensors in sensor_data: acc = HIMUServer.strings2Floats(sensors[0]) gyro = HIMUServer.strings2Floats(sensors[1]) row = [acc[0], acc[1], acc[2], gyro[0], gyro[1], gyro[2]] o_i = numpy.rad2deg(get_o_i(row[4])) # have moved turtle according to given data each 100ms skk.left(o_i) if len(data.index) > 1: d_i = distances[len(data.index) - 1] - distances[len(data.index) - 2] else: d_i = distances[len(data.index)] skk.forward(d_i) data.loc[len(data.index)] = row # HIMUServer instance: myHIMUServer = HIMUServer() # Creating listener and adding it to the server instance: myListener = MyCustomListener(myHIMUServer) myHIMUServer.addListener(myListener) # Change the timeout (in seconds) : myHIMUServer.timeout = 5 # Launch acquisition via UDP on port 2055: myHIMUServer.start("UDP", 2055) print(data) turtle.done() # Close the serial connection ser.close()
Editor is loading...