Tcar:智能车之基于L298N电机驱动芯片的驱动模块

2023-12-27 00:38

本文主要是介绍Tcar:智能车之基于L298N电机驱动芯片的驱动模块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

2、电机驱动模块 - L298N电机驱动芯片
    // env/motor.zip
   两个直流电机,控制前轮的用于转向
                 控制后轮的用于前进后退
   编程,让用户方便的控制小车的运动

   2.1 电机的驱动
       硬件的接法:
           电机上需要的瞬间电流可能是安培级的
           而CPU上的管脚输出的电流是毫安级的
                 
        直流电机 步进电机 伺服电机
       
        L298N芯片 有 15个引脚,可以驱动 两台直流电机
           ENABLE A
           INPUT 1/2
           ENABLE B
           INPUT 3/4
       软件控制直流电机,就是控制L298N芯片
       如何控制L298N?
          M1正转: 
                  ENABLEA 高
                  INPUT1  高
                  INPUT2  低

       控制L298N 就是控制CPU上对应的管脚

    2.2 应用程序
       gui_client: 
           5个按钮
           点击按钮时给server 发不同的命令
              
       开发板上的server
           接收到命令
           根据不同的命令  
               open
               ioctl(...) 
           具体到小车 udp server 8000
                      30/31/32/33/34
// tcar_src.tar.gz/motor/app(应用) driver(驱动)
// 驱动代码基于S5PV210开发板编程的,非S5P6818


/* motor_drv.c - 基于S5PV210开发板连接的L298N芯片驱动 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <mach/gpio.h>
#include <asm/io.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>#include "motor_cmd.h"#define TCAR_ENA    S5PV210_GPH3(2)
#define TCAR_IN1    S5PV210_GPH2(2)
#define TCAR_IN2    S5PV210_GPH3(1)
#define TCAR_ENB    S5PV210_GPH2(0)
#define TCAR_IN3    S5PV210_GPH2(1)
#define TCAR_IN4    S5PV210_GPH3(0)typedef struct _tcar_gpios
{unsigned int gpio_no;const char *name;
}tcar_gpios_t;tcar_gpios_t gpio_pins[] ={{.gpio_no = TCAR_ENA,.name   = "GPH3_2"},{.gpio_no = TCAR_IN1,.name   = "GPH2_2"},{.gpio_no = TCAR_IN2,.name   = "GPH3_1"},{.gpio_no = TCAR_ENB,.name   = "GPH2_0"},{.gpio_no = TCAR_IN3,.name   = "GPH2_1"},{.gpio_no = TCAR_IN4,.name   = "GPH3_0"},
};struct timer_list tcar_timer;void tcar_gpio_init(void)
{int pins_num;int i  = 0;pins_num = ARRAY_SIZE(gpio_pins);for(; i<pins_num; i++){gpio_request(gpio_pins[i].gpio_no, gpio_pins[i].name);gpio_direction_output(gpio_pins[i].gpio_no, 0);}
}static void tcar_forward(void)
{gpio_direction_output(gpio_pins[0].gpio_no,1);gpio_direction_output(gpio_pins[1].gpio_no,1);gpio_direction_output(gpio_pins[2].gpio_no,0);
}
static void tcar_backward(void)
{gpio_direction_output(gpio_pins[0].gpio_no,1);gpio_direction_output(gpio_pins[1].gpio_no,0);gpio_direction_output(gpio_pins[2].gpio_no,1);
}
#define TCAR_TIMER 10void tcar_timer_handler(unsigned long data)
{gpio_direction_output(gpio_pins[3].gpio_no,1);gpio_direction_output(gpio_pins[4].gpio_no,0);gpio_direction_output(gpio_pins[5].gpio_no,0);
}
static void tcar_left(void)
{gpio_direction_output(gpio_pins[3].gpio_no,1);gpio_direction_output(gpio_pins[4].gpio_no,1);gpio_direction_output(gpio_pins[5].gpio_no,0);tcar_timer.expires = jiffies + TCAR_TIMER;tcar_timer.function = tcar_timer_handler;tcar_timer.data = 0;add_timer(&tcar_timer);
}
static void tcar_right(void)
{gpio_direction_output(gpio_pins[3].gpio_no,1);gpio_direction_output(gpio_pins[4].gpio_no,0);gpio_direction_output(gpio_pins[5].gpio_no,1);tcar_timer.expires = jiffies + TCAR_TIMER;tcar_timer.function = tcar_timer_handler;tcar_timer.data = 0;add_timer(&tcar_timer);
}
static void tcar_stop(void)
{gpio_direction_output(gpio_pins[0].gpio_no,1);gpio_direction_output(gpio_pins[1].gpio_no,0);gpio_direction_output(gpio_pins[2].gpio_no,0);gpio_direction_output(gpio_pins[3].gpio_no,1);gpio_direction_output(gpio_pins[4].gpio_no,0);gpio_direction_output(gpio_pins[5].gpio_no,0);}
static int tcar_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long val)
{switch (cmd){case TCAR_FORWARD:tcar_forward();break;case TCAR_BACKWARD:tcar_backward();break;case TCAR_LEFT:tcar_left();break;case TCAR_RIGHT:tcar_right();break;case TCAR_STOP:tcar_stop();break;default: printk("invaild arg!\n");break;}return 0;
}static struct file_operations tcar_fops =
{.owner = THIS_MODULE,.ioctl = tcar_ioctl,
};
static struct miscdevice tcar_miscdev = 
{.minor = MISC_DYNAMIC_MINOR,.name = "tcar",.fops = &tcar_fops,
};
static int tcar_init(void)
{/*申请使用的管脚*/tcar_gpio_init();misc_register(&tcar_miscdev);/*初始化定时器*/init_timer(&tcar_timer);return 0;
}
static void tcar_exit(void)
{int i = 0;int pins_num = ARRAY_SIZE(gpio_pins);del_timer(&tcar_timer);misc_deregister(&tcar_miscdev);for(; i<pins_num; i++){gpio_free(gpio_pins[i].gpio_no);}
}
module_init(tcar_init);
module_exit(tcar_exit);
MODULE_LICENSE("GPL");
/* udp_client.c */
#include "tcar.h"
#include "motor_cmd.h"int main()
{int sd = socket(PF_INET, SOCK_DGRAM, 0);struct sockaddr_in addr;addr.sin_family = PF_INET;addr.sin_port  = htons(PORT);addr.sin_addr.s_addr = inet_addr("192.168.1.6");int cmd = MOTOR_FORWARD;sendto(sd, &cmd, sizeof(cmd), 0, (struct sockaddr *)&addr, sizeof(addr));getchar();cmd = MOTOR_STOP;sendto(sd, &cmd, sizeof(cmd), 0, (struct sockaddr *)&addr, sizeof(addr));getchar();cmd = MOTOR_BACKWARD;sendto(sd, &cmd, sizeof(cmd), 0, (struct sockaddr *)&addr, sizeof(addr));getchar();cmd = MOTOR_STOP;sendto(sd, &cmd, sizeof(cmd), 0, (struct sockaddr *)&addr, sizeof(addr));getchar();cmd = MOTOR_LEFT;sendto(sd, &cmd, sizeof(cmd), 0, (struct sockaddr *)&addr, sizeof(addr));getchar();cmd = MOTOR_RIGHT;sendto(sd, &cmd, sizeof(cmd), 0, (struct sockaddr *)&addr, sizeof(addr));getchar();close(sd);
}

/* udp_server.c */
#include "tcar.h"
#include "motor_cmd.h"int main()
{int fd  = 0;static int to_down_up = 1500;static int to_right_left = 1500;int unit = 200;int sd =  socket(PF_INET, SOCK_DGRAM, 0);struct sockaddr_in addr;addr.sin_family = PF_INET;addr.sin_port   = htons(PORT);addr.sin_addr.s_addr = inet_addr("192.168.1.6");bind(sd, (const struct sockaddr *)&addr, sizeof(addr));while(1){int cmd = 0;struct sockaddr_in fromaddr;int len = sizeof(fromaddr);recvfrom(sd, &cmd, sizeof(cmd), 0, (struct sockaddr *)&fromaddr, &len);switch(cmd){case VIDEO_UP:to_down_up += unit;if(to_down_up >2500){to_down_up = 2500;}fd = open("/dev/mg995", O_RDWR);ioctl(fd, IDEX0, to_down_up);close(fd);break;case VIDEO_DOWN:to_down_up -= unit;if(to_down_up < 500){to_down_up = 500;}fd = open("/dev/mg995", O_RDWR);ioctl(fd, IDEX0, to_down_up);close(fd);break;case VIDEO_LEFT:to_right_left += unit;if(to_right_left >2500){to_right_left = 2500;}fd = open("/dev/mg995", O_RDWR);ioctl(fd, IDEX1, to_right_left);close(fd);break;case VIDEO_RIGHT:to_right_left -= unit;if(to_right_left < 500){to_right_left = 500;}fd = open("/dev/mg995", O_RDWR);ioctl(fd, IDEX1, to_right_left);close(fd);break;case MOTOR_FORWARD:fd = open("/dev/tcar", O_RDWR);ioctl(fd, TCAR_FORWARD);close(fd);break;case MOTOR_BACKWARD:fd = open("/dev/tcar", O_RDWR);ioctl(fd, TCAR_BACKWARD);close(fd);break;case MOTOR_LEFT:fd = open("/dev/tcar", O_RDWR);ioctl(fd, TCAR_LEFT);close(fd);break;case MOTOR_RIGHT:fd = open("/dev/tcar", O_RDWR);ioctl(fd, TCAR_RIGHT);close(fd);break;case MOTOR_STOP:fd = open("/dev/tcar", O_RDWR);ioctl(fd, TCAR_STOP);close(fd);break;default:break;}}
}


这篇关于Tcar:智能车之基于L298N电机驱动芯片的驱动模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/541340

相关文章

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

Python3脚本实现Excel与TXT的智能转换

《Python3脚本实现Excel与TXT的智能转换》在数据处理的日常工作中,我们经常需要将Excel中的结构化数据转换为其他格式,本文将使用Python3实现Excel与TXT的智能转换,需要的可以... 目录场景应用:为什么需要这种转换技术解析:代码实现详解核心代码展示改进点说明实战演练:从Excel到

Python利用自带模块实现屏幕像素高效操作

《Python利用自带模块实现屏幕像素高效操作》这篇文章主要为大家详细介绍了Python如何利用自带模块实现屏幕像素高效操作,文中的示例代码讲解详,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、获取屏幕放缩比例2、获取屏幕指定坐标处像素颜色3、一个简单的使用案例4、总结1、获取屏幕放缩比例from

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

多模块的springboot项目发布指定模块的脚本方式

《多模块的springboot项目发布指定模块的脚本方式》该文章主要介绍了如何在多模块的SpringBoot项目中发布指定模块的脚本,作者原先的脚本会清理并编译所有模块,导致发布时间过长,通过简化脚本... 目录多模块的springboot项目发布指定模块的脚本1、不计成本地全部发布2、指定模块发布总结多模

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit

Python模块导入的几种方法实现

《Python模块导入的几种方法实现》本文主要介绍了Python模块导入的几种方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录一、什么是模块?二、模块导入的基本方法1. 使用import整个模块2.使用from ... i

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空