PCMaster2165
Member
Hello everyone, I have a pretty simple example of using the FlexCAN_T4, the only issue is I am missing CAN frames as it only will read 4 at a time and the rest go missing. I'm reading every 4 milliseconds, and receive 4-5 frames each time, it should be around 10-12 in my case. The only way I can achieve reading them all is reading every 1 millisecond which is strange as my buffer is large and this should be very easy.... any help would be truly appreciated thanks!
Here is my code:
Here is my code:
C++:
#ifndef CAN_DISPATCH_H
#define CAN_DISPATCH_H
#include "Structs.h"
#include "Telemetry.h"
#include <FlexCAN_T4.h>
#include "IMU.h"
#include "GPS.h"
#include "Altitude.h"
const byte LIDAR_CAN = 0x03;
const byte RTK_STATUS_CAN = 0x11;
const byte IMU_ANGLE_QUAT_CAN = 0x21;
const byte IMU_ANGULAR_RATE_CAN = 0x32;
const byte IMU_ACCELERATION_CAN = 0x34;
const byte TEMPERATURE_CAN = 0x51;
const byte GPS_POSITION_CAN = 0x71;
const byte ALTITUDE_CAN = 0x72;
const byte GPS_VELOCITY_CAN = 0x76;
const byte GPS_STATUS_CAN = 0x79;
class TELEMETRY;
class IMU;
class GPS;
class ALTITUDE;
class CANDISPATCH {
public:
CANDISPATCH(DRONE_SETTINGS drone_settings, TELEMETRY* telemetry);
void start();
void getCANData(IMU* imu, IMU_DATA* imu_data, SAFETY_DATA* safety_data, GPS* gps, GPS_DATA* gps_data, ALTITUDE* altitude, ALTITUDE_DATA* altitude_data);
private:
DRONE_SETTINGS drone_settings;
TELEMETRY* telemetry;
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can_interface; //RX can go to 512 or 1024 if needed
};
#endif
C++:
#include "CANDispatch.h"
//***PUBLIC***
CANDISPATCH::CANDISPATCH(DRONE_SETTINGS drone_settings, TELEMETRY* telemetry) {
this->drone_settings = drone_settings;
this->telemetry = telemetry;
}
void CANDISPATCH::start() {
can_interface.begin();
can_interface.setBaudRate(1000000);
}
void CANDISPATCH::getCANData(IMU* imu, IMU_DATA* imu_data, SAFETY_DATA* safety_data, GPS* gps, GPS_DATA* gps_data, ALTITUDE* altitude, ALTITUDE_DATA* altitude_data) {
CAN_message_t can_data;
while (can_interface.read(can_data)) {
switch (can_data.id) {
case IMU_ANGLE_QUAT_CAN:
case IMU_ANGULAR_RATE_CAN:
case IMU_ACCELERATION_CAN:
imu->getIMUDataFromCAN(imu_data, safety_data, can_data);
break;
case TEMPERATURE_CAN:
//TODO call Temperature Library
break;
case GPS_POSITION_CAN:
case GPS_VELOCITY_CAN:
case GPS_STATUS_CAN:
case RTK_STATUS_CAN:
gps->getGPSDataFromCAN(gps_data, safety_data, can_data);
break;
case ALTITUDE_CAN:
case LIDAR_CAN:
altitude->getAltitudeDataFromCAN(altitude_data, imu_data, safety_data, can_data);
break;
}
}
}
//***PRIVATE***