C K / Mbed 2 deprecated x4180_Tank

Dependencies:   USBHost USBHostXpad mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
hotwheelharry
Date:
Thu Nov 13 23:20:02 2014 +0000
Child:
1:3bae10d2507c
Commit message:
first commit - compile ok;

Changed in this revision

Audio.h Show annotated file Show diff for this revision Revisions of this file
Speaker.h Show annotated file Show diff for this revision Revisions of this file
Traxster.cpp Show annotated file Show diff for this revision Revisions of this file
Traxster.h Show annotated file Show diff for this revision Revisions of this file
USBHost.lib Show annotated file Show diff for this revision Revisions of this file
USBHostXpad.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
utils.cpp Show annotated file Show diff for this revision Revisions of this file
utils.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Audio.h	Thu Nov 13 23:20:02 2014 +0000
@@ -0,0 +1,352 @@
+
+#include "Speaker.h"
+
+
+class Audio{
+private:
+    Mutex mutex;
+    Speaker& speaker;
+public:
+    Audio(Speaker& ao) : speaker(ao)
+    {   
+    }
+    void playSong(/**/){
+        mutex.lock();
+        
+        mutex.unlock();
+    }
+    void triggerShot(){
+        mutex.lock();
+        
+        mutex.unlock();   
+    }
+    
+    void run(){
+        while(true){    //service audio requests...
+            
+            Thread::wait(100);   //ms        
+        }
+    }
+    void playNote(float a, float b, float c){
+        mutex.lock();
+        speaker.PlayNote(a,b,c);
+        mutex.unlock(); 
+    }
+    
+    void playMario(){
+          
+        playNote(660.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(660.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(660.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(510.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(660.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(770.0, 0.08, 1.0);
+        Thread::wait(413);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(431);
+        playNote(510.0, 0.08, 1.0);
+        Thread::wait(338);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(300);
+        playNote(320.0, 0.08, 1.0);
+        Thread::wait(375);
+        playNote(440.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(480.0, 0.06, 1.0);
+        Thread::wait(248);
+        playNote(450.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(150);
+        playNote(660.0, 0.06, 1.0);
+        Thread::wait(150);
+        playNote(760.0, 0.04, 1.0);
+        Thread::wait(113);
+        playNote(860.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(700.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(760.0, 0.04, 1.0);
+        Thread::wait(263);
+        playNote(660.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(520.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(580.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(480.0, 0.06, 1.0);
+        Thread::wait(375);
+        playNote(510.0, 0.08, 1.0);
+        Thread::wait(338);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(300);
+        playNote(320.0, 0.08, 1.0);
+        Thread::wait(375);
+        playNote(440.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(480.0, 0.06, 1.0);
+        Thread::wait(248);
+        playNote(450.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(150);
+        playNote(660.0, 0.06, 1.0);
+        Thread::wait(150);
+        playNote(760.0, 0.04, 1.0);
+        Thread::wait(113);
+        playNote(860.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(700.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(760.0, 0.04, 1.0);
+        Thread::wait(263);
+        playNote(660.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(520.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(580.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(480.0, 0.06, 1.0);
+        Thread::wait(375);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(760.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(720.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(680.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(620.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(650.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(570.0, 0.08, 1.0);
+        Thread::wait(165);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(760.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(720.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(680.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(620.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(650.0, 0.15, 1.0);
+        Thread::wait(225);
+        playNote(1020.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(1020.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(1020.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(760.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(720.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(680.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(620.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(650.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(570.0, 0.08, 1.0);
+        Thread::wait(315);
+        playNote(585.0, 0.08, 1.0);
+        Thread::wait(338);
+        playNote(550.0, 0.08, 1.0);
+        Thread::wait(315);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(270);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(760.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(720.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(680.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(620.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(650.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(570.0, 0.08, 1.0);
+        Thread::wait(165);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(760.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(720.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(680.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(620.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(650.0, 0.15, 1.0);
+        Thread::wait(225);
+        playNote(1020.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(1020.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(1020.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(760.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(720.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(680.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(620.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(650.0, 0.11, 1.0);
+        Thread::wait(225);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(430.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(570.0, 0.08, 1.0);
+        Thread::wait(315);
+        playNote(585.0, 0.08, 1.0);
+        Thread::wait(338);
+        playNote(550.0, 0.08, 1.0);
+        Thread::wait(315);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(270);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.04, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.04, 1.0);
+        Thread::wait(263);
+        playNote(500.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(580.0, 0.06, 1.0);
+        Thread::wait(263);
+        playNote(660.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(430.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(380.0, 0.06, 1.0);
+        Thread::wait(450);
+        playNote(500.0, 0.04, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.04, 1.0);
+        Thread::wait(263);
+        playNote(500.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(580.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(660.0, 0.06, 1.0);
+        Thread::wait(413);
+        playNote(870.0, 0.06, 1.0);
+        Thread::wait(244);
+        playNote(760.0, 0.06, 1.0);
+        Thread::wait(450);
+        playNote(500.0, 0.04, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(500.0, 0.04, 1.0);
+        Thread::wait(263);
+        playNote(500.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(580.0, 0.06, 1.0);
+        Thread::wait(263);
+        playNote(660.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(500.0, 0.06, 1.0);
+        Thread::wait(225);
+        playNote(430.0, 0.06, 1.0);
+        Thread::wait(113);
+        playNote(380.0, 0.06, 1.0);
+        Thread::wait(450);
+        playNote(660.0, 0.08, 1.0);
+        Thread::wait(113);
+        playNote(660.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(660.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(510.0, 0.08, 1.0);
+        Thread::wait(75);
+        playNote(660.0, 0.08, 1.0);
+        Thread::wait(225);
+        playNote(770.0, 0.08, 1.0);
+        Thread::wait(413);
+        playNote(380.0, 0.08, 1.0);
+        Thread::wait(431);
+   
+    }
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Thu Nov 13 23:20:02 2014 +0000
@@ -0,0 +1,52 @@
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+// precompute 32 sample points on one sine wave cycle
+// used for continuous sine wave output later
+        for(int k=0; k<32; k++) {
+            Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/32.0*6.28318530717959)))/2.0));
+            // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg
+        }
+
+    }
+// class method to play a note based on AnalogOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        // scale samples using current volume level arg
+        for(int k=0; k<32; k++) {
+            Analog_scaled_data[k] = Analog_out_data[k] * volume;
+        }
+        // reset to start of sample array
+        i=0;
+        // turn on timer interrupts to start sine wave output
+        Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0));
+        // play note for specified time
+        wait(duration);
+        // turns off timer interrupts
+        Sample_Period.detach();
+        // sets output to mid range - analog zero
+        this->_pin.write_u16(32768);
+
+    }
+private:
+// sets up specified pin for analog using AnalogOut class
+    AnalogOut _pin;
+    // set up a timer to be used for sample rate interrupts
+    Ticker Sample_Period;
+
+    //variables used by interrupt routine and PlayNote
+    volatile int i;
+    short unsigned Analog_out_data[32];
+    short unsigned Analog_scaled_data[32];
+
+// Interrupt routine
+// used to output next analog sample whenever a timer interrupt occurs
+    void Sample_timer_interrupt(void) {
+        // send next analog sample out to D to A
+        this->_pin.write_u16(Analog_scaled_data[i]);
+        // increment pointer and wrap around back to 0 at 32
+        i = (i+1) & 0x01F;
+    }
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Traxster.cpp	Thu Nov 13 23:20:02 2014 +0000
@@ -0,0 +1,33 @@
+
+#include "Traxster.h"
+#include "utils.h"
+
+int getMotorSpeedInt(float f){
+    
+    int max = 100;
+    
+    if(f > 1.0)
+        f = 1.0;
+
+    if(f < -1.0)
+        f = -1.0;
+    
+    return   (int)round( (float)max * f );
+}
+
+void Traxster::SetMotors(float fm1, float fm2)
+{
+    int m1 = getMotorSpeedInt(fm1);
+    int m2 = getMotorSpeedInt(fm2);
+    
+    if (m1 == 0 && m2 == 0)
+    {
+        rob.puts("stop\r");
+    }
+    else
+    {
+        rob.printf("mogo 1:%d 2:%d\r", m1, m2);
+    }
+    while (rob.readable())
+        rob.getc();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Traxster.h	Thu Nov 13 23:20:02 2014 +0000
@@ -0,0 +1,16 @@
+#include "mbed.h"
+
+class Traxster { 
+private:
+    Serial& rob;
+public:   
+    Traxster(Serial& robotComm  /* and other controls or sensors...*/)
+    : rob(robotComm)
+    {
+        rob.baud(19200);
+    };
+    
+    // Set motor speeds. [-1, +1] of max speed for motor.
+    void SetMotors(float m1, float m2);
+    
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHost.lib	Thu Nov 13 23:20:02 2014 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/hotwheelharry/code/USBHost/#367b884e5041
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHostXpad.lib	Thu Nov 13 23:20:02 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/okini3939/code/USBHostXpad/#4254192898a9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Nov 13 23:20:02 2014 +0000
@@ -0,0 +1,64 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "Traxster.h"
+#include "USBHostXpad.h"
+#include "utils.h"
+#include "Audio.h"
+
+Serial pc(USBTX, USBRX);
+Serial robotCom(p13, p14);
+AnalogIn ir(p20);
+Speaker speaker(p18);  // the pin must be the AnalogOut pin - p18
+
+
+Audio audio(speaker);
+USBHostXpad controller;
+Xbox360ControllerState controlState;
+Traxster tank(robotCom);
+
+
+
+void onControlInput(int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r){
+    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);
+    controlState = Xbox360ControllerState(buttons, stick_lx, stick_ly, stick_rx, stick_ry, trigger_l, trigger_r);
+};
+
+void thread_audio_run(void const* arg){
+    audio.run();
+}
+
+int main() {
+    pc.baud(19200);
+    
+    controller.attachEvent(&onControlInput);
+    
+    Thread audio_thread(thread_audio_run);
+    
+    audio.playMario();
+    
+    while(1){
+        //acts as a failsafe
+        while(controller.connected()) {
+            if(ir > 0.5){
+                controller.rumble(255,255);
+            }
+            if( controller.read(USBHostXpad::XPAD_PAD_Y) ) {
+                controller.led( USBHostXpad::LED_ROTATE );   
+            } else {
+                controller.led( USBHostXpad::LED_OFF ); 
+            }
+            if(controlState.triggerRight > 0){
+                controller.rumble(255,255);
+            }
+           
+            if( controller.read(USBHostXpad::XPAD_PAD_X) ) {
+                tank.SetMotors(1.0,1.0);
+            } else {
+                tank.SetMotors(0.0,0.0);    //stop, wait for controller to reconnect for a second.   
+            }
+        }
+        tank.SetMotors(0.0,0.0);    //stop, wait for controller to reconnect for a second.
+        Thread::wait(500);
+        controller.connect();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils.cpp	Thu Nov 13 23:20:02 2014 +0000
@@ -0,0 +1,9 @@
+#include "utils.h"
+
+double ghettoFloor(double d){
+    return double((long long)d);
+}
+double round(double d)
+{
+  return ghettoFloor(d + 0.5);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils.h	Thu Nov 13 23:20:02 2014 +0000
@@ -0,0 +1,24 @@
+
+#ifndef utilshhh
+#define utilshhh
+
+
+double ghettoFloor(double d);
+double round(double d);
+
+struct Xbox360ControllerState{
+    int buttons;
+    int analogLeftX;
+    int analogLeftY;
+    int analogRightX;
+    int analogRightY;
+    int triggerLeft;
+    int triggerRight;
+    Xbox360ControllerState(){};
+    Xbox360ControllerState(int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r)
+    : buttons(buttons), analogLeftX(stick_lx), analogLeftY(stick_ly), analogRightX(stick_rx), analogRightY(stick_ry), triggerLeft(trigger_l), triggerRight(trigger_r)
+    {}
+};
+
+
+#endif
\ No newline at end of file