Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4829

Python • Re: PID controller help

$
0
0
I use Pygame. Doesn't require the registering for a key.
I simulated the sensor reading since I don't have one.

Code:

#!/usr/bin/env python3import pygameimport os,sys,threadingfrom gpiozero import PWMLEDfrom time import sleepled = PWMLED(12)duty_cycle = 0.0;done = FalseT1_running = Falsewhite = (192,192,192)black = (0,0,0)blue = (128,128,235)orange = (255,192,0)green = (128,255,128)size = 300,200target_temp = 50current_temp = 50pygame.init()screen = pygame.display.set_mode(size)background = pygame.Surface(screen.get_size())background.fill((16,16,16))font = pygame.font.Font(None,24)arrowfont = pygame.font.Font(None,28)pageup_box = pygame.Rect(210, 10, 80, 60)pagedown_box = pygame.Rect(10, 10, 80, 60)quit_box = pygame.Rect(110, 10, 80, 60)pu_surface = arrowfont.render("UP",True,blue)pd_surface = arrowfont.render("DOWN",True,blue)quit_surface = arrowfont.render("QUIT",True,blue)pygame.display.set_caption("RPi PWM")pygame.mouse.set_visible(1)def update_screen():   screen.fill((16,16,16))   pygame.draw.rect(screen,blue,pageup_box,4)   screen.blit(pu_surface,(pageup_box.x + 20,pageup_box.y + 20))   pygame.draw.rect(screen,blue,pagedown_box,4)   screen.blit(pd_surface,(pagedown_box.x + 8,pagedown_box.y + 20))   pygame.draw.rect(screen,blue,quit_box,4)   screen.blit(quit_surface,(quit_box.x + 12,quit_box.y + 20))   page_display = "Temp: %02d    Thermostat: %02d" % (current_temp,target_temp)   page_display_surface = font.render(page_display,True,white)   screen.blit(page_display_surface,(60,90))   pygame.display.flip()def thread1_function():  global done,T1_running,target_temp, current_temp  current_temp = 20    T1_running = True  while not done:    # do your sensor read here setting current_temp    current_temp +=1    if current_temp > 99:      current_temp = 0          # do your PID calcs here using sensor value and target_temp value    if current_temp < target_temp - 10:      duty_cycle = 100    elif current_temp < target_temp - 5:      duty_cycle = 25    elif current_temp >= target_temp:      duty_cycle = 0    led.value = duty_cycle/100.0    sleep(0.5)           T1_running = Falseth = threading.Thread(target=thread1_function)th.start()while not done:   for event in pygame.event.get():      if event.type == pygame.QUIT:          done = True         continue      if event.type ==pygame.MOUSEBUTTONDOWN:         if pageup_box.collidepoint(event.pos):            if target_temp < 100:              target_temp = target_temp + 1                       elif pagedown_box.collidepoint(event.pos):            if target_temp > 0:              target_temp = target_temp - 1         elif quit_box.collidepoint(event.pos):            done = True            continue                      elif event.type == pygame.KEYDOWN:         if event.key == pygame.K_ESCAPE:            done = True            continue   update_screen()     sleep(0.1)print("Shutting down")while T1_running:  sleep(0.1)print("done")

Statistics: Posted by SurferTim — Mon Oct 21, 2024 2:25 am



Viewing all articles
Browse latest Browse all 4829

Trending Articles