Self balancing robot keeps on falling down. (If you can provide code using mBlock, that would be greatly appreciated.)


We had to build one today because it was for our robotics class, and when we connected it to the app, it worked perfectly for a few rounds but it would start charging forward like a bull and then falling over.
It seems to have been automatically coded after we disconnected it from the app. The problem, though, is that it just keeps falling.
We then made then decided to give it one more test, and it appears to be able to balance itself for two minutes in a dimly lit area. When we presented it to our professor again, we used the same conditions, but it still fell.
We assembled it according to the instructions in the Makeblock video and made sure that no wires touched the wheels, so we’re not exactly sure what went wrong. Please help!

(P.S. : One of the wheels stiffens up after we test the robot; is this normal or could this be the issue?)


You might find the following observations of interest:





Could this code maybe work?

    #include <MeMegaPi.h>
    MeUltrasonicSensor ultrasonic(PORT_7);
    Me3AxisGyro gyro(PORT_6);
    Me3AxisAccelerometer acc(PORT_6);
    MeDCMotor leftMotor(PORT_1);
    MeDCMotor rightMotor(PORT_2);
    void setup() {
    void loop() {
      float distance = ultrasonic.distanceCm();
      if (distance < 10.0) { // if obstacle is too close, stop the robot;;
      int16_t angle = gyro.getAngleX();
      float error = (float) angle;
      float ax = acc.getAccX();
      float ay = acc.getAccY();
      float az = acc.getAccZ();
      float tiltAngle = atan2(ax, sqrt(ay*ay + az*az)) * 180.0 / M_PI;
      float Kp = 2.0; // proportional gain
      float Ki = 0.0; // integral gain
      float Kd = 0.0; // derivative gain
      if (tiltAngle > 5.0) { // robot is leaning forward
        Kp += 0.1;
        Ki += 0.01;
        Kd -= 0.01;
      } else if (tiltAngle < -5.0) { // robot is leaning backward
        Kp += 0.1;
        Ki += 0.01;
        Kd -= 0.01;
      static float integral = 0.0;
      static float previous_error = 0.0;
      float derivative = error - previous_error;
      integral += error;
      float output = Kp*error + Ki*integral + Kd*derivative;
      previous_error = error;
      if (output > 0) {;;
      } else {;;


Sorry, no time to test your code at the moment. Keep up the good work.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.