miércoles, 5 de octubre de 2016

Sistema de Radar con MicroControlador Arduino

He diseñado un Sistema de Radar que permite a los docentes y estudiantes entender el funcionamiento de los radares, sonares y eco localizadores. El sistema se fundamenta en el uso de los micros controladores. El  funcionamiento del sistema y los  respectivos códigos se pueden observar en el siguiente vídeo…..  




Código para la programación del Arduino

#include <Servo.h>.
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin


#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
#define MINPRESSURE 10
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

// Assign human-readable names to some common 16-bit color values:
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

Servo myServo; // Creates a servo object for controlling the servo motor
const int trigPin = 26;
const int echoPin = 24;
// Variables for the duration and the distance

long duration;
int distance,xpos,ypos,iOld;
float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;
int16_t x0 = 0, x1 = 0, yy0 = 0, yy1 = 0, x00 = 0, yy00 = 0,ang,iTemp;
String tmp="";

void setup() {
  Serial.begin(9600);
   tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(0);
  tft.fillScreen(BLACK);
  tft.fillScreen(RED);
  tft.fillScreen(GREEN);
  tft.fillScreen(BLUE);
  tft.fillScreen(BLACK);
  delay(100);

  //tft.setRotation(0);
 // put your setup code here, to run once:
 myServo.attach(52); // Defines on which pin is the servo motor attached 
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  
  tft.setRotation(1);
  radar();
}

void loop() {
   
  // put your main code here, to run repeatedly:
 for(int i=15;i<=165;i=i+2)
 {  
  myServo.write(i);
   //angulo(iOld,BLACK);
   if (distance>30)
    angulo(i,GREEN);
    else
    angulo(i,RED);
   tmp=i;
 tft.fillRect(125,141,40,16,BLACK);
  texto( tmp,125,141,2,GREEN);  
  iOld=i;

  //delay(10);
   distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
   Serial.print(i); // Sends the current degree into the Serial Port
  Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  Serial.print(distance); // Sends the distance value into the Serial Port
  Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  tmp=distance;
 tft.fillRect(125,161,40,16,BLACK);
  texto( tmp,125,161,2,GREEN);  
 }
radar();
 for(int i=165;i>15;i=i-2){  
  myServo.write(i);
  //angulo(iOld,BLACK);
   if (distance>30)
    angulo(i,GREEN);
    else
    angulo(i,RED);
   tmp=i;
 tft.fillRect(125,141,40,16,BLACK);
  texto( tmp,125,141,2,GREEN);  
  iOld=i;

  //delay(10);
   distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
  Serial.print(i); // Sends the current degree into the Serial Port
  Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  Serial.print(distance); // Sends the distance value into the Serial Port
  Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  tmp=distance;
 tft.fillRect(125,161,40,16,BLACK);
  texto( tmp,125,161,2,GREEN);  
}
radar();
}
int calculateDistance(){ 
  
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  distance= duration*0.034/2;
  return distance;
}

void radar()
{
   
   tft.fillRect(0,0,tft.width(),tft.height(),BLACK);
   texto( "Radar Posicion",75,125,2,WHITE);
   texto( "Ang=",75,141,2,GREEN);
   texto( "Dis=",75,161,2,YELLOW);
   
   xpos = tft.width() / 2; 
   ypos = tft.height() / 2; 
  tft.drawCircle(xpos, ypos, 120, YELLOW);
  for ( ang=0;ang<=180;ang=ang+30)
  {
  
  angulo ( ang, BLUE);
}
}

void texto(String texto,int x,int y,int Size,int color)
{
    tft.setTextColor(color);
     tft.setCursor(x, y);
     tft.setTextSize(Size);
     tft.println(texto);
}

void angulo (int16_t ang, int color)
{


sx = cos(( - ang) * 0.0174532925);
    sy = sin(( - ang) * 0.0174532925);
    x0 = sx * 114 + xpos;
    yy0 = sy * 114 + 120;
    x1 = sx * 100 + xpos;
    yy1 = sy * 100 + 120;

    tft.drawLine(x0, yy0, xpos, ypos, color);
   
}

void coordenadas()
{
  for ( ang=0;ang<=180;ang=ang+30)
  {
  
  angulo ( ang,BLUE);
}
  
}


No hay comentarios:

Publicar un comentario