Mobile Robot Project

Dependencies:   RGB_LED m3pi mbed

Files at this revision

API Documentation at this revision

Comitter:
wqz9822
Date:
Thu Oct 29 16:19:19 2015 +0000
Commit message:
Mobile Robot Project

Changed in this revision

RGB_LED.lib Show annotated file Show diff for this revision Revisions of this file
m3pi.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 541dc12fcbf0 RGB_LED.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RGB_LED.lib	Thu Oct 29 16:19:19 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/wqz9822/code/RGB_LED/#8f07ffe423ce
diff -r 000000000000 -r 541dc12fcbf0 m3pi.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m3pi.lib	Thu Oct 29 16:19:19 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/wqz9822/code/m3pi/#f1e6f60b2fdf
diff -r 000000000000 -r 541dc12fcbf0 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 29 16:19:19 2015 +0000
@@ -0,0 +1,166 @@
+#include "mbed.h"
+#include "m3pi.h"
+#include "rgbLed.h"
+#include "color.h"
+
+m3pi m3pi;
+Timer timerA;
+Serial Wixel(p28, p27); // tx, rx
+AnalogIn analog (p20); // Randomness generator
+AnalogIn resistorAin (p16); // Voltage divider reads
+
+//Setup RGB led using PWM pins and class
+RGBLed myRGBled(p23,p22,p21); //RGB PWM pins
+
+//setup some color objects in flash using const's
+const LEDColor red   (255, 0, 0);
+const LEDColor green (0, 255, 0);
+const LEDColor blue  (0, 0, 255);
+const LEDColor yellow(255 ,255 ,0);
+const LEDColor white (255 ,255, 255);
+
+DigitalOut PIN15 (p15);
+DigitalOut PIN17 (p17);
+DigitalOut PIN18 (p18);
+DigitalOut PIN19 (p19);
+DigitalOut PIN20 (p20);
+
+int behaviour; // Behaviour switcher
+float voltage;
+int voltageCounter = 0; // To elmimate ADC spike noise
+float timerThreshold;
+
+
+unsigned int random_generator (void){
+    unsigned int x = 0;
+    unsigned int iRandom = 0;
+ 
+    for (x = 0; x <= 32; x += 2)
+    {
+        iRandom += ((analog.read_u16() % 3) << x);
+        wait_us (10);
+    }
+    
+    return iRandom;
+}
+
+void forward(float amount) {
+    float speed = amount/5.0 + .075;
+    m3pi.left_motor(-speed);
+    m3pi.right_motor(-speed);
+    timerThreshold = 1;
+}
+
+void backward(float amount) {
+    float speed = amount/5.0 + .075;
+    m3pi.left_motor(speed);
+    m3pi.right_motor(speed);
+    timerThreshold = 1;
+}
+
+void turnLeft(float amount) {
+    // try to anchor it so that 1 means 90 degree turn
+    float right_speed = amount/5.0 + .075;
+    m3pi.left_motor(right_speed);
+    
+    m3pi.right_motor(-right_speed);
+    timerThreshold=.5;
+}
+
+void turnRight(float amount) {
+    float left_speed = amount/5.0 + .075;
+    m3pi.right_motor(left_speed);
+    m3pi.left_motor(-left_speed);
+    timerThreshold=.5;
+}
+
+/*void shakeHead() {
+    m3pi.left_motor(.15);
+    m3pi.right_motor(-.15);
+    wait(.3);
+    for(int i = 0; i < 3; i++) {
+        if(i % 2 == 0) {
+            m3pi.left_motor(-.15);
+            m3pi.right_motor(.15);
+        } else {
+            m3pi.left_motor(.15);
+            m3pi.right_motor(-.15);
+        }
+        timerThreshold = .6);
+    }
+    m3pi.left_motor(.15);
+    m3pi.right_motor(-.15);
+    wait(.3);
+}*/
+
+void actLikeYoureInLove() {
+    myRGBled = red;
+    m3pi.left_motor(0.0);
+    m3pi.right_motor(0.0);
+    wait(1);
+    m3pi.left_motor(-0.1);
+    m3pi.right_motor(-0.1);
+    wait(.5);
+    while(1) {
+        m3pi.left_motor(.2);
+        m3pi.right_motor(-.2);
+
+    }
+}
+
+void init()
+{
+    Wixel.baud(9600);
+    timerA.start();
+    myRGBled = green;
+    srand (random_generator()); // Using ADC value to generate random seed
+}
+
+int main() 
+{
+     
+    init();
+
+    while(1)  
+    {
+        voltage = resistorAin;
+        Wixel.printf("Voltage %f \n",voltage);
+        
+        if (( voltage > 0.45) & ( voltage < 0.7))
+        {
+            voltageCounter ++;
+            if (voltageCounter == 2){
+                actLikeYoureInLove();
+            }
+        }
+
+        //Wixel.printf("%f seconds\n", timerA.read());
+
+        if( timerA.read() >= timerThreshold)
+        {
+            behaviour = rand() % 5 + 1;  // Generate num between 1 and 5
+            //Wixel.printf("Behavior &d\n", behaviour);
+            float speed = rand()/double(RAND_MAX);
+
+            switch (behaviour) 
+            {
+                case 1:
+                    forward(1);
+                    break;
+                case 2:
+                    turnLeft(speed);
+                    break;
+                case 3:
+                    turnRight(speed);
+                    break;
+                case 4:
+                    backward(1);
+                    break;
+                case 5:
+                    //Do noting
+                    break;
+            }
+            timerA.reset();
+        }
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 541dc12fcbf0 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Oct 29 16:19:19 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e