From a8ca87ecfa338dd0b0e30ac9ce886f93b7c78e4b Mon Sep 17 00:00:00 2001 From: Grazfather Date: Tue, 22 Apr 2025 15:25:46 -0400 Subject: [PATCH 1/2] esp: Add dumb stepper motor example --- examples/espressif/esp/build.zig | 1 + .../espressif/esp/src/stepper_driver_dumb.zig | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 examples/espressif/esp/src/stepper_driver_dumb.zig diff --git a/examples/espressif/esp/build.zig b/examples/espressif/esp/build.zig index 72c4ff521..b244f95c8 100644 --- a/examples/espressif/esp/build.zig +++ b/examples/espressif/esp/build.zig @@ -22,6 +22,7 @@ pub fn build(b: *std.Build) void { .{ .name = "custom_clock_config", .file = "src/custom_clock_config.zig" }, .{ .name = "interrupts", .file = "src/interrupts.zig" }, .{ .name = "stepper_driver", .file = "src/stepper_driver.zig" }, + .{ .name = "stepper_driver_dumb", .file = "src/stepper_driver_dumb.zig" }, }; for (available_examples) |example| { diff --git a/examples/espressif/esp/src/stepper_driver_dumb.zig b/examples/espressif/esp/src/stepper_driver_dumb.zig new file mode 100644 index 000000000..7d2e39138 --- /dev/null +++ b/examples/espressif/esp/src/stepper_driver_dumb.zig @@ -0,0 +1,56 @@ +const std = @import("std"); +const microzig = @import("microzig"); +const hal = microzig.hal; +const gpio = hal.gpio; +const time = hal.time; + +const GPIO_Device = hal.drivers.GPIO_Device; +const ClockDevice = hal.drivers.ClockDevice; +const ULN2003 = microzig.drivers.stepper.ULN2003; + +const usb_serial_jtag = hal.usb_serial_jtag; + +pub const microzig_options = microzig.Options{ + .logFn = usb_serial_jtag.logger.logFn, +}; + +pub fn main() !void { + var cd = ClockDevice{}; + + // Setup all pins for the stepper driver + var pins: struct { + in1: GPIO_Device, + in2: GPIO_Device, + in3: GPIO_Device, + in4: GPIO_Device, + } = undefined; + inline for (std.meta.fields(@TypeOf(pins)), .{ 5, 6, 7, 8 }) |field, num| { + const pin = gpio.num(num); + // Give the pin a sane default config + pin.apply(.{}); + @field(pins, field.name) = GPIO_Device.init(pin); + } + + var stepper = ULN2003.init(.{ + .in1_pin = pins.in1.digital_io(), + .in2_pin = pins.in2.digital_io(), + .in3_pin = pins.in3.digital_io(), + .in4_pin = pins.in4.digital_io(), + .clock_device = cd.clock_device(), + }); + + try stepper.begin(20, 1); + + while (true) { + // Try different microsteps + inline for (.{ 1, 2 }) |ms| { + _ = try stepper.set_microstep(ms); + std.log.info("microsteps: {}", .{ms}); + try stepper.rotate(360); + time.sleep_ms(250); + try stepper.rotate(-360); + time.sleep_ms(250); + } + time.sleep_ms(1000); + } +} From dec4adbcc149323940ac0e1a87d108054434da11 Mon Sep 17 00:00:00 2001 From: Grazfather Date: Tue, 22 Apr 2025 15:34:34 -0400 Subject: [PATCH 2/2] Fix step table for ULN2003 stepper motor driver --- drivers/stepper/ULN2003.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/stepper/ULN2003.zig b/drivers/stepper/ULN2003.zig index 4e594dd9a..84eca1282 100644 --- a/drivers/stepper/ULN2003.zig +++ b/drivers/stepper/ULN2003.zig @@ -25,8 +25,8 @@ const Direction = enum { forward, backward }; pub const ULN2003 = struct { const Self = @This(); const STEP_MIN_US = 2; - const STEP_TABLE_FULL = [_]u4{ 0b1010, 0b0110, 0b0101, 0b1001 }; - const STEP_TABLE_HALF = [_]u4{ 0b1000, 0b1010, 0b0010, 0b0110, 0b0100, 0b0101, 0b0001, 0b1001 }; + const STEP_TABLE_FULL = [_]u4{ 0b1000, 0b0100, 0b0010, 0b0001 }; + const STEP_TABLE_HALF = [_]u4{ 0b1000, 0b1100, 0b0100, 0b0110, 0b0010, 0b0011, 0b0001, 0b1001 }; in: [4]mdf.base.Digital_IO, clock: mdf.base.Clock_Device,