@@ -50,6 +50,9 @@ GET HEADING
5050GET WD
5151*/
5252
53+ #include < Arduino.h>
54+ #include < EEPROM.h>
55+
5356// function to get sign of a value
5457#define sgn (x ) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0 ))
5558
@@ -125,6 +128,7 @@ volatile bool newDataAvailable = false;
125128void setup () {
126129 // Initialize serial communication
127130 Serial.begin (115200 );
131+ EEPROM.begin ();
128132
129133 // Initialize motor control pins
130134 pinMode (motorAPin1, OUTPUT);
@@ -154,9 +158,14 @@ void setup() {
154158 // use precalibration values to start with:
155159 // compass.setCalibrationOffsets(-69.00, 523.00, -907.00);
156160 // compass.setCalibrationScales(0.97, 1.00, 1.03);
161+
162+ #if 0 // old hardcoded calibration
157163 compass.setCalibrationOffsets(294.00,-176.00,-794.00);
158164 compass.setCalibrationScales(0.90,0.80,1.54);
159-
165+ #else // new calibration as restored from EEPROM
166+ EEPROM.get (0 , calibVal);
167+ restoreCompassCalibration (calibVal);
168+ #endif
160169 compass.setSmoothing (4 ,true ); // set smoothing to max (1..10)
161170
162171 // Initialize DRDY pin and interrupt
@@ -300,10 +309,8 @@ void setupTimer1PWM() {
300309 OCR1B = 0 ;
301310}
302311
303-
304- FloatArray calibrateCompass (){
312+ void calibrateCompass (FloatArray &data) {
305313 // float calib[6];
306- FloatArray data;
307314 // make sure vehicle moves
308315 speedCommand=255 ;// 128;//max speed
309316 nav_state=go_left;// to turn on itself
@@ -327,7 +334,12 @@ FloatArray calibrateCompass(){
327334 compass.setSmoothing (3 ,true );
328335 // Serial.println("CALIBRATION DONE.");
329336 nav_state=stop_engine;
330- return data;
337+ }
338+
339+ static void restoreCompassCalibration (FloatArray &data)
340+ {
341+ compass.setCalibrationOffsets (data.values [0 ], data.values [1 ], data.values [2 ]);
342+ compass.setCalibrationScales (data.values [3 ], data.values [4 ], data.values [5 ]);
331343}
332344
333345void handlePWMcommand (String command) {
@@ -431,7 +443,8 @@ void handleWDcommand(String command) {
431443void handleCALIBcommand () {
432444 if (motor_enable == true ) {
433445 mode_pwm=false ;
434- calibVal = calibrateCompass ();// can take 10sec
446+ calibrateCompass (calibVal);// can take 10sec
447+ EEPROM.put (0 , calibVal);
435448 Serial.println (" OK" );
436449 } else { // motor is not enable, calibration will fail because of vehicle not moving
437450 Serial.println (" ERR" );
0 commit comments