This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

2 wheel balancing robot

Other Parts Discussed in Thread: ENERGIA, TMP006

Hi every one,How are you doing?

I am working on two wheel balancing  robot,with MSP432 and ADXL335 ,can any one please forward me the code of 2 wheel balancing robot,and can you suggest me whether to go with PID and Gyroscope ,and can anyone let me know,how to add libraries to MSP432

   Thanks in advance

  • Hi Anvesh,

    I imagine you are referring to the following Instructable: www.instructables.com/.../

    As shown in step 5, the code was uploaded to Google Drive: drive.google.com/folderview

    It seems that most self-balancing robots use an Arduino board of some form: www.intorobotics.com/.../

    If you would like to use a MSP432 then you will need to port the existing Arduino code to Energia: http://energia.nu/guide/

    I imagine that a robust balancing robot solution will have no other choice but to incorporate PID control loop feedback with the gyroscope inputs available. Assuming that you would like to use Energia, libraries are added by placing them inside the hardware\msp430\libraries folder of the Energia install folder (energia-xxxxxxxxx) followed by selecting sketch -> Import Library inside of Energia.

    energia.nu/Tutorial_Library.html
    energia.nu/Libraries.html

    Regards,
    Ryan
  • Hi Anvesh,

    Take a look at this hackster.io project as well: www.hackster.io/.../wifi-enabled-balancing-bot-33dab1

    Cheers!
  • Hi thanks for your reply im just going with these code below, m struggling hard to implement PID controller code to my code
    you can have a look at my code
    //#include <Adafruit_TMP006.h>
    #include<Energia.h>
    // A0: self test, A1: z-axis, A2: y-axis, A3: x-axis, A4: ground, A5: vcc
    //const int groundpin = 18; // digital input pin 4 -- ground
    //const int powerpin = 19; // digital input pin 5 -- voltage
    const int xpin = 24; // x-axis of the accelerometer
    const int ypin = 25; // y-axis
    const int zpin = 26; // z-axis (only on 3-axis models)
    const float pi = 3.14;
    const int enableA=31;
    const int enableB=32;
    const int direction1=33;
    const int direction2=34;
    //float degreeint=86.6;
    float error=0;
    float errorB=0;
    //float degreeA=90.1;
    //float refdegree=91;
    float refdegree=90;
    float present;
    float presentB;

    //int ActPos = degree_x; // select the input pin for feedback signal
    int DesPos = 90; // select the input pin for control signal

    byte PWMOutput;
    //long Error[10];
    long Accumulator;
    long PID;
    int PTerm=10;
    int ITerm=50;
    int DTerm=30;
    byte Divider=10;




    //const int Voffset = 1.65;
    const float Sensitivity = 0.3;

    void setup()
    {
    Serial.begin(9600);
    //Serial.print(127);

    //pinMode(groundpin, OUTPUT); // Provide ground and power by using the digital inputs as normal
    //pinMode(powerpin, OUTPUT); // digital pins. This makes it possible to directly connect the
    // digitalWrite(groundpin, 0); // breakout board to the Arduino. If you use the normal 5V and
    // digitalWrite(powerpin, HIGH);
    pinMode(31,OUTPUT);
    pinMode(32,OUTPUT);
    pinMode(33,OUTPUT);
    pinMode(34,OUTPUT);
    // GND pins on the Arduino, you can remove these lines +
    analogWrite(enableA,0);
    analogWrite(enableB,0);
    }

    void loop()
    {
    float Vout_x = analogRead(xpin); // x axis /*do we need the step value here?*/
    float Vout_y = analogRead(ypin); // y axis
    float Vout_z = analogRead(zpin); // z axis

    float Vm_x = Vout_x * .003223; // Vmeasured for x axis
    float Vm_y = Vout_y * .003223; // Vmeasured for y axis
    float Vm_z = Vout_z * .003223; // Vmeasured for z axis

    float x= Vm_x - 1.64;
    float y= Vm_y - 1.62;
    float z= Vm_z - 1.98;

    Serial.print("X-axis ");
    Serial.print(digitalRead(xpin), DEC); // print the sensor values
    Serial.print("\t");
    Serial.print("Y-axis ");
    Serial.print(digitalRead(ypin), DEC);
    Serial.print("\t");
    Serial.print("Z-axis ");
    Serial.println(digitalRead(zpin), DEC);



    //delay(500); // delay before next reading


    float R_x = (x/(Sensitivity)); //radians
    float R_y = (y/(Sensitivity));
    float R_z = (z/(Sensitivity));

    Serial.print("x = ");
    Serial.println(x, DEC);
    Serial.print("y = ");
    Serial.println(y, DEC);
    Serial.print("z = ");
    Serial.println(z, DEC);



    float Angle_x = acos(R_x);
    float Angle_y = acos(R_y);
    float Angle_z = acos(R_z);

    float degree_x = (180/pi)*Angle_x;
    float degree_y = (180/pi)*Angle_y;
    float degree_z = (180/pi)*Angle_z;


    /* if(Angle_x<90.6)
    //digitalWrite(enableA,HIGH);
    //for(degree_x;degree_x>115;degree_x++)
    {
    digitalWrite(direction1,LOW);
    digitalWrite(direction2,HIGH);
    // digitalWrite(enable,HIGH);
    //delay(8 );
    digitalWrite(enableA,HIGH);
    digitalWrite(enableB,HIGH);
    delay(500);

    digitalWrite(enableA,LOW);
    digitalWrite(enableB,LOW);
    delay(1000);

    digitalWrite(direction2,LOW);
    // Serial.print("degreeA=");
    // Serial.println(degreeA,DEC);
    }
    */

    Serial.print("Angle_x = "); //Angle x in degrees
    Serial.println(degree_x, DEC);
    Serial.print("Angle_y = "); //Angle y in degrees
    Serial.println(degree_y, DEC);
    Serial.print("Angle_z = "); //Angle z in degrees
    Serial.println(degree_z, DEC);
    if(degree_x>refdegree)
    {
    // for(degree_x;degree_x>=refdegree;degree_x--)
    present=degree_x;

    error=present-refdegree;
    PID = error*PTerm; // start with proportional gain
    Accumulator += error; // accumulator is sum of errors
    PID += ITerm*Accumulator; // add integral gain and error accumulation
    PID += DTerm*(error); // differential gain comes next
    //PID = PID>>Divider;
    Serial.print("PID=");
    Serial.println(PID,DEC);
    for(error;error>=0;error--)
    {
    digitalWrite(direction1,LOW);
    digitalWrite(direction2,HIGH);
    // digitalWrite(enable,HIGH);
    //delay(8 );
    analogWrite(enableA,(PID));
    analogWrite(enableB,(PID));
    // delay(4);

    // Serial.print("error=");
    //Serial.println(error,DEC);
    // analogWriMte(enableA,0);
    // analogWrite(enableB,0);
    digitalWrite(direction2,LOW);
    digitalWrite(direction1,LOW);
    }
    analogWrite(enableA,0);
    analogWrite(enableB,0);

    }
    else
    {
    errorB=refdegree-degree_x;
    Serial.print("errorB=");
    Serial.println(errorB,DEC);
    Serial.print("presentB=");
    Serial.println(presentB,DEC);
    for(errorB;errorB>=0;errorB--)

    //for(error;error<=0;error++)
    {
    digitalWrite(direction1,HIGH);
    digitalWrite(direction2,LOW);
    analogWrite(enableB,(160));
    analogWrite(enableA,(160));
    delay(4);
    //digitalWrite(enableA,0);
    //delay(HIGH0);
    //analogWrite(enableB,0);
    // analogWrite(enableA,0);



    Serial.print("errorB=");
    Serial.println(errorB,DEC);

    }}
    //delay(300);
    analogWrite(enableB,0);
    analogWrite(enableA,0);
    digitalWrite(direction1,LOW);
    digitalWrite(direction2,LOW);

    //digitalWrite(enableA,LOW);
    //igitalWrite(enableB,LOW);
    }





    so what all the changes do you think i should make to balance my robot,
    and my robot is in horizontal chasis
  • No experience with PID controller project, so not sure how to help. Try to look at some of the examples posted before. You can also try posting a picture of your chassis so people can see what you are trying to do.

  • Hello Anvesh,

    I can really only help with your MSP430-related questions, you will have to find another avenue for help with your PID controller code.

    Regards,
    Ryan
  • >so what all the changes do you think i should make to balance my robot,
    "here's my code, please fix it" approach usually does not work. Instead please try to test and debug your hardware/software, provide results. Then ask question, not command us to work for you by fixing your code.

    p.s. I see that you use fixed point PID math and uncommented PID accumulator scaling code (PID>>Divider) at the end of the PID math. Why did you do that?! Such way you just totally destroy everything about intended fixed point PID math, PID accumulator goes skyhigh and no wonder it does not work.

    You would want to read literature mentioned in this post and this post first. I did find them by searching "PID" in this forum search engine.

**Attention** This is a public forum