From 5fea805387e29c6b506c7e50ffb28c96c7bac31d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20SAILLANT?= Date: Sun, 28 May 2023 14:56:24 +0200 Subject: [PATCH] Last update --- src/main.cpp | 109 +++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c8e17c9..aee2582 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,5 @@ /* - -TESTER AVEC LIB : https://github.com/Valar-Systems/FastAccelStepper-ESP32 ??? - controle de moteur pas à pas selon commande DMX code par Clément SAILLANT pour Hémisphère - 2023 @@ -23,29 +20,13 @@ https://www.omc-stepperonline.com/download/34HS59-6004D-E1000.pdf Controleur CL86T V4.0 https://www.omc-stepperonline.com/index.php?route=product/product/get_file&file=389/CL86T%20(V4.0).pdf - -SW1 0 -SW2 0 -SW3 1 -SW4 1 - -SW5 On CCW -SW6 Off Close loop -SW7 Off Pul/Dir -SW8 Off Brk */ -// #define dev_mode // mode developpeur - #include "PIN_mapping.h" // fichier contenant les variables globales et le mapping des pins bool emergencyStop = false; // variable pour stocker l'état de l'arrêt d'urgence #include -// #include -// #include "esp_attr.h" - #include -// #include "esp_task_wdt.h" // librairie pour le watchdog #include // librairie pour le controle des moteurs pas à pas #include // librairie pour le controle des encodeurs @@ -59,11 +40,10 @@ ESP32Encoder encoder; // création de l'objet encoder void setup() { - delay(1000); + // delay(1000); Serial.begin(115200); Serial.println("started"); - // configuration DMX dmx_set_pin(dmx_num, tx_pin, rx_pin, rts_pin); dmx_driver_install(dmx_num, DMX_DEFAULT_INTR_FLAGS); @@ -73,6 +53,7 @@ void setup() // configuration ENABLE motor pinMode(MOTOR_ENABLE_PIN, OUTPUT); + // configuration BCD Coder pinMode(q1, INPUT); // thumbwheel '1' pinMode(q2, INPUT); // thumbwheel '2' @@ -80,21 +61,16 @@ void setup() pinMode(q8, INPUT); // thumbwheel '8' /* - #ifndef dev_mode // attach an interrupt to the IO pin of the ermegency stop switch and specify the handler function pinMode(EMERGENCY_STOP_PIN, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(EMERGENCY_STOP_PIN), emergencySwitchHandler, RISING); - #endif */ - pinMode(MOTOR_ENABLE_PIN, OUTPUT); - pinMode(LIMIT_SWITCH_PIN, INPUT); - pinMode(HOME_SWITCH_PIN, INPUT); + // lecture des potentiomètres pour définir les offset de pas a ajouter aux positions home et limit + limit_steps = map((analogRead(limit_set_pot)), 4095, 0, min_offset, max_offset); + home_steps = map((analogRead(home_set_pot)), 4095, 0, min_offset, max_offset); - // ???????????????? Error : attachInterrupt with ADC enabled :'( ???????????????? - - // attach an interrupt to the IO pin of the home switch and specify the handler function + // attach an interrupt to the IO pin of the home switch and specify the handler function attachInterrupt(digitalPinToInterrupt(HOME_SWITCH_PIN), homeSwitchHandler, CHANGE); stepper.registerHomeReachedCallback(homeReachedCallback); @@ -112,16 +88,24 @@ void setup() stepper.registerTargetPositionReachedCallback(targetPositionReachedCallback); // start the stepper instance as a service in the "background" as a separate task - stepper.startAsService(); // at core 1 + stepper.startAsService(1); // core 1 sinon bug avec le dmx et déplacement aléatoire du moteur init_range(); // set home and limit position of the motor - Serial.print("number of tasks is "); - Serial.println(uxTaskGetNumberOfTasks()); - Serial.print("home position is "); - Serial.println(min_steps); - Serial.print("limit position is "); - Serial.println(max_steps); + DMX_start_channel = readSwitch(); // lecture de la position du codeurs BCD pour définir le canal DMX de départ + // DMX_start_channel = 2; // pour test avec adresse fixe + pos_channel = DMX_start_channel; + speed_channel = DMX_start_channel + 1; + + Serial.println("----------------------------------------"); + Serial.printf("number of tasks is %i\n", uxTaskGetNumberOfTasks()); + Serial.printf("home position is %i\n", min_steps); + Serial.printf("limit position is %i\n", max_steps); + Serial.printf("Limit setting is %i\n", limit_steps); + Serial.printf("Home setting is %i\n", home_steps); + Serial.printf("DMX start channel is %i\n", DMX_start_channel); + Serial.printf("DMX position channel is %i\n", pos_channel); + Serial.printf("DMX speed channel is %i\n", speed_channel); Serial.println("----------------------------------------"); } @@ -129,7 +113,7 @@ void loop() { receiveDMX(); // reception des données DMX - // mise à jour de la vitesse du moteur si la valeur DMX a changé + // mise à jour de la vitesse du moteur si la valeur DMX a changée if (dataChanged[speed_array]) { dataChanged[speed_array] = false; @@ -138,25 +122,46 @@ void loop() } // mise à jour de la position du moteur si la valeur DMX a changée - if (dataChanged[pos_channel] == true && emergencyStop == false) + // if (dataChanged[pos_array] == true && emergencyStop == false && digitalRead(MOTOR_ENABLE_PIN) == LOW) + if (dataChanged[pos_array] == true) { // stepper.setTargetPositionToStop(); - dataChanged[pos_channel] = false; - int pos_in_step = map(DMX_data[pos_array], 0, 255, min_steps, max_steps); - - // adaptation de la vitesse d'acceleration en fonction de la distance à parcourir - if (abs(DMX_data_old[pos_array] - DMX_data[pos_array]) < 25) - { - speed_in_accel = 50; - } - else - { - speed_in_accel = 500; - } - stepper.setAccelerationInStepsPerSecondPerSecond(speed_in_accel); + dataChanged[pos_array] = false; + pos_in_step = map(DMX_data[pos_array], 0, 255, min_steps, max_steps); stepper.setTargetPositionInSteps(pos_in_step); + Serial.printf("acceleration => %ld\n", speed_in_accel); + Serial.printf("deceleration => %i\n", speed_in_decel); Serial.printf("dmx pos to step => %ld\n", pos_in_step); DMX_data_old[pos_array] = DMX_data[pos_array]; + last_dmx_change_pos = millis(); + } + // gestion de la position du moteur si la valeur DMX n'a pas changée mais que le moteur n'est pas à la position demandée + if (millis() - last_dmx_change_pos > 1000) + { + int codeur_pos = map(encoder.getCount(), min_count, max_count, min_steps, max_steps); + if (pos_in_step <= min_steps && ConfirmedHomeSwitchState != switch_active && stepper.getDirectionOfMotion() == 0) + { + Serial.println("!!!!!!!!!!!!! home not reached"); + Serial.printf("codeur pos => %i\n", codeur_pos); + // stepper.setTargetPositionInSteps(min_steps - codeur_pos); // home_steps + stepper.setTargetPositionRelativeInSteps(-nb_microstep); + delay(100); + } + else if (pos_in_step >= max_steps && ConfirmedLimitSwitchState != switch_active && stepper.getDirectionOfMotion() == 0) + { + Serial.println("!!!!!!!!!!!!! limit not reached"); + Serial.printf("codeur pos => %i\n", codeur_pos); + // stepper.setTargetPositionInSteps(max_steps + (max_steps - codeur_pos)); // limit_steps + stepper.setTargetPositionRelativeInSteps(nb_microstep); + delay(100); + } + else if (abs(codeur_pos - pos_in_step) > 100 && stepper.getDirectionOfMotion() == 0) + { + Serial.println("!!!!!!!!!!!!! Not in position"); + Serial.printf("codeur pos => %i\n", codeur_pos); + Serial.printf("pos in step => %i\n", pos_in_step); + stepper.setTargetPositionInSteps(pos_in_step); + } } // motor_follower(emergency_stop_loss_step); // fonction pour suivre la position du moteur et la perte de pas