-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainViewModel.cs
230 lines (198 loc) · 7.57 KB
/
MainViewModel.cs
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using DroneLander.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace DroneLander
{
public class MainViewModel : Common.ObservableBase
{
public MainViewModel(MainPage activityPage)
{
this.ActivityPage = activityPage;
this.ActiveLandingParameters = new LandingParameters();
this.Altitude = this.ActiveLandingParameters.Altitude;
this.Velocity = this.ActiveLandingParameters.Velocity;
this.Fuel = this.ActiveLandingParameters.Fuel;
this.Thrust = this.ActiveLandingParameters.Thrust;
this.FuelRemaining = CoreConstants.StartingFuel;
this.IsActive = false;
}
private MainPage _activityPage;
public MainPage ActivityPage
{
get { return this._activityPage; }
set { this.SetProperty(ref this._activityPage, value); }
}
public LandingParameters ActiveLandingParameters { get; set; }
private double _altitude;
public double Altitude
{
get { return this._altitude; }
set { this.SetProperty(ref this._altitude, value); }
}
private double _descentRate;
public double DescentRate
{
get { return this._descentRate; }
set { this.SetProperty(ref this._descentRate, value); }
}
private double _velocity;
public double Velocity
{
get { return this._velocity; }
set { this.SetProperty(ref this._velocity, value); }
}
private double _fuel;
public double Fuel
{
get { return this._fuel; }
set { this.SetProperty(ref this._fuel, value); }
}
private double _fuelRemaining;
public double FuelRemaining
{
get { return this._fuelRemaining; }
set { this.SetProperty(ref this._fuelRemaining, value); }
}
private double _thrust;
public double Thrust
{
get { return this._thrust; }
set { this.SetProperty(ref this._thrust, value); }
}
private double _throttle;
public double Throttle
{
get { return this._throttle; }
set { this.SetProperty(ref this._throttle, value); }
}
private bool _isActionable() => true;
private string _actionLabel;
public string ActionLabel
{
get { return this._actionLabel; }
set { this.SetProperty(ref this._actionLabel, value); }
}
private bool _isActive;
public bool IsActive
{
get { return this._isActive; }
set { this.SetProperty(ref this._isActive, value); this.ActionLabel = (this.IsActive) ? "Reset" : "Start"; }
}
public ICommand AttemptLandingCommand
{
get
{
return new RelayCommand(() =>
{
this.IsActive = !this.IsActive;
if (this.IsActive)
{
StartLanding();
}
else
{
ResetLanding();
}
}, _isActionable);
}
}
public void StartLanding()
{
Device.StartTimer(TimeSpan.FromMilliseconds(Common.CoreConstants.PollingIncrement), () =>
{
UpdateFlightParameters();
if (this.ActiveLandingParameters.Altitude > 0.0)
{
Device.BeginInvokeOnMainThread(() =>
{
this.Altitude = this.ActiveLandingParameters.Altitude;
this.DescentRate = this.ActiveLandingParameters.Velocity;
this.FuelRemaining = this.ActiveLandingParameters.Fuel / 1000;
this.Thrust = this.ActiveLandingParameters.Thrust;
});
return this.IsActive;
}
else
{
this.ActiveLandingParameters.Altitude = 0.0;
this.IsActive = false;
Device.BeginInvokeOnMainThread(() =>
{
this.Altitude = this.ActiveLandingParameters.Altitude;
this.DescentRate = this.ActiveLandingParameters.Velocity;
this.FuelRemaining = this.ActiveLandingParameters.Fuel / 1000;
this.Thrust = this.ActiveLandingParameters.Thrust;
});
if (this.ActiveLandingParameters.Velocity > -5.0)
{
MessagingCenter.Send(this.ActivityPage, "ActivityUpdate", LandingResultType.Landed);
}
else
{
MessagingCenter.Send(this.ActivityPage, "ActivityUpdate", LandingResultType.Kaboom);
}
return false;
}
});
}
private void UpdateFlightParameters()
{
double seconds = Common.CoreConstants.PollingIncrement / 1000.0;
// Compute thrust and remaining fuel
//thrust = throttle * 1200.0;
var used = (this.Throttle * seconds) / 10.0;
used = Math.Min(used, this.ActiveLandingParameters.Fuel); // Can't burn more fuel than you have
this.ActiveLandingParameters.Thrust = used * 25000.0;
this.ActiveLandingParameters.Fuel -= used;
// Compute new flight parameters
double avgmass = Common.CoreConstants.LanderMass + (used / 2.0);
double force = this.ActiveLandingParameters.Thrust - (avgmass * Common.CoreConstants.Gravity);
double acc = force / avgmass;
double vel2 = this.ActiveLandingParameters.Velocity + (acc * seconds);
double avgvel = (this.ActiveLandingParameters.Velocity + vel2) / 2.0;
this.ActiveLandingParameters.Altitude += (avgvel * seconds);
this.ActiveLandingParameters.Velocity = vel2;
}
public async void ShakeLandscapeAsync(ContentPage page)
{
try
{
for (int i = 0; i < 8; i++)
{
await Task.WhenAll(
page.ScaleTo(1.1, 20, Easing.Linear),
page.TranslateTo(-30, 0, 20, Easing.Linear)
);
await Task.WhenAll(
page.TranslateTo(0, 0, 20, Easing.Linear)
);
await Task.WhenAll(
page.TranslateTo(0, -30, 20, Easing.Linear)
);
await Task.WhenAll(
page.ScaleTo(1.0, 20, Easing.Linear),
page.TranslateTo(0, 0, 20, Easing.Linear)
);
}
}
catch { }
}
public async void ResetLanding()
{
await Task.Delay(500);
this.ActiveLandingParameters = new LandingParameters();
this.Altitude = 5000.0;
this.Velocity = 0.0;
this.Fuel = 1000.0;
this.FuelRemaining = 1000.0;
this.Thrust = 0.0;
this.DescentRate = 0.0;
this.Throttle = 0.0;
}
}
}