Hitechnic IR Seeker V2

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: roger, imaqine, 99jonathan

vegavov
New User
Posts: 2
Joined: Tue Oct 20, 2009 12:24 pm

Hitechnic IR Seeker V2

Postby vegavov » Tue Oct 20, 2009 12:39 pm

Image Image

I just received a IR Seeker V2 and an Infrared Ball from Hitechnic and having some problems getting this to work with lejos.

Im using the IR Seeker from the API but everything i try returns -1.

This is the program im trying:

Code: Select all

      IRSeekerSensor seeker = new IRSeekerSensor(SensorPort.S3);
      
      seeker.setAddress(0x10);
      
      while (!Button.ESCAPE.isPressed())
      {
         int direction = seeker.getDirection();
         LCD.clear();
         LCD.drawInt(direction, 2,2);
         LCD.refresh();
      }



And the output is -1.

Is this because the V2 version is not supported by lejos yet?
Or is there something about the V2 using I2C address is 0x10 instead of the standard address?

User avatar
gloomyandy
leJOS Team Member
Posts: 4525
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby gloomyandy » Tue Oct 20, 2009 1:42 pm

Hi,
You might want to try setting the address to 0x8 instead of 0x10. There are two ways of referring to i2c addresses. leJOS uses the 7bit way which means addresses go from 0 to 7f many others use the eight bit address in which addresses go from 2 to 0xfe (and can not be odd). So you may need to divide the address by 2 for leJOS...

Andy

vegavov
New User
Posts: 2
Joined: Tue Oct 20, 2009 12:24 pm

Postby vegavov » Wed Oct 21, 2009 5:23 pm

Thanks for the help! It worked.

This did the trick:

Code: Select all

seeker.setAddress(0x8);

User avatar
Eric
Novice
Posts: 63
Joined: Sat Oct 24, 2009 5:07 pm
Location: UK
Contact:

IRSeekerV2 Class (not derived from IRSeeker)

Postby Eric » Tue Nov 10, 2009 8:24 pm

I had the same problem. I built a new class since the sensor behaves fundametally differently (ie has a mode).

Code: Select all

package lejos.nxt.addon;

import lejos.nxt.I2CPort;
import lejos.nxt.I2CSensor;

public class IRSeekerV2
{
   public static enum Mode {
      AC, DC
   };

   public static final byte   address   = 0x08;
   byte[]                  buf      = new byte[1];
   public static final   float   noAngle = Float.NaN;

   private Mode            mode;

   /**
    * Set the mode of the sensor
    */
   public void setMode(Mode mode)
   {
      this.mode = mode;
   }

   public IRSeekerV2(I2CPort port, Mode mode)
   {
      super(port, I2CPort.STANDARD_MODE);
      setMode(mode);
      setAddress(address);
   }

   /**
    * Returns the direction of the target (1 to 9) or 0 if no target.
    *
    * @return direction
    */
   public int getDirection()
   {
      int register = 0;
      if(mode == Mode.AC)
      {
         register = 0x49;
      }
      else if(mode == Mode.DC)
      {
         register = 0x42;
      }
      int ret = getData(register, buf, 1);
      if(ret != 0)
         return -1;
      return (0xFF & buf[0]);
   }

   /**
    * Returns the angle of the target (-180 to 180) or NaN.
    *
    * @return direction
    */
   public float getAngle(boolean blocking)
   {
      while(true)
      {
         int dir = getDirection();
         if(dir != 0)
            return (dir - 5) * 30;
         else if(dir == 0 && !blocking)
            return Float.NaN;
      }
   }
   public float getAngle()
   {
      return getAngle(false);
   }

   public boolean hasDirection()
   {
      return getDirection() != 0;
   }

   /**
    * Returns value of sensor 1 - 5.
    *
    * @param id
    *            The id of the sensor to read
    * @return sensor value (0 to 255).
    */
   public int getSensorValue(int id)
   {
      int register = 0;
      if(mode == Mode.AC)
      {
         register = 0x4A;
      }
      else if(mode == Mode.DC)
      {
         register = 0x43;
      }
      if(id < 1 || id > 5)
         throw new IllegalArgumentException(
               "The argument 'id' must be between 1 and 5");
      int ret = getData(register + (id - 1), buf, 1);
      if(ret != 0)
         return -1;
      return (0xFF & buf[0]);
   }

   /**
    * Gets the values of each sensor, returning them in an array.
    *
    * @return Array of sensor values (0 to 255).
    */
   public int[] getSensorValues()
   {
      int[] values = new int[5];
      for(int i = 0; i < 5; i++)
      {
         values[i] = getSensorValue(i + 1);
      }
      return values;
   }

   /**
    * Returns the average sensor reading (DC Only)
    *
    * @return sensor value (0 to 255).
    */
   public int getAverage(int id)
   {
      if(mode == Mode.DC)
      {
         if(id <= 0 || id > 5)
            return -1;
         int ret = getData(0x48, buf, 1);
         if(ret != 0)
            return -1;
         return (0xFF & buf[0]);
      }
      else
      {
         return -1;
      }
   }

   /**
    * Returns a string representation of the strengths
    */
   public String toString()
   {
      return "(" + getSensorValue(1) + "," + getSensorValue(2) + ","
            + getSensorValue(3) + "," + getSensorValue(4) + ","
            + getSensorValue(5) + ")";
   }
}

lawrie
leJOS Team Member
Posts: 955
Joined: Mon Feb 05, 2007 1:27 pm

Postby lawrie » Thu Dec 03, 2009 12:04 am

Hi Eric,

I will add your class to leJOS for the next release. It seemed to need "extends I2CSensor" adding to the public class statement.

Sebmaster
New User
Posts: 1
Joined: Fri Mar 05, 2010 6:34 pm

Postby Sebmaster » Fri Mar 05, 2010 6:51 pm

I've been trying to get this up and running the whole day now, but something goes wrong everytime.

I have this sample code:

Code: Select all

IRSeeker ir = new IRSeeker(SensorPort.S1);
ir.setAddress(0x8);
      
while (true) {
   System.out.println(ir.getDirection());
}


If I do it this way, I always get -1 on my screen.
With the class of Eric, I found out, that the getData-call fails with -1 as return code.

I'm using a NXT 2.0 and the IRSeeker V2 by HiTechnic with LeJOS 0.85.

Can someone give me an advice?

fofa
New User
Posts: 6
Joined: Thu Mar 17, 2011 2:37 pm

Re: Hitechnic IR Seeker V2

Postby fofa » Thu Mar 17, 2011 2:56 pm

hi
plz can explain why use 0xff in getDirection


Return to “NXJ Hardware”

Who is online

Users browsing this forum: No registered users and 1 guest