-
Notifications
You must be signed in to change notification settings - Fork 0
/
HEADING.ino
90 lines (71 loc) · 2.31 KB
/
HEADING.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <Servo.h>
// Arduino Wire library is required if I2Cdev I2CDE
#include "Wire.h"
// I2Cdev and HMC5883L must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "HMC5883L.h"
int servoPin1 = 10;
int servoPin2 = 11;
#define MAX_FDIR 1900
#define MIN 1500
#define MAX_RDIR 1100
Servo servo;
int signal1 = MIN;
int signal2 = MIN;
HMC5883L mag;
int16_t mx, my, mz;
#define LED_PIN 13
bool blinkState = false;
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Wire.begin();
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(38400);
// initialize device
Serial.println("Initializing I2C devices...");
mag.initialize();
servo.attach(servoPin1);
servo.writeMicroseconds(signal1);
servo.attach(servoPin2);
servo.writeMicroseconds(signal2);
// verify connection
Serial.println("Testing device connections...");
Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");
// configure Arduino LED for
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// read raw heading measurements from device
mag.getHeading(&mx, &my, &mz);
// display tab-separated gyro x/y/z values
Serial.print("mag:\t");
Serial.print(mx); Serial.print("\t");
Serial.print(my); Serial.print("\t");
Serial.print(mz); Serial.print("\t");
// To calculate heading in degrees. 0 degree indicates North
float heading = atan2(my, mx);
if(heading < 0)
heading += 2 * M_PI;
if(heading > 2*M_PI)
heading -= 2*M_PI;
float headingDegrees = heading * 180/M_PI;
while (!Serial.available());
if(headingDegrees>55) //If we assume the initial angle to be 55 degrees and we intend to run the bot straight without any turns
{
signal1 -= 50;
signal2 += 50;
}else{
signal2 -= 50;
signal1 += 50;
}
servo.writeMicroseconds(signal1);
servo.writeMicroseconds(signal2);
Serial.print("heading:\t");
Serial.print(heading * 180/M_PI);
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}