Hi everyone,
I built a balancing drone last summer using at STM32. Video here. It flies OK, but I want to fine tune the control loops and need some in-flight data of it to do so. I can't seem to get the STM32 to work with any Arduino SD library, so I have used an Arduino Nano to receive packets of data via I2c from the STM32 then dump them onto an SD card.
I started to notice some pretty strange behaviour, inside the drone code there is a section that sends data over I2C when a certain switch is moved on the controller.
/////////// i2c data trasnfer begins here...../////////
if (ch5 > 1300){
String dataString1 = String(OPStartT) + "," + String(compangleY) + "," + String(RollTarget) + "," + String(RollError)
+ "," + String(dE_Roll);
String dataString2 = "," + String(P_term_Roll) + "," +String(D_term_Roll) + "," + String(PID_Roll) + "," + String(PID_Roll_Avg);
//String dataString3 = + "," + String(flag)
dataString1.toCharArray(shortString1, 32);
dataString2.toCharArray(shortString2, 27);
Wire.beginTransmission(4); // transmit to device #4
Wire.write(shortString1); // sends one string
Wire.endTransmission();
Wire.beginTransmission(4); // transmit to device #4
Wire.write(shortString2); // sends second string
Wire.endTransmission(); // stop transmitting
}
/////////// i2c data trasnfer ends here...../////////
But I started to notice that even with the Arduino Nano not connected, when the data-logging switch was moved the propellers would start to spin on and off. The motors can spin for 2 reasons, you've added throttle, or the HDG PID loop has commanded it. (it yaws by differential thrust).
There is a statement which should inhibit the HDG PID value to '0' if the throttle is below a certain limit.
if (!Flight) PID_HDG = 0;
TV_Pitch.writeMicroseconds(1555 + PID_Pitch); //1555 uSec is mid-point of PWM signal
TV_Roll.writeMicroseconds(1400 + PID_Roll); //1400 uSec is mid-point of PWM signal
Motor1.writeMicroseconds(throttle - PID_HDG + 15); // Bottom motor (2), top propeller
Motor2.writeMicroseconds(throttle + PID_HDG - 40); // Top motor, bottom propeller
This code runs at 50Hz.
This bool value is called 'Flight' and only when the throttle PWM signal gets above 1350uS does it become 'True'.
if (ch3 >= 1350 && !Flight){ // Sets Current HDG as target
Flight = true;
HDG_Target = comp_True_HDG;
}
else if (ch3 < 1350){ // Test whether in flight or not
Flight = false;
}
This code above runs at 10Hz so I think that is why 'Flight' is getting reset to '0' every 4-5 cycles.
I did some tests and saw that this was in fact working normally with data-logging off but when data-logging is enabled the value of 'Flight' (bool) becomes erratic. (see photo)
Apart from initialising the variable as a bool these are the only instances 'Flight' is called.
How can a bool variable that isn't getting called change its value? Am I overloading the I2C bus or something? Is something corrupting inside the software? I'm really drawing a blank on it. All other functions of the drone seem to work apart from this.
The whole code is pretty long so not sure how much more people would like to see but happy to share as much as people need.
Any help or direction would be appreciated!