关于嵌入式学习随笔->14《STM32CubeMX应用5-CAN总线的配置》

STM32CubeMX-CAN总线的配置

关于嵌入式学习随笔->14《STM32CubeMX应用5-CAN总线的配置》_第1张图片

配置好后生成代码

关于HAL库中相关文件代码

1、HAL库滤波器设置c文件

#include "bsp_can.h"
#include "main.h"


extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;

void can_filter_init(void)
{

    CAN_FilterTypeDef can_filter_st;
    can_filter_st.FilterActivation = ENABLE;
    can_filter_st.FilterMode = CAN_FILTERMODE_IDMASK;
    can_filter_st.FilterScale = CAN_FILTERSCALE_32BIT;
    can_filter_st.FilterIdHigh = 0x0000;
    can_filter_st.FilterIdLow = 0x0000;
    can_filter_st.FilterMaskIdHigh = 0x0000;
    can_filter_st.FilterMaskIdLow = 0x0000;
    can_filter_st.FilterBank = 0;
    can_filter_st.FilterFIFOAssignment = CAN_RX_FIFO0;
    HAL_CAN_ConfigFilter(&hcan1, &can_filter_st);
    HAL_CAN_Start(&hcan1);
    HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);


    can_filter_st.SlaveStartFilterBank = 14;
    can_filter_st.FilterBank = 14;
    HAL_CAN_ConfigFilter(&hcan2, &can_filter_st);
    HAL_CAN_Start(&hcan2);
    HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);

}
bsp_can.c

2、HAL库滤波器h文件

#ifndef BSP_CAN_H
#define BSP_CAN_H
#include "main.h"


extern void can_filter_init(void);

#endif
bsp_can.h

3、HAL库can接收发送c文件

/**
  ****************************(C) COPYRIGHT 2019 DJI****************************
  * @file       can_receive.c/h
  * @brief      there is CAN interrupt function  to receive motor data,
  *             and CAN send function to send motor current to control motor.
  *             ÕâÀïÊÇCANÖжϽÓÊÕº¯Êý£¬½ÓÊÕµç»úÊý¾Ý,CAN·¢Ëͺ¯Êý·¢Ë͵ç»úµçÁ÷¿ØÖƵç»ú.
  * @note
  * @history
  *  Version    Date            Author          Modification
  *  V1.0.0     Dec-26-2018     RM              1. done
  *
  @verbatim
  ==============================================================================

  ==============================================================================
  @endverbatim
  ****************************(C) COPYRIGHT 2019 DJI****************************
  */

#include "CAN_receive.h"
#include "main.h"



extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
//motor data read
#define get_motor_measure(ptr, data)                                    \
    {                                                                   \
        (ptr)->last_ecd = (ptr)->ecd;                                   \
        (ptr)->ecd = (uint16_t)((data)[0] << 8 | (data)[1]);            \
        (ptr)->speed_rpm = (uint16_t)((data)[2] << 8 | (data)[3]);      \
        (ptr)->given_current = (uint16_t)((data)[4] << 8 | (data)[5]);  \
        (ptr)->temperate = (data)[6];                                   \
    }
/*
motor data,  0:chassis motor1 3508;1:chassis motor3 3508;2:chassis motor3 3508;3:chassis motor4 3508;
4:yaw gimbal motor 6020;5:pitch gimbal motor 6020;6:trigger motor 2006;
µç»úÊý¾Ý, 0:µ×Å̵ç»ú1 3508µç»ú,  1:µ×Å̵ç»ú2 3508µç»ú,2:µ×Å̵ç»ú3 3508µç»ú,3:µ×Å̵ç»ú4 3508µç»ú;
4:yawÔÆ̨µç»ú 6020µç»ú; 5:pitchÔÆ̨µç»ú 6020µç»ú; 6:²¦µ¯µç»ú 2006µç»ú*/
static motor_measure_t motor_chassis[7];

static CAN_TxHeaderTypeDef  gimbal_tx_message;
static uint8_t              gimbal_can_send_data[8];
static CAN_TxHeaderTypeDef  chassis_tx_message;
static uint8_t              chassis_can_send_data[8];

/**
  * @brief          hal CAN fifo call back, receive motor data
  * @param[in]      hcan, the point to CAN handle
  * @retval         none
  */
/**
  * @brief          hal¿âCAN»Øµ÷º¯Êý,½ÓÊÕµç»úÊý¾Ý
  * @param[in]      hcan:CAN¾ä±úÖ¸Õë
  * @retval         none
  */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
    CAN_RxHeaderTypeDef rx_header;
    uint8_t rx_data[8];

    HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, rx_data);

    switch (rx_header.StdId)
    {
        case CAN_3508_M1_ID:
        case CAN_3508_M2_ID:
        case CAN_3508_M3_ID:
        case CAN_3508_M4_ID:
        case CAN_YAW_MOTOR_ID:
        case CAN_PIT_MOTOR_ID:
        case CAN_TRIGGER_MOTOR_ID:
        {
            static uint8_t i = 0;
            //get motor id
            i = rx_header.StdId - CAN_3508_M1_ID;
            get_motor_measure(&motor_chassis[i], rx_data);
            break;
        }

        default:
        {
            break;
        }
    }
}



/**
  * @brief          send control current of motor (0x205, 0x206, 0x207, 0x208)
  * @param[in]      yaw: (0x205) 6020 motor control current, range [-30000,30000]
  * @param[in]      pitch: (0x206) 6020 motor control current, range [-30000,30000]
  * @param[in]      shoot: (0x207) 2006 motor control current, range [-10000,10000]
  * @param[in]      rev: (0x208) reserve motor control current
  * @retval         none
  */
/**
  * @brief          ·¢Ë͵ç»ú¿ØÖƵçÁ÷(0x205,0x206,0x207,0x208)
  * @param[in]      yaw: (0x205) 6020µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-30000,30000]
  * @param[in]      pitch: (0x206) 6020µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-30000,30000]
  * @param[in]      shoot: (0x207) 2006µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-10000,10000]
  * @param[in]      rev: (0x208) ±£Áô£¬µç»ú¿ØÖƵçÁ÷
  * @retval         none
  */
void CAN_cmd_gimbal(int16_t yaw, int16_t pitch, int16_t shoot, int16_t rev)
{
    uint32_t send_mail_box;
    gimbal_tx_message.StdId = CAN_GIMBAL_ALL_ID;
    gimbal_tx_message.IDE = CAN_ID_STD;
    gimbal_tx_message.RTR = CAN_RTR_DATA;
    gimbal_tx_message.DLC = 0x08;
    gimbal_can_send_data[0] = (yaw >> 8);
    gimbal_can_send_data[1] = yaw;
    gimbal_can_send_data[2] = (pitch >> 8);
    gimbal_can_send_data[3] = pitch;
    gimbal_can_send_data[4] = (shoot >> 8);
    gimbal_can_send_data[5] = shoot;
    gimbal_can_send_data[6] = (rev >> 8);
    gimbal_can_send_data[7] = rev;
    HAL_CAN_AddTxMessage(&GIMBAL_CAN, &gimbal_tx_message, gimbal_can_send_data, &send_mail_box);
}

/**
  * @brief          send CAN packet of ID 0x700, it will set chassis motor 3508 to quick ID setting
  * @param[in]      none
  * @retval         none
  */
/**
  * @brief          ·¢ËÍIDΪ0x700µÄCAN°ü,Ëü»áÉèÖÃ3508µç»ú½øÈë¿ìËÙÉèÖÃID
  * @param[in]      none
  * @retval         none
  */
void CAN_cmd_chassis_reset_ID(void)
{
    uint32_t send_mail_box;
    chassis_tx_message.StdId = 0x700;
    chassis_tx_message.IDE = CAN_ID_STD;
    chassis_tx_message.RTR = CAN_RTR_DATA;
    chassis_tx_message.DLC = 0x08;
    chassis_can_send_data[0] = 0;
    chassis_can_send_data[1] = 0;
    chassis_can_send_data[2] = 0;
    chassis_can_send_data[3] = 0;
    chassis_can_send_data[4] = 0;
    chassis_can_send_data[5] = 0;
    chassis_can_send_data[6] = 0;
    chassis_can_send_data[7] = 0;

    HAL_CAN_AddTxMessage(&CHASSIS_CAN, &chassis_tx_message, chassis_can_send_data, &send_mail_box);
}


/**
  * @brief          send control current of motor (0x201, 0x202, 0x203, 0x204)
  * @param[in]      motor1: (0x201) 3508 motor control current, range [-16384,16384]
  * @param[in]      motor2: (0x202) 3508 motor control current, range [-16384,16384]
  * @param[in]      motor3: (0x203) 3508 motor control current, range [-16384,16384]
  * @param[in]      motor4: (0x204) 3508 motor control current, range [-16384,16384]
  * @retval         none
  */
/**
  * @brief          ·¢Ë͵ç»ú¿ØÖƵçÁ÷(0x201,0x202,0x203,0x204)
  * @param[in]      motor1: (0x201) 3508µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-16384,16384]
  * @param[in]      motor2: (0x202) 3508µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-16384,16384]
  * @param[in]      motor3: (0x203) 3508µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-16384,16384]
  * @param[in]      motor4: (0x204) 3508µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-16384,16384]
  * @retval         none
  */
void CAN_cmd_chassis(int16_t motor1, int16_t motor2, int16_t motor3, int16_t motor4)
{
    uint32_t send_mail_box;
    chassis_tx_message.StdId = CAN_CHASSIS_ALL_ID;
    chassis_tx_message.IDE = CAN_ID_STD;
    chassis_tx_message.RTR = CAN_RTR_DATA;
    chassis_tx_message.DLC = 0x08;
    chassis_can_send_data[0] = motor1 >> 8;
    chassis_can_send_data[1] = motor1;
    chassis_can_send_data[2] = motor2 >> 8;
    chassis_can_send_data[3] = motor2;
    chassis_can_send_data[4] = motor3 >> 8;
    chassis_can_send_data[5] = motor3;
    chassis_can_send_data[6] = motor4 >> 8;
    chassis_can_send_data[7] = motor4;

    HAL_CAN_AddTxMessage(&CHASSIS_CAN, &chassis_tx_message, chassis_can_send_data, &send_mail_box);
}

/**
  * @brief          return the yaw 6020 motor data point
  * @param[in]      none
  * @retval         motor data point
  */
/**
  * @brief          ·µ»Øyaw 6020µç»úÊý¾ÝÖ¸Õë
  * @param[in]      none
  * @retval         µç»úÊý¾ÝÖ¸Õë
  */
const motor_measure_t *get_yaw_gimbal_motor_measure_point(void)
{
    return &motor_chassis[4];
}

/**
  * @brief          return the pitch 6020 motor data point
  * @param[in]      none
  * @retval         motor data point
  */
/**
  * @brief          ·µ»Øpitch 6020µç»úÊý¾ÝÖ¸Õë
  * @param[in]      none
  * @retval         µç»úÊý¾ÝÖ¸Õë
  */
const motor_measure_t *get_pitch_gimbal_motor_measure_point(void)
{
    return &motor_chassis[5];
}


/**
  * @brief          return the trigger 2006 motor data point
  * @param[in]      none
  * @retval         motor data point
  */
/**
  * @brief          ·µ»Ø²¦µ¯µç»ú 2006µç»úÊý¾ÝÖ¸Õë
  * @param[in]      none
  * @retval         µç»úÊý¾ÝÖ¸Õë
  */
const motor_measure_t *get_trigger_motor_measure_point(void)
{
    return &motor_chassis[6];
}


/**
  * @brief          return the chassis 3508 motor data point
  * @param[in]      i: motor number,range [0,3]
  * @retval         motor data point
  */
/**
  * @brief          ·µ»Øµ×Å̵ç»ú 3508µç»úÊý¾ÝÖ¸Õë
  * @param[in]      i: µç»ú±àºÅ,·¶Î§[0,3]
  * @retval         µç»úÊý¾ÝÖ¸Õë
  */
const motor_measure_t *get_chassis_motor_measure_point(uint8_t i)
{
    return &motor_chassis[(i & 0x03)];
}
CAN_receive.c

4、HAL库can接收发送h文件

/**
  ****************************(C) COPYRIGHT 2019 DJI****************************
  * @file       can_receive.c/h
  * @brief      there is CAN interrupt function  to receive motor data,
  *             and CAN send function to send motor current to control motor.
  *             ÕâÀïÊÇCANÖжϽÓÊÕº¯Êý£¬½ÓÊÕµç»úÊý¾Ý,CAN·¢Ëͺ¯Êý·¢Ë͵ç»úµçÁ÷¿ØÖƵç»ú.
  * @note
  * @history
  *  Version    Date            Author          Modification
  *  V1.0.0     Dec-26-2018     RM              1. done
  *
  @verbatim
  ==============================================================================

  ==============================================================================
  @endverbatim
  ****************************(C) COPYRIGHT 2019 DJI****************************
  */

#ifndef CAN_RECEIVE_H
#define CAN_RECEIVE_H

#include "struct_typedef.h"

#define CHASSIS_CAN hcan1
#define GIMBAL_CAN hcan2

/* CAN send and receive ID */
typedef enum
{
    CAN_CHASSIS_ALL_ID = 0x200,
    CAN_3508_M1_ID = 0x201,
    CAN_3508_M2_ID = 0x202,
    CAN_3508_M3_ID = 0x203,
    CAN_3508_M4_ID = 0x204,

    CAN_YAW_MOTOR_ID = 0x205,
    CAN_PIT_MOTOR_ID = 0x206,
    CAN_TRIGGER_MOTOR_ID = 0x207,
    CAN_GIMBAL_ALL_ID = 0x1FF,

} can_msg_id_e;

//rm motor data
typedef struct
{
    uint16_t ecd;
    int16_t speed_rpm;
    int16_t given_current;
    uint8_t temperate;
    int16_t last_ecd;
} motor_measure_t;


/**
  * @brief          send control current of motor (0x205, 0x206, 0x207, 0x208)
  * @param[in]      yaw: (0x205) 6020 motor control current, range [-30000,30000]
  * @param[in]      pitch: (0x206) 6020 motor control current, range [-30000,30000]
  * @param[in]      shoot: (0x207) 2006 motor control current, range [-10000,10000]
  * @param[in]      rev: (0x208) reserve motor control current
  * @retval         none
  */
/**
  * @brief          ·¢Ë͵ç»ú¿ØÖƵçÁ÷(0x205,0x206,0x207,0x208)
  * @param[in]      yaw: (0x205) 6020µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-30000,30000]
  * @param[in]      pitch: (0x206) 6020µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-30000,30000]
  * @param[in]      shoot: (0x207) 2006µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-10000,10000]
  * @param[in]      rev: (0x208) ±£Áô£¬µç»ú¿ØÖƵçÁ÷
  * @retval         none
  */
extern void CAN_cmd_gimbal(int16_t yaw, int16_t pitch, int16_t shoot, int16_t rev);

/**
  * @brief          send CAN packet of ID 0x700, it will set chassis motor 3508 to quick ID setting
  * @param[in]      none
  * @retval         none
  */
/**
  * @brief          ·¢ËÍIDΪ0x700µÄCAN°ü,Ëü»áÉèÖÃ3508µç»ú½øÈë¿ìËÙÉèÖÃID
  * @param[in]      none
  * @retval         none
  */
extern void CAN_cmd_chassis_reset_ID(void);

/**
  * @brief          send control current of motor (0x201, 0x202, 0x203, 0x204)
  * @param[in]      motor1: (0x201) 3508 motor control current, range [-16384,16384]
  * @param[in]      motor2: (0x202) 3508 motor control current, range [-16384,16384]
  * @param[in]      motor3: (0x203) 3508 motor control current, range [-16384,16384]
  * @param[in]      motor4: (0x204) 3508 motor control current, range [-16384,16384]
  * @retval         none
  */
/**
  * @brief          ·¢Ë͵ç»ú¿ØÖƵçÁ÷(0x201,0x202,0x203,0x204)
  * @param[in]      motor1: (0x201) 3508µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-16384,16384]
  * @param[in]      motor2: (0x202) 3508µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-16384,16384]
  * @param[in]      motor3: (0x203) 3508µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-16384,16384]
  * @param[in]      motor4: (0x204) 3508µç»ú¿ØÖƵçÁ÷, ·¶Î§ [-16384,16384]
  * @retval         none
  */
extern void CAN_cmd_chassis(int16_t motor1, int16_t motor2, int16_t motor3, int16_t motor4);

/**
  * @brief          return the yaw 6020 motor data point
  * @param[in]      none
  * @retval         motor data point
  */
/**
  * @brief          ·µ»Øyaw 6020µç»úÊý¾ÝÖ¸Õë
  * @param[in]      none
  * @retval         µç»úÊý¾ÝÖ¸Õë
  */
extern const motor_measure_t *get_yaw_gimbal_motor_measure_point(void);

/**
  * @brief          return the pitch 6020 motor data point
  * @param[in]      none
  * @retval         motor data point
  */
/**
  * @brief          ·µ»Øpitch 6020µç»úÊý¾ÝÖ¸Õë
  * @param[in]      none
  * @retval         µç»úÊý¾ÝÖ¸Õë
  */
extern const motor_measure_t *get_pitch_gimbal_motor_measure_point(void);

/**
  * @brief          return the trigger 2006 motor data point
  * @param[in]      none
  * @retval         motor data point
  */
/**
  * @brief          ·µ»Ø²¦µ¯µç»ú 2006µç»úÊý¾ÝÖ¸Õë
  * @param[in]      none
  * @retval         µç»úÊý¾ÝÖ¸Õë
  */
extern const motor_measure_t *get_trigger_motor_measure_point(void);

/**
  * @brief          return the chassis 3508 motor data point
  * @param[in]      i: motor number,range [0,3]
  * @retval         motor data point
  */
/**
  * @brief          ·µ»Øµ×Å̵ç»ú 3508µç»úÊý¾ÝÖ¸Õë
  * @param[in]      i: µç»ú±àºÅ,·¶Î§[0,3]
  * @retval         µç»úÊý¾ÝÖ¸Õë
  */
extern const motor_measure_t *get_chassis_motor_measure_point(uint8_t i);


#endif
CAN_receive.h

5、主函数中

---》初始化CAN总线和滤波器

MX_CAN1_Init();
MX_CAN2_Init();

can_filter_init();

--》循环中加入电机控制函数

CAN_cmd_chassis(4000, 4000, 4000, 4000);
HAL_Delay(2);
CAN_cmd_gimbal(10000, 10000, 10000, 10000);
HAL_Delay(2);

你可能感兴趣的:(关于嵌入式学习随笔->14《STM32CubeMX应用5-CAN总线的配置》)