chipKIT® Development Platform

Inspired by Arduino™

Problem reading&displaying QTR without the use of libraries

Created Fri, 18 Jan 2013 18:46:34 +0000 by vvysoc


vvysoc

Fri, 18 Jan 2013 18:46:34 +0000

I have a problem reading my QTR-8A Reflectance Sensor Array from http://www.pololu.com/catalog/product/960. I'm using chipkit max 32 on my line following robot with Dual MC33926 Motor Driver Carrier from http://www.pololu.com/catalog/product/1213 and it's all connected correctly i believe. The robot is finally line following but is acting weird and part of my problem is that I'm coding it blindly because I don't exactly know the values of each sensor and position. I have tried to download and put the QTR sensor library into MPIDE under documents/mpide/libraries/QTRSensors to print the values to the serial monitor but I couldn't even verify the code because it gives me error (
C:\Users\Vitaliy\Documents\mpide\libraries\QTRSensors\QTRSensors.cpp:36:21: fatal error: Arduino.h: No such file or directory compilation terminated. ).

I have tried everything from just serial print individual sensor to serialprint my "Readsensor" part from my code and many other combination and also researched everywhere and didnt find any fix. I have also tried to download the library on a diffrent computer which is 32 bit unlike my laptop is 64 bit and that didn't help.

Here's my code for line following without a library:

int LeftSensors, RightSensors, TotalSensorsAvg; int LeftVelocity, RightVelocity;

//const int LEFT_MOTOR = 3, RIGHT_MOTOR = 9; #define stby 8 #define LEFTmotor1 4 #define LEFTmotor2 6 #define RIGHTmotor1 80 #define RIGHTmotor2 81

//#define LEFT_MOTOR 3 //PWM //#define RIGHT_MOTOR 9 //fPWM

const int LEFT_MOTOR = 3, RIGHT_MOTOR = 9; long sensors[] = { 0, 0, 0, 0, 0, 0}; long sensors_average = 0; int sensors_sum = 0; int Position = 0; int proportional = 0; long integral = 0; int derivative = 0; int last_proportional = 0; int control_value = 0;

int max_difference = 75; float Kp = .05200; float Ki = .000001100; float Kd = 1.105*300;

void setup() {

pinMode(stby,OUTPUT); pinMode(LEFTmotor1,OUTPUT); pinMode(LEFTmotor2,OUTPUT); pinMode(RIGHTmotor1,OUTPUT); pinMode(RIGHTmotor2,OUTPUT);
digitalWrite(stby,HIGH);

digitalWrite(LEFTmotor1,HIGH); digitalWrite(LEFTmotor2,LOW); digitalWrite(RIGHTmotor1,HIGH); digitalWrite(RIGHTmotor2,LOW); Serial.begin(9600);

}

void loop() { TotalSensorsAvg = (analogRead(0) + analogRead(1) + analogRead(2) + analogRead(3) + analogRead(4) + analogRead(5))/6/4; Serial.println(TotalSensorsAvg);

{

{
  ReadSensors();
  GetAverage();
  GetSum();
  GetPosition();
  GetProportional();
  GetIntegral();
  GetDerivative();
  GetControl();
  AdjustControl();
  SetMotors();            
} 

} }

//void Stop() //{ //analogWrite(LEFT_MOTOR,0); // analogWrite(RIGHT_MOTOR,0); //}

void ReadSensors() { // read 6 sensors for (int i = 0; i < 6; i++) { sensors[i] = analogRead(i + 1);

        // round readings less than 50 down to zero to filter surface noise
if (sensors[i] &lt; 50)
{
  sensors[i] = 0;
}

} }

void GetAverage() { // zero average variable of the sensors sensors_average = 0;

for (int i = 0; i < 6; i ++) { sensors_average += sensors[i] * i * 1000; } }

void GetSum() { // zero variable of sum of the sensors sensors_sum = 0;

// sum the total of all the sensors for (int i = 0; i < 6; i++) { sensors_sum += int(sensors[i]); } }

void GetPosition() { // calculate the current position on the line Position = int(sensors_average / sensors_sum); }

void GetProportional() { // caculate the proportional value proportional = Position - 2500; }

void GetIntegral() { // calculate the integral value integral = integral + proportional; }

void GetDerivative() { // calculate the derivative value derivative = proportional - last_proportional;

// store proportional value in last_proportional for the derivative calculation on next loop last_proportional = proportional; }

void GetControl() { // calculate the control value control_value = int(proportional * Kp + integral * Ki + derivative * Kd); }

void AdjustControl() { // if the control value is greater than the allowed maximum set it to the maximum if (control_value > max_difference)

// if the control value is less than the allowed minimum set it to the minimum if (control_value < -max_difference) }

void SetMotors() { TotalSensorsAvg = (analogRead(0) + analogRead(1) + analogRead(2) + analogRead(3) + analogRead(4) + analogRead(5))/6/4;

if (TotalSensorsAvg < 85) // on white board { if((TotalSensorsAvg > 30)) // catch the line { // if control_value is less than zero a right turn is needed to acquire the center of the line if (control_value < 0) { analogWrite(RIGHT_MOTOR, max_difference + control_value); analogWrite(LEFT_MOTOR, max_difference); LeftVelocity = max_difference; RightVelocity = max_difference + control_value; } // if control_value is greater than zero a left turn is needed to acquire the center of the line else { analogWrite(RIGHT_MOTOR, max_difference); analogWrite(LEFT_MOTOR, max_difference - control_value); LeftVelocity = max_difference - control_value; RightVelocity = max_difference; } } if (TotalSensorsAvg <= 30) // lost the line { while (LeftVelocity > RightVelocity) // turn right { analogWrite(LEFT_MOTOR,max_difference); // digitalWrite(6,LOW); analogWrite(RIGHT_MOTOR,0); // digitalWrite(81,LOW); break; } while (RightVelocity > LeftVelocity) // turn left { analogWrite(LEFT_MOTOR,0); //digitalWrite(6,LOW); analogWrite(RIGHT_MOTOR,max_difference); //digitalWrite(81,LOW); break; } } } else if (TotalSensorsAvg > 85) //om black board

{ if((TotalSensorsAvg < 160)) // catch the line { if (control_value < 0) { analogWrite(RIGHT_MOTOR, max_difference); analogWrite(LEFT_MOTOR, max_difference + control_value); LeftVelocity = max_difference + control_value; RightVelocity = max_difference; } // If control_value is greater than zero a left turn is needed to // acquire the center of the line else { analogWrite(RIGHT_MOTOR, max_difference - control_value); analogWrite(LEFT_MOTOR, max_difference); LeftVelocity = max_difference; RightVelocity = max_difference - control_value; } } if (TotalSensorsAvg >= 160) // lost the line { while (LeftVelocity > RightVelocity) // turn right { analogWrite(LEFT_MOTOR,max_difference); //digitalWrite(6,LOW); analogWrite(RIGHT_MOTOR,0); //digitalWrite(81,LOW); //low is positive dircetion, high is negative direction break; } while (RightVelocity > LeftVelocity) // turn left { analogWrite(LEFT_MOTOR,0); //digitalWrite(6,LOW); analogWrite(RIGHT_MOTOR,max_difference); //digitalWrite(81,LOW); break; } } } // else // { // analogWrite(RIGHT_MOTOR,max_difference); // analogWrite(LEFT_MOTOR,max_difference);
// } }

So, can anyone please tell me if anything in the code seems wrong that will cause the robot to line follow weirdly, how to serial print the sensor values without the use of the library and lastly I'm confused when I got to Mpide window and click Tools/programmer what option should I pick and why do I need it when the robot line followed without clicking any of the option except what board I'm using and the port.

I'm new to this chipKit and pretty new to programing in general so any help would be very helpful.

Thank you