Orefatoi / Mbed 2 deprecated afero_poc15_171201

Dependencies:   mbed vt100

Committer:
Rhyme
Date:
Tue Dec 12 06:53:12 2017 +0000
Revision:
13:88ee926c56ae
Parent:
12:4c0bd7fce2fd
MCU Reset Reason Attribute added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:f0de320e23ac 1 #include "mbed.h"
Rhyme 0:f0de320e23ac 2 #include "edge_sensor.h"
Rhyme 0:f0de320e23ac 3 #include "edge_time.h"
Rhyme 0:f0de320e23ac 4 #include "PSE530.h"
Rhyme 0:f0de320e23ac 5 #include "edge_pressure.h"
Rhyme 0:f0de320e23ac 6 #include "SO1602A.h"
Rhyme 0:f0de320e23ac 7
Rhyme 3:cac964851bb6 8 extern SO1602A *display ; /* OLED display on I2C */
Rhyme 0:f0de320e23ac 9
Rhyme 0:f0de320e23ac 10 #define LOW_THR 0.2
Rhyme 0:f0de320e23ac 11 #define HIGH_THR 0.3
Rhyme 0:f0de320e23ac 12 #define MIN_TEMP 12.0
Rhyme 0:f0de320e23ac 13 #define MAX_TEMP 30.0
Rhyme 0:f0de320e23ac 14
Rhyme 0:f0de320e23ac 15 /**
Rhyme 0:f0de320e23ac 16 * SMC PSE530 pressure sensor
Rhyme 0:f0de320e23ac 17 * analog output 1.0V - 5.0V
Rhyme 0:f0de320e23ac 18 * 1.0V : 0
Rhyme 0:f0de320e23ac 19 * 5.0V : 1MPa
Rhyme 0:f0de320e23ac 20 * (at 0.6V : -0.1MPa)
Rhyme 10:88e5b8157167 21 * Our sensor I/F converts 0-5V to 0-1
Rhyme 0:f0de320e23ac 22 * So we suppose V = Analog Float Value : Pressure
Rhyme 10:88e5b8157167 23 * 0.2 = 0
Rhyme 10:88e5b8157167 24 * 1.0 = 1MPa
Rhyme 0:f0de320e23ac 25 */
Rhyme 0:f0de320e23ac 26
Rhyme 0:f0de320e23ac 27 float temp2expected(float temp)
Rhyme 0:f0de320e23ac 28 {
Rhyme 0:f0de320e23ac 29 const float coef_A = 0.089 ;
Rhyme 0:f0de320e23ac 30 const float coef_B = 0.831 ;
Rhyme 0:f0de320e23ac 31 float pressure ;
Rhyme 0:f0de320e23ac 32
Rhyme 0:f0de320e23ac 33 pressure = temp * coef_A + coef_B ;
Rhyme 0:f0de320e23ac 34 return( pressure ) ;
Rhyme 0:f0de320e23ac 35 }
Rhyme 0:f0de320e23ac 36
Rhyme 0:f0de320e23ac 37 edge_pressure::edge_pressure(PSE530 *pse)
Rhyme 0:f0de320e23ac 38 {
Rhyme 0:f0de320e23ac 39 _pse = pse ;
Rhyme 0:f0de320e23ac 40 _value = 0.0 ;
Rhyme 0:f0de320e23ac 41 _interval = 30 ;
Rhyme 12:4c0bd7fce2fd 42 _thr_mode = 0 ;
Rhyme 12:4c0bd7fce2fd 43 _thr_high = HIGH_THR ;
Rhyme 12:4c0bd7fce2fd 44 _thr_low = LOW_THR ;
Rhyme 0:f0de320e23ac 45 }
Rhyme 0:f0de320e23ac 46
Rhyme 0:f0de320e23ac 47 edge_pressure::~edge_pressure(void)
Rhyme 0:f0de320e23ac 48 {
Rhyme 0:f0de320e23ac 49 if (_pse) {
Rhyme 0:f0de320e23ac 50 delete _pse ;
Rhyme 0:f0de320e23ac 51 }
Rhyme 0:f0de320e23ac 52 _value = 0.0 ;
Rhyme 0:f0de320e23ac 53 }
Rhyme 0:f0de320e23ac 54
Rhyme 0:f0de320e23ac 55 float edge_pressure::get_value(void)
Rhyme 0:f0de320e23ac 56 {
Rhyme 0:f0de320e23ac 57 float value = 0.0 ;
Rhyme 0:f0de320e23ac 58 value = _pse->getPressure() ;
Rhyme 0:f0de320e23ac 59 return( value ) ;
Rhyme 0:f0de320e23ac 60 }
Rhyme 0:f0de320e23ac 61
Rhyme 0:f0de320e23ac 62 void edge_pressure::reset(void)
Rhyme 0:f0de320e23ac 63 {
Rhyme 0:f0de320e23ac 64 _value = 0.0 ;
Rhyme 0:f0de320e23ac 65 _sampled_time = 0 ;
Rhyme 0:f0de320e23ac 66 }
Rhyme 0:f0de320e23ac 67
Rhyme 0:f0de320e23ac 68 void edge_pressure::prepare(void)
Rhyme 0:f0de320e23ac 69 {
Rhyme 0:f0de320e23ac 70 }
Rhyme 0:f0de320e23ac 71
Rhyme 0:f0de320e23ac 72 void edge_pressure::sample(void)
Rhyme 0:f0de320e23ac 73 {
Rhyme 0:f0de320e23ac 74 _value = get_value() ;
Rhyme 0:f0de320e23ac 75 _sampled_time = edge_time ;
Rhyme 0:f0de320e23ac 76 }
Rhyme 0:f0de320e23ac 77
Rhyme 12:4c0bd7fce2fd 78 void edge_pressure::set_thr_high(int16_t thr_high)
Rhyme 12:4c0bd7fce2fd 79 {
Rhyme 12:4c0bd7fce2fd 80 switch(_thr_mode) {
Rhyme 12:4c0bd7fce2fd 81 case 0: /* absolute value */
Rhyme 12:4c0bd7fce2fd 82 _thr_high = (float)thr_high/100.0 ;
Rhyme 12:4c0bd7fce2fd 83 break ;
Rhyme 12:4c0bd7fce2fd 84 case 1: /* persentage */
Rhyme 12:4c0bd7fce2fd 85 _thr_high = (float)(thr_high/100.0) ;
Rhyme 12:4c0bd7fce2fd 86 break ;
Rhyme 12:4c0bd7fce2fd 87 default:
Rhyme 12:4c0bd7fce2fd 88 printf("Unknown Threshold mode %d\n", _thr_mode) ;
Rhyme 12:4c0bd7fce2fd 89 _thr_high = (float)thr_high/100.0 ;
Rhyme 12:4c0bd7fce2fd 90 break ;
Rhyme 12:4c0bd7fce2fd 91 }
Rhyme 12:4c0bd7fce2fd 92 printf("thr_high = %.3f\n", _thr_high) ;
Rhyme 12:4c0bd7fce2fd 93 }
Rhyme 12:4c0bd7fce2fd 94
Rhyme 12:4c0bd7fce2fd 95 float edge_pressure::get_thr_high(float expected)
Rhyme 12:4c0bd7fce2fd 96 {
Rhyme 12:4c0bd7fce2fd 97 float thr_high ;
Rhyme 12:4c0bd7fce2fd 98
Rhyme 12:4c0bd7fce2fd 99 switch(_thr_mode) {
Rhyme 12:4c0bd7fce2fd 100 case 0: /* absolute value */
Rhyme 12:4c0bd7fce2fd 101 thr_high = expected + _thr_high ;
Rhyme 12:4c0bd7fce2fd 102 break ;
Rhyme 12:4c0bd7fce2fd 103 case 1: /* persentage */
Rhyme 12:4c0bd7fce2fd 104 thr_high = expected * (1.0 + _thr_high) ;
Rhyme 12:4c0bd7fce2fd 105 break ;
Rhyme 12:4c0bd7fce2fd 106 default:
Rhyme 12:4c0bd7fce2fd 107 printf("Unknown Threshold mode %d\n", _thr_mode) ;
Rhyme 12:4c0bd7fce2fd 108 thr_high = expected + _thr_high ; /* use this as default */
Rhyme 12:4c0bd7fce2fd 109 break ;
Rhyme 12:4c0bd7fce2fd 110 }
Rhyme 12:4c0bd7fce2fd 111 return (thr_high) ;
Rhyme 12:4c0bd7fce2fd 112 }
Rhyme 12:4c0bd7fce2fd 113
Rhyme 12:4c0bd7fce2fd 114 void edge_pressure::set_thr_low(int16_t thr_low)
Rhyme 12:4c0bd7fce2fd 115 {
Rhyme 12:4c0bd7fce2fd 116 switch(_thr_mode) {
Rhyme 12:4c0bd7fce2fd 117 case 0: /* absolute value */
Rhyme 12:4c0bd7fce2fd 118 _thr_low = (float)thr_low/100.0 ;
Rhyme 12:4c0bd7fce2fd 119 break ;
Rhyme 12:4c0bd7fce2fd 120 case 1: /* persentage */
Rhyme 12:4c0bd7fce2fd 121 _thr_low = (float)(thr_low/100.0) ;
Rhyme 12:4c0bd7fce2fd 122 break ;
Rhyme 12:4c0bd7fce2fd 123 default:
Rhyme 12:4c0bd7fce2fd 124 printf("Unknown Threshold mode %d\n", _thr_mode) ;
Rhyme 12:4c0bd7fce2fd 125 _thr_low = (float)thr_low/100.0 ;
Rhyme 12:4c0bd7fce2fd 126 break ;
Rhyme 12:4c0bd7fce2fd 127 }
Rhyme 12:4c0bd7fce2fd 128 printf("thr_low = %.3f\n", _thr_low) ;
Rhyme 12:4c0bd7fce2fd 129 }
Rhyme 12:4c0bd7fce2fd 130
Rhyme 12:4c0bd7fce2fd 131 float edge_pressure::get_thr_low(float expected)
Rhyme 12:4c0bd7fce2fd 132 {
Rhyme 12:4c0bd7fce2fd 133 float thr_low ;
Rhyme 12:4c0bd7fce2fd 134
Rhyme 12:4c0bd7fce2fd 135 switch(_thr_mode) {
Rhyme 12:4c0bd7fce2fd 136 case 0: /* absolute value */
Rhyme 12:4c0bd7fce2fd 137 thr_low = expected - _thr_low ;
Rhyme 12:4c0bd7fce2fd 138 break ;
Rhyme 12:4c0bd7fce2fd 139 case 1: /* persentage */
Rhyme 12:4c0bd7fce2fd 140 thr_low = expected * (1.0 - _thr_low) ;
Rhyme 12:4c0bd7fce2fd 141 break ;
Rhyme 12:4c0bd7fce2fd 142 default:
Rhyme 12:4c0bd7fce2fd 143 printf("Unknown Threshold mode %d\n", _thr_mode) ;
Rhyme 12:4c0bd7fce2fd 144 thr_low = expected + _thr_low ; /* use this as default */
Rhyme 12:4c0bd7fce2fd 145 break ;
Rhyme 12:4c0bd7fce2fd 146 }
Rhyme 12:4c0bd7fce2fd 147 return (thr_low) ;
Rhyme 12:4c0bd7fce2fd 148 }
Rhyme 12:4c0bd7fce2fd 149
Rhyme 0:f0de320e23ac 150 int edge_pressure::deliver(void)
Rhyme 0:f0de320e23ac 151 {
Rhyme 0:f0de320e23ac 152 char str_buf[32] ;
Rhyme 8:5590f55bdf41 153 char timestr[16] ;
Rhyme 0:f0de320e23ac 154 int result ;
Rhyme 12:4c0bd7fce2fd 155 float expected, higher, lower ;
Rhyme 0:f0de320e23ac 156 print_time(_sampled_time) ;
Rhyme 0:f0de320e23ac 157 if (current_temp != 0) {
Rhyme 12:4c0bd7fce2fd 158 sprintf(str_buf, "GAS:%.3f@%.1fC", _value, *current_temp ) ;
Rhyme 0:f0de320e23ac 159 } else {
Rhyme 12:4c0bd7fce2fd 160 sprintf(str_buf, "GAS:%.3f ", _value ) ;
Rhyme 0:f0de320e23ac 161 }
Rhyme 3:cac964851bb6 162 printf(" ") ;
Rhyme 0:f0de320e23ac 163 printf(str_buf) ;
Rhyme 3:cac964851bb6 164 if (display != 0) {
Rhyme 9:f958fa2cdc74 165 display->cls() ;
Rhyme 3:cac964851bb6 166 display->locate(0, 0) ;
Rhyme 3:cac964851bb6 167 display->putStr(str_buf) ;
Rhyme 0:f0de320e23ac 168 }
Rhyme 0:f0de320e23ac 169 if (current_temp != 0) {
Rhyme 0:f0de320e23ac 170 expected = temp2expected(*current_temp) ;
Rhyme 12:4c0bd7fce2fd 171 higher = get_thr_high(expected) ;
Rhyme 12:4c0bd7fce2fd 172 lower = get_thr_low(expected) ;
Rhyme 12:4c0bd7fce2fd 173 printf(" (%.3f, %.3f) ", higher, lower) ;
Rhyme 12:4c0bd7fce2fd 174 if (_value > higher) {
Rhyme 0:f0de320e23ac 175 sprintf(str_buf, "High [exp %.2f]", expected) ;
Rhyme 12:4c0bd7fce2fd 176 } else if (_value < lower) {
Rhyme 0:f0de320e23ac 177 sprintf(str_buf, "Low [exp %.2f]", expected) ;
Rhyme 0:f0de320e23ac 178 } else {
Rhyme 0:f0de320e23ac 179 sprintf(str_buf, "Normal") ;
Rhyme 0:f0de320e23ac 180 }
Rhyme 0:f0de320e23ac 181 }
Rhyme 3:cac964851bb6 182 if (display != 0) {
Rhyme 3:cac964851bb6 183 display->locate(0, 1) ;
Rhyme 3:cac964851bb6 184 display->putStr(str_buf) ;
Rhyme 0:f0de320e23ac 185 }
Rhyme 3:cac964851bb6 186 printf(" %s\n", str_buf) ;
Rhyme 8:5590f55bdf41 187 time2seq(_sampled_time, timestr) ;
Rhyme 3:cac964851bb6 188 // printf(str_buf) ;
Rhyme 3:cac964851bb6 189 // printf("\n") ;
Rhyme 0:f0de320e23ac 190 sprintf(_str_buf,
Rhyme 8:5590f55bdf41 191 "{\"DEVICE\":\"PRESS\",\"PN\":\"PSE530\",\"VAL\":\"%.3f\",\"UNIT\":\"kgf/cm2\",\"S\":\"%s\",\"E\":\"%d\"}",
Rhyme 8:5590f55bdf41 192 _value, timestr, _error_count) ;
Rhyme 0:f0de320e23ac 193 result = afero->setAttribute(1, _str_buf) ;
Rhyme 0:f0de320e23ac 194 return( result == afSUCCESS ) ;
Rhyme 0:f0de320e23ac 195 }
Rhyme 0:f0de320e23ac 196
Rhyme 0:f0de320e23ac 197 void edge_pressure::recv_config(void)
Rhyme 0:f0de320e23ac 198 {
Rhyme 0:f0de320e23ac 199 }
Rhyme 0:f0de320e23ac 200
Rhyme 0:f0de320e23ac 201 void edge_pressure::send_config(void)
Rhyme 0:f0de320e23ac 202 {
Rhyme 0:f0de320e23ac 203 }