The controller for a NERF turret, allowing it to track and fire at a human face with visual input gotten by polling a web server. This is part of the final project of the EE149 course of UC Berkeley. The project name is "Pew Pew".

Dependencies:   HTTPClient TSI cc3000_hostdriver_mbedsocket mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers camera.h Source File

camera.h

00001 /**
00002 *
00003 *   Code to get data from the camera by connecting to a web server via WiFi (using Adafruit CC3000).
00004 *
00005 *   author: Leo Kam
00006 *
00007 *
00008 *   The code to connect to the web via Adafruit is adapted from the code by Ben Zhang and Antonio Iannopollo, found at 
00009 *   http://developer.mbed.org/users/nebgnahz/code/CC3000_demo/
00010 *   The following libraries are used:
00011 *    - cc3000_hostdriver_mbedsocket
00012 *     (http://developer.mbed.org/users/Kojto/code/cc3000_hostdriver_mbedsocket/)
00013 *    - HTTPClient (http://developer.mbed.org/users/donatien/code/HTTPClient/)
00014 *
00015 **/
00016 
00017 
00018 #include "mbed.h"
00019 #include "cc3000.h"
00020 #include "HTTPClient.h"
00021 
00022 // KL25Z wifi connection
00023 // we need to define connection pins for:
00024 // - IRQ      => (pin D3)
00025 // - Enable   => (pin D5)
00026 // - SPI CS   => (pin D10)
00027 // - SPI MOSI => (pin D11)
00028 // - SPI MISO => (pin D12)
00029 // - SPI CLK  => (pin D13)
00030 // plus wifi network SSID, password, security level and smart-configuration flag.
00031 mbed_cc3000::cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13),
00032                          "EECS-PSK", "Thequickbrown", WPA2, false);
00033 
00034 // create an http instance
00035 HTTPClient http;
00036 
00037 // str is used to hold the response data
00038 char str[512];
00039 char * p;
00040 
00041 // setup the serial connection, and LEDs
00042 Serial pc(USBTX, USBRX);
00043 DigitalOut led_red(LED_RED);
00044 DigitalOut led_green(LED_GREEN);
00045 
00046 void initializeWiFi() {
00047     // by default, it's red
00048     led_red = 0;
00049     led_green = 1;
00050     
00051     //print message to indicate the program has started
00052     pc.printf("CC3000 Sample Program\r\n");
00053     wifi.init();
00054     pc.printf("Wifi Initialized\r\n");
00055     
00056     // check connection status
00057     while(wifi.is_connected() == false) {
00058         // try to connect
00059         if (wifi.connect() == -1) {
00060             pc.printf("Failed to connect."
00061                       "Please verify connection details and try again.\r\n");
00062             led_red = 0;
00063             led_green = 1;
00064         } else {
00065             pc.printf("IP address: %s \r\n", wifi.getIPAddress());
00066             
00067             //once connected, turn green LED on and red LED off, and exit loop
00068             led_red = 1;
00069             led_green = 0;
00070             break;
00071         }
00072     }
00073 }
00074 
00075 
00076 /* Get the x, y-coordinates and depth from camera. Return true if successfuly retrieve data from server, else return false and do not change parameters. */
00077 bool getCameraData(double &x, double &y, double &z, int timeout=2000) {
00078     // check connection status
00079     while(wifi.is_connected() == false) {
00080         // try to connect
00081         if (wifi.connect() == -1) {
00082             pc.printf("Failed to connect."
00083                       "Please verify connection details and try again.\r\n");
00084             led_red = 0;
00085             led_green = 1;
00086         } else {
00087             pc.printf("IP address: %s \r\n", wifi.getIPAddress());
00088             
00089             //once connected, turn green LED on and red LED off, and exit loop
00090             led_red = 1;
00091             led_green = 0;
00092             break;
00093         }
00094     }
00095     int ret = http.get("https://pew-pew-pew.herokuapp.com/position", str, 512);
00096     if (!ret) {
00097         p = strtok(str, " ");
00098         x = atof(p);
00099         p = strtok(NULL, " ");
00100         y = atof(p);
00101         p = strtok(NULL, " ");
00102         z = atof(p);
00103         pc.printf("Camera data: %f %f %f\r\n", x, y, z);
00104         return true;
00105     } else {
00106         /*pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
00107                   ret,
00108                   http.getHTTPResponseCode());*/
00109         return false;
00110     }
00111 }