commit_cell_locker

Dependencies:   MPU6050_SIM5320_TEST

Fork of KOPIRANO_cell_locker by Suad Suljic

Revision:
1:75966605a6a3
Parent:
0:203cf529f52a
Child:
2:aabc92901e25
--- a/main.cpp	Thu Sep 14 16:50:16 2017 +0000
+++ b/main.cpp	Fri Nov 10 15:35:26 2017 +0000
@@ -1,191 +1,425 @@
 #include "mbed.h"
-#include "SDFileSystem.h"
-#include "SIM5320.h"
 #include "MPU6050.h"
-#include "SensorBoards.h"
-#include "WakeUp.h"
+#include "I2Cdev.h"
+#include <CaseFsm.h>
+#include <CaseEvents.h>
+#include <Fsm.h>
+#include <Events.h>
+#include "MFRC522.h"
+
+#include "log.h"
+#include "DataLogger.h"
+
+#define CURRENT_TIME 1510164886
+
+#define SPI_MOSI    PA_7
+#define SPI_MISO    PA_6
+#define SPI_SCK     PA_5
+#define SPI_CS      PA_8
+
+
+
+
+int cbx[20];
+int cby[20];
+int cbz[20];
+
+
+    Log mojlog;
+    DataLogger <Log> mojlogger;
+  //  mojlogger.print();
+   // mojlogger.savelog(mojlog);
+   // mojlogger.print();
 
 
 
-uint8_t write_sd_buffer[12];
-void create_sensor_data(uint8_t *write_sd_buffer, uint8_t *sensor_board_readings, uint8_t b_id, uint8_t s_id);
-//SIM5320 sim5320(PA_9,PA_10);
-DigitalOut SIM5320_PWR(PA_1);
-DigitalOut SIM_PWR_KEY(PA_15);
-InterruptIn mpuInterrupt(PB_6);
-uint8_t sensor_board_readings[4];
-bool test = false;
-//              MOSI, MISO, SCLK, CS, name
-SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd");
+DigitalOut rfidpower(PA_12);
+
+int x=0;
+
+DigitalOut led1(LED1);
+//Serial pc(USBTX,USBRX);
+
+bool XnegMD, XposMD, YnegMD, YposMD, ZnegMD, ZposMD;
+int8_t threshold;
+bool zero_detect;
+float temp;
+
+//void flip(){test=1;}
+Fsm* motionFsm;
+CaseFsm* caseFsm;
+SwitchPosition switchPosition;
+RFIDEvent rfidEvent;
+TimerEnd timerEnd;
+Move move;
+Stop stop;
+uint8_t print=0;
+//#include "PinDetect.h"
+
+InterruptIn button1(PD_2);
+InterruptIn button2(PC_11);
+InterruptIn button3(PC_10);
+InterruptIn button4(PC_12);//PRINT
+
+
+InterruptIn mpuInterrupt(PB_5);
 
-int go_to_sleep = 0;
+volatile uint8_t test = 0;
+volatile uint8_t mot=0;
+
+bool flag = false;
+bool DeviceIsFull = false;
+
+void button1PressedCallback()
+{
+    // printf("button1\n");
+    caseFsm->handle(switchPosition);
+}
+
+void button2PressedCallback()
+{
+    //printf("button2\n");
+    caseFsm->handle(rfidEvent);
+
+}
+void button3PressedCallback()
+{
+    //printf("button3\n");
+    caseFsm->handle(timerEnd);
+
+}
+
+void button4PressedCallback()
+{
+    print=1;
+
+}
+
 void mpuInterruptCallback()
 {
-    test=!test;
-
-}
-void rtc_wakeup()
-{
-    go_to_sleep = 1;
+    test=1;
 }
 
+
 MPU6050 mpu;
+uint8_t nomotion=0;
+uint8_t motionlogged=0;
+
+void getXYZMotions()
+{
+    wait(0.1);
+    uint8_t motionByte = mpu.readThisByte(MPU6050_RA_MOT_DETECT_STATUS);
+    // motionLog.motionByte = motionByte;
+    //motionLog.timeStamp = time(NULL);
+    //mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
+    uint8_t b;
+    bool zero = false;
+    char x,y,z;
+    int mx=0;
+    int my=0;
+    int mz=0;
+    x=y=z='0';
+    for(int i = 0; i<=7; i++) {
+        b = motionByte & (1<<i);
+        if(i==0 && b==1) {
+            //time_t sec = time(NULL);
+            //printf("Current time is: %s\r\n", ctime(&sec));
+            zero = true;
+            printf("ZERO MOTION detected.\r\n");
+            //pc.printf("    %c    %c    %c     |    ",x,y,z);
+            //pc.printf("%d;%d;%d;%d;%d;%d  ZERO DETECTED\r\n",ax,ay,az,gx,gy,gz);
+        }
+        if(i==1 && b==2) {
+            printf("This should not happen :).\r\n");
+        }
+        if(i==6 && b==64) {
+            x='+';
+            mx=1;
+            printf("X POSITIVE motion detected.\r\n");
+        }
+        if(i==7 && b==128) {
+            x='-';
+            mx=1;
+            printf("X NEGATIVE motion detected.\r\n");
+        }
+        if(i==4 && b==16) {
+            y='+';
+            my=1;
+            printf("Y POSITIVE motion detected.\r\n");
+        }
+        if(i==5 && b==32) {
+            y='-';
+            my=1;
+            printf("Y NEGATIVE motion detected.\r\n");
+        }
+        if(i==2 && b==4) {
+            z='+';
+            mz=1;
+            printf("Z POSITIVE motion detected.\r\n");
+        }
+        if(i==3 && b==8) {
+            z='-';
+            mz=1;
+            printf("Z NEGATIVE motion detected.\r\n");
+        }
+    }
+    printf("%d %d %d\r\n",mx,my,mz);
+
+    if(mx==0 && my==0 && mz==0) {
+        nomotion++;
+        if(nomotion==3 && motionlogged==0) {
+            printf("SAVE NO motion\n");
+            //motionFsm->handle(stop);
+            mojlog.setLog(900,0,0,900);
+            mojlogger.savelog(mojlog);
+            motionlogged=1;
+            nomotion=0;
+        }
+        }
+
+        else if(motionlogged==1) {
+            printf("SAVE motion\n");
+            motionlogged=0;
+            //motionFsm->handle(move);
+            mojlog.setLog(2222,0,1,2222);
+            mojlogger.savelog(mojlog);
+            nomotion=0;
+        }
+
+
+
+
+
+
+
+
+}
+
+
+
+
+
+
+
+MFRC522 RfChip(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, PB_4);
+
 int main()
 {
-/////////////////////////////////////////////////MPU_DEEP_SLEEP_WAKEUP////////////////////////////
-/*  
+
+    rfidpower=1;
+
+
+
+    CaseFsm caseF;
+    caseFsm = &caseF;
+
+    //mpuInterrupt.mode(PullUp);
+    button1.mode(PullUp);
+    button2.mode(PullUp);
+    button3.mode(PullUp);
+    button4.mode(PullUp); //print
+    button4.fall(button4PressedCallback);
+    button3.fall(button3PressedCallback);
+    button2.fall(button2PressedCallback);
+    button1.fall(&button1PressedCallback);
+
+    //mpuInterrupt.fall(mpuInterruptCallback);
+
+    RfChip.PCD_Init();
+    wait(0.2);
+
+
+
+
+
+
+    ////////////////////MPU INTERUPT RADI//////////////////////////////////////
+
     mpuInterrupt.fall(mpuInterruptCallback);
-    wait(2);
+    wait(0.2);
     mpu.calibrate(accelBias, gyroBias);
     mpu.initialize();
     mpu.setSleepEnabled(0);
-    wait(2);
+    wait(0.2);
     mpu.setIntFreefallEnabled(1);
-    mpu.setIntZeroMotionEnabled(0);
+    mpu.setIntZeroMotionEnabled(1);
     mpu.setIntMotionEnabled(1);
     printf("Setting mpu parameters...\r\n");
     mpu.setMotionDetectionThreshold(1);
     mpu.setMotionDetectionDuration(15);
+    mpu.setZeroMotionDetectionThreshold(1);
+    mpu.setZeroMotionDetectionDuration(5);
 
-    while(1){
-        printf("sleep\r\n");
-        deepsleep();
-        if(test){
-            wait(1);
-            printf("Desio se motion\r\n");
-            test = 0;
-            }
-        }
-*/
-        
-///////////////////////////////////////////////////////////////////////////////////////////         
-        
-  //  wait(3);
-  //  SIM5320_PWR = 1;
-   // SIM_PWR_KEY = 1;
-   // wait(1);
-   // SIM_PWR_KEY = 0;
-    /*   
-    wait(25);
+
+    /////////////////////////////////////////////////////////////////////////////
+    /*
+     mpu.calibrate(accelBias, gyroBias);
+    mpu.initialize();
+    mpu.setSleepEnabled(0);
+    mpu.setIntDMPEnabled(1);
+
+    uint8_t pinValue = mpu.getIntEnabled();
+    printf("Is int enabled? : %d\n", pinValue);
+    printf("MPU6050 testing connection \n");
+
+
     wait(2);
-    printf("Starting...\r\n");
-    printf("Starting...\r\n");
-    printf("Starting...\r\n");
-    printf("Starting...\r\n");
-    sim5320.sendCommand("AT+CNUM",2);
-    printf("Checking credit balance...\r\n");
-    sim5320.sendCommand("AT+CUSD=1, \"*100#\",15",5);
-    //Testing GPS
-
-    sim5320.enableGPS(true);
-    wait(30);
-
-    //Gettting GPS location
-    sim5320.sendCommand("AT+CGPSINFO",2);
-    sim5320.sendCommand("AT+CGPSINFO",2);
-    sim5320.sendCommand("AT+CGPSINFO",2);
+    bool mpu6050TestResult = mpu.testConnection();
+    if(mpu6050TestResult) {
+        printf("MPU6050 test passed \n");
+    } else {
+        printf("MPU6050 test failed \n");
+    }
 
 
+    mpu.setIntEnabled(1);
+    //mpu.setIntFreefallEnabled(1);
+    //mpu.setIntZeroMotionEnabled(0);
+    //mpu.setIntMotionEnabled(1);
+    mpu.setDHPFMode(1);
 
-    //Testing network connection and disconnection
-    sim5320.connect("active.bhmobile.ba","","");
-    wait(2);
-    if(sim5320.disconnect()) {
-        printf("Disconnected\r\n");
-    }
+    //*******************************************************************************************************************************
+
+    printf("\n\n\n");
+    uint8_t motionDetectionThreshold = mpu.getMotionDetectionThreshold();
+    //printf("MPU6050 motion detection threshold is: %d \r\n", motionDetectionThreshold);
+    //printf("MPU6050 set motion detection threshold to 2\r\n");
+    mpu.setMotionDetectionThreshold(1);
+    motionDetectionThreshold = mpu.getMotionDetectionThreshold();
+    //printf("MPU6050 motion detection threshold now is: %d \r\n", motionDetectionThreshold);
+    printf("Motion detection threshold is: %d \r\n", motionDetectionThreshold);
+    uint8_t motionDetectionDuration = mpu.getMotionDetectionDuration();
+    //printf("MPU6050 motion detection duration is: %d \r\n", motionDetectionDuration);
+    //printf("MPU6050 set motion detection duration to 40\r\n");
+    mpu.setMotionDetectionDuration(20);
+    motionDetectionDuration = mpu.getMotionDetectionDuration();
+    //printf("MPU6050 motion detection duration now is: %d \r\n", motionDetectionDuration);
+    printf("Motion detection duration is: %d \r\n", motionDetectionDuration);
+
+    //*******************************************************************************************************************************
 
-    else {
-        printf("Still connected or error occured!\r\n");
-    }
+    //printf("\n\n\n");
+    uint8_t zeroMotionDetectionThreshold = mpu.getZeroMotionDetectionThreshold();
+    //printf("MPU6050 zero motion detection threshold is: %d \r\n", zeroMotionDetectionThreshold);
+    //printf("MPU6050 set zero motion detection threshold to 2\r\n");
+    mpu.setZeroMotionDetectionThreshold(2);
+    zeroMotionDetectionThreshold = mpu.getZeroMotionDetectionThreshold();
+    //printf("MPU6050 zero motion detection threshold now is: %d \r\n", zeroMotionDetectionThreshold);
+    printf("Zero motion detection threshold is: %d \r\n", zeroMotionDetectionThreshold);
+    //printf("\n\n\n");
+    int8_t zeroMotionDetectionDuration = mpu.getZeroMotionDetectionDuration();
+    //printf("MPU6050 zero motion detection duration is: %d \r\n", zeroMotionDetectionDuration);
+    //printf("MPU6050 set zero motion detection duration to 1\r\n");
+    mpu.setZeroMotionDetectionDuration(1);
+    zeroMotionDetectionDuration = mpu.getZeroMotionDetectionDuration();
+    //printf("MPU6050 zero motion detection duration now is: %d \r\n", zeroMotionDetectionDuration);
+    printf("Zero motion detection duration is: %d \r\n", zeroMotionDetectionDuration);
+
+    //*******************************************************************************************************************************
+
+    //printf("\n\n\n");
+    int8_t freefallDetectionThreshold = mpu.getFreefallDetectionThreshold();
+    //printf("MPU6050 freefall motion detection threshold is: %d \r\n", freefallDetectionThreshold);
+    //printf("MPU6050 set freefall motion detection threshold to 40\r\n");
+    //mpu.setFreefallDetectionThreshold(2);
+    freefallDetectionThreshold = mpu.getFreefallDetectionThreshold();
+    //printf("MPU6050 freefall motion detection threshold now is: %d \r\n", freefallDetectionThreshold);
+    printf("Freefall motion detection threshold is: %d \r\n", freefallDetectionThreshold);
+    //printf("\n\n\n");
+    int8_t freefallDetectionDuration = mpu.getFreefallDetectionDuration();
+    //printf("MPU6050 freefall motion detection duration is: %d \r\n", freefallDetectionDuration);
+    //printf("MPU6050 set freefall motion detection duration to 40\r\n");
+    //mpu.setFreefallDetectionDuration(40);
+    freefallDetectionDuration = mpu.getFreefallDetectionDuration();
+    //printf("MPU6050 freefall motion detection duration now is: %d \r\n", freefallDetectionDuration);
+    printf("Freefall motion detection duration is: %d \r\n", freefallDetectionDuration);
+    printf("\n\n\n");
+    printf("MPU6050 test\n\n");
+
+
+    //*******************************************************************************************************************************
+    */
 
 
 
     while(1) {
-        wait(1);
-        sim5320.sendCommand("AT",1);
-    }
-    */
-    
-    
-    
-/////////////////////////////////////////////////RTC_DEEP_SLEEP_WAKEUP////////////////////////////  
- /*   
-    set_time(1495040081);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
-    WakeUp::attach(&rtc_wakeup);
-    WakeUp::calibrate();
-    while (1) {
-        WakeUp::set_ms(5000);
-        time_t seconds = time(NULL);
-        printf("Time = %s\n", ctime(&seconds));
-        wait(1);
-        printf("sleep\n");
-        deepsleep();
-        if (go_to_sleep == 1) {
-            wait(1);
-            printf("Wake_up\n");
-            go_to_sleep=0;
+
+        if(test==1) {
+            wait(0.2);
+            test = 0;
+            getXYZMotions();
+            //mot=2;
         }
-    }
-*/
-////////////////////////////////////////////////////////////////////////////////////////////////
+
+
+
+        if(print==1) {
+            mojlogger.print();
+            print=0;
+        }
 
 
 
-//////////////////////////////////////////////CODE FOR SENDOR_BOARDS////////////////////////////////////////////////////////////////
-    /*
-        uint8_t IDBuffer[8];
-        uint8_t IDMeasure[4];
-        float measure=0.0;
-        float measure2=0.0;
-        
+//printf("caseFsm->DeviceInFullState()=%d\n",caseFsm->DeviceInFullState());
+        //This goes into caseFsm, but for now we are testing in main.cpp
+        if(caseFsm->DeviceInFullState()) {
+            printf("Waiting 5 seconds for RFID event...\r\n");
+            while (caseFsm->DeviceInFullState()) {
+                // Look for new cards
+
+
+                if ( ! RfChip.PICC_IsNewCardPresent()) {
+                    wait_ms(500);
+                    printf("!RfChip.PICC_IsNewCardPresent()\n");
+                    continue;
+                }
+
+                // Select one of the cards
+                if ( ! RfChip.PICC_ReadCardSerial()) {
+                    printf("!RfChip.PICC_ReadCardSerial()\n");
+                    wait_ms(500);
+                    continue;
+                }
 
-        SensorBoards B1;
-         B1.getSensorReadings(0x4a,0x10,IDMeasure);
-        printf("number_of__boards= %d\n",B1.numberOfBoards);
-        B1.sensorBoardScanner();
-        printf("SnesorBoardAdress= %d\n",B1.boards[0].I2CAddress);
-        B1.getSensorNumbers();
-        printf("SensorBoardsensors= %d\n",B1.boards[0].numberOfSensors);
-        B1.getSensorIDs();
-        printf("SensorBoardIDs=%d     %d  \n",B1.boards[0].sensorIDs[0],B1.boards[0].sensorIDs[1]);
-  
-        B1.getSensorReadings(0x4a,0x10,IDMeasure);
-      
-        for(int k=0; k<1; k++) {
-            printf("broj senzora %d \n",B1.boards[k].numberOfSensors);
-            for(int i=0; i<B1.boards[k].numberOfSensors; i++){
-                B1.getSensorReadings(B1.boards[k].I2CAddress,B1.boards[k].sensorIDs[i],IDMeasure);
-                printf("measure=%d %d %d %d keaj\n",IDMeasure[0],IDMeasure[1],IDMeasure[2],IDMeasure[3]);
-                //create_sensor_data(write_sd_buffer,IDMeasure,B1.boards[k].I2CAddress,B1.boards[k].sensorIDs[i]);
+                // Print Card UID
+                printf("Card UID: ");
+                for (uint8_t i = 0; i < RfChip.uid.size; i++) {
+                    rfidEvent.uid[i] = RfChip.uid.uidByte[i];
+                    printf("%X ", RfChip.uid.uidByte[i]);
                 }
+                rfidEvent.size = RfChip.uid.size;
+                printf("\n\r");
+
+                // Print Card type
+                uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
+                printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
+                wait_ms(1000);
+                rfidEvent.cardType = piccType;
+                //phone inserted
+                caseFsm->handle(rfidEvent);
+                printf("_%d_\n",caseFsm->DeviceInPhoneDetectedState());
+                if(caseFsm->DeviceInPhoneDetectedState()) {
+                    wait(0.3);
+                    x++;
+                    printf("snimi\n");
+                    mojlog.setLog(600,1,0,600);
+                    mojlogger.savelog(mojlog);
+                    ;//LOGOVATI INSERTED
+
+                }
+
+            }
+            if(caseFsm->DeviceInDeviceEmptyState()) {
+                wait(0.3);
+                printf("ne snimi\n");
+                x++;   //LOGOVATI REMOVED
+                mojlog.setLog(400,0,0,400);
+                mojlogger.savelog(mojlog);
+            }
         }
 
-    for(int i=0;i<12;i++)
-    printf("_%d\n",write_sd_buffer[i]);
-  */  
 
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
-    
-    
-    
-return 0;    
-}
-void create_sensor_data(uint8_t *write_sd_buffer, uint8_t *sensor_board_readings, uint8_t b_id, uint8_t s_id)
-{
-    //pcf8563_read(&rtc);
 
-    write_sd_buffer[0]=0;
-    write_sd_buffer[1]=1;
-    write_sd_buffer[2]=2;
-    write_sd_buffer[3]=3;
-    write_sd_buffer[4]=4;
-    write_sd_buffer[5] =b_id;       //sensor boaard id
-    write_sd_buffer[6] =s_id;    //sensor id
-    write_sd_buffer[7] =sensor_board_readings[3];          //sensor reading
-    write_sd_buffer[8] =sensor_board_readings[2];          //sensor reading
-    write_sd_buffer[9] =sensor_board_readings[1];          //sensor reading
-    write_sd_buffer[10]=sensor_board_readings[0];         //sensor reading
-    write_sd_buffer[11]=0xFF;
-    write_sd_buffer[12]=0xFF;
+    }
 
 }
\ No newline at end of file