diff --git a/src/Client/RobotController/Motor.cs b/src/Client/RobotController/Motor.cs index 2a2bc0c..be3d2f7 100644 --- a/src/Client/RobotController/Motor.cs +++ b/src/Client/RobotController/Motor.cs @@ -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); + } + } + } } diff --git a/src/Client/RobotController/RobotController.csproj b/src/Client/RobotController/RobotController.csproj index fa71b7a..59d6b93 100644 --- a/src/Client/RobotController/RobotController.csproj +++ b/src/Client/RobotController/RobotController.csproj @@ -1,9 +1,12 @@ - + net8.0 enable - enable + + + +