Formula Student Electronics & Software
The code for the embedded software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
calibration.py
Go to the documentation of this file.
1import serial
2import pygame
3from pygame.locals import *
4
5pygame.init()
6screen = pygame.display.set_mode((640, 480))
7pygame.display.set_caption("serial capture utility")
8screen.fill((159, 182, 205))
9centerpos = (screen.get_rect().centerx, screen.get_rect().centery)
10
11
12def display(str, center=centerpos, fontsize=40):
13 font = pygame.font.Font(None, fontsize)
14 text = font.render(str, True, (255, 255, 255), (159, 182, 205))
15 textRect = text.get_rect()
16 textRect.centerx = center[0]
17 textRect.centery = center[1]
18
19 screen.blit(text, textRect)
20 pygame.display.update()
21
22
23step = 0
24done = False
25lines_y = [y for y in range(125, screen.get_height() - 25, 25)]
26
27
29 a1, a2 = t1
30 b1, b2 = t2
31
32 r1 = abs(a1 - b1) / ((a1 + b1) / 2) * 100
33 r2 = abs(a2 - b2) / ((a2 + b2) / 2) * 100
34 return min(r1, r2)
35
36
37def process_and_save(data, filename, saved_points, n_saved):
38 display("Press ESC to exit", center=(centerpos[0], 25))
39 display("Auto-Saving at 10% deviation", center=(centerpos[0], 75))
40
41 display("Minimum percent deviation between", center=(centerpos[0] + 100, 150), fontsize=30)
42 display("saved and current values:", center=(centerpos[0] + 100, 200), fontsize=30)
43 if len(saved_points) > 0:
44 display(
45 str(round(min(percent_deviation(data, val) for val in saved_points), 2)) + "%",
46 center=(centerpos[0] + 100, 250),
47 fontsize=30,
48 )
49
50 display(
51 str(data[0]) + " " + str(data[1]), center=(centerpos[0] - 250, lines_y[n_saved % len(lines_y)]), fontsize=30
52 )
53
54 pygame.event.pump()
55 keys = pygame.key.get_pressed()
56
57 global done
58 if keys[K_ESCAPE]:
59 done = True
60
61 elif not len(saved_points) or (
62 min(percent_deviation(data, val) for val in saved_points) > 3 and data not in saved_points
63 ):
64 with open(filename, "a") as f:
65 f.write(str(data[0]) + " " + str(data[1]) + "\n")
66 display("Saved: ", center=(centerpos[0], 300), fontsize=30)
67 display(str(data[0]) + " " + str(data[1]), center=(centerpos[0] + 100, 300), fontsize=30)
68 display("Number of saved points: " + str(n_saved + 1), center=(centerpos[0], 350), fontsize=30)
69 saved_points.append(data)
70 n_saved += 1
71
72 return saved_points, n_saved
73
74
75with open("rawdata.txt", "r") as f:
76 saved_points = []
77 n_saved = 0
78 for line in f:
79 if done:
80 exit()
81 # print(line.split("\t")[0], line.split("\t")[1], n_saved)
82 data = (int(line.split("\t")[0]), int(line.split("\t")[1]))
83 saved_points, n_saved = process_and_save(data, "APPS_res.txt", saved_points, n_saved)
84
85# try:
86# ser = serial.Serial("/dev/ttyACM0", 9600)
87# except:
88# print("Error: Could not open serial port")
89
90# saved_points = []
91# n_saved = 0
92
93# while not done:
94# data = ser.readline().decode("utf-8").strip().split("\t")
95# data = (int(data[0]), int(data[1]))
96# with open("rawdata.txt", "a") as f:
97# print(data)
98# f.write(str(data[0]) + "\t" + str(data[1]) + "\n")
99# print(data)
100# saved_points, n_saved = main_loop(data, "APPS_res.txt", saved_points, n_saved)
percent_deviation(t1, t2)
process_and_save(data, filename, saved_points, n_saved)
display(str, center=centerpos, fontsize=40)