ECE 4180 Lab 4. Wifi robot controlled via webpage with Huzzah ESP8266 chip as server.

Dependencies:   Motor mbed

Files at this revision

API Documentation at this revision

Comitter:
Zaydax
Date:
Tue Mar 15 22:10:56 2016 +0000
Commit message:
Publishing to make available to import.

Changed in this revision

Motor.lib 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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.lib	Tue Mar 15 22:10:56 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Motor/#f265e441bcd9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Tue Mar 15 22:10:56 2016 +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/main.cpp	Tue Mar 15 22:10:56 2016 +0000
@@ -0,0 +1,87 @@
+// Pratik Gangwani 
+// Justin Tamayo 
+// 3/15/16
+// ECE 4180 Lab 4; Wi-Fi controlled robot
+// C File to run MBEd anc control motor, LED, and speaker functions by reading in digital signals from Huzzah 8266 board
+
+#include "mbed.h"
+#include "Motor.h"
+#include "Speaker.h"
+
+//Define MBED LED's for use
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+DigitalOut myled4(LED4);
+
+// Define Digital Pins to read in digital signals from Huzzah 8266 board
+DigitalIn forward(p30);
+DigitalIn backward(p29);
+DigitalIn left(p28);
+DigitalIn right(p27);
+DigitalIn horn(p26);
+
+// Define Motor pins to connect to H-Bridge
+Motor rightMotor(p21, p16, p15); // pwm, fwd, rev
+Motor leftMotor(p22, p19, p17); // pwm, fwd, rev
+
+// Define Speaker pin (MUST BE PIN 18, ANALOG OUT for use with Speaker.h!)
+Speaker mySpeaker(p18);
+
+int main() {
+
+// Loop forever
+// Check to see if a pin is high. If that pin is high, execute that pin for as long as the signal is high on that pin. 
+// When you let go on the mouse click the code defaults to doing nothing
+// Each pin moves the robot in a particular direction, and activates a specific LED
+// Horn will not run forever if you hold it down, it is simply a click
+// Setting speed to 0.5 on the motors saves a lot of battery power
+
+    while(1){
+        
+        if(forward) {
+            rightMotor.speed(0.5);
+            leftMotor.speed(0.5);
+            myled1 = 1;
+            myled2 = 0;
+            myled3 = 0;
+            myled4 = 0;
+        } else if(backward) {
+            rightMotor.speed(-0.5);
+            leftMotor.speed(-0.5);
+            myled1 = 0;
+            myled2 = 1;
+            myled3 = 0;
+            myled4 = 0;
+        } else if(left) {
+            rightMotor.speed(0.5);
+            leftMotor.speed(-0.5);
+            myled1 = 0;
+            myled2 = 0;
+            myled3 = 1;
+            myled4 = 0;
+        } else if(right) {
+            rightMotor.speed(-0.5);
+            leftMotor.speed(0.5);
+            myled1 = 0;
+            myled2 = 0;
+            myled3 = 0;
+            myled4 = 1;
+        } else if(horn) {
+            rightMotor.speed(0.0);
+            leftMotor.speed(0.0);
+            myled1 = 1;
+            myled2 = 1;
+            myled3 = 1;
+            myled4 = 1;
+            mySpeaker.PlayNote(440.0, 0.5, 0.5);
+        } else {
+            rightMotor.speed(0.0);
+            leftMotor.speed(0.0);
+            myled1 = 0;
+            myled2 = 0;
+            myled3 = 0;
+            myled4 = 0;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Mar 15 22:10:56 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb
\ No newline at end of file