View unanswered posts | View active topics It is currently Sat Oct 25, 2014 6:50 pm






Reply to topic  [ 7 posts ] 
Reading IR data 
Author Message
Rookie

Joined: Thu Jul 26, 2007 12:16 am
Posts: 5
Post Reading IR data
I am attempting to get the NXT and RCX communicating using the minsensors NRLink-Nx. So far I can get the RCX to respond quite well to the NXT. The problem is receiving a message back form the RCX. I have a simple program stored in program slots 1 and 2 of the RCX; each just writes a specific value to the IR port. The NXT program executes each of these RCX programs in turn and attempts to read the IR value sent. The issue I have is finding info on how to read IR using I2C. I am making a lot of assumptions. Here are the programs:

RCX slot 1:

task main()
{
sendMessage(nImmediateBatteryLevel);
PlaySound(soundException);
}

RCX slot 2:

task main()
{
sendMessage(nClockMinutes);
PlaySound(soundBeepBeep);
}

NXT program:

const ubyte NRLinkID = 0x02;
const ubyte NRLinkCommandReg = 0x41;
const tSensors NRLinkPort = S1;
const ubyte NRLinkDefault = 0x44;
const ubyte NRLinkFlush = 0x46;
const ubyte NRLinkMacro = 0x52;
const ubyte Run_Program_1 = 0x09;
const ubyte Run_Program_2 = 0x0D;
const ubyte Beep = 0x39;

void NRLinkRunMacro(byte NRLinkMacroAdd)
{
byte NRLinkMsg[5];
const byte MsgSize = 0;
const byte Address = 1;
const byte CommandAddress = 2;
const byte Command = 3;
const byte MacroAddress = 4;

NRLinkMsg[MsgSize] = 4;
NRLinkMsg[Address] = NRLinkID;
NRLinkMsg[CommandAddress] = NRLinkCommandReg ;
NRLinkMsg[Command] = NRLinkMacro;
NRLinkMsg[MacroAddress] = NRLinkMacroAdd;

while (nI2CStatus[NRLinkPort] == STAT_COMM_PENDING) {}
sendI2CMsg(NRLinkPort, NRLinkMsg[0], 0);
}

void NRLinkCommand(byte NRLinkCommand)
{
byte NRLinkMsg[5];
const byte MsgSize = 0;
const byte Address = 1;
const byte CommandAddress = 2;
const byte Command = 3;

NRLinkMsg[MsgSize] = 3;
NRLinkMsg[Address] = NRLinkID;
NRLinkMsg[CommandAddress] = NRLinkCommandReg ;
NRLinkMsg[Command] = NRLinkCommand;

while (nI2CStatus[NRLinkPort] == STAT_COMM_PENDING){ }
sendI2CMsg(NRLinkPort, NRLinkMsg[0], 0);
}

task main()
{
byte data[2];
nI2CBytesReady[NRLinkPort] = 0;

SensorType[NRLinkPort] = sensorI2CCustomFast9V;
nxtDisplayTextLine(0,"mindsensors.com");
nxtDisplayTextLine(1,"NRLink");
NRLinkCommand(NRLinkFlush);
NRLinkCommand(NRLinkDefault);

while(true)
{
// Run program 1 on RCX
nI2CBytesReady[NRLinkPort] = 0;
NRLinkRunMacro(Run_Program_1);

// read and display the returned value
while(nI2CStatus[NRLinkPort]== STAT_COMM_PENDING){}
readI2CReply(NRLinkPort, data[0], 1);
nxtDisplayTextLine(3, "battery %0d%0d", (short) data[0], (short) data[0]);

wait10Msec(100);

// Run program 2 on RCX
nI2CBytesReady[NRLinkPort] = 0;
NRLinkRunMacro(Run_Program_2);

// read and display the returned value
while(nI2CStatus[NRLinkPort]== STAT_COMM_PENDING){}
readI2CReply(NRLinkPort, data[0], 1);
nxtDisplayTextLine(3, "clock %0d%0d", (short) data[0], (short) data[0]);

wait10Msec(100);
}

StopAllTasks();
}

What I should see on the NXT display is alternating readings for battery and clock, but the values are the same: 8282

I am sure I am not doing things correctly, but I can't find docs to tell me how this should be done.

Thanks for any help you can provide.


Thu Jul 26, 2007 12:33 am
Profile
Rookie

Joined: Thu Jul 26, 2007 12:16 am
Posts: 5
Post Additional info/testing results
I'm trying to isolate the location of the problem. I have verified that the RCX is sending IR data - by viewing the IR port thru a digital camera - the IR port is flashing in sync with the PlaySound() function.

Also, I have inserted this line:

nxtDisplayTextLine(5, "read count = %d", nI2CBytesReady[0]);

right before the read line:

readI2CReply(NRLinkPort, data[0], 1);

and the displayed value is zero. So there seems to be a problem with RobotC not reading IR data from the NRLink device. :(
I have also written an RCX program to go into continuous loop of writing IR data - the NXT doesn't see that either.


Tue Jul 31, 2007 11:59 am
Profile
Rookie

Joined: Thu Jul 26, 2007 12:16 am
Posts: 5
Post 
Another result: the NRLink is receiving the data - but RobotC is not reading the sensor. How do I know? Careful observation... When the RCX program sends data it also beeps. Each time it beeps there is an LED on the NRLink that blinks - putting the RCX into a send loop causes the NRLink to blink in sync with the beeps.
Image
So, the hardware is doing its job - but RobotC does not report there is any data to read: nI2CBytesReady[NRLinkPort]) always contains a zero.

Is there something else I need to do to enable reading IR in RobotC?

I have simplified the NXT code to the bare minimum. This program simply watches the incoming data buffer to contain data:

Code:
const tSensors NRLinkPort        = S1;
byte data[4];

task main()
{
  SensorType[NRLinkPort] = sensorI2CCustomFast9V;
  nxtDisplayTextLine(0,"mindsensors.com");
  nxtDisplayTextLine(1,"NRLink");

  while(true)
    {
    // read and display the returned value
    while(nI2CStatus[NRLinkPort]== STAT_COMM_PENDING ||
            nI2CBytesReady[0] == 0){}

    nxtDisplayTextLine(4, "read count = %d", nI2CBytesReady[NRLinkPort]);
    readI2CReply(NRLinkPort, data[0], 1);
    nxtDisplayTextLine(5, "last read = %d", (int)data[0]);
    }

  StopAllTasks();
}


The while() loop never terminates.


Tue Jul 31, 2007 3:27 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
There are three steps to reading an I2C message from a sensor.
1. Send a message to the I2C sensor to initiate the read. Look at the program "NXT HiTechnic Accelerometer" in the RobotC sample programs folder for an example that you can clone. Generally, as a minimum, this program sets up the sensor's internal register pointer to indicate what sensor registers are being read. Most sensors have multiple registers.
2. Continuously loop checking whether I2C messaging status is "busy". Based on the non-busy results "no error" or various "errors" you can proceed to step 3 or abort.
3. Now read the reply message.
Your program skipped step 1. Once you add this it looks like it will work.


Wed Aug 01, 2007 2:21 pm
Profile
Rookie

Joined: Thu Jul 26, 2007 12:16 am
Posts: 5
Post 
Thanks for the info - I've sent a reply over in Lugnet. Would you prefer this forum for this type of thread? I try to avoid double-posts. I was using lugnet to measure the communities experience with RobotC. I'm a long time poster to Lugnet - brand new to RobotC.


Wed Aug 01, 2007 11:54 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
Feel free to post in lugnut as well, better chances of getting the help you need.

Iv been having problems getting my NRLink to read messages as well, so post any results you get here B-)

Good Luck
Scott

_________________
Mmmm Legos B-)

My Robot Projects:
http://www.freewebs.com/robotprojects/


Fri Aug 03, 2007 5:10 pm
Profile WWW
Rookie

Joined: Thu Jul 26, 2007 12:16 am
Posts: 5
Post 
I am making progress. I can read data - but I don't have the protocol worked out yet.

My objective is to create a set of include files (NRLink.c and NRLink.h) that will encapsulate the process and simplify using the sensor.

-Wes


Fri Aug 03, 2007 7:20 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.