#pragma once

#ifdef __cplusplus
extern "C"
{
#endif

#include "bsp/struct_typedef.h"
#include "component/filter.h"
#include "component/pid.h"
#include "component/ahrs.h"
//#include "device/buzzer.h"
#include "device/bmi088.h"
#include "component/user_math.h"
#include "component/filter.h"
#include "device/motor_rm.h"
#include "module/cmd.h"

#define CHASSIS_OK (0)
#define CHASSIS_ERR (-1)
#define CHASSIS_ERR_NULL (-2)
#define CHASSIS_ERR_MODE (-3) /*CMD_ChassisMode_t */
#define CHASSIS_ERR_TYPE (-4) /*Chassis_Type_t */


//     纵向/横向
#define radians atan(1.0f * 390 /390) // 角度制

	// 四个舵轮的安装误差
	typedef struct
	{
		fp32 MOTOR_OFFSET[4];
	} MotorOffset_t;

	typedef struct
	{

		BMI088_t bmi088;

		/*可通过该枚举类型来决定Imu的数据量纲*/
		enum
		{
			IMU_DEGREE, // 角度值（0-360）
			IMU_RADIAN	// 弧度制（0-2pi)
		} angle_mode;

		AHRS_Eulr_t imu_eulr; // 解算后存放欧拉角（弧度制）
	} ChassisImu_t;

	/*底盘参数结构体*/
	typedef struct
	{

		/*该部分决定PID的参数整定在config中修改*/
		KPID_Params_t C6020Omega_param;
		KPID_Params_t C6020Angle_param;
		KPID_Params_t M3508v_param;
		KPID_Params_t chassis_follow_gimbalPID;

		KPID_Params_t chassis_2006_angle_param;
		KPID_Params_t chassis_2006_Omega_param;
		KPID_Params_t chassis_3508_angle_param;
		KPID_Params_t chassis_3508_Omega_param;

		MOTOR_RM_Param_t motor_3508_param[4]; // 四个3508电机
		MOTOR_RM_Param_t motor_6020_param[4]; // 四个6020电机
		MOTOR_RM_Param_t motor_2006_param;	// 一个2006电机

		// MOTOR_RM_Param_t chassis_follow_gimbal_param; // 底盘跟随云台
		float mech_zero;/*云台6020的机械中点*/
		float travel ;/*云台6020的机械行程*/

		float chassis2006_setangle; // 变形舵轮2006目标角度  大概是48圈
		float chassis3508_setangle; // 变形舵轮3508目标角度  大概是60度左右


	} Chassis_Param_t;

	typedef struct
	{
		fp32 Vx;
		fp32 Vy;
		fp32 Vw;
		fp32 mul; // 油门倍率
	} ChassisMove_Vec;

	typedef struct
	{
		float rotor3508_out[4];
		float rotor6020_out[4];
		float motor2006_out;
	} Chassis_out_t;

	typedef struct
	{
		uint32_t last_wakeup;
		float dt;

		Chassis_mode_t mode;
		ChassisMove_Vec move_vec; // 最终输入速度

		/*期望的底盘输出值(此处为舵轮解算出的各个电机的期望输出值)ֵ*/
		struct
		{
			fp32 rotor3508_jiesuan_1[4];
			fp32 rotor3508_jiesuan_2[4];
			fp32 rotor6020_jiesuan_1[4];
			fp32 rotor6020_jiesuan_2[4];
			// fp32 rotor6020_elur_yaw;

			fp32 motor6020_target[4];
			fp32 motor3508_target[4];

		} hopemotorout;

		struct
		{
			fp32 final_6020out[4];
			fp32 final_3508out[4];
			fp32 final_2006out;
			// fp32 final_pitchout;
		} final_out;

		struct
		{
			fp32 rotor_rpm3508[4];
			fp32 rotor_current3508[4];
			fp32 rotor_angle3508[4];
			fp32 rotor_rpm6020[4];
			fp32 rotor_angle6020[4];
			fp32 rotor_current6020[4];
			fp32 rotor_temp6020[4];
			fp32 motor2006_rpm;
			fp32 motor2006_current;
			fp32 motor2006_temp;
			fp32 motor2006_angle;

			float gimbal_yaw_encoder;
		} motorfeedback;

		struct
		{
			KPID_t chassis_6020anglePid[4];
			KPID_t chassis_6020OmegaPid[4];
			KPID_t chassis_3508VPID[4];
			KPID_t chassis_follow_gimbal_pid;

			KPID_t chassis_2006_anglepid;
			KPID_t chassis_2006_OmegaPid;
			KPID_t chassis_3508_anglepid[4];
			KPID_t chassis_3508_OmegaPid[4];

		} pid;

		uint8_t keeping_angle_flag; // 是否处于保持角度模式

		// AHRS_Eulr_t set_point; // 底盘纠正目标角
		// fp32 angle_current;	   // 当前角度
		// fp32 angle_piancha;	   // 偏差角度
		// fp32 yaw_out;		   // 角度pid输出值

		ChassisImu_t pos088;		 // 088的实时姿态
		MotorOffset_t motoroffset;	 // 5065校准数据
		Chassis_Param_t *param;		 // 一些固定的参数
		fp32 vofa_send[8];			 // vofa输出数据
		LowPassFilter2p_t filled[11]; // 低通滤波器
		float keep_angle[4];		 // 保持的 6020 角度

		Chassis_out_t out;

		float wz_multi; /* 小陀螺模式旋转方向 */
		float mech_zero;/*云台6020的机械中点*/
		float travel ;/*云台6020的机械行程*/

		float chassis2006_setangle; // 变形舵轮2006目标角度  大概是48圈
		float chassis3508_setangle; // 变形舵轮3508目标角度  大概是60度左右
	} Chassis_t;


	int8_t chassis_init(Chassis_t *c,  Chassis_Param_t *param, float target_freq);
	int8_t Chassis_update(Chassis_t *c);
	int8_t Chassis_Control(Chassis_t *c,  Chassis_CMD_t *c_cmd,uint32_t now);
	void Chassis_Setoutput(Chassis_t *c);
#ifdef __cplusplus
}
#endif
