Georgia Tech - ECE 4180 Project - Smart Fan / Mbed 2 deprecated Smart Fan

Dependencies:   mbed Servo mbed-rtos X_NUCLEO_53L0A1

Committer:
BubbaLee
Date:
Sun Apr 26 20:38:19 2020 +0000
Revision:
14:63c2b3d14d06
Parent:
13:74d440e3000b
Final working code for the Smart Fan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnAlexander 0:ce8359133ae6 1 #include "mbed.h"
Davidroid 10:891e10d3b4a6 2 #include "XNucleo53L0A1.h"
spulavarty3 12:dd53c18e9cf2 3 #include "rtos.h"
spulavarty3 12:dd53c18e9cf2 4 #include "Servo.h"
johnAlexander 0:ce8359133ae6 5 #include <stdio.h>
spulavarty3 12:dd53c18e9cf2 6
TSavang 13:74d440e3000b 7 //BusOut myleds(LED4,LED3,LED2,LED1); //LEDS for debugging
TSavang 13:74d440e3000b 8
TSavang 13:74d440e3000b 9 Serial blue(p13,p14);
4180_1 11:5186cc367be0 10 Serial pc(USBTX,USBRX);
spulavarty3 12:dd53c18e9cf2 11 Servo myservo(p21);
4180_1 11:5186cc367be0 12 DigitalOut shdn(p26);
4180_1 11:5186cc367be0 13 // This VL53L0X board test application performs a range measurement in polling mode
4180_1 11:5186cc367be0 14 // Use 3.3(Vout) for Vin, p28 for SDA, p27 for SCL, P26 for shdn on mbed LPC1768
johnAlexander 0:ce8359133ae6 15
4180_1 11:5186cc367be0 16 //I2C sensor pins
4180_1 11:5186cc367be0 17 #define VL53L0_I2C_SDA p28
4180_1 11:5186cc367be0 18 #define VL53L0_I2C_SCL p27
johnAlexander 0:ce8359133ae6 19
Davidroid 10:891e10d3b4a6 20 static XNucleo53L0A1 *board=NULL;
johnAlexander 1:3483e701ec59 21
spulavarty3 12:dd53c18e9cf2 22 Thread lidar_thread;
spulavarty3 12:dd53c18e9cf2 23 Thread motor_thread;
TSavang 13:74d440e3000b 24 Thread bluetooth_thread;
spulavarty3 12:dd53c18e9cf2 25
spulavarty3 12:dd53c18e9cf2 26 uint32_t volatile distance_copy;
spulavarty3 12:dd53c18e9cf2 27 int volatile status;
BubbaLee 14:63c2b3d14d06 28 double volatile on_speed = 0.1;
spulavarty3 12:dd53c18e9cf2 29
spulavarty3 12:dd53c18e9cf2 30 Mutex distance_lock;
spulavarty3 12:dd53c18e9cf2 31 Mutex status_lock;
spulavarty3 12:dd53c18e9cf2 32 Mutex speed_lock;
spulavarty3 12:dd53c18e9cf2 33
spulavarty3 12:dd53c18e9cf2 34 void check_distance()
spulavarty3 12:dd53c18e9cf2 35 {
spulavarty3 12:dd53c18e9cf2 36 uint32_t distance;
spulavarty3 12:dd53c18e9cf2 37
spulavarty3 12:dd53c18e9cf2 38 //loop taking and printing distance
spulavarty3 12:dd53c18e9cf2 39 while (1)
spulavarty3 12:dd53c18e9cf2 40 {
spulavarty3 12:dd53c18e9cf2 41 distance_lock.lock();
spulavarty3 12:dd53c18e9cf2 42 status_lock.lock();
spulavarty3 12:dd53c18e9cf2 43 status = board->sensor_centre->get_distance(&distance);
spulavarty3 12:dd53c18e9cf2 44 distance_copy = distance;
spulavarty3 12:dd53c18e9cf2 45
spulavarty3 12:dd53c18e9cf2 46 // for debugging, print distance to pc
spulavarty3 12:dd53c18e9cf2 47 if (status == VL53L0X_ERROR_NONE)
spulavarty3 12:dd53c18e9cf2 48 {
spulavarty3 12:dd53c18e9cf2 49 pc.printf("D=%ld mm\r\n", distance_copy);
BubbaLee 14:63c2b3d14d06 50 pc.printf("D=%ld mm\r\n", distance);
spulavarty3 12:dd53c18e9cf2 51 }
spulavarty3 12:dd53c18e9cf2 52 distance_lock.unlock();
spulavarty3 12:dd53c18e9cf2 53 status_lock.unlock();
spulavarty3 12:dd53c18e9cf2 54 }
spulavarty3 12:dd53c18e9cf2 55
spulavarty3 12:dd53c18e9cf2 56 }
spulavarty3 12:dd53c18e9cf2 57
spulavarty3 12:dd53c18e9cf2 58 void fan_control()
spulavarty3 12:dd53c18e9cf2 59 {
spulavarty3 12:dd53c18e9cf2 60 while (1)
spulavarty3 12:dd53c18e9cf2 61 {
BubbaLee 14:63c2b3d14d06 62 //status_lock.lock();
BubbaLee 14:63c2b3d14d06 63 //distance_lock.lock();
spulavarty3 12:dd53c18e9cf2 64 speed_lock.lock();
BubbaLee 14:63c2b3d14d06 65 //pc.printf("inside\n");
spulavarty3 12:dd53c18e9cf2 66 if (status == VL53L0X_ERROR_NONE)
spulavarty3 12:dd53c18e9cf2 67 {
spulavarty3 12:dd53c18e9cf2 68 if (distance_copy <= 610) // within 2 feet (distance is in mm)
spulavarty3 12:dd53c18e9cf2 69 {
spulavarty3 12:dd53c18e9cf2 70 myservo = on_speed;
BubbaLee 14:63c2b3d14d06 71 //pc.printf("on\n");
spulavarty3 12:dd53c18e9cf2 72 }
spulavarty3 12:dd53c18e9cf2 73 else
spulavarty3 12:dd53c18e9cf2 74 {
spulavarty3 12:dd53c18e9cf2 75 myservo = 0;
BubbaLee 14:63c2b3d14d06 76 //pc.printf("off\n");
spulavarty3 12:dd53c18e9cf2 77 }
spulavarty3 12:dd53c18e9cf2 78
spulavarty3 12:dd53c18e9cf2 79 }
BubbaLee 14:63c2b3d14d06 80 //status_lock.unlock();
BubbaLee 14:63c2b3d14d06 81 //distance_lock.unlock();
spulavarty3 12:dd53c18e9cf2 82 speed_lock.unlock();
spulavarty3 12:dd53c18e9cf2 83
spulavarty3 12:dd53c18e9cf2 84 Thread::yield();
spulavarty3 12:dd53c18e9cf2 85 }
spulavarty3 12:dd53c18e9cf2 86 }
spulavarty3 12:dd53c18e9cf2 87
TSavang 13:74d440e3000b 88 void speed_control()
TSavang 13:74d440e3000b 89 {
TSavang 13:74d440e3000b 90 char bnum=0;
TSavang 13:74d440e3000b 91 char bhit=0;
TSavang 13:74d440e3000b 92 //myleds = 0;
TSavang 13:74d440e3000b 93 while(1) {
TSavang 13:74d440e3000b 94 if (blue.getc()=='!') {
TSavang 13:74d440e3000b 95 if (blue.getc()=='B') { //button data packet
TSavang 13:74d440e3000b 96 bnum = blue.getc(); //button number
TSavang 13:74d440e3000b 97 bhit = blue.getc(); //1=hit, 0=release
TSavang 13:74d440e3000b 98 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
TSavang 13:74d440e3000b 99 switch (bnum) {
TSavang 13:74d440e3000b 100
TSavang 13:74d440e3000b 101 case '5': //button 5 up arrow - Increase fan speed.
TSavang 13:74d440e3000b 102 if (bhit=='1')
TSavang 13:74d440e3000b 103 {
BubbaLee 14:63c2b3d14d06 104 if( on_speed <= 0.2 )
TSavang 13:74d440e3000b 105 {
TSavang 13:74d440e3000b 106 speed_lock.lock();
BubbaLee 14:63c2b3d14d06 107 on_speed = on_speed + 0.02;
TSavang 13:74d440e3000b 108 speed_lock.unlock();
TSavang 13:74d440e3000b 109 //myleds = myleds + 1;
TSavang 13:74d440e3000b 110 }
TSavang 13:74d440e3000b 111 }
TSavang 13:74d440e3000b 112 break;
TSavang 13:74d440e3000b 113
TSavang 13:74d440e3000b 114 case '6': //button 6 down arrow - Decrease fan speed.
TSavang 13:74d440e3000b 115 if (bhit=='1') {
BubbaLee 14:63c2b3d14d06 116 if( on_speed >= 0.0 )
TSavang 13:74d440e3000b 117 {
TSavang 13:74d440e3000b 118 speed_lock.lock();
BubbaLee 14:63c2b3d14d06 119 on_speed = on_speed - 0.02;
TSavang 13:74d440e3000b 120 speed_lock.unlock();
TSavang 13:74d440e3000b 121 //myleds = myleds - 1;
TSavang 13:74d440e3000b 122 }
TSavang 13:74d440e3000b 123 }
TSavang 13:74d440e3000b 124 break;
TSavang 13:74d440e3000b 125
BubbaLee 14:63c2b3d14d06 126 case '1': //button 1 - Turn fan on.
TSavang 13:74d440e3000b 127 if (bhit=='1') {
TSavang 13:74d440e3000b 128 speed_lock.lock();
BubbaLee 14:63c2b3d14d06 129 on_speed = 0.1;
BubbaLee 14:63c2b3d14d06 130 speed_lock.unlock();
BubbaLee 14:63c2b3d14d06 131 //myleds = 0;
BubbaLee 14:63c2b3d14d06 132 }
BubbaLee 14:63c2b3d14d06 133 break;
BubbaLee 14:63c2b3d14d06 134
BubbaLee 14:63c2b3d14d06 135 case '2': //button 2 - Turn fan off.
BubbaLee 14:63c2b3d14d06 136 if (bhit=='1') {
BubbaLee 14:63c2b3d14d06 137 speed_lock.lock();
BubbaLee 14:63c2b3d14d06 138 on_speed = 0.0;
TSavang 13:74d440e3000b 139 speed_lock.unlock();
TSavang 13:74d440e3000b 140 //myleds = 0;
TSavang 13:74d440e3000b 141 }
TSavang 13:74d440e3000b 142 break;
TSavang 13:74d440e3000b 143 }
TSavang 13:74d440e3000b 144 }
TSavang 13:74d440e3000b 145 }
TSavang 13:74d440e3000b 146 }
TSavang 13:74d440e3000b 147 }
TSavang 13:74d440e3000b 148 }
TSavang 13:74d440e3000b 149
TSavang 13:74d440e3000b 150
johnAlexander 0:ce8359133ae6 151 int main()
4180_1 11:5186cc367be0 152 {
johnAlexander 7:c8087e7333b8 153 DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
johnAlexander 7:c8087e7333b8 154 /* creates the 53L0A1 expansion board singleton obj */
Davidroid 10:891e10d3b4a6 155 board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
spulavarty3 12:dd53c18e9cf2 156
4180_1 11:5186cc367be0 157 shdn = 0; //must reset sensor for an mbed reset to work
spulavarty3 12:dd53c18e9cf2 158 Thread::wait(100);
4180_1 11:5186cc367be0 159 shdn = 1;
spulavarty3 12:dd53c18e9cf2 160 Thread::wait(100);
spulavarty3 12:dd53c18e9cf2 161
spulavarty3 12:dd53c18e9cf2 162 status_lock.lock();
4180_1 11:5186cc367be0 163 /* init the 53L0A1 board with default values */
johnAlexander 7:c8087e7333b8 164 status = board->init_board();
spulavarty3 12:dd53c18e9cf2 165
spulavarty3 12:dd53c18e9cf2 166 while (status)
spulavarty3 12:dd53c18e9cf2 167 {
4180_1 11:5186cc367be0 168 pc.printf("Failed to init board! \r\n");
4180_1 11:5186cc367be0 169 status = board->init_board();
spulavarty3 12:dd53c18e9cf2 170 Thread::yield();
johnAlexander 7:c8087e7333b8 171 }
spulavarty3 12:dd53c18e9cf2 172 status_lock.unlock();
spulavarty3 12:dd53c18e9cf2 173
BubbaLee 14:63c2b3d14d06 174 myservo = 0.0;
spulavarty3 12:dd53c18e9cf2 175
BubbaLee 14:63c2b3d14d06 176 while(1)
BubbaLee 14:63c2b3d14d06 177 {
BubbaLee 14:63c2b3d14d06 178 lidar_thread.start(check_distance);
BubbaLee 14:63c2b3d14d06 179 motor_thread.start(fan_control);
BubbaLee 14:63c2b3d14d06 180 bluetooth_thread.start(speed_control);
BubbaLee 14:63c2b3d14d06 181 }
johnAlexander 0:ce8359133ae6 182 }