2016-12-18 6 views
0

Ich hatte ein Problem mit diesem Code für ein HUD beim Skifahren. Die Temperatur hat eine doppelte Zeichenfolge und die Geschwindigkeit, wenn Null leer ist. Was ist das Problem hier? Für Wind und Temperaturmessung, ich bin mit einem winspeed Sensor durch moderne Elektronik, Windsensor Rev C.pygame strings verdoppeln und flackern

# coding=UTF-8 

import pygame 
import sys 
import datetime 
import math 
import numpy 
import thread 
import RPi.GPIO as GPIO 
import time 
from adxl345 import ADXL345 
import Adafruit_GPIO.SPI as SPI 
from picamera import PiCamera 
import Adafruit_MCP3008 
import random 
volts = 0 
CLK = 18 
MISO = 20 
MOSI = 21 
CS = 8 
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI) 
temp=0 
angle = 0 
velocity = 0   







def draw_borders(screen): 
    #Draws some simple borders to the display"" 
    pygame.draw.lines(screen, (255, 255, 255), False, [(0, 30), (width, 30)], 2) 

def draw_time(screen): 
    #Draws the time to the display"" 
    the_time = datetime.datetime.now() 
    time_as_string = the_time.strftime('%H:%M') 
    font = pygame.font.Font(None, 42) 
    text = font.render(time_as_string, 1, (255, 255, 255)) 
    textpos = text.get_rect(centerx=screen.get_width()/2, centery=15) 
    screen.blit(text, textpos) # paste the text into the background 

def draw_speed(screen): 
    #Draws the speed to the display"" 
    # get the speed... 
    values = [0]*8 
    for i in range(8): 
     values[i] = mcp.read_adc(i) 
     #Get's channels 
     CHANNEL_ZERO = '{0:>4}'.format(*values) #Voltage out 
     CHANNEL_ONE = '{1:>4}'.format(*values) #Raw voltage 
     CHANNEL_TWO = '{2:>4}'.format(*values) 
     #Overflow Channels 
     CHANNEL_THREE = '{3:>4}'.format(*values) 
     CHANNEL_FOUR = '{4:>4}'.format(*values) 
     CHANNEL_FIVE = '{5:>4}'.format(*values) 
     CHANNEL_SIX = '{6:>4}'.format(*values) 
     CHANNEL_SEVEN = '{7:>4}'.format(*values) 
     #Makes channels integers to divide later 
     CHANNEL_ZERO = int(CHANNEL_ZERO) 
     CHANNEL_ONE = int(CHANNEL_ONE) 
     CHANNEL_TWO = int(CHANNEL_TWO) 
     thermVolts = CHANNEL_TWO *(3.3/1023) 
     #Temperature affects voltage, -1 degree celcius is a .5 drop in voltage 
     #If temp was 20 degrees celcius, therm = .5 
     therm = (30 * thermVolts) - 0 
     #Fahrenheight to celcius 
     therm = round(therm, 0) 
     volts = CHANNEL_ONE *(3.3/1023) #+ therm 
     #Temp is based on a 21 degree celcius temp, multiply accordingly 
     temp = (4.246*volts-9.3442) 
     #Get's volts data 
     speed = math.exp(temp) 
     #Get's velocity 
     kph = (speed * 1.609) 
     velocity = (kph * .278) 
     velocity = abs(round(velocity)) 
     the_speed_string = '{} mps'.format(int(velocity)) # display as whole number 
     #Wipes second string 
     if velocity == 0: 
      the_speed_string = 0 
     else: 
      font = pygame.font.Font(None, 160) 
      text = font.render(the_speed_string, 1, (255, 255, 255)) 
      textpos = text.get_rect(centerx=screen.get_width()/2, centery=screen.get_height()/2) 
      screen.blit(text, textpos) # paste the text into the background 
     #GIVES TEMPERATURE 
     the_temp_string = u'{}°F'.format(int(therm)) 
     if int(therm) == 0: 
      the_temp_string = 'ERROR' 
     else: 
      font = pygame.font.Font(None, 65) 
      text = font.render(the_temp_string, 1, (255, 255, 255)) 
      textpos = text.get_rect(centerx=screen.get_width()-80, centery=screen.get_height()-20) 
      screen.blit(text, textpos) # paste the text into the background 


def draw_altitude(screen): 
    accel = ADXL345() 
    axes1 = accel.getAxes(True) 
    x = axes1['x'] 
    z = axes1['z'] 
    y = axes1['y'] 
    #Get's 3D angle 
    angle = numpy.angle(x+y+z+1j, deg=True) 
    the_altitude_string = u'{} °'.format(round(angle,0)) 
    font = pygame.font.Font(None, 65) 
    text = font.render(the_altitude_string, 1, (255, 255, 255)) 
    textpos = text.get_rect(centerx=120, centery=screen.get_height()-20) 
    screen.blit(text, textpos) # paste the text into the background 




def draw_temp(screen): 
    #""Draws the temperature to the display 
    the_temp = 0 
    the_temp_string = u'{}°C'.format(the_temp) 
    font = pygame.font.Font(None, 65) 
    text = font.render(the_temp_string, 1, (255, 255, 255)) 
    textpos = text.get_rect(centerx=screen.get_width()-80, centery=screen.get_height()-20) 
    screen.blit(text, textpos) # paste the text into the background 





if __name__ == '__main__': 
    pygame.init() 
    size = width, height = 650, 400 
    black = 0, 0, 0 
    screen = pygame.display.set_mode(size) 
    while True: 
     # Checks for key presses, e.g. escape to quit 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: sys.exit() 
      if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE: sys.exit() 
     # Fill the screen with black background 
     screen.fill(black) 
     # Draw all the stuff 
     draw_borders(screen) 
     draw_time(screen) 
     draw_speed(screen) 
     draw_altitude(screen) 
     draw_temp(screen) 
     # Update the display 
     pygame.display.flip() 

Antwort

0

Was

Die Temperatur einen Doppel-String

I don hat Ich weiß nicht wovon du sprichst, aber der Grund dafür ist

der Veloci ty, wenn Null leer ist

ist, dass Sie ausdrücklich, wenn velocity == 0 etwas auf dem Bildschirm zeichnen nicht in diesem Teil des Codes:

if velocity == 0: 
     the_speed_string = 0 
    else: 
     font = pygame.font.Font(None, 160) 
     text = font.render(the_speed_string, 1, (255, 255, 255)) 
     textpos = text.get_rect(centerx=screen.get_width()/2, centery=screen.get_height()/2) 
     screen.blit(text, textpos) # paste the text into the background 

Vielleicht möchten Sie gerade

if velocity == 0: 
    the_speed_string = 0 

font = pygame.font.Font(None, 160) 
text = font.render(the_speed_string, 1, (255, 255, 255)) 
textpos = text.get_rect(centerx=screen.get_width()/2, centery=screen.get_height()/2) 
screen.blit(text, textpos) # paste the text into the background 
tun
+0

Ich habe das versucht, aber es flackerte immer noch, und mit doppelter Saite meine ich, dass eine Saite permanent 0 ist und von der richtigen überlagert wird. – Will4cat