Skip to content

Commit

Permalink
track and use reference frame calibrations
Browse files Browse the repository at this point in the history
  • Loading branch information
yconst committed Apr 17, 2024
1 parent 454fbcd commit 8589ed4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
6 changes: 5 additions & 1 deletion firmware/src/sensor/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ void sensor_external_spi_set_type_avlos(sensors_setup_external_spi_type_options
&& (controller_get_state() == CONTROLLER_STATE_IDLE)
&& (internal_type != sensors[SENSOR_CONNECTION_EXTERNAL_SPI].sensor.config.type))
{
frames_reset_calibrated();

Sensor *s = &(sensors[SENSOR_CONNECTION_EXTERNAL_SPI].sensor);
if (s->initialized)
{
Expand All @@ -222,6 +224,8 @@ void sensor_set_connection(Sensor** target_sensor_p, Sensor** other_sensor_p, se
&& new_connection != sensor_get_connection(*target_sensor_p)
&& new_connection >= 0 && new_connection < SENSOR_CONNECTION_MAX)
{
frames_reset_calibrated();

if (sensor_get_connection(*target_sensor_p) != sensor_get_connection(*other_sensor_p))
{
observer_reset_state(observer_get_for_sensor(*target_sensor_p));
Expand Down Expand Up @@ -295,7 +299,7 @@ bool sensors_calibrate_pole_pair_count_and_transforms(void)

if (commutation_sensor_p == position_sensor_p)
{
frame_set_position_sensor_to_motor(frames.commutation_sensor_to_motor);
frame_set_position_sensor_to_motor(*(frame_commutation_sensor_to_motor_p()));
}
else if (sensor_get_type(position_sensor_p) != SENSOR_TYPE_HALL)
{
Expand Down
3 changes: 2 additions & 1 deletion firmware/src/system/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ TM_RAMFUNC float system_get_Vbus(void)

TM_RAMFUNC bool system_get_calibrated(void)
{
return (motor_get_calibrated() &&
return (frames_get_calibrated() &&
motor_get_calibrated() &&
commutation_sensor_p->is_calibrated_func(commutation_sensor_p) &&
position_sensor_p->is_calibrated_func(position_sensor_p));
}
Expand Down
3 changes: 2 additions & 1 deletion firmware/src/xfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ FramesConfig frames = {
.commutation_sensor_to_motor = DEFAULT_TRANSFORM,
.motor_to_commutation_sensor = DEFAULT_TRANSFORM,
.motor_to_user = DEFAULT_TRANSFORM,
.user_to_motor = DEFAULT_TRANSFORM
.user_to_motor = DEFAULT_TRANSFORM,
.calibrated = default_calibrated
};
34 changes: 34 additions & 0 deletions firmware/src/xfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,24 @@

#pragma once

#include <stdbool.h>
#include <src/xf1.h>

typedef enum {
none_calibrated = 0,
position_sensor_to_user_calibrated = 1,
user_to_position_sensor_calibrated = 1 << 1,
position_sensor_to_motor_calibrated = 1 << 2,
motor_to_position_sensor_calibrated = 1 << 3,
commutation_sensor_to_motor_calibrated = 1 << 4,
motor_to_commutation_sensor_calibrated = 1 << 5,
motor_to_user_calibrated = 1 << 6,
user_to_motor_calibrated = 1 << 7,
default_calibrated = (position_sensor_to_user_calibrated | user_to_position_sensor_calibrated),
all_calibrated = 0xFF
} FramesOrder;


typedef struct {
FrameTransform position_sensor_to_user;
FrameTransform user_to_position_sensor;
Expand All @@ -27,10 +43,21 @@ typedef struct {
FrameTransform motor_to_commutation_sensor;
FrameTransform motor_to_user;
FrameTransform user_to_motor;
FramesOrder calibrated;
} FramesConfig;

extern FramesConfig frames;

static inline bool frames_get_calibrated(void)
{
return (frames.calibrated == all_calibrated);
}

static inline void frames_reset_calibrated(void)
{
frames.calibrated = default_calibrated;
}

static inline void frames_get_config(FramesConfig *_config)
{
*_config = frames;
Expand Down Expand Up @@ -97,6 +124,8 @@ static inline void frame_user_to_position_sensor_set_offset(float value)
frames.position_sensor_to_user = derive_inverse_transform(frame_user_to_position_sensor_p());
frames.user_to_motor = combine_transforms(frame_user_to_position_sensor_p(), frame_position_sensor_to_motor_p());
frames.motor_to_user = derive_inverse_transform(frame_user_to_motor_p());
frames.calibrated |= (position_sensor_to_user_calibrated |
user_to_position_sensor_calibrated | motor_to_user_calibrated | user_to_motor_calibrated);
}

static inline void frame_user_to_position_sensor_set_multiplier(float value)
Expand All @@ -107,13 +136,16 @@ static inline void frame_user_to_position_sensor_set_multiplier(float value)
frames.position_sensor_to_user = derive_inverse_transform(frame_user_to_position_sensor_p());
frames.user_to_motor = combine_transforms(frame_user_to_position_sensor_p(), frame_position_sensor_to_motor_p());
frames.motor_to_user = derive_inverse_transform(frame_user_to_motor_p());
frames.calibrated |= (position_sensor_to_user_calibrated |
user_to_position_sensor_calibrated | motor_to_user_calibrated | user_to_motor_calibrated);
}
}

static inline void frame_set_commutation_sensor_to_motor(const FrameTransform value)
{
frames.commutation_sensor_to_motor = value;
frames.motor_to_commutation_sensor = derive_inverse_transform(frame_commutation_sensor_to_motor_p());
frames.calibrated |= (commutation_sensor_to_motor_calibrated | motor_to_commutation_sensor_calibrated);
}

static inline void frame_set_position_sensor_to_motor(const FrameTransform value)
Expand All @@ -122,4 +154,6 @@ static inline void frame_set_position_sensor_to_motor(const FrameTransform value
frames.motor_to_position_sensor = derive_inverse_transform(&value);
frames.user_to_motor = combine_transforms(frame_user_to_position_sensor_p(), frame_position_sensor_to_motor_p());
frames.motor_to_user = derive_inverse_transform(frame_user_to_motor_p());
frames.calibrated |= (position_sensor_to_motor_calibrated |
motor_to_position_sensor_calibrated | motor_to_user_calibrated | user_to_motor_calibrated);
}

0 comments on commit 8589ed4

Please sign in to comment.