Help follow the ballIR

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
linuxnewbie1
Posts: 8
Joined: 17 Oct 2012, 01:39

Help follow the ballIR

Post by linuxnewbie1 »

Help me whit me code, i need the follow Ball IR

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;
	}
   }
}             
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Help follow the ballIR

Post by afanofosc »

Can you explain what the robot does? Anything at all? Or does it just sit there or drive straight or turn in circles? Can you tell if the outputs from your sensor are correct?

Start with this program:

Code: Select all

#define IR S1
task main()
{
  int dir, s1, s3, s5, s7, s9;
  SetSensorLowspeed(IR); // IIC on port 1
  while (true)
  {
    if (ReadSensorHTIRSeeker(IR, dir, s1, s3, s5, s7, s9))
    {
      NumOut(0, LCD_LINE1, dir);
      NumOut(0, LCD_LINE2, s1);
      NumOut(0, LCD_LINE3, s3);
      NumOut(0, LCD_LINE4, s5);
      NumOut(0, LCD_LINE5, s7);
      NumOut(0, LCD_LINE6, s9);
    }
    Wait(MS_50);
  }
}
Then move the IR ball around to see if the values change at all.

It looks like NXC is allowing a variable in the case label, which it should not do. Are you getting a compiler error when you try to compile your code? The switch statement in C has this format:

Code: Select all

  switch(value)
  {
    case 0: // a constant value for the case label
      // put code here for case 0
      break;
    case 1: // another unique constant value
      // code here for case 1
      break;
    // more cases as needed
    default: // optional default case
      // code to execute if no other case matches the value passed into the switch
      break;
  }
The direction from the HiTechnic IR sensor has a value from 0 to 9 where 0 means no signal detected.

Image

I would have your switch tell the robot to go straight for case 5:, stop moving and rotate in place for case 0/default, turn left for cases 1-4, and turn right for cases 6-9.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
linuxnewbie1
Posts: 8
Joined: 17 Oct 2012, 01:39

Re: Help follow the ballIR

Post by linuxnewbie1 »

I probe the code, if you read the values. What you have to do the robot is to search and find the ball station, to go to her and make a sound. But I find the way.
linuxnewbie1
Posts: 8
Joined: 17 Oct 2012, 01:39

Re: Help follow the ballIR

Post by linuxnewbie1 »

in fact lacked an argument at first, before LCD_LINE
linuxnewbie1
Posts: 8
Joined: 17 Oct 2012, 01:39

Re: Help follow the ballIR

Post by linuxnewbie1 »

I mark error file reads nothing, if compiled, but I mark error in the brick
linuxnewbie1
Posts: 8
Joined: 17 Oct 2012, 01:39

Re: Help follow the ballIR

Post by linuxnewbie1 »

Already looking the ball, but I played the sound file, I add that I use linux

Code: Select all

				// BetoBot//

//La utilizad de éste robot es la de la localización de señales infrarojas

  int dir;
  int strength;


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(S1);
while (true)
    {
     TextOut(0, LCD_LINE1, ">>>BetoBot<<<");
     HTEnhancedIRSeekerV2(S1, dir, strength);
     TextOut(0, LCD_LINE3, "Dir:  ");
     NumOut(6*5, LCD_LINE3, dir);
     TextOut(6*7, LCD_LINE3, "Inten:    ");
     NumOut(6*12, LCD_LINE3, strength);
     Wait(50);
	

	switch (dir){

	case 0:
		TextOut(6, LCD_LINE3, "No hay ninguna Señal");
		break;

	case 1:
		OnRev(OUT_C, 100);
		Wait(200);
		Off(OUT_C);
		break;

	case 2:

		OnRev(OUT_C, 75);
		Wait(150);
		Off(OUT_C);
		break;
	
	case 3:

		OnRev(OUT_C, 50);
		Wait(100);
		Off(OUT_C);
		break;
	
	case 4:

		OnRev(OUT_C, 50);
		Wait(50);
		Off(OUT_C);
		break;
	
	case 5:
		
		TextOut(6, LCD_LINE5, " Ya te encontre ");
		PlayFileEx("Woops.rso", 7, true); // no play sound		
		Wait(1000);		
		ClearLine(LCD_LINE5);
		OnFwdSync(OUT_AC, 100, 0);
		break;
	
	case 6:

		OnRev(OUT_A, 50);
		Wait(50);
		Off(OUT_A);
		break;

	case 7:

		OnRev(OUT_A, 50);
		Wait(75);
		Off(OUT_A);
		break;

	case 8:

		OnRev(OUT_A, 75);
		Wait(75);
		Off(OUT_A);
		break;

	case 9:

		OnRev(OUT_A, 100);
		Wait(100);
		Off(OUT_A);
		break;

			

	default:
		TextOut(6, LCD_LINE5, "Me equivoqueno hay Nati");
		Wait(100);
		ClearLine(LCD_LINE5);
		break;
	}
	 
		
    }

}
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests