Eric Wu / Mbed 2 deprecated WifiRobot

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wifi_test_main.cpp Source File

wifi_test_main.cpp

00001 #include "mbed.h"
00002 #include "cc3000.h"
00003 #include "IntensityScanner.h"
00004 #include <MMA8451Q.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <stdbool.h>
00008 #include <string.h>
00009 #include "irobot.h"
00010 #include "irobotSensorTypes.h"
00011 #include "accelerometer.h"
00012 #include "TimeoutMultipleSerial.h"
00013 
00014 #define MMA8451_I2C_ADDRESS (0x1d)
00015 
00016 typedef enum a {
00017     FORWARD,
00018     TURN
00019     
00020     } robotState_t;
00021     
00022 static robotState_t robotState = FORWARD;
00023 
00024 void irobotTestNavigate(
00025     int32_t* netDistance,
00026     int32_t* netAngle,
00027     irobotSensorGroup6_t* sensors,
00028     AccelMeasure* acc_meas,
00029     int32_t* leftWheelSpeed,
00030     int32_t* rightWheelSpeed) {
00031 
00032     /*
00033     switch (robotState) {
00034         case FORWARD:
00035             {
00036                 
00037                 if (*netDistance > 100) {
00038                     robotState = TURN;
00039                     *netDistance = 0;
00040                     *netAngle = 0;
00041                     }
00042                 else {
00043                     *leftWheelSpeed = 50;
00044                     *rightWheelSpeed = 50;
00045                     }
00046                 
00047             }
00048         case TURN:
00049             {
00050                 if (*netAngle > 90) {
00051                         *netDistance = 0;
00052                         *netAngle = 0;
00053                         robotState = FORWARD;
00054                     }
00055                 else {
00056                     *leftWheelSpeed = 50;
00057                     *rightWheelSpeed = -50;
00058                     }
00059             }
00060         }
00061         */
00062         *leftWheelSpeed = 100;
00063         *rightWheelSpeed = 100;
00064 }
00065 
00066 // KL25Z wifi connection
00067 // we need to define connection pins for:
00068 // - IRQ      => (pin D3)
00069 // - Enable   => (pin D5)
00070 // - SPI CS   => (pin D10)
00071 // - SPI MOSI => (pin D11)
00072 // - SPI MISO => (pin D12)
00073 // - SPI CLK  => (pin D13)
00074 // plus wifi network SSID, password, security level and smart-configuration flag.
00075 
00076 Serial pc(USBTX, USBRX);
00077 DigitalOut led_red(LED_RED);
00078 DigitalOut led_green(LED_GREEN);
00079 /*
00080 int main () {
00081     // by default, it's red
00082     printf("beginning\r\n");
00083     led_red = 1;
00084     led_green = 0;
00085 
00086     IntensityScanner rssi_scanner = IntensityScanner(PTD7, PTD6, D10, SPI(D11, D12, PTC5), 
00087         SSID, key, mbed_cc3000::WPA2, false, 200);
00088         
00089     uint8_t rssi;
00090     while (1) {
00091         if (rssi_scanner.rssi_for_ssid("attwifi", &rssi) == 0)
00092             printf("Rssi is %d\r\n", rssi);
00093         wait_ms(100);
00094     }
00095 }
00096 */
00097 
00098 int main (int argc, char** argv) {
00099 
00100     MMA8451Q accelerometer(PTE25, PTE24, MMA8451_I2C_ADDRESS);
00101 
00102     TimeoutMultipleSerial port_as_serial = TimeoutMultipleSerial(PTC4, PTC3, 
00103         NULL, 100);
00104     irobotUARTPort_t port = (irobotUARTPort_t) (&port_as_serial);
00105 
00106     int32_t irobotStatus;
00107     
00108     irobotSensorGroup6_t sensors;
00109     int32_t netDistance = 0;
00110     int32_t netAngle = 0;
00111 
00112     int32_t rightWheelSpeed = 0;
00113     int32_t leftWheelSpeed = 0;
00114 
00115     AccelMeasure accelMeasurements(0.0f, 0.0f, 0.0f);
00116     AccelMeasure accelPrevMeasurements(0.0f, 0.0f, 0.0f);
00117 
00118     int32_t status = irobotOpen(port);
00119 
00120     
00121     /*
00122     For debugging only
00123     */
00124     DigitalOut myled(LED2);
00125 
00126     irobotSensorPollSensorGroup6(port, &sensors);
00127     
00128     while (!sensors.buttons.advance || true) {
00129         myled = 1;
00130 
00131         // update from sensors
00132         irobotSensorPollSensorGroup6(port, &sensors);
00133 
00134 
00135 
00136         irobotTestNavigate(&netDistance,
00137             &netAngle,
00138             &sensors,
00139             &accelMeasurements,
00140             &leftWheelSpeed,
00141             &rightWheelSpeed);
00142        
00143         irobotDriveDirect(port, leftWheelSpeed, rightWheelSpeed); 
00144     }
00145 
00146     status = irobotClose(port);
00147     return status;
00148 }