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.
pressuresensor.cpp
00001 /* Copyright (c) 2017 Philippe Kalaf, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 /* Analog output pressure sensor driver */ 00020 #include "pressuresensor.h" 00021 00022 PressureSensor::PressureSensor(PinName pin) : 00023 _input(pin), 00024 _worker_thread(osPriorityNormal, 256) 00025 { 00026 _worker_thread.start(callback(this, &PressureSensor::_worker)); 00027 } 00028 00029 // returns pressure in bar 00030 float PressureSensor::read_bars() 00031 { 00032 return float(read_pa())/100000; 00033 } 00034 00035 // returns pressure in Pa, 100000 Pa = 1 bar 00036 // avoids float calculations for 00037 int PressureSensor::read_pa() 00038 { 00039 // sensor provides linear voltage between 0.5V to 5V representing 0 to 20 bars 00040 // 0.5V to 3.3V <-> 0 bar to 12.44 bar 00041 // read_u16 should return a value between 9930 and 65535 representing 0.5 to 3.3V 00042 // measurements show a range between 10300 and 60000 respresenting 0.5 to 3.3V 00043 // each 3995 represents 100000 Pa (1 bar) -> each unit represents 25 Pa 00044 // measurements show the 0.5V value at around 10300 00045 int pressure_pascal = (_input.read_u16() - 10300); 00046 pressure_pascal = (pressure_pascal * 25); 00047 00048 return pressure_pascal; 00049 } 00050 00051 void PressureSensor::_worker() 00052 { 00053 while(true) 00054 { 00055 if(_read_count != -1) 00056 { 00057 _read_count++; 00058 // keep calculating average 00059 _average_pressure = ( _average_pressure * (_read_count-1) 00060 + read_pa() ) 00061 /_read_count; 00062 } 00063 00064 ThisThread::sleep_for(200); 00065 } 00066 } 00067 00068 void PressureSensor::start_count() 00069 { 00070 _read_count = 0; 00071 _average_pressure = 0; 00072 } 00073 00074 float PressureSensor::stop_count() 00075 { 00076 _read_count = -1; 00077 return float(_average_pressure) / 100000; 00078 }
Generated on Thu Jul 14 2022 00:36:34 by
1.7.2