Making Self Balancing Robot Software Part

Hey guys, welcome back in my new makelog. Few days ago I decided to make a self balancing robot. To make a self balancing robot we need to prepare software part & hardware part. So here I'm preparing Software part of my robot my robot. You can also call a brain of it. Because we will use this pcb to provide commands to our robot.

 

Required Components

ESP32

MPU6050 

Capacitor

Header Pins

Screw Terminal

Professional PCB 

Stepper Motor Driver 

 

Step 1: PCB Design

Start by finalizing my PCB design using mine preferred design software. Once satisfied, I saved design files in Gerber formats. For designing a PCB I recommend Kicad Because it's preferred for beginner.

I designed this PCB according to me needs;) it has some additional features like I can control two Nema17 stepper motor one Servo Motor and even a Buzzer & LEDs wirelessly, using my smartphone.

 

Step 2: Order PCB After Successfully designing of PCB. I ordered my coustom PCBs from PCBWAY. Head to PCBWAY's website. If you don't have an account, sign up, it's quick and easy. Sign up PCBWAY now to get a US $5 coupon. That means your first order is free of cost only you have to pay the shipping charges. After Sign Up Upload your Gerber files and specify your requirements. Choose the quantity, PCB thickness, and color to match your project needs.

 

Step 3: Solder Components After Receiving PCB, Solder some remaining components like Header Pins, Buzzer, Capacitor & Screw Terminal. After the process of soldering use a IPA to clean the PCB and provide a professional look. For better soldering, I recommend to use Sequre SI012 Pro Soldering Iron, Because I love it have a lot of features and powerful Also.

 

Step 4: Place the Components After Soldering Process, place the necessary components like stepper motor driver, ESP32, MPU6050, etc.

 

Step 5: Upload the Code To upload the code you doesn't need laptop or PC. Because you can even program it using your smartphone. For this go to (Play Store/ App Store) and search for Bluino Electronics and download the application. After Successfully download, open it and click on (Sketch/Code). Here you will see a option to upload the code you can use your smartphone to directly upload firmware to ESP32 either via USB or Wifi OTA, by pressing the upload icon.

If you need to edit the sketch with computer using Arduino IDE, you can find full source code on github

Step 6: Ready to use Now it's ready to use, In my next makelog, I will make a self balancing robot using this PCB. So stay tuned for next one until enjoy it.

CODE
      setMotorSpeedM2(0);
      PID_errorSum = 0;  // Reset PID I term
      Kp = KP_RAISEUP;   // CONTROL GAINS FOR RAISE UP
      Kd = KD_RAISEUP;
      Kp_thr = KP_THROTTLE_RAISEUP;
      Ki_thr = KI_THROTTLE_RAISEUP;
      // RESET steps
      steps1 = 0;
      steps2 = 0;
      positionControlMode = false;
      OSCmove_mode = false;
      throttle = 0;
      steering = 0;
    }
    
    // Push1 Move servo arm
    if (OSCpush[0]) {
      if (angle_adjusted > -40)
        ledcWrite(6, SERVO_MAX_PULSEWIDTH);
      else
        ledcWrite(6, SERVO_MIN_PULSEWIDTH);
    } else
      ledcWrite(6, SERVO_AUX_NEUTRO);

    // Servo2
    //ledcWrite(6, SERVO2_NEUTRO + (OSCfader[2] - 0.5) * SERVO2_RANGE);

    // Normal condition?
    if ((angle_adjusted < 56) && (angle_adjusted > -56)) {
      Kp = Kp_user;            // Default user control gains
      Kd = Kd_user;
      Kp_thr = Kp_thr_user;
      Ki_thr = Ki_thr_user;
    } else // We are in the raise up procedure => we use special control parameters
    {
      Kp = KP_RAISEUP;         // CONTROL GAINS FOR RAISE UP
      Kd = KD_RAISEUP;
      Kp_thr = KP_THROTTLE_RAISEUP;
      Ki_thr = KI_THROTTLE_RAISEUP;
    }

  } // End of new IMU data

}



void processOSCMsg() {
  if (OSCpage == 1) {
    if (modifing_control_parameters)  // We came from the settings screen
    {
      OSCfader[0] = 0.5; // default neutral values
      OSCfader[1] = 0.5; // default neutral values
      OSCtoggle[0] = 0;  // Normal mode
      mode = 0;
      modifing_control_parameters = false;
    }

    if (OSCmove_mode) {
      Serial.print("M ");
      Serial.print(OSCmove_speed);
      Serial.print(" ");
      Serial.print(OSCmove_steps1);
      Serial.print(",");
      Serial.println(OSCmove_steps2);
      positionControlMode = true;
      OSCmove_mode = false;
      target_steps1 = steps1 + OSCmove_steps1;
      target_steps2 = steps2 + OSCmove_steps2;
    } else {
      positionControlMode = false;
      throttle = (OSCfader[0] - 0.5) * max_throttle;
      // We add some exponential on steering to smooth the center band
      steering = OSCfader[1] - 0.5;
      if (steering > 0)
        steering = (steering * steering + 0.5 * steering) * max_steering;
      else
        steering = (-steering * steering + 0.5 * steering) * max_steering;
    }

    if ((mode == 0) && (OSCtoggle[0])) {
      // Change to PRO mode
      max_throttle = MAX_THROTTLE_PRO;
      max_steering = MAX_STEERING_PRO;
      max_target_angle = MAX_TARGET_ANGLE_PRO;
      mode = 1;
    }
    if ((mode == 1) && (OSCtoggle[0] == 0)) {
      // Change to NORMAL mode
      max_throttle = MAX_THROTTLE;
      max_steering = MAX_STEERING;
      max_target_angle = MAX_TARGET_ANGLE;
      mode = 0;
    }
  } else if (OSCpage == 2) { // OSC page 2
    if (!modifing_control_parameters) {
      for (uint8_t i = 0; i < 4; i++)
        OSCfader[i] = 0.5;
      OSCtoggle[0] = 0;

      modifing_control_parameters = true;
      //OSC_MsgSend("$P2", 4);
    }
    // User could adjust KP, KD, KP_THROTTLE and KI_THROTTLE (fadder3,4,5,6)
    // Now we need to adjust all the parameters all the times because we dont know what parameter has been moved
    Kp_user = KP * 2 * OSCfader[0];
    Kd_user = KD * 2 * OSCfader[1];
    Kp_thr_user = KP_THROTTLE * 2 * OSCfader[2];
    Ki_thr_user = KI_THROTTLE * 2 * OSCfader[3];
    // Send a special telemetry message with the new parameters
    char auxS[50];
    sprintf(auxS, "$tP,%d,%d,%d,%d", int(Kp_user * 1000), int(Kd_user * 1000), int(Kp_thr_user * 1000), int(Ki_thr_user * 1000));
    //OSC_MsgSend(auxS, 50);


    // Calibration mode??
    if (OSCpush[2] == 1) {
      Serial.print("Calibration MODE ");
      angle_offset = angle_adjusted_filtered;
      Serial.println(angle_offset);
    }

    // Kill robot => Sleep
    while (OSCtoggle[0] == 1) {
      //Reset external parameters
      PID_errorSum = 0;
      timer_old = millis();
      setMotorSpeedM1(0);
      setMotorSpeedM2(0);
      digitalWrite(PIN_ENABLE_MOTORS, HIGH);  // Disable motors
    }
  }
}
License
All Rights
Reserved
licensBg
0