Accessing the compass programmatically

In order to access the compass capability, you'll need to enable the I2C library on Raspberry. If you used the IR sensor and ADC additional hardware in Chapter 4, Avoiding Obstacles Using Sensors, you will have already done this. If not, follow these instructions to enable the I2C interface:

  1. Run raspi-config. Select the Configure advanced settings, as shown in the following screenshot:
    Accessing the compass programmatically

    From the next selection page, select the Enable/disable automatic loading of the I2C interface, as shown in the following screenshot:

    Accessing the compass programmatically

    Then select yes, as shown in the following screenshot:

    Accessing the compass programmatically

    You'll also want to edit the file /etc/modules and add the lines i2c-bcm2708 and i2c-dev, as shown in the following screenshot:

    Accessing the compass programmatically

    And one final edit, change the last line in /boot/config.txt, as shown in the following screenshot:

    Accessing the compass programmatically

    Now, reboot Raspberry Pi.

  2. With the device connected, you can see if the system knows about your device. To do this, type the following command:
    Accessing the compass programmatically

    You can see the device at 1e.

  3. Now you communicate with your digital compass. To do this, you'll need to create a Python program. But before you create your Python code, you'll want to download a library that will make this all much easier. To do this, first create a directory a directory called compass and cd to that directory. Then, type git clone https://github.com/quick2wire/quick2wire-python-api.git to download the quick2wire-python-api library. Finally, type git clone https://bitbucket.org/thinkbowl/i2clibraries.git to get the i2clibraries.

    You'll also need to set some environment variables. Do this by going to your home directory and editing the .bashrc file, adding these two lines at the end:

    Accessing the compass programmatically
  4. Now, you can create the following Python code:
    Accessing the compass programmatically
  5. Now, run the code by typing python3 compass.py command and you should see:
    Accessing the compass programmatically

    Now, you can add direction to your project! As you move the device around, you should see the Heading value change.

    Note

    This is a basic program; you can find out more about other features that are available with this library at http://think-bowl.com/raspberry-pi/i2c-python-library-3-axis-digital-compass-hmc5883l-with-the-raspberry-pi/.

    One final step in developing your compass code is to make it a file where the functions can then be imported to a different Python program. To do this, edit the file so that all of the code is in functions, as shown by the following:

    Accessing the compass programmatically

    Then you'll be able to use the import capability of Python to import this functionality into a different Python file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.191.86