Writing to CAN bus at 1kHz

Hi guys, I’m trying to send at 1Khz at least a can frame of 8 bytes. My code is written in C, the problem occurs when I try to call the write function. It appears that 1ms between each call is not enough to successfully write to socket CAN. I tried to increase the txqueuelen to 1000 from the default 10. It appears to work but the whole system is delayed anyways by the time between each write is actually sent… There are some kind of limitations? Or there are any configuration for increasing the ‘clock frequency’ I suppose?

This is how I open the socket in C:

    nanotec->socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if (nanotec->socket < 0) {
        logger_error("nanotec", "Error creating socket");
        return -1;
    }

    struct sockaddr_can address;
    struct ifreq ifr;
    strcpy(ifr.ifr_name, can_port);
    ioctl(nanotec->socket, SIOCGIFINDEX, &ifr);
    address.can_family = AF_CAN;
    address.can_ifindex = ifr.ifr_ifindex;

    if (bind(nanotec->socket, (struct sockaddr *) &address, sizeof(address)) < 0) {
        logger_error("nanotec", "Error binding socket to %s", can_port);
        return -1;
    }

This is how I send the messagges:

int nanotec_set_state(t_nanotec *nanotec, motors_state desired_state) {

    nanotec->tx_frame.len = 8;
    nanotec->tx_frame.data[0] = 0x23; //write 4 bytes
    *((uint16_t *) (nanotec->tx_frame.data + 1)) = 0x2400; //custom object for position control
    nanotec->tx_frame.data[3] = 0x01; //sub index for custom object

    for (int i = 0; i < 3; ++i) {

        nanotec->tx_frame.can_id = 0x600 | (i + 1);

        switch (i) {
            case 0:
                *((int32_t *) (nanotec->tx_frame.data + 4)) = desired_state.yaw;
                break;
            case 2:
                *((int32_t *) (nanotec->tx_frame.data + 4)) = desired_state.roll;
                break;
            case 1:
                *((int32_t *) (nanotec->tx_frame.data + 4)) = desired_state.pitch;
                break;
            default:
                break;
        }

        ssize_t nbytes = write(nanotec->socket, &nanotec->tx_frame, sizeof(struct can_frame));
    }

    return 0;
}

Thanks

Hi @raccoman !

Thanks for using Toradex Community! :smiley:

I just sent you a private message.

Could you please check it?

After going through that message, we will be happy to continue supporting you :slight_smile:

Best regards,

Here is the current interface comfiguration:

Hi @raccoman !

Could you share more details about how you are physically connecting the devices?

Are you using the termination resistor? This is very important for the high bitrate you are using.

Also, what is the length of the CAN cable you are using?

Please also share the output from tdx-info (reference: Getting Device Information with Tdx-Info | Toradex Developer Center).

Which carrier board are you using?

Best regards,