Skip to content

ExceptionQWQ/SerialPort_TO_CAN

Repository files navigation

高速串口TTL转CAN

TTL串口波特率 921600
数据位 8位
停止位 1位
校验位

模块上4个拨码开关说明:

SW1 帧格式
ON 标准帧
OFF 扩展帧
SW2 SW3 CAN波特率
ON ON 1M
OFF ON 500K
ON OFF 250K
OFF OFF 100K
SW4 终端电阻
ON 开启
OFF 关闭

##参考文档

首先需要安装 https://github.com/ExceptionQWQ/RobotDevKit

enum class IDType:uint8_t
{
    STD = 0x01, //标准帧
    EXT = 0x02, //扩展帧
};

enum class FrameType:uint8_t
{
    Data = 0x01, //数据帧
    Remote = 0x02, //远程帧
};

struct FrameData
{
    uint8_t d0;
    uint8_t d1;
    uint8_t d2;
    uint8_t d3;
    uint8_t d4;
    uint8_t d5;
    uint8_t d6;
    uint8_t d7;
    uint8_t d8; //扩展帧使用
    uint8_t d9; //扩展帧使用
};

/*
 * @brief 串口转CAN构造函数
 * @param serial_port 串口指针
 * @param id_type 帧类型
 */
SerialToCan::SerialToCan(std::shared_ptr<SerialPort> serial_port, IDType id_type);
/*
 * @brief 异步写入帧数据
 * @param id 帧id
 * @param data 数据帧
 * @param result_handler 写入完成回调函数
 */
std::size_t async_write_frame(uint32_t id, FrameData data, ResultHandler result_handler);
/*
 * @brief 异步读取帧数据
 * @param id 帧id
 * @param data 数据帧
 * @param result_handler 读取完成回调函数
 */
std::size_t async_read_frame(uint32_t* id, FrameData* data, ResultHandler result_handler);

/*
 * @brief 写入帧数据
 * @param id 帧id
 * @param data 数据帧
 */
std::size_t write_frame(uint32_t id, FrameData data);
/*
 * @brief 读取帧数据
 * @param id 帧id
 * @param data 数据帧
 */
std::size_t read_frame(uint32_t* id, FrameData* data);

/*
 * @brief 写入帧数据
 * @param id 帧id
 * @param data 数据帧
 * @param timeout 超时时间
 */
std::size_t write_frame(uint32_t id, FrameData data, int timeout);

/*
 * @brief 读取帧数据
 * @param id 帧id
 * @param data 数据帧
 * @param timeout 超时时间
 */
std::size_t read_frame(uint32_t* id, FrameData* data, int timeout);

例程

Linux平台控制大疆M3508电机例程

/*
 * @author BusyBox
 * @date 2024/4/29
 * @version 1.0
 * @git https://github.com/ExceptionQWQ/RobotDevKit
 */

#include <iostream>
#include <cstring>
#include <memory>
#include "rdk/core.h"

int main()
{
    auto serial = std::make_shared<SerialPort>("/dev/serial/by-path/platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.1:1.0-port0", 921600);
    auto can = std::make_shared<SerialToCan>(serial, CanBus::IDType::STD);
    auto c6xx_controller = std::make_shared<C6xxController>(can);
    auto m3508_motor = std::make_shared<M3508Motor>(c6xx_controller, 1, DjiMotor::Mode::SPEED_POS); //创建id为1,模式为速度位置模式的电机对象

    m3508_motor->set_pos_pid(0.06, 0, 0.06); //设置位置式PID参数

    double angle = 0;
    int cnt = 0;
    while (true) {
        ++cnt;
    
        if (cnt % 800 == 0) {
            angle += 90;
        }
        m3508_motor->set_target_pos(angle);
        
        m3508_motor->tick();
        c6xx_controller->tick();
        
        std::cout << m3508_motor->get_pos() << std::endl;

        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }

    return 0;
}

Linux平台控制达妙电机例程

/*
 * @author BusyBox
 * @date 2024/4/29
 * @version 1.0
 * @git https://github.com/ExceptionQWQ/RobotDevKit
 */

#include <iostream>
#include <cstring>
#include <memory>
#include "rdk/core.h"

int main()
{
    auto serial = std::make_shared<SerialPort>("/dev/serial/by-path/platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.1:1.0-port0", 921600);
    auto can = std::make_shared<SerialToCan>(serial, CanBus::IDType::STD);
    auto damiao_controller = std::make_shared<DamiaoController>(can, DamiaoController::Mode::PosSpeed);

    damiao_controller->enable(1);  //电机使能

    while (true) {
        damiao_controller->write_pos_speed(1, 2 * 3.14, 9.14); //旋转一圈
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        damiao_controller->write_pos_speed(1, 0, 9.14); //回到零点
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }

    return 0;
}

About

高速串口TTL转CAN

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published