一、实验准备
控制连接在扩展板上的舵机。如下图所示,按键KEY1为板载元器件,所以不需要外接其他设备。
二、运行代码
# Import the Raspbot library
import time
from Raspbot_Lib import Raspbot
from ipywidgets import interact
import ipywidgets as widgets
# Create the Rosmaster object bot
bot = Raspbot()
def pwm_servo(S1, S2):
bot.Ctrl_Servo(1, S1)
bot.Ctrl_Servo(2, S2)
return S1, S2
# Create four sliders to control PWM steering gear
interact(pwm_servo, \
S1=widgets.IntSlider(min=0,max=180,step=1,value=90), \
S2=widgets.IntSlider(min=0,max=110,step=1,value=25));
#reset
bot.Ctrl_Servo(1, 90)
bot.Ctrl_Servo(2, 25)
#After the program is complete, delete the object to avoid conflicts caused by using the library in other programs
del bot
三、核心代码解析
控制舵机需要用到的Raspbot_Lib库函数:
Ctrl_Servo(id, angle):
参数解释:控制连接在扩展板S1,S2。
- id=1:控制S1舵机,id=2:控制S1舵机。
- angle=[0,180],控制舵机旋转角度
- 返回值:无。
四、实验现象
程序运行后,可以滑块控制舵机的角度。