PID Library Issues


hi guys,

i current attempting use pid library in order make self balancing robot, running issues in regards changing output limits , settime using functions given on library reference.

my code attached below. trying change limits -255,255 pwm in order run motors , forward , change read times every 10 milliseconds.
the code below:

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);
          }
}

code: [select]
 setoutputlimits (-255,255);
  setsampletime (10);

should these be
code: [select]

  mypid.setoutputlimits (-255,255);
  mypid.setsampletime (10);


Arduino Forum > Using Arduino > Programming Questions > PID Library Issues


arduino

Comments