java高级语言制作智能小车编程实例

java高级语言制作智能小车编程实例,第1张

第一部分:

控制中心:树莓派

*** 作系统: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电池

电源线若干

二.电路连接示意图

使用方式如下:

java高级语言制作智能小车编程实例,watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ppYW9femc=,size_16,color_FFFFFF,t_70,第2张

https://shumeipai.nxez.com/raspberry-pi-pins-version-40

L298N示意图

java高级语言制作智能小车编程实例,watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ppYW9femc=,size_16,color_FFFFFF,t_70,第3张

详细的针脚

java高级语言制作智能小车编程实例,watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ppYW9femc=,size_16,color_FFFFFF,t_70,第4张java高级语言制作智能小车编程实例,watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ppYW9femc=,size_16,color_FFFFFF,t_70,第5张

其中,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("<-- The Pi4J Project -->", "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引脚

具体程序,可以根据例子,进行设置针脚的高低电平实现

 

需要源码请关注专栏,留言

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/dianzi/2484634.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-04
下一篇 2022-08-04

发表评论

登录后才能评论

评论列表(0条)

保存