-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend.py
93 lines (75 loc) · 2.68 KB
/
frontend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import pygame, sys
from pygame.locals import *
from math import sin, cos, radians
from fuzzification import compute_current
pygame.init()
WINDOWSIZE = 800
TIMETICK = 20
BOBSIZE = 15
epsilon_theta = [3, 2, 5]
epsilon_omega = [2, 2, 4]
epsilon_curr = [2, 4, 8, 6, 10, 12]
window = pygame.display.set_mode((WINDOWSIZE, WINDOWSIZE))
pygame.display.set_caption("Inverted Pendulum Fuzzy Logic")
screen = pygame.display.get_surface()
screen.fill((255, 255, 255))
PIVOT = (WINDOWSIZE //2 , 9 * WINDOWSIZE // 10)
SWINGLENGTH = 320
class BobMass(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.theta = -1
self.dtheta = 1
# Rect(left, top, width, height)
self.rect = pygame.Rect(int(PIVOT[0] - SWINGLENGTH * cos(self.theta)),
int(PIVOT[1] - SWINGLENGTH * sin(self.theta)),
1, 1)
self.draw()
def recomputeAngle(self):
current = compute_current(
self.theta, self.dtheta, epsilon_theta, epsilon_omega, epsilon_curr)
theta_new = self.theta + self.dtheta / 10 + current / 200
omega_new = self.dtheta + current / 10
self.theta, self.dtheta = theta_new, omega_new
print(theta_new, omega_new)
# Rect(left, top, width, height)
self.rect = pygame.Rect(PIVOT[0] -
SWINGLENGTH * sin(self.theta),
PIVOT[1] -
SWINGLENGTH * cos(self.theta), 1, 1)
def draw(self):
# pygame.draw.circle(Surface, color, pos, radius, width=0)
# pos is a tuple of (x, y)
pygame.draw.circle(screen, (0, 0, 0), PIVOT, 5, 0)
pygame.draw.circle(screen, (0, 0, 0), self.rect.center, BOBSIZE, 0)
# aaline(Surface, color, startpos, endpos, blend=1)
pygame.draw.aaline(screen, (0, 0, 0), PIVOT, self.rect.center)
# line(Surface, color, start_pos, end_pos, width=1)
pygame.draw.line(screen, (0, 0, 0), (0, PIVOT[1]), (WINDOWSIZE, PIVOT[1]))
def update(self):
self.recomputeAngle()
screen.fill((255, 255, 255))
self.draw()
bob = BobMass()
clock = pygame.time.Clock()
TICK = USEREVENT
pygame.time.set_timer(TICK, TIMETICK)
def input(events):
for event in events:
if event.type == QUIT:
sys.exit(0)
elif event.type == TICK:
bob.update()
# flag = True
# while flag:
# for event in pygame.event.get():
# if event.type == QUIT:
# flag = False
# pygame.display.flip()
# clock.tick(60)
# bob.update()
#
while True:
# clock.tick(60)
input(pygame.event.get())
pygame.display.flip()