From 6c5fbe38b1058f1bc8f1cc96106fbb211e3c612b Mon Sep 17 00:00:00 2001 From: zwarenelle Date: Fri, 28 Jun 2024 15:05:03 +0200 Subject: [PATCH] add motor scripting --- src/Client/RobotController/Motor.cs | 128 +++++++++++--------------- src/Client/RobotController/Test.cs | 13 +++ src/Client/RobotController/stepper.py | 17 ++++ 3 files changed, 82 insertions(+), 76 deletions(-) create mode 100644 src/Client/RobotController/Test.cs create mode 100644 src/Client/RobotController/stepper.py diff --git a/src/Client/RobotController/Motor.cs b/src/Client/RobotController/Motor.cs index be3d2f7..a5f153c 100644 --- a/src/Client/RobotController/Motor.cs +++ b/src/Client/RobotController/Motor.cs @@ -1,93 +1,69 @@ using System; using System.Threading.Tasks; -using System.Device.Gpio; +using System.Diagnostics; +using System.Text; namespace RobotController { 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; + StringBuilder outputBuilder; + ProcessStartInfo processStartInfo; + Process process; + public int STEP_PIN1 { get; set; } + public int DIR_PIN1 { get; set; } - public Motor(int sTEP_PIN, int dIR_PIN) + public int STEP_PIN2 { get; set; } + public int DIR_PIN2 { get; set; } + + public Motor(sTEP_PIN1, dIR_PIN1, sTEP_PIN2, dIR_PIN2) { - STEP_PIN = sTEP_PIN; - DIR_PIN = dIR_PIN; + this.STEP_PIN1 = sTEP_PIN1; + this.DIR_PIN1 = dIR_PIN1; + + this.STEP_PIN2 = sTEP_PIN2; + this.DIR_PIN2 = dIR_PIN2; + } - public Motor(int sTEP_PIN, int dIR_PIN, int eNABLE_PIN = 0) + public Drive(bool direction, int steps) + { +outputBuilder = new StringBuilder(); + +processStartInfo = new ProcessStartInfo(); +processStartInfo.CreateNoWindow = true; +processStartInfo.RedirectStandardOutput = true; +processStartInfo.RedirectStandardInput = true; +processStartInfo.UseShellExecute = false; +processStartInfo.Arguments = "/home/wouter/roommapper/src/Client/RobotController/stepper.py drive"; +processStartInfo.FileName = "/usr/bin/python3"; + +process = new Process(); +process.StartInfo = processStartInfo; +// enable raising events because Process does not raise events by default +process.EnableRaisingEvents = true; +// attach the event handler for OutputDataReceived before starting the process +process.OutputDataReceived += new DataReceivedEventHandler +( + delegate(object sender, DataReceivedEventArgs e) { - STEP_PIN = sTEP_PIN; - DIR_PIN = dIR_PIN; - ENABLE_PIN = eNABLE_PIN; + // append the new data to the data already read-in + outputBuilder.Append(e.Data); } +); +// start the process +// then begin asynchronously reading the output +// then wait for the process to exit +// then cancel asynchronously reading the output +process.Start(); +process.BeginOutputReadLine(); +process.WaitForExit(); +process.CancelOutputRead(); - public void InitGPIO() - { - var gpio = new GpioController(); +// use the output +string output = outputBuilder.ToString(); - stepPin = gpio.OpenPin(STEP_PIN); - stepPin.SetPinMode(PinMode.Output); - stepPinValue = PinValue.Low; - stepPin.Write(stepPinValue); +Console.WriteLine(output); - 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); - } - } - } -} + } \ No newline at end of file diff --git a/src/Client/RobotController/Test.cs b/src/Client/RobotController/Test.cs new file mode 100644 index 0000000..fefec33 --- /dev/null +++ b/src/Client/RobotController/Test.cs @@ -0,0 +1,13 @@ +using System; + +namespace RobotController +{ + class Program + { + static void Main(string[] args) + { + Motor motor = new Motor(1, 2, 3, 4); + motor.drive(True, 200); + } + } +} diff --git a/src/Client/RobotController/stepper.py b/src/Client/RobotController/stepper.py new file mode 100644 index 0000000..fa198c2 --- /dev/null +++ b/src/Client/RobotController/stepper.py @@ -0,0 +1,17 @@ +import RPi.GPIO as GPIO +from RpiMotorLib import RpiMotorLib +import time +import sys + +direction= 23 # Direction (DIR) GPIO Pin +step = 22 # Step GPIO Pin + +def drive(): + mymotortest = RpiMotorLib.A4988Nema(direction, step, (21,21,21), "DRV8825") + + mymotortest.motor_go(False, "Full", 200, 0.0005, False, .05) + + GPIO.cleanup() + +if __name__ == '__main__': + globals()[sys.argv[1]]() \ No newline at end of file