Code: Select all
define IR IN_1
#define TextNumOut(xPos,yPos,str,col,num)  TextOut(xPos,yPos,str); \
                                           NumOut(xPos+6*col,yPos,num)//Combinación de Tex y NumOut
int dir, s1, s3, s5, s7, s9; 
void HTEnhancedIRSeekerV2(const byte  port, int &dir, int &strength)
{
  int cResp; //Función prporcionada por Hitechnic.com
  byte cmdBuf[] = {0x10, 0x43};  // Read DC signal strengths (skip the dir) Lee la señal DC mas fuerte omite la dirección
  byte respBuf[];                 // Respuesta del buffer
  bool fSuccess;
  int i, iMax;
  long dcSigSum, dcStr;
  dir = 0;
  strength = 0;
  // Lee en Modo DC
  cResp=6;
  fSuccess = I2CBytes(port, cmdBuf, cResp, respBuf);
  if (fSuccess) {
    // Busca la máxima fuerza de la señal DC
    iMax = 0;
    for (i=1; i<5; i++) if (respBuf[i] > respBuf[iMax]) iMax = i;
    // Calcula la direccion base de la señal DC
    dir = iMax*2+1;
    // Establece la fuerza DC base basándose en la señal máxima y promedio
    dcSigSum = respBuf[iMax] + respBuf[5];
    // Comprueba la intensidad de la señal si sencuentran sensores aparte en el Area
    if ((iMax > 0) && (respBuf[iMax-1] > respBuf[iMax]/2)) {
        dir--;
        dcSigSum += respBuf[iMax-1];
    }
    if ((iMax < 4) && (respBuf[iMax+1] > respBuf[iMax]/2)) {
        dir++;
        dcSigSum += respBuf[iMax+1];
    }
    // Crea una fuerza DC compatible con la fuerza AC usando la raiz cuadrada: sqrt(dcSigSum*500)
    dcSigSum *= 500; dcStr = 1;
    repeat(10) dcStr = (dcSigSum/dcStr + dcStr) / 2;  // sqrt approx
    strength = dcStr;
    // Decidir si se utiliza la fuerza DC o debe leer y utilizar la fuerza de AC
    if (strength <= 200) {
      // Usa Dirección AC
      dir = 0; strength = 0;
      cmdBuf[1] = 0x49; // Recycle rest of cmdBuf from the DC read operation
      cResp=6;
      fSuccess = I2CBytes(port, cmdBuf, cResp, respBuf);
      if (fSuccess) {
        dir = respBuf[0];
        // Suma los elementos del sensor para obtener fuerza
        if (dir > 0) for (i=1; i<=5; i++) strength += respBuf[i];
      }
    }
  }
}
task main(){
	
	ClearScreen();
	SetSensorLowspeed(IR);	
	while (true){
	ReadSensorHTIRSeeker(IR, dir, s1, s3, s5, s7, s9);
	Wait(50);
	TextNumOut(0, LCD_LINE3, "Dirección:  ", 4, dir);
	switch(dir){
	
	case s1:
	TextNumOut(6, LCD_LINE3, "S1: ", 3, s1);
	Wait(350);
	OnRev(OUT_A, 75);
	Wait(50);
	OnFwdSync(OUT_AB, 75, 100);
	Wait(50);
	Off(OUT_AB);
	ClearScreen();
	break;
	case s3:
	TextNumOut(6, LCD_LINE3, "S3: ", 3, s3);
	Wait(350);
	OnRev(OUT_A, 50);
	Wait(25),
	OnFwdSync(OUT_AB, 75, 100);
	Wait(50);
	Off(OUT_AB);
	ClearScreen();
	break;
	case s5:
	TextNumOut(6, LCD_LINE3, "S5: ", 3, s5);
	Wait(350);
	OnFwdSync(OUT_AB, 75, 100);
	Wait(150);
	Off(OUT_AB);
	ClearScreen();	
	break;
	case s7:
	TextNumoOut(6, LCD_LINE3, "S7: ", 3, s7);
	Wait(350);
	OnRev(OUT_B, 50);
	Wait(50);
	Off(OUT_AB);
	ClearScreen();
	break;
	default :
	TextOut(6,LCD_LINE3, "No se cumplen las condiciones");
	Wait(5000);
	break;
	}
   }
}             
