![](/media/cache/group/MFR16-new.png.50x50_q85.jpg)
Sweep a servo according to Proximity sensor measure
Dependencies: Servo X_NUCLEO_6180XA1 mbed
Fork of HelloWorld_6180XA1 by
main.cpp@10:5319abadb31e, 2015-10-19 (annotated)
- Committer:
- gallonm
- Date:
- Mon Oct 19 13:57:36 2015 +0200
- Revision:
- 10:5319abadb31e
- Parent:
- 9:1d0e839edee8
- Child:
- 11:657dbe7bf245
Fixed the main function introducing #ifdef to test the different operating modes
Test of the all operating modes without threshold and test range interrupt with
low threshold
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gallonm | 0:83c628a58feb | 1 | #include "mbed.h" |
gallonm | 8:4c05f7a5bb60 | 2 | #include "x_nucleo_6180xa1.h" |
gallonm | 0:83c628a58feb | 3 | |
gallonm | 4:ccd62fd7e137 | 4 | #include <string.h> |
gallonm | 4:ccd62fd7e137 | 5 | #include <stdlib.h> |
gallonm | 4:ccd62fd7e137 | 6 | #include <stdio.h> |
gallonm | 8:4c05f7a5bb60 | 7 | #include <assert.h> |
gallonm | 8:4c05f7a5bb60 | 8 | |
gallonm | 5:fa65d931bd96 | 9 | #define VL6180X_I2C_SDA I2C_SDA |
gallonm | 5:fa65d931bd96 | 10 | #define VL6180X_I2C_SCL I2C_SCL |
gallonm | 5:fa65d931bd96 | 11 | |
gallonm | 10:5319abadb31e | 12 | //#define RANGE_SINGLE_SHOT_POLLING |
gallonm | 10:5319abadb31e | 13 | //#define ALS_SINGLE_SHOT_POLLING |
gallonm | 10:5319abadb31e | 14 | //#define RANGE_CONTINUOUS_POLLING |
gallonm | 10:5319abadb31e | 15 | //#define ALS_CONTINUOUS_POLLING |
gallonm | 10:5319abadb31e | 16 | //#define RANGE_CONTINUOUS_INTERRUPT |
gallonm | 10:5319abadb31e | 17 | //#define ALS_CONTINUOUS_INTERRUPT |
gallonm | 10:5319abadb31e | 18 | #define INTERLEAVED_MODE_INTERRUPT |
gallonm | 10:5319abadb31e | 19 | //#define RANGE_CONTINUOUS_POLLING_LOW_THRESHOLD |
gallonm | 10:5319abadb31e | 20 | //#define RANGE_CONTINUOUS_POLLING_HIGH_THRESHOLD |
gallonm | 10:5319abadb31e | 21 | //#define RANGE_CONTINUOUS_POLLING_OUT_OF_WINDOW |
gallonm | 10:5319abadb31e | 22 | //#define ALS_CONTINUOUS_POLLING_LOW_THRESHOLD |
gallonm | 10:5319abadb31e | 23 | //#define ALS_CONTINUOUS_POLLING_HIGH_THRESHOLD |
gallonm | 10:5319abadb31e | 24 | //#define ALS_CONTINUOUS_POLLING_OUT_OF_WINDOW |
gallonm | 10:5319abadb31e | 25 | //#define RANGE_CONTINUOUS_INTERRUPT_LOW_THRESHOLD |
gallonm | 10:5319abadb31e | 26 | //#define RANGE_CONTINUOUS_INTERRUPT_HIGH_THRESHOLD |
gallonm | 10:5319abadb31e | 27 | //#define RANGE_CONTINUOUS_INTERRUPT_OUT_OF_WINDOW |
gallonm | 10:5319abadb31e | 28 | //#define ALS_CONTINUOUS_INTERRUPT_LOW_THRESHOLD |
gallonm | 10:5319abadb31e | 29 | //#define ALS_CONTINUOUS_INTERRUPT_HIGH_THRESHOLD |
gallonm | 10:5319abadb31e | 30 | //#define ALS_CONTINUOUS_INTERRUPT_OUT_OF_WINDOW |
gallonm | 10:5319abadb31e | 31 | |
gallonm | 9:1d0e839edee8 | 32 | /* Polling operating modes (1,2,3 and 4) don`t require callback function that handles IRQ |
gallonm | 9:1d0e839edee8 | 33 | Callback functions are used only for measure that require interrupt */ |
gallonm | 5:fa65d931bd96 | 34 | |
gallonm | 4:ccd62fd7e137 | 35 | void OnErrLog(void){}; |
gallonm | 5:fa65d931bd96 | 36 | void DISP_ExecLoopBody(void){}; |
gallonm | 0:83c628a58feb | 37 | |
gallonm | 8:4c05f7a5bb60 | 38 | DevI2C device_i2c(VL6180X_I2C_SDA, VL6180X_I2C_SCL); |
gallonm | 9:1d0e839edee8 | 39 | static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&device_i2c); |
gallonm | 9:1d0e839edee8 | 40 | |
gallonm | 9:1d0e839edee8 | 41 | MeasureData_t Data; |
gallonm | 9:1d0e839edee8 | 42 | |
gallonm | 9:1d0e839edee8 | 43 | /* flags that handle the call to HandleIRQ function */ |
gallonm | 9:1d0e839edee8 | 44 | bool flag_sensor_top=false, flag_sensor_bottom=false, flag_sensor_left=false, flag_sensor_right=false; |
gallonm | 9:1d0e839edee8 | 45 | |
gallonm | 10:5319abadb31e | 46 | //Timer timer; |
gallonm | 10:5319abadb31e | 47 | //int start, end; |
gallonm | 10:5319abadb31e | 48 | |
gallonm | 9:1d0e839edee8 | 49 | /* callback functions of the sensors */ |
gallonm | 9:1d0e839edee8 | 50 | void SensorTopIRQ(void) |
gallonm | 9:1d0e839edee8 | 51 | { |
gallonm | 10:5319abadb31e | 52 | flag_sensor_top=true; |
gallonm | 9:1d0e839edee8 | 53 | board->sensor_top->DisableInterruptMeasureDetectionIRQ(); |
gallonm | 9:1d0e839edee8 | 54 | } |
gallonm | 9:1d0e839edee8 | 55 | |
gallonm | 9:1d0e839edee8 | 56 | //inserire le callback per gli altri sensori |
gallonm | 9:1d0e839edee8 | 57 | |
gallonm | 4:ccd62fd7e137 | 58 | int main() |
gallonm | 5:fa65d931bd96 | 59 | { |
gallonm | 9:1d0e839edee8 | 60 | int status; |
gallonm | 9:1d0e839edee8 | 61 | |
gallonm | 10:5319abadb31e | 62 | status=board->InitBoard(); |
gallonm | 9:1d0e839edee8 | 63 | if(status) |
gallonm | 9:1d0e839edee8 | 64 | printf("Failed to init the board!\n\r"); |
gallonm | 10:5319abadb31e | 65 | |
gallonm | 10:5319abadb31e | 66 | #ifdef RANGE_SINGLE_SHOT_POLLING |
gallonm | 10:5319abadb31e | 67 | status=board->sensor_top->StartMeasurement(range_single_shot_polling, NULL, &Data, NULL, NULL); |
gallonm | 9:1d0e839edee8 | 68 | if(!status) |
gallonm | 10:5319abadb31e | 69 | printf("Range single shot: %dmm\n\r",Data.range_mm); |
gallonm | 10:5319abadb31e | 70 | else if(status==INVALID_PARAMS) |
gallonm | 9:1d0e839edee8 | 71 | printf("Failed to start measurement!\n\r"); |
gallonm | 9:1d0e839edee8 | 72 | else |
gallonm | 10:5319abadb31e | 73 | printf("Invalid range value!\n\r"); |
gallonm | 10:5319abadb31e | 74 | status=board->sensor_top->StopMeasurement(range_single_shot_polling); |
gallonm | 10:5319abadb31e | 75 | if(status) |
gallonm | 10:5319abadb31e | 76 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 77 | #endif |
gallonm | 9:1d0e839edee8 | 78 | |
gallonm | 10:5319abadb31e | 79 | #ifdef ALS_SINGLE_SHOT_POLLING |
gallonm | 10:5319abadb31e | 80 | status=board->sensor_top->StartMeasurement(als_single_shot_polling, NULL, &Data, NULL, NULL); |
gallonm | 10:5319abadb31e | 81 | if(!status) |
gallonm | 10:5319abadb31e | 82 | printf("Light single shot: %dlux\n\r",Data.lux); |
gallonm | 10:5319abadb31e | 83 | else if(status==INVALID_PARAMS) |
gallonm | 10:5319abadb31e | 84 | printf("Failed to start measurement!\n\r"); |
gallonm | 10:5319abadb31e | 85 | else |
gallonm | 10:5319abadb31e | 86 | printf("Invalid light value!\n\r"); |
gallonm | 10:5319abadb31e | 87 | status=board->sensor_top->StopMeasurement(als_single_shot_polling); |
gallonm | 10:5319abadb31e | 88 | if(status) |
gallonm | 10:5319abadb31e | 89 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 90 | #endif |
gallonm | 10:5319abadb31e | 91 | |
gallonm | 10:5319abadb31e | 92 | #ifdef RANGE_CONTINUOUS_POLLING |
gallonm | 10:5319abadb31e | 93 | status=board->sensor_top->StartMeasurement(range_continuous_polling, NULL, &Data, NULL, NULL); |
gallonm | 9:1d0e839edee8 | 94 | if(!status) |
gallonm | 9:1d0e839edee8 | 95 | { |
gallonm | 10:5319abadb31e | 96 | int i; |
gallonm | 10:5319abadb31e | 97 | for(i=0;i<10;i++) |
gallonm | 10:5319abadb31e | 98 | { |
gallonm | 10:5319abadb31e | 99 | status=board->sensor_top->GetMeasurement(range_continuous_polling, &Data); |
gallonm | 10:5319abadb31e | 100 | if(!status) |
gallonm | 10:5319abadb31e | 101 | printf("Range measure %d: %dmm\n\r",i+1,Data.range_mm); |
gallonm | 10:5319abadb31e | 102 | else |
gallonm | 10:5319abadb31e | 103 | printf("Invalid range value!\n\r"); |
gallonm | 10:5319abadb31e | 104 | } |
gallonm | 10:5319abadb31e | 105 | } |
gallonm | 10:5319abadb31e | 106 | else |
gallonm | 10:5319abadb31e | 107 | printf("Failed to start measurement!\n\r"); |
gallonm | 10:5319abadb31e | 108 | status=board->sensor_top->StopMeasurement(range_continuous_polling); |
gallonm | 10:5319abadb31e | 109 | if(status) |
gallonm | 10:5319abadb31e | 110 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 111 | #endif |
gallonm | 10:5319abadb31e | 112 | |
gallonm | 10:5319abadb31e | 113 | #ifdef ALS_CONTINUOUS_POLLING |
gallonm | 10:5319abadb31e | 114 | status=board->sensor_top->StartMeasurement(als_continuous_polling, NULL, &Data, NULL, NULL); |
gallonm | 10:5319abadb31e | 115 | if(!status) |
gallonm | 10:5319abadb31e | 116 | { |
gallonm | 10:5319abadb31e | 117 | int i; |
gallonm | 10:5319abadb31e | 118 | for(i=0;i<10;i++) |
gallonm | 10:5319abadb31e | 119 | { |
gallonm | 10:5319abadb31e | 120 | status=board->sensor_top->GetMeasurement(als_continuous_polling, &Data); |
gallonm | 10:5319abadb31e | 121 | if(!status) |
gallonm | 10:5319abadb31e | 122 | printf("Light measure %d: %dlux\n\r",i,Data.lux); |
gallonm | 10:5319abadb31e | 123 | else |
gallonm | 10:5319abadb31e | 124 | printf("Invalid light value!\n\r"); |
gallonm | 10:5319abadb31e | 125 | } |
gallonm | 9:1d0e839edee8 | 126 | } |
gallonm | 9:1d0e839edee8 | 127 | else |
gallonm | 9:1d0e839edee8 | 128 | printf("Failed to start measurement!\n\r"); |
gallonm | 10:5319abadb31e | 129 | status=board->sensor_top->StopMeasurement(als_continuous_polling); |
gallonm | 10:5319abadb31e | 130 | if(status) |
gallonm | 10:5319abadb31e | 131 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 132 | #endif |
gallonm | 9:1d0e839edee8 | 133 | |
gallonm | 10:5319abadb31e | 134 | #ifdef RANGE_CONTINUOUS_INTERRUPT |
gallonm | 10:5319abadb31e | 135 | status=board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &Data, NULL, NULL); |
gallonm | 9:1d0e839edee8 | 136 | if(!status) |
gallonm | 9:1d0e839edee8 | 137 | { |
gallonm | 10:5319abadb31e | 138 | while(1) |
gallonm | 10:5319abadb31e | 139 | { |
gallonm | 10:5319abadb31e | 140 | if(flag_sensor_top) |
gallonm | 10:5319abadb31e | 141 | { |
gallonm | 10:5319abadb31e | 142 | flag_sensor_top=false; |
gallonm | 10:5319abadb31e | 143 | status=board->sensor_top->HandleIRQ(range_continuous_interrupt, &Data); |
gallonm | 10:5319abadb31e | 144 | if(!status) |
gallonm | 10:5319abadb31e | 145 | printf("Range int: %dmm\n\r",Data.range_mm); |
gallonm | 10:5319abadb31e | 146 | else |
gallonm | 10:5319abadb31e | 147 | printf("Invalid range value!\n\r"); |
gallonm | 10:5319abadb31e | 148 | } |
gallonm | 10:5319abadb31e | 149 | } |
gallonm | 10:5319abadb31e | 150 | } |
gallonm | 10:5319abadb31e | 151 | else |
gallonm | 10:5319abadb31e | 152 | printf("Failed to start measurement!\n\r"); |
gallonm | 10:5319abadb31e | 153 | status=board->sensor_top->StopMeasurement(range_continuous_interrupt); |
gallonm | 10:5319abadb31e | 154 | if(status) |
gallonm | 10:5319abadb31e | 155 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 156 | #endif |
gallonm | 10:5319abadb31e | 157 | |
gallonm | 10:5319abadb31e | 158 | #ifdef ALS_CONTINUOUS_INTERRUPT |
gallonm | 10:5319abadb31e | 159 | status=board->sensor_top->StartMeasurement(als_continuous_interrupt, SensorTopIRQ, &Data, NULL, NULL); |
gallonm | 10:5319abadb31e | 160 | if(!status) |
gallonm | 10:5319abadb31e | 161 | { |
gallonm | 10:5319abadb31e | 162 | while(1) |
gallonm | 10:5319abadb31e | 163 | { |
gallonm | 10:5319abadb31e | 164 | if(flag_sensor_top) |
gallonm | 10:5319abadb31e | 165 | { |
gallonm | 10:5319abadb31e | 166 | flag_sensor_top=false; |
gallonm | 10:5319abadb31e | 167 | status=board->sensor_top->HandleIRQ(als_continuous_interrupt, &Data); |
gallonm | 10:5319abadb31e | 168 | if(!status) |
gallonm | 10:5319abadb31e | 169 | printf("Light int: %dlux\n\r",Data.lux); |
gallonm | 10:5319abadb31e | 170 | else |
gallonm | 10:5319abadb31e | 171 | printf("Invalid light value!\n\r"); |
gallonm | 10:5319abadb31e | 172 | } |
gallonm | 10:5319abadb31e | 173 | } |
gallonm | 10:5319abadb31e | 174 | } |
gallonm | 9:1d0e839edee8 | 175 | else |
gallonm | 10:5319abadb31e | 176 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 177 | status=board->sensor_top->StopMeasurement(als_continuous_interrupt); |
gallonm | 10:5319abadb31e | 178 | if(status) |
gallonm | 10:5319abadb31e | 179 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 180 | #endif |
gallonm | 9:1d0e839edee8 | 181 | |
gallonm | 10:5319abadb31e | 182 | #ifdef INTERLEAVED_MODE_INTERRUPT |
gallonm | 10:5319abadb31e | 183 | status=board->sensor_top->StartMeasurement(interleaved_mode_interrupt, SensorTopIRQ, &Data, NULL, NULL); |
gallonm | 10:5319abadb31e | 184 | if(!status) |
gallonm | 9:1d0e839edee8 | 185 | { |
gallonm | 10:5319abadb31e | 186 | while(1) |
gallonm | 10:5319abadb31e | 187 | { |
gallonm | 10:5319abadb31e | 188 | if(flag_sensor_top) |
gallonm | 10:5319abadb31e | 189 | { |
gallonm | 10:5319abadb31e | 190 | flag_sensor_top=false; |
gallonm | 10:5319abadb31e | 191 | status=board->sensor_top->HandleIRQ(interleaved_mode_interrupt, &Data); |
gallonm | 10:5319abadb31e | 192 | if(!status) |
gallonm | 10:5319abadb31e | 193 | { |
gallonm | 10:5319abadb31e | 194 | printf("Interleaved_mode->Range: %dmm\n\r",Data.range_mm); |
gallonm | 10:5319abadb31e | 195 | printf("Interleaved_mode->Light: %dlux\n\r",Data.lux); |
gallonm | 10:5319abadb31e | 196 | } |
gallonm | 10:5319abadb31e | 197 | else |
gallonm | 10:5319abadb31e | 198 | printf("Invalid range or light value!\n\r"); |
gallonm | 10:5319abadb31e | 199 | } |
gallonm | 10:5319abadb31e | 200 | } |
gallonm | 9:1d0e839edee8 | 201 | } |
gallonm | 10:5319abadb31e | 202 | else |
gallonm | 10:5319abadb31e | 203 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 204 | status=board->sensor_top->StopMeasurement(interleaved_mode_interrupt); |
gallonm | 10:5319abadb31e | 205 | if(status) |
gallonm | 10:5319abadb31e | 206 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 207 | #endif |
gallonm | 10:5319abadb31e | 208 | |
gallonm | 10:5319abadb31e | 209 | #ifdef RANGE_CONTINUOUS_INTERRUPT_LOW_THRESHOLD |
gallonm | 10:5319abadb31e | 210 | status=board->sensor_top->StartMeasurement(range_continuous_interrupt_low_threshold, SensorTopIRQ, &Data, 80, NULL); |
gallonm | 10:5319abadb31e | 211 | if(!status) |
gallonm | 10:5319abadb31e | 212 | { |
gallonm | 10:5319abadb31e | 213 | while(1) |
gallonm | 10:5319abadb31e | 214 | { |
gallonm | 10:5319abadb31e | 215 | if(flag_sensor_top) |
gallonm | 10:5319abadb31e | 216 | { |
gallonm | 10:5319abadb31e | 217 | flag_sensor_top=false; |
gallonm | 10:5319abadb31e | 218 | status=board->sensor_top->HandleIRQ(range_continuous_interrupt_low_threshold, &Data); |
gallonm | 10:5319abadb31e | 219 | if(!status) |
gallonm | 10:5319abadb31e | 220 | printf("Range int low threshold: %dmm\n\r",Data.range_mm); |
gallonm | 10:5319abadb31e | 221 | else |
gallonm | 10:5319abadb31e | 222 | printf("Invalid range value!\n\r"); |
gallonm | 10:5319abadb31e | 223 | } |
gallonm | 10:5319abadb31e | 224 | } |
gallonm | 10:5319abadb31e | 225 | } |
gallonm | 10:5319abadb31e | 226 | else |
gallonm | 10:5319abadb31e | 227 | printf("Failed to start measurement!\n\r"); |
gallonm | 10:5319abadb31e | 228 | status=board->sensor_top->StopMeasurement(range_continuous_interrupt_low_threshold); |
gallonm | 10:5319abadb31e | 229 | if(status) |
gallonm | 10:5319abadb31e | 230 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 231 | #endif |
gallonm | 5:fa65d931bd96 | 232 | |
gallonm | 4:ccd62fd7e137 | 233 | } |
gallonm | 5:fa65d931bd96 | 234 | |
gallonm | 5:fa65d931bd96 | 235 |