Caddy
A 2005 Roborodentia entry with vision and path planning capability
 All Data Structures Files Functions Variables Typedefs Macros Pages
tweak_data.c
Go to the documentation of this file.
1 /*
2  * This file is part of Caddy.
3  *
4  * Caddy is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Caddy is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Caddy. If not, see <http://www.gnu.org/licenses/>.
16  */
18 #include "tweak_data.h"
19 
20 // avr-libc
21 #include <avr/io.h>
22 #include <avr/interrupt.h>
23 
24 // Global variables - Runtime configurable parameters
25 uint8_t l_base;
26 uint8_t r_base;
27 uint16_t slopeCoef;
28 uint16_t offCoef;
29 uint8_t dampCoef;
30 uint8_t lineCenter;
31 uint8_t turnPoint;
32 uint8_t turnSubtract;
33 int8_t panOffset;
34 int8_t tiltOffset;
35 uint16_t tractorOvershootDelay;
36 uint8_t tempTweak1;
37 int8_t tempTweak2;
38 uint16_t tempTweak3;
39 uint16_t tempTweak4;
40 
41 uint8_t lcdMode; // <- need debugger menu for this, remove old init/toggling, and save in EEPROM
42 uint8_t testMode; // <- need to save this in EEPROM
43 
44 static uint8_t EEPROM_read(unsigned int uiAddress);
45 static void EEPROM_write(unsigned int uiAddress, uint8_t ucData);
46 
47 // Initializes constants that can be tweaked by debugger
48 inline void loadTweakValues(void)
49 {
50  cli(); // disable all interrupts
51 
52  // EEPROM Reads
53  l_base = EEPROM_read(EE_ADDR_LEFT_BASE);
54  r_base = EEPROM_read(EE_ADDR_RIGHT_BASE);
55  slopeCoef = (EEPROM_read(EE_ADDR_SLOPE_COEF) << 8) +
56  EEPROM_read(EE_ADDR_SLOPE_COEF + 1);
57  offCoef = (EEPROM_read(EE_ADDR_OFF_COEF) << 8) +
58  EEPROM_read(EE_ADDR_OFF_COEF + 1);
59  dampCoef = EEPROM_read(EE_ADDR_DAMP_COEF);
60  lineCenter = EEPROM_read(EE_ADDR_LINE_X_CENTER);
61  turnPoint = EEPROM_read(EE_ADDR_TURN_POINT);
62  turnSubtract = EEPROM_read(EE_ADDR_TURN_SUBTRACT);
63  panOffset = EEPROM_read(EE_ADDR_PAN_OFFSET);
64  tiltOffset = EEPROM_read(EE_ADDR_TILT_OFFSET);
65  tractorOvershootDelay = (EEPROM_read(EE_ADDR_TRACTOR_OVERSHOOT_DELAY) << 8) +
66  EEPROM_read(EE_ADDR_TRACTOR_OVERSHOOT_DELAY + 1);
67  testMode = EEPROM_read(EE_ADDR_TEST_MODE);
68  tempTweak1 = EEPROM_read(EE_ADDR_TEMP_TWEAK1);
69  tempTweak2 = EEPROM_read(EE_ADDR_TEMP_TWEAK2);
70  tempTweak3 = (EEPROM_read(EE_ADDR_TEMP_TWEAK3) << 8) +
71  EEPROM_read(EE_ADDR_TEMP_TWEAK3 + 1);
72  tempTweak4 = (EEPROM_read(EE_ADDR_TEMP_TWEAK4) << 8) +
73  EEPROM_read(EE_ADDR_TEMP_TWEAK4 + 1);
74 
75  sei(); // enable all interrupts
76 }
77 
78 // Saves constants after they have been changed by the debugger
79 inline void storeTweakValues(void)
80 {
81  cli(); // disable all interrupts
82 
83  // EEPROM writes
84  EEPROM_write(EE_ADDR_LEFT_BASE, l_base);
85  EEPROM_write(EE_ADDR_RIGHT_BASE, r_base);
86  EEPROM_write(EE_ADDR_SLOPE_COEF, slopeCoef >> 8);
87  EEPROM_write(EE_ADDR_SLOPE_COEF + 1, slopeCoef);
88  EEPROM_write(EE_ADDR_OFF_COEF, offCoef >> 8);
89  EEPROM_write(EE_ADDR_OFF_COEF + 1, offCoef);
90  EEPROM_write(EE_ADDR_DAMP_COEF, dampCoef);
91  EEPROM_write(EE_ADDR_LINE_X_CENTER, lineCenter);
92  EEPROM_write(EE_ADDR_TURN_POINT, turnPoint);
93  EEPROM_write(EE_ADDR_TURN_SUBTRACT, turnSubtract);
94  EEPROM_write(EE_ADDR_PAN_OFFSET, panOffset);
95  EEPROM_write(EE_ADDR_TILT_OFFSET, tiltOffset);
96  EEPROM_write(EE_ADDR_TRACTOR_OVERSHOOT_DELAY, tractorOvershootDelay >> 8);
97  EEPROM_write(EE_ADDR_TRACTOR_OVERSHOOT_DELAY + 1, tractorOvershootDelay);
98  EEPROM_write(EE_ADDR_TEST_MODE, testMode);
99  EEPROM_write(EE_ADDR_TEMP_TWEAK1, tempTweak1);
100  EEPROM_write(EE_ADDR_TEMP_TWEAK2, tempTweak2);
101  EEPROM_write(EE_ADDR_TEMP_TWEAK3, tempTweak3 >> 8);
102  EEPROM_write(EE_ADDR_TEMP_TWEAK3 + 1, tempTweak3);
103  EEPROM_write(EE_ADDR_TEMP_TWEAK4, tempTweak4 >> 8);
104  EEPROM_write(EE_ADDR_TEMP_TWEAK4 + 1, tempTweak4);
105 
106  sei(); // enable all interrupts
107 }
108 
109 uint8_t EEPROM_read(unsigned int uiAddress)
110 {
111  // Wait for completion of previous write
112  while (EECR & (1 << EEWE)) ;
113  // Set up address register
114  EEAR = uiAddress;
115  // Start eeprom read by writing EERE
116  EECR |= (1 << EERE);
117  // Return data from data register
118  return EEDR;
119 }
120 
121 void EEPROM_write(unsigned int uiAddress, uint8_t ucData)
122 {
123  // Wait for completion of previous write
124  while (EECR & (1 << EEWE)) ;
125  // Set up address and data registers
126  EEAR = uiAddress;
127  EEDR = ucData;
128  // Write logical one to EEMWE
129  EECR |= (1 << EEMWE);
130  // Start eeprom write by setting EEWE
131  EECR |= (1 << EEWE);
132  // EEMWE and EEWE are automatically cleared back to 0 by hardware
133 }