マザー 20151028 XBOX-XBEE

Dependencies:   mbed

Fork of Nucleo_PS3_Jikken_XBOX by 2015 robotic contest arakawa A

Committer:
nodoame
Date:
Sun Oct 04 07:34:44 2015 +0000
Revision:
14:aaf2ae25e508
Parent:
13:5bccc2d600eb
Child:
15:c941d06092ec
????????????????; 20151004

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nodoame 0:dc587be179e9 1 #include "mbed.h"
nodoame 0:dc587be179e9 2 #include "math.h"
nodoame 0:dc587be179e9 3 #define M_PI 3.1415926535897932384626433832795
nodoame 0:dc587be179e9 4 #define trigL 0
nodoame 0:dc587be179e9 5 #define trigR 1
nodoame 0:dc587be179e9 6 #define btL 2
nodoame 0:dc587be179e9 7 #define btR 3
nodoame 0:dc587be179e9 8 #define btTri 4
nodoame 0:dc587be179e9 9 #define btSph 5
nodoame 0:dc587be179e9 10 #define btCro 6
nodoame 0:dc587be179e9 11 #define btSqa 7
nodoame 5:428b7ac86810 12 #define presUp 0
nodoame 5:428b7ac86810 13 #define presRight 1
nodoame 5:428b7ac86810 14 #define presDown 2
nodoame 5:428b7ac86810 15 #define presLeft 3
nodoame 4:bae4e2d103d4 16 #define devideNum 32
nodoame 4:bae4e2d103d4 17 #define ED 132
nodoame 6:5171a8245b57 18 #define TD 144//208
nodoame 5:428b7ac86810 19 #define EL 134
nodoame 5:428b7ac86810 20 #define TL 200
nodoame 6:5171a8245b57 21 #define TNR 138//204
nodoame 6:5171a8245b57 22 #define TNL 135
nodoame 11:1ca2498815c9 23 #define debugFlag 1
nodoame 11:1ca2498815c9 24 #define stopCount 33
nodoame 0:dc587be179e9 25
nodoame 0:dc587be179e9 26 Serial pc(SERIAL_TX, SERIAL_RX);
nodoame 1:a3bfd422ea9e 27 //Serial Dev(D8,D2);//コントローラー
nodoame 3:c290afbacd1c 28 Serial Dev(PC_6,PA_12);//コントローラー
nodoame 1:a3bfd422ea9e 29 RawSerial Mechanum(D8,D2);//メカナム
nodoame 0:dc587be179e9 30 I2C i2c(D14,D15);//sda,scl
nodoame 5:428b7ac86810 31 //const int airAddr=0x20;
nodoame 0:dc587be179e9 32 const int airAddr=0xA0;
nodoame 5:428b7ac86810 33 const int valveAddr=0xC0;
nodoame 1:a3bfd422ea9e 34 BusOut dI2c(PC_0,PC_1,PC_2,PC_3);//i2cデバッグ
nodoame 0:dc587be179e9 35
nodoame 0:dc587be179e9 36 int Bflag = 0;
nodoame 0:dc587be179e9 37 DigitalOut myled(LED1);// ,test1(PB_7) ,test2(PA_15) ,test3(PC_13) ,test4(PC_12);
nodoame 4:bae4e2d103d4 38 PwmOut Blue(D5),Green(D4),Red(D3);
nodoame 4:bae4e2d103d4 39 //PwmOut kRed(A3),kBlue(A1),kGreen(A0);
nodoame 4:bae4e2d103d4 40 PwmOut kRed(A3),kBlue(PA_15),kGreen(PB_7);
nodoame 5:428b7ac86810 41 #define N 7
nodoame 0:dc587be179e9 42 char DATA[N];
nodoame 0:dc587be179e9 43 void rotate(double setAngle);
nodoame 0:dc587be179e9 44 void speed();
nodoame 11:1ca2498815c9 45 int stopCounter=0;
nodoame 1:a3bfd422ea9e 46
nodoame 5:428b7ac86810 47 void num()//割り込まれたら7回受信
nodoame 0:dc587be179e9 48 {
nodoame 0:dc587be179e9 49 if(Dev.getc()==114){
nodoame 0:dc587be179e9 50 //printf("ReceiveCommand\r\n");
nodoame 0:dc587be179e9 51 for(int i = 0 ;i<N ;i++ )
nodoame 0:dc587be179e9 52 {
nodoame 0:dc587be179e9 53 DATA[i]=Dev.getc();
nodoame 0:dc587be179e9 54 }
nodoame 0:dc587be179e9 55 }
nodoame 11:1ca2498815c9 56 stopCounter=0;
nodoame 0:dc587be179e9 57 }
nodoame 0:dc587be179e9 58
nodoame 0:dc587be179e9 59 bool getBt(int num)
nodoame 0:dc587be179e9 60 {
nodoame 0:dc587be179e9 61 return (DATA[0]>>num)%2;
nodoame 0:dc587be179e9 62 }
nodoame 0:dc587be179e9 63
nodoame 5:428b7ac86810 64 bool getPress(int num)
nodoame 5:428b7ac86810 65 {
nodoame 5:428b7ac86810 66 return (DATA[6]>>num)%2;
nodoame 5:428b7ac86810 67 }
nodoame 5:428b7ac86810 68
nodoame 0:dc587be179e9 69 double pointToDeg(double y,double x,int threshold)//座標から角度を求める関数thresholdはしきい値
nodoame 0:dc587be179e9 70 {
nodoame 0:dc587be179e9 71 if(abs((int)(y-128))<threshold)
nodoame 0:dc587be179e9 72 y=128;
nodoame 0:dc587be179e9 73 if(abs((int)(x-128))<threshold)
nodoame 0:dc587be179e9 74 x=128;
nodoame 0:dc587be179e9 75 return atan2(-(y-128),x-128)/M_PI*180;//y軸反転、/PI*180でRadからDeg変換
nodoame 0:dc587be179e9 76 }
nodoame 0:dc587be179e9 77
nodoame 0:dc587be179e9 78 double devidePoint(double y,double x,int threshold,float devide)
nodoame 0:dc587be179e9 79 {
nodoame 0:dc587be179e9 80 double tempX,tempY,dev;//仮のX,Yと分けた角度
nodoame 0:dc587be179e9 81 tempX=(x-128);tempY=(y-128);//それぞれ-128
nodoame 0:dc587be179e9 82 if(abs((int)tempY)<threshold)//しきい値以下なら0にする
nodoame 0:dc587be179e9 83 tempY=0;
nodoame 0:dc587be179e9 84 if(abs((int)tempX)<threshold)
nodoame 0:dc587be179e9 85 tempX=0;
nodoame 0:dc587be179e9 86 if(tempX==0&&tempY==0)
nodoame 0:dc587be179e9 87 dev=360;
nodoame 0:dc587be179e9 88 //printf("X:%lfY:%lfDev:%lf\r\n",tempX,tempY,dev);
nodoame 0:dc587be179e9 89 else
nodoame 0:dc587be179e9 90 dev=atan2(tempX,-tempY)/M_PI*180;//軸を逆にして代入、Y軸反転
nodoame 0:dc587be179e9 91 return ((dev<0)?(dev+360):(dev))/devide;//分割
nodoame 0:dc587be179e9 92 }
nodoame 0:dc587be179e9 93
nodoame 4:bae4e2d103d4 94 int valueForMechanum(int deg,int devide,bool triL,bool triR,bool btSpd)
nodoame 0:dc587be179e9 95 {
nodoame 0:dc587be179e9 96 bool stpFlag=false,spnFlag=false;//ストップフラグ、スピンフラグ
nodoame 0:dc587be179e9 97 int val=0,i;//値、For用
nodoame 0:dc587be179e9 98 for(i=0;i<6;i++){
nodoame 0:dc587be179e9 99 switch(i){
nodoame 0:dc587be179e9 100 case 0:
nodoame 4:bae4e2d103d4 101 if(deg!=devide||(triL||triR))//スピンかニュートラル以外なら
nodoame 0:dc587be179e9 102 val=1;
nodoame 0:dc587be179e9 103 else
nodoame 0:dc587be179e9 104 stpFlag=true;//ストップ
nodoame 0:dc587be179e9 105 break;
nodoame 0:dc587be179e9 106
nodoame 0:dc587be179e9 107 case 1:
nodoame 0:dc587be179e9 108 if(stpFlag)
nodoame 4:bae4e2d103d4 109 val++;
nodoame 0:dc587be179e9 110 else
nodoame 0:dc587be179e9 111 val+=btSpd;//スピード
nodoame 0:dc587be179e9 112 break;
nodoame 0:dc587be179e9 113
nodoame 0:dc587be179e9 114 case 2:
nodoame 0:dc587be179e9 115 if(triL||triR){//トリガーのどちらかがONなら
nodoame 0:dc587be179e9 116 val++;
nodoame 0:dc587be179e9 117 spnFlag=true;//スピンフラグON
nodoame 0:dc587be179e9 118 }
nodoame 0:dc587be179e9 119 break;
nodoame 0:dc587be179e9 120
nodoame 0:dc587be179e9 121 case 5:
nodoame 4:bae4e2d103d4 122 val=val<<2;//5bit目から7bit目へ
nodoame 0:dc587be179e9 123 if(spnFlag)//トリガーのどちらかの値
nodoame 0:dc587be179e9 124 val+=(triL)?1:0;
nodoame 4:bae4e2d103d4 125 else if(!stpFlag)//ジョイスティックの値
nodoame 0:dc587be179e9 126 val+=deg;
nodoame 0:dc587be179e9 127 break;
nodoame 0:dc587be179e9 128 }
nodoame 0:dc587be179e9 129 if(i<5)
nodoame 0:dc587be179e9 130 val=val<<1;
nodoame 0:dc587be179e9 131 }
nodoame 0:dc587be179e9 132 //printf("val:%ddeg:%dtrigL:%dtrigR:%dbtSp:%d\r\n",val,deg,triL,triR,btSpd);
nodoame 0:dc587be179e9 133 return val;
nodoame 0:dc587be179e9 134 }
nodoame 0:dc587be179e9 135
nodoame 0:dc587be179e9 136 void detectPole()
nodoame 0:dc587be179e9 137 {
nodoame 9:489a085d1703 138 bool btFlag=false,scFlag=false;
nodoame 5:428b7ac86810 139 int vector=TD;
nodoame 4:bae4e2d103d4 140 Blue=0.0f;Green=0.0f,Red=0.8f;
nodoame 9:489a085d1703 141 double deg=32;
nodoame 0:dc587be179e9 142 while(true)
nodoame 0:dc587be179e9 143 {
nodoame 14:aaf2ae25e508 144 //スイッチが押された時
nodoame 5:428b7ac86810 145 if(((int)DATA[1]&12)==12&&!btFlag){
nodoame 5:428b7ac86810 146 wait(0.001f);
nodoame 5:428b7ac86810 147 Mechanum.putc(64);
nodoame 4:bae4e2d103d4 148 btFlag=true;
nodoame 4:bae4e2d103d4 149 Blue=0.0f;Green=0.8f;Red=0;
nodoame 9:489a085d1703 150 vector=64;
nodoame 14:aaf2ae25e508 151 }
nodoame 11:1ca2498815c9 152
nodoame 11:1ca2498815c9 153 //方向選択
nodoame 14:aaf2ae25e508 154 else if((int)deg==devideNum)
nodoame 9:489a085d1703 155 {
nodoame 9:489a085d1703 156 deg=devidePoint((double)DATA[3],(double)DATA[2],15,360/devideNum);
nodoame 9:489a085d1703 157 pc.printf("deg%lf\r\n",deg);
nodoame 9:489a085d1703 158 if((int)deg!=devideNum){
nodoame 9:489a085d1703 159 vector=(deg<17)?(TNL):(TNR);
nodoame 9:489a085d1703 160 pc.printf("vector%d\r\n",vector);
nodoame 9:489a085d1703 161 scFlag=true;
nodoame 9:489a085d1703 162 }
nodoame 9:489a085d1703 163 }
nodoame 4:bae4e2d103d4 164 //レーザーが反応した時
nodoame 9:489a085d1703 165 else if(!(int)(DATA[1]&0x02)&&btFlag&&scFlag/*!((int)DATA[1]&2==2)&&btFlag*/){
nodoame 4:bae4e2d103d4 166 vector=64;
nodoame 0:dc587be179e9 167 break;
nodoame 0:dc587be179e9 168 }
nodoame 4:bae4e2d103d4 169
nodoame 0:dc587be179e9 170 Mechanum.putc(vector);
nodoame 0:dc587be179e9 171 wait(0.001f);
nodoame 0:dc587be179e9 172 }
nodoame 4:bae4e2d103d4 173 btFlag=false;
nodoame 5:428b7ac86810 174 Blue=0;Green=0;Red=0;
nodoame 0:dc587be179e9 175 }
nodoame 0:dc587be179e9 176
nodoame 0:dc587be179e9 177
nodoame 5:428b7ac86810 178 bool shotToDenziben(char reg, char data ,int size,int addr)
nodoame 0:dc587be179e9 179 {
nodoame 5:428b7ac86810 180 char REG[2] = {reg,size};
nodoame 5:428b7ac86810 181 char DATA[2]={data,0};
nodoame 5:428b7ac86810 182 bool A = i2c.write(addr,REG,2);
nodoame 5:428b7ac86810 183 A|= i2c.write(addr,DATA,size);
nodoame 1:a3bfd422ea9e 184 return A;
nodoame 0:dc587be179e9 185 }
nodoame 0:dc587be179e9 186
nodoame 5:428b7ac86810 187 char getshot(char reg)
nodoame 5:428b7ac86810 188 {
nodoame 5:428b7ac86810 189 char input=0;
nodoame 5:428b7ac86810 190 char DATA[2] = {reg,1};
nodoame 5:428b7ac86810 191 i2c.write(airAddr,DATA,2);
nodoame 5:428b7ac86810 192 wait(0.005f);
nodoame 5:428b7ac86810 193 i2c.read(airAddr,&input,1);
nodoame 5:428b7ac86810 194 return input;
nodoame 5:428b7ac86810 195 }
nodoame 5:428b7ac86810 196
nodoame 12:6445a3c3d34c 197 void setFCLED(double RED,double GREEN,double BLUE)
nodoame 12:6445a3c3d34c 198 {
nodoame 12:6445a3c3d34c 199 Red=RED;
nodoame 12:6445a3c3d34c 200 Green=GREEN;
nodoame 12:6445a3c3d34c 201 Blue=BLUE;
nodoame 12:6445a3c3d34c 202 }
nodoame 12:6445a3c3d34c 203
nodoame 4:bae4e2d103d4 204 int main() {
nodoame 0:dc587be179e9 205 pc.baud(230400);
nodoame 0:dc587be179e9 206 Dev.baud(921600);
nodoame 0:dc587be179e9 207 Mechanum.baud(230400);
nodoame 5:428b7ac86810 208 DigitalIn bt(USER_BUTTON);
nodoame 0:dc587be179e9 209 Dev.attach(num,Serial::RxIrq);//受信割り込み設定
nodoame 1:a3bfd422ea9e 210 Green=0;Blue=0;Red=0;
nodoame 12:6445a3c3d34c 211 double gValue=0,bValue=0,rValue=0;
nodoame 3:c290afbacd1c 212 DigitalOut l(PB_2);
nodoame 3:c290afbacd1c 213 int sqf=0;
nodoame 0:dc587be179e9 214 double deg;
nodoame 0:dc587be179e9 215 int val;
nodoame 1:a3bfd422ea9e 216 i2c.frequency(400000);
nodoame 12:6445a3c3d34c 217 setFCLED(0,0.8,0.8);
nodoame 5:428b7ac86810 218 bool obonFlag=false,pLeftF=false,pRightF=false,btCrF=false;
nodoame 0:dc587be179e9 219
nodoame 0:dc587be179e9 220 while(true)
nodoame 12:6445a3c3d34c 221 {
nodoame 5:428b7ac86810 222 //ポール検出モード
nodoame 0:dc587be179e9 223 if(getBt(btTri)&&getBt(btSph)){
nodoame 0:dc587be179e9 224 detectPole();
nodoame 0:dc587be179e9 225 //printf("DetectMode\r\n");
nodoame 0:dc587be179e9 226 }
nodoame 3:c290afbacd1c 227
nodoame 5:428b7ac86810 228 //射出
nodoame 5:428b7ac86810 229 if(getBt(btL)&&obonFlag){
nodoame 5:428b7ac86810 230 kRed=shotToDenziben(0x02,39,1,airAddr);
nodoame 5:428b7ac86810 231 wait(0.1f);
nodoame 5:428b7ac86810 232 kRed=0;
nodoame 5:428b7ac86810 233 }
nodoame 5:428b7ac86810 234
nodoame 5:428b7ac86810 235 //射出
nodoame 5:428b7ac86810 236 if(getBt(btR)&&obonFlag){
nodoame 5:428b7ac86810 237 kRed=shotToDenziben(0x02,23,1,airAddr);
nodoame 5:428b7ac86810 238 wait(0.1f);
nodoame 3:c290afbacd1c 239 //printf("shot:%d\r\n",i2cS=i2c.write(airAddr,cmd,2));
nodoame 3:c290afbacd1c 240 //i2c.write(airAddr,cmd,2);
nodoame 4:bae4e2d103d4 241 kRed=0;
nodoame 3:c290afbacd1c 242 }
nodoame 3:c290afbacd1c 243
nodoame 5:428b7ac86810 244 //射出
nodoame 5:428b7ac86810 245 if(getBt(btTri)&&obonFlag){
nodoame 5:428b7ac86810 246 kRed=shotToDenziben(0x02,15,1,airAddr);
nodoame 12:6445a3c3d34c 247 wait(0.2f);
nodoame 12:6445a3c3d34c 248 kRed=shotToDenziben(0x02,0,1,airAddr);
nodoame 4:bae4e2d103d4 249 kRed=0;
nodoame 3:c290afbacd1c 250 }
nodoame 3:c290afbacd1c 251
nodoame 5:428b7ac86810 252 //バルブ開放
nodoame 5:428b7ac86810 253 if(getPress(presLeft)&&pLeftF){
nodoame 5:428b7ac86810 254 pLeftF=false;
nodoame 5:428b7ac86810 255 kRed=shotToDenziben(7,1,1,valveAddr);
nodoame 14:aaf2ae25e508 256 wait(2.0);
nodoame 14:aaf2ae25e508 257 if(!kRed)
nodoame 14:aaf2ae25e508 258 kRed=shotToDenziben(7,0,1,valveAddr);
nodoame 5:428b7ac86810 259 }
nodoame 14:aaf2ae25e508 260 else if(!getPress(presLeft)){
nodoame 14:aaf2ae25e508 261 kRed=0;
nodoame 5:428b7ac86810 262 pLeftF=true;
nodoame 14:aaf2ae25e508 263 }
nodoame 5:428b7ac86810 264
nodoame 5:428b7ac86810 265 //バルブ開放
nodoame 5:428b7ac86810 266 if(getPress(presRight)&&pRightF){
nodoame 5:428b7ac86810 267 pRightF=false;
nodoame 5:428b7ac86810 268 kRed=shotToDenziben(8,1,1,valveAddr);
nodoame 14:aaf2ae25e508 269 wait(2.0);
nodoame 14:aaf2ae25e508 270 if(!kRed)
nodoame 14:aaf2ae25e508 271 kRed=shotToDenziben(8,0,1,valveAddr);
nodoame 0:dc587be179e9 272 }
nodoame 14:aaf2ae25e508 273 else if(!getPress(presRight)){
nodoame 14:aaf2ae25e508 274 kRed=0;
nodoame 5:428b7ac86810 275 pRightF=true;
nodoame 14:aaf2ae25e508 276 }
nodoame 0:dc587be179e9 277
nodoame 5:428b7ac86810 278 //バルブ開放
nodoame 5:428b7ac86810 279 if(getBt(btCro)&&btCrF){
nodoame 5:428b7ac86810 280 btCrF=false;
nodoame 5:428b7ac86810 281 kRed=shotToDenziben(9,1,1,valveAddr);
nodoame 14:aaf2ae25e508 282 wait(0.2);
nodoame 14:aaf2ae25e508 283 if(!kRed)
nodoame 14:aaf2ae25e508 284 kRed=shotToDenziben(9,0,1,valveAddr);
nodoame 5:428b7ac86810 285 }
nodoame 5:428b7ac86810 286 else if(!getBt(btCro))
nodoame 14:aaf2ae25e508 287 {
nodoame 14:aaf2ae25e508 288 kRed=0;
nodoame 5:428b7ac86810 289 btCrF=true;
nodoame 14:aaf2ae25e508 290 }
nodoame 5:428b7ac86810 291
nodoame 5:428b7ac86810 292
nodoame 5:428b7ac86810 293 //レーザーポインタ
nodoame 3:c290afbacd1c 294 if(getBt(btSqa)&&sqf){
nodoame 3:c290afbacd1c 295 sqf=0;
nodoame 3:c290afbacd1c 296 l=!l;
nodoame 4:bae4e2d103d4 297 kBlue=!kBlue;
nodoame 0:dc587be179e9 298 }
nodoame 3:c290afbacd1c 299 else if(!getBt(btSqa))
nodoame 3:c290afbacd1c 300 sqf=1;
nodoame 3:c290afbacd1c 301
nodoame 5:428b7ac86810 302 //タッチセンサ
nodoame 12:6445a3c3d34c 303 if(((int)DATA[1]&12)==12){
nodoame 12:6445a3c3d34c 304 bValue=0.0f;gValue=0.8f;rValue=0;
nodoame 12:6445a3c3d34c 305 }
nodoame 12:6445a3c3d34c 306 else{
nodoame 12:6445a3c3d34c 307 bValue=0.8f;gValue=0;rValue=0;
nodoame 12:6445a3c3d34c 308 }
nodoame 4:bae4e2d103d4 309
nodoame 5:428b7ac86810 310 //お盆回し
nodoame 5:428b7ac86810 311 if(getPress(presUp)){
nodoame 5:428b7ac86810 312 kRed=shotToDenziben(0x02,7,1,airAddr);
nodoame 5:428b7ac86810 313 wait(0.1);
nodoame 5:428b7ac86810 314 obonFlag=true;
nodoame 12:6445a3c3d34c 315 rValue=0.8;gValue=0;bValue=0.8;
nodoame 5:428b7ac86810 316 kRed=0;
nodoame 5:428b7ac86810 317 }
nodoame 5:428b7ac86810 318 else if(getPress(presDown)){
nodoame 5:428b7ac86810 319 kRed=shotToDenziben(0x02,0,1,airAddr);
nodoame 5:428b7ac86810 320 wait(0.1);
nodoame 12:6445a3c3d34c 321 bValue=0.8f;gValue=0;rValue=0;
nodoame 5:428b7ac86810 322 obonFlag=false;
nodoame 5:428b7ac86810 323 kRed=0;
nodoame 5:428b7ac86810 324 }
nodoame 5:428b7ac86810 325
nodoame 4:bae4e2d103d4 326 deg=devidePoint((double)DATA[3],(double)DATA[2],15,360/devideNum);
nodoame 4:bae4e2d103d4 327 val=valueForMechanum(deg,devideNum,getBt(trigL),getBt(trigR),getBt(btSph));
nodoame 11:1ca2498815c9 328
nodoame 11:1ca2498815c9 329 //一定時間通信してないと停止
nodoame 11:1ca2498815c9 330 if(stopCounter>stopCount)
nodoame 11:1ca2498815c9 331 val=64;
nodoame 11:1ca2498815c9 332 else
nodoame 11:1ca2498815c9 333 stopCounter++;
nodoame 0:dc587be179e9 334 Mechanum.putc(val);
nodoame 12:6445a3c3d34c 335 setFCLED(rValue,gValue,bValue);
nodoame 0:dc587be179e9 336 wait(0.03f);
nodoame 11:1ca2498815c9 337 do{
nodoame 11:1ca2498815c9 338 if(!debugFlag)
nodoame 11:1ca2498815c9 339 break;
nodoame 14:aaf2ae25e508 340 //pc.printf("val:%d\r\n",val);
nodoame 11:1ca2498815c9 341 //printf("%lf:%d:%d\r\n",deg,(int)DATA[3],(int)DATA[2]);
nodoame 11:1ca2498815c9 342 //printf("%d\r\n",(int)DATA[0]);
nodoame 11:1ca2498815c9 343 //pc.printf("DATA[1]%d\r\n",(int)(DATA[1]&0x02));
nodoame 11:1ca2498815c9 344 //printf("deg:%lfX1:%d:bt%ddeg:%lfval:%d\r\n",deg,(int)DATA[1],getBt(btSqa),deg,val);
nodoame 11:1ca2498815c9 345 }while(false);
nodoame 0:dc587be179e9 346 }
nodoame 0:dc587be179e9 347 }