1
$\begingroup$

I would like to read the data printed by an Arduino in Blender but the Arduino is reset each time Blender reads the data.
Here is my Blender code:

import bge
import mathutils
import serial

from math import pi
def Player():
cont = bge.logic.getCurrentController()
obj = cont.owner

serialport = serial.Serial('COM3', 115200)

# tell Arduino to send values
serialport.write('a'.encode('ascii'))

# read a line from the serial port
line=serialport.readline()

# Pi radians = 180 degrees so, since Arduino sends values between 0 and
# 255, we devide by 255 in order to spread the 255 over pi radians (from
# -90 to +90 degrees).


# There is one angle for each axis, and there are 3 axes.
Angles=[0.0,0.0,0.0]  # inicial angles for X, Y and Z axes
AnglesNumber=3       # number/quantity of angles/axes
for i in range(AnglesNumber):
     Angles[i] = Angles[i] + (float)(line.split()[i])
     # And we convert each value to radians (from -pi/2 to +pi/2).

mat_rot_z = mathutils.Matrix.Rotation(Angles[0], 3, 'X')
mat_rot_x = mathutils.Matrix.Rotation(Angles[1], 3, 'Y')
mat_rot_y = mathutils.Matrix.Rotation(Angles[2], 3, 'Z')

obj.localOrientation = mat_rot_x * mat_rot_y * mat_rot_z
$\endgroup$
  • $\begingroup$ First try to minimise the code. Your Arduino will probably not be reset by parsing text as floats or converting angles to matrices. By removing all but the relevant code it'll be easier for you and us to drill down to the real problem. $\endgroup$ – dr. Sybren Dec 7 '17 at 20:35
  • $\begingroup$ Related: Blender game controller using arduino not working properly $\endgroup$ – Strapicarus Dec 7 '17 at 23:04
  • $\begingroup$ I wonder if the problem does not come because the sensor true level triggering is activated, but if I deactivate it, i have to use a while loop only to read the given in a loop. but when i do that like this, blender don't work, so i don't know how to do... $\endgroup$ – evilcursed Dec 8 '17 at 1:05
1
$\begingroup$

You create a new Serial each time you call that script.

It is better to create it once and communicate with the connection as long as it is established.

Unfortunately I can't provide you with working code due to non-existence environment.

Connecting

One script gets executed and establishes the connection once:

serial = serial.Serial('COM3', 115200)
owner["serial"] = serial 

Communicating

A second script handles communication (polls for data) running at each single frame.

serial = owner["serial"]
serial.write('a'.encode('ascii'))
line = serial.readline()
...

Combined

Due to the strong dependency between connecting and communicating, you might want to combine both operations:

serial = owner.get("serial");
if not serial:
    serial = serial.Serial('COM3', 115200)
    owner["serial"] = serial  

serial.write('a'.encode('ascii'))
line = serial.readline()
...
$\endgroup$
  • $\begingroup$ I've test with if not like you said but that don't work :/ $\endgroup$ – evilcursed Dec 12 '17 at 20:33
  • $\begingroup$ What does not work? (you can discuss the topic at blenderartists.org) $\endgroup$ – Monster Dec 13 '17 at 7:27
  • $\begingroup$ When i run the bge, that don't do anything $\endgroup$ – evilcursed Dec 18 '17 at 20:49
  • $\begingroup$ Sorry for the late answer, but there were some holidays in-between. Can you add some print statements to check that the code gets executed? $\endgroup$ – Monster Jan 2 '18 at 13:05

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.