NXTCamSensor

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

NXTCamSensor

Postby esmetaman » Mon Nov 05, 2007 9:22 pm

Hi, I am developing a Class to use new NXT Cam sensor developed by mindsensors.

I need help with my code. I used the work posted in this forum about I2C (gloomyandy)

Code: Select all

import java.io.*;
import java.util.Vector;

import lejos.nxt.*;

/**
*
* Abstraction for a NXT NXT Cam.
* Further information:
* http://www.mindsensors.com/
* http://nxtcamview.sourceforge.net/
*
* Author: Juan Antonio Breña Moral
* Email: bren@juanantonio.info
* Web: http://www.juanantonio.info
*
*/
public class NXTCamSensor extends I2CSensor{

    byte[] buf = new byte[1];
   
    public NXTCamSensor(SensorPort port)
    {
        super(port);
    }
   
    //I2C Operations
   
    /*
    public int sortTrackedObjectsBySize(){
       buf[0] = 0x1;
        int value = sendData(0x41, buf, 1);
        return value;   
    }
    */     
   
    public int getNumberOfObjectDetected(){
       int value = getData(0x42, buf, 1);
       return value;
    }

    /*
    public int setCameraRegisters(){
       buf[0] = 0x1;
       int value = sendData(0x43, buf, 1);
       return value;
    }   

    public int disableTracking(){
       buf[0] = 0x1;
       int value = sendData(0x44, buf, 1);
       return value;
    }     
   
    public int enableTracking(){
       buf[0] = 0x1;
       int value = sendData(0x45, buf, 1);
       return value;
    }
    */
   
    public int getColorMapFromCameraEngine(){
       int value = getData(0x47, buf, 1);
       return value;
    }
   
    /*
    public int readCameraRegisters(){
       buf[0] = 0x1;
       int value = sendData(0x48, buf, 1);
       return value;
    }     

    public int selectLineTrackingMode(){
       buf[0] = 0x1;
       int value = sendData(0x4C, buf, 1);
       return value;
    }

    public int setADPAModeON(){
       buf[0] = 0x1;
       int value = sendData(0x4E, buf, 1);
       return value;
    }
   
    public int setADPAModeOFF(){
       buf[0] = 0x1;
       int value = sendData(0x4F, buf, 1);
       return value;
    }

    public int pingCameraEngine(){
       buf[0] = 0x1;
       int value = sendData(0x50, buf, 1);
       return value;
    }   

    public int resetCameraEngine(){
       buf[0] = 0x1;
       int value = sendData(0x52, buf, 1);
       return value;
    }   

    public int sendColorMapToCameraEngine(){
       buf[0] = 0x1;
       int value = sendData(0x53, buf, 1);
       return value;
    }     

    public int setIlluminationOff(){
       buf[0] = 0x1;
       int value = sendData(0x54, buf, 1);
       return value;
    }


    public int sortTrackedObjectsByColor(){
       buf[0] = 0x1;
       int value = sendData(0x55, buf, 1);
       return value;
    }
    */   
   
    public int getCameraEngineFirmwareVersion(){
       int value = getData(0x56, buf, 1);
       return value;
    }

    /*
    public int setDoNotSortTrackedObjects(){
       buf[0] = 0x1;
       int value = sendData(0x58, buf, 1);
       return value;
    }
    */   

    public int getNumberObjectsDetected(){
       int value = getData(0x42, buf, 1);
       return value;
    }   
   
    //COLOR MAP1
   
    public int getFirstObjectColor(){
       int value = getData(0x43, buf, 1);
       return value;
    }
   
    public int getXUpperLeft(){
       int value = getData(0x44, buf, 1);
       return value;
    }     

    public int getYUpperLeft(){
       int value = getData(0x45, buf, 1);
       return value;
    }

    public int getXLowerRight(){
       int value = getData(0x46, buf, 1);
       return value;
    }     

    public int getYLowerRight(){
       int value = getData(0x47, buf, 1);
       return value;
    }
   
    //COLOR MAP DATA
   
    public int getColorMapDataRed0(){
       int value = getData(0x80, buf, 1);
       return value;
    }
   
    public int getColorMapDataRed1(){
       int value = getData(0x81, buf, 1);
       return value;
    }

    public int getColorMapDataGreen0(){
       int value = getData(0x90, buf, 1);
       return value;
    }
   
    public int getColorMapDataGreen1(){
       int value = getData(0x91, buf, 1);
       return value;
    }

    public int getColorMapDataBlue0(){
       int value = getData(0xA0, buf, 1);
       return value;
    }
   
    public int getColorMapDataBlue1(){
       int value = getData(0xA1, buf, 1);
       return value;
    }   
}

/*
 * IDEA FROM: gloomyandy
 * URL: http://lejos.sourceforge.net/forum/viewtopic.php?t=489
 *
     public int sendData(int register, byte [] buf, int len) {
        // AS Fix to allow write to work correctly. Avoid using the
        // automatic internal address. Instead send the address as
        // part of the data
        if (len >= byteBuff.length) return -1;
        for(int i=0; i < len; i++ )
            byteBuff[i+1] = buf[i];
        byteBuff[0] = (byte)register;
        int ret = port.i2cStart(address, 0, 0, byteBuff, len+1, 1);
     
        if (ret != 0) return ret;
     
        while (port.i2cBusy() != 0) {
            Thread.yield();
        }
     
        return 0;
    }
 * */



Code: Select all

import lejos.nxt.*;

public class NXTCamTest {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      NXTCamSensor ncs = new NXTCamSensor(SensorPort.S1);

        while (!Button.LEFT.isPressed())
        {
           LCD.drawInt(ncs.getNumberOfObjectDetected(),0,0);
           try{Thread.sleep(20);}catch(Exception e){}
           LCD.drawInt(ncs.getFirstObjectColor(),0,1);
           try{Thread.sleep(20);}catch(Exception e){}
           LCD.drawInt(ncs.getXUpperLeft(),0,2);
           try{Thread.sleep(20);}catch(Exception e){}
           LCD.drawInt(ncs.getYUpperLeft(),0,3);
           try{Thread.sleep(20);}catch(Exception e){}
           LCD.drawInt(ncs.getXLowerRight(),0,4);
           try{Thread.sleep(20);}catch(Exception e){}
           LCD.drawInt(ncs.getXLowerRight(),0,5);
           try{Thread.sleep(20);}catch(Exception e){}
           LCD.drawInt(ncs.getNumberObjectsDetected(),0,6);
           try{Thread.sleep(20);}catch(Exception e){}
           LCD.drawInt(ncs.getCameraEngineFirmwareVersion(),0,7);
           try{Thread.sleep(20);}catch(Exception e){}
                                
           LCD.refresh();
       
        }
       
   }
}


Last edited by esmetaman on Tue Nov 13, 2007 9:52 pm, edited 1 time in total.
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Tue Nov 13, 2007 12:07 am

Have you tried the code?

It looks as if it should work, although you are only returning data for the first object.

It is confusing using the variable "vendor" for returning all the different values. Vendor means the name of the company selling the product, for example, "mindsensors" or "mndsnsrs" (the 8-character version)
.
lawrie
leJOS Team Member
 
Posts: 922
Joined: Mon Feb 05, 2007 1:27 pm

Postby esmetaman » Tue Nov 13, 2007 9:03 pm

Hi Lawrie all times, I receive 0 values.

I am talking with Dick Swan and other guys in Mindsensor's forum to improve the code.

Besides, I think that I will need a I2C method to write values.

I will update this post with the code updated.

Thanks for your interest.

NOTE: Hamilton? jejeje
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Fri Nov 16, 2007 10:42 pm

There is a version of the firmware and classes.jar in the snapshot directory in SVN, which supports I2C writes.

Alonso did not do any better.
lawrie
leJOS Team Member
 
Posts: 922
Joined: Mon Feb 05, 2007 1:27 pm

Thanks Lawrie

Postby esmetaman » Fri Nov 16, 2007 11:36 pm

Thanks Lawrie,

I will try this method.

When do you think to release lejos 0.5? with improvements with I2C and BT?

NOTE: It was a joke :p
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Sat Nov 17, 2007 3:25 pm

The next release should be quite soon as there is a lot of new stuff in the SVN version including: garbage collection, I2C writes, I2C on port 4, greatly improved UltrasonicSensor class, support for more I2C devices (e.g. NRLink and RCX Motor Multiplexer), much faster LCD refreshes, better sound sample playing, etc.

I have not done much work on Bluetooth yet. This things I am planning are:

- supports for PINs other than 1234.
- multibyte packets for Java streams
- method to get signal strength
- improved error handling on connection
- detection of end of file in pccomms

I might also look at the reported problems with streaming a lot of data without sending acknowlegements.

Is there anything else on Bluetooth that you are expecting?
lawrie
leJOS Team Member
 
Posts: 922
Joined: Mon Feb 05, 2007 1:27 pm

Postby esmetaman » Sat Nov 17, 2007 6:59 pm

It sounds very well. Next week I wil try to write you to know how to use subversion system to use "NXJ LAB VERSION" ;)

With relation with my own BT experience your ideas are great.

I need to finish my NXTComm but with my 3 NXT bricks I had problems when I tried to send another time my programs.

Exist any way to format memory in NXT brick?
I noticed that if i tried to execute another program that it doesnt execute bt routines, my NXT bricks runs well, but with NXTComms crashed all and I didnt know the cause.

My idea is create a set of classes to send and receive data using behaviours and threads.

imagine a NXT brick with behaviors that in low layer detect a incomming message, then it proccess the COMMAND and send a command.

it is very important to have this feature to create SOCIAL NXT BRICKS with rules and behaviors. Do you understand me? besides in 2008, Google release BT APIS for Android, the OS for gPhone and I would like to create a bridge with it.
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Sat Nov 17, 2007 8:00 pm

You can call File.format() to reformat flash memory. I could add it to the System menu. I don't know if it would fix your problems.

From what I understand you have hit two problems with Bluetooth:

1. A problem running a Bluetooth problem more than once.
2. A data abort running Bluetooth programs.

I have seen your previous posts about these problems, but did not understand exactly what the problem was.

If you can tell me how to reproduce the problems you are hitting, I will investigate them.

The behavior comms interface you are intending to produce sounds interesting.
lawrie
leJOS Team Member
 
Posts: 922
Joined: Mon Feb 05, 2007 1:27 pm

NXCam updated

Postby esmetaman » Tue Dec 25, 2007 2:42 am

I have updated the Lawrie's work in NXJ trunk with new methods based on Gordon Wyeth
that i saw in: http://www.itee.uq.edu.au/~wyeth/NXT/

[code]
package lejos.nxt;

/**
* Mindsensors NXTCam.
* www.mindsensors.com
*
* Author Lawrie Griffiths
*
*/
public class NXTCam extends I2CSensor {
byte[] buf = new byte[4];

Blob[] arrBlob = new Blob[8];

// I2C Constants associated with camera
private int I2C_addr = 0x02;
private int cmd_reg = 0x41;
private int count_reg = 0x42;
private int data_reg = 0x43;

public NXTCam(I2CPort port)
{
super(port);
port.setType(TYPE_LOWSPEED_9V);
}

/**
* Get the number of objects being tracked
*
* @return number of objects (0 - 8)
*/
public int getNumberOfObjects() {
int ret = getData(0x42, buf, 1);
if(ret != 0) return -1;
return (0xFF & buf[0]);
}

/**
* Get the color number for a tracked object
*
* @param id the object number (starting at zero)
* @return the color of the object (starting at zero)
*/
public int getObjectColor(int id) {
int ret = getData(0x43 + (id * 5), buf, 1);
if(ret != 0) return -1;
return (0xFF & buf[0]);
}

/**
* Get the rectangle containing a tracked object
*
* @param id the object number (starting at zero)
* @return the rectangle
*/
public Rectangle getRectangle(int id) {
for(int i=0;i<4;i++) buf[i] = 0;
getData(0x44 + (id * 5), buf, 4);
return new Rectangle(buf[0] & 0xFF, buf[1] & 0xFF,
(buf[2] & 0xFF) - (buf[0] & 0xFF),
(buf[3] & 0xFF) - (buf[1] & 0xFF));
}

/**
* Send a single byte command represented by a letter
* @param cmd the letter that identifies the command
*/
public void sendCommand(char cmd) {
sendData(0x41, (byte) cmd);
}

//New methods from work developed by: Gordon Wyeth

public void initCam(){
sendCommand('A'); // Sort by size
sendCommand('E'); // Start finding
}

public void void camera_flush()
{
//See method in http://www.itee.uq.edu.au/~wyeth/NXT/nxtcamlib.c
}

public void getBlob(){
byte msg[3];
byte reply[5];
int i;

//camera_flush(camera);

// Request number of blobs from the count register
msg[0] = 2;
msg[1] = I2C_addr;
msg[2] = count_reg;

sendCommand(2);

readI2CReply(camera, reply[0], 1);
nblobs = reply[0];

//camera_flush(camera);

// Get nblobs of blob data from the camera
int color;
int left;
int top;
int right;
int bottom;

for (i = 0; i < nblobs; i++) {

// Request blob data
msg[0] = 2;
msg[1] = I2C_addr;
msg[2] = data_reg + i * 5;

sendCommand(2);

// Get blob data reply

reply = getData(camera, reply[0], 5);

// Put data into global variables.
// Casting to unsigned int throws a warning BUT casting a byte to int is quite
// different to casting to unsigned int, as the former performs sign extension
// (copies first bit of byte to higher bits to make it negative) and latter
// does not. Not sure what Robot C is doing now, but leave it for when they
// fix it. :)
color = ((unsigned int)reply[0]) & 0x00FF;
left = ((unsigned int)reply[1]) & 0x00FF;
top = ((unsigned int)reply[2]) & 0x00FF;
right = ((unsigned int)reply[3]) & 0x00FF;
bottom = ((unsigned int)reply[4]) & 0x00FF;

arrBlob[i] = new Blob(color,left,top,right,bottom);

//camera_flush(camera);

}
}

public Blob getBlob(int blobIndex){
if(blobIndex <= arrBlob.length){
return arrBlob[blobIndex];
}else{
Blob bObj = new Blob(0,0,0,0,0);
return bObj;
}
}

//Used to get the number of blobs detected in a moment
public int getBlobItems(){
return arrBlob.length;
}
[/c

}

ode]
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Blob object

Postby esmetaman » Tue Dec 25, 2007 2:43 am

Code: Select all

public class Blob{

   private int color;
   private int left;
   private int top;
   private int right;
   private int bottom;
   
    public Blob(int _color,int _left,int _top, int _right, int _bottom){
       this.color = _color;
       this.left = _left;
       this.top = _top;
       this.right = _right;
       this.bottom = _bottom;
    }

    public Blob(){
       //Emtpy
    }
   
    /*
     * GET/SET METHODS
     */
   
    public int getColor(){
       return this.color;
    }
   
    public void setColor(int _color){
       this.color = _color;
    }
   
    public int getLeft(){
       return this.left;
    }
   
    public void setLeft(int _left){
       this.left = _left;
    }   

    public int getTop(){
       return this.top;
    }
   
    public void setTop(int _top){
       this.top = _top;
    }
   
    public int getRight(){
       return this.right;
    }
   
    public void setRight(int _right){
       this.right = _right;
    }   

    public int getBottom(){
       return this.bottom;
    }
   
    public void setRight(int _bottom){
       this.bottom = _bottom;
    }     
   
}

Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

BlobChaseExample

Postby esmetaman » Tue Dec 25, 2007 2:44 am

Code: Select all
import lejos.nxt.*;

/**
 * BlobChaseExample.
 * 
 * Author Juan Antonio Breña Moral
 * Email: bren@juanantonio.info
 *
 */

public class BlobChaseExample{
   public static void main(String[] args) {

      //Sensor Information
      NXTCam ncs = new NXTCam(SensorPort.S1);
      ncs.initCam();

      int bItems;
      int x_centre;
      int y_centre;
      int x_error;
      int y_error;
      
        while (!Button.ESCAPE.isPressed())
        {
           ncs.getBlobs();
           bItems = ncs.getBlobItems();
           if(bItems >0){
              LCD.clear();
              LCD.drawString("Tracking ...",0,0);
              LCD.refresh();
              
              Blob bObj = ncs.getBlob(0);
              
             // Find the centre of the blob using double resolution of camera
             x_centre = bObj.getLeft() + bObj.getRight();
             y_centre = bObj.getTop() + bObj.getBottom();
             
             // Compute the error from the desired position of the blob (using double resolution)
             x_error = 176 - x_centre;
             y_error = 143 - y_centre;
             
             // Drive the motors proportional to the error
             Motor.A.Rotate((y_error - x_error) / 5);
             Motor.C.Rotate((y_error + x_error) / 5);
           }else{
              Motor.A.Rotate(0);
              Motor.C.Rotate(0);
              
              String message = "Found " + bItems + " blobs";
              
              LCD.clear();
              LCD.drawString(message,0,0);
              LCD.refresh();              
      
           }
           
        }
   }
}

Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 1 guest

more stuff