-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprediction.c
executable file
·187 lines (140 loc) · 5.36 KB
/
prediction.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "types.h"
#include "misc.h"
#include "prediction.h"
#define DEG2RAD (3.142 / 180)
#define SLOTSIZE 100
#define SLOTS 45000 / SLOTSIZE
#define POLL_PERIOD 5
#define INITIAL_CDA 0.7
#define PAYLOAD_WEIGHT 1.0
#define LANDING_ALTITUDE 100
struct TPosition
{
float LatitudeDelta;
float LongitudeDelta;
};
struct TPosition Positions[SLOTS]; // 100m slots from 0 to 45km
float PreviousLatitude, PreviousLongitude, cd_area;
unsigned long PreviousAltitude;
int GetSlot(int32_t Altitude)
{
int Slot;
Slot = Altitude / SLOTSIZE;
if (Slot < 0) Slot = 0;
if (Slot >= SLOTS) Slot = SLOTS-1;
return Slot;
}
float CalculateAirDensity(float Altitude)
{
float Temperature, Pressure;
if (Altitude < 11000.0)
{
// below 11Km - Troposphere
Temperature = 15.04 - (0.00649 * Altitude);
Pressure = 101.29 * pow((Temperature + 273.1) / 288.08, 5.256);
}
else if (Altitude < 25000.0)
{
// between 11Km and 25Km - lower Stratosphere
Temperature = -56.46;
Pressure = 22.65 * exp(1.73 - ( 0.000157 * Altitude));
}
else
{
// above 25Km - upper Stratosphere
Temperature = -131.21 + (0.00299 * Altitude);
Pressure = 2.488 * pow((Temperature + 273.1) / 216.6, -11.388);
}
return Pressure / (0.2869 * (Temperature + 273.1));
}
float CalculateDescentRate(float Weight, float CDTimesArea, float Altitude)
{
float Density;
Density = CalculateAirDensity(Altitude);
return sqrt((Weight * 9.81)/(0.5 * Density * CDTimesArea));
}
float CalculateCDA(float Weight, float Altitude, float DescentRate)
{
float Density;
Density = CalculateAirDensity(Altitude);
printf("Alt %f, Rate %f, CDA %f\n", Altitude, DescentRate, (Weight * 9.81)/(0.5 * Density * DescentRate * DescentRate));
return (Weight * 9.81)/(0.5 * Density * DescentRate * DescentRate);
}
int CalculateLandingPosition(float CDA, float Latitude, float Longitude, int32_t Altitude, float *PredictedLatitude, float *PredictedLongitude)
{
float TimeTillLanding, TimeInSlot, DescentRate;
int Slot;
int32_t DistanceInSlot;
TimeTillLanding = 0;
Slot = GetSlot(Altitude);
DistanceInSlot = Altitude + 1 - (Slot * SLOTSIZE);
while (Altitude > LANDING_ALTITUDE)
{
Slot = GetSlot(Altitude);
if (Slot == GetSlot(LANDING_ALTITUDE))
{
DistanceInSlot = Altitude - LANDING_ALTITUDE;
}
DescentRate = CalculateDescentRate(PAYLOAD_WEIGHT, CDA, Altitude);
TimeInSlot = DistanceInSlot / DescentRate;
Latitude += Positions[Slot].LatitudeDelta * TimeInSlot;
Longitude += Positions[Slot].LongitudeDelta * TimeInSlot;
// printf("SLOT %d: alt %lu, lat=%f, long=%f, rate=%f, dist=%lu, time=%f\n", Slot, Altitude, Latitude, Longitude, DescentRate, DistanceInSlot, TimeInSlot);
TimeTillLanding = TimeTillLanding + TimeInSlot;
Altitude -= DistanceInSlot;
DistanceInSlot = SLOTSIZE;
}
*PredictedLatitude = Latitude;
*PredictedLongitude = Longitude;
return TimeTillLanding;
}
void setup_prediction(struct TGPS *GPS)
{
PreviousLatitude = 0;
PreviousLongitude = 0;
PreviousAltitude = 0;
GPS->CDA = INITIAL_CDA;
}
void check_prediction(struct TGPS *GPS)
{
static uint64_t NextAction = 0;
if ((get_time() > NextAction) && (GPS->Satellites >= 4) && (GPS->Latitude >= -90) && (GPS->Latitude <= 90) && (GPS->Longitude >= -180) && (GPS->Longitude <= 180))
{
int Slot;
NextAction = get_time() + POLL_PERIOD * 1000000L;
printf("FLIGHT MODE = %d ***\n", GPS->FlightMode);
if ((GPS->FlightMode >= fmLaunched) && (GPS->FlightMode < fmLanded))
{
// Ascent or descent?
if (GPS->FlightMode == fmLaunched)
{
// Going up - store deltas
Slot = GetSlot(GPS->Altitude/2 + PreviousAltitude/2);
// Deltas are scaled to be horizontal distance per second (i.e. speed)
Positions[Slot].LatitudeDelta = (GPS->Latitude - PreviousLatitude) / POLL_PERIOD;
Positions[Slot].LongitudeDelta = (GPS->Longitude - PreviousLongitude) / POLL_PERIOD;
printf("Slot %d (%ld): %f, %f\n", Slot, GPS->Altitude, Positions[Slot].LatitudeDelta, Positions[Slot].LongitudeDelta);
}
else if ((GPS->FlightMode >= fmDescending) && (GPS->FlightMode <= fmLanding))
{
// Coming down - try and calculate how well chute is doing
GPS->CDA = (GPS->CDA*4 + CalculateCDA(PAYLOAD_WEIGHT, GPS->Altitude/2 + PreviousAltitude/2, (PreviousAltitude - GPS->Altitude) / POLL_PERIOD)) / 5;
}
// Estimate landing position
GPS->TimeTillLanding = CalculateLandingPosition(GPS->CDA, GPS->Latitude, GPS->Longitude, GPS->Altitude, &(GPS->PredictedLatitude), &(GPS->PredictedLongitude));
GPS->PredictedLandingSpeed = CalculateDescentRate(PAYLOAD_WEIGHT, GPS->CDA, LANDING_ALTITUDE);
printf("Expected Descent Rate = %4.1f (now) %3.1f (landing), time till landing %d\n",
CalculateDescentRate(PAYLOAD_WEIGHT, GPS->CDA, GPS->Altitude),
GPS->PredictedLandingSpeed, GPS->TimeTillLanding);
printf("Current %f, %f, alt %ld\n", GPS->Latitude, GPS->Longitude, GPS->Altitude);
printf("Prediction %f, %f, CDA %f\n", GPS->PredictedLatitude, GPS->PredictedLongitude, GPS->CDA);
}
PreviousLatitude = GPS->Latitude;
PreviousLongitude = GPS->Longitude;
PreviousAltitude = GPS->Altitude;
}
}