Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PING_rangefinder.h
00001 #pragma once 00002 #ifndef PING_RANGEFINDER_H 00003 #define PING_RANGEFINDER_H 00004 00005 #include <processes.h> 00006 #include <Mutexes.h> 00007 #include <channels.h> 00008 #define PINGPIN p22 00009 00010 00011 //DigitalOut led3(LED3); //these LEDs are used to indicate when an interrupt occurs. 00012 //DigitalOut led4(LED4); 00013 //byte count = 0; 00014 00015 class rangefinder { 00016 public: 00017 rangefinder() 00018 :Pinterrupt(PINGPIN), PingPin(PINGPIN) 00019 { 00020 //led3 = ~(led4 = 0); 00021 00022 time = 0; 00023 enable_flag = false; 00024 set_interrupts(); 00025 PingPin.mode(PullDown); 00026 //Pinterrupt.mode(PullDown); 00027 // Pinterrupt.mode(PullUp); 00028 PingPin.output(); //set pingpin as output 00029 PingPin = 0; 00030 } 00031 00032 void ping() { 00033 T.reset(); //reset the timer to 0 00034 set_interrupts(); 00035 enable_flag = false; 00036 PingPin.output(); //set as output 00037 PingPin = 0; //output initiation sequence of 0 1 0 to the rangefinder 00038 wait_us(2); 00039 PingPin = 1; 00040 wait_us(5); 00041 PingPin = 0; 00042 wait_us(1); 00043 //printf("now waiting for input.\n"); 00044 PingPin.input(); //set as input 00045 wait_us(10); 00046 //set_interrupts(); 00047 enable_flag = true; 00048 wait_ms(25); //more than required for maximum distance measurement 00049 00050 00051 00052 //T.start(); //begin timing the length of time for an interrupt to occur 00053 } 00054 void set_interrupts() 00055 { 00056 Pinterrupt.fall(this, &rangefinder::set_time); 00057 Pinterrupt.rise(this, &rangefinder::start_timing); 00058 } 00059 void disable_interrupts() 00060 { 00061 Pinterrupt.fall(NULL); 00062 Pinterrupt.rise(NULL); 00063 00064 } 00065 void start_timing() { OS::TISRW ISRW; 00066 if(enable_flag == true) 00067 { 00068 T.start(); 00069 //led3 = !led3; 00070 //count++; 00071 } 00072 00073 //printf("start_timing called.\n"); 00074 } 00075 00076 void set_time() { OS::TISRW ISRW; 00077 if(enable_flag == true) 00078 { 00079 T.stop(); 00080 //led4 = !led4; 00081 //disable_interrupts(); 00082 //enable_flag = false; 00083 //printf("set_time being called...\n"); 00084 time = T.read_us(); 00085 //count++; 00086 } 00087 //printf("value stored in time %f: \n", time); 00088 00089 //PingPin.output(); 00090 //PingPin = 0; 00091 //printf("PING:%f\n",get_time()); 00092 00093 } 00094 00095 00096 00097 float get_time() { 00098 //disable_interrupts(); 00099 return time/2.0; 00100 00101 } 00102 00103 00104 private: 00105 Timer T; 00106 float time; //time in us 00107 InterruptIn Pinterrupt; 00108 DigitalInOut PingPin; 00109 bool enable_flag; 00110 //Ticker TICKER;// send_ticker; 00111 00112 00113 }; 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 extern BusOut leds; 00125 00126 template<> OS_PROCESS void PING_PROC::Exec() //Output stream handling process 00127 { 00128 byte M = 0; 00129 rangefinder PING; 00130 00131 00132 char PIN_Return_chars[100]; //defined in accelerometer.h 00133 00134 for(;;) 00135 { 00136 if(PING_MESSAGE.wait(80)) //length of time to wait also determines how rapidly to ticker ping 00137 { leds = 0x2; 00138 M = PING_MESSAGE; //read the message that was destined for this process 00139 PING_MESSAGE.reset(); 00140 00141 if(M == 0) //then ping! command was sent 00142 { 00143 { 00144 //OS::TISRW ISRW; //this is a critical section as timing is involved 00145 //this will not work though as it disables interrupts. 00146 PING.ping(); 00147 } 00148 00149 { 00150 //TCritSect cs; 00151 sprintf(PIN_Return_chars, "PING:%f\n", PING.get_time()); // 00152 } 00153 //Ser_out_Mutex.Lock(); 00154 //if(TX_Channel.get_free_size() > strlen(Return_chars)+1) 00155 TX_channel.write(PIN_Return_chars, strlen(PIN_Return_chars)+1); //output the ping result 00156 //Ser_out_Mutex.Unlock(); 00157 00158 00159 } 00160 } 00161 if(M == 1) //ticker ping 00162 { 00163 { 00164 //OS::TISRW ISRW; //this is a critical section as timing is involved 00165 PING.ping(); 00166 } 00167 //Sleep(2); 00168 { 00169 //TCritSect cs; 00170 sprintf(PIN_Return_chars, "PING:%f\n", PING.get_time()); //"PING:%f\n" 00171 } 00172 //Ser_out_Mutex.Lock(); 00173 //if(TX_Channel.get_free_size() > strlen(Return_chars)+1) 00174 TX_channel.write(PIN_Return_chars, strlen(PIN_Return_chars)+1); //output the result 00175 //Ser_out_Mutex.Unlock(); 00176 //TX_flag.Signal(); 00177 00178 } 00179 00180 //PING_MESSAGE.reset(); 00181 00182 leds = 0x2; 00183 00184 } 00185 } 00186 00187 00188 #endif 00189 00190 //void enable_ticker() { 00191 // TICKER.attach_us(this, &rangefinder::ping, TX_RATE); //call ping() every 40 ms 00192 //send_ticker.attach_us(this, &rangefinder::send_data, TX_RATE); //send the data out every 40 ms 00193 //} 00194 //void disable_ticker() { 00195 //printf("ticker should now be detached.\n"); 00196 // TICKER.detach(); 00197 //send_ticker.detach(); 00198 //} 00199 00200 00201 00202 00203
Generated on Wed Jul 13 2022 05:20:16 by
1.7.2