This library demonstrates how to use the sensors on the QW Shield.
Dependents: QW-TEMP_GPS-NMEA QW-Motiondetection QW-Closet-detection
LinearTempSensor.cpp@0:4b56d28cc7e9, 2015-12-02 (annotated)
- Committer:
- quicksand
- Date:
- Wed Dec 02 11:39:52 2015 +0000
- Revision:
- 0:4b56d28cc7e9
This library demonstrates how to use the sensors on the QW Shield.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
quicksand | 0:4b56d28cc7e9 | 1 | /* mbed Linear Temperature Sensor library |
quicksand | 0:4b56d28cc7e9 | 2 | * Supports Microchip MCP9700/9701, National Semiconductor LM35 |
quicksand | 0:4b56d28cc7e9 | 3 | * |
quicksand | 0:4b56d28cc7e9 | 4 | * Written by Todotani, Nov 22 2010 |
quicksand | 0:4b56d28cc7e9 | 5 | */ |
quicksand | 0:4b56d28cc7e9 | 6 | |
quicksand | 0:4b56d28cc7e9 | 7 | #include "LinearTempSensor.h" |
quicksand | 0:4b56d28cc7e9 | 8 | |
quicksand | 0:4b56d28cc7e9 | 9 | LinearTempSensor::LinearTempSensor(PinName ain, int N, SensorType type): |
quicksand | 0:4b56d28cc7e9 | 10 | // Initialize private variables using Initialization Lists |
quicksand | 0:4b56d28cc7e9 | 11 | // _ain(ain) is equivalent to _ain = new AnalogIn(ain) |
quicksand | 0:4b56d28cc7e9 | 12 | // _sample(N) is equivalent to _sample = N |
quicksand | 0:4b56d28cc7e9 | 13 | _ain(ain), _samples(N), _type(type) { |
quicksand | 0:4b56d28cc7e9 | 14 | sampleBuffer = (float *)malloc(sizeof(float) * _samples); |
quicksand | 0:4b56d28cc7e9 | 15 | sampleCount = 0; |
quicksand | 0:4b56d28cc7e9 | 16 | index = 0; |
quicksand | 0:4b56d28cc7e9 | 17 | Vref = 3300; |
quicksand | 0:4b56d28cc7e9 | 18 | bufferNotFilled = true; |
quicksand | 0:4b56d28cc7e9 | 19 | |
quicksand | 0:4b56d28cc7e9 | 20 | // Set constants to calculate temperature from sensor value |
quicksand | 0:4b56d28cc7e9 | 21 | switch (_type) { |
quicksand | 0:4b56d28cc7e9 | 22 | case LM35: |
quicksand | 0:4b56d28cc7e9 | 23 | V0 = 0.0f; |
quicksand | 0:4b56d28cc7e9 | 24 | Tc = 10.0f; |
quicksand | 0:4b56d28cc7e9 | 25 | break; |
quicksand | 0:4b56d28cc7e9 | 26 | case MCP9701: |
quicksand | 0:4b56d28cc7e9 | 27 | V0 = 400.0f; |
quicksand | 0:4b56d28cc7e9 | 28 | Tc = 19.5f; |
quicksand | 0:4b56d28cc7e9 | 29 | break; |
quicksand | 0:4b56d28cc7e9 | 30 | case MCP9700: |
quicksand | 0:4b56d28cc7e9 | 31 | default: |
quicksand | 0:4b56d28cc7e9 | 32 | V0 = 500.0f; |
quicksand | 0:4b56d28cc7e9 | 33 | Tc = 10.0f; |
quicksand | 0:4b56d28cc7e9 | 34 | } |
quicksand | 0:4b56d28cc7e9 | 35 | |
quicksand | 0:4b56d28cc7e9 | 36 | for (int i = 0; i < _samples; i++) |
quicksand | 0:4b56d28cc7e9 | 37 | sampleBuffer[i] = 0.0f; |
quicksand | 0:4b56d28cc7e9 | 38 | } |
quicksand | 0:4b56d28cc7e9 | 39 | |
quicksand | 0:4b56d28cc7e9 | 40 | |
quicksand | 0:4b56d28cc7e9 | 41 | LinearTempSensor::~LinearTempSensor() { |
quicksand | 0:4b56d28cc7e9 | 42 | free(sampleBuffer); |
quicksand | 0:4b56d28cc7e9 | 43 | } |
quicksand | 0:4b56d28cc7e9 | 44 | |
quicksand | 0:4b56d28cc7e9 | 45 | |
quicksand | 0:4b56d28cc7e9 | 46 | float LinearTempSensor::Sense() { |
quicksand | 0:4b56d28cc7e9 | 47 | float val; |
quicksand | 0:4b56d28cc7e9 | 48 | |
quicksand | 0:4b56d28cc7e9 | 49 | val = _ain * Vref; |
quicksand | 0:4b56d28cc7e9 | 50 | sampleBuffer[index] = val; |
quicksand | 0:4b56d28cc7e9 | 51 | //printf("Index:%d ", index); // Debug print |
quicksand | 0:4b56d28cc7e9 | 52 | if ( ++index >= _samples ) |
quicksand | 0:4b56d28cc7e9 | 53 | index = 0; |
quicksand | 0:4b56d28cc7e9 | 54 | |
quicksand | 0:4b56d28cc7e9 | 55 | if ( bufferNotFilled && (sampleCount == _samples) ) { |
quicksand | 0:4b56d28cc7e9 | 56 | bufferNotFilled = false; |
quicksand | 0:4b56d28cc7e9 | 57 | } |
quicksand | 0:4b56d28cc7e9 | 58 | //printf("flag:%d ", bufferNotFilled); // Debug print |
quicksand | 0:4b56d28cc7e9 | 59 | sampleCount++; |
quicksand | 0:4b56d28cc7e9 | 60 | |
quicksand | 0:4b56d28cc7e9 | 61 | return val; |
quicksand | 0:4b56d28cc7e9 | 62 | } |
quicksand | 0:4b56d28cc7e9 | 63 | |
quicksand | 0:4b56d28cc7e9 | 64 | |
quicksand | 0:4b56d28cc7e9 | 65 | float LinearTempSensor::GetAverageTemp() { |
quicksand | 0:4b56d28cc7e9 | 66 | float sum = 0; |
quicksand | 0:4b56d28cc7e9 | 67 | int i, numberOfsumples; |
quicksand | 0:4b56d28cc7e9 | 68 | |
quicksand | 0:4b56d28cc7e9 | 69 | if (sampleCount == 0) |
quicksand | 0:4b56d28cc7e9 | 70 | return 0; |
quicksand | 0:4b56d28cc7e9 | 71 | |
quicksand | 0:4b56d28cc7e9 | 72 | if (bufferNotFilled) { |
quicksand | 0:4b56d28cc7e9 | 73 | // In case number of samples less than buffer lenght |
quicksand | 0:4b56d28cc7e9 | 74 | for (i = 0; i < sampleCount; i++) { |
quicksand | 0:4b56d28cc7e9 | 75 | sum += sampleBuffer[i]; |
quicksand | 0:4b56d28cc7e9 | 76 | } |
quicksand | 0:4b56d28cc7e9 | 77 | numberOfsumples = sampleCount; |
quicksand | 0:4b56d28cc7e9 | 78 | } else { |
quicksand | 0:4b56d28cc7e9 | 79 | // In case buffer is filled |
quicksand | 0:4b56d28cc7e9 | 80 | for (i = 0; i < _samples; i++) { |
quicksand | 0:4b56d28cc7e9 | 81 | sum += sampleBuffer[i]; |
quicksand | 0:4b56d28cc7e9 | 82 | } |
quicksand | 0:4b56d28cc7e9 | 83 | numberOfsumples = _samples; |
quicksand | 0:4b56d28cc7e9 | 84 | } |
quicksand | 0:4b56d28cc7e9 | 85 | |
quicksand | 0:4b56d28cc7e9 | 86 | return ((sum / numberOfsumples) - V0) / Tc; // Temp = (Vout - V0) / Tc |
quicksand | 0:4b56d28cc7e9 | 87 | } |
quicksand | 0:4b56d28cc7e9 | 88 | |
quicksand | 0:4b56d28cc7e9 | 89 | |
quicksand | 0:4b56d28cc7e9 | 90 | float LinearTempSensor::GetLatestTemp() { |
quicksand | 0:4b56d28cc7e9 | 91 | return (sampleBuffer[(sampleCount-1)%_samples] - V0) / Tc; |
quicksand | 0:4b56d28cc7e9 | 92 | } |