CIS541 / Mbed 2 deprecated Pacemaker_2

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
Huazhi
Date:
Fri Nov 27 21:43:33 2015 +0000
Commit message:
Initial commit

Changed in this revision

AMachine.h Show annotated file Show diff for this revision Revisions of this file
GlobalVar.h Show annotated file Show diff for this revision Revisions of this file
PVARP.h Show annotated file Show diff for this revision Revisions of this file
VMachine.h Show annotated file Show diff for this revision Revisions of this file
VRP.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
pacemaker_Param.h Show annotated file Show diff for this revision Revisions of this file
state_type.h Show annotated file Show diff for this revision Revisions of this file
transition.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AMachine.h	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,36 @@
+/* AMachine */
+void AMachine(void const *i)
+{
+    int CurrentState = AMACHINE_INITIAL;
+    RtosTimer local_timer = RtosTimer(AMachine_timer, osTimerPeriodic,(void *)0);
+    /*rtos timer can only use static functions as callbacks*/
+    local_timer.start(1);
+    while (true) {
+        int trn = 0;
+        switch (CurrentState) {
+            case AMACHINE_INITIAL:
+                if(AMachine_clock > CURRENT_LRI - AVI) {
+                    SyncStatus[APACE] = agre_time;
+                    CurrentState = AMACHINE_V;
+                    pc.printf("APace ");
+                    pc.printf("Time: %ld", agre_time);
+                    pc.printf("\n");
+                }
+        }
+        for(trn=0; trn<=4; trn++) {
+            if(TRANS[trn].source == CurrentState){
+                TRANS[trn].active = true;
+            }else{
+                TRANS[trn].active = false;
+            }
+            if(TRANS[trn].active) {
+                if(EVAL_GUARD(trn)) {
+                    if(TRANS[trn].syncid == NONE || agre_time - SyncStatus[TRANS[trn].syncid] < Broadcase_delay) {
+                        CurrentState = TRANS[trn].dest;
+                        ASSIGN(trn);
+                    }
+                }
+            }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GlobalVar.h	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,158 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "state_type.h"
+#include "pacemaker_Param.h"
+#include "transition.h"
+#include <map>
+using namespace std;
+
+Serial pc (USBTX, USBRX);
+
+/* Transition Table and Status */
+TRANS_T TRANS[100];
+map<Sync, long> SyncStatus;
+
+/* Init Transition Table */
+void initTrans(){
+    TRANS[0] = TRANS_T(true, AMACHINE_INITIAL, AMACHINE_V, ASENSE);
+    TRANS[1] = TRANS_T(true, AMACHINE_INITIAL, AMACHINE_INITIAL, VPACE);
+    TRANS[2] = TRANS_T(true, AMACHINE_INITIAL, AMACHINE_INITIAL, VSENSE);
+    TRANS[3] = TRANS_T(false, AMACHINE_V, AMACHINE_INITIAL, VPACE);
+    TRANS[4] = TRANS_T(false, AMACHINE_V, AMACHINE_INITIAL, VSENSE);
+    TRANS[10] = TRANS_T(true, VMACHINE_INITIAL, VMACHINE_INITIAL, VSENSE);
+    TRANS[11] = TRANS_T(true, VMACHINE_INITIAL, VMACHINE_AEVENT, ASENSE);
+    TRANS[12] = TRANS_T(true, VMACHINE_INITIAL, VMACHINE_AEVENT, APACE);
+    TRANS[13] = TRANS_T(false, VMACHINE_AEVENT, VMACHINE_INITIAL, VSENSE);
+    TRANS[14] = TRANS_T(false, VMACHINE_AEVENT, VMACHINE_INTER, NONE);
+    TRANS[15] = TRANS_T(false, VMACHINE_INTER, VMACHINE_INITIAL, VSENSE);
+    TRANS[16] = TRANS_T(false, VMACHINE_INTER, VMACHINE_AEVENT, ASENSE);
+    TRANS[20] = TRANS_T(true, VRP_INITIAL, VRP_WAIT, VPACE);
+    TRANS[21] = TRANS_T(true, VRP_INITIAL, VRP_VSENSE, VSIGNAL);
+    TRANS[22] = TRANS_T(false, VRP_WAIT, VRP_INITIAL, NONE);
+    TRANS[30] = TRANS_T(true, PVARP_INITIAL, PVARP_INTER, VPACE);
+    TRANS[31] = TRANS_T(true, PVARP_INITIAL, PVARP_INTER, VSENSE);
+    TRANS[32] = TRANS_T(false, PVARP_INTER, PVARP_AEVENT, NONE);
+    TRANS[33] = TRANS_T(false, PVARP_AEVENT, PVARP_ASENSE, ASIGNAL);
+    TRANS[34] = TRANS_T(false, PVARP_AEVENT, PVARP_INITIAL, APACE);
+    
+    SyncStatus[APACE] = -1000;
+    SyncStatus[ASENSE] = -1000;
+    SyncStatus[ASIGNAL] = -1000;
+    SyncStatus[VPACE] = -1000;
+    SyncStatus[VSENSE] = -1000;
+    SyncStatus[VSIGNAL] = -1000;
+}
+
+/* Global Timer */
+long global_time = 0;
+long agre_time = 0;
+void tick_timer(void const *n)
+{
+    global_time++;
+    agre_time++;
+}
+
+/* AMachine Timer */
+long AMachine_clock = 0;
+void AMachine_timer(void const *n)
+{
+    AMachine_clock++;
+}
+
+/* VMachine Timer */
+long VMachine_clock = 0;
+void VMachine_timer(void const *n)
+{
+    VMachine_clock++;
+}
+
+/* VRP Timer */
+long VRP_clock = 0;
+void VRP_timer(void const *n)
+{
+    VRP_clock++;
+}
+
+/* PVARP Timer */
+long PVARP_clock = 0;
+void PVARP_timer(void const *n)
+{
+    PVARP_clock++;
+}
+
+/* Evaluate Guard */
+bool EVAL_GUARD(int trn)
+{
+    switch(trn) {
+        case 0:
+            return AMachine_clock >= CURRENT_LRI-AVI;
+        case 1:
+            return true;
+        case 2:
+        case 3:
+        case 4:
+        case 5:
+            return true;
+        case 10:
+        case 11:
+        case 12:
+        case 13:
+            return true;
+        case 14:
+            return (VMachine_clock >= AVI && global_time < CURRENT_URI);
+        case 15:
+        case 16:
+            return true;
+        case 20:
+        case 21:
+            return true;
+        case 22:
+            return global_time >= VRP_TIME;
+        case 30:
+        case 31:
+            return true;
+        case 32:
+            return PVARP_clock >= PVARP_TIME;
+    }
+    return false;
+}
+
+/* Implement Update*/
+void ASSIGN(int trn)
+{
+    switch(trn) {
+        case 0:
+            break; // Lighten LED
+        case 1:
+        case 2:
+        case 3:
+        case 4:
+            AMachine_clock = 0;
+            break;
+        case 10:
+        case 13:
+        case 15:
+            global_time = 0; 
+            break;
+        case 11:
+        case 12:
+        case 16:
+            VMachine_clock = 0;
+            break;
+        case 14:
+            break;
+        case 20:
+        case 21:
+            VRP_clock = 0;
+            break;
+        case 22:
+        case 32:
+            break;
+        case 30:
+        case 31:
+        case 33:
+        case 34:
+            PVARP_clock = 0;
+            break;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PVARP.h	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,36 @@
+/* PVARP */
+void PVARP(void const *i)
+{
+    int CurrentState = PVARP_INITIAL;
+    RtosTimer local_timer = RtosTimer(PVARP_timer, osTimerPeriodic,(void *)0);
+    local_timer.start(1);
+    while (true) {
+        switch (CurrentState) {
+            case PVARP_ASENSE:
+                if(PVARP_clock >= ASENSE_TIME_UPPER) {
+                    SyncStatus[ASENSE] = agre_time;
+                    CurrentState = PVARP_INITIAL;
+                    PVARP_clock = 0;
+                    pc.printf("ASense ");
+                    pc.printf("Time: %ld", agre_time);
+                    pc.printf("\n");
+                }
+                break;
+        }
+        for(int trn=30; trn<=25; trn++) {
+            if(TRANS[trn].source == CurrentState){
+                TRANS[trn].active = true;
+            }else{
+                TRANS[trn].active = false;
+            }
+            if(TRANS[trn].active) {
+                if(EVAL_GUARD(trn)) {
+                    if(TRANS[trn].syncid == NONE || agre_time - SyncStatus[TRANS[trn].syncid] < Broadcase_delay) {
+                        CurrentState = TRANS[trn].dest;
+                        ASSIGN(trn);
+                    }
+                }
+            }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VMachine.h	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,44 @@
+/* VMachine */
+void VMachine(void const *i)
+{
+    int CurrentState = VMACHINE_INITIAL;
+    RtosTimer local_timer = RtosTimer(VMachine_timer, osTimerPeriodic,(void *)0);
+    local_timer.start(1);
+    while (true) {
+        switch (CurrentState) {
+            case VMACHINE_AEVENT:
+                if(VMachine_clock >= AVI && global_time >= CURRENT_URI) {
+                    SyncStatus[VPACE] = agre_time;
+                    CurrentState = VMACHINE_INITIAL;
+                    pc.printf("VPace ");
+                    pc.printf("Time: %ld", agre_time);
+                    pc.printf("\n");
+                }
+                break;
+            case VMACHINE_INTER:
+                if (global_time >= CURRENT_URI){
+                    SyncStatus[VPACE] = agre_time;
+                    CurrentState = VMACHINE_INITIAL;
+                    pc.printf("VPace ");
+                    pc.printf("Time: %ld", agre_time);
+                    pc.printf("\n");
+                }
+                break;
+        }
+        for(int trn=10; trn<=16; trn++) {
+            if(TRANS[trn].source == CurrentState){
+                TRANS[trn].active = true;
+            }else{
+                TRANS[trn].active = false;
+            }
+            if(TRANS[trn].active) {
+                if(EVAL_GUARD(trn)) {
+                    if(TRANS[trn].syncid == NONE || agre_time - SyncStatus[TRANS[trn].syncid] < Broadcase_delay) {
+                        CurrentState = TRANS[trn].dest;
+                        ASSIGN(trn);
+                    }
+                }
+            }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VRP.h	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,36 @@
+/* VRP */
+void VRP(void const *i)
+{
+    int CurrentState = VRP_INITIAL;
+    RtosTimer local_timer = RtosTimer(VRP_timer, osTimerPeriodic,(void *)0);
+    local_timer.start(1);
+    while (true) {
+        switch (CurrentState) {
+            case VRP_VSENSE:
+                if(VRP_clock >= VSENSE_TIME_UPPER) {
+                    SyncStatus[VSENSE] = agre_time;
+                    CurrentState = VRP_WAIT;
+                    VRP_clock = 0;
+                    pc.printf("VSense ");
+                    pc.printf("Time: %ld", agre_time);
+                    pc.printf("\n");
+                }
+                break;
+        }
+        for(int trn=20; trn<=22; trn++) {
+            if(TRANS[trn].source == CurrentState){
+                TRANS[trn].active = true;
+            }else{
+                TRANS[trn].active = false;
+            }
+            if(TRANS[trn].active) {
+                if(EVAL_GUARD(trn)) {
+                    if(TRANS[trn].syncid == NONE || agre_time - SyncStatus[TRANS[trn].syncid] < Broadcase_delay) {
+                        CurrentState = TRANS[trn].dest;
+                        ASSIGN(trn);
+                    }
+                }
+            }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,18 @@
+#include "GlobalVar.h"
+#include "AMachine.h"
+#include "VMachine.h"
+#include "VRP.h"
+#include "PVARP.h"
+
+int main()
+{
+    /*start timer */
+    RtosTimer global_timer = RtosTimer(tick_timer, osTimerPeriodic, (void *)0);
+    global_timer.start(1);
+    initTrans();
+    Thread t1(AMachine, (void *)1);
+    Thread t2(VMachine, (void *)2);
+    Thread t3(VRP, (void *)3);
+    Thread t4(PVARP, (void *)4);
+    while(1);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#6c35e082773a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9296ab0bfc11
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pacemaker_Param.h	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,25 @@
+#ifndef PACEMAKER_PARAM_H
+#define PACEMAKER_PARAM_H
+
+#define AVI 100
+#define PVARP_TIME 200
+#define ASENSE_TIME_UPPER 100
+
+#define SLEEP_UP_BOUND 60
+#define SLEEP_LOWER_BOUND 30
+#define NORMAL_UP_BOUND 100
+#define NORMAL_LOWER_BOUND 40
+#define SPORTS_UP_BOUND 175
+#define SPORTS_LOWER_BOUND 100
+#define MANUAL_UP_BOUND 175
+#define MANUAL_LOWER_BOUND 30
+
+#define VSENSE_TIME_UPPER 100
+#define VRP_TIME 220
+
+#define CURRENT_URI 600
+#define CURRENT_LRI 1500
+
+#define Broadcase_delay 30
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/state_type.h	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,20 @@
+#ifndef STATE_TYPE_H
+#define STATE_TYPE_H
+
+#define AMACHINE_INITIAL 0
+#define AMACHINE_V 2
+
+#define VMACHINE_INITIAL 30
+#define VMACHINE_AEVENT 31
+#define VMACHINE_INTER 32
+
+#define VRP_INITIAL 10
+#define VRP_WAIT 11
+#define VRP_VSENSE 12
+
+#define PVARP_INITIAL 20
+#define PVARP_INTER 21
+#define PVARP_AEVENT 22
+#define PVARP_ASENSE 23
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/transition.h	Fri Nov 27 21:43:33 2015 +0000
@@ -0,0 +1,9 @@
+enum Sync {NONE, APACE, ASENSE, ASIGNAL, VPACE, VSENSE, VSIGNAL};
+struct TRANS_T {
+    bool active;
+    int source;
+    int dest;
+    Sync syncid;
+    TRANS_T(): active(false), source(-1), dest(-1), syncid(NONE){};
+    TRANS_T(bool a, int b, int c, Sync d): active(a), source(b), dest(c), syncid(d) {};
+};