2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 树莓派 3B+ 使用微雪2-CH CAN FD HAT

树莓派 3B+ 使用微雪2-CH CAN FD HAT

时间:2019-02-28 19:19:01

相关推荐

树莓派 3B+ 使用微雪2-CH CAN FD HAT

准备

目标

疫情在家,想着用树莓派3B+,通过CAN卡接收CAN数据,外接触摸屏,实时显示CAN数据。计划使用python作为开发语言,用pyqt库界面开发,can卡尽量选择低成本解决方案。

kvaser和pcan是比较常用的can接口设备,也自带了免费的上层协议lib,比如ccp,obdII等,但是最便宜的正品也要2000多。

因为应用比较简单,只要能实时接收数据就能满足要求,因此选择了微雪的2通道CAN FD扩展板,其实也没找到其它基于树莓派的CAN扩展板。

今天下午板子顺丰快递到手,试用过程记录如下。

树莓派

树莓派 3B+,安装有 buster 系统,

已经有ssh,vnc viewer

微雪双通道CAN卡

型号:2-CH CAN FD HAT,官方淘宝店购买

产品说明在中写的很详细,

/wiki/2-CH_CAN_FD_HAT

包括电路原理图,CAN芯片和收发器的产品手册等。

安装过程

微雪网站上有比较详细的说明,完全按步骤操作下来,看看是否有问题:

安装BCM2835

C library for Broadcom BCM 2835 as used in Raspberry Pi

This is a C library for Raspberry Pi (RPi). It provides access to GPIO and other IO functions on the Broadcom BCM 2835 chip, as used in the RaspberryPi, allowing access to the GPIO pins on the 26 pin IDE plug on the RPi board so you can control and interface with various external devices.

最新版本是1.62,将微雪说明中的1.60改为最新的1.62版

wget /mikem/bcm2835/bcm2835-1.62.tar.gztar zxvf bcm2835-1.62.tar.gz cd bcm2835-1.60/sudo ./configuresudo makesudo make checksudo make install

顺利完成

安装wiringPi

官方步骤

sudo apt-get install wiringpi#对于树莓派4B可能需要进行升级:cd bcm2835-1.60/wget https://project-/wiringpi-latest.debsudo dpkg -i wiringpi-latest.debgpio -v# 运行gpio -v会出现2.52版本,如果没有出现说明安装出错

我的是3B+,只执行了sudo apt-get install wiringpi,运行gpio -v显示版本 2.50

pi@raspberrypi:~ $ gpio -vgpio version: 2.50Copyright (c) - Gordon HendersonThis is free software with ABSOLUTELY NO WARRANTY.For details type: gpio -warrantyRaspberry Pi Details:Type: Pi 3B+, Revision: 03, Memory: 1024MB, Maker: Sony* Device tree is enabled.*--> Raspberry Pi 3 Model B Plus Rev 1.3* This Raspberry Pi supports user-level GPIO access.

完成此步骤

安装Python函数库

我学的是 python3,所以只安装对应的函数库

#python3sudo apt-get updatesudo apt-get install python3-pipsudo apt-get install python3-pilsudo apt-get install python3-numpysudo pip3 install RPi.GPIOsudo pip3 install spidev sudo pip3 install python-can

实际操作下来,只有最后一个函数库 python-can需要下载安装,其余的都是最新的了。

此步骤完成。

下载软件示例代码

微雪已经提供了软件示例 2-CH-CAN-FD-HAT-Demo.7z,在win10系统下下载,解压后有两个文件夹对应Arduino 和 Raspberry Pi(注意:为了在Pi中的终端操作,将目录中的空格都去掉)。然后将 RaspberryPi文件夹复制到树莓派中。

安装例程依赖的Linux 内核驱动

Raspberry Pi 文件夹下有“Linuxdriver” 文件夹,其中包含ModeA和ModeB两种模式,官方说明 “出厂默认A模式,可通过更改电阻来实现B模式,A模式中两路CAN分别使用两组独立的SPI,B模式则是两路CAN公用一组SPI”。

没有仔细看怎么更改电阻,应该需要重新焊接电阻,而且A模式明显效率更好,所以沿用A模式进行测试。

进入Linuxdriver 的文件夹,获取Amode文件夹权限,之后运行install.sh

sudo chmod -R 777 Amode/cd Amode/sudo ./install.sh

这步安装时间很长,其中将内核做了降级:

downgrading raspberrypi-kernel-headers from 1.0205-1 to 1.0925+1-1

在install.sh 中一开始就强制了内核版本FORCE_KERNEL=“1.0925+1-1”

不知道是否一定要用这个版本,还是只因为微雪的说明用了当时的最新版本,后面没做更新?!

我使用的版本:1.0205-1 ,测试之后再试试将 install.sh 改回到这个版本看看。

安装成功后,重启树莓派,并查看是否成功

DKMS: install completed.------------------------------------------------------Installation completed!You need to reboot your raspberry Pi to apply setting.If you need to uninstall the module, execute the following commandssudo ./uninstall.shThanks!------------------------------------------------------

重启树莓派以应用所有设置:

sudo reboot

待树莓派重启后,查看SPI信息:

dmesg | grep spi

和官方文档结果一致

pi@raspberrypi:~ $ dmesg | grep spi[ 4.701856] mcp25xxfd spi0.0: MCP2517 successfully initialized.[ 4.821127] mcp25xxfd spi1.0: MCP2517 successfully initialized.

测试

将CAN0 和CAN1 的 CAN H 和 CAN L两两相连,CAN转接板上默认跳线设置都已经配置成了有120欧电阻。

之后完全根据官方说明配置CAN,设置波特率,工作模式,是否开启FD及配置传输缓冲区大小:

sudo ip link set can0 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd onsudo ip link set can1 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd onsudo ifconfig can0 txqueuelen 65536sudo ifconfig can1 txqueuelen 65536

之后在树莓派中开启两个终端,分别作为输入和输出设备,可以正常发送和接收数据。官方给的说明是配置成 CAN FD格式,根据CAN FD发送格式测试。上面是 can1 发送,下面是can 0接收

如果格式不对,会有提示信息,稍微看看就知道该如何使用了。

小结

微雪的官方说明文档很完整,整个过程没有遇到什么问题,好评!

接下来进一步研究给的python例程,准备后面的具体开发工作。

应用实例

使用自带的程序进行测试

代码很简单,分为send.py 和 receive.py,分别在两个终端下运行。 在运行receive.py的10s内,运行send.pyj就可成功发送和显示接收内容了。

但是运行send.py后会报错RTNETLINK answers: Device or resource busy

pi@raspberrypi:~ $ sudo python3.7 ~/WaveShare/RaspberryPi/Python/send.pyRTNETLINK answers: Device or resource busy

解决办法很简单,在程序开始先关闭can1接口,增加命令os.system('sudo ifconfig can1 down')

另外最后一句os.system('sudo ifconfig can0 down'),应该改为can1;

更改后完整的send.py为

import osimport canos.system('sudo ifconfig can1 down')os.system('sudo ip link set can1 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on')can0 = can.interface.Bus(channel = 'can1', bustype = 'socketcan_ctypes')# socketcan_nativemsg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)can0.send(msg)os.system('sudo ifconfig can1 down')

receive.py的内容中有一句 print msg 要改为 pirnt(msg)

msg = can0.recv(10.0):打开接口10s,接收msg。

通过 wiringpi,wiringpi2 实现精确定时

can收发需要做精确的定时周期发送,可以使用 wiringpi 和 wiringpi2 中的函数,主要参考其他博客:

/qq_38880380/article/details/80318331

/yafengabc/p/5096300.html

官方项目地址:

/project/wiringpi/

/project/wiringpi2/

安装时需要注意,如果是为python3安装,需要用pip3命令(pip 默认为python2.x版本),而且两个函数库都要装(wiringpi和wiringpi2):

pip3 install wiringpipip3 install wiringpi2

之后就可以用内部的毫秒,微妙定时了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。