I have an unusual bug with the code I have written for my rover V2 robot.
I have a raspberry pi running some computer vision code written in opencv tracking an object doing the heavy processing.
Once the object is detected, a command is sent via serial (USB) to make the robot move left, right, forwards etc.. The pi sends: w, a or d for the direction it wants the rover to move, it then turns off it's ability to send any further commands via a flag and will turn this back on when the rover sends a response confirming it has completed the command.
The idea is, eventually the rover v2 will be able to do some audio processing itself so it can turn and face the direction of a sound impulse but always prioritize any incoming serial data, sending the more accurate movement commands.
So my idea has been to implement an interrupt routine that is called upon receiving a command using the less than perfect approach suggested here.
So now for the weird part.. the code works entirely... until the motors have power!
If the Rover V2 motor power is entirely disabled the interrupt routine will work flawlessly. The Raspberry Pi will send a command and receive every reply from the Rover.
However, if the Rover V2 Motors are powered on, the Rover will receive all commands and send replies accordingly. The issue is for some reason, the Raspberry Pi will occasionally not receive a reply rendering the robot stationary as no more commands can be sent, this happens after varying periods of time all the way from 2 successful command interrupts to 100.
The fact that this occurs only when power is applied to the motors on the Arduino, is leading me to believe it is not a fault in the Pi code.
Why is it, that as soon as the power is applied to the motors there is inconsistency in the transmission of serial replies?
Does anybody have any suggestions on how I could further diagnose the cause of this fault?
Or is there any obvious issues in this code I have not seen?
Thanks in advance!
Code can be viewed HERE