Added step motor (driver) functionality

This commit is contained in:
zwarenelle
2024-06-13 21:22:11 +02:00
parent 06660a2fa1
commit b16f5e57f2
2 changed files with 91 additions and 20 deletions

View File

@@ -1,25 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Threading.Tasks;
using System.Device.Gpio;
namespace RobotController
{
internal class Motor
class Motor
{
public int STEP_PIN { get; set; }
public int DIR_PIN { get; set; }
public int ENABLE_PIN { get; set; }
private GpioPin stepPin;
private GpioPin directionPin;
private GpioPin enablePin;
private PinValue stepPinValue;
private PinValue directionPinValue;
private PinValue enablePinValue;
public Motor(int sTEP_PIN, int dIR_PIN)
{
private int directionPin;
private int stepPin;
public Motor(int directionPin, int stepPin)
{
this.directionPin = directionPin;
this.stepPin = stepPin;
}
public void driveSteps(int steps, int direction)
{
// Simulate driving the motor
}
STEP_PIN = sTEP_PIN;
DIR_PIN = dIR_PIN;
}
public Motor(int sTEP_PIN, int dIR_PIN, int eNABLE_PIN = 0)
{
STEP_PIN = sTEP_PIN;
DIR_PIN = dIR_PIN;
ENABLE_PIN = eNABLE_PIN;
}
public void InitGPIO()
{
var gpio = new GpioController();
stepPin = gpio.OpenPin(STEP_PIN);
stepPin.SetPinMode(PinMode.Output);
stepPinValue = PinValue.Low;
stepPin.Write(stepPinValue);
directionPin = gpio.OpenPin(DIR_PIN);
directionPin.SetPinMode(PinMode.Output);
directionPinValue = PinValue.Low;
directionPin.Write(directionPinValue);
if (ENABLE_PIN != 0)
{
enablePin = gpio.OpenPin(ENABLE_PIN);
enablePin.SetPinMode(PinMode.Output);
enablePinValue = PinValue.High;
enablePin.Write(enablePinValue);
}
}
private void OneStep()
{
var signal = Task.Run(async delegate { await Task.Delay(TimeSpan.FromMilliseconds(1)); });
var pavza = Task.Run(async delegate { await Task.Delay(TimeSpan.FromMilliseconds(1)); });
stepPinValue = PinValue.High;
stepPin.Write(stepPinValue);
signal.Wait();
stepPinValue = PinValue.Low;
stepPin.Write(stepPinValue);
pavza.Wait();
}
public void Step(int steps)
{
if (ENABLE_PIN != 0)
{
enablePinValue = PinValue.Low;
enablePin.Write(enablePinValue);
}
if (steps <= 0) { directionPinValue = PinValue.Low; }
else { directionPinValue = PinValue.High; }
directionPin.Write(directionPinValue);
for (int i = 0; i < Math.Abs(steps); i++)
{
OneStep();
}
if (ENABLE_PIN != 0)
{
enablePinValue = PinValue.High;
enablePin.Write(enablePinValue);
}
}
}
}

View File

@@ -1,9 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Device.Gpio" Version="3.1.0" />
</ItemGroup>
</Project>