private static void WritePulseWidth(PwmChannel pwmChannel, ServoMotor servoMotor) { servoMotor.Start(); while (true) { Console.WriteLine("Enter a pulse width in microseconds ('Q' to quit). "); string pulseWidth = Console.ReadLine(); if (pulseWidth.ToUpper() == "Q") { break; } if (!int.TryParse(pulseWidth, out int pulseWidthValue)) { Console.WriteLine($"Can not parse {pulseWidth}. Try again."); } servoMotor.WritePulseWidth(pulseWidthValue); Console.WriteLine($"Duty Cycle: {pwmChannel.DutyCycle * 100.0}%"); } servoMotor.Stop(); }
static void WritePulseWidth(PwmChannel pwmChannel, ServoMotor servoMotor) { servoMotor.Start(); int pulseWidthValue = 1000; while (true) { //Console.WriteLine("Enter a pulse width in microseconds ('Q' to quit). "); //string? pulseWidth = Console.ReadLine(); //if (pulseWidth?.ToUpper() is "Q" || pulseWidth?.ToUpper() is null) //{ // break; //} //if (!int.TryParse(pulseWidth, out int pulseWidthValue)) //{ // Console.WriteLine($"Can not parse {pulseWidth}. Try again."); //} servoMotor.WritePulseWidth(pulseWidthValue); Console.WriteLine($"Duty Cycle: {pwmChannel.DutyCycle * 100.0}%"); pulseWidthValue = (pulseWidthValue == 1000) ? 2000 : 1000; Thread.Sleep(5000); } servoMotor.Stop(); }
public void Verify_Duty_Cycle_When_Writing_Pulse_Width( int frequency, int pulseWithInMicroseconds, double expectedDutyCycle) { Mock <PwmChannel> mockPwmChannel = new Mock <PwmChannel>(); mockPwmChannel.SetupAllProperties(); mockPwmChannel.Object.Frequency = frequency; ServoMotor servo = new ServoMotor(mockPwmChannel.Object); servo.WritePulseWidth(pulseWithInMicroseconds); Assert.Equal(expectedDutyCycle, mockPwmChannel.Object.DutyCycle, 5); }
/// <summary> /// Writes a pulse width to the servo motor. /// </summary> /// <param name="microseconds">The pulse width, in microseconds, to write to the servo motor.</param> public void WritePulseWidth(int microseconds) => _servoMotor.WritePulseWidth(microseconds);