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
edge_sensor/edge_accel.cpp@1:8818b793d147, 2018-02-23 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:ce97f6d34336 | 1 | #include "mbed.h" |
Rhyme | 0:ce97f6d34336 | 2 | #include "afLib.h" |
Rhyme | 0:ce97f6d34336 | 3 | #include "edge_reset_mgr.h" |
Rhyme | 0:ce97f6d34336 | 4 | #include "edge_sensor.h" |
Rhyme | 0:ce97f6d34336 | 5 | #include "edge_accel.h" |
Rhyme | 0:ce97f6d34336 | 6 | #include "MMA8451Q.h" |
Rhyme | 0:ce97f6d34336 | 7 | |
Rhyme | 0:ce97f6d34336 | 8 | edge_accel::edge_accel(MMA8451Q *accel) : edge_sensor() |
Rhyme | 0:ce97f6d34336 | 9 | { |
Rhyme | 0:ce97f6d34336 | 10 | _accel = accel ; |
Rhyme | 0:ce97f6d34336 | 11 | _sample_count = 0 ; |
Rhyme | 0:ce97f6d34336 | 12 | _accumulation = 0 ; |
Rhyme | 0:ce97f6d34336 | 13 | _prev_x = 0 ; |
Rhyme | 0:ce97f6d34336 | 14 | _prev_y = 0 ; |
Rhyme | 0:ce97f6d34336 | 15 | _prev_z = 0 ; |
Rhyme | 0:ce97f6d34336 | 16 | |
Rhyme | 0:ce97f6d34336 | 17 | _interval = 30 ; |
Rhyme | 0:ce97f6d34336 | 18 | } |
Rhyme | 0:ce97f6d34336 | 19 | |
Rhyme | 0:ce97f6d34336 | 20 | edge_accel::~edge_accel(void) |
Rhyme | 0:ce97f6d34336 | 21 | { |
Rhyme | 0:ce97f6d34336 | 22 | delete _accel ; |
Rhyme | 0:ce97f6d34336 | 23 | } |
Rhyme | 0:ce97f6d34336 | 24 | |
Rhyme | 0:ce97f6d34336 | 25 | void edge_accel::reset(void) |
Rhyme | 0:ce97f6d34336 | 26 | { |
Rhyme | 0:ce97f6d34336 | 27 | clear_value() ; |
Rhyme | 0:ce97f6d34336 | 28 | edge_sensor::reset() ; |
Rhyme | 0:ce97f6d34336 | 29 | } |
Rhyme | 0:ce97f6d34336 | 30 | |
Rhyme | 0:ce97f6d34336 | 31 | #if 0 |
Rhyme | 0:ce97f6d34336 | 32 | void edge_accel::prepare(void) |
Rhyme | 0:ce97f6d34336 | 33 | { |
Rhyme | 0:ce97f6d34336 | 34 | // printf("accel prepare\n") ; |
Rhyme | 0:ce97f6d34336 | 35 | } |
Rhyme | 0:ce97f6d34336 | 36 | #endif |
Rhyme | 0:ce97f6d34336 | 37 | |
Rhyme | 0:ce97f6d34336 | 38 | int edge_accel::sample(void) |
Rhyme | 0:ce97f6d34336 | 39 | { |
Rhyme | 0:ce97f6d34336 | 40 | int result ; |
Rhyme | 0:ce97f6d34336 | 41 | float theValue = 0.0 ; |
Rhyme | 0:ce97f6d34336 | 42 | if (_sample_count > 1) { /* if sample is 1 or less, no data */ |
Rhyme | 0:ce97f6d34336 | 43 | _num_sampled = _sample_count - 1 ; |
Rhyme | 0:ce97f6d34336 | 44 | theValue = (float)_accumulation / (float)(_num_sampled) ; |
Rhyme | 0:ce97f6d34336 | 45 | result = 0 ; /* success */ |
Rhyme | 0:ce97f6d34336 | 46 | } else { |
Rhyme | 0:ce97f6d34336 | 47 | result = 1 ; /* fail! */ |
Rhyme | 0:ce97f6d34336 | 48 | } |
Rhyme | 0:ce97f6d34336 | 49 | _value = theValue / 4096.0 ; |
Rhyme | 0:ce97f6d34336 | 50 | _sampled_time = edge_time ; |
Rhyme | 0:ce97f6d34336 | 51 | _sample_count = 0 ; |
Rhyme | 0:ce97f6d34336 | 52 | _accumulation = 0 ; |
Rhyme | 0:ce97f6d34336 | 53 | return( result ) ; |
Rhyme | 0:ce97f6d34336 | 54 | } |
Rhyme | 0:ce97f6d34336 | 55 | |
Rhyme | 0:ce97f6d34336 | 56 | int edge_accel::deliver(void) |
Rhyme | 0:ce97f6d34336 | 57 | { |
Rhyme | 0:ce97f6d34336 | 58 | int result ; |
Rhyme | 0:ce97f6d34336 | 59 | char timestr[16] ; |
Rhyme | 0:ce97f6d34336 | 60 | |
Rhyme | 0:ce97f6d34336 | 61 | print_time(_sampled_time) ; |
Rhyme | 0:ce97f6d34336 | 62 | // _value = get_value() ; |
Rhyme | 0:ce97f6d34336 | 63 | printf(" accel: %.3f [%d samples]\n", _value, _num_sampled) ; |
Rhyme | 0:ce97f6d34336 | 64 | time2seq(_sampled_time, timestr) ; |
Rhyme | 0:ce97f6d34336 | 65 | sprintf(_str_buf, |
Rhyme | 0:ce97f6d34336 | 66 | "{\"DEVICE\":\"ACCEL\",\"PN\":\"MMA8451Q\",\"VAL_X\":\"%.3f\",\"VAL_Y\":\"0\",\"VAL_Z\":\"0\",\"UNIT\":\"g\",\"T\":\"%s\",\"E\":\"%d\"}", |
Rhyme | 0:ce97f6d34336 | 67 | _value, timestr, _error_count) ; |
Rhyme | 0:ce97f6d34336 | 68 | result = afero->setAttribute(1, _str_buf) ; |
Rhyme | 0:ce97f6d34336 | 69 | |
Rhyme | 0:ce97f6d34336 | 70 | return( result == afSUCCESS ) ; |
Rhyme | 0:ce97f6d34336 | 71 | } |
Rhyme | 0:ce97f6d34336 | 72 | |
Rhyme | 0:ce97f6d34336 | 73 | int accel_v2y(float value, edge_chart_type *p) |
Rhyme | 0:ce97f6d34336 | 74 | { |
Rhyme | 0:ce97f6d34336 | 75 | int y ; |
Rhyme | 0:ce97f6d34336 | 76 | if (value < p->min) { |
Rhyme | 0:ce97f6d34336 | 77 | value = p->min ; |
Rhyme | 0:ce97f6d34336 | 78 | } else if (value > p->max) { |
Rhyme | 0:ce97f6d34336 | 79 | value = p->max ; |
Rhyme | 0:ce97f6d34336 | 80 | } |
Rhyme | 0:ce97f6d34336 | 81 | y = p->top + p->height/2 - 1 |
Rhyme | 0:ce97f6d34336 | 82 | - (int)((p->height - 2) * value /(p->max - p->min)) ; |
Rhyme | 0:ce97f6d34336 | 83 | return( y ) ; |
Rhyme | 0:ce97f6d34336 | 84 | } |
Rhyme | 0:ce97f6d34336 | 85 | |
Rhyme | 0:ce97f6d34336 | 86 | void edge_accel::show(void) |
Rhyme | 0:ce97f6d34336 | 87 | { |
Rhyme | 0:ce97f6d34336 | 88 | int x, y ; |
Rhyme | 0:ce97f6d34336 | 89 | edge_chart_type *p = &edge_chart[0] ; /* edge_chart for accel */ |
Rhyme | 0:ce97f6d34336 | 90 | reset_watch_dog() ; |
Rhyme | 0:ce97f6d34336 | 91 | if (display) { |
Rhyme | 0:ce97f6d34336 | 92 | switch(display_mode) { |
Rhyme | 0:ce97f6d34336 | 93 | case DISPLAY_MODE_SUMMARY: |
Rhyme | 0:ce97f6d34336 | 94 | display->BusEnable(true) ; |
Rhyme | 0:ce97f6d34336 | 95 | display->set_font((unsigned char*) Arial12x12); |
Rhyme | 0:ce97f6d34336 | 96 | display->set_font_zoom(2, 2) ; |
Rhyme | 0:ce97f6d34336 | 97 | display->foreground(White) ; |
Rhyme | 0:ce97f6d34336 | 98 | display->locate(EDGE_SUMMARY_X, EDGE_SUMMARY_TIME_Y) ; |
Rhyme | 0:ce97f6d34336 | 99 | displayTime(_sampled_time) ; |
Rhyme | 0:ce97f6d34336 | 100 | // display->locate(10, 5) ; |
Rhyme | 0:ce97f6d34336 | 101 | // display->printf(timestr) ; |
Rhyme | 0:ce97f6d34336 | 102 | display->locate(EDGE_SUMMARY_X, EDGE_SUMMARY_ACCEL_Y) ; |
Rhyme | 0:ce97f6d34336 | 103 | display->printf("Accel: %.3f [%4d]", _value, _num_sampled) ; |
Rhyme | 0:ce97f6d34336 | 104 | display->BusEnable(false) ; |
Rhyme | 0:ce97f6d34336 | 105 | reset_watch_dog() ; |
Rhyme | 0:ce97f6d34336 | 106 | break ; |
Rhyme | 0:ce97f6d34336 | 107 | case DISPLAY_MODE_CHART: |
Rhyme | 0:ce97f6d34336 | 108 | x = p->left + p->index + 1; |
Rhyme | 0:ce97f6d34336 | 109 | y = accel_v2y(_value, p) ; |
Rhyme | 0:ce97f6d34336 | 110 | display->BusEnable(true) ; |
Rhyme | 0:ce97f6d34336 | 111 | if (p->index == 0) { |
Rhyme | 0:ce97f6d34336 | 112 | draw_chart_frame(p) ; |
Rhyme | 0:ce97f6d34336 | 113 | } |
Rhyme | 0:ce97f6d34336 | 114 | display->pixel(x, y, White) ; |
Rhyme | 0:ce97f6d34336 | 115 | display->BusEnable(false) ; |
Rhyme | 0:ce97f6d34336 | 116 | p->index = (p->index + 1) % (p->width - 2) ; |
Rhyme | 0:ce97f6d34336 | 117 | break ; |
Rhyme | 0:ce97f6d34336 | 118 | default: |
Rhyme | 0:ce97f6d34336 | 119 | break ; |
Rhyme | 0:ce97f6d34336 | 120 | } |
Rhyme | 0:ce97f6d34336 | 121 | } |
Rhyme | 0:ce97f6d34336 | 122 | clear_value() ; |
Rhyme | 0:ce97f6d34336 | 123 | reset_watch_dog() ; |
Rhyme | 0:ce97f6d34336 | 124 | } |
Rhyme | 0:ce97f6d34336 | 125 | |
Rhyme | 0:ce97f6d34336 | 126 | int edge_accel::accum(void) |
Rhyme | 0:ce97f6d34336 | 127 | { |
Rhyme | 0:ce97f6d34336 | 128 | int result ; |
Rhyme | 0:ce97f6d34336 | 129 | int16_t value[3] ; |
Rhyme | 0:ce97f6d34336 | 130 | |
Rhyme | 0:ce97f6d34336 | 131 | if (_enable) { |
Rhyme | 0:ce97f6d34336 | 132 | result = _accel->getAllRawData(value) ; |
Rhyme | 0:ce97f6d34336 | 133 | |
Rhyme | 0:ce97f6d34336 | 134 | if (result == 0) { /* success */ |
Rhyme | 0:ce97f6d34336 | 135 | if (_sample_count != 0) { /* first data does not have prev_data */ |
Rhyme | 0:ce97f6d34336 | 136 | _accumulation += |
Rhyme | 0:ce97f6d34336 | 137 | abs(_prev_x - value[0]) |
Rhyme | 0:ce97f6d34336 | 138 | + abs(_prev_y - value[1]) |
Rhyme | 0:ce97f6d34336 | 139 | + abs(_prev_z - value[2]) ; |
Rhyme | 0:ce97f6d34336 | 140 | } |
Rhyme | 0:ce97f6d34336 | 141 | |
Rhyme | 0:ce97f6d34336 | 142 | _sample_count++ ; |
Rhyme | 0:ce97f6d34336 | 143 | |
Rhyme | 0:ce97f6d34336 | 144 | _prev_x = value[0] ; |
Rhyme | 0:ce97f6d34336 | 145 | _prev_y = value[1] ; |
Rhyme | 0:ce97f6d34336 | 146 | _prev_z = value[2] ; |
Rhyme | 0:ce97f6d34336 | 147 | } |
Rhyme | 0:ce97f6d34336 | 148 | } |
Rhyme | 0:ce97f6d34336 | 149 | |
Rhyme | 0:ce97f6d34336 | 150 | return( result ) ; |
Rhyme | 0:ce97f6d34336 | 151 | } |
Rhyme | 0:ce97f6d34336 | 152 | |
Rhyme | 0:ce97f6d34336 | 153 | void edge_accel::clear_value(void) |
Rhyme | 0:ce97f6d34336 | 154 | { |
Rhyme | 0:ce97f6d34336 | 155 | _sample_count = 0 ; |
Rhyme | 0:ce97f6d34336 | 156 | _accumulation = 0 ; |
Rhyme | 0:ce97f6d34336 | 157 | _prev_x = 0 ; |
Rhyme | 0:ce97f6d34336 | 158 | _prev_y = 0 ; |
Rhyme | 0:ce97f6d34336 | 159 | _prev_z = 0 ; |
Rhyme | 0:ce97f6d34336 | 160 | } |
Rhyme | 0:ce97f6d34336 | 161 | |
Rhyme | 0:ce97f6d34336 | 162 | |
Rhyme | 0:ce97f6d34336 | 163 |