Hello,
I have a 12V DC motor from Faulhaber with an encoder with 16 pulses/rotation. What i am trying to do is to control the position of the motor using a potentiometer, and also controlling the speed of it.
The problem is that the positioning works(with the encoder and pid library) only for pwm values under 70. Over that value the arduino simply crashes and the motor keeps rotating at high speed without reading anything from the encoder.
I tested the motor without the encoder readings and it works fine at any speed or direction. When I try to use the encoder, something goes wrong.
The code is:
#include <PID_v1.h>
#include <Encoder.h>
int sensorValue = 0;
int outputValue = 0;
int k;
long positionLeft = -999;
Encoder myEnc(3, 2);
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
//PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
PID myPID(&Input, &Output, &Setpoint,2,0,0, DIRECT);
void setup() {
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(4,HIGH);
setPwmFrequency(9, 1);
//initialize the variables we're linked to
Input = 0;
Setpoint = 0;
//turn the PID on
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(20);
myPID.SetOutputLimits(-75, 75); //over 75 the encoder can't read anymore
}
void loop() {
sensorValue = analogRead(A0);
k=map(analogRead(A1),0,1023,-70,70);
outputValue=sensorValue/4;
Input = positionLeft;
Setpoint= sensorValue;
myPID.Compute();
if(Output>0)
{analogWrite(9,Output);
analogWrite(10,0);}
else
{analogWrite(10,abs(Output));
analogWrite(9,0);
}
positionLeft=myEnc.read();
positionLeft/=1000;
}
Can you give any suggenstions about what could cause the encoder to not read at high speed?? (I am using Arduino Uno, a driver with L298 H bridge and the encoder channels are using the 2 and 3 interrupt pins on Arduino).
Thanks in advance.