|
|
@ -0,0 +1,207 @@ |
|
|
|
#include <Arduino_FreeRTOS.h>
|
|
|
|
#include <semphr.h> // add the FreeRTOS functions for Semaphores (or Flags).
|
|
|
|
|
|
|
|
const int Trigger = 5; //Pin digital 2 para el Trigger del sensor
|
|
|
|
const int Echo = 6; //Pin digital 3 para el Echo del sensor
|
|
|
|
|
|
|
|
|
|
|
|
#define LedEvasion 2
|
|
|
|
#define LedEvasionLat 3
|
|
|
|
#define LedUltra 4
|
|
|
|
|
|
|
|
|
|
|
|
SemaphoreHandle_t xSerialSemaphore; |
|
|
|
|
|
|
|
void EncontrarEnemigo( void *pvParameters ); |
|
|
|
void SensorTras( void *pvParameters ); |
|
|
|
void SensoresLateral( void *pvParameters ); |
|
|
|
|
|
|
|
void setup() { |
|
|
|
|
|
|
|
pinMode(LedEvasion,OUTPUT); |
|
|
|
pinMode(LedEvasionLat,OUTPUT); |
|
|
|
pinMode(LedUltra,OUTPUT); |
|
|
|
|
|
|
|
pinMode(Trigger, OUTPUT); //pin como salida
|
|
|
|
pinMode(Echo, INPUT); //pin como entrada
|
|
|
|
digitalWrite(Trigger, LOW);//Inicializamos el pin con 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// initialize serial communication at 9600 bits per second:
|
|
|
|
Serial.begin(9600); |
|
|
|
|
|
|
|
while (!Serial) { |
|
|
|
; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( xSerialSemaphore == NULL ) // Check to confirm that the Serial Semaphore has not already been created.
|
|
|
|
{ |
|
|
|
xSerialSemaphore = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use to manage the Serial Port
|
|
|
|
if ( ( xSerialSemaphore ) != NULL ) |
|
|
|
xSemaphoreGive( ( xSerialSemaphore ) ); // Make the Serial Port available for use, by "Giving" the Semaphore.
|
|
|
|
} |
|
|
|
|
|
|
|
// Now set up two Tasks to run independently.
|
|
|
|
xTaskCreate( |
|
|
|
EncontrarEnemigo |
|
|
|
, "DigitalRead" // A name just for humans
|
|
|
|
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
|
|
|
|
, NULL //Parameters for the task
|
|
|
|
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
|
|
|
|
, NULL ); //Task Handle
|
|
|
|
|
|
|
|
xTaskCreate( |
|
|
|
SensorTras |
|
|
|
, "AnalogRead" // A name just for humans
|
|
|
|
, 128 // Stack size
|
|
|
|
, NULL //Parameters for the task
|
|
|
|
, 0 // Priority
|
|
|
|
, NULL ); //Task Handle
|
|
|
|
|
|
|
|
xTaskCreate( |
|
|
|
SensoresLateral |
|
|
|
, "AnalogRead" // A name just for humans
|
|
|
|
, 128 // Stack size
|
|
|
|
, NULL //Parameters for the task
|
|
|
|
, 1 // Priority
|
|
|
|
, NULL ); //Task Handle
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void loop() |
|
|
|
{ |
|
|
|
// Empty. Things are done in Tasks.
|
|
|
|
} |
|
|
|
|
|
|
|
/*--------------------------------------------------*/ |
|
|
|
/*---------------------- Tasks ---------------------*/ |
|
|
|
/*--------------------------------------------------*/ |
|
|
|
|
|
|
|
void EncontrarEnemigo( void *pvParameters __attribute__((unused)) ) // This is a Task.
|
|
|
|
{ |
|
|
|
/*
|
|
|
|
DigitalReadSerial |
|
|
|
Reads a digital input on pin 2, prints the result to the serial monitor |
|
|
|
|
|
|
|
This example code is in the public domain. |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (;;) // A Task shall never return or exit.
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
long t; //timepo que demora en llegar el eco
|
|
|
|
long d; //distancia en centimetros
|
|
|
|
|
|
|
|
digitalWrite(Trigger, HIGH); |
|
|
|
delayMicroseconds(10); //Enviamos un pulso de 10us
|
|
|
|
digitalWrite(Trigger, LOW); |
|
|
|
|
|
|
|
t = pulseIn(Echo, HIGH); //obtenemos el ancho del pulso
|
|
|
|
d = t/59; //escalamos el tiempo a una distancia en cm
|
|
|
|
|
|
|
|
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE ) |
|
|
|
{ |
|
|
|
|
|
|
|
Serial.println(d); |
|
|
|
|
|
|
|
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
|
|
|
|
} |
|
|
|
|
|
|
|
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void SensorTras( void *pvParameters __attribute__((unused)) ) // This is a Task.
|
|
|
|
{ |
|
|
|
|
|
|
|
for (;;) |
|
|
|
{ |
|
|
|
// read the input on analog pin 0:
|
|
|
|
int sensorValue = analogRead(A0); |
|
|
|
String linea=""; |
|
|
|
if(sensorValue>512) |
|
|
|
{ |
|
|
|
linea="Linea detectada"; |
|
|
|
|
|
|
|
//evadir
|
|
|
|
digitalWrite(LedEvasion,HIGH); |
|
|
|
vTaskDelay(50); |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
linea="Linea no detectada"; |
|
|
|
digitalWrite(LedEvasion,LOW); |
|
|
|
} |
|
|
|
|
|
|
|
// See if we can obtain or "Take" the Serial Semaphore.
|
|
|
|
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
|
|
|
|
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE ) |
|
|
|
{ |
|
|
|
Serial.println(linea); |
|
|
|
|
|
|
|
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
|
|
|
|
} |
|
|
|
|
|
|
|
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void SensoresLateral( void *pvParameters __attribute__((unused)) ) // This is a Task.
|
|
|
|
{ |
|
|
|
|
|
|
|
for (;;) |
|
|
|
{ |
|
|
|
// read the input on analog pin 0:
|
|
|
|
int sensorValueIzq = analogRead(A1); |
|
|
|
int sensorValueDer = analogRead(A2); |
|
|
|
String lineaIzq=""; |
|
|
|
String lineaDer=""; |
|
|
|
if(sensorValueIzq>512) |
|
|
|
{ |
|
|
|
lineaIzq="Linea Izquierda detectada"; |
|
|
|
|
|
|
|
//evadir
|
|
|
|
digitalWrite(LedEvasion,HIGH); |
|
|
|
vTaskDelay(50); |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
lineaIzq="Linea Izquierda no detectada"; |
|
|
|
digitalWrite(LedEvasion,LOW); |
|
|
|
} |
|
|
|
|
|
|
|
if(sensorValueDer>512) |
|
|
|
{ |
|
|
|
lineaDer="Linea Derecha detectada"; |
|
|
|
|
|
|
|
//evadir
|
|
|
|
digitalWrite(LedEvasion,HIGH); |
|
|
|
vTaskDelay(50); |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
lineaDer="Linea Derecha no detectada"; |
|
|
|
digitalWrite(LedEvasion,LOW); |
|
|
|
} |
|
|
|
|
|
|
|
// See if we can obtain or "Take" the Serial Semaphore.
|
|
|
|
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
|
|
|
|
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE ) |
|
|
|
{ |
|
|
|
Serial.println(lineaIzq); |
|
|
|
Serial.println(lineaDer); |
|
|
|
|
|
|
|
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
|
|
|
|
} |
|
|
|
|
|
|
|
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
|
|
|
|
} |
|
|
|
} |