I encounter the following problems:
1)I need just a bit more precision. So i want to count 1/2 ticks. I got the code from here post 21. I edit it, but it doesn't work for LeftEncoderASet.
2)How can i set a value of ticks and goes exactly in that value.
3)How can i find the dead zone of the dc motor and embed it in the code.
#include <digitalWriteFast.h>
// Quadrature encoders
// Left encoder
#define c_LeftEncoderInterrupt 0
#define c_LeftEncoderPinA 2
#define c_LeftEncoderInterrupt 1
#define c_LeftEncoderPinB 3
#define LeftEncoderIsReversed
//
volatile bool _LeftEncoderASet;
volatile bool _LeftEncoderBSet;
volatile long _LeftEncoderTicks = 0;
//Variables for PID
double Setpoint=1350.0;//h timi p thelw na paei
unsigned long lastTime;
double errSum, lastErr;
double Input, Output;
double kp=0.009;
double ki=0;
double kd=0;
int SpeedPin=5;
int motorPin1=6;
int motorPin2=7;
//Send command to DC motor
void loop(){
Input=_LeftEncoderTicks;
Compute();
if(Output > 0) {
CC();
}
else if(Output<=0){
CCW();
}
Serial.print(_LeftEncoderTicks);
Serial.print("\n");
delay(20);
}
//Motor Control Function
void CC(){
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,HIGH);
analogWrite(SpeedPin, abs(Output));//abs apoliti t
}
void CCW(){
digitalWrite(motorPin1,HIGH);
digitalWrite(motorPin2,LOW);
analogWrite(SpeedPin, abs(Output));
}
//Compute controller output by PID algorithm
void Compute()
{
/*How long since we last calculated*/
unsigned long now = millis();
double timeChange = (double)(now - lastTime);
Input=_LeftEncoderTicks;
/*Compute all the working error variables*/
double error = Setpoint - Input;
errSum += (error * timeChange);
double dErr = (error - lastErr) / timeChange;
/*Compute PID Output*/
Output = ((kp * error + ki * errSum + kd * dErr)+60);
if(Output>255){Output = 255;}
else if(Output <-255){Output = -255;}
else{}
/*Remember some variables for next time*/
lastErr = error;
lastTime = now;
}
void setup()
{
Serial.begin(115200);
// Quadrature encoders
// Left encoder
pinMode(c_LeftEncoderPinA, INPUT); // sets pin A as input
digitalWrite(c_LeftEncoderPinA, LOW); // turn on pullup resistors
pinMode(c_LeftEncoderPinB, INPUT); // sets pin B as input
digitalWrite(c_LeftEncoderPinB, LOW); // turn on pullup resistors
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA); // read the input pin
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB); // read the input pin
attachInterrupt(c_LeftEncoderInterrupt, HandleLeftMotorInterruptA, RISING);
attachInterrupt(c_LeftEncoderInterrupt, HandleLeftMotorInterruptB, RISING);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(SpeedPin, OUTPUT);
// Left encoder
pinMode(c_LeftEncoderPinA, INPUT); // sets pin A as input
digitalWrite(c_LeftEncoderPinA, HIGH); // turn on pullup resistors
pinMode(c_LeftEncoderPinB, INPUT); // sets pin B as input
digitalWrite(c_LeftEncoderPinB, HIGH); // turn on pullup resistors
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA); // read the input pin
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB); // read the input pin
attachInterrupt(c_LeftEncoderInterrupt, HandleLeftMotorInterruptA, FALLING);
attachInterrupt(c_LeftEncoderInterrupt, HandleLeftMotorInterruptB, FALLING);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(SpeedPin, OUTPUT);
}
// Interrupt service routines for the left motor’s quadrature encoder
void HandleLeftMotorInterruptA()
{
// Test transition
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA); // read the input pin
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB); // read the input pin
// and adjust counter + if A leads B
#ifdef LeftEncoderIsReversed
_LeftEncoderTicks += (_LeftEncoderASet != _LeftEncoderBSet) ? -1 : +1;
#else
_LeftEncoderTicks -= (_LeftEncoderASet != _LeftEncoderBSet) ? -1 : +1;
#endif
}
// Interrupt service routines for the left motor’s quadrature encoder
void HandleLeftMotorInterruptB()
{
// Test transition
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA); // read the input pin
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB); // read the input pin
// and adjust counter + if A leads B
#ifdef LeftEncoderIsReversed
_LeftEncoderTicks += (_LeftEncoderASet != _LeftEncoderBSet) ? -1 : +1;
#else
_LeftEncoderTicks -= (_LeftEncoderASet != _LeftEncoderBSet) ? -1 : +1;
#endif
}