Advertisement
Guest User

Ree Car Dohs Änderung

a guest
Feb 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. #include <HX711_ADC.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>        // LCD i2c Library einbinden
  4.  
  5. HX711_ADC LoadCell(3, 4);
  6. LiquidCrystal_I2C lcd1(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  7. LiquidCrystal_I2C lcd2(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  8.  
  9. #define PotiPin    0  //PIN Potti
  10. #define HallPin    2  //PIN Hallsensor
  11. #define OctoPin    9  //PIN Octokoppler
  12. //#define ledPin    13  //PIN Led-Octo
  13. #define pwmPin     9
  14. /*
  15. int PotiPin = 0;     //PIN Potti
  16. const int HallPin = 2;
  17. int OctoPin =9;
  18. int ledPin =13;
  19. */
  20.  
  21. int                   Prozent;
  22. int                   Umax      = 1500.;    //  Max Drehzahl des Motors
  23. int                   upmSoll;
  24. float                 Force;
  25. unsigned long         pwm = 0;
  26. char                   buf[17];
  27. unsigned long          upmIst   = 0;
  28. const int              AVG      = 4;                            // Glättung über mindestens 4 Messwerte
  29. volatile unsigned long dauer    = 0;             // microsekunden seit dem letzten Interrupt
  30. volatile unsigned long last     = 0;              // Zählerwert beim letzten Interrupt
  31. volatile unsigned long average  = 1;           // Integrierte Dauer // =1: divide by 0 vermeiden
  32. volatile int           avgcnt   = 0;    
  33.  
  34.  
  35. void setup() {
  36.   Serial.begin(9600);
  37.  
  38.   pinMode(PotiPin, INPUT);
  39.   pinMode(HallPin, INPUT);
  40.   pinMode(OctoPin, OUTPUT);
  41.  
  42.  
  43.   attachInterrupt(PotiPin, readmicros, RISING);
  44.  
  45.   LoadCell.begin();
  46.   LoadCell.start(2000);
  47.   LoadCell.setCalFactor(106763.15789474/9.81);
  48.  
  49.   lcd1.begin(16,2);
  50.   lcd1.backlight();
  51.  
  52.   lcd2.begin(16, 2);  // Display hat 2 Zeilen a 16 Zeichen  
  53.   lcd2.backlight();  
  54. }
  55.  
  56. void loop() {
  57.   Prozent = analogRead(PotiPin)*(4.9296/49.89255);  //*(5./4.914)
  58.   Serial.print("Prozent:");
  59.   Serial.print(Prozent);
  60. //  sprintf(Prozent, "%3lu");
  61.  
  62.  
  63.   upmSoll = ((Umax*(analogRead(PotiPin)*(4.9296/49.89255)))/100);
  64.   Serial.print("_UpmSoll:");
  65.   Serial.print(upmSoll);
  66.  
  67.  
  68.   UpmIst();
  69.  
  70.   if (upmIst >= upmSoll){
  71.     Serial.print("_ERROR32:BREMSEEEEE_");
  72.     pwm = Umax -(Umax - upmIst);
  73.     pwm = map(pwm,0,Umax,0,255);
  74.     analogWrite(pwmPin, pwm);
  75.     digitalWrite(pwmPin, HIGH);    
  76.   }else{
  77.     Serial.print("_UpmSoll<UmpIst_");
  78.     digitalWrite(OctoPin, LOW);
  79.     digitalWrite(pwmPin, LOW);
  80.   }
  81.  
  82.   Lcd1();
  83.   Lcd2();
  84.  
  85.   LoadCell.update();
  86.   Force = LoadCell.getData();
  87. }
  88.  
  89. void UpmIst (void){
  90.   if (dauer != 0) {
  91.   upmIst = myround(60000000 / average);       // Drehzahl ausrechnen und runden
  92.   } else {
  93.     upmIst = 0;                               // keine Messung? -> Stillstand
  94.     avgcnt = 0;
  95.   }
  96.   sprintf(buf, "%4lu", upmIst);
  97.   Serial.println("_UpmIst: " + String(buf));
  98.   dauer >>= 10;                               // Flag für Stillstand ( : 1024 )
  99.   delay(500);    
  100. }
  101.                                               // mehr als zwei Werte pro Sekunde kann eh keiner lesen
  102. void readmicros() {                           // Interrupt-Routine
  103.   detachInterrupt(PotiPin);                         // Interrupt ausschalten damit er uns nicht beißt
  104.   int avgmax;
  105.   unsigned long us = micros();                // Microsekundenzähler auslesen
  106.   if (last == 0) {                            // erster Messwert?
  107.     last = us;                                // merken und nicht weiter bearbeiten
  108.   } else {
  109.     if ( us < last ) {                        // Zählerüberlauf
  110.       dauer = 4294967295 - last + us;         // erzeugt einen Fehler von 1µS - vernachlässigbar
  111.     } else {
  112.       dauer = us - last;                      // Differenz zum letzten Durchlauf berechnen
  113.     }
  114.     if (dauer > 5000) {                       // ignorieren wenn <= 5ms (Kontaktpreller)
  115.       average = dauer + average * avgcnt++;   // Wert in buffer und mit Faktor avgcnt glätten
  116.       average /= avgcnt;                      // und zurückrechnen
  117.       avgmax = 1000000 / dauer;               // dynamische Größe des Integrationspuffers
  118.       if (avgmax < AVG) avgmax = AVG;         // Trägheit mindestens 1 Sekunde
  119.       if (avgcnt >= avgmax) avgcnt--;
  120.       last = us;                              // und wieder den letzten Wert merken
  121.     }
  122.   }
  123.   attachInterrupt(PotiPin, readmicros, RISING );    // Interrupt wieder einschalten
  124.   }
  125.  
  126. unsigned long myround(unsigned long value) {  // Gewichtete Rundung
  127.   int rto;
  128.   if (value > 3000) {                         // Rundungswert bestimmen
  129.     rto = 100;
  130.   } else if (value > 1500) {
  131.     rto = 50;
  132.   } else if (value > 500) {
  133.     rto = 10;
  134.   } else if (value > 100) {
  135.     rto = 5;
  136.   } else {
  137.     return (value);
  138.   }
  139.   return (_myround(value, rto));
  140. }
  141.  
  142. unsigned long _myround(unsigned long value, int roundto) {
  143.   value += (roundto >> 1);                    // halben roundto Wert addieren
  144.   value /= roundto;                           // integer division
  145.   value *= roundto;                           // integer multiplikation
  146.   return (value);
  147. }
  148.  
  149. void Lcd1(void){                //Ausgabe-Komplett am LCD1
  150.  
  151.   lcd1.setCursor(0, 0);
  152.   lcd1.print("Force[Nm]:");    
  153.   lcd1.setCursor(10, 0);
  154.   lcd1.print(Force, 3);         //gewicht ausgabe
  155.   lcd1.setCursor(0, 1);
  156.   lcd1.print("Drehzahl ");
  157.   lcd1.print(Prozent);          //Prozent ausgabe
  158.   lcd1.print("%");
  159.  
  160. }
  161.  
  162. void Lcd2(void){                //Ausgabe-Komplett am LCD2
  163.  
  164.  lcd2.clear();               //  Soll es BLINKEN ODER NICHT????
  165.   lcd2.setCursor(0, 00);        
  166.   lcd2.print("SOLL IST MAX U/M");
  167.   lcd2.setCursor(0, 1);        
  168.   lcd2.print(upmSoll);          //Soll-Umdrehung ausgeben
  169.   lcd2.setCursor(6, 1);
  170.   lcd2.print(upmIst);           //Ist-Umdrehung ausgeben
  171.   lcd2.setCursor(12, 1);
  172.   lcd2.print(Umax);             //Max UPM Schwellwert zum schalten der Bremse
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement