why object is rotating continiously?

i am rotating a object based on accelrometer value… but when the value from the sensor is not changing (constant) my object is keep on rotating why?


import bge
import serial

cont=bge.logic.getCurrentController()
obj=cont.owner
sens=cont.sensors["readSer"]
local=False
ser = serial.Serial(port='COM15',baudrate=9600, timeout=1)
accel=[None]*3
def readSer():
    if sens.positive:
        try:
            ser.write('a'.encode('ascii'))
            line=ser.readline()
            words=str(line,'UTF8')
            words=str.split(words,",")
            if len(words) >0:
                accel[0]=float(words[0])
                accel[1]=float(words[1])
                accel[2]=float(words[2])
                print(accel[0])
                print(accel[1])
                print(accel[2])
                obj.applyRotation(accel,local)
        except:
            print("exception")

Maybe you should post your question in the “Python Support” section - because the code experts are in this section …

I’m guessing that applyRotation applies a rotational force to the object? I found a page that said “Rotate the game object a set rotation (works like Motion Actuator Rot)” and another page that says that Motion Actuators set an object into motion. So if that’s the case I would expect it to keep spinning - Newton’s laws… an object continues in its current motion unless acted on by a force.

One, not involving GE, if this helps
http://www.pasteall.org/blend/40638

And this https://blenderartists.org/forum/showthread.php?281256-rotation_euler-or-bge-types-KX_GameObject-applyRotation-(Python-method-in-Game-Types

@others: @Rclub send me a PM, he is still unable to get it working (properly)…

I am no (skilled) python programmer at all but I tried my best by mixing the code of eppo and Rclub:
The script must be aborted by pressing CTRL+C in the console, otherwise it will run forever…

EDIT: My script is horrible. Stick to your concept (use GE) and see my next post…

import bpy
import serial
import time

# rotation function preparations
obj = bpy.data.scenes['Scene'].objects.active
local = False
accel = [0.0]*3

# accelerometer sensor preparations
ser = serial.Serial(port='COM15',baudrate=9600, timeout=1)


# change rot_scale that your accelerometer values rotate
# your object by max. 360 degrees
rot_scale = .0247


def applyRotation(ob, amount, space = False):
    ob.rotation_euler[:] = amount 
    if space:
        ob.convert_space(from_space='LOCAL', to_space='WORLD')
    bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=5)
    
def readSer():
        try:
            ser.write('a'.encode('ascii'))
            line=ser.readline()
            words=str(line,'UTF8')
            words=str.split(words,",")
            if len(words) >0:
                accel[0]=float(words[0]*rot_scale)
                accel[1]=float(words[1]*rot_scale)
                accel[2]=float(words[2]*rot_scale)
                
                # just for debugging
                print(accel[0])
                print(accel[1])
                print(accel[2])
                
                applyRotation(obj, accel, local)
        except:
            print("exception") 


# main function of the script, REPROGRAM!
while True:
    readSer()
    time.sleep(0.2) # delay for 0.2 seconds   

Since I haven’t your serial input I am unable to test the script…

Hope it works,

Luke

I Recapitulated your original problem; I think @Kauranga nailed it: applyRotation (probably) adds a constant rotational motion to your object, so you will have to “reset” it (by setting it to 0) when it is applied (otherwise it will rotate forever).
The following code is not ready to run but should clarify my idea (I used your script and added some lines):


import bge
import serial

cont=bge.logic.getCurrentController()
obj=cont.owner
sens=cont.sensors["readSer"]
local=False
ser = serial.Serial(port='COM15',baudrate=9600, timeout=1)
accel=[None]*3
<b>oldaccel=[None]*3</b>
def readSer():
    if sens.positive:
        try:
            ser.write('a'.encode('ascii'))
            line=ser.readline()
            words=str(line,'UTF8')
            words=str.split(words,",")
            if len(words) &gt;0:
                accel[0]=float(words[0])
                accel[1]=float(words[1])
                accel[2]=float(words[2])
                print(accel[0])
                print(accel[1])
                print(accel[2])
                obj.applyRotation(accel<b> - oldaccel</b>,local)
               <b> oldaccel = accel</b>
        except:
            print("exception")

So the script actually only applies the difference (current read minus read before) of the accelerometers rotation, which means that if there is no change it will apply the rotation “0”.

Hoping this post is more meaningful than the last one,

Luke