Shop OBEX P1 Docs P2 Docs Learn Events
HMC5883L and @file libcompass3d.c — Parallax Forums

HMC5883L and @file libcompass3d.c

oakguyoakguy Posts: 21
edited 2013-11-04 13:13 in Learn with BlocklyProp
Hi
I tried running this sketch and the program seems to freeze at the second i2C instance. Can I ask why two instances are created?

int x, y, z; // Declare x, y, & z axis variables
pause(1000);
print("c32\n");
i2c *bus = i2c_newbus(3, 2, 0); // New I2C bus SCL=P3, SDA=P2
compass_init(bus); // Initialize compass on bus.


print("c54\n");
i2c *bus2 = i2c_newbus(5, 4, 0); // New I2C bus SCL=P3, SDA=P2
compass_init(bus2); // Initialize compass on bus.

My C sketch always freezes at the second instance. Any help is appreciated!

Oakguy

Comments

  • jazzedjazzed Posts: 11,803
    edited 2013-10-22 08:40
    Hi.

    Do you mean that c54 is output and nothing else happens?
    It's hard to tell, but it looks like your program ends after compass_init(bus2).

    Is your code pasted into main() ?
    If you copy/paste the whole file here, it would be clearer.
  • oakguyoakguy Posts: 21
    edited 2013-10-22 10:27
    Ok, here is the file from the learn folder bundled with the Activity Bot. You can see in Andy's file that there are two instances where he constructs the bus and the sketch seems to hang on that.

    Thanks!

    /*
    * @file libcompass3d.c
    *
    * @author Andy Lindsay
    *
    * @copyright
    * Copyright (C) Parallax, Inc. 2013. All Rights MIT Licensed.
    *
    * @brief Temporary project and test harness for the name library.
    *
    * SCL connected to P3 and SDA connected to P2. Display measurement results in
    * SimpleIDE Terminal
    */


    #include "simpletools.h" // Include simpletools header
    #include "compass3d.h" // Include compass3d header
    #include "simplei2c.h"


    int main() // Main function
    {




    int x, y, z; // Declare x, y, & z axis variables
    pause(1000);
    print("c32\n");
    i2c *bus = i2c_newbus(3, 2, 0); // New I2C bus SCL=P3, SDA=P2
    compass_init(bus); // Initialize compass on bus.


    print("c54\n");
    i2c *bus2 = i2c_newbus(5, 4, 0); // New I2C bus SCL=P3, SDA=P2
    compass_init(bus2); // Initialize compass on bus.


    print("%c", HOME);


    while(1) // Repeat indefinitely
    {
    print("%c", HOME);
    print("c32\n");
    compass_read(bus, &x, &y, &z); // Compass vals -> variables


    int *px, *py, *pz;


    px = &x;
    py = &y;
    pz = &z;

    *px = x;
    *py = y;
    *pz = z;


    float heading = atan2(x, y);
    if(heading < 0)
    {
    heading += 2.0 * 3.14;
    }

    float headingDegrees = heading * 180/3.14;


    print("\nx=%d, y=%d, z=%d%c\n", // Display raw compass values
    x, y, z, CLREOL);
    print("heading = %f, \n", // Display raw compass values
    headingDegrees);
    waitcnt(CLKFREQ/2+CNT);


    print("c54\n");
    compass_read(bus2, &x, &y, &z); // Compass vals -> variables


    px = &x;
    py = &y;
    pz = &z;

    *px = x;
    *py = y;
    *pz = z;


    heading = atan2(x, y);
    if(heading < 0)
    {
    heading += 2.0 * 3.14;
    }

    headingDegrees = heading * 180/3.14;


    print("\nx=%d, y=%d, z=%d%c\n", // Display raw compass values
    x, y, z, CLREOL);
    print("heading = %f, \n", // Display raw compass values
    headingDegrees);
    pause(500); // Wait 1/2 second
    }




    }
  • jazzedjazzed Posts: 11,803
    edited 2013-10-22 12:05
    I see.

    So, you have added a second compass?

    I have an appointment this afternoon ... maybe Andy will see this.
  • edited 2013-10-22 14:23
    I would not recommend using the test harness code from the library folder. Instead, open Test Compass HMC5883L.side from this folder and run it:

    ...\Documents\SimpleIDE\Learn\Examples\Devices\Sensor\Compass 3-Axis HMC5883L

    Andy
  • oakguyoakguy Posts: 21
    edited 2013-11-04 10:16
    Andy
    Your right, that sketch does run without error, however, it does not return a 0 - 360 heading value. Just so I'm running the right one:
    Test Compass HMC5883L.c


    Version 0.94 for use with SimpleIDE 9.40 and its Simple Libraries

    Test compass with SCL connected to P3 and SDA connected to P2. Display
    measurement results in SimpleIDE Terminal.

    learn.parallax.com/propeller-c-tutorials

    I only get a -70 to a -80 for a heading.

    Thanks
    Guy
  • oakguyoakguy Posts: 21
    edited 2013-11-04 10:28
    Andy
    I checked the math and the formulas are correct. Perhaps its my chip that is defective. I entered the y and x in Excel and ended up with the same value your sketch computed! My bad!



    206
    -1583
    -82.6275
    -76.3475



    Thanks
    Guy
  • edited 2013-11-04 13:13
    Thanks for the update. I just tested it with a compass and board I've got on-hand, and it correctly reported the entire 360-degree range of headings.

    Let's check and make sure our circuits match:

    Compass GND -> Activity Board GND
    Compass VIN -> Activity Board 3.3 V
    Compass SCL -> 100 ohm resistor (brown-black-brown) -> Activity Board P3
    Compass SDA -> 100 ohm resistor (brown-black-brown) -> Activity Board P2

    NOTE: 220 ohm resistors (red-red-brown) are okay too.

    Another item to check is possible interference from strong sources of magnetic fields. Sometimes a metal brace under the table can be magnetized and affects the measurements. Nearby components and wires can also affect the measurements, but not to the point where they appear to be stuck in one position. I usually hold my board well above the table when I test.

    Last item, make sure you've got the latest Learn folder and SimpleIDE:

    SimpleIDE v0.9.43
    http://learn.parallax.com/propeller-c-set-simpleide

    Learn folder 2013.11.01
    http://learn.parallax.com/propeller-c-set-simpleide/update-your-learn-folder

    Example Code from ....Documents\SimpleIDE\Learn\Examples\Devices\Sensor\Compass 3-Axis HMC5883L\
    /*
      Test Compass HMC5883L.c
    
      Version 0.94 for use with SimpleIDE 9.40 and its Simple Libraries
      
      Test compass with SCL connected to P3 and SDA connected to P2. Display
      measurement results in SimpleIDE Terminal.
      
     learn.parallax.com/propeller-c-tutorials
    */
    
    #include "simpletools.h"                      // Include simpletools header
    #include "compass3d.h"                        // Include compass3d header
    
    int main()                                    // main function
    {
      int x, y, z;                                // Declare x, y, & z axis variables
      
      i2c *bus = i2c_newbus(3, 2, 0);             // New I2C bus SCL=P3, SDA=P2
      compass_init(bus);                          // Initialize compass on bus.
    
      while(1)                                    // Repeat indefinitely
      {
        compass_read(bus, &x, &y, &z);            // Compass vals -> variables
        print("%cx=%d, y=%d, z=%d%c\n",           // Display compass variables
               HOME, x, y, z, CLREOL);
    
        float fy = (float) y;                     // Ints to floats
        float fx = (float) x;
        float heading = atan2(fy, fx) * 180.0/PI; // Calculate heading with floats
        if(heading < 0.0)                         // Convert -180...+180 to 0...360
          heading = (360.0 + heading);
        print("heading = %.2f%c\n",               // Display heading
                            heading, CLREOL);       
    
        pause(500);                               // Wait 1/2 second
      }
    }
    
Sign In or Register to comment.