如何解决使用 obd2 elm327 和 pygame 制作数字仪表板,但是当将 obd2 放入代码中时屏幕是黑色的,obd 可以自行工作
我和一个朋友正在树莓派上制作数字仪表板,但在结合 obd 代码和 pygame 时,我们一直遇到问题。他们分开工作,但在一起是 pygame 或 obd 不起作用。本来我们尝试通过这种方式询问数据来做到这一点:
import obd
from obd import OBDStatus
connection=obd.OBD("/dev/rfcomm0")
obd.logger.setLevel(obd.logging.DEBUG)
print (obd.OBD(fast=False,timeout =30,protocol = 0))
print(OBDStatus.ELM_CONNECTED)
print(OBDStatus.OBD_CONNECTED)
print(OBDStatus.CAR_CONNECTED)
cmd=obd.commands.SPEED
response=connection.query(cmd)
print(response.value)
connection.close()
但是我们遇到了点火关闭的速度问题。现在我们从 obd 执行 watch 命令,现在 obd 部分工作,但 pygame 因它而停止工作。我们真的不知道出了什么问题,因为我们不再在调试屏幕中收到错误
这是组合代码:
import pygame
from pygame.locals import *
import obd
pygame.init()
def rotate(surface,angle,pivot,offset):
rotated_image = pygame.transform.rotozoom(surface,-angle,1)
rotated_offset = offset.rotate(angle)
rect = rotated_image.get_rect(center=pivot+rotated_offset)
return rotated_image,rect
def get_speed(s):
global speed
if not s.is_null():
speed = int(s.value.magnitude) #for kph
print(speed)
def get_rpm(r):
global rpm
if not r.is_null():
rpm = int(r.value.magnitude)
print(rpm)
def get_load(l):
global load
if not l.is_null():
load = int(l.value.magnitude)
def angle_up(angle):
angle += 1
return angle
def angle_down(angle):
angle -= 1
return angle
connection = obd.Async(fast=False)
connection.watch(obd.commands.SPEED,callback=get_speed)
connection.watch(obd.commands.RPM,callback=get_rpm)
connection.watch(obd.commands.ENGINE_LOAD,callback=get_load)
connection.start()
hoek1 = -225
hoek2 = -225
hoek3 = -226
speed = 0
rpm = 0
load = 0
SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1080
offset = pygame.math.Vector2(162.5,0)
center1 = int(SCREEN_WIDTH/5),int(SCREEN_HEIGHT/2)
center2 = int(SCREEN_WIDTH*(3/5)),int(SCREEN_HEIGHT/2)
center3 = SCREEN_WIDTH,int(SCREEN_HEIGHT/2)
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
clock = pygame.time.Clock()
running = True
Background = pygame.image.load('./graphics/Background.png').convert_alpha()
surf1 = pygame.image.load('./graphics/Wijzer.png').convert_alpha()
surf2 = pygame.image.load('./graphics/Wijzer.png').convert_alpha()
surf3 = pygame.image.load('./graphics/Wijzer.png').convert_alpha()
UPDATE =pygame.USEREVENT + 1
pygame.time.set_timer(UPDATE,250)
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
connection.stop()
running = False
elif event.type == QUIT:
connection.stop()
running = False
elif event.type == UPDATE:
if speed > hoek1:
hoek1 = angle_up(hoek1)
if speed < hoek1:
hoek1 = angle_down(hoek1)
if rpm > hoek2:
hoek2 = angle_up(hoek2)
if rpm < hoek2:
hoek2 = angle_down(hoek2)
if load > hoek3:
hoek3 = angle_up(hoek3)
if load < hoek3:
hoek3 = angle_down(hoek3)
if hoek1 > 45:
hoek1 = 45
if hoek2 > 45:
hoek2 = 45
if hoek3 > -135:
hoek3 = -135
if hoek1 < -225:
hoek1 = -225
if hoek2 < -225:
hoek2 = -225
if hoek3 < -225:
hoek3 = -225
surf1_rotated,surf_center1_rotated = rotate(surf1,hoek1,center1,offset)
surf2_rotated,surf_center2_rotated = rotate(surf2,hoek2,center2,offset)
surf3_rotated,surf_center3_rotated = rotate(surf3,hoek3,center3,offset)
screen.blit(Background,(0,0))
screen.blit(surf1_rotated,surf_center1_rotated)
screen.blit(surf2_rotated,surf_center2_rotated)
screen.blit(surf3_rotated,surf_center3_rotated)
pygame.display.flip()
clock.tick(30)
pygame.quit()
代码在这个github链接上: https://github.com/EGO-mercenary/Dashboard
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。