Dear Team,
I have the Meadow F7 V2 Micro OS 1.5.0 board and I am using the Meadow.Foundation.Motors.Stepper.GpioStepper library to run multiple stepper motors (6 nos.) (Pul, Dir) and using multiple synchronized tasks to run each Stepper.GoTo function simultaneously but the execution behavior is very strange, it never allow more than one stepper to run at same time.
My code is below:
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.Motors;
using Meadow.Hardware;
using Meadow.Peripherals;
using Meadow.Peripherals.Motors;
using System.Collections.Generic;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http;
using Meadow.Foundation.Motors.Stepper;
using Meadow.Units;
namespace MeadowApp
{
public class MeadowApp : App<F7FeatherV2>
{
//<!=SNIP=>
private StepDirStepper stepper_1;
private StepDirStepper stepper_2;
private StepDirStepper stepper_3;
private StepDirStepper stepper_4;
private StepDirStepper stepper_5;
private StepDirStepper stepper_6;
private RotationDirection direction;
public override Task Initialize()
{
// st.MinimumStartupDwellMicroseconds = 100;
StepDirStepper st_1 = new StepDirStepper(
Device.Pins.D15.CreateDigitalOutputPort(true),
Device.Pins.D14.CreateDigitalOutputPort(true),
stepsPerRevolution: 400);
st_1.LinearAccelerationConstant = 200;
stepper_1 = st_1;
StepDirStepper st_2 = new StepDirStepper(
Device.Pins.D13.CreateDigitalOutputPort(true),
Device.Pins.D12.CreateDigitalOutputPort(true),
stepsPerRevolution: 400);
st_2.LinearAccelerationConstant = 200;
stepper_2 = st_2;
StepDirStepper st_3 = new StepDirStepper(
Device.Pins.D11.CreateDigitalOutputPort(true),
Device.Pins.D10.CreateDigitalOutputPort(true),
stepsPerRevolution: 400);
st_3.LinearAccelerationConstant = 200;
stepper_3 = st_3;
StepDirStepper st_4 = new StepDirStepper(
Device.Pins.D09.CreateDigitalOutputPort(true),
Device.Pins.D08.CreateDigitalOutputPort(true),
stepsPerRevolution: 400);
st_4.LinearAccelerationConstant = 200;
stepper_4 = st_4;
StepDirStepper st_5 = new StepDirStepper(
Device.Pins.D07.CreateDigitalOutputPort(true),
Device.Pins.D06.CreateDigitalOutputPort(true),
stepsPerRevolution: 400);
st_5.LinearAccelerationConstant = 200;
stepper_5 = st_5;
StepDirStepper st_6 = new StepDirStepper(
Device.Pins.D05.CreateDigitalOutputPort(true),
Device.Pins.D04.CreateDigitalOutputPort(true),
stepsPerRevolution: 400);
st_6.LinearAccelerationConstant = 200;
stepper_6 = st_6;
return base.Initialize();
}
public override async Task Run()
{
direction = RotationDirection.Clockwise;
//while (true)
//{
Resolver.Log.Info($"{direction}");
await ExecuteMultipleTasksAsync();
Thread.Sleep(1000);
direction = direction switch
{
RotationDirection.CounterClockwise => RotationDirection.Clockwise,
_ => RotationDirection.CounterClockwise
};
//}
}
//<!=SNOP=>
public async Task ExecuteMultipleTasksAsync()
{
var tasks = new List<Task>
{
StepperAsync(stepper_1),
StepperAsync(stepper_2),
StepperAsync(stepper_3),
StepperAsync(stepper_4),
StepperAsync(stepper_5),
StepperAsync(stepper_6),
};
try
{
await Task.WhenAll(tasks);
}
catch (AggregateException ex)
{
foreach (var innerEx in ex.InnerExceptions)
{
// Handle each inner exception
}
}
}
public async Task<bool> StepperAsync(StepDirStepper St)
{
try
{
await St.GoTo(new Angle(5, Angle.UnitType.Revolutions), new AngularVelocity(5, AngularVelocity.UnitType.RevolutionsPerSecond));
return true;
}
catch (HttpRequestException ex)
{
// Handle the exception
return false;
}
}
}
}