@@ -303,7 +303,7 @@ biodyn_imu_err_t biodyn_imu_icm20948_init()
303303 if (err != ESP_OK )
304304 {
305305 biodyn_imu_icm20948_add_error_to_subsystem (err , "ICM_20948_INIT: error in completing self-test. Further investigation into self-test part functions likely required." );
306- ESP_LOGE (TAG , "Failed to initialize and start AK09916 (magnetometer) device , error %d" , err );
306+ ESP_LOGE (TAG , "Failed self_test for icm20948, investiage into composite function parts of self_test , error: %d" , err );
307307 return BIODYN_IMU_ERR_COULDNT_INIT_SPI_DEV ;
308308 }
309309 // TODO: write self_tests for magnetometer
@@ -623,6 +623,107 @@ static biodyn_imu_err_t self_test_accel_gyro()
623623 deltas .gyro_z = imd_st_on .gyro_z - imd_st_off .gyro_z ;
624624
625625 // Obtain factory self_test results from icm20948 self_test registers to compare to.
626+ uint8_t trimax = 0 ;
627+ uint8_t trimay = 0 ;
628+ uint8_t trimaz = 0 ;
629+ uint8_t trimgx = 0 ;
630+ uint8_t trimgy = 0 ;
631+ uint8_t trimgz = 0 ;
632+
633+ biodyn_imu_icm20948_read_reg (_b1 , SELF_TEST_X_ACCEL , & trimax );
634+ biodyn_imu_icm20948_read_reg (_b1 , SELF_TEST_Y_ACCEL , & trimay );
635+ biodyn_imu_icm20948_read_reg (_b1 , SELF_TEST_Z_ACCEL , & trimaz );
636+ biodyn_imu_icm20948_read_reg (_b1 , SELF_TEST_X_GYRO , & trimgx );
637+ biodyn_imu_icm20948_read_reg (_b1 , SELF_TEST_Y_GYRO , & trimgy );
638+ biodyn_imu_icm20948_read_reg (_b1 , SELF_TEST_Z_GYRO , & trimgz );
639+
640+ // Compare trims to with a range limiter to deltas
641+ // Compare within magnitude by 50% range (might need to be adjusted)
642+ float RL = 0.5 ;
643+ float RH = 1.5 ;
644+ /** DEBUG*/
645+ ESP_LOGI (TAG , "\n st_off ax: %f\n st_off ay: %f\n st_off az: %f\n st_off gx: %f\n st_off gy: %f\n st_off gz: %f" , imd_st_off .accel_x , imd_st_off .accel_y , imd_st_off .accel_z , imd_st_off .gyro_x , imd_st_off .gyro_y , imd_st_off .gyro_z );
646+ ESP_LOGI (TAG , "\n st_on ax: %f\n st_on ay: %f\n st_on az: %f\n st_on gx: %f\n st_on gy: %f\n st_on gz: %f" , imd_st_on .accel_x , imd_st_on .accel_y , imd_st_on .accel_z , imd_st_on .gyro_x , imd_st_on .gyro_y , imd_st_on .gyro_z );
647+
648+ ESP_LOGI (TAG , "\n delta ax: %f\n delta ay: %f\n delta az: %f\n delta gx: %f\n delta gy: %f\n delta gz: %f" , deltas .accel_x , deltas .accel_y , deltas .accel_z , deltas .gyro_x , deltas .gyro_y , deltas .gyro_z );
649+ ESP_LOGI (TAG , "\n trimax: %d\ntrimay: %d\n trimaz: %d\n trimgx: %d\n trimgy: %d\n trimgz: %d" , trimax , trimay , trimaz , trimgx , trimgy , trimgz );
650+ /** DEBUG */
651+
652+ // CHECK if deltas-trim is between rangel*trim and rangeh*trim
653+ // return error on fails
654+ ESP_LOGI (TAG , "checking against trims" );
655+ if (RL * trimax < deltas .accel_x / (1 << accel_range_value ) && RH * trimax > deltas .accel_x / (1 << accel_range_value ))
656+ {
657+ ESP_LOGI (TAG , "Self-test succesful for accel_gyro on ax" );
658+ }
659+ else
660+ {
661+ ESP_LOGE (TAG , "self test failed on delta ax with ax = %f and trimax = %d" , deltas .accel_x , trimax );
662+ biodyn_imu_icm20948_add_error_to_subsystem (BIODYN_IMU_ERR_COULDNT_CONFIGURE , "SELF_TEST_ACCEL_GYRO: failed self-test on ax" );
663+ return BIODYN_IMU_ERR_COULDNT_CONFIGURE ;
664+ }
665+ if (RL * trimay < deltas .accel_y / (1 << accel_range_value ) && RH * trimay > deltas .accel_y / (1 << accel_range_value ))
666+ {
667+ ESP_LOGI (TAG , "Self-test succesful for accel_gyro on ay" );
668+ }
669+ else
670+ {
671+ ESP_LOGE (TAG , "self test failed on delta ay" );
672+
673+ biodyn_imu_icm20948_add_error_to_subsystem (BIODYN_IMU_ERR_COULDNT_CONFIGURE , "SELF_TEST_ACCEL_GYRO: failed self-test on ay" );
674+ return BIODYN_IMU_ERR_COULDNT_CONFIGURE ;
675+ }
676+ if (RL * trimaz < deltas .accel_z / (1 << accel_range_value ) && RH * trimaz > deltas .accel_z / (1 << accel_range_value ))
677+ {
678+ ESP_LOGI (TAG , "Self-test succesful for accel_gyro on az" );
679+ }
680+ else
681+ {
682+ ESP_LOGE (TAG , "self test failed on delta az" );
683+ biodyn_imu_icm20948_add_error_to_subsystem (BIODYN_IMU_ERR_COULDNT_CONFIGURE , "SELF_TEST_ACCEL_GYRO: failed self-test on az" );
684+ return BIODYN_IMU_ERR_COULDNT_CONFIGURE ;
685+ }
686+ if (RL * trimgx < deltas .gyro_x / (1 << gyro_range_value ) && RH * trimgx > deltas .gyro_x / (1 << gyro_range_value ))
687+ {
688+ ESP_LOGI (TAG , "Self-test succesful for accel_gyro on gx" );
689+ }
690+ else
691+ {
692+ uint8_t and1 = RL * trimgx < deltas .gyro_x / (1 << gyro_range_value );
693+ uint8_t and2 = RH * trimgx > deltas .gyro_x / (1 << gyro_range_value );
694+ ESP_LOGE (TAG , "failed with delta_gx = %f and trimgx = %d" , deltas .gyro_x , trimgx );
695+ ESP_LOGE (TAG , "and1 = %d; and2 = %d" , and1 , and2 );
696+ biodyn_imu_icm20948_add_error_to_subsystem (BIODYN_IMU_ERR_COULDNT_CONFIGURE , "SELF_TEST_ACCEL_GYRO: failed self-test on gx" );
697+ return BIODYN_IMU_ERR_COULDNT_CONFIGURE ;
698+ }
699+ if (RL * trimgy < deltas .gyro_y / (1 << gyro_range_value ) && RH * trimgy > deltas .gyro_y / (1 << gyro_range_value ))
700+ {
701+ ESP_LOGI (TAG , "Self-test succesful for accel_gyro on gy" );
702+ }
703+ else
704+ {
705+ uint8_t and1 = RL * trimgy < deltas .gyro_y / (1 << gyro_range_value );
706+ uint8_t and2 = RH * trimgy > deltas .gyro_y / (1 << gyro_range_value );
707+ ESP_LOGE (TAG , "failed with delta_gx = %f and trimgx = %d" , deltas .gyro_y , trimgy );
708+ ESP_LOGE (TAG , "and1 = %d; and2 = %d" , and1 , and2 );
709+ biodyn_imu_icm20948_add_error_to_subsystem (BIODYN_IMU_ERR_COULDNT_CONFIGURE , "SELF_TEST_ACCEL_GYRO: failed self-test on gy" );
710+ return BIODYN_IMU_ERR_COULDNT_CONFIGURE ;
711+ }
712+ if (RL * trimgz < deltas .gyro_z / (1 << gyro_range_value ) && RH * trimgz > deltas .gyro_z / (1 << gyro_range_value ))
713+ {
714+ ESP_LOGI (TAG , "Self-test succesful for accel_gyro on gz" );
715+ }
716+ else
717+ {
718+ uint8_t and1 = RL * trimgz < deltas .gyro_z / (1 << gyro_range_value );
719+ uint8_t and2 = RH * trimgz > deltas .gyro_z / (1 << gyro_range_value );
720+ ESP_LOGE (TAG , "failed with delta_gx = %f and trimgx = %d" , deltas .gyro_z , trimgy );
721+ ESP_LOGE (TAG , "and1 = %d; and2 = %d" , and1 , and2 );
722+
723+ biodyn_imu_icm20948_add_error_to_subsystem (BIODYN_IMU_ERR_COULDNT_CONFIGURE , "SELF_TEST_ACCEL_GYRO: failed self-test on gz" );
724+ return BIODYN_IMU_ERR_COULDNT_CONFIGURE ;
725+ }
726+ ESP_LOGI (TAG , "Finished checking against trims" );
626727
627728 // Clear self_test bits
628729 // ACCEL SELF_TEST END
@@ -642,8 +743,8 @@ static biodyn_imu_err_t self_test_accel_gyro()
642743 return BIODYN_IMU_OK ;
643744}
644745
645- // TODO: also check gyro self-test
646- static biodyn_imu_err_t self_test_accel ()
746+ // TESTING accel_gyro replacement
747+ /* static biodyn_imu_err_t self_test_accel()
647748{
648749 // SELF-TEST RESPONSE = SENSOR OUTPUT WITH SELF-TEST ENABLED – SENSOR OUTPUT WITHOUT SELF-TEST ENABLED (p. 24)
649750 // Sensor output without self_test_enabled
@@ -661,11 +762,7 @@ static biodyn_imu_err_t self_test_accel()
661762 return BIODYN_IMU_OK;
662763}
663764
664- // TODO: Fix this self-test
665- /**
666- * Self-test the gyroscope of the IMU.
667- * Checks the built-in self-test in the IMU, which identifies if it's gyroscope is working.
668- */
765+
669766static biodyn_imu_err_t self_test_gyro()
670767{
671768 biodyn_imu_err_t err;
@@ -692,6 +789,7 @@ static biodyn_imu_err_t self_test_gyro()
692789
693790 return BIODYN_IMU_OK;
694791}
792+ */
695793
696794/**
697795 * Self-test the magnetometer of the IMU.
@@ -718,6 +816,7 @@ static biodyn_imu_err_t self_test_mag()
718816 biodyn_imu_icm20948_read_reg (_b0 , EXT_SLV_SENS_DATA_00 , & temp );
719817 if (temp & 0b11111111 )
720818 return BIODYN_IMU_OK ;
819+ ESP_LOGE (TAG , "SELF_TEST_MAG: Failed self-test with read value test actual: %x, expected: non-zero" , temp );
721820 biodyn_imu_icm20948_add_error_to_subsystem (BIODYN_IMU_ERR_COULDNT_CONFIGURE , "SELF_TEST_MAG: Failed self-test" );
722821 return BIODYN_IMU_ERR_COULDNT_CONFIGURE ;
723822}
@@ -738,28 +837,45 @@ biodyn_imu_err_t biodyn_imu_icm20948_self_test()
738837 ESP_LOGE (TAG , "Self test failed - whoami (%x)" , err );
739838 return err ;
740839 }
840+ ESP_LOGI (TAG , "Correct WHOAMI" );
841+
741842 // Check that user bank selection works
742843 if ((err = self_test_user_banks ()))
743844 {
744845 ESP_LOGE (TAG , "Self test failed - user banks (%x)" , err );
745846 return err ;
746847 }
747- // Run gyro self-test
748- if ((err = self_test_gyro ()))
749- {
750- ESP_LOGE (TAG , "Self test failed - gyro (%x)" , err );
751- }
752- // Run accel self-test
753- if ((err = self_test_accel ()))
754- {
755- ESP_LOGE (TAG , "Self test failed - accel (%x)" , err );
756- }
848+ // // Run gyro self-test
849+ // if ((err = self_test_gyro()))
850+ // {
851+ // ESP_LOGE(TAG, "Self test failed - gyro (%x)", err);
852+ // }
853+ // // Run accel self-test
854+ // if ((err = self_test_accel()))
855+ // {
856+ // ESP_LOGE(TAG, "Self test failed - accel (%x)", err);
857+ // }
858+
859+ // Run accel and gyro self-test
860+ // TODO: REPAIR
861+ // if ((err = self_test_accel_gyro()))
862+ // {
863+ // ESP_LOGE(TAG, "Self test failed - accel and gyro (%x)", err);
864+ // }
757865
758866 // Run mag self-test
867+ <<<<<<< HEAD
759868 // if ((err = self_test_mag()))
760869 // {
761870 // ESP_LOGE(TAG, "Self test failed - mag (%x)", err);
762871 // }
872+ == = == ==
873+ if ((err = self_test_mag ()))
874+ {
875+ ESP_LOGE (TAG , "Self test failed - mag (%x)" , err );
876+ return err ;
877+ }
878+ >>>>>>> imu_subsystems
763879
764880 return BIODYN_IMU_OK ;
765881}
0 commit comments