![](/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@9:1d0e839edee8, 2015-10-13 (annotated)
- Committer:
- gallonm
- Date:
- Tue Oct 13 14:26:18 2015 +0200
- Revision:
- 9:1d0e839edee8
- Parent:
- 8:4c05f7a5bb60
- Child:
- 10:5319abadb31e
Created a test bench to verify the differet type of operating mode.
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 | 4:ccd62fd7e137 | 9 | |
gallonm | 5:fa65d931bd96 | 10 | #define VL6180X_I2C_SDA I2C_SDA |
gallonm | 5:fa65d931bd96 | 11 | #define VL6180X_I2C_SCL I2C_SCL |
gallonm | 5:fa65d931bd96 | 12 | |
gallonm | 9:1d0e839edee8 | 13 | /* Polling operating modes (1,2,3 and 4) don`t require callback function that handles IRQ |
gallonm | 9:1d0e839edee8 | 14 | Callback functions are used only for measure that require interrupt */ |
gallonm | 5:fa65d931bd96 | 15 | |
gallonm | 4:ccd62fd7e137 | 16 | void OnErrLog(void){}; |
gallonm | 5:fa65d931bd96 | 17 | void DISP_ExecLoopBody(void){}; |
gallonm | 0:83c628a58feb | 18 | |
gallonm | 8:4c05f7a5bb60 | 19 | DevI2C device_i2c(VL6180X_I2C_SDA, VL6180X_I2C_SCL); |
gallonm | 9:1d0e839edee8 | 20 | static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&device_i2c); |
gallonm | 9:1d0e839edee8 | 21 | |
gallonm | 9:1d0e839edee8 | 22 | MeasureData_t Data; |
gallonm | 9:1d0e839edee8 | 23 | |
gallonm | 9:1d0e839edee8 | 24 | /* flags that handle the call to HandleIRQ function */ |
gallonm | 9:1d0e839edee8 | 25 | bool flag_sensor_top=false, flag_sensor_bottom=false, flag_sensor_left=false, flag_sensor_right=false; |
gallonm | 9:1d0e839edee8 | 26 | |
gallonm | 9:1d0e839edee8 | 27 | /* callback functions of the sensors */ |
gallonm | 9:1d0e839edee8 | 28 | void SensorTopIRQ(void) |
gallonm | 9:1d0e839edee8 | 29 | { |
gallonm | 9:1d0e839edee8 | 30 | flag_sensor_top=true; |
gallonm | 9:1d0e839edee8 | 31 | board->sensor_top->DisableInterruptMeasureDetectionIRQ(); |
gallonm | 9:1d0e839edee8 | 32 | } |
gallonm | 9:1d0e839edee8 | 33 | |
gallonm | 9:1d0e839edee8 | 34 | //inserire le callback per gli altri sensori |
gallonm | 9:1d0e839edee8 | 35 | |
gallonm | 4:ccd62fd7e137 | 36 | int main() |
gallonm | 5:fa65d931bd96 | 37 | { |
gallonm | 9:1d0e839edee8 | 38 | int status; |
gallonm | 9:1d0e839edee8 | 39 | |
gallonm | 9:1d0e839edee8 | 40 | status=board->InitBoard(); |
gallonm | 9:1d0e839edee8 | 41 | if(status) |
gallonm | 9:1d0e839edee8 | 42 | printf("Failed to init the board!\n\r"); |
gallonm | 9:1d0e839edee8 | 43 | |
gallonm | 9:1d0e839edee8 | 44 | /* |
gallonm | 9:1d0e839edee8 | 45 | status=board->sensor_top->StartMeasurement(range_single_shot_polling, NULL, &Data); |
gallonm | 9:1d0e839edee8 | 46 | if(!status) |
gallonm | 9:1d0e839edee8 | 47 | printf("Range single shot: %d\n\r",Data.range_mm); |
gallonm | 9:1d0e839edee8 | 48 | else |
gallonm | 9:1d0e839edee8 | 49 | printf("Failed to start measurement!\n\r"); |
gallonm | 9:1d0e839edee8 | 50 | status=board->sensor_top->StartMeasurement(als_single_shot_polling, NULL, &Data); |
gallonm | 9:1d0e839edee8 | 51 | if(!status) |
gallonm | 9:1d0e839edee8 | 52 | printf("Light single shot: %d\n\r",Data.lux); |
gallonm | 9:1d0e839edee8 | 53 | else |
gallonm | 9:1d0e839edee8 | 54 | printf("Failed to start measurement!\n\r"); |
gallonm | 9:1d0e839edee8 | 55 | |
gallonm | 9:1d0e839edee8 | 56 | status=board->sensor_top->StartMeasurement(range_continuous_polling, NULL, &Data); |
gallonm | 9:1d0e839edee8 | 57 | if(!status) |
gallonm | 9:1d0e839edee8 | 58 | { |
gallonm | 9:1d0e839edee8 | 59 | status=board->sensor_top->GetRangeMeasContinuousMode(&Data); |
gallonm | 9:1d0e839edee8 | 60 | if(!status) |
gallonm | 9:1d0e839edee8 | 61 | printf("1a Range: %d\n\r",Data.range_mm); |
gallonm | 9:1d0e839edee8 | 62 | else |
gallonm | 9:1d0e839edee8 | 63 | printf("Invalid range value!\n\r"); |
gallonm | 9:1d0e839edee8 | 64 | status=board->sensor_top->GetRangeMeasContinuousMode(&Data); |
gallonm | 9:1d0e839edee8 | 65 | if(!status) |
gallonm | 9:1d0e839edee8 | 66 | printf("2a Range: %d\n\r",Data.range_mm); |
gallonm | 9:1d0e839edee8 | 67 | else |
gallonm | 9:1d0e839edee8 | 68 | printf("Invalid range value!\n\r"); |
gallonm | 9:1d0e839edee8 | 69 | status=board->sensor_top->GetRangeMeasContinuousMode(&Data); |
gallonm | 9:1d0e839edee8 | 70 | if(!status) |
gallonm | 9:1d0e839edee8 | 71 | printf("3a Range: %d\n\r",Data.range_mm); |
gallonm | 9:1d0e839edee8 | 72 | else |
gallonm | 9:1d0e839edee8 | 73 | printf("Invalid range value!\n\r"); |
gallonm | 9:1d0e839edee8 | 74 | } |
gallonm | 9:1d0e839edee8 | 75 | else |
gallonm | 9:1d0e839edee8 | 76 | printf("Failed to start measurement!\n\r"); |
gallonm | 9:1d0e839edee8 | 77 | |
gallonm | 9:1d0e839edee8 | 78 | status=board->sensor_top->StartMeasurement(als_continuous_polling, NULL, &Data); |
gallonm | 9:1d0e839edee8 | 79 | if(!status) |
gallonm | 9:1d0e839edee8 | 80 | { |
gallonm | 9:1d0e839edee8 | 81 | status=board->sensor_top->GetAlsMeasContinuousMode(&Data); |
gallonm | 9:1d0e839edee8 | 82 | if(!status) |
gallonm | 9:1d0e839edee8 | 83 | printf("1a Light: %d\n\r",Data.lux); |
gallonm | 9:1d0e839edee8 | 84 | else |
gallonm | 9:1d0e839edee8 | 85 | printf("Invalid light value!\n\r"); |
gallonm | 9:1d0e839edee8 | 86 | status=board->sensor_top->GetAlsMeasContinuousMode(&Data); |
gallonm | 9:1d0e839edee8 | 87 | if(!status) |
gallonm | 9:1d0e839edee8 | 88 | printf("2a Light: %d\n\r",Data.lux); |
gallonm | 9:1d0e839edee8 | 89 | else |
gallonm | 9:1d0e839edee8 | 90 | printf("Invalid light value!\n\r"); |
gallonm | 9:1d0e839edee8 | 91 | status=board->sensor_top->GetAlsMeasContinuousMode(&Data); |
gallonm | 9:1d0e839edee8 | 92 | if(!status) |
gallonm | 9:1d0e839edee8 | 93 | printf("3a Light: %d\n\r",Data.lux); |
gallonm | 9:1d0e839edee8 | 94 | else |
gallonm | 9:1d0e839edee8 | 95 | printf("Invalid light value!\n\r"); |
gallonm | 9:1d0e839edee8 | 96 | } |
gallonm | 9:1d0e839edee8 | 97 | else |
gallonm | 9:1d0e839edee8 | 98 | printf("Failed to start measurement!\n\r"); |
gallonm | 9:1d0e839edee8 | 99 | */ |
gallonm | 9:1d0e839edee8 | 100 | |
gallonm | 9:1d0e839edee8 | 101 | /*board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &Data); |
gallonm | 9:1d0e839edee8 | 102 | while(1) |
gallonm | 9:1d0e839edee8 | 103 | { |
gallonm | 9:1d0e839edee8 | 104 | if(flag_sensor_top) |
gallonm | 9:1d0e839edee8 | 105 | { |
gallonm | 9:1d0e839edee8 | 106 | flag_sensor_top=false; |
gallonm | 9:1d0e839edee8 | 107 | board->sensor_top->HandleIRQ(range_measure, &Data); |
gallonm | 9:1d0e839edee8 | 108 | if((Data.range_error==0)&&(Data.int_error==0)) |
gallonm | 9:1d0e839edee8 | 109 | printf("Range int: %d\n\r",Data.range_mm); |
gallonm | 9:1d0e839edee8 | 110 | else |
gallonm | 9:1d0e839edee8 | 111 | printf("Invalid range value!\n\r"); |
gallonm | 9:1d0e839edee8 | 112 | } |
gallonm | 9:1d0e839edee8 | 113 | }*/ |
gallonm | 9:1d0e839edee8 | 114 | |
gallonm | 9:1d0e839edee8 | 115 | /*board->sensor_top->StartMeasurement(als_continuous_interrupt, SensorTopIRQ, &Data); |
gallonm | 9:1d0e839edee8 | 116 | while(1) |
gallonm | 9:1d0e839edee8 | 117 | { |
gallonm | 9:1d0e839edee8 | 118 | if(flag_sensor_top) |
gallonm | 9:1d0e839edee8 | 119 | { |
gallonm | 9:1d0e839edee8 | 120 | flag_sensor_top=false; |
gallonm | 9:1d0e839edee8 | 121 | board->sensor_top->HandleIRQ(light_measure, &Data); |
gallonm | 9:1d0e839edee8 | 122 | if((Data.als_error==0)&&(Data.int_error==0)) |
gallonm | 9:1d0e839edee8 | 123 | printf("Light int: %d\n\r",Data.lux); |
gallonm | 9:1d0e839edee8 | 124 | else |
gallonm | 9:1d0e839edee8 | 125 | printf("Invalid light value!\n\r"); |
gallonm | 9:1d0e839edee8 | 126 | } |
gallonm | 9:1d0e839edee8 | 127 | }*/ |
gallonm | 9:1d0e839edee8 | 128 | |
gallonm | 9:1d0e839edee8 | 129 | |
gallonm | 9:1d0e839edee8 | 130 | board->sensor_top->StartMeasurement(interleaved_mode, SensorTopIRQ, &Data); |
gallonm | 9:1d0e839edee8 | 131 | while(1) |
gallonm | 9:1d0e839edee8 | 132 | { |
gallonm | 9:1d0e839edee8 | 133 | if(flag_sensor_top) |
gallonm | 9:1d0e839edee8 | 134 | { |
gallonm | 9:1d0e839edee8 | 135 | flag_sensor_top=false; |
gallonm | 9:1d0e839edee8 | 136 | board->sensor_top->HandleIRQ(light_range_measure, &Data); |
gallonm | 9:1d0e839edee8 | 137 | if((Data.range_error==0)&&(Data.int_error==0)) |
gallonm | 9:1d0e839edee8 | 138 | printf("Interleaved_mode->Range: %d\n\r",Data.range_mm); |
gallonm | 9:1d0e839edee8 | 139 | else |
gallonm | 9:1d0e839edee8 | 140 | printf("Invalid range value!\n\r"); |
gallonm | 9:1d0e839edee8 | 141 | if((Data.als_error==0)&&(Data.int_error==0)) |
gallonm | 9:1d0e839edee8 | 142 | printf("Interleaved_mode->Light: %d\n\r",Data.lux); |
gallonm | 9:1d0e839edee8 | 143 | else |
gallonm | 9:1d0e839edee8 | 144 | printf("Invalid light value!\n\r"); |
gallonm | 9:1d0e839edee8 | 145 | } |
gallonm | 9:1d0e839edee8 | 146 | } |
gallonm | 9:1d0e839edee8 | 147 | |
gallonm | 9:1d0e839edee8 | 148 | |
gallonm | 9:1d0e839edee8 | 149 | |
gallonm | 9:1d0e839edee8 | 150 | //board->~X_NUCLEO_6180XA1(); |
gallonm | 5:fa65d931bd96 | 151 | |
gallonm | 4:ccd62fd7e137 | 152 | } |
gallonm | 5:fa65d931bd96 | 153 | |
gallonm | 5:fa65d931bd96 | 154 | |
gallonm | 5:fa65d931bd96 | 155 |