|
| 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