滑雪时我的HUD代码存在问题。温度有 双字符串,零为空时的速度。这里有什么问题? 对于风速和温度读数,我使用现代电子设备的winpeed传感器,风传感器Rev C.
# 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()
答案 0 :(得分:0)
至于
温度有双串
我不知道你在说什么,但是
的原因零为空白时的速度
如果您在代码的这一部分velocity == 0
中明确表示不在屏幕上绘图:
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
也许你只想做
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