charles macneill
/
VL53L3CX_NoShield_1Sensors_interrupt
Uses Ayoub's Custom library.
main.cpp@4:251419010655, 2021-07-22 (annotated)
- Committer:
- charlesmn
- Date:
- Thu Jul 22 11:45:12 2021 +0000
- Revision:
- 4:251419010655
- Parent:
- 3:025bcf0d211d
I had changed the name of X_NUCLEO_53L3CX but not the repository where it is store. This fixes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:137ce045e2a2 | 1 | /* |
charlesmn | 0:137ce045e2a2 | 2 | * This VL53L3 Expansion board test application performs range measurements |
charlesmn | 0:137ce045e2a2 | 3 | * using the onboard embedded sensor, and satellite boards, in interrupt mode. |
charlesmn | 0:137ce045e2a2 | 4 | * Measured ranges are output on the Serial Port, running at 115200 baud. |
charlesmn | 0:137ce045e2a2 | 5 | * Only supports one sensor at a time. Only tested on centre sensor. |
charlesmn | 0:137ce045e2a2 | 6 | * |
charlesmn | 0:137ce045e2a2 | 7 | * The Reset button can be used to restart the program. |
charlesmn | 0:137ce045e2a2 | 8 | * |
charlesmn | 0:137ce045e2a2 | 9 | * *** Note : |
charlesmn | 0:137ce045e2a2 | 10 | * Default Mbed build system settings disable printf floating-point support. |
charlesmn | 0:137ce045e2a2 | 11 | * Online builds seem unable to configure this. |
charlesmn | 0:137ce045e2a2 | 12 | * Offline builds can enable printf floating-point support. |
charlesmn | 0:137ce045e2a2 | 13 | * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md |
charlesmn | 0:137ce045e2a2 | 14 | * .\mbed-os\platform\mbed_lib.json |
charlesmn | 0:137ce045e2a2 | 15 | * |
charlesmn | 0:137ce045e2a2 | 16 | */ |
charlesmn | 0:137ce045e2a2 | 17 | |
charlesmn | 0:137ce045e2a2 | 18 | #include <stdio.h> |
charlesmn | 0:137ce045e2a2 | 19 | #include <time.h> |
charlesmn | 0:137ce045e2a2 | 20 | |
charlesmn | 0:137ce045e2a2 | 21 | #include "mbed.h" |
charlesmn | 0:137ce045e2a2 | 22 | |
charlesmn | 0:137ce045e2a2 | 23 | #include "vl53L3_I2c.h" |
charlesmn | 0:137ce045e2a2 | 24 | #include "vl53lx_platform_user_data.h" |
charlesmn | 0:137ce045e2a2 | 25 | #include "custom_ranging_sensor.h" |
charlesmn | 0:137ce045e2a2 | 26 | #include "Custom_RangingClass.h" |
charlesmn | 0:137ce045e2a2 | 27 | #include "pinmap.h" |
charlesmn | 0:137ce045e2a2 | 28 | |
charlesmn | 0:137ce045e2a2 | 29 | |
charlesmn | 0:137ce045e2a2 | 30 | #if (MBED_VERSION > 60300) |
charlesmn | 0:137ce045e2a2 | 31 | UnbufferedSerial pc(USBTX, USBRX); |
charlesmn | 0:137ce045e2a2 | 32 | extern "C" void wait_ms(int ms); |
charlesmn | 0:137ce045e2a2 | 33 | #else |
charlesmn | 0:137ce045e2a2 | 34 | Serial pc(SERIAL_TX, SERIAL_RX); |
charlesmn | 0:137ce045e2a2 | 35 | #endif |
charlesmn | 0:137ce045e2a2 | 36 | |
charlesmn | 0:137ce045e2a2 | 37 | /* Private define ------------------------------------------------------------*/ |
charlesmn | 0:137ce045e2a2 | 38 | #define SENSOR_LEFT_NUMBER 0 |
charlesmn | 0:137ce045e2a2 | 39 | #define SENSOR_CENTRE_NUMBER 1 |
charlesmn | 0:137ce045e2a2 | 40 | #define SENSOR_RIGHT_NUMBER 2 |
charlesmn | 0:137ce045e2a2 | 41 | |
charlesmn | 0:137ce045e2a2 | 42 | |
charlesmn | 0:137ce045e2a2 | 43 | |
charlesmn | 0:137ce045e2a2 | 44 | |
charlesmn | 0:137ce045e2a2 | 45 | PinName InterruptPins[] ={D8,A2,D2}; // interrupt pins for the three sensors. |
charlesmn | 1:3151ff0b9da9 | 46 | // these will depend on how the sensor is wired |
charlesmn | 0:137ce045e2a2 | 47 | |
charlesmn | 0:137ce045e2a2 | 48 | |
charlesmn | 0:137ce045e2a2 | 49 | /* Private variables ---------------------------------------------------------*/ |
charlesmn | 0:137ce045e2a2 | 50 | static int32_t status = 0; |
charlesmn | 0:137ce045e2a2 | 51 | CUSTOM_SENSOR *sensor; //class for sensor commands |
charlesmn | 0:137ce045e2a2 | 52 | |
charlesmn | 0:137ce045e2a2 | 53 | |
charlesmn | 0:137ce045e2a2 | 54 | /* Private function prototypes -----------------------------------------------*/ |
charlesmn | 0:137ce045e2a2 | 55 | static void MX_53L3A2_MultiSensorRanging_Init(void); |
charlesmn | 0:137ce045e2a2 | 56 | static void MX_53L3A2_MultiSensorRanging_Process(void); |
charlesmn | 0:137ce045e2a2 | 57 | static void print_result(RANGING_SENSOR_Result_t *Result); |
charlesmn | 0:137ce045e2a2 | 58 | |
charlesmn | 0:137ce045e2a2 | 59 | |
charlesmn | 0:137ce045e2a2 | 60 | //flag to signal an interrupt has occured |
charlesmn | 0:137ce045e2a2 | 61 | volatile uint8_t EventDetected = 0; |
charlesmn | 0:137ce045e2a2 | 62 | |
charlesmn | 0:137ce045e2a2 | 63 | int ToF_sensor = CUSTOM_VL53L3CX; // select the sensor to use. In custom_ranging_sensor.h |
charlesmn | 0:137ce045e2a2 | 64 | |
charlesmn | 0:137ce045e2a2 | 65 | |
charlesmn | 0:137ce045e2a2 | 66 | /* ISR callback function of the active sensor */ |
charlesmn | 1:3151ff0b9da9 | 67 | /* all it does set a flag which causes the main loop to get data. */ |
charlesmn | 1:3151ff0b9da9 | 68 | /* Interrupts activated and defined in CUSTOM_RANGING_ConfigProfile() */ |
charlesmn | 0:137ce045e2a2 | 69 | void sensor_irq(void) |
charlesmn | 0:137ce045e2a2 | 70 | { |
charlesmn | 0:137ce045e2a2 | 71 | EventDetected = 1; |
charlesmn | 0:137ce045e2a2 | 72 | } |
charlesmn | 0:137ce045e2a2 | 73 | |
charlesmn | 0:137ce045e2a2 | 74 | |
charlesmn | 0:137ce045e2a2 | 75 | // initialise the sensor |
charlesmn | 0:137ce045e2a2 | 76 | static void MX_53L3A2_MultiSensorRanging_Init(void) |
charlesmn | 0:137ce045e2a2 | 77 | { |
charlesmn | 0:137ce045e2a2 | 78 | status = sensor->CUSTOM_RANGING_Init(ToF_sensor); |
charlesmn | 0:137ce045e2a2 | 79 | if (status) |
charlesmn | 0:137ce045e2a2 | 80 | { |
charlesmn | 0:137ce045e2a2 | 81 | printf("CUSTOM_RANGING_Init failed for sensor %d status %d\n",ToF_sensor,status); |
charlesmn | 0:137ce045e2a2 | 82 | } |
charlesmn | 0:137ce045e2a2 | 83 | |
charlesmn | 0:137ce045e2a2 | 84 | wait_ms(100); |
charlesmn | 0:137ce045e2a2 | 85 | } |
charlesmn | 0:137ce045e2a2 | 86 | |
charlesmn | 0:137ce045e2a2 | 87 | |
charlesmn | 0:137ce045e2a2 | 88 | // start ranging and enter an infinite loop to collect measurements |
charlesmn | 0:137ce045e2a2 | 89 | // The collection of data is triggered by a flag which is set after an interrupt occurs |
charlesmn | 0:137ce045e2a2 | 90 | static void MX_53L3A2_MultiSensorRanging_Process(void) |
charlesmn | 0:137ce045e2a2 | 91 | { |
charlesmn | 0:137ce045e2a2 | 92 | |
charlesmn | 0:137ce045e2a2 | 93 | // config profile details |
charlesmn | 0:137ce045e2a2 | 94 | RANGING_SENSOR_Result_t Result; |
charlesmn | 0:137ce045e2a2 | 95 | RANGING_SENSOR_ProfileConfig_t Profile; |
charlesmn | 2:2489dfbd49d7 | 96 | uint16_t zone = 0; |
charlesmn | 2:2489dfbd49d7 | 97 | uint16_t target = 0; |
charlesmn | 0:137ce045e2a2 | 98 | printf("MX_53L3A2_MultiSensorRanging_Process\n"); |
charlesmn | 0:137ce045e2a2 | 99 | |
charlesmn | 0:137ce045e2a2 | 100 | |
charlesmn | 0:137ce045e2a2 | 101 | Profile.RangingProfile = RS_MULTI_TARGET_LONG_RANGE; |
charlesmn | 0:137ce045e2a2 | 102 | Profile.TimingBudget = 30; /* 16 ms < TimingBudget < 500 ms */ |
charlesmn | 0:137ce045e2a2 | 103 | Profile.Frequency = 0; /* not necessary in simple ranging */ |
charlesmn | 0:137ce045e2a2 | 104 | Profile.EnableAmbient = 0; /* Enable: 1, Disable: 0 */ |
charlesmn | 0:137ce045e2a2 | 105 | Profile.EnableSignal = 0; /* Enable: 1, Disable: 0 */ |
charlesmn | 0:137ce045e2a2 | 106 | Profile.pin_gpio1 = InterruptPins[ToF_sensor]; // interrupt pin |
charlesmn | 0:137ce045e2a2 | 107 | Profile.Interrupt_Func = sensor_irq; // function that handles interrupts |
charlesmn | 0:137ce045e2a2 | 108 | Profile.EnableInterrupt = 1; // enables interupts |
charlesmn | 0:137ce045e2a2 | 109 | |
charlesmn | 0:137ce045e2a2 | 110 | printf("CUSTOM_RANGING_ConfigProfile configure sensors\n"); |
charlesmn | 0:137ce045e2a2 | 111 | sensor->CUSTOM_RANGING_ConfigProfile(ToF_sensor, &Profile); |
charlesmn | 0:137ce045e2a2 | 112 | if (status != BSP_ERROR_NONE) |
charlesmn | 0:137ce045e2a2 | 113 | { |
charlesmn | 0:137ce045e2a2 | 114 | printf("CUSTOM_RANGING_ConfigProfile failed sensor %d status %d\n",ToF_sensor,status); |
charlesmn | 0:137ce045e2a2 | 115 | } |
charlesmn | 0:137ce045e2a2 | 116 | wait_ms(100); |
charlesmn | 0:137ce045e2a2 | 117 | |
charlesmn | 0:137ce045e2a2 | 118 | |
charlesmn | 0:137ce045e2a2 | 119 | printf("CUSTOM_RANGING_Start %d\n",ToF_sensor); |
charlesmn | 0:137ce045e2a2 | 120 | status = sensor->CUSTOM_RANGING_Start(ToF_sensor, RS_MODE_BLOCKING_CONTINUOUS); |
charlesmn | 0:137ce045e2a2 | 121 | if (status != BSP_ERROR_NONE) |
charlesmn | 0:137ce045e2a2 | 122 | { |
charlesmn | 0:137ce045e2a2 | 123 | printf("CUSTOM_RANGING_Start failed sensor %d status %d\n",ToF_sensor,status); |
charlesmn | 0:137ce045e2a2 | 124 | } |
charlesmn | 0:137ce045e2a2 | 125 | |
charlesmn | 0:137ce045e2a2 | 126 | EventDetected = 1; // clear any existing interrupts |
charlesmn | 1:3151ff0b9da9 | 127 | // repeatedly read data and start next measurement |
charlesmn | 0:137ce045e2a2 | 128 | while (1) |
charlesmn | 0:137ce045e2a2 | 129 | { |
charlesmn | 0:137ce045e2a2 | 130 | if ( EventDetected == 1 ) // irq detected |
charlesmn | 0:137ce045e2a2 | 131 | { |
charlesmn | 0:137ce045e2a2 | 132 | EventDetected = 0; |
charlesmn | 0:137ce045e2a2 | 133 | status = sensor->CUSTOM_RANGING_GetDistance(ToF_sensor, &Result); |
charlesmn | 1:3151ff0b9da9 | 134 | if ((status == BSP_ERROR_NONE) && |
charlesmn | 2:2489dfbd49d7 | 135 | ( Result.ZoneResult[zone].NumberOfTargets != 0)) // is there data |
charlesmn | 2:2489dfbd49d7 | 136 | { |
charlesmn | 2:2489dfbd49d7 | 137 | for ( target = 0 ; target < Result.ZoneResult[zone].NumberOfTargets; target++) |
charlesmn | 2:2489dfbd49d7 | 138 | { |
charlesmn | 2:2489dfbd49d7 | 139 | if (Result.ZoneResult[zone].Status[target] == VL53LX_RANGESTATUS_RANGE_VALID ) |
charlesmn | 2:2489dfbd49d7 | 140 | { |
charlesmn | 2:2489dfbd49d7 | 141 | printf("\n |---> "); |
charlesmn | 2:2489dfbd49d7 | 142 | printf("Status = %d, Target %d, Distance = %5d mm", |
charlesmn | 2:2489dfbd49d7 | 143 | Result.ZoneResult[zone].Status[target], |
charlesmn | 2:2489dfbd49d7 | 144 | target, |
charlesmn | 2:2489dfbd49d7 | 145 | Result.ZoneResult[zone].Distance[target]); |
charlesmn | 2:2489dfbd49d7 | 146 | } |
charlesmn | 2:2489dfbd49d7 | 147 | } |
charlesmn | 2:2489dfbd49d7 | 148 | } |
charlesmn | 0:137ce045e2a2 | 149 | } |
charlesmn | 0:137ce045e2a2 | 150 | |
charlesmn | 0:137ce045e2a2 | 151 | } |
charlesmn | 0:137ce045e2a2 | 152 | } |
charlesmn | 0:137ce045e2a2 | 153 | |
charlesmn | 0:137ce045e2a2 | 154 | |
charlesmn | 0:137ce045e2a2 | 155 | |
charlesmn | 0:137ce045e2a2 | 156 | static void print_result(RANGING_SENSOR_Result_t *Result) |
charlesmn | 0:137ce045e2a2 | 157 | { |
charlesmn | 0:137ce045e2a2 | 158 | uint8_t j = 0; |
charlesmn | 1:3151ff0b9da9 | 159 | uint8_t zone = 0; |
charlesmn | 0:137ce045e2a2 | 160 | |
charlesmn | 0:137ce045e2a2 | 161 | printf(" |---> "); |
charlesmn | 1:3151ff0b9da9 | 162 | printf("Status = %d, Distance = %5d mm \n", |
charlesmn | 1:3151ff0b9da9 | 163 | Result->ZoneResult[zone].Status[j], |
charlesmn | 1:3151ff0b9da9 | 164 | Result->ZoneResult[zone].Distance[j]); |
charlesmn | 0:137ce045e2a2 | 165 | |
charlesmn | 0:137ce045e2a2 | 166 | } |
charlesmn | 0:137ce045e2a2 | 167 | |
charlesmn | 0:137ce045e2a2 | 168 | |
charlesmn | 0:137ce045e2a2 | 169 | |
charlesmn | 0:137ce045e2a2 | 170 | /*=================================== Main ================================== |
charlesmn | 0:137ce045e2a2 | 171 | =============================================================================*/ |
charlesmn | 0:137ce045e2a2 | 172 | int main() |
charlesmn | 0:137ce045e2a2 | 173 | { |
charlesmn | 0:137ce045e2a2 | 174 | |
charlesmn | 0:137ce045e2a2 | 175 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
charlesmn | 0:137ce045e2a2 | 176 | |
charlesmn | 0:137ce045e2a2 | 177 | sensor = new CUSTOM_SENSOR(); |
charlesmn | 0:137ce045e2a2 | 178 | |
charlesmn | 0:137ce045e2a2 | 179 | printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n"); |
charlesmn | 1:3151ff0b9da9 | 180 | printf("VL53L3CX_NoShield_1Sensors_interrupt\r\n"); |
charlesmn | 0:137ce045e2a2 | 181 | |
charlesmn | 0:137ce045e2a2 | 182 | MX_53L3A2_MultiSensorRanging_Init(); |
charlesmn | 0:137ce045e2a2 | 183 | |
charlesmn | 0:137ce045e2a2 | 184 | while (1) |
charlesmn | 0:137ce045e2a2 | 185 | { |
charlesmn | 0:137ce045e2a2 | 186 | MX_53L3A2_MultiSensorRanging_Process(); |
charlesmn | 0:137ce045e2a2 | 187 | } |
charlesmn | 0:137ce045e2a2 | 188 | |
charlesmn | 0:137ce045e2a2 | 189 | } |
charlesmn | 0:137ce045e2a2 | 190 | |
charlesmn | 0:137ce045e2a2 | 191 | #if (MBED_VERSION > 60300) |
charlesmn | 0:137ce045e2a2 | 192 | extern "C" void wait_ms(int ms) |
charlesmn | 0:137ce045e2a2 | 193 | { |
charlesmn | 0:137ce045e2a2 | 194 | thread_sleep_for(ms); |
charlesmn | 0:137ce045e2a2 | 195 | } |
charlesmn | 0:137ce045e2a2 | 196 | #endif |
charlesmn | 0:137ce045e2a2 | 197 |