Last active
April 20, 2018 09:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code assumes you have a PD script that can act as an OSC server. Setup steps below:
pip3 install python-osc
python3 lsm303_to_pd.py
Troubleshooting:
sudo i2cdetect -y 1
. If the LSM303 is plugged in correctly and working then the addresses0x19
and0x1e
should show up.