-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflash.ino
109 lines (88 loc) · 3.19 KB
/
flash.ino
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//--------------------------------------------------------
// Evil Minion 5 Axis robot firmware
// 2015 September 3
// see http://evilminion.info/ for more information.
//--------------------------------------------------------
#include "config.h"
#include "sensor.h"
#include <EEPROM.h>
//------------------------------------------------------------------------------
// from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234477290/3
void EEPROM_writeLong(int ee, long value) {
byte* p = (byte*)(void*)&value;
for (int i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
}
//------------------------------------------------------------------------------
// from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234477290/3
long EEPROM_readLong(int ee) {
long value = 0;
byte* p = (byte*)(void*)&value;
for (int i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return value;
}
//------------------------------------------------------------------------------
// from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234477290/3
void EEPROM_writeFloat(int ee, float value) {
byte* p = (byte*)(void*)&value;
for (int i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
}
//------------------------------------------------------------------------------
// from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234477290/3
float EEPROM_readFloat(int ee) {
float value = 0;
byte* p = (byte*)(void*)&value;
for (int i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return value;
}
//------------------------------------------------------------------------------
void loadConfig() {
char version_number=EEPROM.read(ADDR_VERSION);
if(version_number!=EEPROM_VERSION) {
// If not the current EEPROM_VERSION or the EEPROM_VERSION is sullied (i.e. unknown data)
// Update the version number
EEPROM.write(ADDR_VERSION,EEPROM_VERSION);
// upgrade path from one version to the next, if needed.
if(version_number==0) {
// Update sensor values
sensor_firstTime();
saveAdjustments();
// Update robot uid
robot_uid=0;
saveUID();
} else {
// Code should not get here if it does we should display some meaningful error message
Serial.println(F("An Error Occurred during LoadConfig"));
}
}
robot_uid = EEPROM_readLong(ADDR_GUID);
sensors_adjust[0] = EEPROM_readFloat(ADDR_ADJ_A);
sensors_adjust[1] = EEPROM_readFloat(ADDR_ADJ_B);
sensors_adjust[2] = EEPROM_readFloat(ADDR_ADJ_C);
sensors_adjust[3] = EEPROM_readFloat(ADDR_ADJ_D);
sensors_adjust[4] = EEPROM_readFloat(ADDR_ADJ_E);
Serial.println(F("Calibration loaded."));
}
/**
* Save the sensor adjustments (calibration offsets)
*/
void saveAdjustments() {
EEPROM_writeFloat(ADDR_ADJ_A,sensors_adjust[0]);
EEPROM_writeFloat(ADDR_ADJ_B,sensors_adjust[1]);
EEPROM_writeFloat(ADDR_ADJ_C,sensors_adjust[2]);
EEPROM_writeFloat(ADDR_ADJ_D,sensors_adjust[3]);
EEPROM_writeFloat(ADDR_ADJ_E,sensors_adjust[4]);
Serial.println(F("Calibration saved."));
}
/**
* Save the robot's unique ID
*/
void saveUID() {
Serial.print(F("New UID="));
Serial.println(robot_uid);
EEPROM_writeLong(ADDR_GUID,robot_uid);
}