Code for the kl25z component of our project - for group Alloy.

Dependencies:   Servo mbed

Files at this revision

API Documentation at this revision

Comitter:
douglasc
Date:
Mon Nov 10 17:30:55 2014 +0000
Commit message:
initial commit

Changed in this revision

Servo.lib Show annotated file Show diff for this revision Revisions of this file
Window.cpp Show annotated file Show diff for this revision Revisions of this file
Window.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/Servo.lib	Mon Nov 10 17:30:55 2014 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/simon/code/Servo/#36b69a7ced07
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Window.cpp	Mon Nov 10 17:30:55 2014 +0000
@@ -0,0 +1,51 @@
+/* Window.cpp */
+#include "Window.h"
+#include "Servo.h"
+#include "mbed.h"
+
+Window::Window() {}
+
+Window::Window(Servo* s, float timer) {
+    servo = s;
+    
+    windowOpen = true;
+    
+    // Had to guess and check this value after
+    // the servo was modified to move 360 degrees.
+    servoStop = 0.48;
+    
+    // How long it takes for the window to completely
+    // raise or lower.
+    windowMovementTimer = timer;
+    
+    // closeWindow();
+}
+
+bool Window::isWindowOpen() {
+    return windowOpen;
+}
+
+bool Window::openWindow() {
+    if (!isWindowOpen()) {
+        servo->write(0.6);
+        windowOpen = true;
+        wait(windowMovementTimer);
+        servo->write(servoStop);
+        windowOpen = true;
+        return true;
+    }
+    // window already open
+    return false;
+}
+
+bool Window::closeWindow() {
+    if (isWindowOpen()) {
+        servo->write(0.35);
+        wait(windowMovementTimer);
+        servo->write(servoStop);
+        windowOpen = false;
+        return true;
+    }
+    // window already closed
+    return false;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Window.h	Mon Nov 10 17:30:55 2014 +0000
@@ -0,0 +1,24 @@
+/* Window.h */
+
+#ifndef WINDOW_H
+#define WINDOW_H
+
+#include "Servo.h"
+#include "mbed.h"
+
+class Window {
+private:
+    bool windowOpen;
+    float servoStop;
+    float windowMovementTimer;
+    Servo* servo;
+    Serial* pc;
+public:
+    Window();
+    Window(Servo* s, float timer);
+    bool isWindowOpen();
+    bool openWindow();
+    bool closeWindow();
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Nov 10 17:30:55 2014 +0000
@@ -0,0 +1,149 @@
+/**
+ * Interactive Device Design, Fall 2014
+ * Homework 5 - Wind and Window controller
+ * Freescale Freedom KL25Z Board
+ *
+ * Ian Shain
+ * Doug Cook
+ * Kiyana Salkeld
+ * Elizabeth Lin
+ *
+ */
+
+#include "mbed.h"
+#include "Servo.h"
+#include "Window.h"
+#include <string>
+
+Serial pc(USBTX, USBRX);
+Serial ble(PTC4,PTC3); // PTC4 (purple) -> TX, PTC3 (gray) -> RX
+const float ANALOG_HALF = 0.499;
+const float ANALOG_DELTA = 0.011;
+const float ANALOG_RANGE = 0.5;
+
+DigitalOut red = LED_RED;
+DigitalOut green = LED_GREEN;
+DigitalOut blue = LED_BLUE;
+
+// converts a voltage reading to a percent
+double getAnalogPercent(float in) {
+    return ((double)in / ANALOG_RANGE) * 100.0;   
+}
+
+// filter out garbage from the serial line
+const char* validCodes = "abcdefgh0123456789";
+bool isValidCode(char c) {
+    for (int i = 0; validCodes[i] != '\0'; i++) {
+        if (c == validCodes[i]) {
+            return true;   
+        }  
+    }
+    // pc.printf("invalid code detected: %c \r\n",c);
+    return false;
+}
+
+char ble_CODE;
+string ble_NUM_str = "";
+int ble_NUM;
+bool serialMessageAvailable = false;
+void bleCallback() {
+    if (ble_CODE == 'z') {
+        char in = ble.getc();
+        if (isValidCode(in)) {
+            ble_CODE = in;
+        } else {
+            return;
+        }
+    } else {
+        char in = ble.getc();
+        if (isValidCode(in)) {
+            ble_CODE = in;
+        } else {
+            return;
+        }
+       ble_NUM_str += ble.getc();
+    }
+    serialMessageAvailable = true;
+}
+
+int main() {
+    // servo control for Window automation
+    Servo s(D7);
+    Window window(& s, 4.0);
+    window.closeWindow();
+    
+    // status indicators
+    red = 1;
+    green = 1;
+    blue = 1;
+    
+    // motor for anemometer
+    AnalogIn motor(A0);
+    AnalogOut motorBase(PTE30);
+    motorBase.write(0.1);//(0.5);
+    string motorSpeed = "";
+    
+    // serial
+    ble.baud(9600);
+    ble.format(8,Serial::Odd,1);
+    ble.attach(bleCallback);
+
+    while(1) {        
+        // check for messages
+        if (serialMessageAvailable) {
+            switch(ble_CODE) {
+                case 'a':
+                    pc.printf("update minimum wind threshold to: ... \r\n");
+                    break;
+                case 'b':
+                    pc.printf("update maximum wind threshold to: ... \r\n");
+                    break;
+                case 'c':
+                    pc.printf("open window. \r\n");
+                    if (!window.isWindowOpen()) {
+                        window.openWindow();
+                    }
+                    break;
+                case 'd':
+                    pc.printf("close window. \r\n");
+                    if (window.isWindowOpen()) {
+                        window.closeWindow();   
+                    }
+                    break;
+                case 'e':
+                    pc.printf("initializing BLE device. \r\n");
+                    red = 0;
+                    wait(0.75);
+                    red = 1;
+                    break;
+                case 'f':
+                    pc.printf("restarting ble advertising. \r\n");
+                    break;
+                case 'g':
+                    pc.printf("current wind speed: ... \r\n");
+                    break;
+                case 'h':
+                    pc.printf("BLE device disconnected. \r\n");
+                    break;
+                default:
+                    // pc.printf("uknown code received from BLE device: %c\r\n",ble_CODE);
+                    break;
+            }
+            ble_CODE = 'z'; // flag indicating no code set
+            serialMessageAvailable = false;  
+        }
+
+        // center input at zero (no wind is blowing)
+        float val = motor.read() - ANALOG_HALF;
+        float absoluteVal = abs(val);
+        if (val < 0.0 - ANALOG_DELTA) {
+            pc.printf("motor is turning NORTH %02f percent \r\n",getAnalogPercent(absoluteVal));
+        } else if (val > 0.0 + ANALOG_DELTA) {
+            pc.printf("motor is turning SOUTH %02f percent \r\n",getAnalogPercent(absoluteVal));
+        }
+        // transmit data to BLE
+        ble.printf("%.0f",getAnalogPercent(absoluteVal));        
+                
+        wait(.5);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Nov 10 17:30:55 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89
\ No newline at end of file