-
Notifications
You must be signed in to change notification settings - Fork 0
/
openDoors.c
161 lines (148 loc) · 5.65 KB
/
openDoors.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "definitions.h"
extern double DoorInclination;
extern double OldDoorInclination;
extern double OpenedDoorInclination;
extern double ClosedDoorInclination;
extern int maxDoorCount;
extern double accelerometer_z, accelerometer_y, accelerometer_x; // variables for accelerometer raw data
extern double gyro_x, gyro_y, gyro_z; // variables for gyro raw data
extern double temperature; // variables for temperature data
returnStruct openDoors(int Setup) {
//
// Return Success or Failure
//
// 1nt i; // used to get integer from accelerometer measure in MPU6050
int dDoorCount;
returnStruct Result;
// Check the accelerometers on all axes and the temperature.
// Return error if the tilt sensor fails.
// Result = I2C_ReadMPU6050(ACCEL_X);
Result = I2C_In(MPU6050SlaveAddress, ACCEL_X,2);
if (Result.level0) {
Result.level1 = Result.level0; // Put the error from I2C in level1
Result.level0 = -1;
return Result;
}
accelerometer_x = (double) Result.level1 / 16384.0;
// Result = I2C_ReadMPU6050(ACCEL_Y);
Result = I2C_In(MPU6050SlaveAddress, ACCEL_Y,2);
if (Result.level0) {
Result.level1 = Result.level0; // Put the error from I2C in level1
Result.level0 = -2;
return Result;
}
accelerometer_y = (double) Result.level1 / 16384.0;
// Result = I2C_ReadMPU6050(ACCEL_Z);
Result = I2C_In(MPU6050SlaveAddress, ACCEL_Z,2);
if (Result.level0) {
Result.level1 = Result.level0; // Put the error from I2C in level1
Result.level0 = -3;
return Result;
}
accelerometer_z = (double) Result.level1 / 16384.0;
// Result = I2C_ReadMPU6050(TEMPERATURE);
Result = I2C_In(MPU6050SlaveAddress, TEMPERATURE,2);
if (Result.level0) {
Result.level1 = Result.level0; // Put the error from I2C in level1
Result.level0 = -4;
return Result;
}
temperature = (double) Result.level1 / 340.00 + 36.53;
// Use the temperature to determine how much the door moves so as to determine
// how many times the software tries to shut the doors completely.
if (temperature > 20.0)
maxDoorCount = 6;
else
maxDoorCount = 12;
dDoorCount = 0;
// Result = I2C_ReadMPU6050(ACCEL_Z);
Result = I2C_In(MPU6050SlaveAddress, ACCEL_Z,2);
if (Result.level0) {
Result.level1 = Result.level0; // Put the error from I2C in level1
Result.level0 = -5;
return Result;
}
DoorInclination = (double) Result.level1 / 16384.0;
OldDoorInclination = -1.0;
while ( (fabs(DoorInclination) < 0.800) && (dDoorCount < maxDoorCount) ) {
DoorMoved = 0;
#ifdef USE_TX_ESP8266
TXout(motorstat);
TXout((char *) "Opening");
#else
I2C_Out(ESP6288ModuleAddress,motorstat);
I2C_Out(ESP6288ModuleAddress,(char *) "Opening");
#endif
LATAbits.LATA2 = 1; // The doors are opening (voltage gets lower)
T1CONbits.ON = 1; // Turn on the timer
while (!DoorMoved) {}; // Timer1 will turn off both doors when time is ended
T1CONbits.ON = 0; // Turn off the timer
#ifdef USE_TX_ESP8266
TXout(motorstat);
TXout((char *) "Stopped");
#else
I2C_Out(ESP6288ModuleAddress,motorstat);
I2C_Out(ESP6288ModuleAddress,(char *) "Stopped");
#endif
OldDoorInclination = DoorInclination;
do {
// Result = I2C_ReadMPU6050(GYRO_Z);
__delay_ms(100);
Result = I2C_In(MPU6050SlaveAddress, GYRO_Z, 2);
if (Result.level0) {
Result.level1 = Result.level0; // Put the error from I2C in level1
Result.level0 = -6;
return Result;
}
gyro_z = (double) Result.level1 / 131.0;
} while (gyro_z > 1.75000);
DoorInclination = 0.0;
// Result = I2C_ReadMPU6050(ACCEL_Z);
Result = I2C_In(MPU6050SlaveAddress, ACCEL_Z,2);
if (Result.level0) {
Result.level1 = Result.level0; // Put the error from I2C in level1
Result.level0 = -7;
return Result;
}
DoorInclination = (double) Result.level1 / 16384.0;
if ((fabs(fabs(OldDoorInclination) - fabs(DoorInclination)) > fabs(DoorInclination * 0.0302)) && OldDoorInclination > DoorInclination) {
if (DoorInclination > 0.700)
dDoorCount++;
}
if (Setup) { // Calculating Opened Door G Force
buffer = formatString(DoorInclination, 3, (char *) "g ");
#ifdef USE_TX_ESP8266 // Position cursor for status message
TXout(lowDoorSw);
#else
I2C_Out(ESP6288ModuleAddress,lowDoorSw);
#endif
// buffer = ftoa((float) (DoorInclination), &status);
// decimalPoint = strchr(buffer, decPt);
// decimalPoint++;
// decimalPoint++;
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 'g';
// *decimalPoint++ = ' ';
// *decimalPoint = '\0';
#ifdef USE_TX_ESP8266
TXout(buffer);
// __delay_ms(100);
#else
I2C_Out(ESP6288ModuleAddress,buffer);
#endif
} else { // Door Percent Open
TXout(doorOFC); // Position cursor for status message
// buffer = formatString(DoorInclination, 3, (char *) "g ");
TXout(buffer);
}
__delay_ms(100);
}
Result.level0 = 0;
Result.level1 = 0;
return Result;
}