Netduino - Sensor de Estacionamento
Autor: Marcio Paulo Martins | Publicado: 2013-04-24T07:42:00-07:00

Olá, amigos!

Como estou sempre buscando algo novo para fazer, esta semana resolvi usar meu sensor ultrassônico HC-SR04 para uma experiência com o Netduino: um sensor de proximidade que emite um bip repetidamente; quanto mais próximo o objeto estiver do sensor, mais rápido o bip toca.

Isso fez o aparelho ter basicamente a mesma característica de um sensor de estacionamento desses que equipam os carros mais modernos, ou que são vendidos em lojas de acessórios para carros.

O objetivo, porém, não é instalar o dispositivo no meu carro: é simplesmente mostrar a utilização do sensor no Netduino e exemplificar como o mesmo lida com tarefas em MultiThread, já que a leitura do sensor é feita pela thread principal, e o bip é controlado por uma thread paralela.

O código fonte do projeto para o Netduino é o seguinte:


Program.cs

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;


// Escrito por Marcio Paulo Mello Martins para testar o sensor HC-SR04
namespace SensorAproximacao
{
&nbsp; &nbsp; public class Program
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; private static int ticks;

&nbsp; &nbsp; &nbsp; &nbsp; private static InterruptPort echoPin = new InterruptPort(Pins.GPIO_PIN_D12, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
&nbsp; &nbsp; &nbsp; &nbsp; private static OutputPort triggerPin = new OutputPort(Pins.GPIO_PIN_D13, false);
&nbsp; &nbsp; &nbsp; &nbsp; private static Beep beep = new Beep() { BeepInterval = 3000 };

&nbsp; &nbsp; &nbsp; &nbsp; public static void Main()
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echoPin.OnInterrupt += new NativeEventHandler(Port_OnInterrupt);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echoPin.DisableInterrupt();

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread t = new Thread(new ThreadStart(BeepThread));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.Start();

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Distance();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(250);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; public static void Distance()
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echoPin.EnableInterrupt();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; triggerPin.Write(false);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(2);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; triggerPin.Write(true);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(10);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; triggerPin.Write(false);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(2);
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; private static void Port_OnInterrupt(uint port, uint state, DateTime time)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (state == 0) // falling edge, end of pulse
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pulseWidth = (int)time.Ticks - ticks;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int distance = (pulseWidth * 10 / (int)578.29);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Print("Distancia = " + distance.ToString() + " milimetros");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lock (beep)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beep.BeepInterval = distance;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticks = (int)time.Ticks;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echoPin.ClearInterrupt();
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; 

&nbsp; &nbsp; &nbsp; &nbsp; private static void BeepThread()
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool play = false;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int localInterval;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lock(beep)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; play = beep.BeepInterval &lt;= 1000;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; localInterval = beep.BeepInterval;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (play)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beep.Play();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(localInterval);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
}


Beep.cs


using System;
using Microsoft.SPOT;
using SecretLabs.NETMF.Hardware;
using System.Threading;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace SensorAproximacao
{
&nbsp; &nbsp; internal class Beep
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; internal int BeepInterval { get; set; }
&nbsp; &nbsp; &nbsp; &nbsp; internal PWM speaker = new PWM(Pins.GPIO_PIN_D9);

&nbsp; &nbsp; &nbsp; &nbsp; internal void Play()
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speaker.SetDutyCycle(100);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(100);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speaker.SetDutyCycle(0);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
}




O link para o vídeo no YouTube também está aqui:
Sensor de Estacionamento com Netduino





Espero que gostem!

Até a próxima!