I want to make close loop negative feedback by using encoder to control rotation degree, which I can configure the desire point. For instance, if I configure the desire point or target angle at 80 degree in the terminal and upload the code to my Arduino Nano, the motor will move to 80 degree and if it is not at 80 degree the system will command the motor again and try to reach to 80 degree.
This is the link which is similar to what I want to do:
Motor detail:
Retarder Reduction Ratio 1:34
11 Pules / 1 Rev. or Hall x Ratio 34.02 = 341.2PPR
In addition, I am using L298H Driver to driver the motor.
I really need you guys a favor, I am a beginner and not sure how to this.
[code]
#define MotorA
int enA = 6;
int in1 = 4;
int in2 = 5;
int EncoderA1 = 3;
int EncoderA2 = 7;
//-----------//
int pos = 0;
long prevT = 0;
float eprev = 0;
float eintegral = 0;
void setup() {
Serial.begin(9600);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(EncoderA1, INPUT);
pinMode(EncoderA2, INPUT);
attachInterrupt(digitalPinToInterrupt(EncoderA1), readencoder, RISING);
Serial.println("target pos");
}
void loop() {
//--------adjustable parameteer(Motor A)------------------------//
int Target_Angle = 200;
//--------PID Constants(Motor A)------------------------//
float Kp = 1; //proprtional constant
float Ki = 0; //intergral constant
float Kd = 0; //derivative constant
float currentTime = millis();
eprev = (currentTime - prevT) * 0.001; //dt
// control signal
float u = Kp * e + Kd * dedt + Ki * eintegral;
// motor power//
float pwr = fabs(u);
if ( pwr > 255 ) {
pwr = 255;
}
// motor direction//
int dir = 1;
if (u < 0) {
dir = -1;
}
void readencoder() {
int A2 = digitalRead(EncoderA2);
if (A2 > 0) {
pos++;
}
else {
pos--;
}
}
[/code]