ITI "DallaChiesa" 2019 / hcsr04

Dependents:   progetto2 progetto3 progetto4 progetto5 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hcsr04.cpp Source File

hcsr04.cpp

00001 /* Copyright (c) 2013 Prabhu Desai
00002  * pdtechworld@gmail.com
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 
00021 #include "hcsr04.h"
00022 
00023 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
00024     trigger(TrigPin), echo(EchoPin)
00025 {
00026     pulsetime.stop();
00027     pulsetime.reset();
00028     echo.rise(this,&HCSR04::isr_rise);
00029     echo.fall(this,&HCSR04::isr_fall);
00030     trigger=0;
00031 }
00032 
00033 HCSR04::~HCSR04()
00034 {
00035 }
00036 
00037 void HCSR04::isr_rise(void)
00038 {
00039     pulsetime.start();
00040 }
00041 void HCSR04::start(void)
00042 {
00043     trigger=1;
00044     wait_us(10);
00045     trigger=0;
00046 }
00047 
00048 void HCSR04::isr_fall(void)
00049 {
00050     pulsetime.stop();
00051     pulsedur = pulsetime.read_us();
00052     distance= (pulsedur*343)/20000;
00053     pulsetime.reset();
00054 }
00055 
00056 void HCSR04::rise (void (*fptr)(void))
00057 {
00058     echo.rise(fptr);
00059 }
00060 void HCSR04::fall (void (*fptr)(void))
00061 {
00062     echo.fall(fptr);
00063 }
00064 
00065 unsigned int HCSR04::get_dist_cm()
00066 {
00067     return distance;
00068 }
00069 unsigned int HCSR04::get_pulse_us()
00070 {
00071     return pulsedur;
00072 }
00073 
00074 float HCSR04::filter(int* measure, int n) {
00075     int temp;
00076     for(int i=0; i<n-1; i++) {
00077         for(int k=0; k<n-1-i; k++) {
00078             if(measure[k] > measure[k+1]) {
00079                 temp = measure[k];
00080                 measure[k] = measure[k+1];
00081                 measure[k+1] = temp;
00082             }
00083          }
00084      }
00085     if(n%2==0) {
00086         int pos_1 = (int)n/2.0 + .5;
00087         int pos_2 = (int)n/2.0 - .5;
00088         value = (measure[pos_1]+measure[pos_2])/2;
00089     } else {
00090         value = measure[n/2];
00091     }
00092 
00093     return value;
00094 }
00095 
00096 /*******************************************************
00097    Here is a sample code usage
00098 ********************************************************* 
00099 #include "hcsr04.h"
00100 HCSR04  usensor(p25,p6);
00101 int main()
00102 {
00103     unsigned char count=0;
00104     while(1) {
00105         usensor.start();
00106         wait_ms(500); 
00107         dist=usensor.get_dist_cm();
00108         lcd.cls();
00109         lcd.locate(0,0);
00110         lcd.printf("cm:%ld",dist );
00111  
00112         count++;
00113         lcd.locate(0,1);
00114         lcd.printf("Distance =%d",count);
00115         
00116     }
00117 */