Difference between revisions of "BakeBit - Servo"
From FriendlyELEC WiKi
(→特性) |
(→示例程序:Servo and Rotary Angle Sensor) |
||
Line 29: | Line 29: | ||
|} | |} | ||
− | == | + | == Code Sample: Servo and Rotary Angle Sensor == |
− | + | By running this code sample users can control a servo with the [[BakeBit - Rotary Angle Sensor]] module. The servo's streering angle is between 0~180 degrees. | |
− | === | + | === Hardware Connection === |
− | + | Connect the servo module to the NEO-Hub at D5 and the rotary angle sensor module to the NEO-Hub at A0: | |
::{| class="wikitable" | ::{| class="wikitable" | ||
Line 42: | Line 42: | ||
|} | |} | ||
− | === | + | === Source Code === |
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
Line 98: | Line 98: | ||
− | === | + | === Run Code Sample === |
− | + | ||
− | + | Before you run the code sample you need to follow the steps in [http://wiki.friendlyarm.com/bakebit bakebit tutorial] to intall the BakeBit package.<br /> | |
− | + | Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_Servo_And_RotaryAngleSensor.py" program: | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
cd ~/BakeBit/Software/Python | cd ~/BakeBit/Software/Python | ||
Line 107: | Line 107: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | === | + | === Observation === |
− | + | When you rotate the rotary angle module the servo will be steered between 0 to 180 degrees. | |
==相关资料== | ==相关资料== |
Revision as of 10:47, 14 December 2016
Contents
1 Introduction
- The BakeBit - Servo is a servo module which contains a DC motor and a transmission system.
- Its input signal is PWM. The motor's steering angle changes according to PWM signals' changes. The input PWM's frequency is 50Hz and its width is 0.5ms—2.5ms. The motor's steering angle is 0 - 180°。
- A PWM input steers the motor to an angle. When no input signals are applied the motor will stop.
2 Hardware Spec
- Standard 2.0mm pitch 4-Pin BakeBit Interface
Servo's dimension
- Spec:
- Pin Description:
Pin | Name | Description |
1 | SIG | Signal |
2 | NC | Not Connected |
3 | 5V | 5V Supply Voltage |
4 | GND | Ground |
3 Code Sample: Servo and Rotary Angle Sensor
By running this code sample users can control a servo with the BakeBit - Rotary Angle Sensor module. The servo's streering angle is between 0~180 degrees.
3.1 Hardware Connection
Connect the servo module to the NEO-Hub at D5 and the rotary angle sensor module to the NEO-Hub at A0:
3.2 Source Code
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")
3.3 Run Code Sample
Before you run the code sample you need to follow the steps in bakebit tutorial to intall the BakeBit package.
Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_Servo_And_RotaryAngleSensor.py" program:
cd ~/BakeBit/Software/Python sudo python bakebit_prj_Servo_And_RotaryAngleSensor.py
3.4 Observation
When you rotate the rotary angle module the servo will be steered between 0 to 180 degrees.
4 相关资料
- [Specification](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)