index.js
/*
* Accumulates sensor readings to compute the average offset for all 6 axes 
*/
Accumulates sensor readings to compute the average offset for all 6 axes
Accumulates sensor readings to compute the average offset for all 6 axes
function findAxesOffset(control_characteristic, data_characteristic, imu_sampling_rate) {
	logger.info("Starting reading data to find offset");
	var start = getStartSamplingCommand(imu_sampling_rate);
	control_characteristic.write(start, false, function (err) {
		if (!err) {
			logger.info("Written successfully to characteristic: " + control_characteristic.uuid);
			data_characteristic.subscribe(function (err) {
				if (!err) {
					sensor_notification_handler = function (data, isNotification) {
						if (utils.checkCheckSum(data, 2) == true) {
							var readings = utils.getSensorReadings(data);
							if (samples_count <= CAL_SAMPLES_COUNT) {
								gyro_sum_axis[0] += utils.convertGyroADCToDegPerSecond(readings[0], gyro_factor);
								gyro_sum_axis[1] += utils.convertGyroADCToDegPerSecond(readings[1], gyro_factor);
								gyro_sum_axis[2] += utils.convertGyroADCToDegPerSecond(readings[2], gyro_factor);
								acc_sum_axis[0] += utils.convertAccelADCToG(readings[3], acc_factor);
								acc_sum_axis[1] += utils.convertAccelADCToG(readings[4], acc_factor);
								acc_sum_axis[2] += utils.convertAccelADCToG(readings[5], acc_factor);
								samples_count++;
							}
							if (samples_count == CAL_SAMPLES_COUNT) {
								gx_offset = gyro_sum_axis[0] / samples_count;
								gy_offset = gyro_sum_axis[1] / samples_count;
								gz_offset = gyro_sum_axis[2] / samples_count;
								ax_offset = acc_sum_axis[0] / samples_count;
								ay_offset = acc_sum_axis[1] / samples_count;
								az_offset = 1 - (-acc_sum_axis[2] / samples_count);
								gyro_sum_axis = [0, 0, 0];
								acc_sum_axis = [0, 0, 0];
								samples_count = 0;
								io.sockets.emit('calibration', {});
								io.sockets.emit('status', { message: "Calibration finished." });
								io.sockets.emit('status', { message: "Gyro Offset x-axis (deg/s) " + gx_offset });
								io.sockets.emit('status', { message: "Gyro Offset y-axis (deg/s) " + gy_offset });
								io.sockets.emit('status', { message: "Gyro Offset z-axis (deg/s) " + gz_offset });
								io.sockets.emit('status', { message: "Acc Offset x-axis (g) " + ax_offset });
								io.sockets.emit('status', { message: "Acc Offset y-axis (g) " + ay_offset });
								io.sockets.emit('status', { message: "Acc Offset z-axis (g) " + az_offset });
								stopSensing(control_characteristic, data_characteristic);
							}
						} else {
							logger.error("Checksum failed!");
						}
					};
					data_characteristic.on('data', sensor_notification_handler);
					io.sockets.emit('status', { message: "Calibration of " + earbudName + " started." });
				}
			});
		}
		else logger.error("Error when starting IMU sampling");
	})
}
function findAxesOffset ( control_characteristic , data_characteristic , imu_sampling_rate ) { logger . info ( "Starting reading data to find offset" ) ; var start = getStartSamplingCommand ( imu_sampling_rate ) ; control_characteristic . write ( start , false , function ( err ) { if ( ! err ) { logger . info ( "Written successfully to characteristic: " + control_characteristic . uuid ) ; data_characteristic . subscribe ( function ( err ) { if ( ! err ) { sensor_notification_handler = function ( data , isNotification ) { if ( utils . checkCheckSum ( data , 2 ) == true ) { var readings = utils . getSensorReadings ( data ) ; if ( samples_count <= CAL_SAMPLES_COUNT ) { gyro_sum_axis [ 0 ] += utils . convertGyroADCToDegPerSecond ( readings [ 0 ] , gyro_factor ) ; gyro_sum_axis [ 1 ] += utils . convertGyroADCToDegPerSecond ( readings [ 1 ] , gyro_factor ) ; gyro_sum_axis [ 2 ] += utils . convertGyroADCToDegPerSecond ( readings [ 2 ] , gyro_factor ) ; acc_sum_axis [ 0 ] += utils . convertAccelADCToG ( readings [ 3 ] , acc_factor ) ; acc_sum_axis [ 1 ] += utils . convertAccelADCToG ( readings [ 4 ] , acc_factor ) ; acc_sum_axis [ 2 ] += utils . convertAccelADCToG ( readings [ 5 ] , acc_factor ) ; samples_count ++ ; } if ( samples_count == CAL_SAMPLES_COUNT ) { gx_offset = gyro_sum_axis [ 0 ] / samples_count ; gy_offset = gyro_sum_axis [ 1 ] / samples_count ; gz_offset = gyro_sum_axis [ 2 ] / samples_count ; ax_offset = acc_sum_axis [ 0 ] / samples_count ; ay_offset = acc_sum_axis [ 1 ] / samples_count ; az_offset = 1 - ( - acc_sum_axis [ 2 ] / samples_count ) ; gyro_sum_axis = [ 0 , 0 , 0 ] ; acc_sum_axis = [ 0 , 0 , 0 ] ; samples_count = 0 ; io . sockets . emit ( 'calibration' , { } ) ; io . sockets . emit ( 'status' , { message : "Calibration finished." } ) ; io . sockets . emit ( 'status' , { message : "Gyro Offset x-axis (deg/s) " + gx_offset } ) ; io . sockets . emit ( 'status' , { message : "Gyro Offset y-axis (deg/s) " + gy_offset } ) ; io . sockets . emit ( 'status' , { message : "Gyro Offset z-axis (deg/s) " + gz_offset } ) ; io . sockets . emit ( 'status' , { message : "Acc Offset x-axis (g) " + ax_offset } ) ; io . sockets . emit ( 'status' , { message : "Acc Offset y-axis (g) " + ay_offset } ) ; io . sockets . emit ( 'status' , { message : "Acc Offset z-axis (g) " + az_offset } ) ; stopSensing ( control_characteristic , data_characteristic ) ; } } else { logger . error ( "Checksum failed!" ) ; } } ; data_characteristic . on ( 'data' , sensor_notification_handler ) ; io . sockets . emit ( 'status' , { message : "Calibration of " + earbudName + " started." } ) ; } } ) ; } else logger . error ( "Error when starting IMU sampling" ) ; } ) }
Accumulates sensor readings to compute the average offset for all 6 axes
Accumulates sensor readings to compute the average offset for all 6 axes
// Accumulate gyro and accelerometer readings