Skip to content

Instantly share code, notes, and snippets.

@notexactlyawe
Last active April 20, 2018 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notexactlyawe/c34e4edbe64c6cca46ec9e8994de994d to your computer and use it in GitHub Desktop.
Save notexactlyawe/c34e4edbe64c6cca46ec9e8994de994d to your computer and use it in GitHub Desktop.
Code to read the Adafruit LSM303 sensor and send the readings to Pure Data
""" Python file to demonstrate reading the LSM303 and pass data to Pure Data
Author: Cameron MacLeod
Date: 2018/03/17
Contact: github.com/notexactlyawe
"""
import time
import argparse
import Adafruit_LSM303
from math import atan2, pi
from pythonosc import udp_client
from pythonosc import osc_message_builder
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--ip', type=str, help="IP address of the OSC server")
parser.add_argument('--port', type=int, help="Port OSC server is listening on")
args = parser.parse_args()
# This is the IP and port of your Pure Data instance
PD_IP = "127.0.0.1"
if args.ip is not None:
PD_IP = args.ip
PD_PORT = 8000
if args.port is not None:
PD_PORT = args.port
# This can be set to whatever you like and will be the address the OSC message
# is sent with
OSC_ADDR = "/readings"
# Create objects used to read LSM303 and communicate with remote OSC server
lsm303 = Adafruit_LSM303.LSM303()
client = udp_client.SimpleUDPClient(PD_IP, PD_PORT)
def compute_heading(mag_read):
""" Computes the compass heading from the magnetometer X and Y.
Returns a float in degrees between 0 and 360.
"""
return ((atan2((mag_read[1] - -933), (mag_read[0]- 812)) * 180) / pi) + 180
# was (atan2(mag_read[0], mag_read[1]) * 180) / pi + 180 added brackets (unesc.) and swapped inputs to arcTan2.
def send_readings_to_pd(accel, mag, heading):
""" Sends compass, accelerometer and heading readings to PD
In the order acc_x, acc_y, acc_z, mag_x, mag_y, mag_z, heading
"""
msg = osc_message_builder.OscMessageBuilder(address=OSC_ADDR)
msg.add_arg(accel[0], arg_type="i")
msg.add_arg(accel[1], arg_type="i")
msg.add_arg(accel[2], arg_type="i")
msg.add_arg(mag[0], arg_type="i")
msg.add_arg(mag[1], arg_type="i")
msg.add_arg(mag[2], arg_type="i")
msg.add_arg(heading, arg_type="f")
client.send_message(OSC_ADDR, msg.build())
while True:
accel, mag = lsm303.read()
heading = compute_heading(mag)
send_readings_to_pd(accel, mag, heading)
# Wait .075 seconds and repeat.
time.sleep(0.05)
@notexactlyawe
Copy link
Author

This code assumes you have a PD script that can act as an OSC server. Setup steps below:

  1. Follow the instructions here to configure I2C on the pi.
  2. Install the LSM303 Python library as detailed here. Follow the "build from source" instructions to get the example code.
  3. Install python-osc with pip3 install python-osc
  4. Run your PD script
  5. Run python3 lsm303_to_pd.py

Troubleshooting:

  • Ensure that the LSM303 is soldered correctly, you can test this with a multimeter's connectivity mode.
  • Ensure that I2C is working on the Pi by running sudo i2cdetect -y 1. If the LSM303 is plugged in correctly and working then the addresses 0x19 and 0x1e should show up.
  • The pins that need to be connected (LSM303->Pi) are as follows: Vin->5V, GND->GND, SDA->SDA1, SCL->SCL1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment