Arduino + Rasp Pi Communication

Good day all,

I’m here to seek for advice on how can read gyro data from arduino uno using Rasp Pi.

Here’s a brief idea on how the flow works:

I’m using YABR code for self balancing robot that runs perfectly when it’s arduino uno alone.
My aim is to read the gyro value from arduino uno on Rasp Pi WITHOUT interrupting the program stability.

What I’ve tried is to use Serial.print() the gyro value and read it on Python script on Rasp Pi via USB Serial, and the moment I start the script, the robot will lose it balance and fall (I assume the Serial.print() stops the entire program on arduino).

I’ve also tried without the Python script, only Serial.print() on Arduino IDE serial monitor but the robot do not behave as it suppose to be causing it to fall as well.

Here’s the YABR code in attachment (notice the Serial.print(angle_gyro) I’ve added on my own)

***I apologise for any dumb question asked, I gotta admit I’m new to coding

Balancing_robot.ino (24.7 KB)

Is this the statement (which you have in the loop) which causes the problem, even if you are not connecting to the Raspberry Pi but are simply using the serial monitor ?

Serial.println(angle_gyro,2 ) ;

If you try putting Serial.print() statements in the timer interrupt service routines, you could get erratic bahaviour.

What writes to the serial input of the Uno ? There is code to calibrate pitch and yaw depending on the serial input.

The code has this rather strange arrangement to make loop() repeat at 4 millisec intervals

//The angle calculations are tuned for a loop time of 4 milliseconds. To make sure every loop is exactly 4 milliseconds a wait loop
  //is created by setting the loop_timer variable to +4000 microseconds every loop.
  while(loop_timer > micros());
  loop_timer += 4000;

My guess is that your Serial.print() statement is the "straw that beaks the camel's back" and it delays loop() just long enough to bring its duration beyond 4000 msecs.

There is so much of the program in the loop() function that I can't see what might be the critical parts.

It might be worth searching for an alternative balancing program. I think @zhomesice posted one here

...R