-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTFGenerator.ino
108 lines (86 loc) · 3.25 KB
/
TFGenerator.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//--------------------------------------------------------------------------------------------------------------------
/*
* CLASS HEADER FILES
*/
//--------------------------------------------------------------------------------------------------------------------
#include <MPU6050.h>
#include <SPI.h>
#include <SD.h>
//--------------------------------------------------------------------------------------------------------------------
/*
* CLASS OBJECT INSTANTIATIONS
*/
//--------------------------------------------------------------------------------------------------------------------
MPU6050 mpu;
//---------------------------------------------------------------------------------------------------------------
/*
* VARIABLE/CONSTANT DEFINITIONS
*/
//---------------------------------------------------------------------------------------------------------------
double z,zz;
unsigned long timer = 0;
float timeStep = 0.01, gyaw = 0,pi = 3.143;
const int chipSelect = 4;
//--------------------------------------------------------------------------------------------------------------
/*
* COMPONENT INITIALISATION LOOP
*/
//--------------------------------------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
if(SD.exists("TFData.txt"))
{
SD.remove("TFData.txt");
}
mpu.calibrateGyro();
mpu.setThreshold(3);
pinMode(10, OUTPUT);
pinMode(6, OUTPUT);
delay(2000);
}
//------------------------------------------------------------------------------------------------------------------
/*
* MAIN CONTROL LOOP
*/
//------------------------------------------------------------------------------------------------------------------
void loop()
{
timer = millis();
Vector norm = mpu.readNormalizeGyro(); // read in gyro data as a 3x1 vector
z = norm.ZAxis; // read in angular velociy
delay((timeStep*1000) - (millis() - timer));
for(int x = 0;x < 10; x++)
{
zz += z; // run it through a 10 point averaging filter
}
zz = zz/10;
analogWrite(10,100);
analogWrite(6,100); // Activate the motors
String dataString = String(zz);
File dataFile = SD.open("TFData.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString); // print to the serial port too:
}
// if the file isn't open, pop up an error:
else
{
Serial.println("error opening datalog.txt");
}
}