code for read VL53l0x sensor with CAN

Dependencies:   mbed

Committer:
pablo_bmxrp
Date:
Mon Mar 18 22:45:39 2019 +0000
Revision:
1:7bbfe329c62b
Parent:
STM32F103C8T6_MPA/main.cpp@0:44429c0a71d4
V1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pablo_bmxrp 0:44429c0a71d4 1 #include "stm32f103c8t6.h"
pablo_bmxrp 0:44429c0a71d4 2 #include "mbed.h"
pablo_bmxrp 0:44429c0a71d4 3 /*
pablo_bmxrp 0:44429c0a71d4 4 * Mbed Application program
pablo_bmxrp 0:44429c0a71d4 5 * Time-of-Flight ranging and gesture detection sensor / STMicro VL53L0X
pablo_bmxrp 0:44429c0a71d4 6 * http://www.st.com/ja/imaging-and-photonics-solutions/vl53l0x.html
pablo_bmxrp 0:44429c0a71d4 7 *
pablo_bmxrp 0:44429c0a71d4 8 * 1) AKIZUKI AE-VL53L0X
pablo_bmxrp 0:44429c0a71d4 9 * http://akizukidenshi.com/catalog/g/gM-12590/
pablo_bmxrp 0:44429c0a71d4 10 * 2) SWITCH SCIENCE Pololu VL53L0X (POLOLU-2490)
pablo_bmxrp 0:44429c0a71d4 11 * https://www.switch-science.com/catalog/2869/
pablo_bmxrp 0:44429c0a71d4 12 * 3) SWITCH SCIENCE VL53L0X (SSCI-028943)
pablo_bmxrp 0:44429c0a71d4 13 * https://www.switch-science.com/catalog/2894/
pablo_bmxrp 0:44429c0a71d4 14 * 4) Strawberry Linux
pablo_bmxrp 0:44429c0a71d4 15 * https://strawberry-linux.com/catalog/items?code=15310
pablo_bmxrp 0:44429c0a71d4 16 *
pablo_bmxrp 0:44429c0a71d4 17 * ---- Tested AE-VL53L0X BOARD and handmade circuit ----
pablo_bmxrp 0:44429c0a71d4 18 * Tested on below Mbed boards and works fine on mbed 2.0
pablo_bmxrp 0:44429c0a71d4 19 * Nucleo-F303K8
pablo_bmxrp 0:44429c0a71d4 20 * Nucleo-F334R8, -F401RE, -F411RE, -F446RE
pablo_bmxrp 0:44429c0a71d4 21 * Nucleo-L053R8, -L073RZ, -L152RE, -L476RG
pablo_bmxrp 0:44429c0a71d4 22 * FRDM-K64F
pablo_bmxrp 0:44429c0a71d4 23 * TY51822r3
pablo_bmxrp 0:44429c0a71d4 24 * Run also on mbed-os5 (Tested on Nucleo-F446RE)
pablo_bmxrp 0:44429c0a71d4 25 *
pablo_bmxrp 0:44429c0a71d4 26 * Copyright (c) 2018 Kenji Arai / JH1PJL
pablo_bmxrp 0:44429c0a71d4 27 * http://www.page.sannet.ne.jp/kenjia/index.html
pablo_bmxrp 0:44429c0a71d4 28 * http://mbed.org/users/kenjiArai/
pablo_bmxrp 0:44429c0a71d4 29 * Created: January 21st, 2018
pablo_bmxrp 0:44429c0a71d4 30 * Revised: Feburary 6th, 2018 (with updated VL53L0X_simple library)
pablo_bmxrp 0:44429c0a71d4 31 */
pablo_bmxrp 0:44429c0a71d4 32
pablo_bmxrp 0:44429c0a71d4 33 // Include --------------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 34 #include "VL53L0X.h"
pablo_bmxrp 0:44429c0a71d4 35
pablo_bmxrp 0:44429c0a71d4 36 // Definition -----------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 37 #define USE_XSHUT 1
pablo_bmxrp 0:44429c0a71d4 38
pablo_bmxrp 0:44429c0a71d4 39 // Constructor ----------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 40 DigitalOut myled(PA_5);
pablo_bmxrp 0:44429c0a71d4 41 DigitalOut CAN_STB(PA_4);
pablo_bmxrp 0:44429c0a71d4 42
pablo_bmxrp 0:44429c0a71d4 43 CAN can1(PA_11, PA_12);
pablo_bmxrp 0:44429c0a71d4 44
pablo_bmxrp 0:44429c0a71d4 45 Serial pc(USBTX, USBRX, 9600);
pablo_bmxrp 0:44429c0a71d4 46 //I2C i2c(P0_30, P0_7); // only for TY51822r3
pablo_bmxrp 0:44429c0a71d4 47 I2C i2c(PB_9, PB_8); //placa hensys
pablo_bmxrp 0:44429c0a71d4 48 //I2C i2c(PB_11, PB_10); //bluepill
pablo_bmxrp 0:44429c0a71d4 49
pablo_bmxrp 0:44429c0a71d4 50 #if USE_XSHUT
pablo_bmxrp 0:44429c0a71d4 51 VL53L0X sensor(i2c, PA_8); // XSHUT = D8
pablo_bmxrp 0:44429c0a71d4 52 #else
pablo_bmxrp 0:44429c0a71d4 53 VL53L0X sensor(i2c, NC); // XSHUT = NC
pablo_bmxrp 0:44429c0a71d4 54 #endif
pablo_bmxrp 0:44429c0a71d4 55 Timer t;
pablo_bmxrp 0:44429c0a71d4 56
pablo_bmxrp 0:44429c0a71d4 57 // RAM ------------------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 58
pablo_bmxrp 0:44429c0a71d4 59 // ROM / Constant data --------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 60 char *const msg0 = "VL53L0X is running correctly!!\r\n";
pablo_bmxrp 0:44429c0a71d4 61 char *const msg1 = "VL53L0X -> something is wrong!!\r\n";
pablo_bmxrp 0:44429c0a71d4 62 char *const msg2 = "#,";
pablo_bmxrp 0:44429c0a71d4 63 char *const msg3 = "d[mm]=,";
pablo_bmxrp 0:44429c0a71d4 64 char *const msg4 = "d[mm]=,error,";
pablo_bmxrp 0:44429c0a71d4 65 char *const msg5 = "VL53[mS]=, ";
pablo_bmxrp 0:44429c0a71d4 66 char *const msg6 = "all[mS]=, ";
pablo_bmxrp 0:44429c0a71d4 67
pablo_bmxrp 0:44429c0a71d4 68 // Function prototypes --------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 69
pablo_bmxrp 0:44429c0a71d4 70 //------------------------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 71 // Control Program
pablo_bmxrp 0:44429c0a71d4 72 //------------------------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 73 int main()
pablo_bmxrp 0:44429c0a71d4 74 {
pablo_bmxrp 0:44429c0a71d4 75 char bosta = 1;
pablo_bmxrp 0:44429c0a71d4 76 can1.frequency(500000);
pablo_bmxrp 0:44429c0a71d4 77 CAN_STB = 0;
pablo_bmxrp 0:44429c0a71d4 78 int status = VL53L0X_ERROR_NONE;
pablo_bmxrp 0:44429c0a71d4 79 uint32_t data;
pablo_bmxrp 0:44429c0a71d4 80 uint32_t count = 0;
pablo_bmxrp 0:44429c0a71d4 81 uint32_t tm_sensor;
pablo_bmxrp 0:44429c0a71d4 82 uint32_t tm_all_work;
pablo_bmxrp 0:44429c0a71d4 83
pablo_bmxrp 0:44429c0a71d4 84 #if USE_XSHUT
pablo_bmxrp 0:44429c0a71d4 85 status = sensor.init_sensor(0x53); // set new I2C address
pablo_bmxrp 0:44429c0a71d4 86 #else
pablo_bmxrp 0:44429c0a71d4 87 // no control XSHUT then set default address (no other way)
pablo_bmxrp 0:44429c0a71d4 88 status = sensor.init_sensor(VL53L0X_DEFAULT_ADDRESS);
pablo_bmxrp 0:44429c0a71d4 89 #endif
pablo_bmxrp 0:44429c0a71d4 90 if (status == VL53L0X_ERROR_NONE) {
pablo_bmxrp 0:44429c0a71d4 91 pc.printf(msg0);
pablo_bmxrp 0:44429c0a71d4 92 } else {
pablo_bmxrp 0:44429c0a71d4 93 pc.printf(msg1);
pablo_bmxrp 0:44429c0a71d4 94 }
pablo_bmxrp 0:44429c0a71d4 95 //status = sensor.set_mode(range_long_distance_33ms_200cm);
pablo_bmxrp 0:44429c0a71d4 96 //status = sensor.set_mode(range_hi_accurate_200ms_120cm);
pablo_bmxrp 0:44429c0a71d4 97 status = sensor.set_mode(range_hi_speed_20ms_120cm);
pablo_bmxrp 0:44429c0a71d4 98 if (status == VL53L0X_ERROR_NONE) {
pablo_bmxrp 0:44429c0a71d4 99 pc.printf(msg0);
pablo_bmxrp 0:44429c0a71d4 100 } else {
pablo_bmxrp 0:44429c0a71d4 101 pc.printf(msg1);
pablo_bmxrp 0:44429c0a71d4 102 }
pablo_bmxrp 0:44429c0a71d4 103 while(true) {
pablo_bmxrp 0:44429c0a71d4 104 //can1.write(CANMessage(1337,&bosta,1));
pablo_bmxrp 0:44429c0a71d4 105 //wait(0.2);
pablo_bmxrp 0:44429c0a71d4 106 t.reset();
pablo_bmxrp 0:44429c0a71d4 107 t.start();
pablo_bmxrp 0:44429c0a71d4 108 pc.printf("olar");
pablo_bmxrp 0:44429c0a71d4 109 //myled = !myled;
pablo_bmxrp 0:44429c0a71d4 110 status = sensor.get_distance(&data);
pablo_bmxrp 0:44429c0a71d4 111 tm_sensor = t.read_ms();
pablo_bmxrp 0:44429c0a71d4 112 if (status == VL53L0X_ERROR_NONE) {
pablo_bmxrp 0:44429c0a71d4 113 pc.printf("%s%5d,%s%5d,", msg2, count++, msg3, data);
pablo_bmxrp 0:44429c0a71d4 114 if(data >= 300){
pablo_bmxrp 0:44429c0a71d4 115 myled = 1;
pablo_bmxrp 0:44429c0a71d4 116 can1.write(CANMessage(1337,&bosta,1));
pablo_bmxrp 0:44429c0a71d4 117 wait(0.2);
pablo_bmxrp 0:44429c0a71d4 118 }
pablo_bmxrp 0:44429c0a71d4 119 else {
pablo_bmxrp 0:44429c0a71d4 120 myled = 0;}
pablo_bmxrp 0:44429c0a71d4 121 } else {
pablo_bmxrp 0:44429c0a71d4 122 pc.printf("%s%5d,%s", msg2, count++, msg4);
pablo_bmxrp 0:44429c0a71d4 123 }
pablo_bmxrp 0:44429c0a71d4 124 pc.printf("%s%d,%s%d\r\n", msg5, tm_sensor, msg6, tm_all_work);
pablo_bmxrp 0:44429c0a71d4 125 tm_all_work = t.read_ms();
pablo_bmxrp 0:44429c0a71d4 126 if (tm_all_work < 99){
pablo_bmxrp 0:44429c0a71d4 127 wait_ms(100 - tm_all_work);
pablo_bmxrp 0:44429c0a71d4 128 }
pablo_bmxrp 0:44429c0a71d4 129 }
pablo_bmxrp 0:44429c0a71d4 130 }