Gas Pressure Display Updated Power control for Pressure sensor added

Dependencies:   UniGraphic mbed vt100

Committer:
Rhyme
Date:
Fri Feb 16 08:27:50 2018 +0000
Revision:
0:37c8ecde13c2
control PSE530 power via PTC5 (pse530_en)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:37c8ecde13c2 1 #include "mbed.h"
Rhyme 0:37c8ecde13c2 2 #include "edge_sensor.h"
Rhyme 0:37c8ecde13c2 3 #include "edge_time.h"
Rhyme 0:37c8ecde13c2 4 #include "edge_reset_mgr.h"
Rhyme 0:37c8ecde13c2 5 #include "PSE530.h"
Rhyme 0:37c8ecde13c2 6 #include "edge_pressure.h"
Rhyme 0:37c8ecde13c2 7 #include <ILI9341.h>
Rhyme 0:37c8ecde13c2 8 #include "edge_chart.h"
Rhyme 0:37c8ecde13c2 9 // #include "SO1602A.h"
Rhyme 0:37c8ecde13c2 10
Rhyme 0:37c8ecde13c2 11 // extern SO1602A *display ; /* OLED display on I2C */
Rhyme 0:37c8ecde13c2 12 extern ILI9341 *display ;
Rhyme 0:37c8ecde13c2 13
Rhyme 0:37c8ecde13c2 14 #define LOW_THR 0.2
Rhyme 0:37c8ecde13c2 15 #define HIGH_THR 0.3
Rhyme 0:37c8ecde13c2 16 #define MIN_TEMP 12.0
Rhyme 0:37c8ecde13c2 17 #define MAX_TEMP 30.0
Rhyme 0:37c8ecde13c2 18
Rhyme 0:37c8ecde13c2 19 /**
Rhyme 0:37c8ecde13c2 20 * SMC PSE530 pressure sensor
Rhyme 0:37c8ecde13c2 21 * analog output 1.0V - 5.0V
Rhyme 0:37c8ecde13c2 22 * 1.0V : 0
Rhyme 0:37c8ecde13c2 23 * 5.0V : 1MPa
Rhyme 0:37c8ecde13c2 24 * (at 0.6V : -0.1MPa)
Rhyme 0:37c8ecde13c2 25 * Our sensor I/F converts 0-5V to 0-1
Rhyme 0:37c8ecde13c2 26 * So we suppose V = Analog Float Value : Pressure
Rhyme 0:37c8ecde13c2 27 * 0.2 = 0
Rhyme 0:37c8ecde13c2 28 * 1.0 = 1MPa
Rhyme 0:37c8ecde13c2 29 */
Rhyme 0:37c8ecde13c2 30
Rhyme 0:37c8ecde13c2 31 float temp2expected(float temp)
Rhyme 0:37c8ecde13c2 32 {
Rhyme 0:37c8ecde13c2 33 const float coef_A = 0.089 ;
Rhyme 0:37c8ecde13c2 34 const float coef_B = 0.831 ;
Rhyme 0:37c8ecde13c2 35 float pressure ;
Rhyme 0:37c8ecde13c2 36
Rhyme 0:37c8ecde13c2 37 pressure = temp * coef_A + coef_B ;
Rhyme 0:37c8ecde13c2 38 return( pressure ) ;
Rhyme 0:37c8ecde13c2 39 }
Rhyme 0:37c8ecde13c2 40
Rhyme 0:37c8ecde13c2 41 edge_pressure::edge_pressure(PSE530 *pse, DigitalOut *en)
Rhyme 0:37c8ecde13c2 42 {
Rhyme 0:37c8ecde13c2 43 _pse = pse ;
Rhyme 0:37c8ecde13c2 44 _en = en ;
Rhyme 0:37c8ecde13c2 45 _value = 0.0 ;
Rhyme 0:37c8ecde13c2 46 _interval = 30 ;
Rhyme 0:37c8ecde13c2 47 _thr_mode = 0 ;
Rhyme 0:37c8ecde13c2 48 _thr_high = HIGH_THR ;
Rhyme 0:37c8ecde13c2 49 _thr_low = LOW_THR ;
Rhyme 0:37c8ecde13c2 50 }
Rhyme 0:37c8ecde13c2 51
Rhyme 0:37c8ecde13c2 52 edge_pressure::~edge_pressure(void)
Rhyme 0:37c8ecde13c2 53 {
Rhyme 0:37c8ecde13c2 54 if (_pse) {
Rhyme 0:37c8ecde13c2 55 delete _pse ;
Rhyme 0:37c8ecde13c2 56 }
Rhyme 0:37c8ecde13c2 57 _value = 0.0 ;
Rhyme 0:37c8ecde13c2 58 }
Rhyme 0:37c8ecde13c2 59
Rhyme 0:37c8ecde13c2 60 float edge_pressure::get_value(void)
Rhyme 0:37c8ecde13c2 61 {
Rhyme 0:37c8ecde13c2 62 float value = 0.0 ;
Rhyme 0:37c8ecde13c2 63 value = _pse->getPressure() ;
Rhyme 0:37c8ecde13c2 64 return( value ) ;
Rhyme 0:37c8ecde13c2 65 }
Rhyme 0:37c8ecde13c2 66
Rhyme 0:37c8ecde13c2 67 void edge_pressure::reset(void)
Rhyme 0:37c8ecde13c2 68 {
Rhyme 0:37c8ecde13c2 69 _value = 0.0 ;
Rhyme 0:37c8ecde13c2 70 _sampled_time = 0 ;
Rhyme 0:37c8ecde13c2 71 }
Rhyme 0:37c8ecde13c2 72
Rhyme 0:37c8ecde13c2 73 void edge_pressure::prepare(void)
Rhyme 0:37c8ecde13c2 74 {
Rhyme 0:37c8ecde13c2 75 }
Rhyme 0:37c8ecde13c2 76
Rhyme 0:37c8ecde13c2 77 int edge_pressure::sample(void)
Rhyme 0:37c8ecde13c2 78 {
Rhyme 0:37c8ecde13c2 79 int result = 0 ;
Rhyme 0:37c8ecde13c2 80 *_en = 1 ; /* enable pressure sensor */
Rhyme 0:37c8ecde13c2 81 wait_ms(40) ;
Rhyme 0:37c8ecde13c2 82 _value = get_value() ;
Rhyme 0:37c8ecde13c2 83 *_en = 0 ; /* disable pressure sensor */
Rhyme 0:37c8ecde13c2 84 _sampled_time = edge_time ;
Rhyme 0:37c8ecde13c2 85 return( result ) ; /* this always success */
Rhyme 0:37c8ecde13c2 86 }
Rhyme 0:37c8ecde13c2 87
Rhyme 0:37c8ecde13c2 88 void edge_pressure::set_thr_high(int16_t thr_high)
Rhyme 0:37c8ecde13c2 89 {
Rhyme 0:37c8ecde13c2 90 switch(_thr_mode) {
Rhyme 0:37c8ecde13c2 91 case 0: /* absolute value */
Rhyme 0:37c8ecde13c2 92 _thr_high = (float)thr_high/100.0 ;
Rhyme 0:37c8ecde13c2 93 break ;
Rhyme 0:37c8ecde13c2 94 case 1: /* persentage */
Rhyme 0:37c8ecde13c2 95 _thr_high = (float)(thr_high/100.0) ;
Rhyme 0:37c8ecde13c2 96 break ;
Rhyme 0:37c8ecde13c2 97 default:
Rhyme 0:37c8ecde13c2 98 printf("Unknown Threshold mode %d\n", _thr_mode) ;
Rhyme 0:37c8ecde13c2 99 _thr_high = (float)thr_high/100.0 ;
Rhyme 0:37c8ecde13c2 100 break ;
Rhyme 0:37c8ecde13c2 101 }
Rhyme 0:37c8ecde13c2 102 // printf("thr_high = %.3f\n", _thr_high) ;
Rhyme 0:37c8ecde13c2 103 }
Rhyme 0:37c8ecde13c2 104
Rhyme 0:37c8ecde13c2 105 float edge_pressure::get_thr_high(float expected)
Rhyme 0:37c8ecde13c2 106 {
Rhyme 0:37c8ecde13c2 107 float thr_high ;
Rhyme 0:37c8ecde13c2 108
Rhyme 0:37c8ecde13c2 109 switch(_thr_mode) {
Rhyme 0:37c8ecde13c2 110 case 0: /* absolute value */
Rhyme 0:37c8ecde13c2 111 thr_high = expected + _thr_high ;
Rhyme 0:37c8ecde13c2 112 break ;
Rhyme 0:37c8ecde13c2 113 case 1: /* persentage */
Rhyme 0:37c8ecde13c2 114 thr_high = expected * (1.0 + _thr_high) ;
Rhyme 0:37c8ecde13c2 115 break ;
Rhyme 0:37c8ecde13c2 116 default:
Rhyme 0:37c8ecde13c2 117 printf("Unknown Threshold mode %d\n", _thr_mode) ;
Rhyme 0:37c8ecde13c2 118 thr_high = expected + _thr_high ; /* use this as default */
Rhyme 0:37c8ecde13c2 119 break ;
Rhyme 0:37c8ecde13c2 120 }
Rhyme 0:37c8ecde13c2 121 return (thr_high) ;
Rhyme 0:37c8ecde13c2 122 }
Rhyme 0:37c8ecde13c2 123
Rhyme 0:37c8ecde13c2 124 void edge_pressure::set_thr_low(int16_t thr_low)
Rhyme 0:37c8ecde13c2 125 {
Rhyme 0:37c8ecde13c2 126 switch(_thr_mode) {
Rhyme 0:37c8ecde13c2 127 case 0: /* absolute value */
Rhyme 0:37c8ecde13c2 128 _thr_low = (float)thr_low/100.0 ;
Rhyme 0:37c8ecde13c2 129 break ;
Rhyme 0:37c8ecde13c2 130 case 1: /* persentage */
Rhyme 0:37c8ecde13c2 131 _thr_low = (float)(thr_low/100.0) ;
Rhyme 0:37c8ecde13c2 132 break ;
Rhyme 0:37c8ecde13c2 133 default:
Rhyme 0:37c8ecde13c2 134 printf("Unknown Threshold mode %d\n", _thr_mode) ;
Rhyme 0:37c8ecde13c2 135 _thr_low = (float)thr_low/100.0 ;
Rhyme 0:37c8ecde13c2 136 break ;
Rhyme 0:37c8ecde13c2 137 }
Rhyme 0:37c8ecde13c2 138 //printf("thr_low = %.3f\n", _thr_low) ;
Rhyme 0:37c8ecde13c2 139 }
Rhyme 0:37c8ecde13c2 140
Rhyme 0:37c8ecde13c2 141 float edge_pressure::get_thr_low(float expected)
Rhyme 0:37c8ecde13c2 142 {
Rhyme 0:37c8ecde13c2 143 float thr_low ;
Rhyme 0:37c8ecde13c2 144
Rhyme 0:37c8ecde13c2 145 switch(_thr_mode) {
Rhyme 0:37c8ecde13c2 146 case 0: /* absolute value */
Rhyme 0:37c8ecde13c2 147 thr_low = expected - _thr_low ;
Rhyme 0:37c8ecde13c2 148 break ;
Rhyme 0:37c8ecde13c2 149 case 1: /* persentage */
Rhyme 0:37c8ecde13c2 150 thr_low = expected * (1.0 - _thr_low) ;
Rhyme 0:37c8ecde13c2 151 break ;
Rhyme 0:37c8ecde13c2 152 default:
Rhyme 0:37c8ecde13c2 153 printf("Unknown Threshold mode %d\n", _thr_mode) ;
Rhyme 0:37c8ecde13c2 154 thr_low = expected + _thr_low ; /* use this as default */
Rhyme 0:37c8ecde13c2 155 break ;
Rhyme 0:37c8ecde13c2 156 }
Rhyme 0:37c8ecde13c2 157 return (thr_low) ;
Rhyme 0:37c8ecde13c2 158 }
Rhyme 0:37c8ecde13c2 159
Rhyme 0:37c8ecde13c2 160 int edge_pressure::deliver(void)
Rhyme 0:37c8ecde13c2 161 {
Rhyme 0:37c8ecde13c2 162 char str_buf[32] ;
Rhyme 0:37c8ecde13c2 163 char timestr[16] ;
Rhyme 0:37c8ecde13c2 164 int result ;
Rhyme 0:37c8ecde13c2 165
Rhyme 0:37c8ecde13c2 166 reset_watch_dog() ;
Rhyme 0:37c8ecde13c2 167 print_time(_sampled_time) ;
Rhyme 0:37c8ecde13c2 168 if (current_temp != 0) {
Rhyme 0:37c8ecde13c2 169 sprintf(str_buf, "GAS: %.3f kgf/cm2 @ %.1fC", _value, *current_temp ) ;
Rhyme 0:37c8ecde13c2 170 } else {
Rhyme 0:37c8ecde13c2 171 sprintf(str_buf, "GAS: %.3f kgf/cm2", _value ) ;
Rhyme 0:37c8ecde13c2 172 }
Rhyme 0:37c8ecde13c2 173 printf(" ") ;
Rhyme 0:37c8ecde13c2 174 printf(str_buf) ;
Rhyme 0:37c8ecde13c2 175
Rhyme 0:37c8ecde13c2 176 if (current_temp != 0) {
Rhyme 0:37c8ecde13c2 177 reset_watch_dog() ;
Rhyme 0:37c8ecde13c2 178 _expected = temp2expected(*current_temp) ;
Rhyme 0:37c8ecde13c2 179 _higher = get_thr_high(_expected) ;
Rhyme 0:37c8ecde13c2 180 _lower = get_thr_low(_expected) ;
Rhyme 0:37c8ecde13c2 181 printf(" (%.3f, %.3f) ", _higher, _lower) ;
Rhyme 0:37c8ecde13c2 182 }
Rhyme 0:37c8ecde13c2 183
Rhyme 0:37c8ecde13c2 184 reset_watch_dog() ;
Rhyme 0:37c8ecde13c2 185 printf(" %s\n", str_buf) ;
Rhyme 0:37c8ecde13c2 186 time2seq(_sampled_time, timestr) ;
Rhyme 0:37c8ecde13c2 187 // printf(str_buf) ;
Rhyme 0:37c8ecde13c2 188 // printf("\n") ;
Rhyme 0:37c8ecde13c2 189 sprintf(_str_buf,
Rhyme 0:37c8ecde13c2 190 "{\"DEVICE\":\"PRESS\",\"PN\":\"PSE530\",\"VAL\":\"%.3f\",\"UNIT\":\"kgf/cm2\",\"T\":\"%s\",\"E\":\"%d\"}",
Rhyme 0:37c8ecde13c2 191 _value, timestr, _error_count) ;
Rhyme 0:37c8ecde13c2 192 reset_watch_dog() ;
Rhyme 0:37c8ecde13c2 193 result = afero->setAttribute(1, _str_buf) ;
Rhyme 0:37c8ecde13c2 194 return( result == afSUCCESS ) ;
Rhyme 0:37c8ecde13c2 195 }
Rhyme 0:37c8ecde13c2 196
Rhyme 0:37c8ecde13c2 197 int v2x(float value)
Rhyme 0:37c8ecde13c2 198 {
Rhyme 0:37c8ecde13c2 199 int result ;
Rhyme 0:37c8ecde13c2 200 if (value < 0) {
Rhyme 0:37c8ecde13c2 201 result = 20 ;
Rhyme 0:37c8ecde13c2 202 } else if (value > 4) {
Rhyme 0:37c8ecde13c2 203 result = 300 ;
Rhyme 0:37c8ecde13c2 204 } else {
Rhyme 0:37c8ecde13c2 205 result = 20 + (int)(70 * value + 0.5) ;
Rhyme 0:37c8ecde13c2 206 }
Rhyme 0:37c8ecde13c2 207 return( result ) ;
Rhyme 0:37c8ecde13c2 208 }
Rhyme 0:37c8ecde13c2 209
Rhyme 0:37c8ecde13c2 210 int press_v2y(float value, edge_chart_type *p)
Rhyme 0:37c8ecde13c2 211 {
Rhyme 0:37c8ecde13c2 212 int y ;
Rhyme 0:37c8ecde13c2 213 if (value < p->min) {
Rhyme 0:37c8ecde13c2 214 value = p->min ;
Rhyme 0:37c8ecde13c2 215 } else if (value > p->max) {
Rhyme 0:37c8ecde13c2 216 value = p->max ;
Rhyme 0:37c8ecde13c2 217 }
Rhyme 0:37c8ecde13c2 218 y = p->top + p->height - 2
Rhyme 0:37c8ecde13c2 219 - (int)((p->height - 2) * value /(p->max - p->min)) ;
Rhyme 0:37c8ecde13c2 220 return( y ) ;
Rhyme 0:37c8ecde13c2 221 }
Rhyme 0:37c8ecde13c2 222
Rhyme 0:37c8ecde13c2 223 void edge_pressure::show(void)
Rhyme 0:37c8ecde13c2 224 {
Rhyme 0:37c8ecde13c2 225 edge_chart_type *p = &edge_chart[ _id ] ;
Rhyme 0:37c8ecde13c2 226 uint16_t color = White ;
Rhyme 0:37c8ecde13c2 227 char str_buf[32] ;
Rhyme 0:37c8ecde13c2 228 int i, x, y, l, r, c ;
Rhyme 0:37c8ecde13c2 229 if (display) {
Rhyme 0:37c8ecde13c2 230 reset_watch_dog() ;
Rhyme 0:37c8ecde13c2 231 /* for debug */
Rhyme 0:37c8ecde13c2 232 // _value = _lower - 0.5 ; /* LOW */
Rhyme 0:37c8ecde13c2 233 // _value = (_higher + _lower) / 2 ; /* GOOD */
Rhyme 0:37c8ecde13c2 234 // _value = _higher + 0.2 ; /* HIGH */
Rhyme 0:37c8ecde13c2 235
Rhyme 0:37c8ecde13c2 236 if (_value > _higher) {
Rhyme 0:37c8ecde13c2 237 sprintf(str_buf, "HIGH") ;
Rhyme 0:37c8ecde13c2 238 color = Red ;
Rhyme 0:37c8ecde13c2 239 } else if (_value < _lower) {
Rhyme 0:37c8ecde13c2 240 sprintf(str_buf, "LOW") ;
Rhyme 0:37c8ecde13c2 241 color = Yellow ;
Rhyme 0:37c8ecde13c2 242 } else {
Rhyme 0:37c8ecde13c2 243 sprintf(str_buf, "GOOD") ;
Rhyme 0:37c8ecde13c2 244 color = Green ;
Rhyme 0:37c8ecde13c2 245 }
Rhyme 0:37c8ecde13c2 246 switch(display_mode) {
Rhyme 0:37c8ecde13c2 247 case DISPLAY_MODE_GAS:
Rhyme 0:37c8ecde13c2 248 display->BusEnable(true) ;
Rhyme 0:37c8ecde13c2 249 display->cls() ;
Rhyme 0:37c8ecde13c2 250 /* printf frame */
Rhyme 0:37c8ecde13c2 251 display->foreground(White) ;
Rhyme 0:37c8ecde13c2 252 display->line(20, 75, 300, 75, White) ;
Rhyme 0:37c8ecde13c2 253 for (i = 0 ; i <= 8 ; i++ ) {
Rhyme 0:37c8ecde13c2 254 x = 20 + i * 35 ;
Rhyme 0:37c8ecde13c2 255 if (i & 0x01) { /* odd */
Rhyme 0:37c8ecde13c2 256 display->line(x, 55, x, 95, White) ;
Rhyme 0:37c8ecde13c2 257 } else { /* even */
Rhyme 0:37c8ecde13c2 258 display->line(x, 45, x, 105, White) ;
Rhyme 0:37c8ecde13c2 259 }
Rhyme 0:37c8ecde13c2 260 }
Rhyme 0:37c8ecde13c2 261 display->set_font((unsigned char*) Arial28x28);
Rhyme 0:37c8ecde13c2 262 for (i = 0 ; i <= 4 ; i++ ) {
Rhyme 0:37c8ecde13c2 263 x = 12 + i * 70 ;
Rhyme 0:37c8ecde13c2 264 display->locate(x, 10) ;
Rhyme 0:37c8ecde13c2 265 display->printf("%d", i) ;
Rhyme 0:37c8ecde13c2 266 }
Rhyme 0:37c8ecde13c2 267 /* print expected area and current pressure */
Rhyme 0:37c8ecde13c2 268 l = v2x(_lower) ;
Rhyme 0:37c8ecde13c2 269 r = v2x(_higher) ;
Rhyme 0:37c8ecde13c2 270 c = v2x(_value) ;
Rhyme 0:37c8ecde13c2 271 display->fillrect(l, 70, r, 80, Red) ;
Rhyme 0:37c8ecde13c2 272 display->fillcircle(c, 75, 10, White) ;
Rhyme 0:37c8ecde13c2 273
Rhyme 0:37c8ecde13c2 274 /* print status */
Rhyme 0:37c8ecde13c2 275 display->locate(30, 140) ;
Rhyme 0:37c8ecde13c2 276 display->set_font_zoom(3, 3) ;
Rhyme 0:37c8ecde13c2 277 display->foreground(color) ;
Rhyme 0:37c8ecde13c2 278 display->printf(str_buf) ;
Rhyme 0:37c8ecde13c2 279 display->set_font_zoom(1, 1) ;
Rhyme 0:37c8ecde13c2 280 display->BusEnable(false) ;
Rhyme 0:37c8ecde13c2 281 break ;
Rhyme 0:37c8ecde13c2 282 case DISPLAY_MODE_SUMMARY:
Rhyme 0:37c8ecde13c2 283 display->BusEnable(true) ;
Rhyme 0:37c8ecde13c2 284 display->set_font((unsigned char*) Arial12x12);
Rhyme 0:37c8ecde13c2 285 display->set_font_zoom(2, 2) ;
Rhyme 0:37c8ecde13c2 286 display->foreground(White) ;
Rhyme 0:37c8ecde13c2 287 display->locate(10, EDGE_SUMMARY_TIME_Y) ;
Rhyme 0:37c8ecde13c2 288 displayTime(_sampled_time) ;
Rhyme 0:37c8ecde13c2 289 // display->locate(10,50) ;
Rhyme 0:37c8ecde13c2 290 // display->printf(timestr) ;
Rhyme 0:37c8ecde13c2 291 display->locate(10, EDGE_SUMMARY_PRESS_Y) ;
Rhyme 0:37c8ecde13c2 292 display->printf("Press: ") ;
Rhyme 0:37c8ecde13c2 293 display->foreground(color) ;
Rhyme 0:37c8ecde13c2 294 display->locate(90, EDGE_SUMMARY_PRESS_Y) ;
Rhyme 0:37c8ecde13c2 295 display->printf("%.3f ", _value) ;
Rhyme 0:37c8ecde13c2 296 display->foreground(White) ;
Rhyme 0:37c8ecde13c2 297 display->printf("kgf/cm2") ;
Rhyme 0:37c8ecde13c2 298 display->BusEnable(false) ;
Rhyme 0:37c8ecde13c2 299 break ;
Rhyme 0:37c8ecde13c2 300 case DISPLAY_MODE_CHART:
Rhyme 0:37c8ecde13c2 301 x = p->left + p->index + 1;
Rhyme 0:37c8ecde13c2 302 y = press_v2y(_value, p) ;
Rhyme 0:37c8ecde13c2 303 display->BusEnable(true) ;
Rhyme 0:37c8ecde13c2 304 if (p->index == 0) {
Rhyme 0:37c8ecde13c2 305 draw_chart_frame(p) ;
Rhyme 0:37c8ecde13c2 306 }
Rhyme 0:37c8ecde13c2 307 display->foreground(color) ;
Rhyme 0:37c8ecde13c2 308 display->pixel(x, y, color) ;
Rhyme 0:37c8ecde13c2 309 display->set_font((unsigned char*) Arial12x12);
Rhyme 0:37c8ecde13c2 310 display->locate(p->left + 40, p->top + 5) ;
Rhyme 0:37c8ecde13c2 311 display->printf("%5s", str_buf) ;
Rhyme 0:37c8ecde13c2 312 display->foreground(White) ;
Rhyme 0:37c8ecde13c2 313 display->BusEnable(false) ;
Rhyme 0:37c8ecde13c2 314 p->index = (p->index + 1) % (p->width - 2) ;
Rhyme 0:37c8ecde13c2 315 break ;
Rhyme 0:37c8ecde13c2 316 }
Rhyme 0:37c8ecde13c2 317 }
Rhyme 0:37c8ecde13c2 318 reset_watch_dog() ;
Rhyme 0:37c8ecde13c2 319 }