主導機 mbed用のプログラムです

Dependencies:   mbed

Fork of F3RC_syudou_master by F3RC1班

User.cpp

Committer:
hokkyokuh2
Date:
2016-11-06
Revision:
12:1254070d444a
Parent:
11:86d717718dbf

File content as of revision 12:1254070d444a:

#include "Utils.h"
#include "USBHost.h"
#include "hci.h"
#include "ps3.h"
#include "User.h"

#include "mbed.h"


int RSX,RSY,LSX,LSY,BSU,BSL;
//これより下に関数外に書く要素を記入する

//SPI通信の宣言
SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut cs(p8);

/*
2進数の足回り、昇降機構、開閉機構の情報 
valmasterは全ての情報の合計
*/
int valArm = 0;
int valUpDown = 0;
int valLeg = 0;
int valMaster = 0;

DigitalOut led (LED1);


void UserLoopSetting()
{
    //一度だけ行いたい初期設定をここに書く

}

void UserLoop(char n,const u8* data)
{
    u16 ButtonState;
    if(n==0) { //有線Ps3USB.cpp
        RSX = ((ps3report*)data)->RightStickX;
        RSY = ((ps3report*)data)->RightStickY;
        LSX = ((ps3report*)data)->LeftStickX;
        LSY = ((ps3report*)data)->LeftStickY;
        BSU = (u8)(((ps3report*)data)->ButtonState & 0x00ff);
        BSL = (u8)(((ps3report*)data)->ButtonState >> 8);
        //ボタンの処理
        ButtonState =  ((ps3report*)data)->ButtonState;
    } else {//無線TestShell.cpp
        RSX = ((ps3report*)(data + 1))->RightStickX;
        RSY = ((ps3report*)(data + 1))->RightStickY;
        LSX = ((ps3report*)(data + 1))->LeftStickX;
        LSY = ((ps3report*)(data + 1))->LeftStickY;
        BSU = (u8)(((ps3report*)(data + 1))->ButtonState & 0x00ff);
        BSL = (u8)(((ps3report*)(data + 1))->ButtonState >> 8);
        //ボタンの処理
        ButtonState =  ((ps3report*)(data + 1))->ButtonState;
    }
    //ここより下にプログラムを書く
    //送る情報は2進数で考えた後、10進数に直して送る。(2進数での書き込みができないため)

/*
SPI通信の設定
通信量が多い場合、frequencyの値を上げると問題が解決するかもしれない
*/
    spi.format(16,3);
    spi.frequency(1000000);
    cs = 0;

    
    ///////アーム部分/////////
    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通信で送る
   
    spi.write(valMaster);
    cs = 1;
}