extracting_graphing_printed_data.py 2.14 KB
Newer Older
ChristineC8's avatar
ChristineC8 committed
1
2
3
4
5
6
7
8
9
from matplotlib import pyplot as plt
import numpy as np

# Takes in a text file and string that labels the floating points that you would like to find
# Returns a list of all the floating point data coming after the label string
def extract_data(text_file, string_label) :

    # Set up
    f = open(text_file, "r")
10
    data_list = []
ChristineC8's avatar
ChristineC8 committed
11
12
13
14
15
16
17
18
19
20
21
22
    label_size = len(string_label)

    # Read each line from the file and extract data if present
    while True:
        # Get next line from file
        line = f.readline()

        # If line is empty
        # End of file is reached
        if not line:
            break

23
24
        # Find data cycle data
        data_index = line.find(string_label)
ChristineC8's avatar
ChristineC8 committed
25
        
26
27
        
    # If found, extract data
ChristineC8's avatar
ChristineC8 committed
28
    # Our floats are printed with 5 decimal places
29
        if data_index != -1 :
ChristineC8's avatar
ChristineC8 committed
30
            # Go to actual index of the data rather than the label
31
            float_start_index = data_index + label_size
ChristineC8's avatar
ChristineC8 committed
32
33
34
35
36
37
38
39
40

            # Find decimal point index and add 5 decimal points to find size of floating point number
            float_size = line[float_start_index:].find(".") + 5

            # Add floating point data into list
            data_point = float(line[float_start_index: float_start_index + float_size + 1])
            if data_point > 0.8 :
                print(line)
                print(data_point)
41
                print(data_index)
42
            data_list.append(float(line[float_start_index: float_start_index + float_size + 1]))
ChristineC8's avatar
ChristineC8 committed
43
44
45
46

    
    f.close()
    
47
    return data_list
ChristineC8's avatar
ChristineC8 committed
48

49
# Plots line graph given y data
ChristineC8's avatar
ChristineC8 committed
50
51
52
53
54
55
56
def plot_data(y_data, x_label, y_label, title) :
    plt.plot(y_data)
    plt.xlabel(x_label)
    plt.ylabel(y_label)
    plt.title(title)
    plt.show()  
    
57
# Main :
58
59
# find and print data
text_file = "/Users/cc/Documents/School/ee185/software/firmware/circuitpython-main/ports/atmel-samd/common-hal/motor_control/PID_testing/pid_scripts/pid_KI_0.005_KP_0.005.txt"
60
61
data_label = "duty_left: "
data_list = extract_data(text_file, data_label)
ChristineC8's avatar
ChristineC8 committed
62
63
title = "Calculated PID Left Wing Duty Cycles: KI = 0.04375 & KP = 0.00625"
y_title = "Calculated Left Wing Duty Cycle"
64
plot_data(data_list[0:10000], "Time Interval (20 ms)", y_title, title)
ChristineC8's avatar
ChristineC8 committed
65