Self Balancing Robot with PID Control


hi guys,

currently working on pid control based system allow robot self balance. however, having issues , relatively new arduino, let alone coding. current list of parts using include:

- tamaya double gear box 2 dc motors polulu
- tamaya wheel set polulu
- axdl203eb - dual-axis accelometer
- arduino uno
- adafruit motorshield v2

the main issue having getting robot balance , whether or not coding correct in case. have read lot in regards brett's pid library, kas' robots, instructables, , fourm, still confused on why doesn't balance. have encased components within grey box mounted on side of robot, makes sorta heavy on 1 side..., think coding may real issue? have attached code below interested in helping me project.

i tried using pid library attempt this, ran few problems in regards setting read speed , output limits. tried using functions wouldn't let me in code , gave me error code.

code: [select]
/********************************************************
 * pid basic example
 * reading analog input 0 control analog pwm output 3
 ********************************************************/

#include <pid_v1.h>
#include <wire.h> //define wire library
#include <adafruit_motorshield.h> //define adafruit motorshield library

//define variables we'll connecting to
double setpoint, input, output;

//specify links , initial tuning parameters
pid mypid(&input, &output, &setpoint,1,0,0, direct);

//specifiy motor control parameters
adafruit_motorshield afms = adafruit_motorshield(); //define motorshield object
adafruit_dcmotor *mymotor = afms.getmotor (1); //define motor 1
adafruit_dcmotor *mymotor2 = afms.getmotor (2); //define motor 2

///bias parameters
float vb = 0;
int samplenum = 500;

void setup()
{
serial.begin(115200);

//////////////////// motor controller ///////////////////

//turn motorcontroller on
afms.begin(); //turn on motorshield
mymotor->setspeed(0);
mymotor2->setspeed(0);

/////////////////////////  pid ///////////////////////

//initialize variables we're linked to
  input = analogread(0);
  setpoint = 0;

  //turn pid on
  mypid.setmode(automatic);
 
  setoutputlimits (-255,255);
  setsampletime (10);

/////////////////////// vb (counts) ///////////////////////////

//500 samples of readings vb
 for(int n=0;n<samplenum;n++)
 {
    float accel_x = analogread(a0);
     vb += accel_x;
  }
 
vb = vb/samplenum;

delay(1000);

}

void loop()
{
  input = analogread(0);
  input = input - vb;
  input = input * 0.00488758553;
  input = input / 0.0159;
  mypid.compute();
  float wer = analogread(0);
 
    if ( output > 0)
      {
        mymotor->setspeed(abs(output));
        mymotor2->setspeed(abs(output));
        mymotor->run(backward);
        mymotor2->run(backward);
      }
        else if (output < 0)
          {
              mymotor->setspeed(abs(output));
              mymotor2->setspeed(abs(output));
              mymotor->run(forward);
              mymotor2->run(forward);
          }
}

try putting "mypid." before name "setoutputlimits" knows pid talking about. same "setsampletime".

you can leave part out:
code: [select]
  input = input * 0.00488758553;
  input = input / 0.0159;

the pid work on un-scaled values.


Arduino Forum > Using Arduino > Project Guidance > Self Balancing Robot with PID Control


arduino

Comments