Skip to content

Driver DRV8825 for stepper motor was added #1407

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

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions devices/Drv8825/Direction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Iot.Device.Drv8825
{
/// <summary>
/// Motor rotation direction.
/// </summary>
public enum Direction
{
/// <summary>
/// Rotation in the same direction as a clock's hands.
/// </summary>
Clockwise,

/// <summary>
/// Rotation in the opposite direction to the movement of the hands of a clock.
/// </summary>
Counterclockwise
}
}
194 changes: 194 additions & 0 deletions devices/Drv8825/Drv8825.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Device;
using System.Device.Gpio;
using System.Threading;
using UnitsNet;

namespace Iot.Device.Drv8825
{
/// <summary>
/// Class for controlling Drv8825 stepper motor driver.
/// </summary>
public class Drv8825 : IDisposable
{
private readonly ushort _fullStepsPerRotation;
private readonly GpioPin _stepPin;
private readonly GpioPin _dirPin;
private readonly GpioPin? _sleepPin;
private readonly bool _shouldDisposeGpioController;
private readonly MicrostepsController _microstepsController;

private GpioController? _gpioController;

/// <summary>
/// Gets or sets the delay that allows the driver to recognize a step. By default, it is 5 microseconds.
/// It is not recommended to set this delay less than 2 microseconds,
/// otherwise the motor will "miss"(skip) some steps.
/// And it is not recommended to set this delay more than 50 microseconds,
/// otherwise the morot will stop recognize steps.
/// </summary>
public TimeSpan StepDelay { get; set; } = TimeSpan.FromTicks(5 * 10L);

/// <summary>
/// Gets or sets delay between each step (or microstep).
/// </summary>
public TimeSpan DelayBetweenSteps { get; set; } = TimeSpan.Zero;

/// <summary>
/// Initializes a new instance of the <see cref="Drv8825" /> class.
/// </summary>
/// <param name="stepPin">Microcontroller pin connected to STEP driver pin. Used to set steps count.</param>
/// <param name="dirPin">Microcontroller pin connected to DIR driver pin. Used to set rotation direction.</param>
/// <param name="sleepPin">Microcontroller pin connected to SLP driver pin. Used to wake up and put the driver to sleep. Usually SLP need to connect with RST pin.</param>
/// <param name="fullStepsPerRotation">How many steps your motor need to make full rotation. For example, Nema 17 takes 200 full steps to complete full rotation.</param>
/// <param name="gpioController">GPIO controller. If it not passed, then it be created here.</param>
/// <param name="shouldDisposeGpioController">True to dispose the Gpio Controller when this class wiill be disposed.</param>
/// <param name="m0Pin">Microcontroller pin connected to M0 driver pin. Can be used to microsteps control. 0 if not connected.</param>
/// <param name="m1Pin">Microcontroller pin connected to M1 driver pin. Can be used to microsteps control. 0 if not connected.</param>
/// <param name="m2Pin">Microcontroller pin connected to M2 driver pin. Can be used to microsteps control. 0 if not connected.</param>
public Drv8825(
byte stepPin,
byte dirPin,
byte sleepPin = 0,
ushort fullStepsPerRotation = 200,
GpioController? gpioController = null,
bool shouldDisposeGpioController = true,
byte m0Pin = 0,
byte m1Pin = 0,
byte m2Pin = 0)
{
_fullStepsPerRotation = fullStepsPerRotation;
_gpioController = gpioController ?? new GpioController();
_shouldDisposeGpioController = shouldDisposeGpioController || gpioController is null;
_stepPin = _gpioController.OpenPin(stepPin, PinMode.Output);
_dirPin = _gpioController.OpenPin(dirPin, PinMode.Output);

if (sleepPin > 0)
{
_sleepPin = _gpioController.OpenPin(sleepPin, PinMode.Output);
}

_microstepsController = new MicrostepsController(_gpioController, m0Pin, m1Pin, m2Pin);
}

/// <summary>
/// Switch driver to working mode.
/// </summary>
/// <exception cref="InvalidOperationException">Throws when sleep pin doesn't passed.</exception>
public void WakeUp()
{
if (_sleepPin == null)
{
throw new InvalidOperationException("Sleep pin does not passed. Try to create driver class with sleepPin param");
}

_sleepPin.Write(PinValue.High);
}

/// <summary>
/// Switch driver to sleep mode.
/// </summary>
/// <param name="millisecondsDelay">The number of milliseconds to wait before going to sleep. Use if you want give to driver time to process all previously sent steps.</param>
/// <exception cref="InvalidOperationException">Throws when sleep pin doesn't passed.</exception>
public void Sleep(int millisecondsDelay = 0)
{
if (_sleepPin == null)
{
throw new InvalidOperationException("Sleep pin does not passed. Try to create driver class with sleepPin param");
}

if (millisecondsDelay > 0)
{
Thread.Sleep(millisecondsDelay);
}

_sleepPin.Write(PinValue.Low);
}

/// <summary>
/// Rotates a stepper motor.
/// </summary>
/// <param name="angle">Angle to rotate.</param>
/// <param name="stepSize">Step size.</param>
public void Rotate(Angle angle, StepSize stepSize = StepSize.FullStep)
{
if (angle.Degrees == 0)
{
return;
}

_dirPin.Write(angle.Degrees > 0 ? PinValue.High : PinValue.Low);
var degreeForStepsCalculation = angle.Degrees < 0 ? -angle.Degrees : angle.Degrees;
var pulses = degreeForStepsCalculation / 360 * _fullStepsPerRotation * (byte)stepSize;

_microstepsController.Set(stepSize);
Rotate((int)pulses);
}

/// <summary>
/// Rotates a stepper motor.
/// </summary>
/// <param name="steps">Steps count.</param>
/// <param name="direction">True to go forward, false to go back(or vice versa if you connect the motor in the opposite direction).</param>
/// <param name="size">Step size.</param>
public virtual void Rotate(
int steps,
Direction direction = Direction.Clockwise,
StepSize size = StepSize.FullStep)
{
if (steps == 0)
{
return;
}

_microstepsController.Set(size);
_dirPin.Write(direction == Direction.Clockwise ? PinValue.High : PinValue.Low);

Rotate(steps);
}

/// <summary>
/// <inheritdoc/>
/// </summary>
public void Dispose()
{
if (_shouldDisposeGpioController)
{
_gpioController?.Dispose();
}

_gpioController = null;
}

/// <summary>
/// Controls the speed of rotation.
/// </summary>
private void SleepBetweenSteps()
{
if (DelayBetweenSteps == TimeSpan.Zero)
{
return;
}

Thread.Sleep(DelayBetweenSteps);
}

/// <summary>
/// Rotates a stepper motor.
/// </summary>
/// <param name="steps">Steps count.</param>
private void Rotate(int steps)
{
for (var i = 0; i < steps; i++)
{
_stepPin.Write(PinValue.High);
Thread.Sleep(StepDelay);
_stepPin.Write(PinValue.Low);
SleepBetweenSteps();
}
}
}
}
72 changes: 72 additions & 0 deletions devices/Drv8825/Drv8825.nfproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.props" Condition="Exists('packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.props')" />
<PropertyGroup Label="Globals">
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
<Nullable>enable</Nullable>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>5aae94ba-29f0-4004-8404-1a5095698531</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>Iot.Device.Drv8825</RootNamespace>
<AssemblyName>Iot.Device.Drv8825</AssemblyName>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<DocumentationFile>bin\$(Configuration)\Iot.Device.Drv8825.xml</DocumentationFile>
<LangVersion>9.0</LangVersion>
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<RestoreLockedMode Condition="'$(TF_BUILD)' == 'True' or '$(ContinuousIntegrationBuild)' == 'True'">true</RestoreLockedMode>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\key.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Reference Include="mscorlib, Version=1.17.11.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>packages\nanoFramework.CoreLibrary.1.17.11\lib\mscorlib.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.Runtime.Events, Version=1.11.32.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>packages\nanoFramework.Runtime.Events.1.11.32\lib\nanoFramework.Runtime.Events.dll</HintPath>
</Reference>
<Reference Include="System.Device.Gpio, Version=1.1.57.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>packages\nanoFramework.System.Device.Gpio.1.1.57\lib\System.Device.Gpio.dll</HintPath>
</Reference>
<Reference Include="System.Math, Version=1.5.116.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>packages\nanoFramework.System.Math.1.5.116\lib\System.Math.dll</HintPath>
</Reference>
<Reference Include="UnitsNet.Angle, Version=5.75.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>packages\UnitsNet.nanoFramework.Angle.5.75.0\lib\UnitsNet.Angle.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<Compile Include="Direction.cs" />
<Compile Include="Drv8825.cs" />
<Compile Include="StepSize.cs" />
<Compile Include="MicrostepsController.cs" />
<None Include="README.md" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
<Import Project="packages\StyleCop.MSBuild.6.2.0\build\StyleCop.MSBuild.targets" Condition="Exists('packages\StyleCop.MSBuild.6.2.0\build\StyleCop.MSBuild.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\StyleCop.MSBuild.6.2.0\build\StyleCop.MSBuild.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\StyleCop.MSBuild.6.2.0\build\StyleCop.MSBuild.targets'))" />
<Error Condition="!Exists('packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.props'))" />
<Error Condition="!Exists('packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.targets'))" />
</Target>
<Import Project="packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.targets" Condition="Exists('packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.targets')" />
</Project>
39 changes: 39 additions & 0 deletions devices/Drv8825/Drv8825.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>nanoFramework.Iot.Device.Drv8825</id>
<version>$version$</version>
<title>nanoFramework.Iot.Device.Drv8825</title>
<authors>nanoframework</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="file">LICENSE.md</license>
<releaseNotes>
</releaseNotes>
<readme>docs\README.md</readme>
<developmentDependency>false</developmentDependency>
<projectUrl>https://github.com/nanoframework/nanoFramework.IoT.Device</projectUrl>
<icon>images\nf-logo.png</icon>
<repository type="git" url="https://github.com/nanoframework/nanoFramework.IoT.Device" commit="$commit$" />
<copyright>Copyright (c) .NET Foundation and Contributors</copyright>
<description>This package includes the .NET IoT Core binding Iot.Device.Drv8825 for .NET nanoFramework C# projects.</description>
<summary>Iot.Device.Drv8825 assembly for .NET nanoFramework C# projects</summary>
<tags>nanoFramework C# csharp netmf netnf Iot.Device.Drv8825 Drv8825</tags>
<dependencies>
<dependency id="nanoFramework.CoreLibrary" version="1.17.11" />
<dependency id="nanoFramework.Runtime.Events" version="1.11.32" />
<dependency id="nanoFramework.System.Device.Gpio" version="1.1.57" />
<dependency id="nanoFramework.System.Math" version="1.5.116" />
<dependency id="UnitsNet.nanoFramework.Angle" version="5.75.0" />
</dependencies>
</metadata>
<files>
<file src="bin\Release\Iot.Device.Drv8825.dll" target="lib\Iot.Device.Drv8825.dll" />
<file src="bin\Release\Iot.Device.Drv8825.pdb" target="lib\Iot.Device.Drv8825.pdb" />
<file src="bin\Release\Iot.Device.Drv8825.pdbx" target="lib\Iot.Device.Drv8825.pdbx" />
<file src="bin\Release\Iot.Device.Drv8825.pe" target="lib\Iot.Device.Drv8825.pe" />
<file src="bin\Release\Iot.Device.Drv8825.xml" target="lib\Iot.Device.Drv8825.xml" />
<file src="README.md" target="docs\" />
<file src="..\..\assets\nf-logo.png" target="images" />
<file src="..\..\LICENSE.md" target="" />
</files>
</package>
35 changes: 35 additions & 0 deletions devices/Drv8825/Drv8825.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36203.30
MinimumVisualStudioVersion = 10.0.40219.1
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Drv8825", "Drv8825.nfproj", "{5AAE94BA-29F0-4004-8404-1A5095698531}"
EndProject
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Drv8825.samples", "samples\Drv8825.samples.nfproj", "{E8798205-E0D7-443F-B96A-8F752B7EF454}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5AAE94BA-29F0-4004-8404-1A5095698531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5AAE94BA-29F0-4004-8404-1A5095698531}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AAE94BA-29F0-4004-8404-1A5095698531}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{5AAE94BA-29F0-4004-8404-1A5095698531}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AAE94BA-29F0-4004-8404-1A5095698531}.Release|Any CPU.Build.0 = Release|Any CPU
{5AAE94BA-29F0-4004-8404-1A5095698531}.Release|Any CPU.Deploy.0 = Release|Any CPU
{E8798205-E0D7-443F-B96A-8F752B7EF454}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E8798205-E0D7-443F-B96A-8F752B7EF454}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8798205-E0D7-443F-B96A-8F752B7EF454}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{E8798205-E0D7-443F-B96A-8F752B7EF454}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8798205-E0D7-443F-B96A-8F752B7EF454}.Release|Any CPU.Build.0 = Release|Any CPU
{E8798205-E0D7-443F-B96A-8F752B7EF454}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {724AA4FB-36FD-46CB-ADB0-63E6CA4C322F}
EndGlobalSection
EndGlobal
Binary file added devices/Drv8825/Drv8825_circuit_bb.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading