MDテスト用プログラム

Dependencies:   FastPWM GMD_template_lib mbed

Fork of NewMD2 by tarou yamada

Revision:
7:9671d3ae163e
Parent:
6:004e6174445a
Child:
8:ad41522dde78
--- a/main.cpp	Sun Jul 10 13:17:14 2016 +0000
+++ b/main.cpp	Wed Jul 13 09:02:41 2016 +0000
@@ -19,32 +19,36 @@
 }
 
 int main(){
+    const float timeout_ms = 300;
     I2CSlave i2c(dp5, dp27);
+    Timer timer;
     i2c.address(read_address());
     basic_motor_driver md(dp1, dp2, dp9);
     
+    timer.start();
+    
     while (true){
         char buf;
         switch (i2c.receive()){
             case I2CSlave::ReadAddressed:
+                timer.reset();
                 break;
             
             case I2CSlave::WriteGeneral:
+                timer.reset();
                 break;
             
             case I2CSlave::WriteAddressed:
+                timer.reset();
                 i2c.read(&buf, 1);
-                
-                /*
-                float p = (buf & 0x80) ? -1.0f : 1.0f;
-                p *= (buf & 0x7F);
-                p *= 1.0f / 0x7F;
-                */
-                
                 md.set(static_cast<int8_t>(buf) / 127.0f);
                 break;
                 
             case I2CSlave::NoData:
+                if (timer.read_ms() > timeout_ms) {
+                    // I2Cの通信がタイムアウトしたならブレーキ
+                    md.release();
+                }
                 break;
         }
     }