Sweep a servo according to Proximity sensor measure

Dependencies:   Servo X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

Committer:
gallonm
Date:
Wed Nov 04 08:22:02 2015 +0000
Revision:
26:e19e6cb2e6bb
Parent:
25:2c4710283e02
Child:
27:2afb9baf718f
Updated main.cpp

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
gallonm 20:b2e0b41a0e6b 8 /* This test application performs a range measurement and an als measurement.
gallonm 20:b2e0b41a0e6b 9 Display select slider allow to switch the measurement type; previuos
gallonm 20:b2e0b41a0e6b 10 measurement is stopped and a new measurement is started.
gallonm 20:b2e0b41a0e6b 11 User button allows to stop current measurement and the entire program.
gallonm 20:b2e0b41a0e6b 12 Reset button is used to restart the program. */
gallonm 20:b2e0b41a0e6b 13
gallonm 14:946e62f44f4f 14 /* Polling operating modes don`t require callback function that handles IRQ
gallonm 14:946e62f44f4f 15 Callback functions are used only for measure that require interrupt */
gallonm 14:946e62f44f4f 16
gallonm 20:b2e0b41a0e6b 17 /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value
gallonm 20:b2e0b41a0e6b 18 is not ready to be read from the corresponding register. So you need to wait
gallonm 20:b2e0b41a0e6b 19 for the result to be ready */
gallonm 20:b2e0b41a0e6b 20
gallonm 5:fa65d931bd96 21 #define VL6180X_I2C_SDA I2C_SDA
gallonm 5:fa65d931bd96 22 #define VL6180X_I2C_SCL I2C_SCL
gallonm 5:fa65d931bd96 23
gallonm 19:394c9a4a3046 24 #define RANGE 0
gallonm 19:394c9a4a3046 25 #define ALS 1
gallonm 10:5319abadb31e 26
gallonm 14:946e62f44f4f 27 /* timer and digital out pin used for debugging */
gallonm 14:946e62f44f4f 28 //Timer timer;
gallonm 14:946e62f44f4f 29 //int start, end;
gallonm 14:946e62f44f4f 30 //DigitalOut pin(PA_15);
gallonm 5:fa65d931bd96 31
gallonm 8:4c05f7a5bb60 32 DevI2C device_i2c(VL6180X_I2C_SDA, VL6180X_I2C_SCL);
gallonm 9:1d0e839edee8 33 static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&device_i2c);
gallonm 9:1d0e839edee8 34
gallonm 19:394c9a4a3046 35 MeasureData_t data_sensor_top;
gallonm 11:657dbe7bf245 36
gallonm 15:b94bc967fecd 37 /* flags that handle interrupt request */
gallonm 19:394c9a4a3046 38 bool flag_sensor_top=false, flag_stop_measure=false;
gallonm 15:b94bc967fecd 39
gallonm 19:394c9a4a3046 40 /* callback function of the sensor_top */
gallonm 15:b94bc967fecd 41 void SensorTopIRQ(void)
gallonm 9:1d0e839edee8 42 {
gallonm 11:657dbe7bf245 43 flag_sensor_top=true;
gallonm 14:946e62f44f4f 44 board->sensor_top->DisableInterruptMeasureDetectionIRQ();
gallonm 9:1d0e839edee8 45 }
gallonm 9:1d0e839edee8 46
gallonm 19:394c9a4a3046 47 /* callback function of the user button to stop program */
gallonm 15:b94bc967fecd 48 void StopMeasureIRQ(void)
gallonm 15:b94bc967fecd 49 {
gallonm 15:b94bc967fecd 50 flag_stop_measure=true;
gallonm 15:b94bc967fecd 51 }
gallonm 14:946e62f44f4f 52
gallonm 19:394c9a4a3046 53 void DisplayRefresh(OperatingMode op_mode)
gallonm 19:394c9a4a3046 54 {
gallonm 25:2c4710283e02 55 char str[5];
gallonm 19:394c9a4a3046 56
gallonm 19:394c9a4a3046 57 if(op_mode==range_continuous_interrupt)
gallonm 19:394c9a4a3046 58 {
gallonm 19:394c9a4a3046 59 if(data_sensor_top.range_mm!=0xFFFFFFFF)
gallonm 19:394c9a4a3046 60 {
gallonm 19:394c9a4a3046 61 sprintf(str,"%d",data_sensor_top.range_mm);
gallonm 19:394c9a4a3046 62 board->display->DisplayString(str, strlen(str));
gallonm 19:394c9a4a3046 63 }
gallonm 24:018a59122fe3 64 else
gallonm 22:bb440c864489 65 {
gallonm 25:2c4710283e02 66 sprintf(str,"%s","----");
gallonm 22:bb440c864489 67 board->display->DisplayString(str, strlen(str));
gallonm 24:018a59122fe3 68 }
gallonm 19:394c9a4a3046 69 }
gallonm 19:394c9a4a3046 70 else if(op_mode==als_continuous_interrupt)
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 board->display->DisplayString(str, strlen(str));
gallonm 19:394c9a4a3046 76 }
gallonm 24:018a59122fe3 77 else
gallonm 22:bb440c864489 78 {
gallonm 25:2c4710283e02 79 sprintf(str,"%s","----");
gallonm 22:bb440c864489 80 board->display->DisplayString(str, strlen(str));
gallonm 24:018a59122fe3 81 }
gallonm 19:394c9a4a3046 82 }
gallonm 19:394c9a4a3046 83 }
gallonm 19:394c9a4a3046 84
gallonm 4:ccd62fd7e137 85 int main()
gallonm 5:fa65d931bd96 86 {
gallonm 19:394c9a4a3046 87 OperatingMode operating_mode, prev_operating_mode;
gallonm 19:394c9a4a3046 88 /* user button used to stop measurements in interrupt mode */
gallonm 19:394c9a4a3046 89 InterruptIn stop_button(USER_BUTTON);
gallonm 19:394c9a4a3046 90 STMPE1600DigiIn measure_type(device_i2c, GPIO_11);
gallonm 26:e19e6cb2e6bb 91 int status, measure;
gallonm 25:2c4710283e02 92 char str[5];
gallonm 19:394c9a4a3046 93
gallonm 24:018a59122fe3 94 //device_i2c.frequency(400000); //change i2c frequncy from 100kHz to 400kHz
gallonm 19:394c9a4a3046 95 pc.baud(115200); //change the printf baudrate
gallonm 14:946e62f44f4f 96
gallonm 10:5319abadb31e 97 status=board->InitBoard();
gallonm 14:946e62f44f4f 98 if(status)
gallonm 20:b2e0b41a0e6b 99 printf("Failed to init the board!\n\r");
gallonm 19:394c9a4a3046 100
gallonm 15:b94bc967fecd 101 stop_button.rise(&StopMeasureIRQ);
gallonm 19:394c9a4a3046 102 measure=measure_type;
gallonm 19:394c9a4a3046 103 if(measure==RANGE)
gallonm 19:394c9a4a3046 104 operating_mode=range_continuous_interrupt;
gallonm 19:394c9a4a3046 105 else if(measure==ALS)
gallonm 19:394c9a4a3046 106 operating_mode=als_continuous_interrupt;
gallonm 20:b2e0b41a0e6b 107 status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL);
gallonm 19:394c9a4a3046 108 prev_operating_mode=operating_mode;
gallonm 14:946e62f44f4f 109 if(!status)
gallonm 14:946e62f44f4f 110 {
gallonm 19:394c9a4a3046 111 printf("\nStart measurement\n\r");
gallonm 14:946e62f44f4f 112 while(1)
gallonm 14:946e62f44f4f 113 {
gallonm 14:946e62f44f4f 114 if(flag_sensor_top)
gallonm 14:946e62f44f4f 115 {
gallonm 14:946e62f44f4f 116 flag_sensor_top=false;
gallonm 19:394c9a4a3046 117 status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top);
gallonm 22:bb440c864489 118 DisplayRefresh(operating_mode);
gallonm 14:946e62f44f4f 119 }
gallonm 15:b94bc967fecd 120 if(flag_stop_measure)
gallonm 19:394c9a4a3046 121 {
gallonm 19:394c9a4a3046 122 status=board->sensor_top->StopMeasurement(prev_operating_mode);
gallonm 19:394c9a4a3046 123 if(status)
gallonm 19:394c9a4a3046 124 printf("Failed to stop measurement!\n\r");
gallonm 19:394c9a4a3046 125 else
gallonm 19:394c9a4a3046 126 printf("Measurement stopped!\n\r");
gallonm 19:394c9a4a3046 127 printf("\nProgram stopped!\n\n\r");
gallonm 19:394c9a4a3046 128 break;
gallonm 19:394c9a4a3046 129 }
gallonm 19:394c9a4a3046 130 measure=measure_type;
gallonm 19:394c9a4a3046 131 if(measure==RANGE)
gallonm 19:394c9a4a3046 132 operating_mode=range_continuous_interrupt;
gallonm 19:394c9a4a3046 133 else if(measure==ALS)
gallonm 19:394c9a4a3046 134 operating_mode=als_continuous_interrupt;
gallonm 19:394c9a4a3046 135 if(operating_mode!=prev_operating_mode)
gallonm 14:946e62f44f4f 136 {
gallonm 19:394c9a4a3046 137 DisplayRefresh(prev_operating_mode);
gallonm 19:394c9a4a3046 138 status=board->sensor_top->StopMeasurement(prev_operating_mode);
gallonm 19:394c9a4a3046 139 prev_operating_mode=operating_mode;
gallonm 19:394c9a4a3046 140 if(status)
gallonm 19:394c9a4a3046 141 printf("Failed to stop measurement!\n\r");
gallonm 19:394c9a4a3046 142 else
gallonm 19:394c9a4a3046 143 printf("Measurement stopped!\n\r");
gallonm 20:b2e0b41a0e6b 144 status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL);
gallonm 19:394c9a4a3046 145 if(status)
gallonm 19:394c9a4a3046 146 {
gallonm 19:394c9a4a3046 147 printf("\nFailed to start measurement!\n\r");
gallonm 19:394c9a4a3046 148 break;
gallonm 19:394c9a4a3046 149 }
gallonm 19:394c9a4a3046 150 else
gallonm 19:394c9a4a3046 151 printf("\nStart measurement\n\r");
gallonm 19:394c9a4a3046 152 }
gallonm 19:394c9a4a3046 153 else
gallonm 19:394c9a4a3046 154 DisplayRefresh(operating_mode);
gallonm 14:946e62f44f4f 155 }
gallonm 14:946e62f44f4f 156 }
gallonm 14:946e62f44f4f 157 else
gallonm 19:394c9a4a3046 158 printf("\nFailed to start measurement!\n\r");
gallonm 20:b2e0b41a0e6b 159
gallonm 20:b2e0b41a0e6b 160 for(status=0;status<200;status++)
gallonm 20:b2e0b41a0e6b 161 {
gallonm 20:b2e0b41a0e6b 162 sprintf(str,"%s","CIAO");
gallonm 20:b2e0b41a0e6b 163 board->display->DisplayString(str, strlen(str));
gallonm 20:b2e0b41a0e6b 164 }
gallonm 10:5319abadb31e 165
gallonm 20:b2e0b41a0e6b 166 delete board;
gallonm 4:ccd62fd7e137 167 }
gallonm 5:fa65d931bd96 168