首页 > 其他分享 >【关节电机专栏】ESP32-TWAI-CAN库Readme文档

【关节电机专栏】ESP32-TWAI-CAN库Readme文档

时间:2025-01-08 23:32:59浏览次数:1  
标签:begin ESP32Can ESP32 TWAI obdFrame Readme data uint32

ESP32-TWAI-CAN

ESP32 driver library for TWAI / CAN for Adruino using ESP-IDF drivers.

Tested on ESP32 and ESP32-S3.

Usage

Library has everything inside it's header, just include that and then use ESP32Can object to send or receive CanFrame.

Here is simple example how to query and receive OBD2 PID frames:

#include <ESP32-TWAI-CAN.hpp>

// Default for ESP32
#define CAN_TX		5
#define CAN_RX		4


CanFrame rxFrame;

void sendObdFrame(uint8_t obdId) {
	CanFrame obdFrame = { 0 };
	obdFrame.identifier = 0x7DF; // Default OBD2 address;
	obdFrame.extd = 0;
	obdFrame.data_length_code = 8;
	obdFrame.data[0] = 2;
	obdFrame.data[1] = 1;
	obdFrame.data[2] = obdId;
	obdFrame.data[3] = 0xAA;    // Best to use 0xAA (0b10101010) instead of 0
	obdFrame.data[4] = 0xAA;    // CAN works better this way as it needs
	obdFrame.data[5] = 0xAA;    // to avoid bit-stuffing
	obdFrame.data[6] = 0xAA;
	obdFrame.data[7] = 0xAA;
    // Accepts both pointers and references 
    ESP32Can.writeFrame(obdFrame);  // timeout defaults to 1 ms
}

void setup() {
    // Setup serial for debbuging.
    Serial.begin(115200);

    // Set pins
	ESP32Can.setPins(CAN_TX, CAN_RX);
	
    // You can set custom size for the queues - those are default
    ESP32Can.setRxQueueSize(5);
	ESP32Can.setTxQueueSize(5);

    // .setSpeed() and .begin() functions require to use TwaiSpeed enum,
    // but you can easily convert it from numerical value using .convertSpeed()
    ESP32Can.setSpeed(ESP32Can.convertSpeed(500));

    // You can also just use .begin()..
    if(ESP32Can.begin()) {
        Serial.println("CAN bus started!");
    } else {
        Serial.println("CAN bus failed!");
    }

    // or override everything in one command;
    // It is also safe to use .begin() without .end() as it calls it internally
    if(ESP32Can.begin(ESP32Can.convertSpeed(500), CAN_TX, CAN_RX, 10, 10)) {
        Serial.println("CAN bus started!");
    } else {
        Serial.println("CAN bus failed!");
    }
}

void loop() {
    static uint32_t lastStamp = 0;
    uint32_t currentStamp = millis();
    
    if(currentStamp - lastStamp > 1000) {   // sends OBD2 request every second
        lastStamp = currentStamp;
        sendObdFrame(5); // For coolant temperature
    }

    // You can set custom timeout, default is 1000
    if(ESP32Can.readFrame(rxFrame, 1000)) {
        // Comment out if too many requests 
        Serial.printf("Received frame: %03X \r\n", rxFrame.identifier);
        if(rxFrame.identifier == 0x7E8) {   // Standard OBD2 frame responce ID
            Serial.printf("Collant temp: %3d°C \r\n", rxFrame.data[3] - 40); // Convert to °C
        }
    }
}

Advanced

You can also setup your own masks and configurations:

// Everything is defaulted so you can just call .begin() or .begin(TwaiSpeed)
// Calling begin() to change speed works, it will disable current driver first
bool begin(TwaiSpeed twaiSpeed = TWAI_SPEED_500KBPS, 
                int8_t txPin = -1, int8_t rxPin = -1,
                uint16_t txQueue = 0xFFFF, uint16_t rxQueue = 0xFFFF,
                twai_filter_config_t*  fConfig = nullptr,
                twai_general_config_t* gConfig = nullptr,
                twai_timing_config_t*  tConfig = nullptr);

Follow soc/twai_types.h for more info:

typedef struct {
    union {
        struct {
            //The order of these bits must match deprecated message flags for compatibility reasons
            uint32_t extd: 1;           /**< Extended Frame Format (29bit ID) */
            uint32_t rtr: 1;            /**< Message is a Remote Frame */
            uint32_t ss: 1;             /**< Transmit as a Single Shot Transmission. Unused for received. */
            uint32_t self: 1;           /**< Transmit as a Self Reception Request. Unused for received. */
            uint32_t dlc_non_comp: 1;   /**< Message's Data length code is larger than 8. This will break compliance with ISO 11898-1 */
            uint32_t reserved: 27;      /**< Reserved bits */
        };
        //Todo: Deprecate flags
        uint32_t flags;                 /**< Deprecated: Alternate way to set bits using message flags */
    };
    uint32_t identifier;                /**< 11 or 29 bit identifier */
    uint8_t data_length_code;           /**< Data length code */
    uint8_t data[TWAI_FRAME_MAX_DLC];    /**< Data bytes (not relevant in RTR frame) */
} twai_message_t;

/**
 * @brief   Structure for bit timing configuration of the TWAI driver
 *
 * @note    Macro initializers are available for this structure
 */
typedef struct {
    uint32_t brp;                   /**< Baudrate prescaler (i.e., APB clock divider). Any even number from 2 to 128 for ESP32, 2 to 32768 for ESP32S2.
                                         For ESP32 Rev 2 or later, multiples of 4 from 132 to 256 are also supported */
    uint8_t tseg_1;                 /**< Timing segment 1 (Number of time quanta, between 1 to 16) */
    uint8_t tseg_2;                 /**< Timing segment 2 (Number of time quanta, 1 to 8) */
    uint8_t sjw;                    /**< Synchronization Jump Width (Max time quanta jump for synchronize from 1 to 4) */
    bool triple_sampling;           /**< Enables triple sampling when the TWAI controller samples a bit */
} twai_timing_config_t;

/**
 * @brief   Structure for acceptance filter configuration of the TWAI driver (see documentation)
 *
 * @note    Macro initializers are available for this structure
 */
typedef struct {
    uint32_t acceptance_code;       /**< 32-bit acceptance code */
    uint32_t acceptance_mask;       /**< 32-bit acceptance mask */
    bool single_filter;             /**< Use Single Filter Mode (see documentation) */
} twai_filter_config_t;

标签:begin,ESP32Can,ESP32,TWAI,obdFrame,Readme,data,uint32
From: https://www.cnblogs.com/FBsharl/p/18660779

相关文章

  • ESP32-S3模组上实现低功耗(6)
    接前一篇文章:ESP32-S3模组上实现低功耗(5) 本文内容参考:系统低功耗模式介绍-ESP32-S3-—ESP-IDF编程指南latest文档电源管理-ESP32-S3-—ESP-IDF编程指南latest文档......
  • ESP32作为BLE客户端gatt-client教程
    一、介绍该代码实现了一个低功耗蓝牙(BLE)通用属性(GATT)客户端,该客户端扫描附近的外围服务器并连接到预定义的服务。然后,客户端搜索可用特征并订阅已知特征,以便接收通知或指示。该示例可以注册应用程序配置文件并初始化一系列事件,这些事件可用于配置通用访问配置文件(GAP)参......
  • ESP32 的蓝牙协议栈
    ESP32的蓝牙协议栈一、蓝牙协议栈BLUEDROID架构在ESP-IDF中,使⽤经过⼤量修改后的BLUEDROID作为蓝⽛主机(ClassicBT+ BluetoothLE)。BLUEDROID拥有较为完善的功能,⽀持常⽤的规范和架构设计,同时也较为复杂。经过⼤量修改后,BLUEDROID保留了⼤多数BTA层以下......
  • 基于ESP32的桌面小屏幕实战[5]:PCB下单
    1.焊接调试前准备PCB下单点击“PCB下单”检查一下DRC确认无错误之后,确认下单然后就会跳转到下面的网页基本上保持默认选项即可。可以看到“焊盘喷镀”有3个选项。在选择表面处理工艺时,应综合考虑产品的具体需求、环保法规以及成本等因素。例如,对于环保要求较高的消费......
  • 孕妇地区推荐菜品助手(appinventor+掌控板esp32+su-03t语音模块)
    一、系统概述该系统主要由APP端和掌控板两部分组成,并通过连接多个硬件设备实现功能。系统利用云计算和物联网技术,能够实现用户与餐饮设备之间的高效交互。系统功能地区选择与菜品推荐用户可以通过APP选择所在地区,系统会根据所选地区在掌控板上显示当地的菜品信息。当......
  • ESP32-S3-N16R8在platformio中的开发板设置
    前言platformio现有的板子库里面没有ESP32-S3-N16R8(8MBPSRAM+16MBFLASH)的开发板模型,直接强行套用,要么就是解锁不了8MBPSRAM,要么就下载后运行不起来。 一、选用esp32-s3-devkitc-1开发板先选用esp32-s3-devkitc-1作为开发板模型,点击Finish后务必耐心等待。二、下载完毕......
  • ESP32物联网无线方案,智能穿戴设备联网通信,产品无线交互应用
    在物联网的世界里,每一个设备都不再是孤立的个体,它们通过无线连接芯片相互连接,形成一个庞大的智能网络。这些芯片是实现万物互联的基础,它们使得设备能够相互沟通,共享数据,从而创造出无限的可能性。这些芯片的设计理念是简单而强大的。它们不仅要支持多种无线协议,以适应不同的应......
  • C# 使用TWAIN协议进行扫描仪操作
    C#使用TWAIN协议进行扫描仪操作 折腾两台扫描仪程序的修改,今天有空把程序中涉及到扫描仪操作的代码贴出来,供大家一起交流学习。1publicArrayListTransferPictures()2{3ArrayListpics=newArrayList();4if(srcds.Id==......
  • ESP32使用protobuf
    非原创,引用自:https://techtutorialsx.com/2018/10/19/esp32-esp8266-arduino-protocol-buffers/对上述英文技术博客做了总结:1.先去这个网站下载最新的Nanopb包   https://jpa.kapsi.fi/nanopb/download/   2.解压,解压之后如下图 3.在Arduino的libraries目录下,C......
  • esp32+thonny烧入micropython固件和一般程序烧录步骤
    一、烧入固件:可以及时运行程序,方便学习编程1、点击右下方弹出“配置解释器” 2、选择“安装或更新MicroPython 3、选择相应的端口和固件,烧入是按住”boot"键 4、py文件可以及时运行,保存在本地电脑,不能脱离thonny工作 二、烧入写好的程序,相对固定板子的用途。今后......