#include "bsp/bsp_driver.h"
#include "foc/core/foc_observer.h"
#include "foc/core/ladrc_observer.h"
#include "foc/core/smo_observer.h"
#include "foc/motor/motor.h"

static foc_observer_t foc_obser;
void foc_observer_init(void) {
	foc_obser.is_sensorless_enable = false;
	foc_obser.is_sensorless_running = false;
	foc_obser.enc_angle = INVALID_ANGLE;
	foc_obser.enc_est_angle = INVALID_ANGLE;
	foc_obser.enc_speed = 0;
	foc_obser.sensorless_angle = INVALID_ANGLE;
	foc_obser.sensorless_est_angle = INVALID_ANGLE;
	foc_obser.sensorless_speed = 0;
	foc_obser.enc_err_cnt = 0;
#ifdef CONFIG_SMO_OBSERVER
	smo_observer_init(CONFIG_SMO_PLL_BANDWITH, CONFIG_SMO_LFP_WC, CONFIG_SMO_GAIN_K, CONFIG_SMO_SIGMOID_MAX);
	foc_obser.is_sensorless_enable = true;
#endif
#ifdef CONFIG_LADRC_OBSERVER
	ladrc_observer_init(CONFIG_LADRC_OBSERVER_MIN_Wo, CONFIG_LADRC_OBSERVER_MIN_eVEL, CONFIG_LADRC_OBSERVER_LPF_FREQ);
	foc_obser.is_sensorless_enable = true;
#endif
}

#define RPM_2_degree(rpm)  ((rpm) * 6.0f * nv_get_motor_params()->poles * FOC_CTRL_US)

float foc_observer_update(float uAlp, float uBeta, float iAlp, float iBeta){
	foc_obser.sensorless_est_angle = foc_obser.sensorless_angle + RPM_2_degree(foc_obser.sensorless_speed);
#ifdef CONFIG_SMO_OBSERVER
	foc_obser.sensorless_angle = smo_observer_update(uAlp, uBeta, iAlp, iBeta);
	foc_obser.sensorless_speed = smo_observer_vel();
#elif defined CONFIG_LADRC_OBSERVER
	foc_obser.sensorless_angle = ladrc_observer_update(uAlp, uBeta, iAlp, iBeta);
	foc_obser.sensorless_speed = ladrc_observer_vel();
#endif
	return foc_obser.sensorless_angle;
}

bool foc_observer_diagnostic(float enc_angle, float enc_vel) {
	if (enc_vel <= 50.0f) {
		foc_obser.enc_angle = enc_angle;
		foc_obser.enc_speed = ABS(enc_vel);
		return true;
	}
	if (!foc_obser.is_sensorless_running) {
		float delta_angle_est = RPM_2_degree((foc_obser.enc_speed + ABS(enc_vel))/2.0f);
		float delta_angle_enc = enc_angle - foc_obser.enc_angle;
		delta_angle_enc = ABS(delta_angle_enc);
		if (delta_angle_enc > 180.0f) {
			delta_angle_enc = 360.0f - delta_angle_enc;
		}
		float r = ABS(delta_angle_enc - delta_angle_est)/(delta_angle_est + 1e-20f);
		float thro = 2.0f;
		if (delta_angle_est < 0.5f) {
			thro = 6.0f;
		}else if (delta_angle_est < 2.0f) {
			thro = 2.0f;
		}else if (delta_angle_est < 4.0f) {
			thro = 1.0f;
		}else if (delta_angle_est < 6.0f) {
			thro = 0.7f;
		}else if (delta_angle_est < 10.0f) {
			thro = 0.5f;
		}else {
			thro = 0.4f;
		}
		if (r >= thro) {
			if (foc_obser.enc_err_cnt < 65535) {
				foc_obser.enc_err_cnt ++;
			}
		}
	}
	foc_obser.enc_angle = enc_angle;
	foc_obser.enc_speed = ABS(enc_vel);
	return !foc_obser.is_sensorless_running;
}

float foc_observer_speed(void) {
	return foc_obser.sensorless_speed;
}

bool  foc_observer_is_encoder(void) {
	return !foc_obser.is_sensorless_running;
}
void  foc_observer_use_sensorless(bool use_smo) {
	if (foc_obser.is_sensorless_enable) {
		foc_obser.is_sensorless_running = use_smo;
	}else {
		foc_obser.is_sensorless_running = false;
	}
}
void  foc_observer_enable_sensorless(bool enable) {
	foc_obser.is_sensorless_enable = enable;
}

float foc_observer_sensorless_angle(void) {
	return foc_obser.sensorless_angle;
}

float foc_observer_sensorless_speed(void) {
	return foc_obser.sensorless_speed;
}

u16 foc_observer_enc_errcount(void) {
	return foc_obser.enc_err_cnt;
}

float foc_observer_sensorless_working_speed(void) {
#ifdef CONFIG_SMO_OBSERVER
	return CONFIG_SMO_MIN_SPEED;
#elif defined CONFIG_LADRC_OBSERVER
	return CONFIG_LADRC_OBSERVER_MIN_SPEED;
#else
	return 20000.0f;
#endif
}

