主導機 mbed用のプログラムです 改良しました

Dependencies:   mbed

Fork of F3RC_syudou_master_3 by 日記

Revision:
12:1254070d444a
Parent:
11:86d717718dbf
diff -r 86d717718dbf -r 1254070d444a User.cpp
--- a/User.cpp	Mon Feb 15 02:23:25 2016 +0000
+++ b/User.cpp	Sun Nov 06 01:25:39 2016 +0000
@@ -6,21 +6,36 @@
 
 #include "mbed.h"
 
+
 int RSX,RSY,LSX,LSY,BSU,BSL;
 //これより下に関数外に書く要素を記入する
-PwmOut led1(LED1);
-PwmOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
+
+//SPI通信の宣言
+SPI spi(p5, p6, p7); // mosi, miso, sclk
+DigitalOut cs(p8);
 
-void UserLoopSetting(){
+/*
+2進数の足回り、昇降機構、開閉機構の情報 
+valmasterは全ての情報の合計
+*/
+int valArm = 0;
+int valUpDown = 0;
+int valLeg = 0;
+int valMaster = 0;
+
+DigitalOut led (LED1);
+
+
+void UserLoopSetting()
+{
     //一度だけ行いたい初期設定をここに書く
-    led1.period_ms(20);
+
 }
 
-void UserLoop(char n,const u8* data){
+void UserLoop(char n,const u8* data)
+{
     u16 ButtonState;
-    if(n==0){//有線Ps3USB.cpp
+    if(n==0) { //有線Ps3USB.cpp
         RSX = ((ps3report*)data)->RightStickX;
         RSY = ((ps3report*)data)->RightStickY;
         LSX = ((ps3report*)data)->LeftStickX;
@@ -29,7 +44,7 @@
         BSL = (u8)(((ps3report*)data)->ButtonState >> 8);
         //ボタンの処理
         ButtonState =  ((ps3report*)data)->ButtonState;
-    }else {//無線TestShell.cpp
+    } else {//無線TestShell.cpp
         RSX = ((ps3report*)(data + 1))->RightStickX;
         RSY = ((ps3report*)(data + 1))->RightStickY;
         LSX = ((ps3report*)(data + 1))->LeftStickX;
@@ -40,17 +55,78 @@
         ButtonState =  ((ps3report*)(data + 1))->ButtonState;
     }
     //ここより下にプログラムを書く
-    //データ取得例
+    //送る情報は2進数で考えた後、10進数に直して送る。(2進数での書き込みができないため)
+
+/*
+SPI通信の設定
+通信量が多い場合、frequencyの値を上げると問題が解決するかもしれない
+*/
+    spi.format(16,3);
+    spi.frequency(1000000);
+    cs = 0;
+
     
-    if((ButtonState >> BUTTONCIRCLE)&1) {
-        //○が押されたとき
+    ///////アーム部分/////////
+    if(((ButtonState >> BUTTONR1)&1 == 1)&&((ButtonState >> BUTTONL1)&1 == 1)) {//両方閉じる
+        valArm =          3584;//0111000000000;
+    } else if((ButtonState >> BUTTONR1)&1 == 1) {//右側閉じる
+        valArm =          2560;//0101000000000;
+    } else if((ButtonState >> BUTTONL1)&1 == 1) {//左側閉じる
+        valArm =          3072;//0110000000000;
+    } else if(((ButtonState >> BUTTONR2)&1 == 1)&&((ButtonState >> BUTTONL2)&1 == 1)) {//両方開く
+        valArm =          1536;//0011000000000;
+    } else if ((ButtonState >> BUTTONR2)&1 == 1) {//右側開く
+        valArm =           512;//0001000000000;
+    } else if((ButtonState >> BUTTONL2)&1 == 1) {//左側開く
+        valArm =          1024;//0010000000000;
+    } else {
+        valArm =             0;//0000000000000;
+    }
+    if((ButtonState >> BUTTONCIRCLE)&1 == 1) { //開く時、○ボタンを押すとスピードが上がる
+        valArm = valArm + 4096;//1000000000000;
+    }
+
+//昇降機構
+    if((ButtonState >> BUTTONSQUARE)&1 == 1) {//big up
+        valUpDown = 24;//11000;
+    } else if((ButtonState >> BUTTONCROSS)&1 == 1) {//big down
+        valUpDown = 16;//10000;
+    } else {//not moving
+        valUpDown = 0;//00000;
     }
+
+//足回り
+    if(RSY < 110 && LSY < 110) { //forward
+        valLeg =480; //111100000;
+        led = 1;
+    } else if( RSY < 140 && RSY >= 110 && LSY < 110) { //small turn right1
+        valLeg = 384;//110000000;
+    } else if(RSY >= 140 && LSY < 110) { //big turn right
+        valLeg = 416;//110100000;
+    } else if(RSY >= 140 && LSY >= 110 && LSY < 140) { //small turn right2
+        valLeg = 32;//000100000;
+    } else if(RSY < 110 && LSY >= 110 && LSY < 140) { //small turn left1
+        valLeg = 96;//001100000;
+    } else if(RSY < 110 && LSY >= 140) { //big turn left
+        valLeg = 224;//011100000;
+    } else if(RSY < 140 && RSY >= 110 && LSY >=140) { //small turn left2
+        valLeg = 128;//010000000;
+    } else if(RSY >= 140 && LSY >= 140) {//back
+        valLeg = 160;//010100000;
+    } else if((ButtonState >> BUTTONUP)&1 == 1) { //微調整前進
+        valLeg = 256;//100000000;
+    } else if((ButtonState >> BUTTONDOWN)&1 == 1) { //微調整後進
+        valLeg = 288;//100100000
+    } else if((ButtonState >> BUTTONRIGHT)&1 == 1) { //微調整右折
+        valLeg = 320;//101000000
+    } else if((ButtonState >> BUTTONLEFT)&1 == 1) { //微調整左折
+        valLeg = 352;//101100000
+    } else {
+        valLeg = 0;//000000000;
+    }
+
+    valMaster = valLeg + valUpDown  + valArm;//この値をSPI通信で送る
    
-        
-    led1=LSX/256.0f;
-    led2=LSY/256.0f;
-    led3=ButtonState & 0x0400;  //L1の状態
-    led4=ButtonState & 0x0800;  //R1の状態
-    //値の取得はps3.hを参照
-        
-}
\ No newline at end of file
+    spi.write(valMaster);
+    cs = 1;
+}