2015_robocon_bteam / Mbed 2 deprecated 2015robot_main

Dependencies:   PID QEI mbed

Fork of 2015robot_main by Naoto Deguchi

Revision:
32:eb7c263cb5cf
Parent:
26:8e6c736b6791
--- a/communicate.h	Sat Sep 19 10:57:41 2015 +0000
+++ b/communicate.h	Sun Sep 20 04:31:34 2015 +0000
@@ -4,7 +4,8 @@
 Serial RS485(RS485_TX, RS485_RX);
 #define EnablePin p7
 DigitalOut Enable(EnablePin);
-inline void sendData(int address, int data5) {
+inline void sendData(int address, int data5)
+{
     unsigned int data;
     if (data5 <= 31) {
         data = ((address << 5) | data5);
@@ -12,6 +13,164 @@
     }
 }
 
+/***IM920***/
+/*char rcvBUFF_IM920[40], rcvBUFF_SBDBT[40];
+int rcvCOUNT_IM920 = 0, rcvCOUNT_SBDBT = 0;
+int rcvFLAG = 0;
+int sendFLAG = 0;
+int deadCOUNT = 0;
+int oldDATA;
+unsigned int cm, a2, b, X, Y;
+unsigned int sdDATA = 0;
+int LED = 0;
+
+Serial IM920_USART(p9, p10);
+DigitalIn BUSY(p8);
+
+Ticker DTimer;
+
+void DEVICE_TX(int DATA, int DEVICE = 0) {
+    char sDATA[2];
+    if (DATA <= 15)
+        sprintf(sDATA, "0%X", DATA);
+    else
+        sprintf(sDATA, "%X", DATA);
+
+    if (DEVICE == 1) {
+        IM920_USART.printf("TXDA %s\r\n", sDATA);
+    }
+    else if (DEVICE == 2) {
+        SBDBT_USART.printf("00000000000%s\r\n", sDATA);
+    }
+    else {
+        IM920_USART.printf("TXDA %s\r\n", sDATA);
+        SBDBT_USART.printf("00000000000%s\r\n", sDATA);
+    }
+}
+
+int cv(int Buzzer, int LED) {
+    if (Buzzer > 1)
+        Buzzer = 1;
+    else if (Buzzer < 0)
+        Buzzer = 0;
+    if (LED < 0)
+        LED = 0;
+    else if (LED >= 16)
+        LED = 15;
+    return (Buzzer << 6) | (LED << 2);
+}
+
+void cvrecvDATA(char *buffDATA, unsigned long *outputDATA) {
+   char s1[2], s2[2], s3[2], s4[2];
+   strncpy(s1, buffDATA+11, 2);
+   strncpy(s2, buffDATA+14, 2);
+   strncpy(s3, buffDATA+17, 2);
+   strncpy(s4, buffDATA+20, 2);
+   outputDATA[0] = strtoul(s1, (char **) NULL, 16);
+   outputDATA[1] = strtoul(s2, (char **) NULL, 16);
+   outputDATA[2] = strtoul(s3, (char **) NULL, 16);
+   outputDATA[3] = strtoul(s4, (char **) NULL, 16);
+}
+
+void cvrecvDATAsbdbt(char *buffDATA, unsigned long *outputDATA) {
+   char s1[2], s2[2];
+   strncpy(s1, buffDATA+5, 2);
+   strncpy(s2, buffDATA+7, 2);
+   outputDATA[0] = strtoul(s1, (char **) NULL, 16);
+   outputDATA[1] = strtoul(s2, (char **) NULL, 16);
+}
+
+void readDATA(unsigned long* outDATA) {
+    cm = (outDATA[0] & 128) >> 7;
+    a2 = (outDATA[0] & 64) >> 6;
+    X = (outDATA[0] & 56) >> 3;
+    Y = outDATA[0] & 7;
+    b = outDATA[1];
+}
+
+void SBDBT_RX() {
+    char rcvDATA;
+    rcvDATA = SBDBT_USART.getc();
+    rcvBUFF_SBDBT[rcvCOUNT_SBDBT] = rcvDATA;
+    rcvCOUNT_SBDBT++;
+    if (rcvDATA == 0x0A) {
+        rcvCOUNT_SBDBT = 0;
+        rcvFLAG = 2;
+    }
+    else if (rcvCOUNT_SBDBT >= 40) {
+        rcvCOUNT_SBDBT = 0;
+        memset(rcvBUFF_SBDBT, '\0', 40);
+    }
+}
+
+void IM920_RX() {
+    char rcvDATA;
+    rcvDATA = IM920_USART.getc();
+    rcvBUFF_IM920[rcvCOUNT_IM920] = rcvDATA;
+    rcvCOUNT_IM920++;
+    if (rcvDATA == 0x0A) {
+        rcvCOUNT_IM920 = 0;
+        rcvFLAG = 1;
+    }
+    else if (rcvCOUNT_IM920 >= 40) {
+        rcvCOUNT_IM920 = 0;
+        memset(rcvBUFF_IM920, '\0', 40);
+    }
+}
+
+void deadCheck() {
+    if (rcvFLAG == 0 && deadCOUNT >= 5) {
+        PC.printf("DEAD\r\n");
+        deadCOUNT = 0;
+    }
+    else if (rcvFLAG == 0 && deadCOUNT >= 2) {
+        deadCOUNT++;
+        DEVICE_TX();
+    }
+    else if (rcvFLAG == 0) {
+        deadCOUNT++;
+    }
+    sdDATA = cvsendDATA(0, LED);
+}
+
+void initializeIM920(){
+    IM920_USART.baud(9600);
+    IM920_USART.format(8, Serial::None, 1);
+    IM920_USART.attach(IM920_RX, Serial::RxIrq);
+    SBDBT_USART.baud(9600);
+    SBDBT_USART.format(8, Serial::None, 1);
+    SBDBT_USART.attach(SBDBT_RX, Serial::RxIrq);
+    DTimer.attach(deadCheck, 0.3);
+    sdDATA = cvsendDATA(0, 0);
+    unsigned long outDATA[4];
+}
+
+void stateIM920(){
+    if (rcvFLAG == 1) {
+        if (strlen(rcvBUFF_IM920) > 8) {
+            cvrecvDATA(rcvBUFF_IM920, outDATA);
+            readDATA(outDATA);
+            PC.printf("A2 = %d, X = %d, Y = %d, B = %d \r\n", a2, X, Y, b);
+            deadCOUNT = 0;
+            DEVICE_TX(sdDATA, 1);
+        }
+        memset(rcvBUFF_IM920, '\0', 40);
+        rcvFLAG = 0;
+    }
+    else if (rcvFLAG == 2) {
+        if (strlen(rcvBUFF_SBDBT) > 4) {
+            cvrecvDATAsbdbt(rcvBUFF_SBDBT, outDATA);
+            readDATA(outDATA);
+            PC.printf("A2 = %d, X = %d, Y = %d, B = %d \r\n", a2, X, Y, b);
+            deadCOUNT = 0;
+            DEVICE_TX(sdDATA, 2);
+        }
+        memset(rcvBUFF_SBDBT, '\0', 40);
+        rcvFLAG = 0;
+    }
+}*/
+
+
 /***Get state ps3con.***/
 #define SBDBT_TX p28
 #define SBDBT_RX p27
@@ -30,6 +189,9 @@
 short edge_right        = 0;
 short edge_left         = 0;
 short edge_up           = 0;
+short edge_down         = 0;
+short edge_r2           = 0;
+short edge_l2           = 0;
 short square            = 0;
 short triangle          = 0;
 short circle            = 0;
@@ -82,10 +244,13 @@
     if(!triangle) edge_triangle=1;
     if(!r1) edge_r1=1;
     if(!l1) edge_l1=1;
+    if(!r2) edge_r2=1;
+    if(!l2) edge_l2=1;
     if(!right) edge_right=1;
-    if(!left) edge_left=1; 
+    if(!left) edge_left=1;
     if(!up) edge_up=1;
     if(!square) edge_square=1;
+    if(!down) edge_down=1;
 }
 ///interrupt SBDBT RX.
 inline void SBDBT_interrupt()
@@ -103,7 +268,7 @@
 {
     wait(0.1);
     while(analog_ly==-64)
-    for(int i=0; i<7; i++) ps3_data[i]=0;
+        for(int i=0; i<7; i++) ps3_data[i]=0;
     SBDBT.baud(2400);
     SBDBT.format(8, Serial::None, 1);
     SBDBT.attach(SBDBT_interrupt, Serial::RxIrq);