- fchao-Sinus-Wechselrichter AliExpress         
Seite 1 von 2 12 LetzteLetzte
Ergebnis 1 bis 10 von 35

Thema: std::thread für ESP32 unter Arduino IDE zum Laufen zu kriegen...?

Hybrid-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #1
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    40
    Beiträge
    3.416
    öhm ... ich seh da eigentlich kein problem, du erstellst counter mit 0 und gibst es ständig immer wieder aus ohne damit überhaupt was zu machen ... cih glaube du hast da irgendwo in der schleife dein counter++ vergessen oder !?

    edit: muss gesetehen habs das counter++ beim ersten mal lesen mental in den code gedichtet und hab den offensichtlichen fehler nicht gleich bemerkt (aber das thread_local sollte dennoch bei jeder lokalen variable dabei sein)
    Es gibt 10 Sorten von Menschen: Die einen können binär zählen, die anderen
    nicht.

  2. #2
    HaWe
    Gast
    hahahaha, ich lach mich tot.....

    JA!!!

    sogar ohne static und ohne thread_local:

    Code:
    #include <Arduino.h>
    #include <thread>
    #include <chrono>
    
    #ifndef LED_BUILTIN
    #define LED_BUILTIN 13
    #endif
    
    const auto one_sec = std::chrono::seconds
    {
        1
    };
    
    void counter_loop() {
        uint32_t counter = 0;
        while(true) {
            Serial.print("counter_loop: ");
            Serial.println(counter);
            std::this_thread::sleep_for(one_sec);
            counter++;
        }
    }
    
    void blinker_loop() {
        uint32_t counter = 0;
        while(true) {
            digitalWrite(LED_BUILTIN, HIGH);
            Serial.print("blinker_loop (HIGH) counter: ");
            Serial.println(counter);
            std::this_thread::sleep_for(one_sec);
            
            digitalWrite(LED_BUILTIN, LOW);
            Serial.print("blinker_loop (LOW) counter: ");
            Serial.println(counter);
            std::this_thread::sleep_for(one_sec);
            counter++;
        }
    }
    
    std::thread counter_loop_thread(counter_loop);
    std::thread blinker_loop_thread(blinker_loop);
        
    void setup() {
        Serial.begin(115200);
        pinMode(LED_BUILTIN, OUTPUT);
        
    }
    
    uint32_t main_loop_counter = 0;
    void loop() {
        main_loop_counter++;
        Serial.print("main loop: ");
        Serial.println(main_loop_counter);
        delay(10000);
    }
    main loop: 1
    counter_loop: 1
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 1
    counter_loop: 2
    counter_loop: 3
    blinker_loop (LOW) counter: 1
    blinker_loop (HIGH) counter: 2
    counter_loop: 4
    counter_loop: 5
    blinker_loop (LOW) counter: 2
    blinker_loop (HIGH) counter: 3
    counter_loop: 6
    counter_loop: 7
    blinker_loop (LOW) counter: 3
    blinker_loop (HIGH) counter: 4
    counter_loop: 8
    counter_loop: 9
    blinker_loop (LOW) counter: 4
    blinker_loop (HIGH) counter: 5
    counter_loop: 10
    main loop: 2
    counter_loop: 11
    blinker_loop (LOW) counter: 5
    blinker_loop (HIGH) counter: 6
    counter_loop: 12
    counter_loop: 13
    blinker_loop (LOW) counter: 6
    blinker_loop (HIGH) counter: 7
    counter_loop: 14
    counter_loop: 15
    blinker_loop (LOW) counter: 7
    blinker_loop (HIGH) counter: 8
    counter_loop: 16
    counter_loop: 17
    blinker_loop (LOW) counter: 8
    blinker_loop (HIGH) counter: 9
    counter_loop: 18
    counter_loop: 19
    blinker_loop (LOW) counter: 9
    blinker_loop (HIGH) counter: 10
    counter_loop: 20
    main loop: 3
    counter_loop: 21
    vielen Dank an euch beide, das ist jetzt echt ne super Basis um weiterzumachen!
    Ein Riesen-Schritt für die Menschheit!
    großartig, danke!

  3. #3
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    40
    Beiträge
    3.416
    sogar ohne static und ohne thread_local:
    wie gesagt, thread_local ist garnicht falsch, erspart dir später womöglich kuriose bugs ... ich würde es immer drin lassen!
    Es gibt 10 Sorten von Menschen: Die einen können binär zählen, die anderen
    nicht.

  4. #4
    HaWe
    Gast
    okidoki, danke!

    - - - Aktualisiert - - -

    PS,
    interessanter Effekt, gerade bemerkt:
    in der blinker_loop zählt der counter zwischen den sleeps und dem LED ON/OFF-Paar weiter, obwohl ich hier denselben counterwert für ON und OFF erwartet hätte, bis zum nächsten loop-Durchlauf für das nächste ON/OFF-Paar

  5. #5
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    40
    Beiträge
    3.416
    hast du dazu einen aktualisierten code ?
    Es gibt 10 Sorten von Menschen: Die einen können binär zählen, die anderen
    nicht.

  6. #6
    HaWe
    Gast
    ist immer noch der von #7 !

    - - - Aktualisiert - - -

    gerade getestet:
    thread_local uint32_t counter = 0;
    macht hier auch keinen Unterschied

  7. #7
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    40
    Beiträge
    3.416
    ja ... sorry ... hab ich nicht gesehen

    interessante ausgabe, gebe ich zu ... es wäre mal interessant zu erfahren wie der zeitliche ablauf bei der ausgabe aussieht! ich vermute hier das problem!

    benutze mal irgend einen timer mit einer auflösung im wenigstens 100tel Sekunden bereich und speichere vor und nach jedem sleep jeweils einmal die zeit und gib die zeiten plus die aktuelle zeit auch als print aus

    also quasi print("Jetzt: %d Vorher: %d Nachher %d",timer.now(), beforesleep, aftersleep)

    so kann man erstmal prüfen ob der sleep vernünftig funktioniert und dann ob der print hier irgendwelche delays verursacht

    ich denke dass die ausgaben bei dir nicht chronologisch korrekt ausgegeben werden
    Es gibt 10 Sorten von Menschen: Die einen können binär zählen, die anderen
    nicht.

  8. #8
    HaWe
    Gast
    update:
    mutex ist doch drin, braucht nur zusätzlich
    Code:
    #include <mutex>
    was ich nicht wusste,
    dann klappt jetzt auch

    Code:
    std::mutex print_mutex;
    
    void myPrint(String str) // String: Arduino API  // C++: std::string
    {
        print_mutex.lock();
        Serial.println(str);
        print_mutex.unlock();
    }

  9. #9
    HaWe
    Gast
    es gibt hier noch einen Bug oder Issue, wie man thread prios richtig setzt - bisher brachten alle Vorschläge noch keine Lösung.
    Man muss nämlich die thread prios der main loop prio angleichen, wenn beide ohne delays parallel laufen sollen
    (std::threads laufen nämlich per prio=5 per default, main loop nur bei default prio=1!!):
    Code:
    esp_pthread_cfg_t cfg;
      esp_pthread_get_cfg(&cfg);
      cfg.prio=1;
      esp_pthread_set_cfg(&cfg);
    funktioniert nicht, weil anfangs cfg noch keine Werte enthält und daher einen Fehler zurückgibt,

    und

    Code:
      esp_pthread_cfg_t cfg;
      if (esp_pthread_get_cfg(&cfg) != ESP_OK) {
          cfg = esp_pthread_get_default_config();
      }
      cfg.prio=1;
      if (esp_pthread_set_cfg(&cfg) != ESP_OK) {
          printf("esp_pthread_set_cfg failed\n");
          abort();
      };
    funktioniert auch nicht, weil die hier verwendete Funktion
    esp_pthread_get_default_config();
    nicht gefunden wird.

    siehe Topic-Posts u.a.
    https://github.com/espressif/ESP8266...SDK/issues/609
    https://github.com/espressif/esp-idf...ment-496157019
    und folgende...

    betroffene libs am ehesten wohl
    https://github.com/espressif/ESP8266...ts/pthread/src


    Hat jemand eine idee, wie es richtig geht?
    Geändert von HaWe (29.05.2019 um 08:02 Uhr)

  10. #10
    HaWe
    Gast
    so, ich habe es hin bekommen:
    man muss die thread prios unter die von main loop setzen, damit der watch dog von main gefüttert wird.
    Da main immer prio=1 by default hat, müssen die threads bei prio 0 laufen, damit sie nicht blockieren können:

    Code:
    // std::thread for ESP32, Arduino IDE
    
    // ver 0.0.6 fibonacci
    
    #include <Arduino.h>
    #include <thread>
    #include <freertos/task.h>
    
    #ifndef LED_BUILTIN
    #define LED_BUILTIN 13
    #endif
    
    const auto one_sec = std::chrono::seconds
    {
        1
    };
    
    
    
    
    uint32_t fibonacci(uint32_t n) {
       if(n == 0){
          return 0;
       } else if(n == 1) {
          return 1;
       } else {
          return (fibonacci(n-1) + fibonacci(n-2));
       }
    }
    
    
    
    void blinker_loop() {
        thread_local uint32_t counter = 0;
        Serial.println((String)"blinker_loop Current priority :" + uxTaskPriorityGet(NULL));
        vTaskPrioritySet(NULL,0);//set Priority
        Serial.println((String)"blinker_loop Current priority :" + uxTaskPriorityGet(NULL));
        while(true) {
            digitalWrite(LED_BUILTIN, HIGH);
            Serial.println((String)"blinker_loop (HIGH) counter: "+ counter);
            std::this_thread::sleep_for(one_sec);
            
            digitalWrite(LED_BUILTIN, LOW);
            Serial.println((String)"blinker_loop (LOW) counter: "+ counter);
            std::this_thread::sleep_for(one_sec);
            
            counter++;
        }
    }
    
    
    void fibonacci_loop() {
        thread_local uint32_t counter = 0, i=0;
        Serial.println((String)"fibonacci_loop Current priority :" + uxTaskPriorityGet(NULL));
        vTaskPrioritySet(NULL,0);//set Priority
        Serial.println((String)"fibonacci_loop Current priority :" + uxTaskPriorityGet(NULL));
        while(true) {
            for(i=30; i<41; i++) {    // limits: test, debug
              Serial.println( (String)"Fibbonacci of "+i+"="+fibbonacci(i));            
            }        
            Serial.println((String)"fibonacci_loop counter: "+counter);  
            counter++;      
        }
    }
    
    
    
    std::thread *thread_1;
    std::thread *thread_2;
    
    
    void setup() {
      Serial.begin(115200);
      delay(1000);
      thread_1 = new std::thread(blinker_loop);
      thread_2 = new std::thread(fibonacci_loop);
    }
    
    
    void loop() {
        delay(500);
        static uint32_t main_loop_counter = 0;
        Serial.println((String)"main loop: " + main_loop_counter);
        delay(5000);
        main_loop_counter++;
    }

    Code:
    blinker_loop Current priority :5
    blinker_loop Current priority :0
    fibonacci_loop Current priority :5
    blinker_loop (HIGH) counter: 0
    fibonacci_loop Current priority :0
    Fibbonacci of 30=832040
    main loop: 0
    Fibbonacci of 31=1346269
    blinker_loop (LOW) counter: 0
    Fibbonacci of 32=2178309
    blinker_loop (HIGH) counter: 1
    Fibbonacci of 33=3524578
    blinker_loop (LOW) counter: 1
    blinker_loop (HIGH) counter: 2
    Fibbonacci of 34=5702887
    blinker_loop (LOW) counter: 2
    main loop: 1
    blinker_loop (HIGH) counter: 3
    blinker_loop (LOW) counter: 3
    blinker_loop (HIGH) counter: 4
    Fibbonacci of 35=9227465
    blinker_loop (LOW) counter: 4
    blinker_loop (HIGH) counter: 5
    blinker_loop (LOW) counter: 5
    main loop: 2
    blinker_loop (HIGH) counter: 6
    blinker_loop (LOW) counter: 6
    Fibbonacci of 36=14930352
    blinker_loop (HIGH) counter: 7
    blinker_loop (LOW) counter: 7
    blinker_loop (HIGH) counter: 8
    main loop: 3
    blinker_loop (LOW) counter: 8
    blinker_loop (HIGH) counter: 9
    blinker_loop (LOW) counter: 9
    blinker_loop (HIGH) counter: 10
    blinker_loop (LOW) counter: 10
    Fibbonacci of 37=24157817
    blinker_loop (HIGH) counter: 11
    main loop: 4
    blinker_loop (LOW) counter: 11
    blinker_loop (HIGH) counter: 12
    blinker_loop (LOW) counter: 12
    blinker_loop (HIGH) counter: 13
    blinker_loop (LOW) counter: 13
    main loop: 5
    blinker_loop (HIGH) counter: 14
    blinker_loop (LOW) counter: 14
    blinker_loop (HIGH) counter: 15
    blinker_loop (LOW) counter: 15
    blinker_loop (HIGH) counter: 16
    blinker_loop (LOW) counter: 16
    main loop: 6
    blinker_loop (HIGH) counter: 17
    blinker_loop (LOW) counter: 17
    Fibbonacci of 38=39088169
    blinker_loop (HIGH) counter: 18
    blinker_loop (LOW) counter: 18
    blinker_loop (HIGH) counter: 19
    main loop: 7
    blinker_loop (LOW) counter: 19
    blinker_loop (HIGH) counter: 20
    blinker_loop (LOW) counter: 20
    blinker_loop (HIGH) counter: 21
    blinker_loop (LOW) counter: 21
    blinker_loop (HIGH) counter: 22
    main loop: 8
    blinker_loop (LOW) counter: 22
    blinker_loop (HIGH) counter: 23
    blinker_loop (LOW) counter: 23
    blinker_loop (HIGH) counter: 24
    blinker_loop (LOW) counter: 24
    main loop: 9
    blinker_loop (HIGH) counter: 25
    blinker_loop (LOW) counter: 25
    blinker_loop (HIGH) counter: 26
    blinker_loop (LOW) counter: 26
    blinker_loop (HIGH) counter: 27
    blinker_loop (LOW) counter: 27
    main loop: 10
    blinker_loop (HIGH) counter: 28
    blinker_loop (LOW) counter: 28
    blinker_loop (HIGH) counter: 29
    Fibbonacci of 39=63245986
    blinker_loop (LOW) counter: 29
    blinker_loop (HIGH) counter: 30
    main loop: 11
    blinker_loop (LOW) counter: 30
    blinker_loop (HIGH) counter: 31
    blinker_loop (LOW) counter: 31
    blinker_loop (HIGH) counter: 32
    blinker_loop (LOW) counter: 32
    blinker_loop (HIGH) counter: 33
    main loop: 12
    blinker_loop (LOW) counter: 33
    blinker_loop (HIGH) counter: 34
    blinker_loop (LOW) counter: 34
    blinker_loop (HIGH) counter: 35
    blinker_loop (LOW) counter: 35
    main loop: 13
    blinker_loop (HIGH) counter: 36
    blinker_loop (LOW) counter: 36
    blinker_loop (HIGH) counter: 37
    blinker_loop (LOW) counter: 37
    blinker_loop (HIGH) counter: 38
    blinker_loop (LOW) counter: 38
    main loop: 14
    blinker_loop (HIGH) counter: 39
    blinker_loop (LOW) counter: 39
    blinker_loop (HIGH) counter: 40
    blinker_loop (LOW) counter: 40
    blinker_loop (HIGH) counter: 41
    main loop: 15
    blinker_loop (LOW) counter: 41
    blinker_loop (HIGH) counter: 42
    blinker_loop (LOW) counter: 42
    blinker_loop (HIGH) counter: 43
    blinker_loop (LOW) counter: 43
    blinker_loop (HIGH) counter: 44
    main loop: 16
    blinker_loop (LOW) counter: 44
    blinker_loop (HIGH) counter: 45
    blinker_loop (LOW) counter: 45
    blinker_loop (HIGH) counter: 46
    blinker_loop (LOW) counter: 46
    main loop: 17
    blinker_loop (HIGH) counter: 47
    Fibbonacci of 40=102334155
    fibonacci_loop counter: 0
    Fibbonacci of 30=832040
    blinker_loop (LOW) counter: 47
    Fibbonacci of 31=1346269
    blinker_loop (HIGH) counter: 48
    Fibbonacci of 32=2178309
    blinker_loop (LOW) counter: 48
    Fibbonacci of 33=3524578
    blinker_loop (HIGH) counter: 49
    blinker_loop (LOW) counter: 49
    main loop: 18
    Fibbonacci of 34=5702887
    blinker_loop (HIGH) counter: 50
    blinker_loop (LOW) counter: 50
    blinker_loop (HIGH) counter: 51
    Fibbonacci of 35=9227465
    blinker_loop (LOW) counter: 51
    blinker_loop (HIGH) counter: 52
    main loop: 19
    blinker_loop (LOW) counter: 52
    blinker_loop (HIGH) counter: 53
    blinker_loop (LOW) counter: 53
    blinker_loop (HIGH) counter: 54
    Fibbonacci of 36=14930352
    blinker_loop (LOW) counter: 54
    blinker_loop (HIGH) counter: 55
    main loop: 20
    blinker_loop (LOW) counter: 55
    ntl nur der jetzige Stand, möglicherweise erscheinen doch wieder irgendwann neue Erkenntnisse...
    Geändert von HaWe (11.08.2019 um 12:07 Uhr) Grund: typo

Seite 1 von 2 12 LetzteLetzte

Ähnliche Themen

  1. Esp32 a2dp in arduino IDE
    Von Flos6323 im Forum Elektronik
    Antworten: 0
    Letzter Beitrag: 27.06.2018, 15:28
  2. Installation des Arduino Core für ESP32 ohne GIT
    Von mischaka im Forum NodeMCU-Board und ESP8266, ESP32-Serie
    Antworten: 0
    Letzter Beitrag: 26.04.2018, 07:20
  3. Arduino Cinque: RISC-V-Prozessor und ESP32 auf einem Board vereint
    Von Roboternetz-News im Forum Neuigkeiten / Technik-News / Nachrichten / Aktuelles
    Antworten: 1
    Letzter Beitrag: 22.05.2017, 16:29
  4. Display für esp32?
    Von NotEvil im Forum NodeMCU-Board und ESP8266, ESP32-Serie
    Antworten: 7
    Letzter Beitrag: 04.12.2016, 16:37
  5. Kugelgelenke - woher kriegen?
    Von Gottfreak im Forum Mechanik
    Antworten: 15
    Letzter Beitrag: 04.01.2005, 17:56

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

Labornetzteil AliExpress