I'm simulating the flight of a drone using observables for the altitude. The altitude should vary according to this scheme:
- Altitude increases from 0 to
BaseAltitude, that is a fixed altitude. - After the
BaseAltitudeis reached, the drone starts cruising, describing a sine wave, starting atBaseAltitude - Upon a signal, the drone should start landing. This is, starting from the current altitude, the drone should go down linearly until it reaches 0.
As you might notice, when the landing starts, the altitude is unknown at design time. The takeoff sequence should take the last altitude as the start. So, one sequence depends on the last value produced by another sequence. My brain aches!
Well, I'm completely stuck with this.
The only code I have currently is below. I put it to illustrate the problem. You will get it quickly...
public class Drone
{
public Drone()
{
var interval = TimeSpan.FromMilliseconds(200);
var takeOff = Observable.Interval(interval).TakeWhile(h => h < BaseAltitude).Select(t => (double)t);
var cruise = Observable
.Interval(interval).Select(t => 100 * Math.Sin(t * 2 * Math.PI / 180) + BaseAltitude)
.TakeUntil(_ => IsLanding);
var landing = Observable
.Interval(interval).Select(t => ??? );
Altitude = takeOff.Concat(cruise).Concat(landing);
}
public bool IsLanding { get; set; }
public double BaseAltitude { get; set; } = 100;
public IObservable<double> Altitude { get; }
}