code for read VL53l0x sensor with CAN

Dependencies:   mbed

STM32F103C8T6_MPA/main.cpp

Committer:
pablo_bmxrp
Date:
2019-03-18
Revision:
0:44429c0a71d4

File content as of revision 0:44429c0a71d4:

#include "stm32f103c8t6.h"
#include "mbed.h"
/*
 * Mbed Application program
 *   Time-of-Flight ranging and gesture detection sensor / STMicro VL53L0X
 *      http://www.st.com/ja/imaging-and-photonics-solutions/vl53l0x.html
 *
 *    1) AKIZUKI AE-VL53L0X
 *      http://akizukidenshi.com/catalog/g/gM-12590/
 *    2) SWITCH SCIENCE  Pololu VL53L0X (POLOLU-2490)
 *      https://www.switch-science.com/catalog/2869/
 *    3) SWITCH SCIENCE  VL53L0X (SSCI-028943)
 *      https://www.switch-science.com/catalog/2894/
 *    4) Strawberry Linux
 *      https://strawberry-linux.com/catalog/items?code=15310
 *
 *    ---- Tested AE-VL53L0X BOARD and handmade circuit ----
 *      Tested on below Mbed boards and works fine on mbed 2.0
 *          Nucleo-F303K8
 *          Nucleo-F334R8, -F401RE, -F411RE, -F446RE
 *          Nucleo-L053R8, -L073RZ, -L152RE, -L476RG
 *          FRDM-K64F
 *          TY51822r3
 *      Run also on mbed-os5 (Tested on Nucleo-F446RE)
 *
 * Copyright (c) 2018 Kenji Arai / JH1PJL
 *  http://www.page.sannet.ne.jp/kenjia/index.html
 *  http://mbed.org/users/kenjiArai/
 *      Created:    January   21st, 2018
 *      Revised:    Feburary   6th, 2018 (with updated VL53L0X_simple library)
 */

//  Include --------------------------------------------------------------------
#include "VL53L0X.h"

//  Definition -----------------------------------------------------------------
#define USE_XSHUT   1

//  Constructor ----------------------------------------------------------------
DigitalOut  myled(PA_5);
DigitalOut CAN_STB(PA_4);

CAN can1(PA_11, PA_12);

Serial      pc(USBTX, USBRX, 9600);
//I2C         i2c(P0_30, P0_7);     // only for TY51822r3
I2C            i2c(PB_9, PB_8);    //placa hensys
//I2C            i2c(PB_11, PB_10);    //bluepill

#if USE_XSHUT
VL53L0X     sensor(i2c, PA_8);        // XSHUT = D8
#else
VL53L0X     sensor(i2c, NC);        // XSHUT = NC
#endif
Timer       t;

//  RAM ------------------------------------------------------------------------

//  ROM / Constant data --------------------------------------------------------
char *const msg0  = "VL53L0X is running correctly!!\r\n";
char *const msg1  = "VL53L0X -> something is wrong!!\r\n";
char *const msg2  = "#,";
char *const msg3  = "d[mm]=,";
char *const msg4  = "d[mm]=,error,";
char *const msg5  = "VL53[mS]=, ";
char *const msg6  = "all[mS]=, ";

//  Function prototypes --------------------------------------------------------

//------------------------------------------------------------------------------
//  Control Program
//------------------------------------------------------------------------------
int main()
{
    char bosta = 1;
    can1.frequency(500000);
    CAN_STB = 0;
    int status = VL53L0X_ERROR_NONE;
    uint32_t data;
    uint32_t count = 0;
    uint32_t tm_sensor;
    uint32_t tm_all_work;

#if USE_XSHUT
    status = sensor.init_sensor(0x53);  // set new I2C address
#else
    // no control XSHUT then set default address (no other way)
    status = sensor.init_sensor(VL53L0X_DEFAULT_ADDRESS);
#endif
    if (status == VL53L0X_ERROR_NONE) {
        pc.printf(msg0);
    } else {
        pc.printf(msg1);
    }
    //status = sensor.set_mode(range_long_distance_33ms_200cm);
    //status = sensor.set_mode(range_hi_accurate_200ms_120cm);
    status = sensor.set_mode(range_hi_speed_20ms_120cm);
    if (status == VL53L0X_ERROR_NONE) {
        pc.printf(msg0);
    } else {
        pc.printf(msg1);
    }
    while(true) {
        //can1.write(CANMessage(1337,&bosta,1));
        //wait(0.2);
        t.reset();
        t.start();
         pc.printf("olar");
        //myled = !myled;
        status = sensor.get_distance(&data);
        tm_sensor = t.read_ms();
        if (status == VL53L0X_ERROR_NONE) {
            pc.printf("%s%5d,%s%5d,", msg2, count++, msg3, data);
            if(data >= 300){
             myled = 1;
             can1.write(CANMessage(1337,&bosta,1));
             wait(0.2);
             }
            else {
             myled = 0;}
        } else {
            pc.printf("%s%5d,%s", msg2, count++, msg4);
        }
        pc.printf("%s%d,%s%d\r\n", msg5, tm_sensor, msg6, tm_all_work);
        tm_all_work = t.read_ms();
        if (tm_all_work < 99){
            wait_ms(100 - tm_all_work);
        }
    }
}