自由軟體到物聯網--智慧手錶(smart watch) === Group 2 [github](https://github.com/csielee/2017IOT_SmartWatch) [Demo Video(youtube)](https://www.youtube.com/watch?v=AP51p2a-vo0&feature=youtu.be) ## Memeber F74031027 李東霖 ## flow control ### init ```flow a=>start: start b=>condition: cellphone connect? c=>operation: Wear a watch d=>operation: Arduino bluno beetle start working e=>operation: Time correction by cellphone int real_time[6]; real_time[0] = year_number; real_time[1] = month_number; real_time[2] = day_number; real_time[3] = hour_number; real_time[4] = minute_number; real_time[5] = second_number; unsigned long last_time = millis(); f=>subroutine: Display time a->c->d->b(yes)->e->f b(no)->b ``` ### Display time ```flow a=>start: start z=>condition: millis() - last_time >= 1000 b=>operation: unsigned long append_second = (millis() - last_time)/1000; real_time[5] += append_second % 60; append_second /= 60; real_time[4] += append_second % 60; append_second /= 60; real_time[3] += append_second % 24; append_second /= 24; real_time[2] += append_second; c=>operation: firstPage(); d=>operation: draw time on OLED by u8glib e=>condition: nextPage() == true f=>condition: detect gesture? g=>subroutine: Display heartrate、HRV、LF、HF h=>subroutine: Display time a->f(yes,right)->g f(no)->z z(yes)->b->c->d->e(yes,right)->d e(no)->h ``` ### Display heartrate、HRV、LF、HF ```flow st=>start: start connect=>condition: cellphone connect? getwavefront=>subroutine: bool IsWaveFront = getWaveFront(sensor data); a=>condition: IsWaveFront == true; b=>operation: unsigned long sample = millis(); getwavefront2=>subroutine: bool IsWaveFront = getWaveFront(sensor data); a2=>condition: IsWaveFront == true; b2=>operation: sample = millis() - sample; io=>inputoutput: send sample(4 Bytes) to cellphone c=>condition: cellphone send message? d=>operation: get Message from cellphone double HRV = Message_HRV; double LF_HF = Message_LF_HF; e=>operation: calculate HeartRate int HR = 60000/sample; f=>operation: firstPage(); h=>operation: draw HR on OLED by uglib8 i=>condition: HRV LF_HF has value? j=>operation: draw HRV LF_HF on OLED by u8glib k=>operation: draw "HRV LF_HF in calculate" on OLED by u8glib l=>condition: nextPage() == true g=>subroutine: Display heartrate、HRV、LF、HF m=>condition: detect gesture? n=>subroutine: Display steps、altitude st->connect(no)->connect connect(yes)->getwavefront->a(no)->getwavefront a(yes)->b->getwavefront2->a2(no)->getwavefront2 a2(yes)->b2->io->c(no)->e c(yes)->d->e->f->h->i(yes)->j->l(yes)->h i(no)->k->l l(no,r)->m(no)->connect m(yes)->n ``` ### getWaveFront ```c bool getWaveFront(int sensor_data) { static int threshold = 512; static int data = -1; static bool first = true; if (threshold >= sensor_data) { first = true; return false; } else { if (!first) return false; if (sensor_data - data < 0) { first = false; return true; } } data = sensor_data; return false; } ``` ### Display steps、altitude ```flow a=>start: start init=>operation: int step = 0; bool overFlag = false; b=>condition: over Threshold? c=>operation: step += 1; d=>operation: filter e=>inputoutput: get horizontally accel from GY-80 by I2C f=>operation: overFlag = false; g=>operation: overFlag = true h=>condition: overFlag == true i=>inputoutput: get air pressure from GY-80 by I2C j=>operation: double altitude = ((double)(101325 - sensor_data)/100)*9; l=>operation: firstPage(); m=>operation: draw step altitude on OLED by u8glib n=>condition: nextPage() == true o=>subroutine: Display steps、altitude p=>condition: detect gesture q=>subroutine: Display time a->init->e->d->b(yes)->h(no,left)->c->g->i h(yes)->i b(no)->f(right)->i i->j->l->m->n(yes)->m n(no,left)->p(yes)->q p(no)->e ``` ### get data from GY-80 use I^2^C to get - 加速度計 ADXL345 - 0x53 - 陀螺儀 L3G4200 - 0x69 - 電子羅盤 HMC5883 - 0x1E - 氣壓計 BMP085 - 0x77 ## block diagram ![](https://i.imgur.com/voGB2A1.png) ## What functions you plan to implement in your smart watch - HRV + LF/HF - HeartRate Display - Gesture operation - Pedometer - OLED Display - Altitude Display - Unplug to power off - Three display modes ## The algorithms you’re going to use to implement your functions ### HRV + LF/HF Use PulseSensor to produce PPG(Photoplethysmography) . **PPG** ![](https://cdn.shopify.com/s/files/1/0100/6632/files/Thresh_03_grande.jpg?10) In PPG ,when the heart shrinks,blood vessel flow and value of PPG will rise So PPG wave front interval, can represent the contraction of the heart interval, but also can represent the heart rate like RR interval. Use SDNN to calculate HRV SDNN is record all PPG wave peak interval,and calculate standard deviation of these datas; $$\sigma=\sqrt{\frac{\sum_{i=0}^{N-1} X_i^2}{N} - u^2}$$ To deal with LF/HF, need to transform PPG wave weak interval by Discrete Fourier Transform. $$X_k = \sum_{n=0}^{N-1} e^{-i\frac{2\pi}{N}nk}X_n\ ,\ k=0,1,2...N-1$$ Get LF HF LF/HF from spectrum. To more than 5 minutes, have enough information. To avoid second wave, we also need firs wave peak. ### HeartRate Display When calculate HRV, can record PPG wave peak interval. Then, calculate average interval in one minute. $$HR = 60/(average\ interval\ in\ one\ minute)$$ Can get HeartRate. ### Gesture operation Judge gesture by gyroscope(L3G4200). Then , opera watch by gesture. ![](https://sites.google.com/site/parallaxinretailstores/_/rsrc/1321471656622/home/gyroscope-module-l3g4200d/Gyro-2.png) It can judge gestures 1. Arm outwart flip 2. Arm inward flip Fixed parallel to the arm is the Y axis. In theory, when the outward flip, Y-axis angular velocity will rise and then decline. When the inward flip, Y-axis angular velocity will decline and then rise. ![](https://i.imgur.com/ooVwmmP.jpg) To detect changes, you can know the user's gestures ### Pedometer Get accel of x、y、z by accelerometer(ADXL345). Judge movement state of user by these datas. Acceleration cyclical changes ![](https://i2.read01.com/uploads/08JzTF01.jpg) Flow ```flow a=>start: start b=>condition: over Threshold? c=>operation: increase one step d=>operation: filter e=>inputoutput: get horizontally accel f=>subroutine: clear overFlag g=>subroutine: set overFlag h=>condition: is overFlag set? a->e->d->b(yes)->h(no,left)->c->g(left)->e h(yes)->e b(no)->f(right)->e ``` ### OLED Display Use u8glib to drive OLED board. https://github.com/olikraus/u8glib Use a loop to control pixel ```clike firstPage(); do { //call u8glib to draw drawPixel(x,y); drawLine(x1,y1,x2,y2); drawTriangle(x1,y1,x2,y2,x3,y3); drawFrame(x,y,w,h); drawBox(x,y,w,h); drawCircle(x,y,r,opt); drawDisc(x,y,r,opt); setFont(...); drawBitmapP(x,y,cnt,h,bitmap); drawStr(x,y,str); } while ( nextPage() ); ``` ### Altitude Display Get current air pressure by BMP085. Then, calculate altitude by air pressure. $$H = ((101325\ pa - P_c)/100)*9\ m $$ ### Unplug to power off Let the strap connect as a power switch. ### Three display modes 1. Display time 2. Display heartrate、HRV、LF、HF 3. Display steps、altitude ## How your watch is supposed to look like ### block diagram ![](https://i.imgur.com/voGB2A1.png) <br> ![](https://i.imgur.com/0QVrK50.jpg) ### look like ![](https://i.imgur.com/RUgfeKQ.png) ## Job descriptions - program design - hardware design - UI/UX ## Reference [基於智慧型手機之跌倒偵測與三維計步器之設計與實現](http://tkuir.lib.tku.edu.tw:8080/dspace/bitstream/987654321/96952/2/%E5%9F%BA%E6%96%BC%E6%99%BA%E6%85%A7%E5%9E%8B%E6%89%8B%E6%A9%9F%E4%B9%8B%E8%B7%8C%E5%80%92%E5%81%B5%E6%B8%AC%E8%88%87%E4%B8%89%E7%B6%AD%E8%A8%88%E6%AD%A5%E5%99%A8%E4%B9%8B%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E7%8F%BE-%E6%9C%83%E8%AD%B0%E8%AB%96%E6%96%87.pdf) [利用3軸數字加速度計實現功能全面的計步器設計](https://read01.com/AN0oxm.html) [情緒偵測系統-以 LabVIEW 為平台之系統 設計](http://ir.lib.cyut.edu.tw:8080/retrieve/28334/201108.pdf) [讀取 GY-80 模組:加速度計(ADXL345) 陀螺儀(L3G4200) 電子羅盤(HMC5883) 氣壓計(BMP085)](http://wukcsoft.blogspot.tw/2014/06/read-gy-80.html) [ADXL345-datasheet](http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf) [L3G4200-datasheet](http://www.st.com/content/ccc/resource/technical/document/datasheet/04/46/d6/00/be/d9/46/ae/CD00265057.pdf/files/CD00265057.pdf/jcr:content/translations/en.CD00265057.pdf) [HMC5883-datasheet](https://aerocontent.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/HMC5883L_3-Axis_Digital_Compass_IC.pdf) [BMP085-datasheet](https://www.sparkfun.com/datasheets/Components/General/BST-BMP085-DS000-05.pdf) [U8glib Arduino OLED Tutorial 1: Hello World on Steroids](http://henrysbench.capnfatz.com/henrys-bench/u8glib-graphics-library-user-guide/u8glib-arduino-oled-tutorial-1-hello-world-on-steroids/) [使用U8glib驱动12864图形液晶显示器(一)](http://www.arduino.cn/thread-19802-1-1.html) [逢甲大學自動控制工程學系大學部_畢業專題_ECG 與 PPG 信號之相關性研究](http://www.auto.fcu.edu.tw/wSite/publicfile/Attachment/f1256196852569.pdf) [Pulse Sensor Amped](https://pulsesensor.com/pages/pulse-sensor-amped-arduino-v1dot1) [u8glib常用函数](http://www.geek-workshop.com/thread-10634-1-1.html) [ArduioFFT](http://wiki.openmusiclabs.com/wiki/ArduinoFFT) [PulseSensor_Amped_Arduino](https://github.com/WorldFamousElectronics/PulseSensor_Amped_Arduino) [arduino quadcopter](https://mlwmlw.org/2014/09/arduino-quadcopter/) [arduino bluno bettle DFR0267](https://www.dfrobot.com/wiki/index.php/Bluno_SKU:DFR0267) [arduino bluno bettle DFR0339](https://www.dfrobot.com/wiki/index.php/Bluno_Beetle_SKU:DFR0339) [arduino bluno bettle DFR0339 中文](http://wiki.dfrobot.com.cn/index.php?title=(SKU:DFR0339)Bluno_Beetle%E6%8E%A7%E5%88%B6%E5%99%A8_%E5%85%BC%E5%AE%B9Arduino)