2025-05-19 02:32:09 +00:00
|
|
|
#pragma once
|
2025-03-28 09:05:38 +00:00
|
|
|
|
2025-05-19 02:32:09 +00:00
|
|
|
#include <cstdint>
|
2025-03-28 09:05:38 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <net/if.h>
|
|
|
|
|
#include <linux/can.h>
|
|
|
|
|
#include <linux/can/raw.h>
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <fcntl.h>
|
2025-05-19 02:32:09 +00:00
|
|
|
#include <string>
|
2025-03-28 09:05:38 +00:00
|
|
|
|
2025-05-19 02:32:09 +00:00
|
|
|
enum CANMode {
|
|
|
|
|
CAN_MODE_CLASSIC = 0,
|
|
|
|
|
CAN_MODE_FD = 1
|
|
|
|
|
};
|
2025-03-28 09:05:38 +00:00
|
|
|
class CANBus {
|
|
|
|
|
public:
|
2025-05-19 02:32:09 +00:00
|
|
|
explicit CANBus(const std::string& interface, int mode);
|
2025-03-28 09:05:38 +00:00
|
|
|
~CANBus();
|
2025-05-19 02:32:09 +00:00
|
|
|
int whichCAN();
|
2025-03-28 09:05:38 +00:00
|
|
|
bool send(uint16_t motor_id, const std::array<uint8_t, 8>& data);
|
2025-05-19 02:32:09 +00:00
|
|
|
std::array<uint8_t, 64> recv(uint16_t& out_id, uint8_t& out_len);
|
2025-03-28 09:05:38 +00:00
|
|
|
|
|
|
|
|
private:
|
2025-05-19 02:32:09 +00:00
|
|
|
bool sendClassic(uint16_t motor_id, const std::array<uint8_t, 8>& data);
|
|
|
|
|
bool sendFD(uint16_t motor_id, const std::array<uint8_t, 8>& data);
|
|
|
|
|
|
|
|
|
|
struct can_frame recvClassic();
|
|
|
|
|
struct canfd_frame recvFD();
|
|
|
|
|
|
2025-03-28 09:05:38 +00:00
|
|
|
int sock_;
|
2025-05-19 02:32:09 +00:00
|
|
|
int mode_;
|
2025-03-28 09:05:38 +00:00
|
|
|
};
|
|
|
|
|
|
2025-05-19 02:32:09 +00:00
|
|
|
|