Sonar Arduíno + Processing

Sonar sempre foi entusiasmante em painel de bordo, mostrando em pontos, aviões inimigo, submarinos e navios. Esse projeto, trata-se de um monitoramento de detecção de obstáculos com ondas ultrassônicas e Arduíno, lembra bastante um radar visto constantemente nos filmes, agora você poderá construir e ter um em sua casa. Desenvolvido por Dejan Nedelkovski aqui, e modificado por mim (add som de sonar, add biblioteca NewPing.h, entre outras melhorias).

Demo

Lista de componentes

  • 01 Sensor Ultra-sônico HC-SR04;
  • 01 Servo Motor (qualquer modelo);
  • 01 Placa Arduíno (qualquer modelo);
  • Fios e cabos.

Código Arduíno

/**
 * #######################################################################
 * Software Arduino Sonar Monitor
 *
 * alterado por Rodrigues <https://rodriguesfas.github.io>
 * by Dejan Nedelkovski <www.HowToMechatronics.com>  
 *########################################################################
 */

 /*Inclue bibliotecas*/
 #include <Servo.h>. 
 #include <NewPing.h>

 #define TRIGGER_PIN  5    /* Arduino pino trigger (envia onda)*/
 #define ECHO_PIN     6    /* Arduino pino echo (recebe onda) */
 #define MAX_distancia 100  /* Distancia máxima do ping em centimetro. */ 

 NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_distancia); /* NewPing configuração de pinos e distancia máxima. */

 int distancia;

 Servo myServo; /* Cria um objeto servo para controlar o servo motor. */

/**
 * setup -
 */
 void setup() {
  Serial.begin(9600); /* Config serial monitor (valor taxa de transmição). */
  myServo.attach(3);  /* Definido no qual pino é o servo motor ligado no arduino. */ 
}


/**
 * loop - 
 */
 void loop() {
  /* faz rotacionar o servo motroe de 15º à 165º graus. */
  for(int i = 15; i <= 165; i++){  
    myServo.write(i);
    delay(30);

    /* chama a função que calcula a sistância medida pelo sensor ultrasônico para cada grau. */
    distancia = calcularPING();

    Serial.print(i);         /* Envia o atual grau na porta serial. */
    Serial.print(",");       /* Envia carácter adicional à direita préximo ao valor anterior necessário mais tarde na IDE Processing  */ 
    Serial.print(distancia);  /* Envia o valor da distância na porta serial. */ 
    Serial.print(".");       /* Envia carácter à direita próximo ao valor anterior necessário mais tarde na IDE processing.*/ 

  }

  /* Repete as linha anteriores de 165º a 15º graus. */
  for(int i = 165; i > 15; i--){
    myServo.write(i);
    delay(30);
    
    distancia = calcularPING();
    
    Serial.print(i);
    Serial.print(",");
    Serial.print(distancia);
    Serial.print(".");
    
  }

}

/**
 * calcularPING - Função pa calcular a distácia medida pelo sensor ultrasônico..
 */
 int calcularPING(){ 
  unsigned int uS = sonar.ping();  /* Envia ping, retorna ping tempo em microseconds (uS). */
  distancia = uS / US_ROUNDTRIP_CM; /* Converte oing tempo para distancia em cm e retorana resultado. */
  return distancia;
}

 

Código Processing

/**
 * #######################################################################
 * Software Arduino Sonar Monitor
 *
 * alterado por Rodrigues <https://rodriguesfas.github.io>
 * by Dejan Nedelkovski <www.HowToMechatronics.com>  
 *########################################################################
 */

import processing.serial.*; /* biblioteca para comunicação serial */
import java.awt.event.KeyEvent; /*biblioteca para leitura de dados na porta serial. */
import java.io.IOException;
import ddf.minim.*;

Serial myPort; /* Define objeto serial */

/* define algumas variaveis.. */
String angle="";
String distance="";
String data="";
String noObject;
float pixsDistance;
int iAngle, iDistance;
int index1=0;
int index2=0;
//PFont orcFont;
PFont myFont;

/* Audio */
AudioPlayer player;
Minim minim;//audio context

/**
 * setup - 
 */
void setup() {

  size (1024, 768); /* ***Alterar para resolução que desejar*** */
  smooth();
  myPort = new Serial(this, "/dev/ttyACM0", 9600); /* inicia comunicação serial. */
  myPort.bufferUntil('.'); /* Lê oas dados atravéz da porta serial (angulo e distância) */

  //orcFont = loadFont("OCRAExtended-30.vlw");
  myFont = createFont("verdana", 10); /* define tipo de fonte e tamanho. */

  /* Som */
  minim = new Minim(this);
  player = minim.loadFile("/home/rodriguesfas/Musica/SoundEffect/SonarEffect.mp3");
  player.rewind();
  player.loop(); //(play - uma vez) / (loop - indeterminado.)
}

/**
 * draw - 
 */
void draw() {
  fill(98, 245, 31);
  //textFont(orcFont);
  textFont(myFont); 

  /* simulando o borrão de movimento e lento desaparecer da linha móvel. */
  noStroke();
  fill(0, 4); 
  rect(0, 0, width, height-height*0.065); 

  fill(98, 245, 31); /* cor verde */

  /* chama as funções para desenhar o radar. */
  drawRadar(); 
  drawLine();
  drawObject();
  drawText();
}

/**
 * serialEvent --
 */
void serialEvent (Serial myPort) { /* começa a ler os dados aparti da porta serial. */
  /* lê os dados na porta serial (caractere) e armazena em uma string de dados. */
  data = myPort.readStringUntil('.');
  data = data.substring(0, data.length()-1);

  index1 = data.indexOf(","); /* encontra o caractere e armazena na variavel "index1" */
  angle= data.substring(0, index1); /* ler os dados da posição "0" a posição do index1 váriavel ou isso é o valor do ânculo da placa Ardíno enviado */
  distance= data.substring(index1+1, data.length()); /* ler os dados de posição "index1" para o final do pr de dados que é o valor da distância. */

  // converte string para inteiro.
  iAngle = int(angle);
  iDistance = int(distance);
}

/**
 * drawRadar - 
 */
void drawRadar() {
  pushMatrix();
  translate(width/2, height-height*0.074); /* move as coordenadas para o novo local.  */
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);

  // draws the arc lines
  arc(0, 0, (width-width*0.0625), (width-width*0.0625), PI, TWO_PI);
  arc(0, 0, (width-width*0.27), (width-width*0.27), PI, TWO_PI);
  arc(0, 0, (width-width*0.479), (width-width*0.479), PI, TWO_PI);
  arc(0, 0, (width-width*0.687), (width-width*0.687), PI, TWO_PI);

  // draws the angle lines
  line(-width/2, 0, width/2, 0);
  line(0, 0, (-width/2)*cos(radians(30)), (-width/2)*sin(radians(30)));
  line(0, 0, (-width/2)*cos(radians(60)), (-width/2)*sin(radians(60)));
  line(0, 0, (-width/2)*cos(radians(90)), (-width/2)*sin(radians(90)));
  line(0, 0, (-width/2)*cos(radians(120)), (-width/2)*sin(radians(120)));
  line(0, 0, (-width/2)*cos(radians(150)), (-width/2)*sin(radians(150)));
  line((-width/2)*cos(radians(30)), 0, width/2, 0);

  popMatrix();
}

/**
 * drawObject - 
 */
void drawObject() {
  pushMatrix();
  translate(width/2, height-height*0.074);  /* move as coordenadas para o novo local.  */
  strokeWeight(9);
  stroke(255, 10, 10); /* cor vermelho */

  pixsDistance = iDistance*((height-height*0.1666)*0.025); /* converte a distância entre o sensor de cm para px */

  /* limite de faixa de 40 cms */
  if (iDistance<40 & iDistance > 3) {
    /* desenha o objeto de acordo com o ângulo e a distância. */
    line(pixsDistance*cos(radians(iAngle)), -pixsDistance*sin(radians(iAngle)), (width-width*0.505)*cos(radians(iAngle)), -(width-width*0.505)*sin(radians(iAngle)));
  }
  popMatrix();
}

/**
 * drawLine - 
 */
void drawLine() {
  pushMatrix();
  strokeWeight(9);
  stroke(30, 250, 60);
  translate(width/2, height-height*0.074); /* move as coordenada iniciais para o novo local. */
  line(0, 0, (height-height*0.12)*cos(radians(iAngle)), -(height-height*0.12)*sin(radians(iAngle))); /* chama a linha de acordo com o angulo */
  popMatrix();
}

/**
 * drawText - 
 */
void drawText() { /* desenha os textos na tela. */

  pushMatrix();

  if (iDistance > 40) {
    noObject = "Fora de alcance!";
  } else {
    noObject = "Na faixa!";
  }

  fill(0, 0, 0);
  noStroke();
  rect(0, height-height*0.0648, width, height);
  fill(98, 245, 31);
  textSize(12);

  text("10cm", width-width*0.3854, height-height*0.0833);
  text("20cm", width-width*0.281, height-height*0.0833);
  text("30cm", width-width*0.177, height-height*0.0833);
  text("40cm", width-width*0.0729, height-height*0.0833);
  textSize(12);

  text("Objeto: " + noObject, width-width*0.875, height-height*0.0277);
  text("Ângulo: " + iAngle +" °", width-width*0.48, height-height*0.0277);
  text("Distância: ", width-width*0.26, height-height*0.0277);

  if (iDistance < 40) {
    text("        " + iDistance +" cm", width-width*0.225, height-height*0.0277);
  }

  /*plano cartesiano*/

  textSize(20);
  fill(98, 245, 60);
  translate((width-width*0.4994)+width/2*cos(radians(30)), (height-height*0.0907)-width/2*sin(radians(30)));
  rotate(-radians(-60));
  text("30°", 0, 0);
  resetMatrix();
  translate((width-width*0.503)+width/2*cos(radians(60)), (height-height*0.0888)-width/2*sin(radians(60)));
  rotate(-radians(-30));
  text("60°", 0, 0);
  resetMatrix();
  translate((width-width*0.507)+width/2*cos(radians(90)), (height-height*0.0833)-width/2*sin(radians(90)));
  rotate(radians(0));
  text("90°", 0, 0);
  resetMatrix();
  translate(width-width*0.513+width/2*cos(radians(120)), (height-height*0.07129)-width/2*sin(radians(120)));
  rotate(radians(-30));
  text("120°", 0, 0);
  resetMatrix();
  translate((width-width*0.5104)+width/2*cos(radians(150)), (height-height*0.0574)-width/2*sin(radians(150)));
  rotate(radians(-60));
  text("150°", 0, 0);
  popMatrix();
}

/**
 * stop - 
 */
void stop() {
  player.close();
  minim.stop();
  super.stop();
}

GitHub

 

Espero que tenha gostado, não deixe de compartilhar.

Francisco de Assis

Servo de DEUS, Mestrando em Ciências da Computação (UFPE) Pós-Graduado em Docência do Ensino Superior (IDJ/UVA), Graduando em Automação Industrial (IFCE), Graduado em Analise e Desenvolvimento de Sistemas (UNILEÃO), casado com a mulher mais maravilhosa, Tamires Alencar e amante Python, Java, Games, Eletrônica, Robótica, Violão, Aviação...

Você pode gostar...

6 Resultados

  1. Rodrigo disse:

    Boa tarde!

    Você tem um passo a passo mais detalhado ou pode me dizer onde encontrá-lo?

    Obrigado!

  2. Paula disse:

    Tem mais detalhes desse projeto? Em qual programa vc rodou o processing?

  3. tiago disse:

    teria como usar esse sonar para controlar motores? por exemplo naqueles carrinhos autônomos em vez de deixar um sensor fixo ou usar vários sensores usar apenas um simulando esse sonar

  4. Marcelo disse:

    Como eu vejo essa imagem do sonar no monitor? obg

Deixe um comentário para Francisco de Assis Cancelar resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *