I am trying to code a Rover using PID which is meant to go straight a certain distance that you input and then it is supposed to turn. I got the Rover to go straight, but I can't get it to turn for some reason. I know that in the part:
Motor1Drive((long)-pidVal3*slope);
Motor2Drive((long)pidVal4*slope);
this is how the motor goes forward. I also know that to make it turn you need to change the sign of one of the motor drives such as
Motor1Drive((long)-pidVal3*slope);
Motor2Drive((long)-pidVal4*slope);
i also set it into two different function: one to go forward and the other to turn. However, for some reason the code doesn't read the turn function. Please help.
/* Vriable Decleration*/
//Counters Output Pins
int counterInputs1[8] = {53,51,49,47,45,43,41,39}; // this is the standard but it can be connected differently depending on the rover.
int counterInputs2[8] = {37,35,33,31,29,27,25,23};
//Counter select Pins
int counterSEL1 = 52;
int counterSEL2 = 32;
//Counter enable pins
int counterOE1 = 50;
int counterOE2 = 30;
//Counter reset Pins
int counterRST1 = 48;
int counterRST2 = 28;
// Motor Pins
int motor1PWM = 3;
int motor1Dir = 12;
int motor2PWM = 11;
int motor2Dir = 13;
/* The counterInit() method initalizes all the pin outputs for the encoders
* Paramters:
* counterInputs Is a pointer to an array of input pin numbers for one of the HCTL2016 encoder
* counterSEL Pin number for the encoder "Select" pin
* counterOE Pin number for the encoder "Output Enable" pin
* counterRST Pin number for the encoder "Reset" pin
*/
void counter1Init(int *counterInputs1, int counterSEL1, int counterOE1, int counterRST1)
{
pinMode(counterSEL1,OUTPUT);
pinMode(counterOE1,OUTPUT);
pinMode(counterRST1,OUTPUT);
for(int i = 0; i < 8; i++)
{
pinMode(counterInputs1[i],INPUT);
}
// Reset the counter
digitalWrite(counterRST1,LOW);
digitalWrite(counterRST1,HIGH);
}
void counter2Init(int *counterInputs2, int counterSEL2, int counterOE2, int counterRST2)
{
pinMode(counterSEL2,OUTPUT);
pinMode(counterOE2,OUTPUT);
pinMode(counterRST2,OUTPUT);
for(int i = 0; i < 8; i++)
{
pinMode(counterInputs2[i],INPUT);
}
// Reset the counter
digitalWrite(counterRST2,LOW);
digitalWrite(counterRST2,HIGH);
}
/* The counterRead() method reads the counts from a HCTL2016 IC which is connected
* to the quadrature encoder and it returns the counter reading which can be transformed
* to distance or angle.
* parameters:
* - counterInput[]: an array of the outputs of the counter. the first element represents
* Least Significant Bit (LSB).
* - counterOE the number of the pin connected the counter's Enable pin.
* - counterSEL the number of the pin connected the counter's Select pin.
*
* return: the number of counts of the counter.
*/
void counterRead(int inputs[8], int _OE, int _SEL, int counterRST, long *counter_value)
{
long value = 0;
int readBack[16];
bool signbit = 0;
long counterValue;
counterValue = *counter_value;
//Enable the counter
digitalWrite(_OE, LOW);
//Select High Byte
digitalWrite(_SEL,LOW);
//Read High Byte from Counter
for (int i = 8; i < 15; i++)
{
value = value + (digitalRead(inputs[i-8])<<i);
readBack[i] = digitalRead(inputs[i-8]);
}
readBack[15] = digitalRead(inputs[7]);
signbit = digitalRead(inputs[7]);
//Select Low Byte
digitalWrite(_SEL,HIGH);
//Read Low Byte
for (int i = 0; i < 8; i++)
{
value = value + (digitalRead(inputs[i])<<i);
readBack[i] = digitalRead(inputs[i]);
}
if(signbit)
{
value = value-32768;//why?
}
counterValue += value;
digitalWrite(_OE, HIGH);
digitalWrite(counterRST,LOW);
digitalWrite(counterRST,HIGH);
*counter_value = counterValue;
}
/* These are the methods that drive the motor
* Usage Instruction:
* - Make sure the 12V power source is connected to the rover because less
* power sources may burn the motor drive.
* - To use the putMotorValue method just copy and paste it to your sketch
* and provide the values you want.
* Paramters:
* val is a signed 9 bit number (-256, 255) which indicates the magnitude
* and direction of the motor
*/
void Motor1Drive(long val)
{
val = constrain(val, -255,255);
//Serial.print("Motor Val ");
//Serial.print(val);
if (val >= 0 )
{
digitalWrite(motor1Dir,HIGH);
analogWrite(motor1PWM, val);
}
else
{
val = -val;
analogWrite(motor1Dir,LOW);
analogWrite(motor1PWM,val);
}
}
void Motor2Drive(long val)
{
val = constrain(val, -255,255);
if (val >= 0 )
{
digitalWrite(motor2Dir,HIGH);
analogWrite(motor2PWM, val);
}
else
{
val = -val;
analogWrite(motor2Dir,LOW);
analogWrite(motor2PWM,val);
}
}
/* The motorInit() method initalizes all the pin outputs for the motors
* Paramters:
* counterInputs Is a pointer to an array of input pin numbers for one of the HCTL2016 encoder
* counterSEL Pin number for the encoder "Select" pin
* counterOE Pin number for the encoder "Output Enable" pin
* counterRST Pin number for the encoder "Reset" pin
*/
void motor1Init(int motor1PWM, int motor1Dir)
{
pinMode(motor1PWM,OUTPUT);
pinMode(motor1Dir,OUTPUT);
}
void motor2Init(int motor2PWM, int motor2Dir)
{
pinMode(motor2PWM,OUTPUT);
pinMode(motor2Dir,OUTPUT);
}
/* Differentiate() method
*/
float dt = 0;
float aDelay = 0;
float Differentiate(float input)
{
float t1 = 0;
float t2 = 0;
float diff_out = 0;
diff_out = input - aDelay;
aDelay = input;
dt = t1 - t2;
return diff_out;
}
/* Integrate() method
*/
float sum = 0;
float previousError;
float error;
float Integrate(float input)
{
float t1 = 0;
float t2 = 0;
sum = input + sum;
dt = t2 - t1;
error = error - previousError;
return sum;
}
/* PID() method
*/
float PID(float input, float kp, float ki, float kd)
{
return (kp*input + ki*Integrate(input) + kd*Differentiate(input));
}
/* The setup() method
*/
void setup(){
//Initializing Arduino's pins
counter1Init(counterInputs1, counterSEL1, counterOE1, counterRST1);
counter2Init(counterInputs2, counterSEL2, counterOE2, counterRST2);
motor1Init(motor1PWM,motor1Dir);
motor2Init(motor2PWM,motor2Dir);
//Start the Serial output
Serial.begin(9600);
}
long counterValue1 = 0;
long counterValue2 = 0;
float readValue1 = 0;
float readValue2 = 0;
long pidVal1 = 0;
long pidVal2 = 0;
long pidVal3 = 0;
long pidVal4 = 0;
float u_t = 0; // u(t) = 5ft;
//float motorSlope = .5;
float u_t2 = 0;
float t1 = 0;
float t2 = 0;
long dist[4] = {27000,194616,20000,276624};
+long distR[4] = {27000,194616,20000,276624};
int index = 3;
float slope = 0.1;
float kp1 =.64029;
float ki1 = 0;//.062678;
float kd1 = .069619;
float kp2 = 1.3568;
float ki2 = 0;//2.683;
float kd2 = .16614 ;
void firstline()
{
t1 = micros();
u_t = dist[index];
u_t2 = distR[index];
counterRead(counterInputs1, counterOE1, counterSEL1, counterRST1, &counterValue1);
counterRead(counterInputs2, counterOE2, counterSEL2, counterRST2, &counterValue2);
//void firstline(){
pidVal1 = (long)(PID(u_t + counterValue1, kp1, ki1, kd1));
pidVal2 = (long)-(PID(u_t - counterValue2, kp2, ki2, kd2));
Motor1Drive((long)-pidVal1*slope);
Motor2Drive((long)pidVal2*slope);
t2 = micros();
//Serial.print("Time ");
Serial.print(t2);
Serial.print("\t");
//Serial.print("PID Val ");
Serial.print(counterValue1);
Serial.print("\t");
Serial.println(-
counterValue2);
// readValue1 = 0;
// readValue2 = 0;
// pidVal = 0;
// pidVal2 = 0;
// counterValue1 = 0;
// counterValue2 = 0;
}
void turn(){ //add delay? my part
pidVal3 = (long)(PID(u_t2 + counterValue1, kp1, ki1, kd1));
pidVal4 = (long)-(PID(u_t2 - counterValue2, kp2, ki2, kd2));
// add delay? my part
Motor1Drive((long)-pidVal3*slope);
Motor2Drive((long)-pidVal4*slope);
}
void loop() {
firstline();
//delay(4000);
turn();
//delay(4000);
}