EZR
Dependencies: CRC16 FreescaleIAP FreescaleWatchdog GGSProtocol LM75B PIMA Parameters PersistentCircularQueue SerialNumberV2COM mbed-dev-watchdog_2016_03_04
Fork of smartRamalKW by
sensor.cpp@10:7a1d1abb5172, 2015-07-23 (annotated)
- Committer:
- tpadovani
- Date:
- Thu Jul 23 22:46:18 2015 +0000
- Revision:
- 10:7a1d1abb5172
- Parent:
- 9:e501499af4ef
- Child:
- 16:cae76dbd681f
Mem?ria de massa de tens?o e comando para ler o rel?gio atual.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tpadovani | 5:1bc98277c257 | 1 | #include "sensor.h" |
tpadovani | 5:1bc98277c257 | 2 | |
tpadovani | 8:e8d86c66283c | 3 | extern AnalogIn voltage[]; |
tpadovani | 5:1bc98277c257 | 4 | extern ParametersBlock APP_PARAMETERS; |
tpadovani | 5:1bc98277c257 | 5 | |
tpadovani | 8:e8d86c66283c | 6 | Ticker tickerSamples; |
tpadovani | 8:e8d86c66283c | 7 | Timeout samplesTimeout; |
tpadovani | 8:e8d86c66283c | 8 | unsigned char currentChannel; |
tpadovani | 8:e8d86c66283c | 9 | float sample[DEFAULT_SAMPLES]; |
tpadovani | 8:e8d86c66283c | 10 | int currentSample; |
tpadovani | 8:e8d86c66283c | 11 | bool finished; |
tpadovani | 8:e8d86c66283c | 12 | bool timeout; |
tpadovani | 8:e8d86c66283c | 13 | |
tpadovani | 8:e8d86c66283c | 14 | float getTensaoInstantanea(unsigned char channel){ |
tpadovani | 8:e8d86c66283c | 15 | currentChannel = channel; |
tpadovani | 8:e8d86c66283c | 16 | currentSample = 0; |
tpadovani | 8:e8d86c66283c | 17 | finished = false; |
tpadovani | 8:e8d86c66283c | 18 | timeout = false; |
tpadovani | 8:e8d86c66283c | 19 | tickerSamples.attach_us(&readSample, APP_PARAMETERS.SAMPLES_DELAY_US); |
tpadovani | 8:e8d86c66283c | 20 | samplesTimeout.attach_us(&timeoutReadingSamples, (DEFAULT_SAMPLES + 2) * APP_PARAMETERS.SAMPLES_DELAY_US); |
tpadovani | 8:e8d86c66283c | 21 | |
tpadovani | 8:e8d86c66283c | 22 | while(!finished && !timeout){ |
tpadovani | 8:e8d86c66283c | 23 | wait(0.1f); |
tpadovani | 8:e8d86c66283c | 24 | } |
tpadovani | 8:e8d86c66283c | 25 | |
tpadovani | 8:e8d86c66283c | 26 | return calculateRMS(); |
tpadovani | 8:e8d86c66283c | 27 | } |
tpadovani | 8:e8d86c66283c | 28 | |
tpadovani | 8:e8d86c66283c | 29 | bool getEstadoSensor(unsigned char channel){ |
tpadovani | 8:e8d86c66283c | 30 | if(getTensaoInstantanea(channel) > APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V){ |
tpadovani | 7:fe8e827f4d1b | 31 | return SENSOR_COM_FORNECIMENTO; |
tpadovani | 7:fe8e827f4d1b | 32 | } else{ |
tpadovani | 7:fe8e827f4d1b | 33 | return SENSOR_SEM_FORNECIMENTO; |
tpadovani | 7:fe8e827f4d1b | 34 | } |
tpadovani | 5:1bc98277c257 | 35 | } |
tpadovani | 8:e8d86c66283c | 36 | |
tpadovani | 8:e8d86c66283c | 37 | void readSample(){ |
tpadovani | 8:e8d86c66283c | 38 | sample[currentSample++] = voltage[currentChannel]; |
tpadovani | 8:e8d86c66283c | 39 | if(currentSample == DEFAULT_SAMPLES){ |
tpadovani | 8:e8d86c66283c | 40 | finished = true; |
tpadovani | 8:e8d86c66283c | 41 | samplesTimeout.detach(); |
tpadovani | 8:e8d86c66283c | 42 | tickerSamples.detach(); |
tpadovani | 8:e8d86c66283c | 43 | } |
tpadovani | 8:e8d86c66283c | 44 | } |
tpadovani | 8:e8d86c66283c | 45 | |
tpadovani | 8:e8d86c66283c | 46 | void timeoutReadingSamples(){ |
tpadovani | 8:e8d86c66283c | 47 | tickerSamples.detach(); |
tpadovani | 8:e8d86c66283c | 48 | timeout = true; |
tpadovani | 8:e8d86c66283c | 49 | } |
tpadovani | 8:e8d86c66283c | 50 | |
tpadovani | 8:e8d86c66283c | 51 | float calculateRMS(){ |
tpadovani | 8:e8d86c66283c | 52 | float rms = 0; |
tpadovani | 8:e8d86c66283c | 53 | for(int i=0; i < DEFAULT_SAMPLES; i++){ |
tpadovani | 9:e501499af4ef | 54 | sample[i] = abs(sample[i]*APP_PARAMETERS.SAMPLES_ANG_COEF.floatValue + APP_PARAMETERS.SAMPLES_LIN_COEF.floatValue); |
tpadovani | 8:e8d86c66283c | 55 | rms += sample[i]*sample[i]; |
tpadovani | 8:e8d86c66283c | 56 | } |
tpadovani | 9:e501499af4ef | 57 | rms = rms / DEFAULT_SAMPLES; |
tpadovani | 8:e8d86c66283c | 58 | return sqrt(rms); |
tpadovani | 8:e8d86c66283c | 59 | } |