Hi
I'm trying to use an encoder and the servo library. Where 90 on the servo is 0 movement - ie PID has finished it's loop can br3ttb's PID be used in this fashion. I'm using a polulu twin H-bridge which uses RC servo pulses like in servo out to control the H-bridge 90 is all stop and 180 full one direction and 0 full the other. Here is the code so far. Any help much appreciated.
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*Yn = k1 * (Xc - Xn) + k2 * (Xn - Xn-1),
Yn is the control output,
Xc is the control input,
Xn is the position feedback sample,
Xn-1 is the previous position feedback sample, and
k1 and k2 are the PID coefficients.
* This example code is in the public domain.
*/
#include <Encoder.h>
#include <PID_v1.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,1,5,1,DIRECT);
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(5, 6);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
// reserve 200 bytes for the inputString:
// inputString.reserve(200);
Serial.println("Basic Encoder Test:");
// Input = analogRead(0);
// Setpoint = 500;
Output = 0;
//turn the PID on
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(0, 180);
myPID.SetSampleTime(20);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
long oldPosition = -999;
long newPosition = 0;
void loop() {
// Read serial input:
// if (stringComplete = false) {
// Serial.println("next");
while (Serial.available() > 0) {
int inChar = Serial.read();
Serial.println(inChar);
if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inputString += (char)inChar;
Serial.println(inputString);
}
if (inChar == '*') {
stringComplete = true;
}
//}
while (stringComplete) {
// Serial.println(inputString);
Setpoint = inputString.toInt();
newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
Input = newPosition;
myPID.Compute();
Output = Output + 90;
//Output = constrain(Output, 90,180);
Output = map(Output, 0, 270, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(Output); // sets the servo position according to the scaled value
// analogWrite(4,Output);
Serial.println(Output);
if (newPosition == Setpoint) {
// clear the string:
myservo.write(90);
inputString = "";
stringComplete = false;
Serial.println("enter new value");
myEnc.write(0);
Serial.println(newPosition);
}
}
}
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.