From 913a6d1f15d437dc86abcb34b9c22e4d0960953f Mon Sep 17 00:00:00 2001 From: sieja Date: Sun, 15 Jun 2025 17:14:20 +0200 Subject: [PATCH] =?UTF-8?q?Uusni=C4=99cie=20zb=C4=99dnego=20kodu=20Przywr?= =?UTF-8?q?=C3=B3cenie=20SpeedTrend=20Przywr=C3=B3cenie=20migania=20=C5=9B?= =?UTF-8?q?wiat=C5=82a=20stop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ESP_AutomatedGearShifter.ino | 121 ++++++------------ 1 file changed, 37 insertions(+), 84 deletions(-) diff --git a/ESP32/ESP_AutomatedGearShifter/ESP_AutomatedGearShifter.ino b/ESP32/ESP_AutomatedGearShifter/ESP_AutomatedGearShifter.ino index acec2bc..7d33abf 100644 --- a/ESP32/ESP_AutomatedGearShifter/ESP_AutomatedGearShifter.ino +++ b/ESP32/ESP_AutomatedGearShifter/ESP_AutomatedGearShifter.ino @@ -5,7 +5,7 @@ #include #include -#define Version "2.1.4" +#define Version "2.2.0" ////2DO: // menu do zmiany zakresu predkosci biegów // menu do zmiany zakresu kątów biegów, obwodu koła, ilosci magnesow @@ -40,7 +40,6 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); Servo myservo; - //SPEED double readSignalTime_5; double readSignalTime_4; @@ -62,20 +61,8 @@ unsigned long millissSpd = millis(); unsigned long lastMillisSpd = millis(); unsigned long lastLastMillisSpd = millis(); unsigned long loopTime = millis(); -//speed validation -double speed4Gear = 0.0; -double speed4Gear_1 = 0.0; -double speed4Gear_2 = 0.0; -double speed4Gear_3 = 0.0; -double speed4Geat_estimated = 0.0; -double speedDiff_1 = 0.0; -double speedDiff_2 = 0.0; -double speedDiff_3 = 0.0; -int avgWeight_2 = 2; -int avgWeight_3 = 1; //GEAR int currentGear = 1; -int previousGear = 1; int calculatedGear = 1; //Przedziały dia biegów float spdRange1and2 = 7.5; @@ -87,11 +74,10 @@ float spdRange6and7 = 29.9; float spdRange7and8 = 36.5; double calcTimeDiff = 0.0; double lastGearCalc = millis(); -double changeDelayMs = 2000.0; +double changeDelayMs = 1000.0; double accelerationShift = 1.0; int displGear = 9 - currentGear; float currentGearRangeLower = 0; -float currentGearRangeMiddle = 3.0; float currentGearRangeUpper = 7.5; //SERVO int pos = 0; @@ -101,12 +87,7 @@ int servoCurrPos = ServoMaxAngle; int gearBarHeight = 0; int gearBarPosition = 0; float speedForBar = 0; -//DST -int totalDistMemLocation = 60; -int loop_cnt = 0; -double totalDist = 0.0; -unsigned int totalDistReaded = 0; -unsigned int totalDistWrited = 0; +unsigned int pointerVisibility = 1; //BATTERY float referenceVoltage = 3.3; int maxADCValue = 4095; @@ -118,8 +99,6 @@ int voltBarHeight = 0; int voltBarPosition = 0; //oth int BrakingLightSwitch; -int ups = 0; -int downs = 0; int run_hrs = 0; int run_mins = 0; void setup() { @@ -127,14 +106,7 @@ void setup() { digitalWrite(ServoSwitch, HIGH); myservo.attach(ServoPin); // attaches the servo on pin 4 to the servo object setPosition(8); -// -// Serial.begin(9600); -// //DIPLAY settings -// if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { -// Serial.println(F("SSD1306 allocation failed")); -// for (;;); // Don't proceed, loop forever -// } display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); display.clearDisplay(); display.setTextColor(WHITE); @@ -187,10 +159,6 @@ void setup() { myservo.write(servoCurrPos); delay(15); } - - //MEMORY - // eeprom_read_block(&totalDistReaded, totalDistMemLocation, 2); - // totalDist = float(totalDistReaded); } @@ -226,7 +194,6 @@ void readSpeed() { readSignalTimeAvg = (readSignalTime_1 + readSignalTime_2 + readSignalTime_3 )/3; raw_speed = (((2 * Pi) / readSignalTimeAvg * ((WheelCircumference) / (Pi)) * ms2kmh)) / MagnetsCnt; - } @@ -241,11 +208,7 @@ void prepareTurnOff() { myservo.write(servoCurrPos); delay(15); } -//Na czas diagnostyki -// eeprom_write_block(&totalDist,totalDistMemLocation,2); -// totalDistWrited = totalDist; -// loop_cnt = 0; -// + for (; 1500 < (millis() - lastMillisSpd);) { // zmiana z 1000 na 1500 w 1.13.19 digitalWrite(ServoSwitch, LOW); digitalWrite(ServoPin, LOW); @@ -264,22 +227,11 @@ void prepareTurnOff() { display.println("+"); display.setCursor(15, 0); display.write(30); - display.setCursor(25, 0); - display.println(downs + ups); //Version display.setCursor(5, 13); display.println("V: "); display.setCursor(15, 13); display.println(Version); - //DST - display.setCursor(50, 0); - display.println("Spins: "); - display.setCursor(85, 0); - display.println(totalDistWrited); - display.setCursor(58, 13); - display.println("KM: "); - display.setCursor(95, 13); - display.println((totalDistWrited*WheelCircumference)/1000); //RUN TIME run_mins = floor((millis() / 1000) / 60); run_hrs = floor(run_mins / 60); @@ -299,7 +251,6 @@ void prepareTurnOff() { voltBarPosition = 64 - voltBarHeight; display.fillRect(0, voltBarPosition, 2, voltBarHeight, SSD1306_WHITE); display.display(); - delay(500); } } @@ -311,6 +262,17 @@ void calcSpeed(){ calcSpeedAvg = (calcSpeed1 + calcSpeed2 + calcSpeed3)/3; calcSpeedMain = calcSpeedAvg; + + if(abs(raw_speed - calcSpeed2) >= changeDelayMs) { + if ((raw_speed - calcSpeed2) < 0) { + speedTrend = -1; + + } else { + speedTrend = 1; + } + }else { + speedTrend = 0; + } } void calcGear() { accelerationShift = 1; @@ -352,16 +314,23 @@ void calcGear() { }; calcTimeDiff = millis() - lastGearCalc; - // previousGear = currentGear; - currentGear = calculatedGear; - lastGearCalc = millis(); - + if (calcTimeDiff < 2000 && abs((calculatedGear-currentGear))<=1){ + currentGear = currentGear; + pointerVisibility = 0; + } else { + currentGear = calculatedGear; + lastGearCalc = millis(); + pointerVisibility = 1; + } } //########################################### LOOP ############################################################ //########################################### LOOP ############################################################ + + + void loop() { loopTime = millis(); @@ -382,8 +351,6 @@ void loop() { //SPEED sleepSpd = millis() - millissSpd; if (sleepSpd >= 1000.0) { //podaj zerową prędkość jeśli nie było odcztu od 1,5 s - speed4Gear = 0.0; - speed4Gear_2 = 0.0; raw_speed = 0.0; } //przejście w tryb uśpienia za przuycisku lub czasu @@ -391,9 +358,6 @@ void loop() { prepareTurnOff(); } - if (speed4Gear > 40 || isinf(speed4Gear)) { - speed4Gear = speed4Gear_3; - } calcSpeed(); calcGear(); displGear = 9 - currentGear; @@ -423,18 +387,16 @@ void loop() { display.setCursor(40, 40); display.println(calcSpeedMain, 1); //GearBar - currentGearRangeMiddle = (currentGearRangeLower + currentGearRangeUpper) / 2; - display.fillRect(115, 32, 7, 1, SSD1306_WHITE); - display.fillRect(115, 0, 7, 1, SSD1306_WHITE); - display.fillRect(115, 63, 7, 1, SSD1306_WHITE); - - display.setTextSize(2); - - gearBarHeight = 64 - int(((calcSpeedMain - currentGearRangeLower) / (currentGearRangeUpper - currentGearRangeLower)) * 64) - 5; - display.setCursor(115, gearBarHeight); - display.write(16); - display.setTextSize(3); - + if (pointerVisibility = 1){ + display.fillRect(115, 32, 7, 1, SSD1306_WHITE); + display.fillRect(115, 0, 7, 1, SSD1306_WHITE); + display.fillRect(115, 63, 7, 1, SSD1306_WHITE); + display.setTextSize(2); + gearBarHeight = 64 - int(((calcSpeedMain - currentGearRangeLower) / (currentGearRangeUpper - currentGearRangeLower)) * 64) - 5; + display.setCursor(115, gearBarHeight); + display.write(16); + display.setTextSize(3); + } //VOLT_BAR adcBattVoltValue = analogRead(VoltInptPin); @@ -460,7 +422,7 @@ void loop() { - if (speedTrend == -1 && speed4Gear > 0.0 ) { + if (speedTrend == -1 ) { if (BrakingLightSwitch == 1) { digitalWrite(BrakingLight, HIGH); BrakingLightSwitch = 0; @@ -471,15 +433,6 @@ void loop() { } else { digitalWrite(BrakingLight, LOW); } -// //TTL DST -// // totalDist = 0; -// loop_cnt = loop_cnt +1; -// if (loop_cnt >= 20 //&& totalDistWrited != totalDist -// ){ -// totalDistWrited = int(totalDist); -// // eeprom_write_block(&totalDistWrited,totalDistMemLocation,2); -// loop_cnt = 0; -// } } //########################################### LOOP ############################################################