Niet van mijzelf
Dependencies: USBHost USBHostXpad mbed-rtos mbed
Fork of x4180_Tank by
main.cpp@1:3bae10d2507c, 2014-11-13 (annotated)
- Committer:
- jmccranie3
- Date:
- Thu Nov 13 23:29:03 2014 +0000
- Revision:
- 1:3bae10d2507c
- Parent:
- 0:79485480cd7e
- Child:
- 2:5e870c215495
added an aweesome comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hotwheelharry | 0:79485480cd7e | 1 | #include "mbed.h" |
hotwheelharry | 0:79485480cd7e | 2 | #include "rtos.h" |
hotwheelharry | 0:79485480cd7e | 3 | #include "Traxster.h" |
hotwheelharry | 0:79485480cd7e | 4 | #include "USBHostXpad.h" |
hotwheelharry | 0:79485480cd7e | 5 | #include "utils.h" |
hotwheelharry | 0:79485480cd7e | 6 | #include "Audio.h" |
hotwheelharry | 0:79485480cd7e | 7 | |
hotwheelharry | 0:79485480cd7e | 8 | Serial pc(USBTX, USBRX); |
hotwheelharry | 0:79485480cd7e | 9 | Serial robotCom(p13, p14); |
hotwheelharry | 0:79485480cd7e | 10 | AnalogIn ir(p20); |
hotwheelharry | 0:79485480cd7e | 11 | Speaker speaker(p18); // the pin must be the AnalogOut pin - p18 |
hotwheelharry | 0:79485480cd7e | 12 | |
hotwheelharry | 0:79485480cd7e | 13 | |
hotwheelharry | 0:79485480cd7e | 14 | Audio audio(speaker); |
hotwheelharry | 0:79485480cd7e | 15 | USBHostXpad controller; |
hotwheelharry | 0:79485480cd7e | 16 | Xbox360ControllerState controlState; |
hotwheelharry | 0:79485480cd7e | 17 | Traxster tank(robotCom); |
hotwheelharry | 0:79485480cd7e | 18 | |
jmccranie3 | 1:3bae10d2507c | 19 | //comment added |
hotwheelharry | 0:79485480cd7e | 20 | |
hotwheelharry | 0:79485480cd7e | 21 | void onControlInput(int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r){ |
hotwheelharry | 0:79485480cd7e | 22 | pc.printf("buttons: %04x Lstick X,Y: %-5d, %-5d Rstick X,Y: %-5d, %-5d LTrig: %02x (%d) RTrig: %02x (%d)\n", buttons, stick_lx, stick_ly, stick_rx, stick_ry, trigger_l,trigger_l, trigger_r,trigger_r); |
hotwheelharry | 0:79485480cd7e | 23 | controlState = Xbox360ControllerState(buttons, stick_lx, stick_ly, stick_rx, stick_ry, trigger_l, trigger_r); |
hotwheelharry | 0:79485480cd7e | 24 | }; |
hotwheelharry | 0:79485480cd7e | 25 | |
hotwheelharry | 0:79485480cd7e | 26 | void thread_audio_run(void const* arg){ |
hotwheelharry | 0:79485480cd7e | 27 | audio.run(); |
hotwheelharry | 0:79485480cd7e | 28 | } |
hotwheelharry | 0:79485480cd7e | 29 | |
hotwheelharry | 0:79485480cd7e | 30 | int main() { |
hotwheelharry | 0:79485480cd7e | 31 | pc.baud(19200); |
hotwheelharry | 0:79485480cd7e | 32 | |
hotwheelharry | 0:79485480cd7e | 33 | controller.attachEvent(&onControlInput); |
hotwheelharry | 0:79485480cd7e | 34 | |
hotwheelharry | 0:79485480cd7e | 35 | Thread audio_thread(thread_audio_run); |
hotwheelharry | 0:79485480cd7e | 36 | |
hotwheelharry | 0:79485480cd7e | 37 | audio.playMario(); |
hotwheelharry | 0:79485480cd7e | 38 | |
hotwheelharry | 0:79485480cd7e | 39 | while(1){ |
hotwheelharry | 0:79485480cd7e | 40 | //acts as a failsafe |
hotwheelharry | 0:79485480cd7e | 41 | while(controller.connected()) { |
hotwheelharry | 0:79485480cd7e | 42 | if(ir > 0.5){ |
hotwheelharry | 0:79485480cd7e | 43 | controller.rumble(255,255); |
hotwheelharry | 0:79485480cd7e | 44 | } |
hotwheelharry | 0:79485480cd7e | 45 | if( controller.read(USBHostXpad::XPAD_PAD_Y) ) { |
hotwheelharry | 0:79485480cd7e | 46 | controller.led( USBHostXpad::LED_ROTATE ); |
hotwheelharry | 0:79485480cd7e | 47 | } else { |
hotwheelharry | 0:79485480cd7e | 48 | controller.led( USBHostXpad::LED_OFF ); |
hotwheelharry | 0:79485480cd7e | 49 | } |
hotwheelharry | 0:79485480cd7e | 50 | if(controlState.triggerRight > 0){ |
hotwheelharry | 0:79485480cd7e | 51 | controller.rumble(255,255); |
hotwheelharry | 0:79485480cd7e | 52 | } |
hotwheelharry | 0:79485480cd7e | 53 | |
hotwheelharry | 0:79485480cd7e | 54 | if( controller.read(USBHostXpad::XPAD_PAD_X) ) { |
hotwheelharry | 0:79485480cd7e | 55 | tank.SetMotors(1.0,1.0); |
hotwheelharry | 0:79485480cd7e | 56 | } else { |
hotwheelharry | 0:79485480cd7e | 57 | tank.SetMotors(0.0,0.0); //stop, wait for controller to reconnect for a second. |
hotwheelharry | 0:79485480cd7e | 58 | } |
hotwheelharry | 0:79485480cd7e | 59 | } |
hotwheelharry | 0:79485480cd7e | 60 | tank.SetMotors(0.0,0.0); //stop, wait for controller to reconnect for a second. |
hotwheelharry | 0:79485480cd7e | 61 | Thread::wait(500); |
hotwheelharry | 0:79485480cd7e | 62 | controller.connect(); |
hotwheelharry | 0:79485480cd7e | 63 | } |
hotwheelharry | 0:79485480cd7e | 64 | } |