Difference between revisions of "BakeBit - Servo/zh"

From FriendlyELEC WiKi
Jump to: navigation, search
(硬件连接)
(介绍)
 
(11 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
==介绍==
 
==介绍==
 
[[File:BakeBit - Servo.jpg|thumb|Servo]]
 
[[File:BakeBit - Servo.jpg|thumb|Servo]]
*BakeBit - Servo是一个舵机模块,内部包含一个直流电机和齿轮传动反馈系统。用去机器人等机械结构的驱动和控制。
+
*BakeBit - Servo是一个舵机模块,内部包含一个直流电机和齿轮传动反馈系统。用于机器人等机械结构的驱动和控制。
 
*舵机的转动和转角都是跟随着信号脉冲的宽度变化而变化的,控制信号为频率50Hz的PWM信号,该控制信号高电平部分范围为0.5ms—2.5ms,对应的舵机旋转角度为0-180°。
 
*舵机的转动和转角都是跟随着信号脉冲的宽度变化而变化的,控制信号为频率50Hz的PWM信号,该控制信号高电平部分范围为0.5ms—2.5ms,对应的舵机旋转角度为0-180°。
给舵机一个PWM信号,舵机便会旋转到对应的角度。信号停止在一个脉冲宽度时舵机也就不转了,停在对应的角度上.
+
*给舵机一个PWM信号,舵机便会旋转到对应的角度。信号停止在一个脉冲宽度时舵机也就不转了,停在对应的角度上.
  
 
==特性==
 
==特性==
 
* 使用标准的2.0mm 4 Pin BakeBit接口
 
* 使用标准的2.0mm 4 Pin BakeBit接口
[[File:BakeBit - Servo002.png | frameless|400px|BakeBit - Servo]]
+
舵机规格尺寸如下:
 +
 
 +
[[File:BakeBit - Servo002.png | frameless|300px|BakeBit - Servo]]
 
*规格参数如下:
 
*规格参数如下:
[[File:BakeBit - Servo003.png | frameless|400px|BakeBit - Servo detail]]
+
[[File:BakeBit - Servo003.png | frameless|430px|BakeBit - Servo detail]]
  
 
* 引脚说明:
 
* 引脚说明:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
|名称 || 描述
+
|引脚 || 名称 || 描述
 
|-
 
|-
|GND   ||
+
|1|| SIG   || 信号
 
|-
 
|-
|5V   || 电源5V
+
|2 || NC   ||
 
|-
 
|-
|NC   ||
+
|3 || 5V   || 电源5V
 
|-
 
|-
|SIG   || 信号
+
|4 || GND   ||
 
|}
 
|}
==硬件连接==
 
使用标准4 Pin连接线与NEO Hub连接。
 
  
参考下图连接模块:
+
== 示例程序:Servo and Rotary Angle Sensor ==
[[File:BakeBit - Servo_nanopi_NEO.jpg|frameless|600px|BakeBit - Servo_nanopi_NEO]]
+
 
 +
这个示例演示用[[BakeBit - Rotary Angle Sensor]]来控制舵机的转动, 随着你转动电位器,舵机将在0~180角范围里旋转。
 +
 
 +
=== 硬件连接 ===
 +
简单的将 舵机 模块插入D5接口,将电位器模块插入A0接口,如下面这样:
 +
 
 +
::{| class="wikitable"
 +
|-
 +
|[[File:Servo and Rotary Angle Sensor-1.jpg |frameless|300px]]    || [[File:Servo and Rotary Angle Sensor-2.jpg |frameless|300px]]
 +
|-
 +
|}
 +
 
 +
=== 示例源代码 ===
 +
 
 +
<syntaxhighlight lang="python">
 +
import time
 +
import bakebit
 +
import random
 +
 
 +
# Connect the servo to digital port D5
 +
# SIG,NC,VCC,GND
 +
servo = 5
 +
 
 +
# Connect the BakeBit Rotary Angle Sensor to analog port A0
 +
# SIG,NC,VCC,GND
 +
potentiometer = 0
 +
 
 +
# Reference voltage of ADC is 5v
 +
adc_ref = 5
 +
 
 +
# Vcc of the bakebit interface is normally 5v
 +
bakebit_vcc = 5
 +
 
 +
# Full value of the rotary angle is 180 degrees, as per it's specs (0 to 180)
 +
full_angle = 180
 +
old_degrees = -1
 +
 
 +
bakebit.pinMode(potentiometer,"INPUT")
 +
bakebit.bakeBitServo_Attach(servo)
 +
 
 +
 
 +
while True:
 +
    try:
 +
        # Read sensor value from potentiometer
 +
        sensor_value = bakebit.analogRead(potentiometer)
 +
 
 +
        # Calculate voltage
 +
        voltage = round((float)(sensor_value) * adc_ref / 1023, 2)
 +
 
 +
        # Calculate rotation in degrees (0 to 180)
 +
        degrees = int((voltage * full_angle) / bakebit_vcc)
 +
 
 +
        if degrees != old_degrees:
 +
            print("sensor_value = %d voltage = %.2f degrees = %d" % (sensor_value, voltage, degrees))
 +
            bakebit.bakeBitServo_Write(servo, degrees)
 +
 
 +
        old_degrees = degrees
 +
 
 +
    except KeyboardInterrupt:
 +
        bakebit.bakeBitServo_Detach(servo)
 +
        break
 +
    except IOError:
 +
        print ("Error")
 +
</syntaxhighlight>
 +
 
 +
[https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_prj_Servo_And_RotaryAngleSensor.py Github]
 +
 
 +
 
 +
=== 运行示例 ===
 +
 +
假设你已经参考[http://wiki.friendlyarm.com/bakebit bakebit教程]安装了BakeBit源代码,<br />
 +
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_Servo_And_RotaryAngleSensor.py:
 +
<syntaxhighlight lang="bash">
 +
cd ~/BakeBit/Software/Python
 +
sudo python bakebit_prj_Servo_And_RotaryAngleSensor.py
 +
</syntaxhighlight>
 +
 
 +
=== 运行结果 ===
 +
 
 +
随着你转动电位器,舵机将在0~180角范围里旋转。
  
==代码说明==
 
 
==相关资料==
 
==相关资料==
 +
*[Specification]([http://wiki.friendlyarm.com/wiki/images/6/6c/Guo_Hua_Servo_GH-SN90D_Specification.pdf BakeBit - Servo Specification.pdf])
 +
*[BakeBit Github项目](https://github.com/friendlyarm/BakeBit)
 +
*[BakeBit Starter Kit手册](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_cn.pdf)

Latest revision as of 10:40, 14 February 2017

English

1 介绍

Servo
  • BakeBit - Servo是一个舵机模块,内部包含一个直流电机和齿轮传动反馈系统。用于机器人等机械结构的驱动和控制。
  • 舵机的转动和转角都是跟随着信号脉冲的宽度变化而变化的,控制信号为频率50Hz的PWM信号,该控制信号高电平部分范围为0.5ms—2.5ms,对应的舵机旋转角度为0-180°。
  • 给舵机一个PWM信号,舵机便会旋转到对应的角度。信号停止在一个脉冲宽度时舵机也就不转了,停在对应的角度上.

2 特性

  • 使用标准的2.0mm 4 Pin BakeBit接口

舵机规格尺寸如下:

BakeBit - Servo

  • 规格参数如下:

BakeBit - Servo detail

  • 引脚说明:
引脚 名称 描述
1 SIG 信号
2 NC
3 5V 电源5V
4 GND

3 示例程序:Servo and Rotary Angle Sensor

这个示例演示用BakeBit - Rotary Angle Sensor来控制舵机的转动, 随着你转动电位器,舵机将在0~180角范围里旋转。

3.1 硬件连接

简单的将 舵机 模块插入D5接口,将电位器模块插入A0接口,如下面这样:

Servo and Rotary Angle Sensor-1.jpg Servo and Rotary Angle Sensor-2.jpg

3.2 示例源代码

import time
import bakebit
import random
 
# Connect the servo to digital port D5
# SIG,NC,VCC,GND
servo = 5
 
# Connect the BakeBit Rotary Angle Sensor to analog port A0
# SIG,NC,VCC,GND
potentiometer = 0
 
# Reference voltage of ADC is 5v
adc_ref = 5
 
# Vcc of the bakebit interface is normally 5v
bakebit_vcc = 5
 
# Full value of the rotary angle is 180 degrees, as per it's specs (0 to 180)
full_angle = 180
old_degrees = -1
 
bakebit.pinMode(potentiometer,"INPUT")
bakebit.bakeBitServo_Attach(servo)
 
 
while True:
    try:
        # Read sensor value from potentiometer
        sensor_value = bakebit.analogRead(potentiometer)
 
        # Calculate voltage
        voltage = round((float)(sensor_value) * adc_ref / 1023, 2)
 
        # Calculate rotation in degrees (0 to 180)
        degrees = int((voltage * full_angle) / bakebit_vcc)
 
        if degrees != old_degrees:
            print("sensor_value = %d voltage = %.2f degrees = %d" % (sensor_value, voltage, degrees))
            bakebit.bakeBitServo_Write(servo, degrees)
 
        old_degrees = degrees
 
    except KeyboardInterrupt:
        bakebit.bakeBitServo_Detach(servo)
        break
    except IOError:
        print ("Error")

Github


3.3 运行示例

假设你已经参考bakebit教程安装了BakeBit源代码,
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_Servo_And_RotaryAngleSensor.py:

cd ~/BakeBit/Software/Python
sudo python bakebit_prj_Servo_And_RotaryAngleSensor.py

3.4 运行结果

随着你转动电位器,舵机将在0~180角范围里旋转。

4 相关资料