├── README.md └── newthrottlecontrolFaST ├── ETC.h ├── README.md └── newthrottlecontrolFaST.ino /README.md: -------------------------------------------------------------------------------- 1 | # ETC_Control 2 | -------------------------------------------------------------------------------- /newthrottlecontrolFaST/ETC.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int FindMinTPS(int, int, int); 5 | int FindMaxTPS(int, int, int); 6 | unsigned int FindMinAPP(int); 7 | unsigned int FindMaxAPP(int); 8 | bool burnCalibration(int,int,int,int,int,int); 9 | bool clearCalibration(); 10 | 11 | int FindMinTPS(int HB_PWM, int HB_DIRECTION, int POT_THROTTLE){ 12 | digitalWrite(HB_DIRECTION, 1); 13 | analogWrite(HB_PWM, 90); 14 | 15 | delay(500); 16 | int MinTPSV = analogRead(POT_THROTTLE); 17 | MinTPSV = analogRead(POT_THROTTLE) - 10; 18 | Serial.println(MinTPSV); 19 | return MinTPSV; 20 | //closing 21 | } 22 | 23 | int FindMaxTPS(int HB_PWM, int HB_DIRECTION, int POT_THROTTLE){ 24 | digitalWrite(HB_DIRECTION, 0); 25 | analogWrite(HB_PWM, 195); 26 | 27 | delay(500); 28 | int MaxTPSV = analogRead(POT_THROTTLE); 29 | MaxTPSV = analogRead(POT_THROTTLE); 30 | Serial.println(MaxTPSV); 31 | return MaxTPSV; 32 | } 33 | 34 | int FindRestingTPS(int HB_PWM, int HB_DIRECTION, int POT_THROTTLE){ 35 | analogWrite(HB_PWM, 0); 36 | digitalWrite(HB_DIRECTION, 1); 37 | delay(700); 38 | int RestingTPSV = analogRead(POT_THROTTLE); 39 | RestingTPSV = analogRead(POT_THROTTLE); 40 | return RestingTPSV; 41 | } 42 | 43 | unsigned int FindMaxAPP(int POT_PEDAL){ 44 | Serial.println("Please depress APP"); 45 | delay(1000); 46 | unsigned int MaxAPP = analogRead(POT_PEDAL); 47 | MaxAPP = analogRead(POT_PEDAL); 48 | Serial.print("Max APP raw val: ");Serial.println(MaxAPP); 49 | delay(1000); 50 | return MaxAPP; 51 | } 52 | 53 | unsigned int FindMinAPP(int POT_PEDAL){ 54 | Serial.println("Please let go of APP"); 55 | delay(1000); 56 | unsigned int MinAPP = analogRead(POT_PEDAL); 57 | MinAPP = analogRead(POT_PEDAL); 58 | Serial.print("Min APP raw val: ");Serial.println(MinAPP); 59 | delay(1000); 60 | return MinAPP; 61 | } 62 | 63 | bool burnCalibration(int minTPS, int maxTPS, int minAPP, int maxAPP, int minTPS2, int maxTPS2, int minAPP2, int maxAPP2, int restTPS, int throttleMode){ 64 | FastCRC8 CRC8; 65 | 66 | Serial.print("MaxTPS = "); Serial.println(maxTPS); 67 | Serial.print("MinTPS = "); Serial.println(minTPS); 68 | Serial.print("MaxAPP = "); Serial.println(maxAPP); 69 | Serial.print("MinAPP = "); Serial.println(minAPP); 70 | EEPROM.update(0,highByte(minTPS)); 71 | EEPROM.update(1,lowByte(minTPS)); 72 | EEPROM.update(2,highByte(maxTPS)); 73 | EEPROM.update(3,lowByte(maxTPS)); 74 | EEPROM.update(4,highByte(minAPP)); 75 | EEPROM.update(5,lowByte(minAPP)); 76 | EEPROM.update(6,highByte(maxAPP)); 77 | EEPROM.update(7,lowByte(maxAPP)); 78 | EEPROM.update(8,true);// address 8 saved calibration status 79 | EEPROM.update(9,highByte(restTPS)); 80 | EEPROM.update(10,lowByte(restTPS)); 81 | EEPROM.update(11,highByte(minTPS2)); 82 | EEPROM.update(12,lowByte(minTPS2)); 83 | EEPROM.update(13,highByte(maxTPS2)); 84 | EEPROM.update(14,lowByte(maxTPS2)); 85 | EEPROM.update(15,highByte(minAPP2)); 86 | EEPROM.update(16,lowByte(minAPP2)); 87 | EEPROM.update(17,highByte(maxAPP2)); 88 | EEPROM.update(18,lowByte(maxAPP2)); 89 | EEPROM.update(19,throttleMode); 90 | Serial.println("All calibration values are saved"); 91 | 92 | uint8_t CRCbuf[19]; 93 | for (int i = 0; i < 20; i++){ 94 | CRCbuf[i] = EEPROM.read(i); 95 | Serial.print("CRC buffer[");Serial.print(i);Serial.print("] = ");Serial.println(CRCbuf[i]); 96 | Serial.print("EEPROM[");Serial.print(i);Serial.print("] = ");Serial.println(EEPROM.read(i)); 97 | } 98 | EEPROM.update(20, CRC8.smbus(CRCbuf, sizeof(CRCbuf))); 99 | Serial.print("CRC value = "); Serial.println(CRC8.smbus(CRCbuf, sizeof(CRCbuf))); 100 | return true; //to be used to set calibration flag 101 | } 102 | 103 | bool clearCalibration(){ 104 | EEPROM.update(0,highByte(0)); 105 | EEPROM.update(1,lowByte(0)); 106 | EEPROM.update(2,highByte(0)); 107 | EEPROM.update(3,lowByte(0)); 108 | EEPROM.update(4,highByte(0)); 109 | EEPROM.update(5,lowByte(0)); 110 | EEPROM.update(6,highByte(0)); 111 | EEPROM.update(7,lowByte(0)); 112 | 113 | EEPROM.update(8,false);// address 8 saved calibration status 114 | EEPROM.update(9,highByte(0)); 115 | EEPROM.update(10,0); 116 | 117 | EEPROM.update(11, 0); 118 | EEPROM.update(12,0); 119 | EEPROM.update(13,0); 120 | EEPROM.update(14,0); 121 | EEPROM.update(15,0); 122 | EEPROM.update(16,0); 123 | EEPROM.update(17,0); 124 | EEPROM.update(18,0); 125 | EEPROM.update(19,0); 126 | Serial.println("Calibration Cleared"); 127 | return false; 128 | } 129 | 130 | 131 | -------------------------------------------------------------------------------- /newthrottlecontrolFaST/README.md: -------------------------------------------------------------------------------- 1 | # ETC_Control 2 | -------------------------------------------------------------------------------- /newthrottlecontrolFaST/newthrottlecontrolFaST.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | #include "ETC.h" 9 | 10 | 11 | //AlphaX Throttle Control 12 | //Licensed under the GPL v3 13 | /* 14 | Alpha X ThrttoleController 15 | Copyright (C) Norman Paulino 16 | 17 | This program is free software; you can redistribute it and/or 18 | modify it under the terms of the GNU General Public License 19 | as published by the Free Software Foundation; either version 2 20 | of the License, or (at your option) any later version. 21 | 22 | This program is distributed in the hope that it will be useful,la 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 | GNU General Public License for more details. 26 | 27 | You should have received a copy of the GNU General Public License 28 | along with this program; if not, write to the Free Software 29 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 30 | */ 31 | void loadCalibration(); 32 | void idleRequestPWM(); 33 | void serialRead(byte); 34 | 35 | // Inputs 36 | const int POT_THROTTLE = A0; // Servo Position Input 37 | const int POT_THROTTLE2 = A1; // Servo Position Input2 38 | const int POT_PEDAL = A3; // Pedal Sensor Input 39 | const int POT_PEDAL2 = A2; // Pedal Sensor Input 40 | const int POT_IDLE = A4; //Requested Idle Input 41 | const byte idlePWMPin = 2; 42 | 43 | // Pins H bridge 44 | const byte HB_DIRECTION = 8; // H bridge Direction 45 | const byte HB_PWM = 11; // H bridge PWM (speed) 46 | 47 | // Throttle constraints, max voltages in both direction 48 | int MinTPS; 49 | int MinTPS2; 50 | int MaxTPS; 51 | int MaxTPS2; 52 | int MaxAPP = 1023; 53 | int MaxAPP2 = 1023; 54 | int MinAPP = 0; 55 | int MinAPP2 = 0; 56 | unsigned int throttleRest; 57 | 58 | //PID Control variables 59 | double kP = 2.3; 60 | double kI = 1; 61 | double kD = .9; 62 | //Idle constants 63 | double idlekP = 1.8; 64 | double idlekI = .08; 65 | double idlekD = 0.9; 66 | 67 | //customvars 68 | unsigned long timeDiag = 500; 69 | bool diagEnabled = false; 70 | bool calFlag = 0; 71 | int throttleCorr = 0; 72 | byte idleSetPoint = 0; 73 | unsigned long idlePWMtime = 0; 74 | bool idleCount = true; 75 | byte dutyCyclein = 0; 76 | byte throttleMode = 0; 77 | bool safetyCheck = true; 78 | byte safetyCount = 0; 79 | byte safetyMode = 0; 80 | 81 | FastCRC8 CRC8; 82 | 83 | //Define the PID controller 84 | double Input, Output, SetPoint; 85 | PID throttlePID(&Input, &Output, &SetPoint, kP, kI, kD, DIRECT); 86 | //double idleOutput, idleSetPoint; 87 | //PID idlePID(&Input, &idleOutput, = true&idleSetPoint, idlekP, idlekI, idlekD, DIRECT); 88 | 89 | //Operating States flag 90 | char state = 's'; //s = safety, t = transient, i = idle, c = constant (steady state) 91 | 92 | void setup() { 93 | // put your setup code here, to run once: 94 | //Enable PID control 95 | Serial.begin(115200); 96 | Serial.println("Box OK!"); 97 | throttlePID.SetMode(AUTOMATIC); 98 | throttlePID.SetSampleTime(1); 99 | //idlePID.SetMode(AUTOMATIC); 100 | calFlag = EEPROM.read(8); 101 | if (calFlag){ 102 | uint8_t CRCbuf[19]; 103 | for (int i = 0; i < 20; i++){ 104 | CRCbuf[i] = EEPROM.read(i); 105 | } 106 | uint8_t CRCvalue = EEPROM.read(20); 107 | Serial.print("CRC buffer value = "); Serial.println(CRC8.smbus(CRCbuf, sizeof(CRCbuf))); 108 | Serial.print("CRC value = "); Serial.println(EEPROM.read(20)); 109 | if (CRCvalue == CRC8.smbus(CRCbuf, sizeof(CRCbuf))){ 110 | loadCalibration(); 111 | Serial.println("Calibration Loaded"); 112 | } 113 | else{ 114 | safetyMode = 3; 115 | Serial.println("Calibration Corrupted! No loading was done!"); 116 | } 117 | 118 | } 119 | pinMode(HB_DIRECTION, OUTPUT); 120 | enableInterrupt(idlePWMPin, idleRequestPWM, CHANGE); 121 | 122 | } 123 | 124 | void loop() { 125 | if (Serial.available()){ 126 | byte serialRead = Serial.read() - 48; 127 | Serial.println(serialRead); 128 | serialCommands(serialRead); 129 | } 130 | 131 | //Read all inputs 132 | int inTPS = analogRead(POT_THROTTLE); //read for example 48, meaning throttle at rest, max is 758 133 | int rawAPP = analogRead(POT_PEDAL); //read for example 70, app is 0 (no demand), max is 940 134 | int inAPP = map(rawAPP,MinAPP, MaxAPP, MinTPS, MaxTPS); 135 | idleSetPoint = 100 + dutyCyclein; //analogRead(POT_IDLE); 136 | 137 | 138 | 139 | switch(throttleMode){ 140 | case 0: 141 | break; 142 | case 1: 143 | if (inAPP < 300){ 144 | inAPP = map(inAPP, MinAPP, 300, MinAPP, 250); 145 | } 146 | else if( (inAPP > 300) && (inAPP < 600)){ 147 | inAPP = map(inAPP, 300, 600, 250, 700); 148 | } 149 | else{ 150 | inAPP = map(inAPP, 600, MaxAPP, 700, MaxAPP); 151 | } 152 | break; 153 | } 154 | 155 | /* if (abs(inTPS - inAPP) < 70){ 156 | state = 'c'; //Sets to steady state 157 | } 158 | else if (abs(inTPS - inAPP) >= 70){ 159 | //Serial.println(inTPS -inAPP); 160 | state = 't'; //sets to transient mode 161 | } 162 | if ((rawAPP <= MinAPP + 3)){ 163 | state = 'i'; //sets idle mode 164 | idleSetPoint = 100 + dutyCyclein; //analogRead(POT_IDLE); 165 | }*/ 166 | 167 | if (inAPP <= idleSetPoint){ 168 | state = 'i'; //sets idle mode 169 | } 170 | else if (inAPP > idleSetPoint){ 171 | state = 'c'; //Sets to steady state 172 | } 173 | if (abs(inTPS - inAPP) >= 60){ 174 | //Serial.println(inTPS -inAPP); 175 | state = 't'; //sets to transient mode 176 | } 177 | if (safetyMode > 0){ 178 | state = 's'; 179 | } 180 | 181 | if (((millis() % 200) == 0) && (safetyCheck == true)){ 182 | int inTPS2 = analogRead(POT_THROTTLE2); 183 | int rawAPP2 = analogRead(POT_PEDAL2); 184 | inTPS2 = map(inTPS2, MinTPS2+18, MaxTPS2, MinTPS, MaxTPS); 185 | rawAPP2 = map(rawAPP2, MinAPP2, MaxAPP2, MinAPP, MaxAPP); 186 | int TPSerror = abs(inTPS-inTPS2); 187 | int APPerror = abs(rawAPP-rawAPP2); 188 | int correlationError = 0; 189 | if (state == 'i'){ 190 | //correlationError = abs(inTPS - idleSetPoint); 191 | TPSerror = 0; 192 | } 193 | else{ correlationError = abs(inTPS - inAPP);} 194 | 195 | if ((TPSerror > 10) || (APPerror > 10) || (correlationError > 35)){ 196 | safetyCount++; 197 | } 198 | else if (safetyCount != 0){ 199 | safetyCount--; 200 | } 201 | if (safetyCount == 0){ 202 | state = 'i'; 203 | safetyCheck = true; 204 | safetyMode = 0; 205 | } 206 | //if(safetyCount > 10){ 207 | if ((safetyCount > 10) && (safetyCount <= 20)){ 208 | //Serial.println("ETC SYSTEM ERROR!"); 209 | safetyMode = 1; 210 | } 211 | /*else if ((safetyCount > 20) && (safetyCount <= 30)){ 212 | safetyMode = 2; 213 | Serial.println("ETC SYSTEM ERROR HIGH!"); 214 | } 215 | else if (safetyCount > 30){ 216 | safetyMode = 3; 217 | Serial.println("ETC SYSTEM ERROR CRITICAL!"); 218 | }*/ 219 | safetyCheck = false; 220 | if ((diagEnabled) && safetyMode > 0){ 221 | Serial.print("TPS Error = "); Serial.println(TPSerror); 222 | Serial.print("TPS % = "); Serial.println(inTPS); 223 | Serial.print("TPS2 % = "); Serial.println(inTPS2); 224 | Serial.print("correlationError% = "); Serial.println(correlationError); 225 | Serial.print("safetyCount = "); Serial.println(safetyCount); 226 | Serial.print("safetyMode = "); Serial.println(safetyMode); 227 | /* Serial.print("MaxTPS % = "); Serial.println(MaxTPS); 228 | Serial.print("MinTPS % = "); Serial.println(MinTPS); 229 | Serial.print("MaxTPS2 % = "); Serial.println(MaxTPS2); 230 | Serial.print("MinTPS2 % = "); Serial.println(MinTPS2);*/ 231 | // Serial.print("APPerror = "); Serial.println(APPerror); 232 | 233 | } 234 | } 235 | else if((millis() % 200) != 0) { safetyCheck = true;} 236 | 237 | if ((millis() - timeDiag) > 600){ 238 | 239 | 240 | if ((diagEnabled) && safetyMode == 0){ 241 | Serial.print("TPS % = "); Serial.println(inTPS); 242 | Serial.print("Calibrated APP% = "); Serial.println(inAPP); 243 | Serial.print("State: "); Serial.println(state); 244 | Serial.print("Idle% = "); Serial.println(idleSetPoint); 245 | Serial.print("Input idle DC = ");Serial.println(dutyCyclein); 246 | Serial.print("PID Output = "); Serial.println(Output); 247 | } 248 | //Serial.print("Idle% = "); Serial.println(idleSetPoint); 249 | timeDiag = millis(); 250 | } 251 | 252 | /* if (inAPP > MaxAPP -15){ 253 | state = 'c'; //WOT mode 254 | }*/ 255 | //convert inputs to doubles 256 | Input = inTPS; 257 | SetPoint = inAPP; 258 | switch (state){ 259 | case 'c': 260 | throttlePID.SetTunings(1.1, .07, 0.03); 261 | // throttlePID.SetSampleTime(2); 262 | if (inTPS < inAPP){ 263 | digitalWrite(HB_DIRECTION, 0); //sets ETC Direction to forward 264 | throttlePID.SetControllerDirection(DIRECT); 265 | throttlePID.SetOutputLimits(50,200); 266 | //Serial.print("PID Output = "); Serial.println(Output); 267 | throttlePID.Compute(); //compute PID based correction 268 | analogWrite(HB_PWM,(Output)); //uses the base duty and adds a correction factor with PID 269 | //Serial.println("Steady State Forward"); 270 | } 271 | else { 272 | digitalWrite(HB_DIRECTION, 1); //set ETC Direction Backward 273 | /*throttlePID.SetTunings(1.4, 1, 0.7); 274 | throttlePID.SetControllerDirection(REVERSE); 275 | throttlePID.SetOutputLimits(20,120); 276 | throttlePID.Compute(); */ 277 | if (abs(inTPS - inAPP) < 5){ 278 | analogWrite(HB_PWM,20); //makes throttle go backward with open loop values 279 | //Serial.println("Idle backslow"); 280 | } 281 | else{ 282 | analogWrite(HB_PWM,80); //makes throttle go backward with open loop values 283 | //Serial.println("Idle back"); 284 | } 285 | throttlePID.Compute(); //compute PID based correction 286 | 287 | } 288 | break; 289 | case 't': 290 | //Transient function that forces the TP to open at max rate, and close at a relatively fast rate to avoid slamming 291 | if (inTPS < inAPP){ //opening 292 | digitalWrite(HB_DIRECTION, 0); 293 | analogWrite(HB_PWM,200); 294 | //Serial.println("Open Transient"); 295 | } 296 | else { 297 | digitalWrite(HB_DIRECTION, 1); //closing 298 | if ((inTPS < throttleRest -50)) { 299 | analogWrite(HB_PWM, 20); 300 | //Serial.println("Closing transient - slow"); 301 | } 302 | else{ 303 | analogWrite(HB_PWM, 170); 304 | //Serial.println("Closing transient"); 305 | } 306 | } 307 | //Serial.println("Transient mode"); 308 | break; 309 | case 'i': 310 | // throttlePID.SetSampleTime(1); 311 | SetPoint = idleSetPoint; 312 | 313 | if (inTPS < idleSetPoint){ 314 | throttlePID.SetTunings(1.1, .07, 0.0); 315 | throttlePID.SetControllerDirection(DIRECT); 316 | digitalWrite(HB_DIRECTION, 0); //sets ETC Direction to forward 317 | throttlePID.SetOutputLimits(5,60); 318 | throttlePID.Compute(); //compute PID based correction 319 | analogWrite(HB_PWM,(Output)); //uses the base duty and adds a correction facttor with PID 320 | //Serial.println("Idle opening"); 321 | } 322 | else { 323 | digitalWrite(HB_DIRECTION, 1); //set ETC Direction Backward 324 | /*throttlePID.SetTunings(1.4, 1, 0.7); 325 | throttlePID.SetControllerDirection(REVERSE); 326 | throttlePID.SetOutputLimits(20,120); 327 | throttlePID.Compute(); */ 328 | if (abs(inTPS - idleSetPoint) < 5){ 329 | analogWrite(HB_PWM,20); //makes throttle go backward with open loop values 330 | //Serial.println("Idle backslow"); 331 | } 332 | else{ 333 | analogWrite(HB_PWM,105); //makes throttle go backward with open loop values 334 | //Serial.println("Idle back"); 335 | } 336 | //Serial.println("Idle closing"); 337 | // Serial.println(Output); 338 | throttlePID.Compute(); 339 | } 340 | //Serial.println("Idle control"); 341 | break; 342 | case 's': // safetymodes 343 | if (safetyMode == 1){ 344 | throttlePID.SetTunings(1.9, .07, 0.03); 345 | SetPoint = constrain(SetPoint, 160, 980); 346 | // throttlePID.SetSampleTime(2); 347 | if (inTPS < inAPP){ 348 | digitalWrite(HB_DIRECTION, 0); //sets ETC Direction to forward 349 | throttlePID.SetControllerDirection(DIRECT); 350 | throttlePID.SetOutputLimits(20,180); 351 | //Serial.print("PID Output = "); Serial.println(Output); 352 | throttlePID.Compute(); //compute PID based correction 353 | analogWrite(HB_PWM,(Output)); //uses the base duty and adds a correction factor with PID 354 | //Serial.println("Steady State Forward"); 355 | } 356 | else { 357 | digitalWrite(HB_DIRECTION, 1); //set ETC Direction Backward 358 | /*throttlePID.SetTunings(1.4, 1, 0.7); 359 | throttlePID.SetControllerDirection(REVERSE); 360 | throttlePID.SetOutputLimits(20,120); 361 | throttlePID.Compute(); */ 362 | if (abs(inTPS - inAPP) < 5){ 363 | analogWrite(HB_PWM,20); //makes throttle go backward with open loop values 364 | //Serial.println("Idle backslow"); 365 | } 366 | else{ 367 | analogWrite(HB_PWM,80); //makes throttle go backward with open loop values 368 | //Serial.println("Idle back"); 369 | } 370 | throttlePID.Compute(); //compute PID based correction 371 | 372 | } 373 | } 374 | else if (safetyMode == 2){ 375 | digitalWrite(HB_DIRECTION, 1); 376 | analogWrite(HB_PWM,60); 377 | } 378 | else { 379 | digitalWrite(HB_DIRECTION, 1); 380 | analogWrite(HB_PWM,0); 381 | safetyCheck = false; 382 | } 383 | break; 384 | default: //Assumes an error in the char assignation and closes the throttle 385 | digitalWrite(HB_DIRECTION, 1); 386 | analogWrite(HB_PWM,0); 387 | break; 388 | } 389 | } 390 | 391 | void serialCommands(byte serialRead){ 392 | switch(serialRead){ 393 | case 1: 394 | //Calibrate TPS 395 | MaxTPS = FindMaxTPS(HB_PWM, HB_DIRECTION, POT_THROTTLE); 396 | MaxTPS2 = FindMaxTPS(HB_PWM, HB_DIRECTION, POT_THROTTLE2); 397 | MinTPS = FindMinTPS(HB_PWM, HB_DIRECTION, POT_THROTTLE); 398 | MinTPS2 = FindMinTPS(HB_PWM, HB_DIRECTION, POT_THROTTLE2); 399 | throttleRest = FindRestingTPS(HB_PWM, HB_DIRECTION, POT_THROTTLE); 400 | break; 401 | 402 | case 2: 403 | //CalibrateAPP 404 | MaxAPP = FindMaxAPP(POT_PEDAL); 405 | MaxAPP2 = FindMaxAPP(POT_PEDAL2); 406 | MinAPP = FindMinAPP(POT_PEDAL); 407 | MinAPP2 = FindMinAPP(POT_PEDAL2); 408 | break; 409 | case 3: 410 | diagEnabled = !diagEnabled; 411 | break; 412 | case 4: 413 | calFlag = burnCalibration(MinTPS, MaxTPS, MinAPP, MaxAPP, MinTPS2, MaxTPS2, MinAPP2, MaxAPP2,throttleRest,throttleMode); 414 | break; 415 | case 5: 416 | calFlag = clearCalibration(); 417 | break; 418 | case 6: 419 | throttleMode++; 420 | if (throttleMode > 1){ throttleMode = 0;} 421 | Serial.print("Throttlemode changed to = ");Serial.println(throttleMode); 422 | break; 423 | case 7: 424 | safetyMode = 0; 425 | break; 426 | case 8: 427 | uint8_t CRCbuf[19]; 428 | for (int i = 0; i < 20; i++){ 429 | CRCbuf[i] = EEPROM.read(i); 430 | } 431 | //uint8_t CRCvalue2 = EEPROM.read(20); 432 | Serial.print("CRC buffer value = "); Serial.println(CRC8.smbus(CRCbuf, sizeof(CRCbuf))); 433 | Serial.print("CRC value = "); Serial.println(EEPROM.read(20)); 434 | break; 435 | default: 436 | break; 437 | } 438 | } 439 | 440 | void loadCalibration(){ 441 | byte high = EEPROM.read(0); 442 | byte low = EEPROM.read(1); 443 | MinTPS = word(high,low); 444 | high = EEPROM.read(2); 445 | low = EEPROM.read(3); 446 | MaxTPS = word(high,low); 447 | high = EEPROM.read(4); 448 | low = EEPROM.read(5); 449 | MinAPP = word(high,low); 450 | high = EEPROM.read(6); 451 | low = EEPROM.read(7); 452 | MaxAPP = word(high,low); 453 | high = EEPROM.read(9); 454 | low = EEPROM.read(10); 455 | throttleRest = word(high,low); 456 | calFlag = EEPROM.read(8); 457 | 458 | high = EEPROM.read(11); 459 | low = EEPROM.read(12); 460 | MinTPS2 = word(high,low); 461 | high = EEPROM.read(13); 462 | low = EEPROM.read(14); 463 | MaxTPS2 = word(high,low); 464 | high = EEPROM.read(15); 465 | low = EEPROM.read(16); 466 | MinAPP2 = word(high,low); 467 | high = EEPROM.read(17); 468 | low = EEPROM.read(18); 469 | MaxAPP2 = word(high,low); 470 | throttleMode = EEPROM.read(19); 471 | } 472 | 473 | void idleRequestPWM(){ 474 | //Serial.println("INTERRUPT! "); 475 | if (digitalRead(idlePWMPin)){ 476 | if (idleCount){ 477 | idlePWMtime = millis(); 478 | idleCount = false; 479 | } 480 | } 481 | else{ 482 | if (!idleCount){ 483 | dutyCyclein = millis() - idlePWMtime; 484 | idleCount = true; 485 | } 486 | } 487 | } 488 | --------------------------------------------------------------------------------