64 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# MG90(s) 舵机驱动
## -1. This repository was archived.
由于目前已经无力进行单片机开发因此本项目暂停开发仓库将于2024/2/2归档您可以在Github、Gitee等网站上寻找类似项目代码使用之后如果有需要可能会不定时恢复项目开发。
## 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)
```
## 3. 引用
本模块部分内容参考和引用了以下文章或者视频:
- [【csdn】物联网开发笔记54- 使用Micropython开发ESP32开发板之控制MG90S舵机](https://blog.csdn.net/zhusongziye/article/details/128085911)
- [【bilibili】33. 舵机-基于MicroPython的ESP32仿真](https://www.bilibili.com/video/BV1D84y1y7hC/?share_source=copy_web&vd_source=d5c937d9897e79931f44e07bc5cf875c)