#ifndef _MOTOR_PARAM_H__
#define _MOTOR_PARAM_H__
#include "bsp/bsp.h"
#include "os/os_types.h"
#include "foc/core/controller.h"

#define CONFIG_MAX_D_COUNT 20
#define CONFIG_MAX_VEL_COUNT 20
#define CONFIG_MAX_VOL_COUNT 5
/* 电机外特性map, 每个转速点对应的最大扭矩 */
typedef struct {
	s16 rpm;
	s16 torque;
}motor_map_t;

typedef struct{
	s16 torque;
	s16 d;
	s16 q;
}torque2dq_t;

typedef struct {
	s16 iq;
	float lq;
}iq_lq_map_t;
typedef torque2dq_t torque_map_t;

typedef struct {
	s16 curr;
	s16 id;
}curr_pair_t;

/* 转速对应的d轴电流 */
typedef struct {
	s16 rpm;
	curr_pair_t curr[CONFIG_MAX_D_COUNT];
}rpm_torque_map;

/* 电压、转速对应的d轴电流 */
typedef struct {
	s16 vol;
	rpm_torque_map rpm_torque[CONFIG_MAX_VEL_COUNT];
}vol_rpm_torque_map;

s16 motor_max_torque_for_rpm(s16 rpm);
void motor_mpta_fw_lookup(float rpm, float torque, dq_t *dq_out);
float motor_get_lq_from_iq(s16 iq);
float motor_get_ebreak_toruqe(float rpm);

#if 0
#if CONFIG_MOT_TYPE==MOTOR_BLUESHARK_NEW1
#define MOTOR_R   0.010f
#define MOTOR_Ld (0.000120f*0.5f)
#define MOTOR_Lq (0.000200f*0.5f)
#define MOTOR_POLES  4
#define MOTOR_ENC_OFFSET 0.0F
#define CONFIG_MAX_MOTOR_TORQUE 40.0F

#define TRQ_PI_KP 0.14F
#define TRQ_PI_KI 0.15F

#define CONFIG_CURRENT_BANDWITH  1000.0f /* 电流环带宽 */
#define CONFIG_DEFAULT_PHASE_CURR_LIM 200
#define CONFIG_MAX_FW_D_CURR     100.0F //d轴最大的退磁电流

#define MOTOR_NR 0x11
#elif CONFIG_MOT_TYPE==MOTOR_BLUESHARK_A1
#define MOTOR_STATOR_5N

#ifndef MOTOR_STATOR_5N
#define MOTOR_R   0.011f  //5N -> 0.0164/2
#define MOTOR_Ld (0.000140F*0.5f) //5N -> 0.000048
#define MOTOR_Lq (0.000356f*0.5f) //5N -> 0.0001335
#define MOTOR_Flux (0.019f)
#else
#define MOTOR_R   0.009f
#define MOTOR_Ld (0.000100F*0.5f) //5N -> 0.000048
#define MOTOR_Lq (0.000240f*0.5f) //5N -> 0.0001335
#define MOTOR_Flux (0.0158f)
#endif

#define MOTOR_POLES  4
#define MOTOR_ENC_OFFSET 0.0F
#define CONFIG_MAX_MOTOR_TORQUE 400.0f//45.0F

#define PI_VEL_LIM_KP 0.085F //0.13f
#define PI_VEL_LIM_KI 2.5F
#define PI_VEL_LIM_KD 0.0F

#define PI_VEL_KP 0.1F //0.13f
#define PI_VEL_KI 3.0F
#define PI_VEL_KD 0.0F

#define MOTOR_NR 0x16

//4000->10, 34; 8000->8, 15
#define CONFIG_CURRENT_BANDWITH  (800 * 2 * PI) /* 电流环带宽 = bandwith / (2 * pi)*/
#define CONFIG_DEFAULT_PHASE_CURR_LIM 500
#define CONFIG_MAX_FW_D_CURR     300.0F //d轴最大的退磁电流
#define CONFIG_CURRENT_LOOP_DECOUPE    //电流环解耦
#define CONFIG_SENSORLESS_MAX_IDC 60
#define CONFIG_SENSORLESS_MAX_TORQUE 100
#define CONFIG_SENSORLESS_MAX_SPEED 5000

#elif CONFIG_MOT_TYPE==MOTOR_BLUESHARK_NEW2
#define MOTOR_R   0.013f
#define MOTOR_Ld (0.000140f*0.5f)
#define MOTOR_Lq (0.000320f*0.5f)
#define MOTOR_POLES  4
#define MOTOR_ENC_OFFSET 250.0F
#define CONFIG_MAX_MOTOR_TORQUE 40.0F

#define TRQ_PI_KP 0.14F
#define TRQ_PI_KI 0.15F

#define MOTOR_NR 0x12

#define CONFIG_CURRENT_BANDWITH  1000.0f /* 电流环带宽 */
#define CONFIG_DEFAULT_PHASE_CURR_LIM 200
#define CONFIG_MAX_FW_D_CURR     100.0F //d轴最大的退磁电流

#elif CONFIG_MOT_TYPE==MOTOR_BLUESHARK_ZD_100
#define MOTOR_R   0.008f
#define MOTOR_Lq (0.000125f*0.5f)
#define MOTOR_Ld (0.000091f*0.5f)
#define MOTOR_POLES  5
#define MOTOR_Flux (0.019f)
#define MOTOR_ENC_OFFSET 0.0F


#define PI_VEL_LIM_KP 0.085F //0.13f
#define PI_VEL_LIM_KI 2.5F
#define PI_VEL_LIM_KD 0.0F

#define PI_VEL_KP 0.1F //0.13f
#define PI_VEL_KI 3.0F
#define PI_VEL_KD 0.0F

#define MOTOR_NR 0x13

#define CONFIG_CURRENT_LOOP_DECOUPE    //电流环解耦
#define CONFIG_SENSORLESS_MAX_IDC 30
#define CONFIG_SENSORLESS_MAX_TORQUE 80
#define CONFIG_SENSORLESS_MAX_SPEED 3000

#elif CONFIG_MOT_TYPE==MOTOR_BLUESHARK_OLD
#define MOTOR_R 0.012f
#define MOTOR_Ld (0.000143f*0.5f)
#define MOTOR_Lq (0.000205f*0.5f)
#define MOTOR_POLES  5
#define MOTOR_ENC_OFFSET 180.0F
#define CONFIG_MAX_MOTOR_TORQUE 40.0F

#define TRQ_PI_KP 0.14F
#define TRQ_PI_KI 0.15F

#define MOTOR_NR 0x14

#define CONFIG_CURRENT_BANDWITH  1000.0f /* 电流环带宽 */
#define CONFIG_DEFAULT_PHASE_CURR_LIM 200
#define CONFIG_MAX_FW_D_CURR     100.0F //d轴最大的退磁电流

#elif CONFIG_MOT_TYPE==MOTOR_3505
//编码器电机 3505
#define MOTOR_R 0.08f
#define MOTOR_Ld 0.000032f
#define MOTOR_Lq 0.000032f
#define MOTOR_POLES  10
#define CONFIG_MAX_MOTOR_CURR 20.0F

#define TRQ_PI_KP 0.14F
#define TRQ_PI_KI 0.15F

#define MOTOR_NR 0x15

#define CONFIG_CURRENT_BANDWITH  1000.0f /* 电流环带宽 */

#endif
#endif

#endif /* _MOTOR_PARAM_H__ */
