code updates for lsm303 guide

This commit is contained in:
siddacious 2019-10-29 18:50:22 -07:00
parent b7abb0cc72
commit a9e0a1482b
3 changed files with 113 additions and 0 deletions

View file

@ -0,0 +1,15 @@
from time import sleep
import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303agr_mag
i2c = busio.I2C(board.SCL, board.SDA)
mag = adafruit_lsm303agr_mag.LSM303AGR_Mag(i2c)
accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
while True:
print("Acceleration (m/s^2): X=%0.3f Y=%0.3f Z=%0.3f"%accel.acceleration)
print("Magnetometer (micro-Teslas)): X=%0.3f Y=%0.3f Z=%0.3f"%mag.magnetic)
print("")
sleep(0.5)

View file

@ -0,0 +1,12 @@
#include <Servo.h>
Servo servo;
void setup()
{
servo.attach(9); // attaches the servo on pin 9 to the servo object
servo.write(90); // change this value to achieve minimum rotation!
}
void loop()
{
}

View file

@ -0,0 +1,86 @@
// **********************************************
// Zax-O-Meter Sketch
// for the Adafruit LSM303 Magnetometer Breakout
//
// Written by Bill Earl for Adafruit Industries
//
// **********************************************
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303AGR_Mag.h>
//Uncomment this line and comment out the above for the LSM303DLH
//#include <Adafruit_LSM303DLH_Mag.h>
#include <Servo.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303AGR_Mag_Unified mag = Adafruit_LSM303AGR_Mag_Unified(12345);
//Uncomment this line and comment out the above for the LSM303DLH
//Adafruit_LSM303DLH_Mag_Unified mag = Adafruit_LSM303AGR_Mag_Unified(12345);
// This is our continuous rotation servo
Servo servo;
// Pi for calculations - not the raspberry type
const float Pi = 3.14159;
// This is the value that gives you minimal rotation on
// a continuous rotation servo. It is usually about 90.
// adjust this value to give minimal rotation for your servo
const float ServoNeutral = 90;
// This is the desired direction of travel
// expressed as a 0-360 degree compass heading
// 0.0 = North
// 90.0 = East
// 180.0 = South
// 270 = West
const float targetHeading = 0.0;
void setup(void)
{
Serial.begin(115200);
Serial.println("Magnetometer Test"); Serial.println("");
/* Initialise the sensor */
if(!mag.begin())
{
/* There was a problem detecting the LSM303 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
}
servo.attach(9); // Attach servo to pin 9
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
// Calculate the angle of the vector y,x
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
// Calculate the error between tha measured heading and the target heading.
float error = heading - targetHeading;
if (error > 180)
{
error = error - 360; // for angles > 180, correct in the opposite direction.
}
// A non-zero difference between the heading and the
// targetHeading will bias the servoNeutral value and
// cause the servo to rotate back toward the targetHeading.
// The divisor is to reduce the reaction speed and avoid oscillations
servo.write(ServoNeutral + error / 4 );
delay(40);
}