Guenter11 Beginner

  • Member since October 12, 2019
  • Last Activity: July 16, 2020 at 8:20 PM
Posts
13
Points
80
Profile Hits
325

Anfängerfrage ...

Raspberry 3

Python 3

TKinter

Ich starte 2 Threads in main.py:

BNO055 = ThreadBNO055()

gui = ThreadTinker()

ThreadBNO055() generiert neue Daten die im ThreadTinker verarbeitet werden sollten. Leider ist es so, dass im ThreadTinker die Funktion zum Verarbeiten der Werte aus ThreadBNO055() nur einmal aufgerufen werden. Sobald mainloop() im ThreadTinker() angestossen ist passiert da nichts mehr.

Frage:

Gibt es eine Möglichkeit, dass ThreadTinker() immer wieder durchlaufen wird?

Wie gesagt ich bin Anfänger .....

anbei der Code:

from tkinter import *

import time

import threading

import board

import busio

import adafruit_bno055

import RPi.GPIO as gpio


### Global Variables

next_call = time.time()

head = 0.0

roll = 0.0

pitch = 0.0

w_width = 320 # pixel width

w_height = 240 # pixel height


gpio.setmode(gpio.BCM)

gpio.setup(18, gpio.OUT)

gpio.output(18, gpio.HIGH)

i2c = busio.I2C(board.SCL, board.SDA)

sensor = adafruit_bno055.BNO055(i2c)


def ThreadBNO055():

global next_call, heading, roll, pitch # define the variables outside of the class

# print (datetime.datetime.now())

# get the data from the sensor via I2C

#head = float(sensor.euler[0])

roll = float(sensor.euler[1])

pitch = float(sensor.euler[2])

# print("Roll: %2.2f Head: %2.2f Pitch:%2.2f Grad" % (roll, head, pitch))

# print('Euler angle: {}'.format(sensor.euler))

# print ("Roll: %2.2f Head: %2.2f Pitch:%2.2f Grad" % (roll, head, pitch))

next_call = next_call+2 # 2 = 2 seconds, 0,1 = 100 ms

threading.Timer( next_call - time.time(), ThreadBNO055).start() # here start the thread when the timer will trigger

############################## end class ###########################

def calc_horizont(xAngle, yAngle, xStart, yStart, xEnd, yEnd):

x0 = 0

y0 = 0

x1 = 100

y1 = 100

yMiddle = ((yEnd-yStart)/2) + yAngle/(45/120)

xf0 = xStart

xf1 = xEnd

z = xAngle/(90/160)

yf0 = yMiddle + z

yf1 = yMiddle - z

# now convert to INT

x0 = int(xf0)

y0 = int(yf0)

x1 = int(xf1)

y1 = int(yf1)

return(x0, y0, x1, y1)

####################################################################


def ThreadThinker():

# create the main window

master = Tk()

# define the window

w = Canvas(master, width=w_width, height=w_height)

w.pack()

# create and draw the blue box

bluePoints =[0, 0, w_width, 0, w_width, int(w_height/2-1), 0, int(w_height/2-1)]

blueBox = w.create_polygon(bluePoints, fill='blue', width=0)

# create and draw the green box

greenPoints =[0, int(w_height/2+1), w_width, int(w_height/2+1), w_width, w_height, 0, w_height]

greenBox = w.create_polygon(greenPoints, fill='green', width=0)

# create and define horizont line

horizont = w.create_line(0, w_height/2, w_width, w_height/2, fill="#476042", width=3)

# create middle line

w.create_line(0, w_height/2, w_width, w_height/2, fill='red', width=5)

# calculate the new position xAngle, yAngle, xStart, yStart, xEnd, yEnd

(x0, y0, x1, y1)= calc_horizont(roll, pitch, 0, 0, w_width, w_height )

#draw the new blue box

bluePoints =[0, 0, w_width, 0, w_width, y1-1, 0, y0-1]

w.coords(blueBox, bluePoints)

# draw the new green box

greenPoints =[x0, y0+1, x1, y1+1, x1, w_height, x0, w_height]

w.coords(greenBox, greenPoints)

# draw the new horizont

w.coords(horizont, x0, y0, x1, y1)

# run forever ...

mainloop()

###############################################################

### start the threads

BNO055 = ThreadBNO055()

gui = ThreadThinker()


### console outputs for test and stay forever

while True:

time.sleep(0.5)

print ("Roll: %2.2f Head: %2.2f Pitch:%2.2f Grad" % (roll, head, pitch))

###############################################################