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

相关文章

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

Python实现word文档内容智能提取以及合成

《Python实现word文档内容智能提取以及合成》这篇文章主要为大家详细介绍了如何使用Python实现从10个左右的docx文档中抽取内容,再调整语言风格后生成新的文档,感兴趣的小伙伴可以了解一下... 目录核心思路技术路径实现步骤阶段一:准备工作阶段二:内容提取 (python 脚本)阶段三:语言风格调

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详