Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stepper over MCP23017 #1078

Open
Juan1ll0 opened this issue Mar 18, 2024 · 0 comments
Open

Stepper over MCP23017 #1078

Juan1ll0 opened this issue Mar 18, 2024 · 0 comments

Comments

@Juan1ll0
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant