ビーンバック回収、テープLED、美少女ボイス担当マイコンのプログラム

Dependencies:   mbed SBDBT arrc_mbed play_mp3

Files at this revision

API Documentation at this revision

Comitter:
sopuranoaruto
Date:
Sat Mar 19 12:38:45 2022 +0000
Parent:
5:200723c2d111
Commit message:

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 200723c2d111 -r d29a019b96cf main.cpp
--- a/main.cpp	Fri Mar 11 05:38:21 2022 +0000
+++ b/main.cpp	Sat Mar 19 12:38:45 2022 +0000
@@ -1,76 +1,57 @@
 #include "mbed.h"
 #include "neopixel.h"
 #include "mp3.hpp"
+#include "rotary_inc.hpp"
 #include "scrp_slave.hpp"
 
 Serial pc(USBTX,USBRX);
 ScrpSlave slave(PC_12,PD_2,PH_1,SERIAL_TX,SERIAL_RX,5);
+RotaryInc rotaryx(PA_7,PA_6,30 * M_PI,512);
+RotaryInc rotaryy(PC_4,PA_13,30 * M_PI,512);
+PwmOut motor1[2] = {
+    PwmOut(PB_7),
+    PwmOut(PB_9)
+};
+PwmOut motor2[2] = {
+    PwmOut(PB_6),
+    PwmOut(PB_8)
+};
 NeoPixelOut npx(PB_0,16);
 Playmp3 mp3(PA_0,PA_1);
-bool areamode = 0;
 bool colormode = 0;
 bool stop = 0;
-bool start = 0;
-
+bool up = 1;
+bool down = 0;
+bool front = 0;
+bool back = 0;
+bool slope = 0;
+bool move = 0;
+int num = 0;
 int i = 0;
 
-//農作物回収 1
+//ボイス 2
 bool play1(int rx_data,int &tx_data)
 {
+    //農作物回収 1
     if(rx_data == 1) {
         mp3.set_number_of_tracks(1);
     }
-    return true;
-}
-
-//農作物設置 2
-bool play2(int rx_data,int &tx_data)
-{
-    if(rx_data == 1) {
+    //農作物設置 2
+    else if(rx_data == 2) {
         mp3.set_number_of_tracks(2);
     }
-    return true;
-}
-
-//ビーンバック回収 3
-bool play3(int rx_data,int &tx_data)
-{
-    if(rx_data == 1) {
+    //ビーンバック回収 3
+    else if(rx_data == 3) {
         mp3.set_number_of_tracks(3);
     }
-    return true;
-}
-
-//自動運転モードに移行 4
-bool play4(int rx_data,int &tx_data)
-{
-    if(rx_data == 1) {
+    //自動運転モードに移行 4
+    else if(rx_data == 4) {
         mp3.set_number_of_tracks(4);
     }
-    return true;
-}
-
-//ビーンバック発射 5
-bool play5(int rx_data,int &tx_data)
-{
-    if(rx_data == 1) {
+    //ビーンバック発射 5
+    else if(rx_data == 5) {
         mp3.set_number_of_tracks(5);
-    }
-    return true;
-}
-
-//スタート 6
-bool get_start(int rx_data,int &tx_data)
-{
-    if(rx_data == 0) {
-        if(start == 1) {
-            start = 0;
-        }
-    } else {
-        if(start == 0) {
-            start = 1;
-        }
-    }
+    } else {}
     return true;
 }
 
@@ -103,16 +84,17 @@
     return true;
 }
 
-//エリアチェンジ 52
-bool get_areachange(int rx_data,int &tx_data)
+//ビーンバック回収 1
+bool getting(int rx_data,int &tx_data)
 {
     if(rx_data == 0) {
-        if(areamode == 1) {
-            areamode = 0;
+        if(move == 1) {
+            move = 0;
         }
     } else {
-        if(areamode == 0) {
-            areamode = 1;
+        if(move == 0) {
+            move = 1;
+            num++;
         }
     }
     return true;
@@ -120,24 +102,14 @@
 
 int main()
 {
-    //農作物回収
-    slave.addCMD(1,play1);
-    //農作物設置
-    slave.addCMD(2,play2);
     //ビーンバック回収
-    slave.addCMD(3,play3);
-    //自動運転モードに移行
-    slave.addCMD(4,play4);
-    //ビーンバック発射
-    slave.addCMD(5,play5);
-    //スタート
-    slave.addCMD(6,get_start);
+    slave.addCMD(1,getting);
+    //ボイス
+    slave.addCMD(2,play1);
     //モード変更
     slave.addCMD(50,get_changemode);
     //非常停止
     slave.addCMD(51,get_stop);
-    //エリア変更
-    slave.addCMD(52,get_areachange);
     //光の強さ
     npx.global_scale = 0.05f;
     //信号来ていないときは光らない
@@ -150,55 +122,139 @@
     while(1) {
         //非常停止解除
         if(stop == 0) {
-            if(start == 0) {
-                //フィールド赤
-                if(areamode == 0) {
-                    for(i = 0; i < npx.numPixels(); i++) {
-                        npx.setPixelColor(i,0xFF0000);
-                        npx.show();
-                        wait(0.05);
+            double distance = rotaryx.get();
+            double height = rotaryy.get();
+            pc.printf("num %d move %d\n",num,move);
+            //pc.printf("distance %lf  height %lf up %d down %d front %d back %d\n",distance,height,up,down,front,back);
+            wait(0.01);
+            //手動モード 黄色
+            if(colormode == 0) {
+                for(i = 0; i < npx.numPixels(); i++) {
+                    npx.setPixelColor(i,0xFFF100);
+                    npx.show();
+                    wait(0.01);
+                    if(i == npx.numPixels()) {
+                        i = 0;
                     }
-                    pc.printf("red area\n");
-                    //フィールド青
-                } else if(areamode == 1) {
-                    for(i = 0; i < npx.numPixels(); i++) {
-                        npx.setPixelColor(i,0x0000FF);
-                        npx.show();
-                        wait(0.05);
+                }
+                pc.printf("control\n");
+                //自動モード 緑
+            } else if(colormode == 1) {
+                for(i = 0; i < npx.numPixels(); i++) {
+                    npx.setPixelColor(i,0x00FF00);
+                    npx.show();
+                    wait(0.01);
+                    if(i == npx.numPixels()) {
+                        i = 0;
                     }
-                    pc.printf("bule area\n");
                 }
-            } else {
-                //手動モード 黄色
-                if(colormode == 0) {
-                    for(i = 0; i < npx.numPixels(); i++) {
-                        npx.setPixelColor(i,0xFFF100);
-                        npx.show();
-                        wait(0.05);
-                        if(i == npx.numPixels()) {
-                            i = 0;
+                pc.printf("auto\n");
+            }
+            if(num == 0) {}
+            else {
+                switch(num%2) {
+                    case 1:
+                        if(distance >= -300 && up == 1) {
+                            motor1[0] = 0.35;
+                            motor2[0] = 0;
+                            motor1[1] = 0;
+                            motor2[1] = 0;
+                        }
+
+                        if(distance <= -300 && up == 1) {
+                            up = 0;
+                            down = 0;
+                            front = 1;
+                            back = 0;
+                            slope = 0;
+                        }
+
+                        if(front == 1 && height >= -127) {
+                            motor1[0] = 0;
+                            motor2[0] = 0;
+                            motor1[1] = 0.2;
+                            motor2[1] = 0;
+                        }
+
+                        if(height <= -127 && front == 1) {
+                            up = 0;
+                            down = 1;
+                            front = 0;
+                            back = 0;
+                            slope = 0;
+                        }
+
+                        if(distance  <= -300 && height <= -127) {
+                            motor1[0] = 0;
+                            motor2[0] = 0;
+                            motor1[1] = 0;
+                            motor2[1] = 0;
+                        }
+                        break;
+
+                    case 0:
+                        if(distance <= -90 && down == 1) {
+                            motor1[0] = 0;
+                            motor2[0] = 0.35;
+                            motor1[1] = 0;
+                            motor2[1] = 0;
                         }
-                    }
-                    pc.printf("control\n");
-                    //自動モード 緑
-                } else if(colormode == 1) {
-                    for(i = 0; i < npx.numPixels(); i++) {
-                        npx.setPixelColor(i,0x00FF00);
-                        npx.show();
-                        wait(0.05);
-                        if(i == npx.numPixels()) {
-                            i = 0;
+
+                        if(distance >= -90 && down == 1) {
+                            up = 0;
+                            down = 0;
+                            front = 0;
+                            back = 1;
+                            slope = 0;
+                        }
+
+                        if(distance <= 0 && back == 1) {
+                            motor1[0] = 0;
+                            motor2[0] = 0.2;
+                            motor1[1] = 0;
+                            motor2[1] = 0.2;
+                        }
+
+                        if(distance >= 0 && back == 1) {
+                            up = 0;
+                            down = 0;
+                            front = 0;
+                            back = 0;
+                            slope = 1;
                         }
-                    }
-                    pc.printf("auto\n");
+
+                        if(slope == 1 && height <= 1) {
+                            motor1[0] = 0;
+                            motor2[0] = 0;
+                            motor1[1] = 0;
+                            motor2[1] = 0.2;
+                        }
+
+                        if(height >= 1 && slope == 1) {
+                            up = 1;
+                            down = 0;
+                            front = 0;
+                            back = 0;
+                            slope = 0;
+                        }
+
+                        if(distance >= 1 && height >= 1) {
+                            motor1[0] = 0;
+                            motor2[0] = 0;
+                            motor1[1] = 0;
+                            motor2[1] = 0;
+                        }
+                        break;
+                    default:
+                        break;
                 }
-            }
-            //非常停止
+            }    
+        //非常停止
         } else if(stop == 1) {
             for(i = 0; i < npx.numPixels(); i++) {
                 npx.setPixelColor(i,0x000000);
                 npx.show();
-                wait(0.05);
+                wait(0.01);
                 if(i == npx.numPixels()) {
                     i = 0;
                 }