|
@@ -36,6 +36,7 @@ static u16 _hall_table[] = {0xFFFF, 121/*1*/, 240/*2*/, 190/*3*/, 13/*4*/, 58/*5
|
|
|
static hall_t _hall;
|
|
static hall_t _hall;
|
|
|
static hall_sample_t h_samples;
|
|
static hall_sample_t h_samples;
|
|
|
static timer_t _hall_detect_timer = TIMER_INIT(_hall_detect_timer, _hall_timeout_handler);
|
|
static timer_t _hall_detect_timer = TIMER_INIT(_hall_detect_timer, _hall_timeout_handler);
|
|
|
|
|
+measure_time_t g_meas_hall = {.exec_max_time = 1,};
|
|
|
|
|
|
|
|
#define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
|
|
#define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
|
|
|
#define us_2_s(tick) ((float)tick / 1000000.0f)
|
|
#define us_2_s(tick) ((float)tick / 1000000.0f)
|
|
@@ -44,10 +45,6 @@ static float __inline _delta_seconds(u32 prev) {
|
|
|
return (float)timer_count32_delta_us(prev, NULL)/1000000.0f;
|
|
return (float)timer_count32_delta_us(prev, NULL)/1000000.0f;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static u32 __inline get_seconds(void) {
|
|
|
|
|
- return ticks_2_ms(co_task_sys64_ticks()/1000);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
static void _hall_put_sample(float angle, u32 ticks) {
|
|
static void _hall_put_sample(float angle, u32 ticks) {
|
|
|
hall_sample_t *s = &h_samples;
|
|
hall_sample_t *s = &h_samples;
|
|
|
s->index += 1;
|
|
s->index += 1;
|
|
@@ -82,8 +79,8 @@ void hall_sensor_init(void) {
|
|
|
|
|
|
|
|
void _hall_timeout_handler(timer_t *timer){
|
|
void _hall_timeout_handler(timer_t *timer){
|
|
|
|
|
|
|
|
- _hall.degree_per_s = 0;
|
|
|
|
|
- _hall.e_rpm = _hall.e_filted_rpm = 0;
|
|
|
|
|
|
|
+ _hall.angle_speed = _hall.angle_speed_avg = 0;
|
|
|
|
|
+ _hall.rpm = _hall.rpm_avg = 0;
|
|
|
h_samples.index = 0;
|
|
h_samples.index = 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -99,7 +96,7 @@ float hall_sensor_get_theta(void){
|
|
|
read_hall(_hall.state, _hall.theta);
|
|
read_hall(_hall.state, _hall.theta);
|
|
|
return _hall.theta;
|
|
return _hall.theta;
|
|
|
}
|
|
}
|
|
|
- _hall.est_theta = _delta_seconds(_hall.ticks) * _hall.degree_per_s + _hall.theta;
|
|
|
|
|
|
|
+ _hall.est_theta = _delta_seconds(_hall.ticks) * _hall.angle_speed + _hall.theta;
|
|
|
float est_delta = _hall.est_theta - _hall.theta;
|
|
float est_delta = _hall.est_theta - _hall.theta;
|
|
|
if (est_delta > PHASE_60_DEGREE) {
|
|
if (est_delta > PHASE_60_DEGREE) {
|
|
|
_hall.est_theta = _hall.theta + PHASE_60_DEGREE;
|
|
_hall.est_theta = _hall.theta + PHASE_60_DEGREE;
|
|
@@ -108,7 +105,7 @@ float hall_sensor_get_theta(void){
|
|
|
}
|
|
}
|
|
|
}else if (est_delta < -PHASE_60_DEGREE){
|
|
}else if (est_delta < -PHASE_60_DEGREE){
|
|
|
_hall.est_theta = _hall.theta - PHASE_60_DEGREE;
|
|
_hall.est_theta = _hall.theta - PHASE_60_DEGREE;
|
|
|
- if (_hall.est_theta <= -PHASE_360_DEGREE) {
|
|
|
|
|
|
|
+ if (_hall.est_theta < 0/*= -PHASE_360_DEGREE*/) {
|
|
|
_hall.est_theta += PHASE_360_DEGREE;
|
|
_hall.est_theta += PHASE_360_DEGREE;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -121,11 +118,11 @@ void hall_sensor_set_theta(bool override, float theta){
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
float hall_sensor_get_speed(void) {
|
|
float hall_sensor_get_speed(void) {
|
|
|
- return _hall.e_rpm;
|
|
|
|
|
|
|
+ return _hall.rpm;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
float hall_sensor_avg_speed(void) {
|
|
float hall_sensor_avg_speed(void) {
|
|
|
- return _hall.e_filted_rpm;
|
|
|
|
|
|
|
+ return _hall.rpm_avg;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int hall_offset_increase(int inc) {
|
|
int hall_offset_increase(int inc) {
|
|
@@ -204,10 +201,12 @@ int hall_sensor_calibrate(float voltage, u16 *hall_table){
|
|
|
|
|
|
|
|
|
|
|
|
|
void hall_sensor_handler(void) {
|
|
void hall_sensor_handler(void) {
|
|
|
|
|
+
|
|
|
|
|
+ time_measure_start(&g_meas_hall);
|
|
|
u8 state_now = get_hall_stat(HALL_READ_TIMES);
|
|
u8 state_now = get_hall_stat(HALL_READ_TIMES);
|
|
|
float theta_now = _hall_table[state_now];
|
|
float theta_now = _hall_table[state_now];
|
|
|
u8 state_prev = _hall.state;
|
|
u8 state_prev = _hall.state;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (!_hall.working) {
|
|
if (!_hall.working) {
|
|
|
if(theta_now != 0xFFFF) {
|
|
if(theta_now != 0xFFFF) {
|
|
|
_hall.working = true;
|
|
_hall.working = true;
|
|
@@ -217,7 +216,7 @@ void hall_sensor_handler(void) {
|
|
|
}
|
|
}
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
switch (state_now) {
|
|
switch (state_now) {
|
|
|
case STATE_1:
|
|
case STATE_1:
|
|
|
if (state_prev == STATE_5) {
|
|
if (state_prev == STATE_5) {
|
|
@@ -277,26 +276,39 @@ void hall_sensor_handler(void) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if (theta_now >= 360.0f) {
|
|
|
|
|
+ theta_now -= 360.0f;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
float delta_time = _delta_seconds(_hall.ticks);
|
|
float delta_time = _delta_seconds(_hall.ticks);
|
|
|
if (delta_time == 0.0f) { //may be errors ???
|
|
if (delta_time == 0.0f) { //may be errors ???
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
float delta_theta = (_hall.direction == POSITIVE)?60.0f : -60.0f;
|
|
float delta_theta = (_hall.direction == POSITIVE)?60.0f : -60.0f;
|
|
|
_hall_put_sample(delta_theta, timer_count32_delta_us(_hall.ticks, NULL));
|
|
_hall_put_sample(delta_theta, timer_count32_delta_us(_hall.ticks, NULL));
|
|
|
|
|
+ _hall.angle_speed = delta_theta / delta_time;
|
|
|
|
|
+
|
|
|
|
|
+ os_disable_irq();
|
|
|
|
|
+
|
|
|
if (!h_samples.full) {
|
|
if (!h_samples.full) {
|
|
|
- _hall.degree_per_s = delta_theta / delta_time;
|
|
|
|
|
- }else {
|
|
|
|
|
- _hall.degree_per_s = _hall_avg_speed();
|
|
|
|
|
- _hall.e_filted_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
|
|
|
|
|
|
|
+ _hall.angle_speed_avg = _hall.angle_speed;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ _hall.angle_speed_avg = _hall_avg_speed();
|
|
|
}
|
|
}
|
|
|
_hall.ticks = timer_count32_get();
|
|
_hall.ticks = timer_count32_get();
|
|
|
- _hall.second = get_seconds();
|
|
|
|
|
_hall.theta = theta_now;
|
|
_hall.theta = theta_now;
|
|
|
|
|
+
|
|
|
|
|
+ os_enable_irq();
|
|
|
|
|
+
|
|
|
_hall.state = state_now;
|
|
_hall.state = state_now;
|
|
|
- _hall.e_rpm = _hall.degree_per_s / 360.0f * 60.0f;
|
|
|
|
|
- timer_post(&_hall_detect_timer, 2000);
|
|
|
|
|
- //printf("speed :%.4f - %.4f - %.4f - %d\n", _hall.degree_per_s, delta_theta, delta_time, (int)_hall.e_rpm);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ _hall.rpm = _hall.angle_speed / 360.0f * 60.0f;
|
|
|
|
|
+ _hall.rpm_avg = _hall.angle_speed_avg / 360 * 60.0f;
|
|
|
|
|
+
|
|
|
|
|
+ timer_post(&_hall_detect_timer, 1500);
|
|
|
|
|
+
|
|
|
|
|
+ time_measure_end(&g_meas_hall);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|