hybridgroup / gobot

Golang framework for robotics, drones, and the Internet of Things (IoT)
https://gobot.io
Other
8.97k stars 1.04k forks source link

Stepper over MCP23017 #1078

Open Juan1ll0 opened 8 months ago

Juan1ll0 commented 8 months ago

Hi friends, I'm working with a Nema17 connected to an L298N and this to a MC23017. I wrote a basic adaptor for MCP23017 to pass as DigitalWriter to Stepper driver. The code is like this:

package mcpDigitalWriter

import (
    "fmt"
    "slices"
    "strconv"

    "gobot.io/x/gobot/v2/drivers/i2c"
)

type MCPDigitalWriter struct {
    name      string
    MCPDevice *i2c.MCP23017Driver
}

func NewMCPDigitalWriter(mcp *i2c.MCP23017Driver) *MCPDigitalWriter {
    return &MCPDigitalWriter{
        name:      "default",
        MCPDevice: mcp,
    }
}

func (mcp *MCPDigitalWriter) DigitalWrite(pin string, level byte) error {
    if slices.Contains([]string{"0", "1", "2", "3", "4", "5", "6", "7"}, pin) {
        intPin, _ := strconv.Atoi(pin)
        mcp.MCPDevice.WriteGPIO(uint8(intPin), "A", level)
        return nil
    }

    if slices.Contains([]string{"8", "9", "10", "11", "12", "13", "14", "15"}, pin) {
        intPin, _ := strconv.Atoi(pin)
        intPin = intPin - 8
        mcp.MCPDevice.WriteGPIO(uint8(intPin), "B", level)
        return nil
    }

    return fmt.Errorf("not a valid pin number")
}

func (mcp *MCPDigitalWriter) Connect() error {
    return nil
}

func (mcp *MCPDigitalWriter) Finalize() error {
    return nil
}

func (mcp *MCPDigitalWriter) Name() string {
    return mcp.name
}

func (mcp *MCPDigitalWriter) SetName(n string) {
    mcp.name = n
}

And in main function:

....
r := raspi.NewAdaptor()
bus := 1

mux2 := i2c.NewMCP23017Driver(r, i2c.WithBus(bus), i2c.WithAddress(0x21))
mcpDigitalWriter := mcpDigitalWriter.NewMCPDigitalWriter(mux2)

countRot := 10
stepPerRevision := int(360.0 / 1.875)

stepper := gpio.NewStepperDriver(mcpDigitalWriter,
    [4]string{"0", "1", "2", "3"},
    gpio.StepperModes.DualPhaseStepping,
    uint(stepPerRevision),
)
.....

It works, but I notice that doesn't work as fine as if I connect L298N with stepper directly to Raspberry Pi, makes more noise, less torque and speed. I try changing pins but same result. Does anyone what is happening?. Is something wrong?

Thank you very much.