I have somewhat small success with the following code but still the hard grip of the motor at a particular position is not there, which parameter of the kp,ki or kd I must modify or what changes in the programme must be made?
BEHAVIOUR: Now the motor seems to catch up a position but moves pretty fast from one point to another , the distance between these two points is too less.
PLEASE! watch - Video>> Magnetic Encoder on Shaft - YouTube
#include <PID_v1.h>
#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;
// Motor control 1 is connected to the Digital PIN 5
const int MotorControlPin1 = 5;
// Motor control 2 is connected to the Digital PIN 6
const int MotorControlPin2 = 6;
double KD,KP,KI,lastError,sumError;
void setup() {
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
Serial.begin (19200);
}
void loop(){
int val; //= analogRead(14);
int target = map(212, 0, 1023, 0,12 );// set the seek to target by mapping the potentiometer range to the encoder max count
int error = encoder0Pos - target; // find the error term = current position - target
// generalized PID formula
//correction = Kp * error + Kd * (error - prevError) + kI * (sum of errors)
// calculate a motor speed for the current conditions
int motorSpeed = 10 * error;//kp
motorSpeed += 5 * (error - lastError);//kd
motorSpeed += 10 * (sumError);//ki
// set the last and sumerrors for next loop iteration
lastError = error;
sumError += error;
if (motorSpeed > 0)
{
// Output is positive so set MotorControlPin2 (Direction) to HIGH
digitalWrite(MotorControlPin2, HIGH);
// When Direction is HIGH, a LOW on MotorControlPin1 (PWM Speed) causes the motor to run
analogWrite(MotorControlPin1, 255 - constrain((int)motorSpeed, 0, 255));
}
else
{
// Output is negative so set MotorControlPin2 (Direction) to LOW
digitalWrite(MotorControlPin2, LOW);
// When Direction is LOW, a HIGH on MotorControlPin1 (PWM Speed) causes the motor to run
analogWrite(MotorControlPin1, constrain((int)-motorSpeed, 0, 255));
}
}
void doEncoderA(){
// look for a low-to-high on channel A
if (digitalRead(encoder0PinA) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == HIGH) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
Serial.println (encoder0Pos, DEC);
// use for debugging - remember to comment out
}
void doEncoderB(){
// look for a low-to-high on channel B
if (digitalRead(encoder0PinB) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(encoder0PinA) == HIGH) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinA) == LOW) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}