- fchao-Sinus-Wechselrichter AliExpress         
Seite 1 von 4 123 ... LetzteLetzte
Ergebnis 1 bis 10 von 35

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

  1. #1
    HaWe
    Gast

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

    Anzeige

    Praxistest und DIY Projekte
    hallo,
    ist jemand erfahren in std::thread und besitzt auch einen Arduino-kompatiblen ESP32?

    Ich bin dabei zu vesuchen, eine std::thread Implementierung zu bekommen, bin aber selber zu wenig erfahren damit (habe selber nur geringe Kenntnisse in C99 Posix pthread).

    Ich habe hier ein Topic eröffnet und auch schon einen Vorschlag, der allerdings zwar compilier- aber nicht lauffähig ist:
    https://github.com/espressif/arduino-esp32/issues/2814

    Welche Kenner und Könner können hier weiterhelfen und den Beispielcode zum Laufen kriegen?

    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);
        }
    }
    
    void blinker_loop() {
        uint32_t counter = 0;
        while(true) {
            digitalWrite(LED_BUILTIN, HIGH);
            std::this_thread::sleep_for(one_sec);
            digitalWrite(LED_BUILTIN, LOW);
            std::this_thread::sleep_for(one_sec);
        }
    }
    
    
    void setup() {
        Serial.begin(115200);
        pinMode(LED_BUILTIN, OUTPUT);
        std::thread counter_loop_thread(counter_loop);
        std::thread blinker_loop_thread(blinker_loop);
    }
    
    uint32_t main_loop_counter = 0;
    void loop() {
        main_loop_counter++;
        Serial.print("main loop: ");
        Serial.println(main_loop_counter);
        delay(10000);
    }
    Fehlerausgabe seriell:
    Rebooting...
    ets Jun 8 2016 00:22:57

    rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd _drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff0018,len:4
    load:0x3fff001c,len:928
    ho 0 tail 12 room 4
    load:0x40078000,len:8424
    ho 0 tail 12 room 4
    load:0x40080400,len:5868
    entry 0x4008069c
    counter_loop: 0
    abort() was called at PC 0x400e5ca7 on core 1

    Backtrace: 0x40089150:0x3ffb1ed0 0x4008937d:0x3ffb1ef0 0x400e5ca7:0x3ffb1f10 0x400e5cee:0x3ffb1f30 0x400d0da2:0x3ffb1f50 0x400d0f92:0x3ffb1f70 0x400d1bc3:0x3ffb1fb0 0x40087c9d:0x3ffb1fd0

    Rebooting...
    ets Jun 8 2016 00:22:57

    rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd _drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff0018,len:4
    load:0x3fff001c,len:928
    ho 0 tail 12 room 4
    load:0x40078000,len:8424
    ho 0 tail 12 room 4
    load:0x40080400,len:5868
    entry 0x4008069c
    counter_loop: 0
    abort() was called at PC 0x400e5ca7 on core 1

    (dauerhaft wiederholend)
    Geändert von HaWe (23.05.2019 um 17:00 Uhr)

  2. #2
    shedepe
    Gast
    Hi,

    std::thread counter_loop_thread(counter_loop);
    std::thread blinker_loop_thread(blinker_loop);

    Definier die Threads global, bzw. als Pointer. Sonst werden die instantan wieder zerstört beim Verlassen von setup() (Scope von Variablen).
    Und dann entscheide dich mal ob du in dem Thread oder in der Mainloop eine Serielle Ausgabe machst. Sonst versuchst du unter Umständen von zwei Threads gleichzeitig auf das selbe Stück Hardware zuzugreifen. Je nachdem wie der Chip aufgebaut ist, könnte das durchaus als Hardware Error zu einem Cpu Reset führen.

  3. #3
    HaWe
    Gast
    hallo,
    danke, den 1. Teil mache und teste ich sofort!
    auf der anderen Seite sollten gerade die Threads ihren momentanen Status per Serial an die Konsole melden, damit man den augenblicklichen Status erkenn kann, so ähnlich wie bei fprintf(stderr, ...) auf meinem Pi ...

    - - - Aktualisiert - - -

    update:
    Code geändert:

    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() {
        static uint32_t counter = 0;
        while(true) {
            Serial.print("counter_loop: ");
            Serial.println(counter);
            std::this_thread::sleep_for(one_sec);
        }
    }
    
    void blinker_loop() {
        static 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);
        }
    }
    
    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);
    }

    Good news: es läuft (irgendwie ),
    und die LED blinkt auch!
    A-Bär:
    Trotz "static" in den threads zählen aber die counter dort nicht hoch:


    main loop: 1
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    main loop: 2
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    main loop: 3
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    main loop: 4

  4. #4
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    39
    Beiträge
    3.416
    versuchs mal mit "thread_local" zwischen static und uint_32

    das static wird bei thread_local zwar impliziert aber schaden kanns nicht es explizit zu schreiben

    thread_local sorgt dafür dass die variable im richtigen stack kontext verwendet wird

    Nachtrag: std::threads haben jeweils einene eigenen stack kontext und wenn man darin mit statischen variablen arbeitet kann das zu kuriosen erscheinungen führen, deshalb sollte man variablen die nur für den thread relevnt sind immer als thread_local definieren damit sie im stack korrekt verarbeitet werden, ansonsten natürlich mit globalen variabl... äh objekten arbeiten bitte
    Es gibt 10 Sorten von Menschen: Die einen können binär zählen, die anderen
    nicht.

  5. #5
    HaWe
    Gast
    danke für den Tipp, aber leider: nein
    Code:
    void counter_loop() {
        static thread_local uint32_t counter = 0;
        while(true) {
            Serial.print("counter_loop: ");
            Serial.println(counter);
            std::this_thread::sleep_for(one_sec);
        }
    }
    
    void blinker_loop() {
        static thread_local 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);
        }
    }
    main loop: 1
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    main loop: 2
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    counter_loop: 0
    blinker_loop (LOW) counter: 0
    blinker_loop (HIGH) counter: 0
    counter_loop: 0
    main loop: 3
    auch keine Änderung, wenn man dann das static wieder rausnimmt.

  6. #6
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    39
    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.

  7. #7
    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!

  8. #8
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    39
    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.

  9. #9
    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

  10. #10
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    39
    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.

Seite 1 von 4 123 ... 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
  •  

Solar Speicher und Akkus Tests