Permalink
Browse files

Re-write QTI library, reduce complexity of tests...

Change threshold to 10% of calibration for left QTI and to 20% for right QTI.

Signed-off-by: gbriand <gbriand@gmail.com>
  • Loading branch information...
1 parent 85ce7b8 commit 29f3d7543b5f290334c2958311c2b35ace03372b @gbriand gbriand committed Mar 11, 2011
Showing with 85 additions and 42 deletions.
  1. +85 −42 ArduinoLibraries/QTISensor/QTISensor.cpp
View
127 ArduinoLibraries/QTISensor/QTISensor.cpp
@@ -3,16 +3,20 @@
#define TIMEOUT 2000
+int l_avg = 0;
+int r_avg =0 ;
+
+
QTISensor::QTISensor()
{
/* Defaults are actually analog */
LeftQTIPin = 16;
RightQTIPin = 17;
- r_threshold = 0;
- l_threshold = 0;
- _history = 0;
- l_history = 0;
- r_history = 0;
+ r_threshold = 0; // threshold, avg of 20 readings * 0.8 set in calibrate
+ l_threshold = 0; // threshold, avg of 20 readings * 0.8 set in calibrate
+ _history = 0; // number of time to detect edge before returning edge
+ l_history = 0; // counter for the number of time left edge was detected
+ r_history = 0; // counter for the number of time left edge was detected
}
int QTISensor::pulseQTI(int pin)
@@ -24,10 +28,10 @@ int QTISensor::pulseQTI(int pin)
delay(1);
pinMode(pin, INPUT);
digitalWrite(pin, LOW); // turn pullups off - or it won't work
- while(digitalRead(pin) && !fail) { // Wait for pin to go low
+ while(digitalRead(pin) && !fail) { // Wait for pin to go low
timecount++;
if(timecount > TIMEOUT)
- fail = true;
+ fail = true;
}
if(fail)
return 0;
@@ -58,40 +62,71 @@ int QTISensor::edgeDetected()
{
int left = leftQTI();
int right = rightQTI();
- int r_avg;
- int l_avg;
-
- if(left <= l_threshold && right <= r_threshold) {
- l_history++;
- r_history++;
- if(l_history >= _history && r_history >= _history)
- return QTI_BOTH;
- }
- if(left <= l_threshold) {
- l_history++;
+
+// increment history if read value are inferior to threshold (see calibrate)
+ if (left <= l_threshold) l_history++;
+ else l_history=0;
+ if (right <= r_threshold) r_history++;
+ else r_history=0;
+//check if l_ & r_ history are over the history trigger and return edge
+ if (l_history>= _history && r_history>= _history) {
+ l_history = 0;
r_history = 0;
- if(l_history >= _history)
- return QTI_LEFT;
- } else
+ return QTI_BOTH;
+ }
+ else if (l_history>= _history){
l_history = 0;
- if(right <= r_threshold) {
- r_history++;
+ r_history = 0;
+ return QTI_LEFT;
+ }
+ else if (r_history>= _history){
l_history = 0;
- if(r_history >= _history)
- return QTI_RIGHT;
- } else
r_history = 0;
- // We have determined that there isn't an edge above the hysteresis at this point. Send an additional flag if we are falling off the edge. Negative value
- r_avg = r_threshold / 0.8;
- l_avg = l_threshold / 0.8;
- if(left >= l_avg *2 && right >= r_avg *2) {
- return QTI_EDGE_BOTH;
- } else if(left >= l_avg *2) {
- return QTI_EDGE_LEFT;
- } else if (right >= r_avg *2) {
- return QTI_EDGE_RIGHT;
- }
-
+ return QTI_RIGHT;
+ }
+ // if value read is 2 times bigger than historic value, we are off the board
+ if(left >= l_avg && right >= r_avg) {
+ return QTI_EDGE_BOTH;
+ } else if(left >= l_avg) {
+ return QTI_EDGE_LEFT;
+ } else if (right >= r_avg) {
+ return QTI_EDGE_RIGHT;
+ }
+
+ /* if(left <= l_threshold && right <= r_threshold) {
+ l_history++;
+ r_history++;
+ if(l_history >= _history && r_history >= _history){
+ l_history = 0;
+ r_history = 0;
+ return QTI_BOTH;}
+ }
+ if(left <= l_threshold && right > r_threshold ) {
+ l_history++;
+ r_history = 0;
+ if(l_history >= _history){
+ l_history = 0
+ return QTI_LEFT;}
+ } else
+ l_history = 0;
+ if(right <= r_threshold) {
+ r_history++;
+ l_history = 0;
+ if(r_history >= _history)
+ return QTI_RIGHT;
+ } else
+ r_history = 0;
+ // We have determined that there isn't an edge above the hysteresis at this point. Send an additional flag if we are falling off the edge. Negative value
+ //r_avg = r_threshold / 0.8;
+ //l_avg = l_threshold / 0.8;
+ if(left >= l_avg && right >= r_avg) {
+ return QTI_EDGE_BOTH;
+ } else if(left >= l_avg) {
+ return QTI_EDGE_LEFT;
+ } else if (right >= r_avg) {
+ return QTI_EDGE_RIGHT;
+ }
+ */
return QTI_NONE;
}
// Set threshold valu
@@ -130,19 +165,27 @@ void QTISensor::calibrate()
lastnum_l = lval;
rval = rightQTI();
lastnum_r = rval; //ooooops was (lastnum_r + rval) / 2 but was still working !?
- } else {
+ }
+ else {
lastnum_l = (lastnum_l + lval) / 2;
rval = rightQTI();
lastnum_r = (lastnum_r + rval) / 2;
}
}
- // 80% of the calculated average each sensor
- setLeftThreshold(lastnum_l * 0.8);
- setRightThreshold(lastnum_r * 0.8);
+ // define the limit over motors stop
+ l_avg = lastnum_l * 2;
+ r_avg = lastnum_r * 2;
+
+ // 80% of the calculated average each sensor to avoid false edge
+ setLeftThreshold(lastnum_l * 0.1);
+ setRightThreshold(lastnum_r * 0.2);
+
+
}
// Sets the number of sequential hits necessary to detect an edge
void QTISensor::sethistory(int count)
{
- _history = count;
+ _history = count;
}
+

0 comments on commit 29f3d75

Please sign in to comment.