openarm_ros2/openarm_hardware/include/openarm_hardware/canbus.hpp
toki d6cc80c17e
fix: compatible with canfd (#7)
## Summary

This PR updates the C++ hardware interface to support **CANFD** and confirms successful operation with **MoveIt** on the actual robot hardware.

---

##  Changes Made

- Migrated from classic CAN to **CANFD** in the motor control interface.
- Updated the following modules:
  - `openarm_hardware/include/openarm_hardware/canbus.hpp`
  - `openarm_hardware/src/canbus.cpp`
  - `openarm_hardware/src/motor_control.cpp`
  - `openarm_hardware/include/openarm_hardware/motor_control.hpp`
  - `openarm_hardware/src/openarm_hardware.cpp`
- Improved CAN socket handling and data frame structure.
- Refactored internal motor communication logic for robustness.

---

##  Verification

-  Confirmed joint control and motion execution with real hardware using **MoveIt**.
-  Controllers were properly loaded and executed planned trajectories without error.
-  CANFD communication is now functional and stable during runtime.
2025-05-19 11:32:09 +09:00

39 lines
824 B
C++

#pragma once
#include <cstdint>
#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>
#include <string>
enum CANMode {
CAN_MODE_CLASSIC = 0,
CAN_MODE_FD = 1
};
class CANBus {
public:
explicit CANBus(const std::string& interface, int mode);
~CANBus();
int whichCAN();
bool send(uint16_t motor_id, const std::array<uint8_t, 8>& data);
std::array<uint8_t, 64> recv(uint16_t& out_id, uint8_t& out_len);
private:
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();
int sock_;
int mode_;
};