码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-02-02 01:49:50      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:ptr   rate   call   eset   mamicode   activate   ddt   let   end   

STM32CubeMX-CAN总线的配置

技术图片

配置好后生成代码

关于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.
  *             ?aà?ê?CAN?D???óê?oˉêy£??óê?μ??úêy?Y,CAN·¢?íoˉêy·¢?íμ??úμ?á÷????μ??ú.
  * @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;
μ??úêy?Y, 0:μ×?ìμ??ú1 3508μ??ú,  1:μ×?ìμ??ú2 3508μ??ú,2:μ×?ìμ??ú3 3508μ??ú,3:μ×?ìμ??ú4 3508μ??ú;
4:yaw??ì¨μ??ú 6020μ??ú; 5:pitch??ì¨μ??ú 6020μ??ú; 6:2|μˉμ??ú 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?aCAN??μ÷oˉêy,?óê?μ??úêy?Y
  * @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?a0x700μ?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μ??úêy?Y????
  * @param[in]      none
  * @retval         μ??úêy?Y????
  */
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μ??úêy?Y????
  * @param[in]      none
  * @retval         μ??úêy?Y????
  */
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          ·μ??2|μˉμ??ú 2006μ??úêy?Y????
  * @param[in]      none
  * @retval         μ??úêy?Y????
  */
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μ??úêy?Y????
  * @param[in]      i: μ??ú±ào?,·??§[0,3]
  * @retval         μ??úêy?Y????
  */
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.
  *             ?aà?ê?CAN?D???óê?oˉêy£??óê?μ??úêy?Y,CAN·¢?íoˉêy·¢?íμ??úμ?á÷????μ??ú.
  * @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?a0x700μ?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μ??úêy?Y????
  * @param[in]      none
  * @retval         μ??úêy?Y????
  */
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μ??úêy?Y????
  * @param[in]      none
  * @retval         μ??úêy?Y????
  */
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          ·μ??2|μˉμ??ú 2006μ??úêy?Y????
  * @param[in]      none
  * @retval         μ??úêy?Y????
  */
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μ??úêy?Y????
  * @param[in]      i: μ??ú±ào?,·??§[0,3]
  * @retval         μ??úêy?Y????
  */
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总线的配置》

标签:ptr   rate   call   eset   mamicode   activate   ddt   let   end   

原文地址:https://www.cnblogs.com/vcan123/p/12250662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!