Color sensor reset at the end of calibration added. sensor id auto assignment was changed to be a fixed value assignment to avoid sensor id shift when some sensor is absent.

Dependencies:   UniGraphic mbed vt100

Committer:
Rhyme
Date:
Fri Feb 23 07:51:55 2018 +0000
Revision:
1:8818b793d147
Parent:
0:ce97f6d34336
Wrong behavior when one of color sensor is missing has been fixed.

Who changed what in which revision?

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