第一部分:
控制中心:树莓派
操作系统:centos arm64
针对树莓派4b操作系统,需要安装不同于2,3系列的安装包。由于4b有运行内存 1g,2g,4g,8g多个版本。由于支持4g以上内存的系统必须是64位系统。所以4g,8g需要选择64位系统。
centos操作系统也一样,需要使用和3b,3b+不同的安装包。
附件分享一个操作系统可用的4b的操作系统下载地址:
下面是32位系统,支持1g,2g版本
一.链接:https://pan.baidu.com/s/1zetymqfjykx9avbvkz60oq
提取码:gh8g
二.sd卡格式化,需要使用sdcardformatterv5_winen;
三. 烧录系统需要使用balenaetcher-setup-1.4.7-x64
具体下载地址:链接: https://pan.baidu.com/s/1vmlymt-dllyarvery8y0vq 提取码: cguu
其他操作系统也可以从centos官网下载,centos8现在在4b-4g上能启动,不过进入不了系统。这个是需要注意的。
8g版本需要使用下面的连接:
链接: https://pan.baidu.com/s/1xyyfah4vftg-37r_f2m7zq 提取码: 48hw
购买树莓派并安装操作系统成功以后,进入第二部分
第二部分:
我们知道树莓派提供一个硬件pwm引脚,可以通过脉冲宽度调制进行控制电机的速度。实际上在精度要求不是非常高的情况下,普通引脚也可以通过软件模拟来控制电机,实现pwm一样的功能。
一.准备
树莓派4b
电机驱动l298n
直流电机
电源盒(4节1.5v电池)
电源线若干
二.电路连接示意图
使用方式如下:
https://shumeipai.nxez.com/raspberry-pi-pins-version-40
l298n示意图
详细的针脚
其中,in1,in2控制电机a,in3,in4控制电机b
l298n12符输入,接电源盒正极;l298n的gnd接电源盒的负极,同时接树莓派gnd,以实现共地。
电路连接设计思路:使用gpio 1控制in1;guio 02控制in2;gpio29控制ena(a通道使能;通过使能接口接成pwm模式,控制速度)。
关于跳线帽:
关于ena和enb键帽,拆下后ena和enb分别有两根线,与in1-4平行的是ena和enb使能端,剩下的是5v电源针脚。由于必须在ena或enb处于高电平时,才能使相应的电机运转,所以通过键帽把它们默认接到5v电源上,使之默认为高电平。
故若只需控制电机的正反转,可以不用管键帽,只关心in1-4即可。若要对电机进行调速,则需拆下键帽,对ena和enb使能端输入pwm脉冲,而剩下的5v电源针脚空闲即可。
不知道你买的是什么型号的l298n。有些型号标记了ea、eb和5v,就算没标记也没关系,自己实际试一下就知道了。
实际上,可以用万用表测试一下两个针脚的电压,如果红色的(万用表的正极)连接的一端显示出来的数字是正数,那么这端就是高电压(实际测试下来是2v,不到5v).所以在将低电压的那一段外接到树莓派即可。
测试下来,我买的红板,是靠近字符 ena这边更近的那根针是需要外接的,是低电平。
https://www.zhihu.com/question/50074435/answer/140589620
三。程序
原理:gpio使用gpio模式进行工作,通过gpio接口,设置脉冲幅度数值(默认值是100,通过调整数值来控制电流的强弱,从而控制电机的运动速度)。
官网的例子
/* * #%l * ********************************************************************** * organization : pi4j * project : pi4j :: java examples * filename : softpwmexample.java * * this file is part of the pi4j project. more information about * this project can be found here: http://www.pi4j.com/ * ********************************************************************** * %% * copyright (c) 2012 - 2019 pi4j * %% * this program is free software: you can redistribute it and/or modify * it under the terms of the gnu lesser general public license as * published by the free software foundation, either version 3 of the * license, or (at your option) any later version. * * this program is distributed in the hope that it will be useful, * but without any warranty; without even the implied warranty of * merchantability or fitness for a particular purpose. see the * gnu general lesser public license for more details. * * you should have received a copy of the gnu general lesser public * license along with this program. if not, see * . * #l% */import com.pi4j.io.gpio.*;import com.pi4j.platform.platformalreadyassignedexception;import com.pi4j.util.commandargumentparser;import com.pi4j.util.console;/** *
* this example code demonstrates how to setup a software emulated pwm pin using the raspberrypi gpio pins. *
* * @author robert savage */public class softpwmexample {/** * [argument/option --pin (#) | -p (#) ] * this example program accepts an optional argument for specifying the gpio pin (by number) * to use with this gpio listener example. if no argument is provided, then gpio #1 will be used. * -- example: --pin 4 or -p 0. * * @param args * @throws interruptedexception * @throws platformalreadyassignedexception */public static void main(string[] args) throws interruptedexception, platformalreadyassignedexception {// create pi4j console wrapper/helper// (this is a utility class to abstract some of the boilerplate code)final console console = new console();// print program title/headerconsole.title(, softpwm example (software-driven pwm emulation));// allow for user to exit program using ctrl-cconsole.promptforexit();// create gpio controllerfinal gpiocontroller gpio = gpiofactory.getinstance();// by default we will use gpio pin #01; however, if an argument// has been provided, then lookup the pin by addresspin pin = commandargumentparser.getpin(raspipin.class, // pin provider class to obtain pin instance fromraspipin.gpio_01, // default pin if no pin argument foundargs); // argument array to search in// we will provision the pin as a software emulated pwm output// pins that support hardware pwm should be provisioned as normal pwm outputs// each software emulated pwm pin does consume additional overhead in// terms of cpu usage.//// software emulated pwm pins support a range between 0 (off) and 100 (max) by default.//// please see: http://wiringpi.com/reference/software-pwm-library/// for more details on software emulated pwmgpiopinpwmoutput pwm = gpio.provisionsoftpwmoutputpin(pin);// optionally set the pwm range (100 is default range)pwm.setpwmrange(100);// prompt user that we are readyconsole.println( ... successfully provisioned pwm pin: + pwm.tostring());console.emptyline();// set the pwm rate to 100 (fully on)pwm.setpwm(100);console.println(software emulated pwm rate is: + pwm.getpwm());console.println(press enter to set the pwm to a rate of 50);system.console().readline();// set the pwm rate to 50 (1/2 duty cycle)pwm.setpwm(50);console.println(software emulated pwm rate is: + pwm.getpwm());console.println(press enter to set the pwm to a rate to 0 (stop pwm));system.console().readline();// set the pwm rate to 0 (fully off)pwm.setpwm(0);console.println(software emulated pwm rate is: + pwm.getpwm());// stop all gpio activity/threads by shutting down the gpio controller// (this method will forcefully shutdown all gpio monitoring threads and scheduled tasks)gpio.shutdown();}}
其中console是java控制台,打日志用的,方便调试。
// by default we will use gpio pin #01; however, if an argument// has been provided, then lookup the pin by addresspin pin = commandargumentparser.getpin(raspipin.class, // pin provider class to obtain pin instance fromraspipin.gpio_01, // default pin if no pin argument foundargs); // argument array to search in
这里的含义是,如果输入控制引脚,则使用输入的引脚,如果不输入,就使用默认的gpio_01引脚
具体程序,可以根据例子,进行设置针脚的高低电平实现
需要源码请关注专栏,留言
亚太地区数字化转型正面临最大挑战
IOS和安卓手机综合性能排行榜前五十,再没有你的就没救了!
工业创新智造先行 机器视觉与智能生产线受关注
如何选购HDMI电缆,有哪些技巧和事项
劲爆!2016年5月畅销手机市场分析报告
java高级语言制作智能小车编程实例
机器人咖啡厅在日本开业,由软银机器人提供技术支持
Vivo继续为其Y系列添加新成员
中国通信行业的发展历史以及目前现状
天玑 × 虎牙高能嘉年华燃情谢幕,MediaTek 天玑助力电竞名手决胜赛场
未来电脑配件趋势:透明的多点触摸键盘鼠标
基于单片机实现智能照明控制系统的设计
授人以鱼不如授人以渔,这些三极管放大电路简便设计技巧,让你可以少走弯路
vivo S5发布,几何菱形四摄打破传统设计思维
品茗股份小茗AI入选2021年服贸会专题“科技创新应用成果案例”
ARM中的编码方式与寻址方式有何不同?
新能源汽车的未来市场前景将会是如何
探究金属锂负极关键技术及解决思路
电子管音调电路图大全(六款电子管音调电路原理图详解)
为什么需要双工器?