用树莓派控制直流电机(L298N)

树莓派 L289N 电机
vctea
发布时间: 2019-06-13
阅读: 1215
<p class="MsoNormal" style="text-indent:21.0pt;"> <br /> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 直流电机是机器人行走机构的理想选择之一,我们通常使用直流电机来驱动机器人行走。之前您已经了解到如何用树莓派控制一个<span>LED</span>灯,本章我们将进一步介绍如何用树莓派控制直流电机,主要内容如下。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 直流电机的基本知识。<span></span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> <span>L298N</span>双<span>H</span>桥直流电机驱动模块相关知识。<span></span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 如何控制直流电机的转向和转速。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 本章我们将继续使用<span><a href="https://www.jianshu.com/p/2e3f65f11390"><span></span></a></span>搭建的多功能实验平台。此外,还需要以下材料:<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 一个小型直流电机。<span></span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 一个可以为小型直流电机供电的电源,为便于后续的实验,我们这里使用一个<span>3V-24V</span>的直流可调电源。<span></span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 一个<span>L298N</span>直流电机驱动模块。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.1pt;"> <b><span>7.1 </span></b><b>直流电机<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 直流电机是一种将直流电能转换为机械能的装置,被广泛的用于驱动各种设备,如电风扇、遥控小车、电动车窗等,非常适合作为机器人的行走机构,您可以很容易的在网上购买到,价格也不是很贵。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 小型直流电机的的电压范围通常是<span>1.5V~30V</span>,使用两根导线连接到直流电源上就能让电机转动起来,电压越高转得就越快。每个直流电机都有一个推荐电压,超出推荐电压太多容易导致电机被烧毁,而供电电压太低则转不起来。通过交换两根连接在电机上的导线顺序(实际上是改变电源的正负极),可以改变电机的转向。前面章节我们说过,直流电机属于高功耗元件,不可以直接使用树莓派进行驱动,因此我们需要使用一个外部电源。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 小型直流电机通常输出很高的转速和较低的扭矩,在作为机器人的驱动装置时,一般需要连接一个变速器,将其转换为低转速、高扭矩,使之有足够的力量驱动机器人。图<span>7.1</span>是典型的带变速器的小型直流电机,建议变速器和电机一起购买。<span></span> </p> <p class="MsoNormal" style="text-align:center;" align="center"> <span></span> </p> <p class="MsoNormal" style="text-align:center;" align="center"> <img src="https://b04.ickimg.com/201906/fb4d5f6995ae08ce98b3ae001131c90f.png" title="diagram7.1.png" alt="diagram7.1.png" /> </p> <p class="MsoNormal" style="text-align:center;" align="center"> 图<span> 7.1 </span>带变速器的小型直流电机<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.1pt;"> <b><span>7.2 </span></b><b>双<span>H</span></b><b>桥直流电机驱动模块(<span>L298N</span></b><b>)<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>H</span>桥(<span>H-Bridge</span>)是控制直流电机的常用电路,因其电路形状酷似字母<span>H</span>而得名,可以控制直流电机的转速和方向,典型的<span>H</span>桥芯片有<span>L293D</span>、<span>L298N</span>等,以下我们以<span>L298N</span>为例进行介绍。实际使用的时候,为提高芯片的安全性、可靠性和方便性,我们一般会给芯片外接一些保护电路,并把输入输出引脚引接到排针上,从而形成一个功能模块。图<span>7.2</span>是一款典型的<span>L298N</span>双<span>H</span>桥直流电机驱动模块,可以很容易从网上购买到,价格也便宜。该模块的主要特点如下: </p> <p class="MsoNormal" style="text-align:center;text-indent:21.0pt;" align="center"> <img src="https://b03.ickimg.com/201906/c8d6357b5b9e4f926c6ede0a1f07de1a.png" title="diagram7.2.png" alt="diagram7.2.png" /> </p> <p class="MsoNormal" style="text-align:center;text-indent:21.0pt;" align="center"> 图<span> 7.2 L298N</span>双<span>H</span>桥直流电机驱动模块<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 采用<span>L298N</span>双桥直流电机驱动芯片<span></span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 工作电压范围<span>5V~35V</span>,如果需要从模块内取电,则供电范围为<span>7V~35V</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 峰值输出电流<span>2A</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 每个通道有一个使能输入端<span></span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> 可以驱动两个直流电机或者一个四线步进电机<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>L298N</span>双<span>H</span>桥直流电机驱动模块的引脚可以归纳成电源、控制和输出等三大类,下面是各类引脚的功能说明。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> <b>电压类引脚<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>+12V</span>输入:<span>L298N</span>芯片的电源正极,模块上标称<span>+12V</span>,但实际范围可以是<span>5V~35V</span>,如果需从模块内取电,则其范围为<span>7V~35V</span>。<span>&nbsp; </span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>GND</span>:<span>L298N</span>芯片的电源地,使用的时候应该把树莓派的<span>GND</span>接到这里,即两者需要共地。<span>&nbsp; </span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>+5V</span>输出:<span>L298N</span>芯片输出的<span>5V</span>电源,可以给外部设备供电,使用的时候需要用跳线把<span>5V</span>输出使能端短接起来。<span>&nbsp; </span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> <b>控制类引脚<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>ENA</span>、<span>ENB</span>:<span>A</span>、<span>B</span>通道的使能端,高电平有效,可以用<span>PWM</span>来实现调速。使用时,可以接到树莓派的<span>GPIO</span>上,实现用程序进行控制。<span>&nbsp; </span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>IN1</span>、<span>IN2</span>、<span>IN3</span>、<span>IN4</span>:<span>IN1</span>、<span>IN2</span>为<span>A</span>通道的控制输入,<span>IN3</span>、<span>IN4</span>为<span>B</span>通道的控制输入。<span>&nbsp; </span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> <b>输出类引脚<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>OUT1</span>、<span>OUT2</span>、<span>OUT3</span>、<span>OUT4</span>:<span>OUT1</span>、<span>OUT2</span>由<span>A</span>通道输出,<span>OUT3</span>、<span>OUT4</span>由<span>B</span>通道输出,可以用于连接直流电机等设备。<span>&nbsp; </span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 控制直流电机的转向和转速实际是通过给控制类引脚设置不同的值来实现的,表<span>7.1</span>是使用通道<span>A</span>的控制直流电机的真值表,通道<span>B</span>与此类似。<span>&nbsp; </span> </p> <p class="MsoNormal" style="text-align:center;text-indent:21.0pt;" align="center"> <span style="text-indent:21pt;"><img src="https://b04.ickimg.com/201906/38be6d0cb0be7cc4ebe18248babb40aa.png" title="table7.1.png" alt="table7.1.png" /><br /> </span> </p> <p class="MsoNormal" style="text-align:center;text-indent:21.0pt;" align="center"> <span style="text-indent:21pt;">表</span><span style="text-indent:21pt;"> 7.1 </span><span style="text-indent:21pt;">通道</span><span style="text-indent:21pt;">A</span><span style="text-indent:21pt;">的控制直流电机的真值表</span> </p> <p class="MsoNormal" style="text-align:center;text-indent:21.0pt;" align="center"> <span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.1pt;background:#E7E6E6;"> <b>提示:<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;background:#E7E6E6;"> 您购买到的<span>L298N</span>双<span>H</span>桥直流电机驱动模块可能与本章介绍的存在一定的差异,但这一类功能模块大同小异,请跟商家索要相应的技术资料了解更多的使用细节。<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.1pt;"> <b><span>7.3 </span></b><b>让直流电机动起来<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 下面进入实战环节,我们将用树莓派通过<span>L298N</span>双<span>H</span>桥直流电机驱动模块控制一个电机的转向和转速。为了能让读者更好的理解,我们首先演示如何控制电机的转向,然后再进一步介绍如何控制电机转速。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>7.3.1 </span>实验电路<span> </span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 图<span>7.3</span>所示的是树莓派、<span>L298N</span>双<span>H</span>桥直流电机驱动模块和电机间连接的电路图,最终实物连线图如图<span>7.4</span>所示。我们使用树莓派的<span>GPIO13</span>、<span>GPIO19</span>和<span>GPIO26</span>等<span>3</span>个引脚,分别连接到<span>L298N</span>双<span>H</span>桥直流电机驱动模块的<span>ENA</span>、<span>IN1</span>和<span>IN2</span>,通过输入表<span>7.1</span>所示的不同电平组合就可以实现对电机的操纵。控制电机的转向和转速都是使用该电路,向<span>ENA</span>输入不同占空比的<span>PWM</span>脉冲信号就可以控制电机的转速。<span></span> </p> <p class="MsoNormal" style="text-align:center;text-indent:21.0pt;" align="center"> <img src="https://b01.ickimg.com/201906/acc75be8d97e05598945a351c45e8ac5.png" title="diagram7.3.png" alt="diagram7.3.png" /> </p> <p class="MsoNormal" style="text-align:center;text-indent:21.0pt;" align="center"> 图<span> 7.3 </span>小型直流电机控制电路图<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-align:center;text-indent:21.0pt;" align="center"> <img src="https://b02.ickimg.com/201906/5652f2080afbf5db324409ffc59ad560.png" title="diagram7.4.png" alt="diagram7.4.png" /> </p> <p class="MsoNormal" style="text-align:center;text-indent:21.0pt;" align="center"> 图<span> 7.4 </span>小型直流电机控制实物连接图<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 尽管<span>L298N</span>双<span>H</span>桥直流电机驱动模块能驱动两个直流电机,但我们只演示如何驱动一个。驱动两个电机使用的是相同的技术,如果在您的项目中需要用到两个,也可以很容易地扩展它。<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.1pt;"> <b><span>7.3.2 </span></b><b>程序思路<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 控制电机转向和转速可以使用同一个电路,但程序思路上我们分两个程序来演示,具体思路如下:<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> <b>控制电机转向思路<span></span></b> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>BEGIN&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>引入<span>GPIO</span>库<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>将连接<span>ENA</span>的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>将连接<span>IN1</span>的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>将连接<span>IN2</span>的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; DO FOREVER&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>IN1</span>设置为<span>0</span>,<span>IN2</span>设置为<span>1</span>,将电机设置为正向转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>ENA</span>设置为<span>1</span>,驱动<span>A</span>通道电机转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>等待<span>5</span>秒<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>ENA</span>设置为<span>0</span>,停止电机转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>等待<span>2</span>秒<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>IN1</span>设置为<span>1</span>,<span>IN2</span>设置为<span>0</span>,将电机设置为反向转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>ENA</span>设置为<span>1</span>,驱动<span>A</span>通道电机转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>等待<span>5</span>秒<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>ENA</span>设置为<span>0</span>,停止电机转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>等待<span>2</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; ENDO&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>停止<span>PWM&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>清理释放<span>GPIO</span>资源<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>END&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.1pt;background:#E7E6E6;"> <b>提示:<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;background:#E7E6E6;"> 除了采用将<span>ENA</span>置<span>0</span>的方法来让电机停止转动,将<span>IN1</span>和<span>IN2</span>同时设置为<span>0</span>或<span>1</span>也可以让电机停止下来。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> <b>控制电机转速思路 <span></span></b> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>BEGIN&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>引入<span>GPIO</span>库<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>将连接<span>ENA</span>的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>将连接<span>IN1</span>的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>将连接<span>IN2</span>的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>设置<span>ENA</span>输入一定频率的<span>PWM</span>脉冲信号,创建<span>PWM</span>对象<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>开始向<span>ENA</span>输入占空比为<span>0</span>的<span>PWM</span>脉冲信号<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; DO FOREVER&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>IN1</span>设置为<span>0</span>,<span>IN2</span>设置为<span>1</span>,将电机设置为正向转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>ENA</span>设置为<span>1</span>,驱动<span>A</span>通道电机转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; speed = 0</span> </p> <p class="MsoNormal"> <span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DOWHILE speed &lt; 100&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将输入<span>ENA</span>的<span>PWM</span>脉冲信号的占空比修改为<span>speed</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; speed += 5</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>等待<span>1</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ENDDO</span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>IN1</span>设置为<span>1</span>,<span>IN2</span>设置为<span>0</span>,将电机设置为反向转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将<span>ENA</span>设置为<span>1</span>,驱动<span>A</span>通道电机转动<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; speed = 0</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DOWHILE speed &lt; 100&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>将输入<span>ENA</span>的<span>PWM</span>脉冲信号的占空比修改为<span>speed</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; speed += 5</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>等待<span>1</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ENDDO</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; ENDO&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>停止<span>PWM&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; </span>清理释放<span>GPIO</span>资源<span>&nbsp; </span> </p> <p class="MsoNormal"> <span>END&nbsp; </span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 电机的转向控制和转速控制的程序思路的最大区别在于<span>ENA</span>输入信号的不同。控制电机转向的程序思路只是简单的通过将<span>ENA</span>设置为<span>1</span>或<span>0</span>来启动和停止电机。对于电机转速控制,<span>ENA</span>输入的<span>PWM</span>脉冲信号,通过改变<span>PWM</span>脉冲信号的占空比来改变电机转速,占空比越大速度就越快,占空比为<span>100%</span>时相当于输入<span>1</span>(高电平),占空比为<span>0%</span>时相当于输入<span>0</span>(低电平)。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.1pt;"> <b><span>7.3.3 </span></b><b>程序代码<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 以下是直流电机转向和转速控制程序,程序的详细说明见注释。<span>&nbsp; </span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> <b>控制电机转向程序 <span></span></b> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>import RPi.GPIO as GPIO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>引入<span>GPIO</span>模块<span></span> </p> <p class="MsoNormal"> <span>import time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span>引入<span>time</span>模块<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>ENA = 13</span> </p> <p class="MsoNormal"> <span>IN1 = 19</span> </p> <p class="MsoNormal"> <span>IN2 = 26</span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>if __name__ == '__main__':</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; try:</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>初始化<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.setmode(GPIO.BCM)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>使用<span>BCM</span>编号方式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.setup(ENA, GPIO.OUT)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将连接<span>ENA</span>的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.setup(IN1, GPIO.OUT)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将连接<span>IN1</span>的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.setup(IN2, GPIO.OUT)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将连接<span>IN2</span>的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while True:</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>驱动电机正向旋转<span>5</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(IN1, False)&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN1</span>设置为<span>0</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(IN2, True)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN2</span>设置为<span>1</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(ENA, True)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>ENA</span>设置为<span>1</span>,启动<span>A</span>通道电机<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sl<ickey>eep(5)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; # </ickey></span>等待电机转动<span>5</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>电机停止<span>2</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(ENA, False)&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>ENA</span>设置为<span>0</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sl<ickey>eep(2)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; # </ickey></span>等待电机停止<span>2</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>驱动电机反向旋转<span>5</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(IN1, True)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN1</span>设置为<span>1</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(IN2, False)&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN2</span>设置为<span>0</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(ENA, True)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>ENA</span>设置为<span>1</span>,启动<span>A</span>通道电机<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sl<ickey>eep(5)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; # </ickey></span>等待电机转动<span>5</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>电机停止<span>2</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(ENA, False)&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>ENA</span>设置为<span>0</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sl<ickey>eep(2)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; # </ickey></span>等待电机停止<span>2</span>秒<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; finally:</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pwm.stop()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>停止<span>PWM</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.cleanup()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>清理释放<span>GPIO</span>资源,将<span>GPIO</span>复位<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 运行这个程序后,您将看到直流电机反复的正向转动<span>5</span>秒后,停止两秒,再反向转动<span>5</span>秒后,停止<span>2</span>秒。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoListParagraph" style="margin-left:42.0pt;text-indent:-21.0pt;"> l&nbsp; <b>控制电机转速程序<span></span></b> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>import RPi.GPIO as GPIO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>引入<span>GPIO</span>模块<span></span> </p> <p class="MsoNormal"> <span>import time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>引入<span>time</span>模块<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>ENA = 13</span> </p> <p class="MsoNormal"> <span>IN1 = 19</span> </p> <p class="MsoNormal"> <span>IN2 = 26</span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>if __name__ == '__main__':</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; try:</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>初始化<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.setmode(GPIO.BCM)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>使用<span>BCM</span>编号方式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.setup(ENA, GPIO.OUT)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# </span>将<span>ENA</span>对应的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.setup(IN1, GPIO.OUT)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN1</span>对应的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.setup(IN2, GPIO.OUT)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN2</span>对应的<span>GPIO</span>引脚设置为输出模式<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; freq = 500</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; speed = 0</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pwm = GPIO.PWM(ENA, freq)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>设置向<span>ENA</span>输入<span>PWM</span>脉冲信号,频率为<span>freq</span>并创建<span>PWM</span>对象<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pwm.start(speed)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>以<span>speed</span>的初始占空比开始向<span>ENA</span>输入<span>PWM</span>脉冲信号<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while True:</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将电机设置为正向转动<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(IN1, False)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN1</span>设置为<span>0</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(IN2, True)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN2</span>设置为<span>1</span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>通过改变<span>PWM</span>占空比,让电机转速不断加快<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for speed in range(0, 100, 5):</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pwm.ChangeDutyCycle(speed)&nbsp; # </span>改变<span>PWM</span>占空比<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sl<ickey>eep(1)</ickey></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将电机设置为反向转动<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;GPIO.output(IN1, True)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN1</span>设置为<span>1</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.output(IN2, False)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>将<span>IN2</span>设置为<span>0</span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>通过改变<span>PWM</span>占空比,让电机转速不断加快<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for speed in range(0, 100, 5):</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pwm.ChangeDutyCycle(speed)&nbsp; # </span>改变<span>PWM</span>占空比<span></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sl<ickey>eep(1)</ickey></span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp; finally:</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pwm.stop()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>停止<span>PWM</span> </p> <p class="MsoNormal"> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO.cleanup()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # </span>清理释放<span>GPIO</span>资源,将<span>GPIO</span>复位<span></span> </p> <p class="MsoNormal"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 运行这个程序后,您将看到直流电机首先正向转动,转动速度不断增加,在达到最大转速后,直流电机短暂停止转动,然后反向转动,转动速度不断增加,如此反复运动。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.1pt;"> <b><span>7.4 </span></b><b>本章小结<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 本章首先介绍了直流电机的基本知识,然后介绍了<span>L298N</span>双<span>H</span>桥直流电机驱动模块的相关基础知识,最后介绍了使用树莓派和<span>L298N</span>双<span>H</span>桥直流电机驱动模块来控制一个直流电机的转向和转速的电路、思路和程序等知识。<span></span> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span>&nbsp;</span> </p> <p class="MsoNormal" style="text-indent:21.1pt;"> <b>后记<span></span></b> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> 最近工作太忙,项目在赶进度,几乎每周都有几天要跑现场,这两周又增加网络安全的事情,断断续续写了一个多月,乘着端午小长假陪完家人还有点时间,好不容易才把这篇给完成了。写这篇文章的成本算是较低,就烧坏了两个一块多钱的小直流电机。另外,把十几年没用过的烙铁又整了出来,居然还能焊得上,且没烧东西,算是对得起当年的老师哈!<span></span> </p> <p> <br /> </p> <p class="MsoNormal" style="text-indent:21.0pt;"> <span></span> </p> ### **树莓派4(4G)秒杀429元活动正在进行中,数量有限,扫码抢购** ![](https://b01.ickimg.com/201909/9ba04e7ba842973d286236329a9412d7.jpg)


原创作品,未经权利人授权禁止转载。详情见转载须知 举报文章

点赞 (2)
打赏
当前打赏0人    
vctea 擅长:什么都玩一点,什么都不擅长,哈
评论(0)

登录后可评论,请 登录注册

相关文章推荐
X
你的打赏是对原创作者最大的认可
请选择打赏IC币的数量,一经提交无法退回 !
100IC币
500IC币
1000IC币
自定义
IC币
确定
X
提交成功 ! 谢谢您的支持
返回

我要举报该内容理由

×
请输入您举报的理由(50字以内)
App