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.
Revision 7:652e2c5ad650, committed 2021-04-28
- Comitter:
- timo_k2
- Date:
- Wed Apr 28 14:48:15 2021 +0000
- Parent:
- 6:a0b604602460
- Commit message:
- Sensor reading is in a separate thread. The results can be used in the main thread.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed_app.json | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Dec 30 15:19:15 2020 +0000 +++ b/main.cpp Wed Apr 28 14:48:15 2021 +0000 @@ -36,66 +36,115 @@ #include "mbed.h" #include "ADXL362.h" +#include <cmath> +#define BUFF_SIZE 6 // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) : ADXL362 ADXL362(D5,D11,D12,D13); +//Threads + Thread detect_thread; + DigitalOut moveLed(D1); int ADXL362_reg_print(int start, int length); -void ADXL362_movement_detect(); +int ADXL362_movement_detect(); +int acceleration3D(int8_t ax,int8_t ay,int8_t az); -int main() -{ +int8_t x,y,z; +int movementDetected = 0; +int i = 0; + + +int main(){ + ADXL362.reset(); // we need to wait at least 500ms after ADXL362 reset ThisThread::sleep_for(600ms); ADXL362.set_mode(ADXL362::MEASUREMENT); ADXL362_reg_print(0, 0); - ADXL362_movement_detect(); + detect_thread.start(ADXL362_movement_detect); + + while(1){ + moveLed.write(movementDetected); + if(movementDetected){ + i += 1; + printf("i = %d\n", i); + } + printf("Acceleration 3D %d\n", acceleration3D(x,y,z)); + ThisThread::sleep_for(1s); + } } -void ADXL362_movement_detect() +int ADXL362_movement_detect() { - int8_t x1,y1,z1,x2,y2,z2,x,y,z,dx,dy,dz; - int i = 0; - while(1) - { - - while(1) - { - x1=ADXL362.scanx_u8(); - y1=ADXL362.scany_u8(); - z1=ADXL362.scanz_u8(); - ThisThread::sleep_for(10ms); - x2=ADXL362.scanx_u8(); - y2=ADXL362.scany_u8(); - z2=ADXL362.scanz_u8(); - - x=(x1 + x2)/2; - y=(y1 + y2)/2; - z=(z1 + z2)/2; + int8_t x1,y1,z1,x2,y2,z2,dx,dy,dz; + int detect; + while(1){ + x1=ADXL362.scanx_u8(); + y1=ADXL362.scany_u8(); + z1=ADXL362.scanz_u8(); + ThisThread::sleep_for(10ms); + x2=ADXL362.scanx_u8(); + y2=ADXL362.scany_u8(); + z2=ADXL362.scanz_u8(); - dx=abs(x1 - x2); - dy=abs(y1 - y2); - dz=abs(z1 - z2); - - if (dx>10 || dy>10 || dz>10) - break; - - printf("x = %3d y = %3d z = %3d dx = %3d dy = %3d dz = %3d\r\n",x,y,z,dx,dy,dz); - ThisThread::sleep_for(100ms); + x=(x1 + x2)/2; + y=(y1 + y2)/2; + z=(z1 + z2)/2; + + dx=abs(x1 - x2); + dy=abs(y1 - y2); + dz=abs(z1 - z2); + + if (dx>10 || dy>10 || dz>10){ + detect = 1; + } + else{ + detect = 0; + } + movementDetected = detect; + printf("x = %3d y = %3d z = %3d dx = %3d dy = %3d dz = %3d\r\n",x,y,z,dx,dy,dz); + ThisThread::sleep_for(100ms); + } +} + +int acceleration3D(int8_t ax,int8_t ay,int8_t az){ + float acc3D; + static int count = 0; + static int8_t x1[BUFF_SIZE]; + static int8_t y1[BUFF_SIZE]; + static int8_t z1[BUFF_SIZE]; + float averx; + float avery; + float averz; + + if(count >= BUFF_SIZE){ + count = 0; } - - moveLed = 1; - //wait(2); - ThisThread::sleep_for(2s); - moveLed = 0; - i++; - printf("%d\r\n", i); - - } -} + + x1[count]=ax; + y1[count]=ay; + z1[count]=az; + + count += 1; + + averx=0.0; + avery=0.0; + averz=0.0; + for(int k=0; k<BUFF_SIZE; k++){ + averx = averx+(float)x1[k]; + avery = avery+(float)y1[k]; + averz = averz+(float)z1[k]; + } + averx=averx/BUFF_SIZE; + avery=avery/BUFF_SIZE; + averz=averz/BUFF_SIZE; + + acc3D = sqrtf(pow(averx,2)+pow(avery,2)+pow(averz,2)); + //acc3D = sqrtf(pow(3.0,2) + pow(3.0,2) + pow(3.0,2)); + return((int)acc3D); +} int ADXL362_reg_print(int start, int length) /*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Wed Apr 28 14:48:15 2021 +0000 @@ -0,0 +1,9 @@ +{ + "target_overrides": { + "*": { + "platform.stdio-baud-rate": 115200, + "platform.stdio-buffered-serial": 1 + + } + } +} \ No newline at end of file