add Readme and update code
This commit is contained in:
parent
9aa38ed22f
commit
3ff8a6cf89
2
MG90.py
2
MG90.py
@ -22,6 +22,8 @@ class MG90:
|
|||||||
|
|
||||||
# 设置电机旋转位置
|
# 设置电机旋转位置
|
||||||
def setDegree(self, target):
|
def setDegree(self, target):
|
||||||
|
if target > self.MAX_DEG or target < self.MIN_DEG:
|
||||||
|
raise ValueError("'target' out of range, it must beteween " + str(self.MIN_DEG) + " and " + str(self.MAX_DEG))
|
||||||
self._degree = target
|
self._degree = target
|
||||||
self.pwm_control.duty(self._dutyCalc(self._degree))
|
self.pwm_control.duty(self._dutyCalc(self._degree))
|
||||||
|
|
||||||
|
|||||||
57
README.md
Normal file
57
README.md
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
# MG90(s) 舵机驱动
|
||||||
|
|
||||||
|
## 0. 驱动原理
|
||||||
|
舵机控制一般需要一个20ms左右的时基脉冲(50Hz),该脉冲的高电平部分一般为0.5ms~2.5ms范围内角度控制脉冲部分,总间隔为2ms。
|
||||||
|
MG90(s)的驱动范围是0°~180°,可以得出控制角度和高电平时间的对应关系:t = 0.5 + 2 * 角度 / 180° (ms)
|
||||||
|
根据此原理可以通过控制PWM占空比来调整舵机旋转角。
|
||||||
|
|
||||||
|
## 1. 使用说明
|
||||||
|
首先,需要上传模块到开发板并导入:
|
||||||
|
```python
|
||||||
|
import MG90
|
||||||
|
# 当然,也可以使用以下导入语句
|
||||||
|
from MG90 import MG90
|
||||||
|
```
|
||||||
|
|
||||||
|
之后初始化一个MG90对象
|
||||||
|
```python
|
||||||
|
from machine import Pin
|
||||||
|
# 直接传入GPIO Pin编号
|
||||||
|
Servo = MG90.MG90(32)
|
||||||
|
# 您也可以选择直接传入一个Pin对象
|
||||||
|
Servo = MG90.MG90(Pin(32))
|
||||||
|
```
|
||||||
|
|
||||||
|
大功告成!下面给出模块可以使用的方法
|
||||||
|
```python
|
||||||
|
setDegree(target) # 旋转电机到指定角度
|
||||||
|
getDegree() -> int # 获取当前电机角度,不一定准确,仅供参考
|
||||||
|
Servo.resetDegree() # 电机角度置零,即旋转到0度
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## 2. 使用样例
|
||||||
|
```python
|
||||||
|
import MG90
|
||||||
|
from time import sleep
|
||||||
|
from machine import Pin
|
||||||
|
|
||||||
|
# 初始化MG90对象
|
||||||
|
# Servo = MG90.MG90(32)
|
||||||
|
Servo = MG90.MG90(Pin(32))
|
||||||
|
# 旋转到 0 度
|
||||||
|
Servo.setDegree(0)
|
||||||
|
sleep(1)
|
||||||
|
# 旋转到 45 度
|
||||||
|
Servo.setDegree(45)
|
||||||
|
sleep(1)
|
||||||
|
Servo.setDegree(90)
|
||||||
|
# 获取当前电机位置
|
||||||
|
print(Servo.getDegree())
|
||||||
|
sleep(1)
|
||||||
|
Servo.setDegree(180)
|
||||||
|
sleep(2)
|
||||||
|
# 重置
|
||||||
|
Servo.resetDegree()
|
||||||
|
sleep(1)
|
||||||
|
```
|
||||||
Loading…
x
Reference in New Issue
Block a user