Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jobily
Iot
提交
803d4ec7
I
Iot
项目概览
jobily
/
Iot
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
Iot
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
803d4ec7
编写于
1月 27, 2022
作者:
I
Ian Bebbington
提交者:
GitHub
1月 27, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make MotorHat compatible with Waveshare Motor Driver Hat (#1764)
上级
62d161bf
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
150 addition
and
42 deletion
+150
-42
src/devices/MotorHat/AdafruitMotorPinProvider.cs
src/devices/MotorHat/AdafruitMotorPinProvider.cs
+29
-0
src/devices/MotorHat/MotorHat.cs
src/devices/MotorHat/MotorHat.cs
+26
-42
src/devices/MotorHat/MotorPinProvider.cs
src/devices/MotorHat/MotorPinProvider.cs
+46
-0
src/devices/MotorHat/MotorPins.cs
src/devices/MotorHat/MotorPins.cs
+21
-0
src/devices/MotorHat/README.md
src/devices/MotorHat/README.md
+1
-0
src/devices/MotorHat/WaveshareMotorPinProvider.cs
src/devices/MotorHat/WaveshareMotorPinProvider.cs
+27
-0
未找到文件。
src/devices/MotorHat/AdafruitMotorPinProvider.cs
0 → 100644
浏览文件 @
803d4ec7
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using
System
;
namespace
Iot.Device.MotorHat
{
/// <summary>
/// <see cref="MotorPinProvider"/> implementation for AdaFruit/Aliexpress
/// </summary>
/// <remarks>
/// These correspond to motor hat screw terminals M1, M2, M3 and M4.
/// </remarks>
public
class
AdafruitMotorPinProvider
:
IMotorPinProvider
{
/// <inheritdoc/>
public
MotorPins
GetPinsForMotor
(
int
index
)
{
return
index
switch
{
1
=>
new
MotorPins
(
8
,
9
,
10
),
2
=>
new
MotorPins
(
13
,
12
,
11
),
3
=>
new
MotorPins
(
2
,
3
,
4
),
4
=>
new
MotorPins
(
7
,
6
,
5
),
_
=>
throw
new
ArgumentException
(
nameof
(
index
),
$"MotorHat Motor must be between 1 and 4 inclusive.
{
nameof
(
index
)}
:
{
index
}
"
)
};
}
}
}
src/devices/MotorHat/MotorHat.cs
浏览文件 @
803d4ec7
...
...
@@ -19,6 +19,11 @@ namespace Iot.Device.MotorHat
/// </summary>
public
const
int
I2cAddressBase
=
0x60
;
/// <summary>
/// The <see cref="IMotorPinProvider"/> to use
/// </summary>
private
readonly
IMotorPinProvider
_pinProvider
;
/// <summary>
/// Motor Hat is built on top of a PCa9685
/// </summary>
...
...
@@ -40,9 +45,26 @@ namespace Iot.Device.MotorHat
/// Setting the frequency above or below this range will cause PWM hardware to be set at its maximum or minimum setting.
/// </remarks>
public
MotorHat
(
I2cConnectionSettings
settings
,
double
frequency
=
1600
)
:
this
(
settings
,
frequency
,
MotorPinProvider
.
Default
)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MotorHat"/> class with the specified I2C settings and PWM frequency.
/// </summary>
/// <param name="settings">The I2C settings of the MotorHat.</param>
/// <param name="frequency">The frequency in Hz to set the PWM controller.</param>
/// <param name="pinProvider">The <see cref="IMotorPinProvider"/> that provides <see cref="MotorPins"/> for various hats.</param>
/// <remarks>
/// The default i2c address is 0x60, but the HAT can be configured in hardware to any address from 0x60 to 0x7f.
/// The PWM hardware used by this HAT is a PCA9685. It has a total possible frequency range of 24 to 1526 Hz.
/// Setting the frequency above or below this range will cause PWM hardware to be set at its maximum or minimum setting.
/// </remarks>
public
MotorHat
(
I2cConnectionSettings
settings
,
double
frequency
=
1600
,
IMotorPinProvider
?
pinProvider
=
default
)
{
I2cDevice
device
=
I2cDevice
.
Create
(
settings
);
_pca9685
=
new
Pca9685
(
device
);
_pinProvider
=
pinProvider
??
MotorPinProvider
.
Default
;
_pca9685
.
PwmFrequency
=
frequency
;
}
...
...
@@ -71,49 +93,11 @@ namespace Iot.Device.MotorHat
/// </remarks>
public
DCMotor
.
DCMotor
CreateDCMotor
(
int
motorNumber
)
{
if
(
motorNumber
<
1
||
motorNumber
>
4
)
{
throw
new
ArgumentOutOfRangeException
(
nameof
(
motorNumber
),
$"Must be between 1 and 4, corresponding with M1, M2, M3 and M4. (Received:
{
motorNumber
}
"
);
}
// The PCA9685 PWM controller is used to control the inputs of two dual motor drivers.
// These correspond to motor hat screw terminals M1, M2, M3 and M4.
// Each motor driver circuit has one speed pin and two IN pins.
// The PWM pin expects a PWM input signal. The two IN pins expect a logic 0 or 1 input signal.
// The variables speed, in1 and in2 variables identify which PCA9685 PWM output pins will be used to drive this DCMotor.
// The speed variable identifies which PCA9685 output pin is used to drive the PWM input on the motor driver.
// And the in1 and in2 variables are used to specify which PCA9685 output pins are used to drive the xIN1 and xIN2 input pins of the motor driver.
int
speedPin
,
in1Pin
,
in2Pin
;
switch
(
motorNumber
)
{
case
1
:
speedPin
=
8
;
in2Pin
=
9
;
in1Pin
=
10
;
break
;
case
2
:
speedPin
=
13
;
in2Pin
=
12
;
in1Pin
=
11
;
break
;
case
3
:
speedPin
=
2
;
in2Pin
=
3
;
in1Pin
=
4
;
break
;
case
4
:
speedPin
=
7
;
in2Pin
=
6
;
in1Pin
=
5
;
break
;
default
:
throw
new
ArgumentException
(
nameof
(
motorNumber
),
$"MotorHat Motor must be between 1 and 4 inclusive.
{
nameof
(
motorNumber
)}
:
{
motorNumber
}
"
);
}
var
motorPins
=
_pinProvider
.
GetPinsForMotor
(
motorNumber
);
var
speedPwm
=
_pca9685
.
CreatePwmChannel
(
s
peedPin
);
var
in1Pwm
=
_pca9685
.
CreatePwmChannel
(
i
n1Pin
);
var
in2Pwm
=
_pca9685
.
CreatePwmChannel
(
i
n2Pin
);
var
speedPwm
=
_pca9685
.
CreatePwmChannel
(
motorPins
.
S
peedPin
);
var
in1Pwm
=
_pca9685
.
CreatePwmChannel
(
motorPins
.
I
n1Pin
);
var
in2Pwm
=
_pca9685
.
CreatePwmChannel
(
motorPins
.
I
n2Pin
);
_channelsUsed
.
Add
(
speedPwm
);
_channelsUsed
.
Add
(
in1Pwm
);
...
...
src/devices/MotorHat/MotorPinProvider.cs
0 → 100644
浏览文件 @
803d4ec7
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using
System
;
namespace
Iot.Device.MotorHat
{
/// <summary>
/// Represents a provider for <see cref="MotorPins"/>
/// </summary>
public
interface
IMotorPinProvider
{
/// <summary>
/// Gets the <see cref="MotorPins"/> for motor at the specified <paramref name="index"/>
/// </summary>
/// <param name="index">The index of the motor to get pins for</param>
/// <returns></returns>
public
abstract
MotorPins
GetPinsForMotor
(
int
index
);
}
/// <summary>
/// Static class providing known <see cref="IMotorPinProvider"/> instances
/// </summary>
public
static
class
MotorPinProvider
{
/// <summary>
/// <see cref="IMotorPinProvider"/> for AdaFruit motor hat
/// </summary>
public
static
readonly
IMotorPinProvider
AdaFruit
=
new
AdafruitMotorPinProvider
();
/// <summary>
/// <see cref="IMotorPinProvider"/> for Aliexpress motor hat
/// </summary>
public
static
readonly
IMotorPinProvider
Aliexpress
=
AdaFruit
;
/// <summary>
/// <see cref="IMotorPinProvider"/> for Waveshare motor hat
/// </summary>
public
static
readonly
IMotorPinProvider
Waveshare
=
new
WaveshareMotorPinProvider
();
/// <summary>
/// Default <see cref="IMotorPinProvider"/>
/// </summary>
public
static
readonly
IMotorPinProvider
Default
=
AdaFruit
;
}
}
src/devices/MotorHat/MotorPins.cs
0 → 100644
浏览文件 @
803d4ec7
// 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.MotorHat
{
/// <summary>
/// Represents a Motor PinConfiguration
/// </summary>
/// <param name="SpeedPin">The pin controlling the speed of the motor</param>
/// <param name="In1Pin">The first pin for controlling direction</param>
/// <param name="In2Pin">The second pin for controlling direction</param>
/// <remarks>
/// The PCA9685 PWM controller is used to control the inputs of two dual motor drivers.
/// Each motor driver circuit has one speed pin and two IN pins.
/// The PWM pin expects a PWM input signal. The two IN pins expect a logic 0 or 1 input signal.
/// The variables SpeedPin, In1Pin and In2Pin variables identify which PCA9685 PWM output pins will be used to drive this DCMotor.
/// The speed variable identifies which PCA9685 output pin is used to drive the PWM input on the motor driver.
/// And the In1Pin and In2Pin are used to specify which PCA9685 output pins are used to drive the xIN1 and xIN2 input pins of the motor driver.
/// </remarks>
public
record
MotorPins
(
int
SpeedPin
,
int
In1Pin
,
int
In2Pin
);
}
src/devices/MotorHat/README.md
浏览文件 @
803d4ec7
...
...
@@ -9,6 +9,7 @@ It also provides 4 extra PWM Outputs, that can be used for anything that require
-
[
Adafruit
](
https://www.adafruit.com/product/2348
)
-
[
Aliexpress
](
http://s.click.aliexpress.com/e/mTB4ZB2s
)
-
[
Waveshare
](
https://www.waveshare.com/wiki/Motor_Driver_HAT
)
## Usage
...
...
src/devices/MotorHat/WaveshareMotorPinProvider.cs
0 → 100644
浏览文件 @
803d4ec7
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using
System
;
namespace
Iot.Device.MotorHat
{
/// <summary>
/// <see cref="MotorPinProvider"/> implementation for Waveshare
/// </summary>
/// <remarks>
/// These correspond to motor hat screw terminals M1 and M2
/// </remarks>
public
class
WaveshareMotorPinProvider
:
IMotorPinProvider
{
/// <inheritdoc/>
public
MotorPins
GetPinsForMotor
(
int
index
)
{
return
index
switch
{
1
=>
new
MotorPins
(
0
,
1
,
2
),
2
=>
new
MotorPins
(
5
,
4
,
3
),
_
=>
throw
new
ArgumentException
(
nameof
(
index
),
$"MotorHat Motor must be either 1 or 2.
{
nameof
(
index
)}
:
{
index
}
"
)
};
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录