-
Notifications
You must be signed in to change notification settings - Fork 0
/
floor_sensor.ino
72 lines (68 loc) · 1.72 KB
/
floor_sensor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <QTRSensors.h>
#define numSensors 8
QTRSensors qtr;
int sensorValues[6];
int position;
int goalPosition=3500;
float kp=0.3;
float ki=0.000;
float kd=0.0;
float feedbackVal;
int previousError,currentError,errorTotal;
void qtrSetup() {
qtr.setTypeRC();
qtr.setSensorPins((const uint8_t[]){22,23,24,25,26,27,28,29},numSensors);
qtr.setEmitterPin(46);
delay(500);
Serial.println("Calibration Starting");
digitalWrite(ledPin,HIGH);
//10 second calibration
for(int i=0;i<400;i++) {
qtr.calibrate();
}
digitalWrite(ledPin,LOW);
Serial.println("Calibration Finished");
for(int i=0;i<numSensors;i++) {
Serial.print(qtr.calibrationOn.minimum[i]);
Serial.print(' ');
}
Serial.println();
for(int i=0;i<numSensors;i++) {
Serial.print(qtr.calibrationOn.maximum[i]);
Serial.print(' ');
}
delay(1000);
}
void getFloorPosition() {
position=qtr.readLineWhite(sensorValues);
for(int i=0;i<numSensors;i++) {
Serial.print(sensorValues[i]);
Serial.print('\t');
}
Serial.println();
Serial.print(position);
Serial.print('\t');
Serial.println(position-goalPosition);
}
void feedback() {
currentError=position-goalPosition;
errorTotal+=currentError;
previousError=currentError;
//test just kd and see if it works
feedbackVal=(kp*currentError)+(ki*errorTotal)+(kd*(currentError-previousError));
Serial.println(position,DEC);
//Serial.println(kd*(currentError-previousError),DEC);
if(feedbackVal<0) {
setLeftMotorSpeed(255);
setRightMotorSpeed(255+feedbackVal);
}
else if(feedbackVal>0) {
setLeftMotorSpeed(255-feedbackVal);
setRightMotorSpeed(255);
}
else {
setRightMotorSpeed(255);
setLeftMotorSpeed(255);
}
Serial.println(currentError);
}