Skip to content

Commit 80147cc

Browse files
authored
Merge pull request #909 from siddacious/lsm303_guide_update
code updates for lsm303 guide
2 parents b7abb0c + a9e0a14 commit 80147cc

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

LSM303/lsm303agr_combined.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from time import sleep
2+
import board
3+
import busio
4+
import adafruit_lsm303_accel
5+
import adafruit_lsm303agr_mag
6+
7+
i2c = busio.I2C(board.SCL, board.SDA)
8+
mag = adafruit_lsm303agr_mag.LSM303AGR_Mag(i2c)
9+
accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
10+
11+
while True:
12+
print("Acceleration (m/s^2): X=%0.3f Y=%0.3f Z=%0.3f"%accel.acceleration)
13+
print("Magnetometer (micro-Teslas)): X=%0.3f Y=%0.3f Z=%0.3f"%mag.magnetic)
14+
print("")
15+
sleep(0.5)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <Servo.h>
2+
Servo servo;
3+
4+
void setup()
5+
{
6+
servo.attach(9); // attaches the servo on pin 9 to the servo object
7+
servo.write(90); // change this value to achieve minimum rotation!
8+
}
9+
10+
void loop()
11+
{
12+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// **********************************************
2+
// Zax-O-Meter Sketch
3+
// for the Adafruit LSM303 Magnetometer Breakout
4+
//
5+
// Written by Bill Earl for Adafruit Industries
6+
//
7+
// **********************************************
8+
9+
#include <Wire.h>
10+
#include <Adafruit_Sensor.h>
11+
#include <Adafruit_LSM303AGR_Mag.h>
12+
13+
//Uncomment this line and comment out the above for the LSM303DLH
14+
//#include <Adafruit_LSM303DLH_Mag.h>
15+
#include <Servo.h>
16+
17+
/* Assign a unique ID to this sensor at the same time */
18+
Adafruit_LSM303AGR_Mag_Unified mag = Adafruit_LSM303AGR_Mag_Unified(12345);
19+
//Uncomment this line and comment out the above for the LSM303DLH
20+
//Adafruit_LSM303DLH_Mag_Unified mag = Adafruit_LSM303AGR_Mag_Unified(12345);
21+
22+
// This is our continuous rotation servo
23+
Servo servo;
24+
25+
// Pi for calculations - not the raspberry type
26+
const float Pi = 3.14159;
27+
28+
// This is the value that gives you minimal rotation on
29+
// a continuous rotation servo. It is usually about 90.
30+
// adjust this value to give minimal rotation for your servo
31+
const float ServoNeutral = 90;
32+
33+
// This is the desired direction of travel
34+
// expressed as a 0-360 degree compass heading
35+
// 0.0 = North
36+
// 90.0 = East
37+
// 180.0 = South
38+
// 270 = West
39+
const float targetHeading = 0.0;
40+
41+
42+
void setup(void)
43+
{
44+
Serial.begin(115200);
45+
Serial.println("Magnetometer Test"); Serial.println("");
46+
47+
/* Initialise the sensor */
48+
if(!mag.begin())
49+
{
50+
/* There was a problem detecting the LSM303 ... check your connections */
51+
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
52+
while(1);
53+
}
54+
55+
servo.attach(9); // Attach servo to pin 9
56+
57+
}
58+
59+
void loop(void)
60+
{
61+
/* Get a new sensor event */
62+
sensors_event_t event;
63+
mag.getEvent(&event);
64+
65+
// Calculate the angle of the vector y,x
66+
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
67+
// Normalize to 0-360
68+
if (heading < 0)
69+
{
70+
heading = 360 + heading;
71+
}
72+
73+
// Calculate the error between tha measured heading and the target heading.
74+
float error = heading - targetHeading;
75+
if (error > 180)
76+
{
77+
error = error - 360; // for angles > 180, correct in the opposite direction.
78+
}
79+
// A non-zero difference between the heading and the
80+
// targetHeading will bias the servoNeutral value and
81+
// cause the servo to rotate back toward the targetHeading.
82+
// The divisor is to reduce the reaction speed and avoid oscillations
83+
servo.write(ServoNeutral + error / 4 );
84+
85+
delay(40);
86+
}

0 commit comments

Comments
 (0)