@ -7,6 +7,12 @@ using namespace EKFS; 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					using  namespace  Eigen ;  
					 
					 
					 
					using  namespace  Eigen ;  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					ExitHandler  do_exit ;  
					 
					 
					 
					ExitHandler  do_exit ;  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					const  double  ACCEL_SANITY_CHECK  =  100.0 ;   // m/s^2
  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					const  double  ROTATION_SANITY_CHECK  =  10.0 ;   // rad/s
  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					const  double  TRANS_SANITY_CHECK  =  200.0 ;   // m/s
  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					const  double  CALIB_RPY_SANITY_CHECK  =  0.5 ;  // rad (+- 30 deg)
  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					const  double  ALTITUDE_SANITY_CHECK  =  10000 ;  // m
  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					const  double  MIN_STD_SANITY_CHECK  =  1e-5 ;  // m or rad
  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					static  VectorXd  floatlist2vector ( const  capnp : : List < float ,  capnp : : Kind : : PRIMITIVE > : : Reader &  floatlist )  {  
					 
					 
					 
					static  VectorXd  floatlist2vector ( const  capnp : : List < float ,  capnp : : Kind : : PRIMITIVE > : : Reader &  floatlist )  {  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  VectorXd  res ( floatlist . size ( ) ) ;   
					 
					 
					 
					  VectorXd  res ( floatlist . size ( ) ) ;   
				
			 
			
		
	
	
		
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
					 
					@ -183,7 +189,10 @@ void Localizer::handle_sensors(double current_time, const capnp::List<cereal::Se 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    // Gyro Uncalibrated
   
					 
					 
					 
					    // Gyro Uncalibrated
   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    if  ( sensor_reading . getSensor ( )  = =  SENSOR_GYRO_UNCALIBRATED  & &  sensor_reading . getType ( )  = =  SENSOR_TYPE_GYROSCOPE_UNCALIBRATED )  {   
					 
					 
					 
					    if  ( sensor_reading . getSensor ( )  = =  SENSOR_GYRO_UNCALIBRATED  & &  sensor_reading . getType ( )  = =  SENSOR_TYPE_GYROSCOPE_UNCALIBRATED )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      auto  v  =  sensor_reading . getGyroUncalibrated ( ) . getV ( ) ;   
					 
					 
					 
					      auto  v  =  sensor_reading . getGyroUncalibrated ( ) . getV ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      this - > kf - > predict_and_observe ( sensor_time ,  OBSERVATION_PHONE_GYRO ,  {  Vector3d ( - v [ 2 ] ,  - v [ 1 ] ,  - v [ 0 ] )  } ) ;   
					 
					 
					 
					      auto  meas  =  Vector3d ( - v [ 2 ] ,  - v [ 1 ] ,  - v [ 0 ] ) ;   
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					      if  ( meas . norm ( )  <  ROTATION_SANITY_CHECK )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        this - > kf - > predict_and_observe ( sensor_time ,  OBSERVATION_PHONE_GYRO ,  {  meas  } ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					      }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    }   
					 
					 
					 
					    }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    // Accelerometer
   
					 
					 
					 
					    // Accelerometer
   
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -194,7 +203,10 @@ void Localizer::handle_sensors(double current_time, const capnp::List<cereal::Se 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      // 40m/s**2 is a good filter for falling detection, no false positives in 20k minutes of driving
   
					 
					 
					 
					      // 40m/s**2 is a good filter for falling detection, no false positives in 20k minutes of driving
   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      this - > device_fell  | =  ( floatlist2vector ( v )  -  Vector3d ( 10.0 ,  0.0 ,  0.0 ) ) . norm ( )  >  40.0 ;   
					 
					 
					 
					      this - > device_fell  | =  ( floatlist2vector ( v )  -  Vector3d ( 10.0 ,  0.0 ,  0.0 ) ) . norm ( )  >  40.0 ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      this - > kf - > predict_and_observe ( sensor_time ,  OBSERVATION_PHONE_ACCEL ,  {  Vector3d ( - v [ 2 ] ,  - v [ 1 ] ,  - v [ 0 ] )  } ) ;   
					 
					 
					 
					      auto  meas  =  Vector3d ( - v [ 2 ] ,  - v [ 1 ] ,  - v [ 0 ] ) ;   
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					      if  ( meas . norm ( )  <  ACCEL_SANITY_CHECK )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        this - > kf - > predict_and_observe ( sensor_time ,  OBSERVATION_PHONE_ACCEL ,  {  meas  } ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					      }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    }   
					 
					 
					 
					    }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  }   
					 
					 
					 
					  }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					}  
					 
					 
					 
					}  
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -205,8 +217,21 @@ void Localizer::handle_gps(double current_time, const cereal::GpsLocationData::R 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    return ;   
					 
					 
					 
					    return ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  }   
					 
					 
					 
					  }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  this - > last_gps_fix  =  current_time ;   
					 
					 
					 
					  // Sanity checks
   
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  if  ( ( log . getVerticalAccuracy ( )  < =  0 )  | |  ( log . getSpeedAccuracy ( )  < =  0 )  | |  ( log . getBearingAccuracyDeg ( )  < =  0 ) )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    return ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  if  ( ( std : : abs ( log . getLatitude ( ) )  >  90 )  | |  ( std : : abs ( log . getLongitude ( ) )  >  180 )  | |  ( std : : abs ( log . getAltitude ( ) )  >  ALTITUDE_SANITY_CHECK ) )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    return ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  if  ( floatlist2vector ( log . getVNED ( ) ) . norm ( )  >  TRANS_SANITY_CHECK ) {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    return ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  // Process message
   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  this - > last_gps_fix  =  current_time ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  Geodetic  geodetic  =  {  log . getLatitude ( ) ,  log . getLongitude ( ) ,  log . getAltitude ( )  } ;   
					 
					 
					 
					  Geodetic  geodetic  =  {  log . getLatitude ( ) ,  log . getLongitude ( ) ,  log . getAltitude ( )  } ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  this - > converter  =  std : : make_unique < LocalCoord > ( geodetic ) ;   
					 
					 
					 
					  this - > converter  =  std : : make_unique < LocalCoord > ( geodetic ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
	
		
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
					 
					@ -255,9 +280,21 @@ void Localizer::handle_cam_odo(double current_time, const cereal::CameraOdometry 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  VectorXd  rot_device  =  this - > device_from_calib  *  floatlist2vector ( log . getRot ( ) ) ;   
					 
					 
					 
					  VectorXd  rot_device  =  this - > device_from_calib  *  floatlist2vector ( log . getRot ( ) ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  VectorXd  trans_device  =  this - > device_from_calib  *  floatlist2vector ( log . getTrans ( ) ) ;   
					 
					 
					 
					  VectorXd  trans_device  =  this - > device_from_calib  *  floatlist2vector ( log . getTrans ( ) ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  if  ( ( rot_device . norm ( )  >  ROTATION_SANITY_CHECK )  | |  ( trans_device . norm ( )  >  TRANS_SANITY_CHECK ) )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    return ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  VectorXd  rot_calib_std  =  floatlist2vector ( log . getRotStd ( ) ) ;   
					 
					 
					 
					  VectorXd  rot_calib_std  =  floatlist2vector ( log . getRotStd ( ) ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  VectorXd  trans_calib_std  =  floatlist2vector ( log . getTransStd ( ) ) ;   
					 
					 
					 
					  VectorXd  trans_calib_std  =  floatlist2vector ( log . getTransStd ( ) ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  if  ( ( rot_calib_std . minCoeff ( )  < =  MIN_STD_SANITY_CHECK )  | |  ( trans_calib_std . minCoeff ( )  < =  MIN_STD_SANITY_CHECK ) ) {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    return ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  if  ( ( rot_calib_std . norm ( )  >  10  *  ROTATION_SANITY_CHECK )  | |  ( trans_calib_std . norm ( )  >  10  *  TRANS_SANITY_CHECK ) )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    return ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					  }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  this - > posenet_stds . pop_front ( ) ;   
					 
					 
					 
					  this - > posenet_stds . pop_front ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  this - > posenet_stds . push_back ( trans_calib_std [ 0 ] ) ;   
					 
					 
					 
					  this - > posenet_stds . push_back ( trans_calib_std [ 0 ] ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -275,7 +312,12 @@ void Localizer::handle_cam_odo(double current_time, const cereal::CameraOdometry 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					void  Localizer : : handle_live_calib ( double  current_time ,  const  cereal : : LiveCalibrationData : : Reader &  log )  {  
					 
					 
					 
					void  Localizer : : handle_live_calib ( double  current_time ,  const  cereal : : LiveCalibrationData : : Reader &  log )  {  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					  if  ( log . getRpyCalib ( ) . size ( )  >  0 )  {   
					 
					 
					 
					  if  ( log . getRpyCalib ( ) . size ( )  >  0 )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    this - > calib  =  floatlist2vector ( log . getRpyCalib ( ) ) ;   
					 
					 
					 
					    auto  calib  =  floatlist2vector ( log . getRpyCalib ( ) ) ;   
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    if  ( ( calib . minCoeff ( )  <  - CALIB_RPY_SANITY_CHECK )  | |  ( calib . maxCoeff ( )  >  CALIB_RPY_SANITY_CHECK ) ) {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					      return ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    this - > calib  =  calib ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    this - > device_from_calib  =  euler2rot ( this - > calib ) ;   
					 
					 
					 
					    this - > device_from_calib  =  euler2rot ( this - > calib ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    this - > calib_from_device  =  this - > device_from_calib . transpose ( ) ;   
					 
					 
					 
					    this - > calib_from_device  =  this - > device_from_calib . transpose ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    this - > calibrated  =  log . getCalStatus ( )  = =  1 ;   
					 
					 
					 
					    this - > calibrated  =  log . getCalStatus ( )  = =  1 ;