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


#1

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?)


#2

You might find the following observations of interest:

balance%20(a)

balance%20(b)

balance%20(c)


#3

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() {
      Serial.begin(115200);
      ultrasonic.begin();
      gyro.begin();
      acc.begin();
      leftMotor.run(0);
      rightMotor.run(0);
    }
    
    void loop() {
      float distance = ultrasonic.distanceCm();
      if (distance < 10.0) { // if obstacle is too close, stop the robot
        leftMotor.run(0);
        rightMotor.run(0);
        delay(500);
        return;
      }
    
      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) {
        leftMotor.run(-output);
        rightMotor.run(output);
      } else {
        leftMotor.run(-output);
        rightMotor.run(output);
      }
    }

#4

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


#5

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