Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library
main.cpp@8:c0a6a3363e43, 2020-04-22 (annotated)
- Committer:
- mmarine3
- Date:
- Wed Apr 22 18:37:33 2020 +0000
- Revision:
- 8:c0a6a3363e43
- Parent:
- 7:85d42006e380
- Child:
- 9:f4f03767acc0
Added code to read TOF sensor and display it to the LCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mmarine3 | 2:1ec843c95914 | 1 | #include "mbed.h" |
rmalik8 | 4:7d02b3f3bee6 | 2 | #include "rtos.h" |
mmarine3 | 2:1ec843c95914 | 3 | #include "SDFileSystem.h" |
mmarine3 | 2:1ec843c95914 | 4 | #include "uLCD_4DGL.h" |
rmalik8 | 3:b5cdd40e99e9 | 5 | #include "XNucleo53L0A1.h" |
rmalik8 | 4:7d02b3f3bee6 | 6 | #include "ultrasonic.h" |
mmarine3 | 8:c0a6a3363e43 | 7 | #include "wave_player.h" |
rmalik8 | 4:7d02b3f3bee6 | 8 | |
rmalik8 | 3:b5cdd40e99e9 | 9 | //#include <stdio.h> |
mmarine3 | 2:1ec843c95914 | 10 | #include <string> |
mmarine3 | 2:1ec843c95914 | 11 | |
rmalik8 | 4:7d02b3f3bee6 | 12 | //I2C sensor pins |
rmalik8 | 4:7d02b3f3bee6 | 13 | #define VL53L0_I2C_SDA p28 |
rmalik8 | 4:7d02b3f3bee6 | 14 | #define VL53L0_I2C_SCL p27 |
rmalik8 | 4:7d02b3f3bee6 | 15 | |
rmalik8 | 3:b5cdd40e99e9 | 16 | uLCD_4DGL uLCD(p13, p14, p12); // serial tx, serial rx, reset pin; |
rmalik8 | 4:7d02b3f3bee6 | 17 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
rmalik8 | 3:b5cdd40e99e9 | 18 | DigitalOut shdn(p26); |
rmalik8 | 4:7d02b3f3bee6 | 19 | RawSerial BT(p9,p10); |
rmalik8 | 4:7d02b3f3bee6 | 20 | //Serial BT(p9,p10); |
mmarine3 | 7:85d42006e380 | 21 | AnalogOut speaker(p18); //NEW |
mmarine3 | 7:85d42006e380 | 22 | wave_player waver(&speaker);//NEW |
rmalik8 | 6:068146497fe6 | 23 | Mutex mutex; |
mmarine3 | 8:c0a6a3363e43 | 24 | Mutex dist; |
mmarine3 | 2:1ec843c95914 | 25 | DigitalOut MyLED(LED1); |
mmarine3 | 2:1ec843c95914 | 26 | |
rmalik8 | 4:7d02b3f3bee6 | 27 | //needed for TOF |
rmalik8 | 4:7d02b3f3bee6 | 28 | static XNucleo53L0A1 *board=NULL; |
mmarine3 | 2:1ec843c95914 | 29 | |
mmarine3 | 8:c0a6a3363e43 | 30 | /*void sonar(int distance) |
rmalik8 | 4:7d02b3f3bee6 | 31 | { |
rmalik8 | 4:7d02b3f3bee6 | 32 | //put code here to execute when the sonar distance has changed |
rmalik8 | 4:7d02b3f3bee6 | 33 | |
mmarine3 | 8:c0a6a3363e43 | 34 | }*/ |
rmalik8 | 4:7d02b3f3bee6 | 35 | |
mmarine3 | 8:c0a6a3363e43 | 36 | //ultrasonic mu(p30, p29, .1, 1, &sonar); //Set the trigger pin to p30 and the echo pin to p29 |
rmalik8 | 4:7d02b3f3bee6 | 37 | //have updates every .1 seconds and a timeout after 1 |
rmalik8 | 4:7d02b3f3bee6 | 38 | //second, and call dist when the distance changes |
mmarine3 | 8:c0a6a3363e43 | 39 | uint32_t far; //Global variable to hold lidar TOF distance |
rmalik8 | 4:7d02b3f3bee6 | 40 | void TOF(void const *arguments) { |
rmalik8 | 4:7d02b3f3bee6 | 41 | //initialize the TOF |
rmalik8 | 4:7d02b3f3bee6 | 42 | int status; |
rmalik8 | 4:7d02b3f3bee6 | 43 | uint32_t distance; |
rmalik8 | 4:7d02b3f3bee6 | 44 | DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL); |
rmalik8 | 4:7d02b3f3bee6 | 45 | /* creates the 53L0A1 expansion board singleton obj */ |
rmalik8 | 4:7d02b3f3bee6 | 46 | board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2); |
rmalik8 | 4:7d02b3f3bee6 | 47 | shdn = 0; //must reset sensor for an mbed reset to work |
rmalik8 | 4:7d02b3f3bee6 | 48 | wait(0.1); |
rmalik8 | 4:7d02b3f3bee6 | 49 | shdn = 1; |
rmalik8 | 4:7d02b3f3bee6 | 50 | wait(0.1); |
rmalik8 | 4:7d02b3f3bee6 | 51 | /* init the 53L0A1 board with default values */ |
rmalik8 | 4:7d02b3f3bee6 | 52 | status = board->init_board(); |
rmalik8 | 4:7d02b3f3bee6 | 53 | while (status) { |
rmalik8 | 4:7d02b3f3bee6 | 54 | //pc.printf("Failed to init board! \r\n"); |
rmalik8 | 4:7d02b3f3bee6 | 55 | status = board->init_board(); |
rmalik8 | 4:7d02b3f3bee6 | 56 | } |
rmalik8 | 4:7d02b3f3bee6 | 57 | |
mmarine3 | 8:c0a6a3363e43 | 58 | |
mmarine3 | 8:c0a6a3363e43 | 59 | |
rmalik8 | 4:7d02b3f3bee6 | 60 | |
mmarine3 | 8:c0a6a3363e43 | 61 | //Loop that reads in the TOF value 100 times per second |
mmarine3 | 8:c0a6a3363e43 | 62 | while(1){ |
mmarine3 | 8:c0a6a3363e43 | 63 | status = board->sensor_centre->get_distance(&distance); //How to get distance value |
mmarine3 | 8:c0a6a3363e43 | 64 | dist.lock(); |
mmarine3 | 8:c0a6a3363e43 | 65 | far = distance; |
mmarine3 | 8:c0a6a3363e43 | 66 | dist.unlock(); |
mmarine3 | 8:c0a6a3363e43 | 67 | Thread::wait(10); |
mmarine3 | 8:c0a6a3363e43 | 68 | } |
mmarine3 | 8:c0a6a3363e43 | 69 | /* |
rmalik8 | 5:3ff2acbd08ab | 70 | How you would print TOF to pc serial |
rmalik8 | 4:7d02b3f3bee6 | 71 | if (status == VL53L0X_ERROR_NONE) { |
rmalik8 | 4:7d02b3f3bee6 | 72 | pc.printf("D=%ld mm\r\n", distance); |
rmalik8 | 4:7d02b3f3bee6 | 73 | } |
rmalik8 | 4:7d02b3f3bee6 | 74 | |
rmalik8 | 4:7d02b3f3bee6 | 75 | */ |
rmalik8 | 4:7d02b3f3bee6 | 76 | } |
mmarine3 | 2:1ec843c95914 | 77 | |
mmarine3 | 8:c0a6a3363e43 | 78 | //Thread to print the TOF and Sonar Values to the LCD |
mmarine3 | 8:c0a6a3363e43 | 79 | void LCD(void const *arguments) |
mmarine3 | 8:c0a6a3363e43 | 80 | { |
mmarine3 | 8:c0a6a3363e43 | 81 | Thread::wait(1000); //Wait for lidar and sonar setup |
mmarine3 | 8:c0a6a3363e43 | 82 | while(1) |
mmarine3 | 8:c0a6a3363e43 | 83 | { |
mmarine3 | 8:c0a6a3363e43 | 84 | uLCD.cls(); |
mmarine3 | 8:c0a6a3363e43 | 85 | dist.lock(); |
mmarine3 | 8:c0a6a3363e43 | 86 | uLCD.printf("TOF Distance %i \n", far); |
mmarine3 | 8:c0a6a3363e43 | 87 | dist.unlock(); |
mmarine3 | 8:c0a6a3363e43 | 88 | Thread::wait(1000); //Allow time to read value before reprint |
mmarine3 | 8:c0a6a3363e43 | 89 | } |
mmarine3 | 8:c0a6a3363e43 | 90 | } |
mmarine3 | 8:c0a6a3363e43 | 91 | |
mmarine3 | 2:1ec843c95914 | 92 | int main() |
mmarine3 | 2:1ec843c95914 | 93 | { |
mmarine3 | 7:85d42006e380 | 94 | uLCD.cls(); |
mmarine3 | 7:85d42006e380 | 95 | uLCD.baudrate(BAUD_3000000); |
mmarine3 | 7:85d42006e380 | 96 | //wait(1.0); |
mmarine3 | 7:85d42006e380 | 97 | |
mmarine3 | 7:85d42006e380 | 98 | //blu.attach(&parse_message,Serial::RxIrq); |
mmarine3 | 8:c0a6a3363e43 | 99 | //Was used in lab 3 to interupt if reading in a blutooth command |
mmarine3 | 7:85d42006e380 | 100 | //Thread t#(name_of_thread_function); |
mmarine3 | 8:c0a6a3363e43 | 101 | Thread t1(LCD);//Initialize LCD thread |
mmarine3 | 8:c0a6a3363e43 | 102 | Thread t2(TOF);//Initialize TOF thread |
rmalik8 | 4:7d02b3f3bee6 | 103 | |
mmarine3 | 8:c0a6a3363e43 | 104 | /* //Code to read and play a file |
mmarine3 | 7:85d42006e380 | 105 | FILE *wave_file; |
mmarine3 | 7:85d42006e380 | 106 | //printf("Hello World"); |
mmarine3 | 7:85d42006e380 | 107 | Thread::wait(1000); |
mmarine3 | 7:85d42006e380 | 108 | wave_file=fopen("/sd/test.wav","r"); |
mmarine3 | 7:85d42006e380 | 109 | //serial_mutex.lock(); |
mmarine3 | 7:85d42006e380 | 110 | if(wave_file==NULL) printf("file open error!\n\n\r"); |
mmarine3 | 7:85d42006e380 | 111 | //serial_mutex.unlock(); |
mmarine3 | 7:85d42006e380 | 112 | waver.play(wave_file); |
mmarine3 | 7:85d42006e380 | 113 | fclose(wave_file); |
mmarine3 | 8:c0a6a3363e43 | 114 | */ |
mmarine3 | 8:c0a6a3363e43 | 115 | |
mmarine3 | 8:c0a6a3363e43 | 116 | //Loop to validate the main loop is executing |
mmarine3 | 8:c0a6a3363e43 | 117 | while(1) |
mmarine3 | 8:c0a6a3363e43 | 118 | { |
mmarine3 | 8:c0a6a3363e43 | 119 | MyLED = !MyLED; |
mmarine3 | 8:c0a6a3363e43 | 120 | Thread::wait(100); |
mmarine3 | 8:c0a6a3363e43 | 121 | } |
mmarine3 | 2:1ec843c95914 | 122 | } |