views:

36

answers:

2

m using arduino to interact the accelerometer MMA7361L with blender2.49.using python 2.62. my arduino code is :

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.print(analogRead(0)/4, BYTE);    //x-axis
  Serial.print(analogRead(1)/4, BYTE);    //y-axis
  Serial.print(analogRead(2)/4, BYTE);    //z-axis
  delay(40);
}

my python code is:

import Blender
import GameLogic
import serial

serialport = serial.Serial('COM4', 9600)
ob = Blender.Object.Get ('Cube')
cont=GameLogic.getCurrentController()
own = cont.owner
rotxx = move.dRot[0]
rotyy = move.dRot[1]
rotzz = move.dRot[2]
move = cont.actuators["move"]
Blender.Window.WaitCursor(1)
x=[0,0,0]
for i in range(1, 100):
    x1[0] = serialport.read(size=1)
    x1[1] = serialport.read(size=1)
    x1[2] = serialport.read(size=1)

    x2=[(((m - n)*1.65)/180) for m,n in zip(x,x1)]
    rotxx = rotxx + x2[0]
    rotyy = rotyy + x2[1]
    rotzz = rotzz + x2[2]
    move.dRot=(rotxx,rotyy,rotzz)
    cont.activate(move)
    x1=x

else:
    serialport.close()
    Blender.Window.WaitCursor(0)

have made the sensor actuator connection.. no errors shown in implementation but blender cube shows no movement.

A: 

In your python code, you only read the sensor values coming over a serial port 100 times, which is 4 seconds worth of sensor data at 40 ms per update (according to your processing code). You need to constantly read the sensor values and update scene orientation, so use a while loop like this:

read_sensors = True
while(read_sensors):
    # call serialport.read() to read current sensor values
    # update scene

Make sure that this can run at least every 40 milliseconds, since that's how often you're having arduino board write accelerometer values to the serial port. If you don't read data from the serial port often enough it will buffer, causing values appearing in serialport.read() to lag behind current accelerometer values that arduino board is reading. That won't look very interactive.

vls
A: 

I am not sure if you are incurring in the same problem I was when I made a python <-> arduino application, yet...

By default, when a serial connection is opened on the USB port, arduino will auto reset itself. Since the reboot it is not instant, your python programme will most likely complete its 100 times read cycle before arduino even starts writing.

If that is the problem you can either:

  1. Insert time.sleep(3) (or whatever values works for you) after opening the serial connection.
  2. Stick a 120 ohm resistor between RESET and 5V pins on your board, as described here (this will deactivate the auto-reset feature).

HTH!

mac
To elaborate, the RS232 control lines can be used to pull reset low, resetting the MCU. On reset, the MCU enters bootloader mode for a few seconds to see if it will receive a new binary to flash with. Connecting RESET to VCC with a low resistor prevents the control-line from pulling down the RESET voltage to the threshold.
Nick T