ameyer27
Posts: 1
Joined: Fri May 24, 2019 3:25 am

No errors but the window doesn't even open.

Fri May 24, 2019 3:35 am

Hello,
I am coding a display for a formula car team I work on and have hit a snag. My code on my pi compiled well enough and when it wasn't wired into the car's CAN network it said the network was down but I was still able to make it open the window and all my gauges would read 0. However once that display was moved off my TV and into the car's 4" LCD screen and it was wired into the CAN network the board was able to pull data from the network no problem and the code compiles with no errors but the window that contained my gauges would no longer open, the terminal would read everything it was supposed to for a successful connection to the network but no window with gauges appear. My code is below and the dimensions of my screen according to the data sheet it 800x480.
Thanks!

Code: Select all

#!/usr/bin/env python3
from tkinter import *
import tkinter as tk
from tkinter.font import Font
from tkinter import messagebox
import time
import random
import gaugelib
import can
import RPi.GPIO as GPIO
import os
import queue
from threading import Thread

COOL_data = 0x8CFFF548
RPM_data = 0x8CFFF048
SHAFT_data = 0x6FA020A

print('\n\rLets Go Racing!')
print('\n\rBring up CAN0...')

#bring up can interface at 500mbps
os.system("sudo /sbin/ip link set can0 up type can bitrate 500000")
time.sleep(0.1)
print('\n\rReady')

try:
    bus = can.interface.Bus(channel='can0', bustype='socketcan_native')
except OSError:
    print('cannot find CAN.')
    exit()
        
def can_rx_task():   #receive thread
    while True:
        message = bus.recv()
        if message.arbitration_id == COOL_data or message.arbitration_id == RPM_data:
            q.put(message)
            
q = queue.Queue()
rx = Thread(target = can_rx_task)
rx.start()

win = tk.Tk()
#a5 = PhotoImage(file="g1.png")  these two lines caused errors but the code compiles and displayed without them
#win.tk.call('wm', 'iconphoto', win._w, a5)
win.title("Cyclone Racing")
win.geometry("400x240+60+6")
win.resizable(width=True, height=True)
win.configure(bg='black')

g_value=0
rpm=0
ratio = 0
gear = 0
shaft = 0
coolant = 0
x=0

def read_every_second():
    global x
    message = q.get()
    
    if message.arbitration_id == RPM_data:
        rpm = message.data[1]*256 + message.data[0]
        p1.set_value(int(rpm))
        
    if message.arbitration_id == COOL_data:
        coolant= message.data[5]*256+message.data[4]
        if coolant >32767:
            coolant = (cool-65536)/10
        p2.set_value(int(coolant))
        
    if message.arbitration_id ==  SHAFT_data:   
        shaft=message.data[1]
        ratio = rpm/shaft
        p3.set_value(float(ratio))
        
    x+=1    
    if x>100:
#        graph1.draw_axes()
        x=0
    win.after(100, read_every_second)

p1 = gaugelib.DrawGauge2(
    win,
    max_value=20000,
    min_value=0,
    size=300,
    bg_col='black',
    unit = "Tach. RPM",bg_sel = 2)
p1.pack()
#p2 = gaugelib.DrawGauge2( edited out because this will be used in the future but not immediately for the first race
#    win,
#    max_value=5,
#    min_value= 0,
#    size=150,
#    bg_col='black',
#    unit = "Gear",bg_sel = 2)
#p2.pack(side = RIGHT)

p3 = gaugelib.DrawGauge3(
    win,
    max_value=200.0,
    min_value= 0.0,
    size=150,
    bg_col='black',
    unit = "Temp. F",bg_sel = 1)
p3.pack(side = LEFT)


read_every_second()
mainloop()

ghp
Posts: 1325
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: No errors but the window doesn't even open.

Fri May 24, 2019 8:02 pm

My formula car is currently not available to test this piece of software.
Hence a proposal to make the code testable.

Code: Select all

# some code cut off from the start
print('\n\rLets Go Racing!')
print('\n\rBring up CAN0...')

#bring up can interface at 500mbps
time.sleep(0.1)
print('\n\rReady')

class Message:
    def __init__(self):
        # just produce some data
        self.arbitration_id = COOL_data
        self.data=[1,2,3,4,5,6,7,8]
        
class CanReader:
    def __init__(self, queue_):
        self.queue_ = queue_
        try:
            self.bus = can.interface.Bus(channel='can0', bustype='socketcan_native')
        except OSError:
            print('cannot find CAN.')
            exit()
        self.rx = Thread(target = self.can_rx_task)
        self.rx.start()
           
    def can_rx_task(self):   #receive thread
        while True:
            message = self.bus.recv()
            if message.arbitration_id == COOL_data or message.arbitration_id == RPM_data:
                self.queue_.put(message)
                
class CanReaderTEst:
    def __init__(self, queue_):
        self.queue_ = queue_
        print("CAN TEST harness")
        self.rx = Thread(target = self.can_rx_task)
        self.rx.start()
           
    def can_rx_task(self):   #receive thread
        while True:
            time.sleep(0.1)
            self.queue_.put(Message() )
            
q = queue.Queue()

if False:
    CanReader(q)
else:
    CanReaderTEst(q)

win = tk.Tk()
# and the other code goes here
The point is to replace the real can reader by a mock implementation which produces some fake data.
This code is not tested. From where do you install the gauge library ?

And I think there is a problem at "coolant = (cool-65536)/10", cool is not available. And running a loop before the mainloop looks strange.

Return to “Python”