public async void StepperMotorExample() { MotorHat2348 mh = null; PwmStepperMotor stepper = null; PwmPin pwm = null; if (mh == null) { // Create a driver object for the HAT at address 0x60 mh = new MotorHat2348(0x60); // Create a stepper motor object at the specified ports and steps per rev stepper = mh.CreateStepperMotor(1, 2, 200); // Create a PwmPin object at one of the auxiliary PWMs on the HAT pwm = mh.CreatePwm(1); } // step 200 full steps in the forward direction using half stepping (so 400 steps total) at 30 rpm stepper.SetSpeed(30); await stepper.StepAsync(200, Direction.Forward, SteppingStyle.Half); // Activate the pin and set it to 50% duty cycle pwm.Start(); pwm.SetActiveDutyCyclePercentage(0.5); // for demonstration purposes we will wait 10 seconds to observe the PWM and motor operation. await Task.Delay(10000); // Stop the auxiliary PWM pin pwm.Stop(); // Dispose of the MotorHat and free all its resources mh.Dispose(); }
/// <summary> /// Creates a <see cref="PwmStepperMotor"/> object for the specified channels and adds it to the list of Motors. /// </summary> /// <param name="driverChannelA">A motor driver channel from 1 to 4.</param> /// <param name="driverChannelB">A motor driver channel from 1 to 4.</param> /// <param name="steps">The number of full steps per revolution that this motor has.</param> /// <returns>The created <see cref="PwmStepperMotor"/> object.</returns> /// <remarks> /// The driverChannelA and driverChannelB parameters must be different and must be channels not already assigned to other /// <see cref="PwmDCMotor"/> or <see cref="PwmStepperMotor"/> objects for this <see cref="MotorHat2348"/>. /// </remarks> public PwmStepperMotor CreateStepperMotor(byte driverChannelA, byte driverChannelB, byte steps) { PwmStepperMotor value; if ((driverChannelA < 1) || (driverChannelA > 4)) { throw new ArgumentOutOfRangeException("driverChannelA"); } if ((driverChannelB < 1) || (driverChannelB > 4)) { throw new ArgumentOutOfRangeException("driverChannelB"); } if (driverChannelA == driverChannelB) { throw new ArgumentOutOfRangeException("driverChannelB", "Parameters driverChannelA and driverChannelB must be different."); } if (motorChannelsUsed[driverChannelA - 1] == true) { throw new MotorHatException(string.Format("Channel {0} is already assigned.", driverChannelA)); } if (motorChannelsUsed[driverChannelB - 1] == true) { throw new MotorHatException(string.Format("Channel {0} is already assigned.", driverChannelB)); } EnsureInitialized(); value = new PwmStepperMotor(this._pwm, driverChannelA, driverChannelB, steps); motorChannelsUsed[driverChannelA - 1] = true; motorChannelsUsed[driverChannelB - 1] = true; motors.Add(value); return(value); }