Pages: 1 [2]   Go Down
Author Topic: Pendulum Balance Robot  (Read 1763 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 5
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm pretty sure you need to use an interrupt
attach the encoder to pins 2 and 4
Code:
Encoder encoder(2, 4);

        void setup() {
            attachInterrupt(0, doEncoder, CHANGE);
            Serial.begin (9600);
            Serial.println("start");
        }

        void loop(){
            // do some stuff here - the joy of interrupts is that they take care of themselves
        }

        void doEncoder(){
            encoder.update();
            Serial.println( encoder.getPosition() );
        }
Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks like you are using a different encoder library.  Seems to be several out there.  Just try using the encoder on different interrupt pins.

Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also try putting this bit in the setup
Code:
myEnc.write(-512);
You may be setting the encoder faster than the interrupt can update, also you won't get far from -512 as it is incremental and you are basically resetting it during the loop.

edit: and why -512 and not 0
« Last Edit: March 13, 2013, 12:19:08 pm by laadams85 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,  I have disabled the line setting the encoder at -512,  and now the encoder reads the value correctly.  smiley-grin Unfortunately the motor isnt working,  the output from the PID is still constant , despite the Position changing

Any thoughts,  I think its the PID causing the issues now,  as the encoder reads correctly,  and motor moves when an step is put in to it.


heres the code
Code:
#include <PID_v1.h>
#include <Encoder.h>
#include <Stepper.h>
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
const int STEPS = 200;
double m = 0;
double newPosition  = 0;
double Position = 0;
int chanA = 20;
int chanB = 21;
Stepper myStepper(STEPS, dirA, dirB);
Encoder myEnc(chanA, chanB);
PID myPID(&Position, &m, 0, 1, 20, 100, DIRECT);
void setup() {
  myStepper.setSpeed(90);
  Serial.begin(9600);
pinMode (pwmA, OUTPUT);
  digitalWrite(pwmA, HIGH);
  pinMode(pwmB, OUTPUT);
  digitalWrite(pwmB, HIGH);
  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW);
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW);
}

long oldPosition  = -999;
void loop() {
  newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    }
    Position = ((newPosition/11.37)+180);
    if (Position > 180) {
    Position = Position -360;
    }
    if (Position < -180) {
    Position = Position +360;
    }
//Serial.println(Position);
  myPID.Compute();
  myStepper.step(int(m));
  Serial.println(int(&m));
 
}

Just for referance,  the  PID wants the motor to move 536 steps no matter the actual location, I'm not sure why.
« Last Edit: March 18, 2013, 11:05:12 am by Yewzorz » Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm glad that you got the encoder to work properly.  To get the PID to work you are going to have to tune the PID.  There are several ways to do this, but I'll borrow from wikipedia for the easiest way.
-Set the I and D terms to zero.
-Increase the P term until the system oscillates.
Then set your values based on the table found here
http://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method
I would also suggest printing out the double value of m as well as the integer value of m.  Maybe it is changing but not enough to make a difference.
Code:
Serial.println(int(&m));
Serial.println(&m);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,  unfortuantely I'm still having no luck,  the PID output it still remaining constant. I have tried normalising the position data between 0-1, however that has not given a response. Changing the P value has no effect on the output,  neither does changing the position. Has anyone got any thoughts on what might not be working?
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2945
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is the PID on?

Quote
//turn the PID on
  myPID.SetMode(AUTOMATIC);

http://playground.arduino.cc//Code/PIDLibaryBasicExample
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Pages: 1 [2]   Go Up
Jump to: