diff --git a/2009/TONI.zip b/2009/TONI.zip deleted file mode 100644 index 6c39139..0000000 Binary files a/2009/TONI.zip and /dev/null differ diff --git a/2024/P25_055_Julia_Montilla/4_Carrito_Motor_Vibrador/src/main.cpp b/2024/P25_055_Julia_Montilla/4_Carrito_Motor_Vibrador/src/main.cpp index f201c8b..3c2de23 100644 --- a/2024/P25_055_Julia_Montilla/4_Carrito_Motor_Vibrador/src/main.cpp +++ b/2024/P25_055_Julia_Montilla/4_Carrito_Motor_Vibrador/src/main.cpp @@ -10,6 +10,7 @@ #include <AsyncTCP.h> #include <ESPAsyncWebServer.h> #include <WebSerial.h> +//#include <analogWrite.h> AsyncWebServer server(8080); unsigned long last_print_time = millis(); @@ -24,10 +25,16 @@ unsigned long last_print_time = millis(); #define TIME_TO_CLOSE 1000 #define TIME_TO_OPEN 750 #define TIME_VIBRATION_ACTIVE 7000 +#define STOP_MOTOR 0 +#define CLOSE_MOTOR 1 +#define OPEN_MOTOR 2 -const int Channel = 0; // Canal PWM, puede ser de 0 a 15 + +const int Channel0 = 0; // Canal PWM, puede ser de 0 a 15 +const int Channel1 = 1; // Canal PWM, puede ser de 0 a 15 const int frequency = 5000; // Frecuencia en Hz const int resolution = 8; // Resolución en bits (de 1 a 15) +const int resolution_analog = 12; // Resolución en bits (de 9 a 12) /*#define UP_ANGLE 0 #define DOWN_ANGLE 66 @@ -41,9 +48,13 @@ const int resolution = 8; // Resolución en bits (de 1 a 15) volatile int dir = 1; const int K1 = 16; const int K2 = 17; -const int ENA = 14; -const int IN1 = 26; -const int IN2 = 33; +const int PWM_R = 33; +const int PWM_L = 14; +const int EN_M = 26; +const int IS_R = 32; +const int IS_L = 35; +const int FC_UP = 18; +const int FC_DOWN = 22; // Set these to your desired credentials. char *ssid = "Neteja_Neteja"; // your network SSID (name) @@ -64,15 +75,23 @@ IPAddress subnet(255, 255, 255, 0); const char* host_ip = "192.168.1.255"; bool first_time = true; +unsigned long time_first_time = millis(); bool state_tapa = false; unsigned long time_cycle = millis(); int num_loop = 1; +int orientation = 0; +int protection_orientation = 0; +unsigned long time_protection = millis(); void cierra(int SPEED) { - ledcWrite(Channel, SPEED); - digitalWrite(IN2, LOW); - digitalWrite(IN1, HIGH); + if (protection_orientation==CLOSE_MOTOR) ledcWrite(Channel1, 0); + else ledcWrite(Channel1, SPEED); + ledcWrite(Channel0, 0); + if (orientation!=CLOSE_MOTOR) time_protection = millis(); + orientation = CLOSE_MOTOR; + //digitalWrite(IN2, LOW); + //digitalWrite(IN1, HIGH); if(state_tapa == true) { WebSerial.println(String(num_loop) + "-Cerrando Tapa:" + String(millis()-time_cycle)); @@ -80,11 +99,16 @@ void cierra(int SPEED) } } + void abrir(int SPEED) { - ledcWrite(Channel, SPEED); - digitalWrite(IN1, LOW); - digitalWrite(IN2, HIGH); + ledcWrite(Channel1, 0); + if (protection_orientation==OPEN_MOTOR) ledcWrite(Channel0, 0); + else ledcWrite(Channel0, SPEED); + if (orientation!=OPEN_MOTOR) time_protection = millis(); + orientation = OPEN_MOTOR; + //digitalWrite(IN1, LOW); + //digitalWrite(IN2, HIGH); if(state_tapa == true) { WebSerial.println(String(num_loop) + "-Abriendo Tapa:" + String(millis()-time_cycle)); @@ -92,13 +116,14 @@ void abrir(int SPEED) } } -void stop_motor() +void stop_motor(bool emergency=false) { - ledcWrite(Channel, 0); - //digitalWrite(IN1, LOW); - //digitalWrite(IN2, LOW); - digitalWrite(IN1, HIGH); - digitalWrite(IN2, HIGH); + ledcWrite(Channel0, 0); + ledcWrite(Channel1, 0); + orientation = STOP_MOTOR; + if(!emergency) protection_orientation = STOP_MOTOR; + //digitalWrite(IN1, HIGH); + //digitalWrite(IN2, HIGH); if(state_tapa == false) { WebSerial.println(String(num_loop) + "-Parando Motor:" + String(millis()-time_cycle)); @@ -106,31 +131,8 @@ void stop_motor() } } -void STOP() - { - digitalWrite(K1, LOW); - digitalWrite(K2, LOW); - } - -/*#define TIME_START 30000 -#define TIME_CLOSE 55000 -#define TIME_MOTOR_ON 60000 -#define TIME_MOTOR_OFF 65000 -#define TIME_LOOP 120000 -#define SPEED_MOTOR 180*/ - void START() { - - /*cierra(SPEED_MOTOR); - delay(1000); - stop_motor(); - delay(5000); - abrir(SPEED_MOTOR); - delay(1000); - stop_motor(); - delay(5000);*/ - if (((millis()-time_cycle)<TIME_START+TIME_TO_OPEN)&&(millis()-time_cycle)>TIME_START) { abrir(SPEED_MOTOR); @@ -150,11 +152,13 @@ void START() else if (((millis()-time_cycle)<TIME_VIBRATION_ON+TIME_VIBRATION_ACTIVE)&&(millis()-time_cycle)>TIME_VIBRATION_ON) { stop_motor(); + //WebSerial.println("Activa vibracion"); digitalWrite(K1, HIGH); digitalWrite(K2, HIGH); } else if (((millis()-time_cycle)<TIME_LOOP)&&(millis()-time_cycle)>TIME_VIBRATION_ON+TIME_VIBRATION_ACTIVE) { + //WebSerial.println("Desactiva vibracion"); digitalWrite(K1, LOW); digitalWrite(K2, LOW); } @@ -168,23 +172,83 @@ void START() } +const int N = 10; +float measure_current() + { + int pin = IS_L; + float current_correction = 1.0; + if (orientation==OPEN_MOTOR) + { + pin = IS_R; + } + + int val = analogRead(pin); + for (int i=0; i<N; i++) + val += analogRead(pin); + return (val/N)*(3300*current_correction)/4095; + } + +bool fc_stop_up = false; +volatile unsigned long time_fc_up_low = millis(); +volatile unsigned long time_fc_up_high = millis(); +void is_up() { + if(!digitalRead(FC_UP)&&(millis()-time_fc_up_low)>=100) + { + //Serial.println("Close_Up"); + time_fc_up_high = millis(); + protection_orientation = OPEN_MOTOR; + fc_stop_up = true; + } + else if(digitalRead(FC_UP)&&(millis()-time_fc_up_high)>=100) + { + //Serial.println("Open_Up"); + fc_stop_up = false; + time_fc_up_low = millis(); + } + //has_interrupted = true; +} + +volatile unsigned long time_fc_down_high = millis(); +volatile unsigned long time_fc_down_low = millis(); +bool fc_stop_down = false; +void is_down() { + if(!digitalRead(FC_DOWN)&&(millis()-time_fc_down_low)>=100) + { + //Serial.println("Close_Down"); + time_fc_down_high = millis(); + protection_orientation = CLOSE_MOTOR; + fc_stop_down = true; + } + else if(digitalRead(FC_DOWN)&&(millis()-time_fc_down_high)>=100) + { + //Serial.println("Open_Down"); + fc_stop_down = false; + time_fc_down_low = millis(); + } + //has_interrupted = true; +} + void setup() { IPAddress ip; delay(1000); - ledcSetup(Channel, frequency, resolution); + ledcSetup(Channel0, frequency, resolution); + ledcSetup(Channel1, frequency, resolution); + analogReadResolution(resolution_analog); pinMode(K1, OUTPUT); pinMode(K2, OUTPUT); - ledcAttachPin(ENA, Channel); - pinMode(IN1, OUTPUT); - pinMode(IN2, OUTPUT); + pinMode(FC_UP, INPUT_PULLUP); + pinMode(FC_DOWN, INPUT_PULLUP); + ledcAttachPin(PWM_R, Channel0); + ledcAttachPin(PWM_L, Channel1); + pinMode(EN_M, OUTPUT); digitalWrite(K1, LOW); digitalWrite(K2, LOW); stop_motor(); - //ledcWrite(Channel, 128); - //digitalWrite(IN1, LOW); - //digitalWrite(IN2, HIGH); + ledcWrite(Channel0, 0); + ledcWrite(Channel1, 0); + digitalWrite(EN_M, LOW); Serial.begin(115200); if(readId==0) ip=ip0; else if(readId==1) ip=ip1; @@ -284,15 +348,38 @@ void setup() time_cycle = millis(); });*/ time_cycle = millis(); + digitalWrite(EN_M, HIGH); + cierra(SPEED_MOTOR); + time_first_time = millis(); + //attachInterrupt(digitalPinToInterrupt(FC_UP), is_up, CHANGE); // Configurar la interrupción + //attachInterrupt(digitalPinToInterrupt(FC_DOWN), is_down, CHANGE); // Configurar la interrupción + + } + + void loop() { + is_down(); + is_up(); + if (((measure_current()>=2000)&&(orientation!=STOP_MOTOR)&&((millis()-time_protection)>=250))||((fc_stop_up)&&(orientation==OPEN_MOTOR))||((fc_stop_down)&&(orientation==CLOSE_MOTOR))) { //Para el motor si la corriente es mayor a 1.5A (Borrar en caso de no funcionar) + if((!fc_stop_up)&&(!fc_stop_down)) + { + protection_orientation = orientation; + Serial.println("Proteccion por corriente"); + } + else Serial.println("Proteccion por FC"); + stop_motor(true); + //Serial.println("Parada de emergencia"); + } + if (first_time) { - cierra(SPEED_MOTOR); - delay(1000); - stop_motor(); - first_time = false; + if (millis()-time_first_time>=1000) + { + stop_motor(); + first_time = false; + } } WebSerial.loop(); httpServer.handleClient();