@@ -63,17 +63,18 @@ void Sensors::init()
6363 rf_.board_ .sensors_init ();
6464
6565 init_imu ();
66+ init_mag ();
6667
6768 this ->update_battery_monitor_multipliers ();
6869}
6970
7071void Sensors::init_imu ()
7172{
72- // Quaternion to compensate for FCU orientation
73- float roll = rf_.params_ .get_param_float (PARAM_FC_ROLL ) * 0.017453293 ;
74- float pitch = rf_.params_ .get_param_float (PARAM_FC_PITCH ) * 0.017453293 ;
75- float yaw = rf_.params_ .get_param_float (PARAM_FC_YAW ) * 0.017453293 ;
76- fcu_orientation_ = turbomath::Quaternion (roll, pitch, yaw);
73+ // Quaternion to compensate for IMU orientation
74+ float roll = rf_.params_ .get_param_float (PARAM_IMU_ROLL ) * 0.017453293 ;
75+ float pitch = rf_.params_ .get_param_float (PARAM_IMU_PITCH ) * 0.017453293 ;
76+ float yaw = rf_.params_ .get_param_float (PARAM_IMU_YAW ) * 0.017453293 ;
77+ imu_orientation_ = turbomath::Quaternion (roll, pitch, yaw);
7778
7879 // See if the IMU is uncalibrated, and throw an error if it is
7980 if (rf_.params_ .get_param_float (PARAM_ACC_X_BIAS) == 0.0
@@ -86,14 +87,28 @@ void Sensors::init_imu()
8687 }
8788}
8889
90+ void Sensors::init_mag ()
91+ {
92+ // Quaternion to compensate for mag orientation
93+ float roll = rf_.params_ .get_param_float (PARAM_MAG_ROLL) * 0.017453293 ;
94+ float pitch = rf_.params_ .get_param_float (PARAM_MAG_PITCH) * 0.017453293 ;
95+ float yaw = rf_.params_ .get_param_float (PARAM_MAG_YAW) * 0.017453293 ;
96+ mag_orientation_ = turbomath::Quaternion (roll, pitch, yaw);
97+ }
98+
8999void Sensors::param_change_callback (uint16_t param_id)
90100{
91101 switch (param_id) {
92- case PARAM_FC_ROLL :
93- case PARAM_FC_PITCH :
94- case PARAM_FC_YAW :
102+ case PARAM_IMU_ROLL :
103+ case PARAM_IMU_PITCH :
104+ case PARAM_IMU_YAW :
95105 init_imu ();
96106 break ;
107+ case PARAM_MAG_ROLL:
108+ case PARAM_MAG_PITCH:
109+ case PARAM_MAG_YAW:
110+ init_mag ();
111+ break ;
97112 case PARAM_BATTERY_VOLTAGE_MULTIPLIER:
98113 case PARAM_BATTERY_CURRENT_MULTIPLIER:
99114 update_battery_monitor_multipliers ();
@@ -135,14 +150,28 @@ void Sensors::rotate_imu_in_place(ImuStruct * imu, turbomath::Quaternion rotatio
135150 imu->gyro [2 ] = gyro.z ;
136151}
137152
153+ void Sensors::rotate_mag_in_place (MagStruct * mag, turbomath::Quaternion rotation)
154+ {
155+ turbomath::Vector flux;
156+ flux.x = mag->flux [0 ];
157+ flux.y = mag->flux [1 ];
158+ flux.z = mag->flux [2 ];
159+
160+ flux = rotation * flux;
161+
162+ mag->flux [0 ] = flux.x ;
163+ mag->flux [1 ] = flux.y ;
164+ mag->flux [2 ] = flux.z ;
165+ }
166+
138167got_flags Sensors::run ()
139168{
140169 got_flags got = {};
141170
142171 // IMU:
143172 if ((got.imu = rf_.board_ .imu_read (&imu_))) {
144173 rf_.state_manager_ .clear_error (StateManager::ERROR_IMU_NOT_RESPONDING);
145- rotate_imu_in_place (&imu_, fcu_orientation_ );
174+ rotate_imu_in_place (&imu_, imu_orientation_ );
146175
147176 if (calibrating_acc_flag_) { calibrate_accel (); }
148177 if (calibrating_gyro_flag_) { calibrate_gyro (); }
@@ -160,7 +189,10 @@ got_flags Sensors::run()
160189 }
161190
162191 // MAGNETOMETER:
163- if ((got.mag = rf_.board_ .mag_read (&mag_))) { correct_mag (); }
192+ if ((got.mag = rf_.board_ .mag_read (&mag_))) {
193+ rotate_mag_in_place (&mag_, mag_orientation_);
194+ correct_mag ();
195+ }
164196
165197 // DIFF_PRESSURE:
166198 if ((got.diff_pressure = rf_.board_ .diff_pressure_read (&diff_pressure_))) {
0 commit comments