View unanswered posts | View active topics It is currently Sun Oct 26, 2014 12:27 am






Reply to topic  [ 7 posts ] 
Bug: Repeated I2C writes fail 
Author Message
Rookie

Joined: Mon Oct 20, 2008 12:49 pm
Posts: 6
Post Bug: Repeated I2C writes fail
Or atleast I think it a bug:

Under some conditions writing to I2C sensors command register will fail. For example when I want to change the address of my Mindsensors' sensor I will send a sequence of commands to the command register of the sensor. However, sending more than one command after another will always fail thus making the changing of the I2C address of the sensor impossible. I have confirmed this bug with several different kinds of codes and sensors. Seemingly identical code in NXC works just fine so it's not the sensors' fault. The same goes for trying to write a word in to the sensors registers (like Minsensors Accelerometers offsets).

Here is one of the codes I used:
Code:
#define port S1
#define IDnow 0x02 //Default address..
#define IDnew 0x04 //New address...

void I2CCommand(byte bCommand)
{
    nI2CBytesReady[S1]=0; //Not neccessary really...
    byte cmpsMsg[5];
    const byte MsgSize        = 0;
    const byte CmpsAddress    = 1;
    const byte CommandAddress = 2;
    const byte Command        = 3;


    cmpsMsg[MsgSize]          = 3;
    cmpsMsg[CmpsAddress]      = IDnow;
    cmpsMsg[CommandAddress]   = 0x41 ;
    cmpsMsg[Command]          = bCommand;

    while (nI2CStatus[port] == STAT_COMM_PENDING);   // Wait for I2C bus to be ready
    sendI2CMsg(port, cmpsMsg[0], 0);                    // Send the message
}


task main()
{
   SensorType[S1]=sensorI2CCustom9V;
        //Command sequence: 0xA0,0xAA,0xA5,"NewI2CAddress"
   I2CCommand(0xA0);
   wait10Msec(20);
   I2CCommand(0xAA);
   wait10Msec(20);
   I2CCommand(0xA5);
   wait10Msec(20);
   I2CCommand(IDnew);
   wait10Msec(200);
}


Is there possibly a work around for this bug? Ofcourse I'm able to change the settings of my sensors with NXC but changing firmware every 5 minutes or so is rather impractical..

EDIT: And I'm using RobotC v. 1.45...


Fri Oct 24, 2008 11:24 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post Re: Bug: Repeated I2C writes fail
Hey Staffer,

What Mindsensors sensors are you using? I have a pretty good collection of their sensors and they seem to communicate fine. One source of problems could be faulty cables, your cable could be positional and will only send messages some of the time. I have had allot of problems with this, and rewritten allot of good code lol ;-).

One thing I noticed in your code is the array cmpsMsg is being initialized with 5 indexes, but you only declare 4 of the indexes, 0 - 3. I think you need to change cmpsMsg[5] to cmpsMsg[4]. It may be trying to send an index of information that doesn't exist, and I'm not sure how a sensor would react.

Let me know if any of this helps B-)
Scott

_________________
Mmmm Legos B-)

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


Wed Oct 29, 2008 2:05 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post Re: Bug: Repeated I2C writes fail
I went and looked at the code I use for changing my sensors addresses. Here is the code, I hope this helps. I noticed that the array size declared in mine is 8, I'm not sure why its so big, it is something to fiddle with ;-)

Code:
/*
 * change_add.c v2.0
 *
 * This is driver/utility program to use  with mindsensors.com  sensor to
 * change the address.
 *
 * written by Dr. Nitin Patil
 * modified by Scott Briscoe October 28 2008
 * 2006(c) mindsensors.com
 */

/*
 * NXT I2C register storage
 *
 * 0x41 is command register
 * writing  0xA0 0xAA 0x A5 and <new address> will change the address of
 * the device to new address
 * New address is effective immediately.
 */

//////////////////////////////////////////////////////////////////////////////
//
//  change the adress of the I2C sensor old Addresswith the NewAddress
//
/////////////////////////////////////////////////////////////////////////////

#pragma fileExtension("rtm")  // Makes A Try Me Program //
#pragma platform("NXT")


#define PortToChange S1        // Hook sensor to change to this port //


void  Change_add(byte oldAddress,byte newAddress)
{
   byte i2cMsg[8];
   const byte kMsgSize        = 0;
   const byte kDeviceAddress    = 1;
   const byte kCommandAddress  = 2;
   const byte kCommand  = 3;

   nI2CBytesReady[PortToChange]    = 0;
   SensorType[PortToChange] = sensorI2CCustom;

   // Build the I2C message
  i2cMsg[kMsgSize]           = 3;
  i2cMsg[kDeviceAddress]     = oldAddress ;
  i2cMsg[kCommandAddress]    =0x41;

   i2cMsg[kCommand]=0xA0;
   while (nI2CStatus[PortToChange] == STAT_COMM_PENDING);  // Wait till I2C bus is ready
   sendI2CMsg(PortToChange, i2cMsg[0], 0);                           // Send the message
   wait10Msec(10);

   i2cMsg[kCommand]=0xAA;
   while (nI2CStatus[PortToChange] == STAT_COMM_PENDING);  // Wait till I2C bus is ready
   sendI2CMsg(PortToChange, i2cMsg[0], 0);                           // Send the message
   wait10Msec(10);

   i2cMsg[kCommand]=0xA5;
   while (nI2CStatus[PortToChange] == STAT_COMM_PENDING);  // Wait till I2C bus is ready
   sendI2CMsg(PortToChange, i2cMsg[0], 0);                           // Send the message
   wait10Msec(10);

   i2cMsg[kCommand]=newAddress;
   while (nI2CStatus[PortToChange] == STAT_COMM_PENDING);  // Wait till I2C bus is ready
   sendI2CMsg(PortToChange, i2cMsg[0], 0);                           // Send the message
   wait10Msec(10);

}

//////////////////////////////////////////////////////////////////////////////
//
//      demo main function to show change adress function
//
/////////////////////////////////////////////////////////////////////////////



task main()
{
   nxtDisplayTextLine(1, "mindsensors.com");
   nxtDisplayTextLine(2, "address change");


// Change_add(Current Address , New Address);
    Change_add(0x02,0xA2);

}

_________________
Mmmm Legos B-)

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


Wed Oct 29, 2008 2:18 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3227
Location: Rotterdam, The Netherlands
Post Re: Bug: Repeated I2C writes fail
Are changes made to the I2C address persistent across power cycles? In other words, is the new address stored somewhere should the sensor be disconnected and used at a later stage?

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Wed Oct 29, 2008 6:24 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post Re: Bug: Repeated I2C writes fail
Hey Mightor,

Yes, when you change the address of a I2C sensor, it saves it in the sensors registry for later use, and it doesn't reset on a power cycle.

Scott B-)

_________________
Mmmm Legos B-)

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


Wed Oct 29, 2008 9:11 pm
Profile WWW
Rookie

Joined: Mon Oct 20, 2008 12:49 pm
Posts: 6
Post Re: Bug: Repeated I2C writes fail
starwarslegokid wrote:
I went and looked at the code I use for changing my sensors addresses. Here is the code, I hope this helps. I noticed that the array size declared in mine is 8, I'm not sure why its so big, it is something to fiddle with ;-)



-I'm using Minsensors Servo controller, g-sensor and compass sensor and I'm back to RobotC 1.40 right now.. The way I see it is that the size of the array shouldn't matter because the number of the bytes to be sent over I2C is set in the first byte of the array. I will, however, try and see if my cables are faulty which would be rather odd as reading the sensors registers virtually never fails (and changing the address works with NXC). As for the Mindsensors.com code sample it never worked for me but if it works for you I'm a bit confused.... :? (I think I'll have to retest that too.)

Staffer


BTW/OFFTOPIC, my NXT (and RobotC with it :D) went today airborne!! Everything worked fine until my elevator servo broke but luckily NXT managed to land my glider somewhat safely (big thanks to the integral part of the PID controller which compensated the servo failure).. I think I'll post the video to YouTube some time soon... :bigthumb:


Sun Nov 02, 2008 1:32 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post Re: Bug: Repeated I2C writes fail
lol I would love to see a video of that!

Let me know if you are still having issues, this may pertain to allot of other people. I'm working on making updated drivers for my Mindsensor sensors. I have an updated compass driver if your interested, it has error checking code in case the NXT cannot communicate with the compass, and it happens more frequently than I like, mostly because of my cables.

Good luck B-)
Scott

_________________
Mmmm Legos B-)

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


Thu Nov 06, 2008 12:05 am
Profile WWW
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.