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

Dependencies:   Servo mbed

Committer:
douglasc
Date:
Mon Nov 10 17:30:55 2014 +0000
Revision:
0:85181831ea03
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
douglasc 0:85181831ea03 1 /**
douglasc 0:85181831ea03 2 * Interactive Device Design, Fall 2014
douglasc 0:85181831ea03 3 * Homework 5 - Wind and Window controller
douglasc 0:85181831ea03 4 * Freescale Freedom KL25Z Board
douglasc 0:85181831ea03 5 *
douglasc 0:85181831ea03 6 * Ian Shain
douglasc 0:85181831ea03 7 * Doug Cook
douglasc 0:85181831ea03 8 * Kiyana Salkeld
douglasc 0:85181831ea03 9 * Elizabeth Lin
douglasc 0:85181831ea03 10 *
douglasc 0:85181831ea03 11 */
douglasc 0:85181831ea03 12
douglasc 0:85181831ea03 13 #include "mbed.h"
douglasc 0:85181831ea03 14 #include "Servo.h"
douglasc 0:85181831ea03 15 #include "Window.h"
douglasc 0:85181831ea03 16 #include <string>
douglasc 0:85181831ea03 17
douglasc 0:85181831ea03 18 Serial pc(USBTX, USBRX);
douglasc 0:85181831ea03 19 Serial ble(PTC4,PTC3); // PTC4 (purple) -> TX, PTC3 (gray) -> RX
douglasc 0:85181831ea03 20 const float ANALOG_HALF = 0.499;
douglasc 0:85181831ea03 21 const float ANALOG_DELTA = 0.011;
douglasc 0:85181831ea03 22 const float ANALOG_RANGE = 0.5;
douglasc 0:85181831ea03 23
douglasc 0:85181831ea03 24 DigitalOut red = LED_RED;
douglasc 0:85181831ea03 25 DigitalOut green = LED_GREEN;
douglasc 0:85181831ea03 26 DigitalOut blue = LED_BLUE;
douglasc 0:85181831ea03 27
douglasc 0:85181831ea03 28 // converts a voltage reading to a percent
douglasc 0:85181831ea03 29 double getAnalogPercent(float in) {
douglasc 0:85181831ea03 30 return ((double)in / ANALOG_RANGE) * 100.0;
douglasc 0:85181831ea03 31 }
douglasc 0:85181831ea03 32
douglasc 0:85181831ea03 33 // filter out garbage from the serial line
douglasc 0:85181831ea03 34 const char* validCodes = "abcdefgh0123456789";
douglasc 0:85181831ea03 35 bool isValidCode(char c) {
douglasc 0:85181831ea03 36 for (int i = 0; validCodes[i] != '\0'; i++) {
douglasc 0:85181831ea03 37 if (c == validCodes[i]) {
douglasc 0:85181831ea03 38 return true;
douglasc 0:85181831ea03 39 }
douglasc 0:85181831ea03 40 }
douglasc 0:85181831ea03 41 // pc.printf("invalid code detected: %c \r\n",c);
douglasc 0:85181831ea03 42 return false;
douglasc 0:85181831ea03 43 }
douglasc 0:85181831ea03 44
douglasc 0:85181831ea03 45 char ble_CODE;
douglasc 0:85181831ea03 46 string ble_NUM_str = "";
douglasc 0:85181831ea03 47 int ble_NUM;
douglasc 0:85181831ea03 48 bool serialMessageAvailable = false;
douglasc 0:85181831ea03 49 void bleCallback() {
douglasc 0:85181831ea03 50 if (ble_CODE == 'z') {
douglasc 0:85181831ea03 51 char in = ble.getc();
douglasc 0:85181831ea03 52 if (isValidCode(in)) {
douglasc 0:85181831ea03 53 ble_CODE = in;
douglasc 0:85181831ea03 54 } else {
douglasc 0:85181831ea03 55 return;
douglasc 0:85181831ea03 56 }
douglasc 0:85181831ea03 57 } else {
douglasc 0:85181831ea03 58 char in = ble.getc();
douglasc 0:85181831ea03 59 if (isValidCode(in)) {
douglasc 0:85181831ea03 60 ble_CODE = in;
douglasc 0:85181831ea03 61 } else {
douglasc 0:85181831ea03 62 return;
douglasc 0:85181831ea03 63 }
douglasc 0:85181831ea03 64 ble_NUM_str += ble.getc();
douglasc 0:85181831ea03 65 }
douglasc 0:85181831ea03 66 serialMessageAvailable = true;
douglasc 0:85181831ea03 67 }
douglasc 0:85181831ea03 68
douglasc 0:85181831ea03 69 int main() {
douglasc 0:85181831ea03 70 // servo control for Window automation
douglasc 0:85181831ea03 71 Servo s(D7);
douglasc 0:85181831ea03 72 Window window(& s, 4.0);
douglasc 0:85181831ea03 73 window.closeWindow();
douglasc 0:85181831ea03 74
douglasc 0:85181831ea03 75 // status indicators
douglasc 0:85181831ea03 76 red = 1;
douglasc 0:85181831ea03 77 green = 1;
douglasc 0:85181831ea03 78 blue = 1;
douglasc 0:85181831ea03 79
douglasc 0:85181831ea03 80 // motor for anemometer
douglasc 0:85181831ea03 81 AnalogIn motor(A0);
douglasc 0:85181831ea03 82 AnalogOut motorBase(PTE30);
douglasc 0:85181831ea03 83 motorBase.write(0.1);//(0.5);
douglasc 0:85181831ea03 84 string motorSpeed = "";
douglasc 0:85181831ea03 85
douglasc 0:85181831ea03 86 // serial
douglasc 0:85181831ea03 87 ble.baud(9600);
douglasc 0:85181831ea03 88 ble.format(8,Serial::Odd,1);
douglasc 0:85181831ea03 89 ble.attach(bleCallback);
douglasc 0:85181831ea03 90
douglasc 0:85181831ea03 91 while(1) {
douglasc 0:85181831ea03 92 // check for messages
douglasc 0:85181831ea03 93 if (serialMessageAvailable) {
douglasc 0:85181831ea03 94 switch(ble_CODE) {
douglasc 0:85181831ea03 95 case 'a':
douglasc 0:85181831ea03 96 pc.printf("update minimum wind threshold to: ... \r\n");
douglasc 0:85181831ea03 97 break;
douglasc 0:85181831ea03 98 case 'b':
douglasc 0:85181831ea03 99 pc.printf("update maximum wind threshold to: ... \r\n");
douglasc 0:85181831ea03 100 break;
douglasc 0:85181831ea03 101 case 'c':
douglasc 0:85181831ea03 102 pc.printf("open window. \r\n");
douglasc 0:85181831ea03 103 if (!window.isWindowOpen()) {
douglasc 0:85181831ea03 104 window.openWindow();
douglasc 0:85181831ea03 105 }
douglasc 0:85181831ea03 106 break;
douglasc 0:85181831ea03 107 case 'd':
douglasc 0:85181831ea03 108 pc.printf("close window. \r\n");
douglasc 0:85181831ea03 109 if (window.isWindowOpen()) {
douglasc 0:85181831ea03 110 window.closeWindow();
douglasc 0:85181831ea03 111 }
douglasc 0:85181831ea03 112 break;
douglasc 0:85181831ea03 113 case 'e':
douglasc 0:85181831ea03 114 pc.printf("initializing BLE device. \r\n");
douglasc 0:85181831ea03 115 red = 0;
douglasc 0:85181831ea03 116 wait(0.75);
douglasc 0:85181831ea03 117 red = 1;
douglasc 0:85181831ea03 118 break;
douglasc 0:85181831ea03 119 case 'f':
douglasc 0:85181831ea03 120 pc.printf("restarting ble advertising. \r\n");
douglasc 0:85181831ea03 121 break;
douglasc 0:85181831ea03 122 case 'g':
douglasc 0:85181831ea03 123 pc.printf("current wind speed: ... \r\n");
douglasc 0:85181831ea03 124 break;
douglasc 0:85181831ea03 125 case 'h':
douglasc 0:85181831ea03 126 pc.printf("BLE device disconnected. \r\n");
douglasc 0:85181831ea03 127 break;
douglasc 0:85181831ea03 128 default:
douglasc 0:85181831ea03 129 // pc.printf("uknown code received from BLE device: %c\r\n",ble_CODE);
douglasc 0:85181831ea03 130 break;
douglasc 0:85181831ea03 131 }
douglasc 0:85181831ea03 132 ble_CODE = 'z'; // flag indicating no code set
douglasc 0:85181831ea03 133 serialMessageAvailable = false;
douglasc 0:85181831ea03 134 }
douglasc 0:85181831ea03 135
douglasc 0:85181831ea03 136 // center input at zero (no wind is blowing)
douglasc 0:85181831ea03 137 float val = motor.read() - ANALOG_HALF;
douglasc 0:85181831ea03 138 float absoluteVal = abs(val);
douglasc 0:85181831ea03 139 if (val < 0.0 - ANALOG_DELTA) {
douglasc 0:85181831ea03 140 pc.printf("motor is turning NORTH %02f percent \r\n",getAnalogPercent(absoluteVal));
douglasc 0:85181831ea03 141 } else if (val > 0.0 + ANALOG_DELTA) {
douglasc 0:85181831ea03 142 pc.printf("motor is turning SOUTH %02f percent \r\n",getAnalogPercent(absoluteVal));
douglasc 0:85181831ea03 143 }
douglasc 0:85181831ea03 144 // transmit data to BLE
douglasc 0:85181831ea03 145 ble.printf("%.0f",getAnalogPercent(absoluteVal));
douglasc 0:85181831ea03 146
douglasc 0:85181831ea03 147 wait(.5);
douglasc 0:85181831ea03 148 }
douglasc 0:85181831ea03 149 }