code with calibration added

checks for stability to end the calibration stage