public GetDistanceInCmAsync ( int timeoutInMilliseconds ) : Task |
||
timeoutInMilliseconds | int | |
Résultat | Task |
private async void DoWork(object sender, DoWorkEventArgs e) { var driver = new TwoMotorsDriver(new Motor(27, 22), new Motor(5, 6)); var ultrasonicDistanceSensor = new UltrasonicDistanceSensor(23, 24); await WriteLog("Moving forward"); while (!_finish) { driver.MoveForward(); await Task.Delay(200); var distance = await ultrasonicDistanceSensor.GetDistanceInCmAsync(1000); if (distance > 35.0) { continue; } await WriteLog($"Obstacle found at {distance} cm or less. Turning right"); await driver.TurnRightAsync(); await WriteLog("Moving forward"); } }
private async void DoWork(object sender, DoWorkEventArgs e) { var driver = new TwoMotorsDriver(new Motor(27, 22), new Motor(5, 6)); var ultrasonicDistanceSensor = new UltrasonicDistanceSensor(23, 24); await WriteLog("Moving forward"); while (!_finish) { driver.MoveForward(); await Task.Delay(200); var distance = await ultrasonicDistanceSensor.GetDistanceInCmAsync(1000); if (distance > 35.0) continue; await WriteLog($"Obstacle found at {distance} cm or less. Turning right"); await driver.TurnRightAsync(); await WriteLog("Moving forward"); } }
private async void DoWork(object sender, DoWorkEventArgs e) { var driver = new TwoMotorsDriver(new Motor(27, 22), new Motor(5, 6)); var ultrasonicDistanceSensor = new UltrasonicDistanceSensor(23, 24); await ultrasonicDistanceSensor.InitAsync(); WriteLog("Moving forward"); while (!_finish) { try { driver.MoveForward(); await Task.Delay(200); var distance = await ultrasonicDistanceSensor.GetDistanceInCmAsync(1000); WriteData("Forward", distance); if (distance > 35.0) continue; WriteLog($"Obstacle found at {distance:F2} cm or less. Turning right"); WriteData("Turn Right", distance); await driver.TurnRightAsync(); WriteLog("Moving forward"); } catch (Exception ex) { WriteLog(ex.Message); driver.Stop(); WriteData("Stop", -1); } } }