Hi,
I am using libcanard library and I just want tor subscribe and receive data from the remote node.
So first, I have initiated the instance:
static O1HeapInstance* heap_;
static void* memAllocate(CanardInstance* const ins, const size_t amount)
{
(void) ins;
return o1heapAllocate(heap_, amount);
}
static void memFree(CanardInstance* const ins, void* const pointer)
{
(void) ins;
o1heapFree(heap_, pointer);
}
inline auto allocate(CanardInstance* const ins, const std::size_t amount) -> void*
{
(void) ins;
(void) amount;
return nullptr;
}
inline void free(CanardInstance* const ins, void* const pointer)
{
(void) ins;
(void) pointer;
}
and Instance is a member of my class which is initialized during the constructor call:
ins_ = canardInit(&memAllocate, &memFree);
ins_.mtu_bytes = CANARD_MTU_CAN_CLASSIC;
ins_.node_id = 10;
After initializing my instance, I made a subscription:
(void) canardRxSubscribe(&ins_, CanardTransferKindMessage, 1234, 500, CANARD_DEFAULT_TRANSFER_ID_TIMEOUT_USEC, &radar1_subscription_);
And then, in my main loop function I just read the CAN interface to populate my CanardFrame
and then using canardRxAccept
:
CanardTransfer transfer;
CanardFrame frame{};
struct canfd_frame cfd;
poll_int = poll(&poll_, 1, 100);
if (poll_int <= 0) continue;
struct timespec ts;
clock_gettime(CLOCK_TAI, &ts);
uint8_t payload_buffer[500]{};
const ssize_t read_size = read(soc_, &cfd, sizeof(struct can_frame));
(void) memset(&frame, 0, sizeof(CanardFrame));
frame.timestamp_usec = (CanardMicrosecond)((ts.tv_sec * MEGA) + (ts.tv_nsec / KILO));
frame.extended_can_id = cfd.can_id & CAN_EFF_MASK;
frame.payload_size = cfd.len;
frame.payload = payload_buffer;
(void) memcpy(payload_buffer, &cfd.data[0], cfd.len);
ROS_INFO("The Extended CAN ID: %d, payload size from CAN: %lu", frame.extended_can_id, frame.payload_size);
const int8_t result = canardRxAccept(&ins_, &frame, 0, &transfer);
ROS_INFO("Port ID: %d, remote_id: %d, the payload size: %lu", transfer.port_id, transfer.remote_node_id, transfer.payload_size);
However, I do not see the correct Port ID nor correct remote id and some garbage payload size and eventually the program crashes due to heap assertion:
[ INFO] [1597998075.124745439]: The Extended CAN ID: 268751402, payload size from CAN: 8
[ INFO] [1597998075.124920397]: Port ID: 58992, remote_id: 58, the payload size: 187650331065304
can_server1: /home/master-wheel/ros_ws/src/can_server/include/can_server/o1heap.c:293: o1heapAllocate: Assertion `handle != ((void *)0)' failed.
What am I doing wrong?