@@ -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
@@ -126,6 +129,7 @@ void setup()
126129{
127130 // Initialize serial communication
128131 Serial.begin (115200 );
132+ EEPROM.begin ();
129133
130134 // Initialize motor control pins
131135 pinMode (motorAPin1, OUTPUT);
@@ -155,9 +159,13 @@ void setup()
155159 // use precalibration values to start with:
156160 // compass.setCalibrationOffsets(-69.00, 523.00, -907.00);
157161 // compass.setCalibrationScales(0.97, 1.00, 1.03);
162+ #if 0 // old hardcoded calibration
158163 compass.setCalibrationOffsets(294.00, -176.00, -794.00);
159164 compass.setCalibrationScales(0.90, 0.80, 1.54);
160-
165+ #else
166+ EEPROM.get (0 , calibVal);
167+ restoreCompassCalibration (calibVal);
168+ #endif
161169 compass.setSmoothing (4 , true ); // set smoothing to max (1..10)
162170
163171 // Initialize DRDY pin and interrupt
@@ -305,10 +313,8 @@ void setupTimer1PWM()
305313}
306314
307315
308- FloatArray calibrateCompass ()
316+ void calibrateCompass (FloatArray & data )
309317{
310- // float calib[6];
311- FloatArray data;
312318 // make sure vehicle moves
313319 speedCommand = 255 ; // 128;//max speed
314320 nav_state = go_left; // to turn on itself
@@ -332,7 +338,12 @@ FloatArray calibrateCompass()
332338 compass.setSmoothing (3 , true );
333339 // Serial.println("CALIBRATION DONE.");
334340 nav_state = stop_engine;
335- return data;
341+ }
342+
343+ static void restoreCompassCalibration (FloatArray & data)
344+ {
345+ compass.setCalibrationOffsets (data.values [0 ], data.values [1 ], data.values [2 ]);
346+ compass.setCalibrationScales (data.values [3 ], data.values [4 ], data.values [5 ]);
336347}
337348
338349void handlePWMcommand (String command)
@@ -442,7 +453,8 @@ void handleCALIBcommand()
442453{
443454 if (motor_enable == true ) {
444455 mode_pwm = false ;
445- calibVal = calibrateCompass (); // can take 10sec
456+ calibrateCompass (calibVal); // can take 10sec
457+ EEPROM.put (0 , calibVal);
446458 Serial.println (" OK" );
447459 } else { // motor is not enable, calibration will fail because of vehicle not moving
448460 Serial.println (" ERR" );
0 commit comments