embedded / RemoteIR

Dependents:   line_tracing

Revision:
12:96209c979a7f
Parent:
11:268cc2ab63bd
Child:
13:5e445169cf83
--- a/ReceiverIR.cpp	Mon Sep 20 00:54:59 2010 +0000
+++ b/ReceiverIR.cpp	Wed Jun 05 04:49:42 2019 +0000
@@ -1,15 +1,8 @@
-/**
- * IR receiver (Version 0.0.4)
- *
- * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
- * http://shinta.main.jp/
- */
-
 #include "ReceiverIR.h"
-
+ 
 #define LOCK()
 #define UNLOCK()
-
+ 
 #define InRange(x,y)   ((((y) * 0.7) < (x)) && ((x) < ((y) * 1.3)))
 
 /**
@@ -17,20 +10,25 @@
  *
  * @param rxpin Pin for receive IR signal.
  */
-ReceiverIR::ReceiverIR(PinName rxpin) : evt(rxpin) {
+
+ReceiverIR::ReceiverIR(PinName rxpin, float _speed, PinName _pwma,PinName _pwmb, PinName _ain0, PinName _ain1, PinName _bin0, PinName _bin1)
+: evt(rxpin), pwma(_pwma), pwmb(_pwmb),ain1(_ain1),ain0(_ain0), bin0(_bin0),bin1(_bin1) {
     init_state();
     evt.fall(this, &ReceiverIR::isr_fall);
     evt.rise(this, &ReceiverIR::isr_rise);
     evt.mode(PullUp);
-    ticker.attach_us(this, &ReceiverIR::isr_wdt, 10 * 1000);
+    ticker.attach_us(this, &ReceiverIR::isr_wdt, 10 * 1000);    
+    Speed_L = 0.09 ;
+    //Speed_R = _speed *1.005;
+    Speed_R = 0.085 ;
 }
-
+ 
 /**
  * Destructor.
  */
 ReceiverIR::~ReceiverIR() {
 }
-
+ 
 /**
  * Get state.
  *
@@ -42,7 +40,7 @@
     UNLOCK();
     return s;
 }
-
+ 
 /**
  * Get data.
  *
@@ -54,24 +52,164 @@
  */
 int ReceiverIR::getData(RemoteIR::Format *format, uint8_t *buf, int bitlength) {
     LOCK();
-
+ 
     if (bitlength < data.bitcount) {
         UNLOCK();
         return -1;
     }
-
+ 
     const int nbits = data.bitcount;
     const int nbytes = data.bitcount / 8 + (((data.bitcount % 8) != 0) ? 1 : 0);
     *format = data.format;
     for (int i = 0; i < nbytes; i++) {
         buf[i] = data.buffer[i];
     }
-
+    
+    move(buf);
     init_state();
-
+    
     UNLOCK();
     return nbits;
 }
+ 
+void ReceiverIR::move(uint8_t *buf){
+    char buffer[64];
+    sprintf(buffer, "%02X%02X%02X%02X", buf[0], buf[1], buf[2], buf[3]);
+    //start
+//    if(!strncmp(buffer, "00FF09F6",8)){
+//        //pc.printf("forward!\r\n");
+//        start = 1;
+//        //forward();      
+//    }    
+    // forward
+    if(!strncmp(buffer, "00FF18E7",8)){
+        //pc.printf("forward!\r\n");
+        forward();      
+    }
+    // back
+    if(!strncmp(buffer, "00FF52AD",8)){
+        //pc.printf("back!\r\n");       
+        backward();
+    }
+    // left
+    if(!strncmp(buffer, "00FF08F7",8)){
+        //pc.printf("left!\r\n");       
+        start = 1;
+    }
+    // right
+    if(!strncmp(buffer, "00FF5AA5",8)){
+        //pc.printf("right!\r\n");       
+        right();
+    }
+    // stop
+    if(!strncmp(buffer, "00FF0CF3",8)){
+        speedup_l(1);
+    }   
+    if(!strncmp(buffer, "00FF5EA1",8)){
+        speedup_r(1);
+    }   
+    if(!strncmp(buffer, "00FF42BD",8)){
+        speeddown_l(1);
+    }   
+    if(!strncmp(buffer, "00FF4AB5",8)){
+        speeddown_r(1);
+    }   
+    if(!strncmp(buffer, "00FF1CE3",8)){
+//        pc.printf("stop!\r\n");       
+        stop();
+    }       
+}
+
+void ReceiverIR::speedup_l(int weight){
+    Speed_L += 0.0003*weight; 
+    if(Speed_L > 0.5){
+        Speed_L = 0.5;
+    }
+    pwma = Speed_L;
+}
+void ReceiverIR::speeddown_l(int weight){
+    Speed_L -= 0.0003*weight;
+    if(Speed_L < 0){
+        Speed_L = 0;
+    }
+    pwma = Speed_L;
+}
+void ReceiverIR::speedup_r(int weight){
+    Speed_R += 0.0003*weight; 
+    if(Speed_R > 0.5){
+        Speed_R = 0.5;
+    }
+    pwmb = Speed_R;
+}
+void ReceiverIR::speeddown_r(int weight){
+    Speed_R -= 0.0003*weight;
+    if(Speed_R < 0){
+        Speed_R = 0;
+    }
+    pwmb = Speed_R;
+}
+
+void ReceiverIR::speed_l(float sp_l){
+    Speed_L = sp_l;
+}
+
+void ReceiverIR::speed_r(float sp_r){
+    Speed_R = sp_r;
+}
+
+void ReceiverIR::forward(void){
+    pwma = Speed_L;
+    pwmb = Speed_R;
+
+    ain0 = 1;
+    ain1 = 0;
+
+    bin0 = 1;
+    bin1 = 0;
+}
+void ReceiverIR::backward(void){
+    
+    pwma = Speed_L;
+    pwmb = Speed_R;
+
+    ain0 = 0;
+    ain1 = 1;
+
+    bin0 = 0;
+    bin1 = 1;    
+}
+
+void  ReceiverIR::left(void){
+    pwma = Speed_L*0.8;
+    pwmb = Speed_R;
+
+    ain0 = 1;
+    ain1 = 0;
+
+    bin0 = 1;
+    bin1 = 0;
+}
+void ReceiverIR::right(void){
+    pwma = Speed_L;
+    pwmb = Speed_R*0.85;
+
+    ain0 = 1;
+    ain1 = 0;
+
+    bin0 = 1;
+    bin1 = 0;
+}
+void ReceiverIR::stop(void){
+    
+    pwma = 0;
+    pwmb = 0;
+
+    ain0 = 0;
+    ain1 = 0;
+
+    bin0 = 0;
+    bin1 = 0;
+}
 
 void ReceiverIR::init_state(void) {
     work.c1 = -1;
@@ -88,24 +226,13 @@
         data.buffer[i] = 0;
     }
 }
-
+ 
 void ReceiverIR::isr_wdt(void) {
     LOCK();
     static int cnt = 0;
     if ((Idle != work.state) || ((0 <= work.c1) || (0 <= work.c2) || (0 <= work.c3) || (0 <= work.d1) || (0 <= work.d2))) {
         cnt++;
         if (cnt > 50) {
-#if 0
-            printf("# WDT [c1=%d, c2=%d, c3=%d, d1=%d, d2=%d, state=%d, format=%d, bitcount=%d]\n",
-                   work.c1,
-                   work.c2,
-                   work.c3,
-                   work.d1,
-                   work.d2,
-                   work.state,
-                   data.format,
-                   data.bitcount);
-#endif
             init_state();
             cnt = 0;
         }
@@ -114,7 +241,7 @@
     }
     UNLOCK();
 }
-
+ 
 void ReceiverIR::isr_fall(void) {
     LOCK();
     switch (work.state) {
@@ -145,25 +272,6 @@
                     work.c3 = -1;
                     work.d1 = -1;
                     work.d2 = -1;
-                } else if (InRange(a, RemoteIR::TUS_AEHA * 8) && InRange(b, RemoteIR::TUS_AEHA * 4)) {
-                    /*
-                     * AEHA.
-                     */
-                    data.format = RemoteIR::AEHA;
-                    work.state = Receiving;
-                    data.bitcount = 0;
-                } else if (InRange(a, RemoteIR::TUS_AEHA * 8) && InRange(b, RemoteIR::TUS_AEHA * 8)) {
-                    /*
-                     * AEHA Repeat.
-                     */
-                    data.format = RemoteIR::AEHA_REPEAT;
-                    work.state = Received;
-                    data.bitcount = 0;
-                    work.c1 = -1;
-                    work.c2 = -1;
-                    work.c3 = -1;
-                    work.d1 = -1;
-                    work.d2 = -1;
                 } else {
                     init_state();
                 }
@@ -179,56 +287,11 @@
                     data.buffer[data.bitcount / 8] &= ~(1 << (data.bitcount % 8));
                 }
                 data.bitcount++;
-#if 0
-                /*
-                 * Length of NEC is always 32 bits.
-                 */
-                if (32 <= data.bitcount) {
-                    data.state = Received;
-                    work.c1 = -1;
-                    work.c2 = -1;
-                    work.c3 = -1;
-                    work.d1 = -1;
-                    work.d2 = -1;
-                }
-#else
                 /*
                  * Set timeout for tail detection automatically.
                  */
                 timeout.detach();
                 timeout.attach_us(this, &ReceiverIR::isr_timeout, RemoteIR::TUS_NEC * 5);
-#endif
-            } else if (RemoteIR::AEHA == data.format) {
-                work.d2 = timer.read_us();
-                int a = work.d2 - work.d1;
-                if (InRange(a, RemoteIR::TUS_AEHA * 3)) {
-                    data.buffer[data.bitcount / 8] |= (1 << (data.bitcount % 8));
-                } else if (InRange(a, RemoteIR::TUS_AEHA * 1)) {
-                    data.buffer[data.bitcount / 8] &= ~(1 << (data.bitcount % 8));
-                }
-                data.bitcount++;
-#if 0
-                /*
-                 * Typical length of AEHA is 48 bits.
-                 * Please check a specification of your remote controller if you find a problem.
-                 */
-                if (48 <= data.bitcount) {
-                    data.state = Received;
-                    work.c1 = -1;
-                    work.c2 = -1;
-                    work.c3 = -1;
-                    work.d1 = -1;
-                    work.d2 = -1;
-                }
-#else
-                /*
-                 * Set timeout for tail detection automatically.
-                 */
-                timeout.detach();
-                timeout.attach_us(this, &ReceiverIR::isr_timeout, RemoteIR::TUS_AEHA * 5);
-#endif
-            } else if (RemoteIR::SONY == data.format) {
-                work.d1 = timer.read_us();
             }
             break;
         case Received:
@@ -238,7 +301,7 @@
     }
     UNLOCK();
 }
-
+ 
 void ReceiverIR::isr_rise(void) {
     LOCK();
     switch (work.state) {
@@ -246,16 +309,6 @@
             if (0 <= work.c1) {
                 work.c2 = timer.read_us();
                 int a = work.c2 - work.c1;
-                if (InRange(a, RemoteIR::TUS_SONY * 4)) {
-                    data.format = RemoteIR::SONY;
-                    work.state = Receiving;
-                    data.bitcount = 0;
-                } else {
-                    static const int MINIMUM_LEADER_WIDTH = 150;
-                    if (a < MINIMUM_LEADER_WIDTH) {
-                        init_state();
-                    }
-                }
             } else {
                 init_state();
             }
@@ -263,39 +316,7 @@
         case Receiving:
             if (RemoteIR::NEC == data.format) {
                 work.d1 = timer.read_us();
-            } else if (RemoteIR::AEHA == data.format) {
-                work.d1 = timer.read_us();
-            } else if (RemoteIR::SONY == data.format) {
-                work.d2 = timer.read_us();
-                int a = work.d2 - work.d1;
-                if (InRange(a, RemoteIR::TUS_SONY * 2)) {
-                    data.buffer[data.bitcount / 8] |= (1 << (data.bitcount % 8));
-                } else if (InRange(a, RemoteIR::TUS_SONY * 1)) {
-                    data.buffer[data.bitcount / 8] &= ~(1 << (data.bitcount % 8));
-                }
-                data.bitcount++;
-#if 0
-                /*
-                 * How do I know the correct length? (6bits, 12bits, 15bits, 20bits...)
-                 * By a model only?
-                 * Please check a specification of your remote controller if you find a problem.
-                 */
-                if (12 <= data.bitcount) {
-                    data.state = Received;
-                    work.c1 = -1;
-                    work.c2 = -1;
-                    work.c3 = -1;
-                    work.d1 = -1;
-                    work.d2 = -1;
-                }
-#else
-                /*
-                 * Set timeout for tail detection automatically.
-                 */
-                timeout.detach();
-                timeout.attach_us(this, &ReceiverIR::isr_timeout, RemoteIR::TUS_SONY * 4);
-#endif
-            }
+            } 
             break;
         case Received:
             break;
@@ -304,20 +325,10 @@
     }
     UNLOCK();
 }
-
+ 
 void ReceiverIR::isr_timeout(void) {
     LOCK();
-#if 0
-    printf("# TIMEOUT [c1=%d, c2=%d, c3=%d, d1=%d, d2=%d, state=%d, format=%d, bitcount=%d]\n",
-           work.c1,
-           work.c2,
-           work.c3,
-           work.d1,
-           work.d2,
-           work.state,
-           data.format,
-           data.bitcount);
-#endif
+
     if (work.state == Receiving) {
         work.state = Received;
         work.c1 = -1;
@@ -328,3 +339,4 @@
     }
     UNLOCK();
 }
+ 
\ No newline at end of file