0
$\begingroup$

I want to run a thread in the background of the BGE, that retrieves accelerometer data from a LoPy, and uses the values to rotate the object.

For now, the code I have can only receive and print the data in the console, however the thread itself only runs when I exit the blender game engine. This is my code:

print("---------------------------------------------")
print("Begin collection")

import serial, threading, keyboard

port = 'COM5'

def acc_data(ser):
    while True:
        if keyboard.is_pressed(' '): 
            print('The script was interrupted!')
            break
        raw = ser.readline().decode("utf-8")
        data = raw.split(",")
        try:
            pitch = float(data[0])
            roll = float(data[1])
            print(str(pitch) + " " + str(roll))
        except:
            print("Only one value read")


def acc_thread():
    acc_data(serial.Serial(port, 115200))


try:
    thread = threading.Thread()
    thread.run = acc_thread
    thread.start()
except:
    print("Unable to connect")

print("End collection")

This is the logic editor:

enter image description here

blender console print data enter image description here

$\endgroup$
  • $\begingroup$ Can you place an image of how this prints when you leave BGE? $\endgroup$ – user58715 Aug 23 at 21:52
  • $\begingroup$ @BlenderBlackened here is the image $\endgroup$ – Miguel Andrade Aug 24 at 22:20
  • $\begingroup$ Ok, it's showing on the blender console, and you want this to be displayed somewhere on your screen when you're in BGE? you have considered using UPBGE, the game engine is much developed, it is the same but it is more focused on the game engine $\endgroup$ – user58715 Aug 24 at 22:43
  • $\begingroup$ Do you have a HUD in your project? $\endgroup$ – user58715 Aug 24 at 22:50
  • $\begingroup$ Try this tutorial, maybe it will give you a better idea and help you understand a couple of concepts Blender Tutorial Car Racing Game - Setting Up a Head-Up Display (HUD) and Using New Dynamic Text $\endgroup$ – user58715 Aug 24 at 23:23
1
$\begingroup$

I found out that you can make the script run in a loop by doing this:

enter image description here

Which allowed me to remove the thread and the infinite loop from my code:

print("---------------------------------------------")
print("Begin collection")

import serial

port = 'COM5'

def acc_data(ser):
    raw = ser.readline().decode("utf-8")
    data = raw.split(",")
    try:
        pitch = float(data[0])
        roll = float(data[1])
        print(str(pitch) + " " + str(roll))
    except:
        print("Only one value read")


try:
    acc_data(serial.Serial(port, 115200))
except:
    print("Unable to connect")


print("End collection")

This is what is printed in the console:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.