forked from Lauszus/BalancingRobotFullSize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EEPROM.cpp
76 lines (58 loc) · 2.23 KB
/
EEPROM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved.
This software may be distributed and modified under the terms of the GNU
General Public License version 2 (GPL2) as published by the Free Software
Foundation and appearing in the file GPL2.TXT included in the packaging of
this file. Please note that GPL2 Section 2[b] requires that all works based
on this software must also be made publicly available under the terms of
the GPL2 ("Copyleft").
Contact information
-------------------
Kristian Lauszus, TKJ Electronics
Web : http://www.tkjelectronics.com
e-mail : kristianl@tkjelectronics.com
*/
#include "EEPROM.h"
#include "EEPROMAnything.h"
#include "IMU.h"
cfg_t cfg; // Struct for all the configuration values
//static const char *firmwareVersion = "1.0.0";
static const uint8_t eepromVersion = 2; // EEPROM version - used to restore the EEPROM values if the configuration struct have changed
/* EEPROM Address Definitions */
static const uint8_t versionAddr = 0; // Set the first byte to the EEPROM version
static const uint8_t configAddr = 1; // Save the configuration starting from this location
void restoreEEPROMValues();
bool checkEEPROMVersion() {
uint8_t version;
EEPROM_readAnything(versionAddr, version);
if (version != eepromVersion) { // Check if the EEPROM version matches the current one
restoreEEPROMValues();
EEPROM_updateAnything(versionAddr, eepromVersion); // After the default values have been restored, set the flag
return true; // Indicate that the values have been restored
}
return false;
}
void readEEPROMValues() {
EEPROM_readAnything(configAddr, cfg);
kalmanPitch.setQangle(cfg.Qangle);
kalmanPitch.setQbias(cfg.Qbias);
kalmanPitch.setRmeasure(cfg.Rmeasure);
}
void updateEEPROMValues() {
EEPROM_updateAnything(configAddr, cfg);
kalmanPitch.setQangle(cfg.Qangle);
kalmanPitch.setQbias(cfg.Qbias);
kalmanPitch.setRmeasure(cfg.Rmeasure);
}
void restoreEEPROMValues() {
cfg.Kp = 2.8;
cfg.Ki = 2.0;
cfg.Kd = 3.5;
cfg.targetAngle = 0.0;
cfg.turningScale = 40.0;
cfg.Qangle = 0.001;
cfg.Qbias = 0.003;
cfg.Rmeasure = 0.03;
cfg.accYzero = cfg.accZzero = 0;
cfg.leftMotorScaler = cfg.rightMotorScaler = 1;
updateEEPROMValues();
}