La Suno / Mbed 2 deprecated afero_poc15_180223

Dependencies:   UniGraphic mbed vt100

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers edge_pressure.cpp Source File

edge_pressure.cpp

00001 #include "mbed.h"
00002 #include "edge_sensor.h"
00003 #include "edge_time.h"
00004 #include "edge_reset_mgr.h"
00005 #include "PSE530.h"
00006 #include "edge_pressure.h"
00007 #include <ILI9341.h>
00008 #include "edge_chart.h"
00009 // #include "SO1602A.h"
00010 
00011 // extern SO1602A *display ; /* OLED display on I2C */
00012 extern ILI9341 *display ;
00013 
00014 #define LOW_THR   0.2
00015 #define HIGH_THR  0.3 
00016 #define MIN_TEMP 12.0
00017 #define MAX_TEMP 30.0
00018 
00019 /**
00020  * SMC PSE530 pressure sensor
00021  * analog output 1.0V - 5.0V
00022  * 1.0V : 0
00023  * 5.0V : 1MPa
00024  * (at 0.6V : -0.1MPa)
00025  * Our sensor I/F converts 0-5V to 0-1
00026  * So we suppose V = Analog Float Value : Pressure
00027  * 0.2 =  0
00028  * 1.0 = 1MPa
00029  */
00030  
00031 float temp2expected(float temp)
00032 {
00033     const float coef_A = 0.089 ;
00034     const float coef_B = 0.831 ;
00035     float pressure ;
00036 
00037     pressure = temp * coef_A + coef_B ;
00038     return( pressure ) ;
00039 }
00040 
00041 edge_pressure::edge_pressure(PSE530 *pse, DigitalOut *en)
00042 {
00043     _pse = pse ;
00044     _en = en ;
00045     _value = 0.0 ;
00046     _interval = 30 ;
00047     _thr_mode = 0 ;
00048     _thr_high = HIGH_THR ;
00049     _thr_low = LOW_THR ;
00050 }
00051 
00052 edge_pressure::~edge_pressure(void)
00053 {
00054     if (_pse) {
00055         delete _pse ;
00056     }
00057     _value = 0.0 ;
00058 }
00059 
00060 float edge_pressure::get_value(void)
00061 {
00062     float value = 0.0 ;
00063     value = _pse->getPressure() ;
00064     return( value ) ;
00065 }
00066 
00067 void edge_pressure::reset(void) 
00068 {
00069     _value = 0.0 ;
00070     _sampled_time = 0 ;
00071 }
00072 
00073 void edge_pressure::prepare(void) 
00074 {
00075 }
00076 
00077 int edge_pressure::sample(void) 
00078 {
00079     int result = 0 ;
00080     *_en = 1 ; /* enable pressure sensor */
00081     wait_ms(30) ;
00082     _value = get_value() ;
00083     _sampled_time = edge_time ;
00084     *_en = 0 ; /* disable pressure sensor */
00085     wait_ms(10) ; /* to avoid power transition effect remaining */
00086     return( result ) ; /* this always success */
00087 }
00088 
00089 void edge_pressure::set_thr_high(int16_t thr_high)
00090 {
00091     switch(_thr_mode) {
00092     case 0: /* absolute value */
00093         _thr_high = (float)thr_high/100.0 ;
00094         break ;
00095     case 1: /* persentage */
00096         _thr_high = (float)(thr_high/100.0) ;
00097         break ;
00098     default:
00099         printf("Unknown Threshold mode %d\n", _thr_mode) ;
00100         _thr_high = (float)thr_high/100.0 ;
00101         break ;
00102     }
00103 // printf("thr_high = %.3f\n", _thr_high) ;
00104 }
00105 
00106 float edge_pressure::get_thr_high(float expected)
00107 {
00108     float thr_high ;
00109     
00110     switch(_thr_mode) {
00111     case 0: /* absolute value */
00112         thr_high = expected + _thr_high ;
00113         break ;
00114     case 1: /* persentage */
00115         thr_high = expected * (1.0 + _thr_high) ;
00116         break ;
00117     default:
00118         printf("Unknown Threshold mode %d\n", _thr_mode) ;
00119         thr_high = expected + _thr_high ; /* use this as default */
00120         break ;
00121     }
00122     return (thr_high) ;
00123 }
00124 
00125 void edge_pressure::set_thr_low(int16_t thr_low)
00126 {   
00127     switch(_thr_mode) {
00128     case 0: /* absolute value */
00129         _thr_low = (float)thr_low/100.0 ;
00130         break ;
00131     case 1: /* persentage */
00132         _thr_low = (float)(thr_low/100.0) ;
00133         break ;
00134     default:
00135         printf("Unknown Threshold mode %d\n", _thr_mode) ;
00136         _thr_low = (float)thr_low/100.0 ;
00137         break ;
00138     }
00139 //printf("thr_low = %.3f\n", _thr_low) ;
00140 }
00141 
00142 float edge_pressure::get_thr_low(float expected)
00143 {
00144     float thr_low ;
00145     
00146     switch(_thr_mode) {
00147     case 0: /* absolute value */
00148         thr_low = expected - _thr_low ;
00149         break ;
00150     case 1: /* persentage */
00151         thr_low = expected * (1.0 - _thr_low) ;
00152         break ;
00153     default:
00154         printf("Unknown Threshold mode %d\n", _thr_mode) ;
00155         thr_low = expected + _thr_low ; /* use this as default */
00156         break ;
00157     }
00158     return (thr_low) ;
00159 }
00160 
00161 int edge_pressure::deliver(void) 
00162 {
00163     char str_buf[32] ;
00164     char timestr[16] ;
00165     int result ;
00166 
00167 reset_watch_dog() ;
00168     print_time(_sampled_time) ;
00169     if (current_temp != 0) {
00170         sprintf(str_buf, "GAS: %.3f kgf/cm2 @ %.1fC", _value, *current_temp ) ;
00171     } else {
00172         sprintf(str_buf, "GAS: %.3f kgf/cm2", _value  ) ;
00173     }
00174     printf(" ") ;
00175     printf(str_buf) ;
00176 
00177     if (current_temp != 0) {
00178 reset_watch_dog() ;
00179         _expected = temp2expected(*current_temp) ;
00180         _higher = get_thr_high(_expected) ;
00181         _lower = get_thr_low(_expected) ;
00182         printf(" (%.3f, %.3f) ", _higher, _lower) ;
00183     }
00184 
00185 reset_watch_dog() ;
00186     printf(" %s\n", str_buf) ;
00187     time2seq(_sampled_time, timestr) ;
00188 //    printf(str_buf) ;
00189 //    printf("\n") ;
00190     sprintf(_str_buf,
00191        "{\"DEVICE\":\"PRESS\",\"PN\":\"PSE530\",\"VAL\":\"%.3f\",\"UNIT\":\"kgf/cm2\",\"T\":\"%s\",\"E\":\"%d\"}",
00192        _value, timestr, _error_count) ;
00193 reset_watch_dog() ;
00194     result = afero->setAttribute(1, _str_buf) ;
00195     return( result == afSUCCESS ) ;
00196 }
00197 
00198 int v2x(float value)
00199 {
00200     int result ;
00201     if (value < 0) {
00202         result = 20 ;
00203     } else if (value > 4) {
00204         result = 300 ;
00205     } else {
00206         result = 20 + (int)(70 * value + 0.5) ;
00207     }
00208     return( result ) ;
00209 }
00210 
00211 int press_v2y(float value, edge_chart_type *p)
00212 {
00213     int y ;
00214     if (value < p->min) {
00215         value = p->min ;
00216     } else if (value > p->max) {
00217         value = p->max ;
00218     }
00219     y = p->top + p->height - 2
00220         - (int)((p->height - 2) * ((value - p->min) /(p->max - p->min))) ;
00221     return( y ) ;
00222 }
00223 
00224 void edge_pressure::show(void)
00225 {
00226     edge_chart_type *p = &edge_chart[ _id ] ;
00227     uint16_t color = White ;
00228     char str_buf[32] ;
00229     int i, x, y, l, r, c ;
00230     if (display) {
00231         reset_watch_dog() ;
00232 /* for debug */
00233 //  _value = _lower - 0.5 ; /* LOW */
00234 // _value = (_higher + _lower) / 2 ; /* GOOD */
00235 // _value = _higher + 0.2 ; /* HIGH */
00236  
00237         if (_value > _higher) {
00238             sprintf(str_buf, "HIGH") ;
00239             color = Red ;
00240         } else if (_value < _lower) {
00241             sprintf(str_buf, "LOW") ;
00242             color = Yellow ;
00243         } else {
00244             sprintf(str_buf, "GOOD") ;
00245             color = Green ;
00246         }
00247         switch(display_mode) {
00248         case DISPLAY_MODE_GAS:
00249             display->BusEnable(true) ;
00250             display->cls() ;
00251             /* printf frame */
00252             display->foreground(White) ;
00253             display->line(20, 75, 300, 75, White) ;
00254             for (i = 0 ; i <= 8 ; i++ ) {
00255                 x = 20 + i * 35 ;
00256                 if (i & 0x01) { /* odd */
00257                     display->line(x, 55, x, 95, White) ;
00258                 } else { /* even */
00259                     display->line(x, 45, x, 105, White) ;
00260                 }
00261             }
00262             display->set_font((unsigned char*) Arial28x28);
00263             for (i = 0 ; i <= 4 ; i++ ) {
00264                 x = 12 + i * 70 ;
00265                 display->locate(x, 10) ;
00266                 display->printf("%d", i) ;
00267             }
00268             /* print expected area and current pressure */
00269             l = v2x(_lower) ;
00270             r = v2x(_higher) ;
00271             c = v2x(_value) ;
00272             display->fillrect(l, 70, r, 80, Red) ;
00273             display->fillcircle(c, 75, 10, White) ;
00274                     
00275             /* print status */
00276             display->locate(30, 140) ;
00277             display->set_font_zoom(3, 3) ;
00278             display->foreground(color) ;
00279             display->printf(str_buf) ;
00280             display->set_font_zoom(1, 1) ;
00281             display->BusEnable(false) ;
00282             break ;
00283         case DISPLAY_MODE_SUMMARY:
00284             display->BusEnable(true) ;
00285             display->set_font((unsigned char*) Arial12x12);
00286             display->set_font_zoom(2, 2) ;
00287             display->foreground(White) ;
00288             display->locate(10, EDGE_SUMMARY_TIME_Y) ;
00289             displayTime(_sampled_time) ;
00290 //          display->locate(10,50) ;
00291 //          display->printf(timestr) ;
00292             display->locate(10, EDGE_SUMMARY_PRESS_Y) ;
00293             display->printf("Press: ") ;
00294             display->foreground(color) ;
00295             display->locate(90, EDGE_SUMMARY_PRESS_Y) ;
00296             display->printf("%.3f ", _value) ;
00297             display->foreground(White) ;
00298             display->printf("kgf/cm2") ;
00299             display->BusEnable(false) ;
00300             break ;
00301         case DISPLAY_MODE_CHART:
00302             x = p->left + p->index + 1;
00303             y = press_v2y(_value, p) ;
00304             display->BusEnable(true) ;
00305             if (p->index == 0) {
00306                 draw_chart_frame(p) ;
00307             }
00308             display->foreground(color) ;
00309             display->pixel(x, y, color) ;
00310             display->set_font((unsigned char*) Arial12x12);
00311             display->locate(p->left + 40, p->top + 5) ;
00312             display->printf("%5s", str_buf) ;
00313             display->foreground(White) ;
00314             display->BusEnable(false) ;
00315             p->index = (p->index + 1) % (p->width - 2) ;
00316             break ;
00317         }
00318     }
00319     reset_watch_dog() ;
00320 }