Niet van mijzelf
Dependencies: USBHost USBHostXpad mbed-rtos mbed
Fork of x4180_Tank by
Revision 10:7e3987c8fa37, committed 2015-02-25
- Comitter:
- 347467
- Date:
- Wed Feb 25 08:38:37 2015 +0000
- Parent:
- 9:789350244478
- Commit message:
- Xboxcontroller;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 789350244478 -r 7e3987c8fa37 main.cpp --- a/main.cpp Sat Dec 06 05:20:03 2014 +0000 +++ b/main.cpp Wed Feb 25 08:38:37 2015 +0000 @@ -10,20 +10,30 @@ #include <cmath> - Serial pc(USBTX, USBRX); Serial robotCom(p13, p14); AnalogIn ir(p20); Speaker speaker(p18); // the pin must be the AnalogOut pin - p18 +DigitalOut DQ1(p9); //motor lv +PwmOut PWM1(p21); //motor lv +DigitalOut DQ2(p10);//motor rv +PwmOut PWM2 (p22); //Motor rv +DigitalOut DQ3(p11);//motor la +PwmOut PWM3(p23); //motor la +DigitalOut DQ4(p12);//motor ra +PwmOut PWM4 (p24); //Motor ra DigitalOut led1(LED1); //shows trigger DigitalOut led2(LED2); //shows collision prevention DigitalOut led3(LED3); //shows laser pointer active DigitalOut led4(LED4); //shows cpu activity + DigitalOut fire(p8); DigitalOut laser(p27); + + Audio audio(speaker); Xbox360ControllerState controlState; Traxster tank(robotCom); @@ -42,22 +52,38 @@ float tank_L(float x, float y){ float scale = 0.0; - if(x >= 0.0 && y <= 0){ //bottom right + + + if(x >= 0.0 && y <= 0){ //LBA bottom right + pc.printf("1\r\n"); + + + + if(y == 0){ scale = 1.0; goto End;} if(x == 0){ scale = -1.0; goto End;} float theta = atan(y/x) / 3.14159; scale = theta * 4.0 + 1.0; - } else if(x <= 0.0 && y <= 0){ //bottom left + + } else if(x <= 0.0 && y <= 0){ //LBA bottom left + pc.printf("2\r\n"); + + + scale = -1.0; } else if(x <= 0.0 && y >= 0){ //top left + + if(y == 0){ scale = -1.0; goto End;} if(x == 0){ scale = 1.0; goto End;} float theta = atan(y/x) / 3.14159; scale = -theta * 4.0 - 1.0; } else { //top-right + + scale = 1.0; } @@ -69,6 +95,7 @@ float scale = 0.0; if(x >= 0.0 && y <= 0){ //bottom right + scale = -1.0; } else if(x <= 0.0 && y <= 0){ //bottom left @@ -103,7 +130,7 @@ //acts as a failsafe while(controller.connected()) { if(wasdisconnected){ - //pc.printf("Controller Status >> Connected!\r\n"); + pc.printf("Controller Status >> Connected!\r\n"); controller.led( USBHostXpad::LED1_ON ); tank.SetMotors(0.0,0.0); //stop, wait for controller to reconnect for a second. wasdisconnected = false; @@ -114,17 +141,32 @@ bool trigger = false; // trigger rely - airsoft gun - if(controlState.triggerRight > 0x80){ - controller.rumble(255,255); + + float AS = 1.0; // acutele snelheid + if(controlState.triggerRight > 0x80){ //Rechtdoor rijden + //controller.rumble(255,255); fire = 1; trigger = true; + + }else{ - controller.rumble(0,0); + //controller.rumble(0,0); fire = 0; } //lazzzzeeer sight - if( controlState.buttons & USBHostXpad::XPAD_PAD_X){ + if( controlState.buttons & USBHostXpad::XPAD_PAD_X){ //Knop X laser=DQ3 + //Stoppen + DQ1= 1; //lv + DQ2= 1; //rv + DQ3= 1; //la + DQ4= 1; //ra + PWM1 = AS*0; //lv in de vrij + PWM2 = AS*0; //rv in de vrij + PWM3 = AS*0; //la in de vrij + PWM4 = AS*0; //ra in de vrij + wait(2.0); + laser = 1; }else{ laser = 0; @@ -134,14 +176,51 @@ //dpad sounds if(trigger || (controlState.buttons & (USBHostXpad::XPAD_HAT_UP | USBHostXpad::XPAD_HAT_DOWN | USBHostXpad::XPAD_HAT_LEFT | USBHostXpad::XPAD_HAT_RIGHT | USBHostXpad::XPAD_PAD_Y))){ int code = 0; - if(controlState.buttons & USBHostXpad::XPAD_HAT_UP){ - code = 0; - } else if(controlState.buttons & USBHostXpad::XPAD_HAT_DOWN){ - code = 1; - } else if(controlState.buttons & USBHostXpad::XPAD_HAT_LEFT){ - code = 2; - } else if(controlState.buttons & USBHostXpad::XPAD_HAT_RIGHT){ - code = 3; + + float z = 0.5; // persentage voor het draaien van de motoren + float j = 0.75; // persentage voor het draaien van de motoren + if(controlState.buttons & USBHostXpad::XPAD_HAT_UP){ //vooruit + + DQ1= 1; //lv + DQ2= 1; //rv + DQ3= 1; //la + DQ4= 1; //ra + PWM1 = AS*1.5; + PWM2 = AS*1.5; + PWM3 = AS*1.5; + PWM4 = AS*1.5; + } else if(controlState.buttons & USBHostXpad::XPAD_HAT_DOWN){ //Achteruit + DQ1= 0; //lv + DQ2= 0; //rv + DQ3= 0; //la + DQ4= 0; //ra + PWM1 = AS*1.5; + PWM2 = AS*1.5; + PWM3 = AS*1.5; + PWM4 = AS*1.5; + + } else if(controlState.buttons & USBHostXpad::XPAD_HAT_LEFT){ //links + led1=1; //links + DQ1 = 0; //lv + DQ2 = 1; //rv + DQ3 = 0; //La + DQ4 = 1; //Ra + PWM1 = AS*z; + PWM2 = AS*j; + PWM3 = AS*z; + PWM4 = AS*j; + } else if(controlState.buttons & USBHostXpad::XPAD_HAT_RIGHT){//rechts + led3=1; //rechts + + DQ1 = 1; //lv + DQ2 = 0; //rv + DQ3 = 1; //la + DQ4 = 0; //Ra + PWM1 = AS*j; + PWM2 = AS*z; + PWM3 = AS*j; + PWM4 = AS*z; + } else if (trigger){ code = 4; }else if (controlState.buttons & USBHostXpad::XPAD_PAD_Y){ @@ -158,13 +237,13 @@ //update output leds - led1 = fire; - led2 = collisionAvoidance; - led3 = laser; + led1= fire; + led2= collisionAvoidance; + led3= laser; // on collision, reverse tank if(ir > 0.75 && collisionAvoidance){ - controller.rumble(255,255); + // controller.rumble(255,255); tank.SetMotors(-0.5,-0.5); continue; }else{ @@ -197,11 +276,11 @@ tank.SetMotors(0,0); fire = 0; laser = 1; - led1 = 1; - led4 = 1; + led1 = 1; + led2= 1; - pc.baud(19200); + pc.baud(9600); pc.printf("TANK\r\n"); @@ -218,7 +297,7 @@ //tank.SetMotors(0,0); while(1){ - led4 = !led4; + led4= !DQ4; // fire = !fire; Thread::wait(1000); }