Skip to content

Commit

Permalink
promote data types
Browse files Browse the repository at this point in the history
  • Loading branch information
yconst committed Feb 25, 2024
1 parent 5de6b0d commit eb1b579
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions firmware/src/sensor/sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ void sensor_reset(Sensor *s)
bool sensor_calibrate_offset_and_rectification(Sensor *s, Observer *o)
{
// Size below is an arbitrary large number ie > ECN_SIZE * npp
float error_ticks[ECN_SIZE * 20];
float error_ticks[ECN_SIZE * 24];
const int16_t npp = motor_get_pole_pairs();
const int16_t n = ECN_SIZE * npp;
const int16_t nconv = 100;
const float delta = 2 * PI * npp / (n * nconv);
const float e_pos_to_ticks = ((float)SENSOR_COMMON_RES_TICKS) / (2 * PI * npp);
float e_pos_ref = 0.f;
float e_pos_ref = 0;
const float I_setpoint = motor_get_I_cal();
int16_t *lut = s->config.rec_table;
int32_t *lut = s->config.rec_table;
set_epos_and_wait(e_pos_ref, I_setpoint);
wait_pwm_cycles(5000);
const uint16_t offset_idx = (s->get_raw_angle_func(s)) >> (SENSOR_COMMON_RES_BITS - ECN_BITS);
const int32_t offset_idx = (s->get_raw_angle_func(s)) >> (SENSOR_COMMON_RES_BITS - ECN_BITS);

for (uint32_t i = 0; i < n; i++)
{
for (uint8_t j = 0; j < nconv; j++)
for (int16_t j = 0; j < nconv; j++)
{
e_pos_ref += delta;
set_epos_and_wait(e_pos_ref, I_setpoint);
Expand All @@ -54,7 +54,7 @@ bool sensor_calibrate_offset_and_rectification(Sensor *s, Observer *o)
}
for (uint32_t i = 0; i < n; i++)
{
for (uint8_t j = 0; j < nconv; j++)
for (int16_t j = 0; j < nconv; j++)
{
e_pos_ref -= delta;
set_epos_and_wait(e_pos_ref, I_setpoint);
Expand All @@ -72,7 +72,7 @@ bool sensor_calibrate_offset_and_rectification(Sensor *s, Observer *o)
float acc = 0;
for (int16_t j = 0; j < ECN_SIZE; j++)
{
int16_t read_idx = -ECN_SIZE / 2 + j + i * npp;
int32_t read_idx = -ECN_SIZE / 2 + j + i * npp;
if (read_idx < 0)
{
read_idx += n;
Expand All @@ -84,12 +84,12 @@ bool sensor_calibrate_offset_and_rectification(Sensor *s, Observer *o)
acc += error_ticks[read_idx];
}
acc = acc / ((float)(ECN_SIZE * 2));
int16_t write_idx = i + offset_idx;
int32_t write_idx = i + offset_idx;
if (write_idx > (ECN_SIZE - 1))
{
write_idx -= ECN_SIZE;
}
lut[write_idx] = (int16_t)acc;
lut[write_idx] = (int32_t)acc;
}
wait_pwm_cycles(5000);
s->config.rec_calibrated = true;
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/sensor/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ typedef enum {
struct SensorConfig {
uint32_t id;
sensor_type_t type;
int16_t rec_table[ECN_SIZE];
int32_t rec_table[ECN_SIZE];
bool rec_calibrated;
};

Expand Down

0 comments on commit eb1b579

Please sign in to comment.