This type of sensor is different from the custom sensors like the light, the touch and the sound.
First of all very handy is this site:
http://www.koders.com/info.aspx?c=ProjectInfo&pid=RHGY8H3PWYCG28MNNNS8V1BLGC
where all the classes are shown.
These class can be found in the lejos folder too: lejos_nxj/src/java/classes as textfiles, but the Koders is handy, it has a search.
You can see that the I2Csensor class is an abstract class: you have to use the super() as a constructor:
//------------------------------------------------------------
// I2Csensor class
//------------------------------------------------------------
For instance you can see how to write a class for an I2Csensor which is not in the list (i did the clock), see on Koders how it is done with the tilt sensor.
import lejos.nxt.*;
public class clock extends I2CSensor
{
byte[] buf = new byte[2];
public clock(SensorPort port)
{
super(port);
}
public int getByte(byte myByte)
{
int ret = getData(myByte, buf, 1);
return ret;
}
}
Then continuing trying to get the clocksensor to work under lejos JAVA, (it works in Dev Cpp).
i found this bit of important information:
The sensor ports have two channels to them:etc gives the right seconds.....
1) Analog channel. These are read with the raw value. This is used to
read devices like the light sensor and push button and other resistive
sensors.
2) Digital wires. These can be used as inputs or outputs. On the light sensor
they are used to turn the LED on or off. Thay also can be used to make an I2C
port which can be used to communicate with a compass sensor or ultrasonic
sensor.
Note!!! I2C only works with port 1.. port 3 at the moment. There is something
wrong with trying to use it on port 4 at present, but this is being debugged.
So, trying to use the raw reading method for an I2C sensor does not even look
at the correct wires on the port.
(by Charles Manning)
But that is not the way to proceed.
The problem is the getData.
This method used i2cStart( address, register, len, buf, len, 0);
where the address is written in the I2Csensor class as 1.
Actually this int 1 is shifted in the lowlevel routine (written in C, can also be
found in the lejos folder: lejos_nxj/src/nxtvm\platform\nxt : i2c.c , in C)
This address is the what is called the factory address. But for the tilt and compass sensors this is 0x02.
The clocksensor has a factory address 0xD0.
All a bit mysterious, till I made the link: in the lejos JAVA I2C sensor class the factory setting is shifted,
to become 1: 2>>1 = 1.
So trying: 0xD0>>1 = 0xD0/2 (as address) worked in the i2cStart method.
I wrote a method for the clock sensor oerriding the getData in the I2C class.
public int getData(int register, byte [] buf, int len) {
int ret = myPort.i2cStart(0xD0 / 2, register, len, buf, len, 0);
if (ret != 0) return ret;
while (myPort.i2cBusy() != 0) {
Thread.yield();
}
return 0;
}
then you can use the data provided by the mindsensor pdf:
public int rawSec()
{
int ret = getData(0x00, buf, 1);
return (ret == 0 ? (buf[0] ) : -1);
}
public int rawMin()
{
int ret = getData(0x01, buf, 1);
return (ret == 0 ? (buf[0] ) : -1);
}
public int rawHoures()
{
int ret = getData(0x02, buf, 1);
return (ret == 0 ? (buf[0] ) : -1);
}
etc, why "rawSec", because of the byte structure:
(see clock sensor pdf)
public int getSec()
{
int a = rawSec();
return a - (a>>4) *6;
}
public int getMin()
{
int a = rawHoures();
return a - (a>>4) *6;
}
The clocksensor class will be available here soon for download.
//------------------------------------------------------------
// long in Lejos?
//------------------------------------------------------------
You can get a time in milliseconds on the NXT. I used it for measuring the responsetime of the sensors. This is given in: long currentTimeMillis()
But adding arithmetical operations are not supported in the Lejos Java (told Eclipse).
So what to do?
This was my solution:
before the measurments
double startTime= (double) System.currentTimeMillis();
after:
double nowTime = (double)System.currentTimeMillis() ;
int now = (int)(( nowTime - startTime)/1000);
LCD.drawInt( now ,0,5) ;
LCD.refresh();
Mind also the System before the currentTimeMillis();!
//--------------------------------------------------------------------------------------------
0 comments:
Post a Comment