Sweep a servo according to Proximity sensor measure

Dependencies:   Servo X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

Committer:
mapellil
Date:
Tue Nov 24 16:06:03 2015 +0000
Revision:
35:8b4a5cc0fb1f
Parent:
33:b903a0ec8803
Child:
36:0d4aa5b3234d
Removed polling measure, passed GPIOs to board constructor, added comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gallonm 0:83c628a58feb 1 #include "mbed.h"
gallonm 8:4c05f7a5bb60 2 #include "x_nucleo_6180xa1.h"
gallonm 4:ccd62fd7e137 3 #include <string.h>
gallonm 4:ccd62fd7e137 4 #include <stdlib.h>
gallonm 4:ccd62fd7e137 5 #include <stdio.h>
gallonm 8:4c05f7a5bb60 6 #include <assert.h>
gallonm 8:4c05f7a5bb60 7
mapellil 30:c8efec21544f 8 /* This VL6180X Expansion board test application performs a range measurement and an als measurement in interrupt mode
mapellil 30:c8efec21544f 9 on the onboard embedded top sensor.
mapellil 30:c8efec21544f 10 The board red slider select on the flight the measurement type as ALS or RANGE; the measured data is diplayed on the
mapellil 30:c8efec21544f 11 on bord 4digits display.
mapellil 30:c8efec21544f 12
mapellil 30:c8efec21544f 13 User Blue button allows to stop current measurement and the entire program releasing all the resources.
gallonm 20:b2e0b41a0e6b 14 Reset button is used to restart the program. */
gallonm 20:b2e0b41a0e6b 15
gallonm 14:946e62f44f4f 16 /* Polling operating modes don`t require callback function that handles IRQ
mapellil 35:8b4a5cc0fb1f 17 Callback IRQ functions are used only for measure that require interrupt */
gallonm 14:946e62f44f4f 18
gallonm 20:b2e0b41a0e6b 19 /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value
gallonm 20:b2e0b41a0e6b 20 is not ready to be read from the corresponding register. So you need to wait
gallonm 20:b2e0b41a0e6b 21 for the result to be ready */
gallonm 20:b2e0b41a0e6b 22
gallonm 5:fa65d931bd96 23 #define VL6180X_I2C_SDA I2C_SDA
gallonm 5:fa65d931bd96 24 #define VL6180X_I2C_SCL I2C_SCL
gallonm 5:fa65d931bd96 25
gallonm 19:394c9a4a3046 26 #define RANGE 0
gallonm 19:394c9a4a3046 27 #define ALS 1
gallonm 10:5319abadb31e 28
gallonm 14:946e62f44f4f 29 /* timer and digital out pin used for debugging */
gallonm 14:946e62f44f4f 30 //Timer timer;
gallonm 14:946e62f44f4f 31 //int start, end;
gallonm 14:946e62f44f4f 32 //DigitalOut pin(PA_15);
gallonm 5:fa65d931bd96 33
gallonm 27:2afb9baf718f 34 static X_NUCLEO_6180XA1 *board=NULL;
gallonm 19:394c9a4a3046 35 MeasureData_t data_sensor_top;
gallonm 27:2afb9baf718f 36 OperatingMode operating_mode, prev_operating_mode;
gallonm 11:657dbe7bf245 37
gallonm 15:b94bc967fecd 38 /* flags that handle interrupt request */
mapellil 30:c8efec21544f 39 bool int_sensor_top=false, int_stop_measure=false;
gallonm 15:b94bc967fecd 40
mapellil 30:c8efec21544f 41 /* ISR callback function of the sensor_top */
gallonm 15:b94bc967fecd 42 void SensorTopIRQ(void)
gallonm 9:1d0e839edee8 43 {
mapellil 30:c8efec21544f 44 int_sensor_top=true;
gallonm 14:946e62f44f4f 45 board->sensor_top->DisableInterruptMeasureDetectionIRQ();
gallonm 9:1d0e839edee8 46 }
gallonm 9:1d0e839edee8 47
mapellil 30:c8efec21544f 48 /* ISR callback function of the user blue button to stop program */
gallonm 15:b94bc967fecd 49 void StopMeasureIRQ(void)
gallonm 15:b94bc967fecd 50 {
mapellil 30:c8efec21544f 51 int_stop_measure=true;
gallonm 15:b94bc967fecd 52 }
gallonm 14:946e62f44f4f 53
mapellil 30:c8efec21544f 54 /* On board 4 digit local display refresh */
gallonm 19:394c9a4a3046 55 void DisplayRefresh(OperatingMode op_mode)
gallonm 27:2afb9baf718f 56 {
gallonm 25:2c4710283e02 57 char str[5];
gallonm 19:394c9a4a3046 58
licio.mapelli@st.com 32:724d2afb0ca2 59 if(op_mode==range_continuous_interrupt || op_mode==range_continuous_polling)
gallonm 19:394c9a4a3046 60 {
gallonm 19:394c9a4a3046 61 if(data_sensor_top.range_mm!=0xFFFFFFFF)
gallonm 19:394c9a4a3046 62 {
gallonm 19:394c9a4a3046 63 sprintf(str,"%d",data_sensor_top.range_mm);
gallonm 19:394c9a4a3046 64 }
gallonm 24:018a59122fe3 65 else
gallonm 22:bb440c864489 66 {
gallonm 25:2c4710283e02 67 sprintf(str,"%s","----");
gallonm 24:018a59122fe3 68 }
gallonm 19:394c9a4a3046 69 }
licio.mapelli@st.com 32:724d2afb0ca2 70 else if(op_mode==als_continuous_interrupt || op_mode==als_continuous_polling)
gallonm 19:394c9a4a3046 71 {
gallonm 19:394c9a4a3046 72 if(data_sensor_top.lux!=0xFFFFFFFF)
gallonm 19:394c9a4a3046 73 {
gallonm 19:394c9a4a3046 74 sprintf(str,"%d",data_sensor_top.lux);
gallonm 19:394c9a4a3046 75 }
gallonm 24:018a59122fe3 76 else
gallonm 22:bb440c864489 77 {
gallonm 25:2c4710283e02 78 sprintf(str,"%s","----");
gallonm 24:018a59122fe3 79 }
mapellil 30:c8efec21544f 80 }
mapellil 30:c8efec21544f 81 board->display->DisplayString(str, strlen(str));
gallonm 19:394c9a4a3046 82 }
gallonm 19:394c9a4a3046 83
mapellil 30:c8efec21544f 84 /* On board red slider position check */
licio.mapelli@st.com 32:724d2afb0ca2 85 enum OpModeIntPoll_t{ PollMeasure, IntMeasure };
licio.mapelli@st.com 32:724d2afb0ca2 86
licio.mapelli@st.com 33:b903a0ec8803 87 OperatingMode CheckSlider(enum OpModeIntPoll_t OpMode) {
licio.mapelli@st.com 33:b903a0ec8803 88
licio.mapelli@st.com 33:b903a0ec8803 89 OperatingMode ret;
licio.mapelli@st.com 33:b903a0ec8803 90 int measure= board->RdSwitch();
licio.mapelli@st.com 32:724d2afb0ca2 91
licio.mapelli@st.com 32:724d2afb0ca2 92 switch (OpMode) {
licio.mapelli@st.com 32:724d2afb0ca2 93 case PollMeasure:
licio.mapelli@st.com 32:724d2afb0ca2 94 if(measure==RANGE)
licio.mapelli@st.com 32:724d2afb0ca2 95 ret = range_continuous_polling;
licio.mapelli@st.com 32:724d2afb0ca2 96 else if(measure==ALS)
licio.mapelli@st.com 32:724d2afb0ca2 97 ret = als_continuous_polling;
licio.mapelli@st.com 32:724d2afb0ca2 98 break;
licio.mapelli@st.com 32:724d2afb0ca2 99
licio.mapelli@st.com 32:724d2afb0ca2 100 case IntMeasure:
licio.mapelli@st.com 32:724d2afb0ca2 101 if(measure==RANGE)
licio.mapelli@st.com 32:724d2afb0ca2 102 ret = range_continuous_interrupt;
licio.mapelli@st.com 32:724d2afb0ca2 103 else if(measure==ALS)
licio.mapelli@st.com 32:724d2afb0ca2 104 ret = als_continuous_interrupt;
licio.mapelli@st.com 32:724d2afb0ca2 105 break;
licio.mapelli@st.com 32:724d2afb0ca2 106 }
licio.mapelli@st.com 33:b903a0ec8803 107 return ret;
gallonm 27:2afb9baf718f 108 }
gallonm 27:2afb9baf718f 109
mapellil 30:c8efec21544f 110 /* Print on USB Serial the started OperatingMode */
mapellil 30:c8efec21544f 111 void PrintStartMessage(OperatingMode op_mode)
gallonm 27:2afb9baf718f 112 {
gallonm 27:2afb9baf718f 113 if(op_mode==range_continuous_interrupt)
gallonm 27:2afb9baf718f 114 printf("\nStarted range continuous interrupt measure\n\r");
gallonm 27:2afb9baf718f 115 else if(prev_operating_mode==als_continuous_interrupt)
gallonm 27:2afb9baf718f 116 printf("\nStarted als continuous interrupt measure\n\r");
gallonm 27:2afb9baf718f 117 }
gallonm 27:2afb9baf718f 118
mapellil 30:c8efec21544f 119 /* Print on USB Serial the stopped OperatingMode */
mapellil 30:c8efec21544f 120 void PrintStopMessage(OperatingMode op_mode)
gallonm 27:2afb9baf718f 121 {
gallonm 27:2afb9baf718f 122 if(op_mode==range_continuous_interrupt)
gallonm 27:2afb9baf718f 123 printf("Stopped range continuous interrupt measure\n\r");
gallonm 27:2afb9baf718f 124 else if(prev_operating_mode==als_continuous_interrupt)
gallonm 27:2afb9baf718f 125 printf("Stopped als continuous interrupt measure\n\r");
gallonm 27:2afb9baf718f 126 }
gallonm 27:2afb9baf718f 127
mapellil 35:8b4a5cc0fb1f 128 /* Print on board 4 Digit display the indicated message <= 4 char */
mapellil 31:1aaf6b8b066e 129 #define DELAY 2000 // 2Sec
licio.mapelli@st.com 32:724d2afb0ca2 130 void DisplayMsg(const char * msg)
gallonm 27:2afb9baf718f 131 {
mapellil 30:c8efec21544f 132 Timer timer;
gallonm 27:2afb9baf718f 133 char str[5];
gallonm 27:2afb9baf718f 134
mapellil 30:c8efec21544f 135 timer.start();
mapellil 30:c8efec21544f 136 for(int i=0; i<DELAY; i=timer.read_ms())
gallonm 27:2afb9baf718f 137 {
licio.mapelli@st.com 32:724d2afb0ca2 138 sprintf(str,"%s",msg);
gallonm 27:2afb9baf718f 139 board->display->DisplayString(str, strlen(str));
gallonm 27:2afb9baf718f 140 }
mapellil 30:c8efec21544f 141 timer.stop();
gallonm 27:2afb9baf718f 142 }
gallonm 27:2afb9baf718f 143
licio.mapelli@st.com 32:724d2afb0ca2 144
licio.mapelli@st.com 32:724d2afb0ca2 145 void IntContinousALSorRangeMeasure (DevI2C device_i2c) {
mapellil 35:8b4a5cc0fb1f 146 int status;
mapellil 35:8b4a5cc0fb1f 147 /* creates the 6180XA1 expansion board singleton obj */
mapellil 35:8b4a5cc0fb1f 148 board=X_NUCLEO_6180XA1::Instance(&device_i2c, PB_0, PA_4, PA_5, PA_10);
licio.mapelli@st.com 32:724d2afb0ca2 149 DisplayMsg ("INT");
mapellil 35:8b4a5cc0fb1f 150 /* init the 6180XA1 expansion board with default values */
gallonm 10:5319abadb31e 151 status=board->InitBoard();
gallonm 14:946e62f44f4f 152 if(status)
gallonm 27:2afb9baf718f 153 printf("Failed to init board!\n\r");
mapellil 35:8b4a5cc0fb1f 154 /* check the red slider position for ALS/Range measure */
licio.mapelli@st.com 33:b903a0ec8803 155 operating_mode=CheckSlider(IntMeasure);
mapellil 35:8b4a5cc0fb1f 156 /* start the measure on sensor top */
gallonm 20:b2e0b41a0e6b 157 status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL);
gallonm 14:946e62f44f4f 158 if(!status)
gallonm 14:946e62f44f4f 159 {
gallonm 27:2afb9baf718f 160 prev_operating_mode=operating_mode;
mapellil 30:c8efec21544f 161 PrintStartMessage(operating_mode);
gallonm 14:946e62f44f4f 162 while(1)
gallonm 14:946e62f44f4f 163 {
mapellil 35:8b4a5cc0fb1f 164 if(int_sensor_top) /* 6180 isr was triggered */
gallonm 14:946e62f44f4f 165 {
mapellil 30:c8efec21544f 166 int_sensor_top=false;
mapellil 35:8b4a5cc0fb1f 167 status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top); /* handle the isr and read the meaure */
gallonm 22:bb440c864489 168 DisplayRefresh(operating_mode);
gallonm 14:946e62f44f4f 169 }
mapellil 35:8b4a5cc0fb1f 170 if(int_stop_measure) /* Blue Button isr was triggered */
gallonm 19:394c9a4a3046 171 {
mapellil 35:8b4a5cc0fb1f 172 status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the measure and exit */
gallonm 27:2afb9baf718f 173 if(!status)
mapellil 30:c8efec21544f 174 PrintStopMessage(prev_operating_mode);
mapellil 35:8b4a5cc0fb1f 175 int_stop_measure = false;
gallonm 19:394c9a4a3046 176 printf("\nProgram stopped!\n\n\r");
gallonm 19:394c9a4a3046 177 break;
gallonm 19:394c9a4a3046 178 }
mapellil 35:8b4a5cc0fb1f 179 operating_mode=CheckSlider(IntMeasure); /* check if red slider was moved */
gallonm 19:394c9a4a3046 180 if(operating_mode!=prev_operating_mode)
gallonm 14:946e62f44f4f 181 {
gallonm 19:394c9a4a3046 182 DisplayRefresh(prev_operating_mode);
mapellil 35:8b4a5cc0fb1f 183 status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the running measure */
gallonm 27:2afb9baf718f 184 if(!status)
mapellil 30:c8efec21544f 185 PrintStopMessage(prev_operating_mode);
gallonm 19:394c9a4a3046 186 prev_operating_mode=operating_mode;
mapellil 35:8b4a5cc0fb1f 187 status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); /* start the new measure */
gallonm 27:2afb9baf718f 188 if(!status)
mapellil 30:c8efec21544f 189 PrintStartMessage(operating_mode);
licio.mapelli@st.com 32:724d2afb0ca2 190 } else
licio.mapelli@st.com 32:724d2afb0ca2 191 DisplayRefresh(operating_mode);
licio.mapelli@st.com 32:724d2afb0ca2 192 }
licio.mapelli@st.com 32:724d2afb0ca2 193 }
mapellil 35:8b4a5cc0fb1f 194 DisplayMsg("BYE");
mapellil 35:8b4a5cc0fb1f 195 delete board;
licio.mapelli@st.com 32:724d2afb0ca2 196 }
licio.mapelli@st.com 32:724d2afb0ca2 197
mapellil 35:8b4a5cc0fb1f 198 /*=================================== Main ==================================
mapellil 35:8b4a5cc0fb1f 199 Move the VL6180X Expansion board red slider to switch between ALS or Range
mapellil 35:8b4a5cc0fb1f 200 measures.
mapellil 35:8b4a5cc0fb1f 201 Press the blue user button to stop the measurements in progress
mapellil 35:8b4a5cc0fb1f 202 =============================================================================*/
mapellil 35:8b4a5cc0fb1f 203 int main()
mapellil 35:8b4a5cc0fb1f 204 {
mapellil 35:8b4a5cc0fb1f 205 InterruptIn stop_button (USER_BUTTON);
mapellil 35:8b4a5cc0fb1f 206 stop_button.rise (&StopMeasureIRQ);
mapellil 35:8b4a5cc0fb1f 207 DevI2C device_i2c (VL6180X_I2C_SDA, VL6180X_I2C_SCL);
mapellil 35:8b4a5cc0fb1f 208
mapellil 35:8b4a5cc0fb1f 209 IntContinousALSorRangeMeasure (device_i2c); // start continous measures Interrupt based
gallonm 4:ccd62fd7e137 210 }
gallonm 5:fa65d931bd96 211