Trouble accessing CMPS03 via I2CGeneric
Navigates to RoboticsConnection.com Home RoboticsConnection.com HomePage
RoboticsConnection User Forum
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        



Trouble accessing CMPS03 via I2CGeneric Expand / Collapse
Author
Message
Posted Thursday, March 27, 2008 5:21 PM
Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior Member

Group: Forum Members
Last Login: Thursday, April 03, 2008 8:22 PM
Posts: 16, Visits: 24
I attempted to roughly follow the example for the I2CDevice accessing CMPS03 compass, posted at:
http://www.roboticsconnection.com/multimedia/libraries/MSDNDocumentation/_23n15r2ti.htm

The main difference being that I am doing it from a Windows Forms not Console.  Here is what I coded, which is not working (always returns "255").  [FYI, my compass is working fine using Serializer.NET class CMPS03, so its not a hardware issue].  Let me know if you see anything wrong here ...

[Declaration]:
private static I2CDevice i2cd;


[Form_Load]:
i2c = new I2CDevice(serializer)
i2cd.I2CAddress = 0xC0;
i2cd.ReadCommand = "1";
i2cd.WriteCommand = "1";
//I notice there is no i2cd.Update() like I would normally use for CMPS03 class?

[Periodic_Timer_Tick]:
serializer.PumpEvents();
i2cd.Write();
lblCompass.Text = i2cd.Read().ToString();    //always posts 255 to my label

Post #260
Posted Thursday, March 27, 2008 8:48 PM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Administrators
Last Login: Tuesday, September 02, 2008 9:05 AM
Posts: 270, Visits: 400
Try something like this...

private void ReadHighResCompassRegister()
{
    i2cd.Write(0xC0, "2");                      // Start reading at register 2
    string reading = i2cd.Read(0xC0, "2"); // Read registers/bytes 2 and 3
    // Bytes returned are separated by spaces:
    string [] readings = reading.Split(' ');
    int pos = 0;
    int heading = 0;
    // Convert bytes to word:
    foreach (string r in readings)
    {
        // There's probably a simpler way to do this...
        int part = Convert.ToByte(r) << (pos * 1);
        heading = heading | part;
    }

    lblCompass.Text = heading.ToString();
}

Best Regards!

Jason Summerour
President,
Summerour Robotics Corporation
Microsoft MVP
www.roboticsconnection.com

Post #263
Posted Friday, March 28, 2008 4:16 PM
Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior Member

Group: Forum Members
Last Login: Thursday, April 03, 2008 8:22 PM
Posts: 16, Visits: 24
Jason,

Thanks for quick response.  Had some trouble with the code, so I started stepping through it.  First problem seems to be that rather than strReadings getting "255 255" as your code assumed, it was actually pulling the value "255/r/n255r/n".  So before I could do the Split(' '), I had to do a replace 

readings.Replace("\r", " ");

With that taken care of, it now gets stuck on the line:

int part = Convert.ToByte(r) << (pos*1);

The runtime error it gives (which occurs the first time it hits this line) is "Input string was not in the correct format".

Since I am not entirely sure what you are doing on this row, I'm not sure how to debug it.  I'm not too familiar with the << operator, although I just finished reading a little about it on MSDN.  And I am not sure why you would multiply pos by 1, that wouldn't ever do anything would it?

However, that problem aside, I'm thinking there is a bug in the I2CDevice class when it comes to reading the I2C registers.  I just compiled and ran your example code for the I2CDevice http://www.roboticsconnection.com/multimedia/libraries/MSDNDocumentation/ (modifying only the COM port, BaudRate, and the I2CAddress to the TPA81 so it would fully run on my system), and it too read only "255" off the compass sensor and 255 for all registers of the TPA81 as well.  (Both these sensors work fine when read from their respective CMPS03 and TPA81 classes).

Post #267
Posted Friday, March 28, 2008 9:05 PM
Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior Member

Group: Forum Members
Last Login: Thursday, April 03, 2008 8:22 PM
Posts: 16, Visits: 24
UPDATE:  I updated the firmware in the Serializer and it started working.  Guess I should have thought of that one first ... 
Post #268
Posted Saturday, March 29, 2008 1:40 PM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Administrators
Last Login: Tuesday, September 02, 2008 9:05 AM
Posts: 270, Visits: 400
Cool!  That works!

I was using the '<<' shift operator t shift each sequential bit to the left, to convert the bytes into an int.

I didn't have time to compile it and debug it, and typed it at almost 1:00am.

Also, now that you have the updated firmware, you should be getting spaces between the values, instead of the '\r'.

Best Regards!

Jason Summerour
President,
Summerour Robotics Corporation
Microsoft MVP
www.roboticsconnection.com

Post #269
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: jsummerour

Permissions Expand / Collapse

All times are GMT -8:00, Time now is 5:06pm

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.156. 9 queries. Compression Disabled.