Problem with serial communication from arduino

I have made a human armature in blender which I’m controlling by python code and the serial data which is aqquired from arduino. The following are my blender-python code and arduino code respectively.


<b>import GameLogic
import time
import serial
from math import radians as r
arm=GameLogic.getCurrentController().owner
arm.channels['Upperarm.Right'].rotation_mode=1
port = serial.Serial('/dev/ttyACM2', 9600)
def upperarm():
    global x
    x= int(port.readline())
    arm.channels['Upperarm.Right'].rotation_euler=[r(x),r(26.639),r(0.576)]
    arm.update()</b>

  • i’m running the module upperarm in blender with an always sensor.


<b> double x=0;
void setup() {
    Serial.begin(9600);
    Serial.println("connected");
    
}

void loop() {
    while ( Serial.available()&gt;0)
    {x = Serial.parseInt();
     
       Serial.println(x);
Serial.flush();
      
       delay(2000);
       x=0;
    }
}</b>

MY Problem:
when i input some values in the serial monitor the armature is moving for the first value and then there is no change. I tried for many times but the same is repeating. The human armature is moving only for the first entered value in serial monitor. hoping somebody could help me out.
Thankyou

You need to enable True Level Triggering to trigger the Python controller at each frame.

Remarks:

  • Calling delay() adds lags to your game.
  • The loop makes your game stop unless there is no data from the communication.

yah…thats fine.
But still i’m getting a error when i run the game engine
ValueError: invalid literal for int() with base 10: b’0.00

could anyone help me out

I would recommend that you change your Arduino code to remove the delay() call. It should not be needed:


<b>void loop() {
     if ( Serial.available())
    {
        x = Serial.parseInt();
       Serial.println(x);
       Serial.flush();
    }
}</b>

This will loop forever and only read data is there is something on the serial port. The docs for ‘parseInt’ say that it should block to read in more data for up to 1s trying to find more characters.

Things to check for about why your code does not update:

  1. It looks like your computer is Linux or OSX from opening the ‘tty’ serial device. The Arduino docs say that ‘println’ sends "
    " for a line ending. Your Python ‘readline’ might be expecting just "
    "

  2. You have defined ‘double x’ in your arduino code but you are trying to process integer values when you receive the value in Blender. You should update your code so that the types are all being processed the same.

Those are starter observations. Other things to consider:

  • As far as I know, Arduino boards have one serial interface to communicate over and usually they only talk directly on that port. It seems a little odd that you can write to the Arduio from the serial console but when the Arduino writes, it goes to Blender. I’ve never tried a setup like that and it seems a bit shady. Perhaps wire a button to the Arduino (with de-bounce!) that triggers sending a set code from Arduino to the serial port. If your port sharing is an issue this might help find the problem.

  • Other good ways to debug are to remove the string conversions. Just have your Arduino send a single byte 128 every second to Blender and see if that byte is received correctly. Then add the string parsing and start debugging from there.

thnk you kastoria…for ur suggestion…i’ll improve my code.

Using .rstrip() on the string removes the end lines from the end of the string from all OS,
included.


variable = "stuff
".rstrip()  # leaves only "stuff"
 

‘device reports readiness to read but returned no data (device disconnected?)’)
I’m getting this error in my terminal when i run the game engine. please…anyone could help me…:no:

Which side of the serial connection are you getting that error? Are you still trying to get the Arduino to talk to BGE at the same time you are using the serial console (I’m assuming the one that is built into the Arduino editor)?

You should simplify your code as much as possible until you get your serial connection stable. I would recommend some steps like the following:

  1. Get your Arudino to just echo back what you sent to it using the serial console built in to Arduino editor.

Once you have that working correctly.

  1. Get your Arduino to echo back a number sent from BGE.

You should be aware that the Arduino resets itself every time a serial connection is opened. i.e. If your python code allocates a new Serial object each frame then your Arduino will constantly reset itself and probably won’t have time to run anything.

  1. Once you have a simple echo working, add on your sensor hardware to the Arduino and see if you can get that data passed in.

In general, double-and-triple check the settings in your serial connection to ensure that you are using the same settings on both sides.

It would be helpful if you posted your full .blend and Arduino code. You might also search though these formus because people have asked about passing rotations numbers between Blender and an Arduino board before.