Alex Wong
Published

DAQopter from Digilent Design Contest

DAQopter” is a data acquisition and monitoring system for a wide range of signals using Digilent Wi-Fire and an Android device.

AdvancedFull instructions provided10 hours565
DAQopter from Digilent Design Contest

Things used in this project

Hardware components

Digilent Wi-FIRE: WiFi Enabled PIC32MZ Microcontroller Board
×1

Story

Read more

Schematics

Design Overview

Code

Wi-Fire Code

Arduino
/************************************************************************/
#include <MRF24G.h>                     // This is for the MRF24WGxx on a pmodWiFi or WiFiShield

/************************************************************************/
/*                    Required libraries, Do NOT comment out            */
/************************************************************************/
#include <DEIPcK.h>
#include <DEWFcK.h>

#include <Servo.h>
#include <SPI.h>

/************************************************************************/
/*                                                                      */
/*              SET THESE VALUES FOR YOUR NETWORK                       */
/*                                                                      */
/************************************************************************/

//IPv4 ipServer = {192,168,43,120};
IPv4 ipServer = {192,168,2,120};
unsigned short portServer = DEIPcK::iPersonalPorts44 + 300;     // port 44300

// Specify the SSID
//const char * szSsid = "AndroidAP";
//const char * szPassPhrase = "kjso0851";
const char * szSsid = "c17";
const char * szPassPhrase = "Politehnica";
#define WiFiConnectMacro() deIPcK.wfConnect(szSsid, szPassPhrase, &status)

Servo throttle;
Servo rudder;
Servo aileron;
Servo elevator;
Servo gear;

typedef enum
{
    NONE = 0,
    CONNECT,
    LISTEN,
    ISLISTENING,
    WAITISLISTENING,
    AVAILABLECLIENT,
    ACCEPTCLIENT,
    READ,
    WRITE,
    CLOSE,
    EXIT,
    DONE
} STATE;

STATE state = CONNECT;

unsigned tStart = 0;
unsigned tWait = 5000;
int flightControls[4];
String inString = "";
TCPServer tcpServer;
#define cTcpClients 2
TCPSocket rgTcpClient[cTcpClients];

TCPSocket * ptcpClient = NULL;

byte rgbRead[1024];
byte response[9] = {'m','u','l','t','z','a','m','\r','\n'};
int cbRead = 0;
int count = 0;

//int throttlePin = 9;
//int rudderPin = 10;
//int elePin = 6;
//int ailPin = 5;
//int gearPin = 11;

int throttlePin = 6;
int rudderPin = 9;
int elePin = 5;
int ailPin = 3;
int gearPin = 10;

int throttleSetting = 1220; //1220 us correspnds to 0% thrust on Naza. We want to always know what the throttle setting is for safety reasons.
int gearSetting = 1185;

IPSTATUS status;

void setup() {
    pinMode(28, OUTPUT);
    digitalWrite(28, LOW);    // set the LED off
    throttle.attach(throttlePin);
    rudder.attach(rudderPin);
    aileron.attach(ailPin);
    elevator.attach(elePin);
    gear.attach(gearPin);

    //Initial throttle controls: Zero thrust, all other controls neutral.
    //Imperfections in system necessitate using 1470 as opposed to 1500 in some cases
    flightControls[0] = 1220;
    flightControls[1] = 1470;
    flightControls[2] = 1500;
    flightControls[3] = 1470;
    throttleSetting = flightControls[0]; //Redundant, but done mostly for code clarity.
    throttle.writeMicroseconds(throttleSetting);
    aileron.writeMicroseconds(flightControls[1]);
    rudder.writeMicroseconds(flightControls[2]);
    elevator.writeMicroseconds(flightControls[3]);
    gear.writeMicroseconds(gearSetting); //1525 sets dji naza to "Attitude Hold" mode
                                  //1185 sets to "Manual" Mode
                                  //1855 sets to "GPS" mode




    Serial.begin(115200);
    Serial.println("DaqOpter");
    Serial.println("Copyright 2015");
    Serial.println("");
    Serial.println("throttle = " + (String) flightControls[0]);
    Serial.println("aileron  = " + (String) flightControls[1]);
    Serial.println("rudder   = " + (String) flightControls[2]);
    Serial.println("elevator = " + (String) flightControls[3]);
    Serial.println("");
    for (int i = 0; i < 50; i++)
    {
      throttle.writeMicroseconds(1220);
      aileron.writeMicroseconds(1850);
      rudder.writeMicroseconds(1220);
      elevator.writeMicroseconds(1220);
      delay(20);
    }


}

void loop() {

    switch(state)
    {

        case CONNECT:
//        	Serial.println("State = CONNECT");
            if(WiFiConnectMacro())
            {
                Serial.println("Connection Created");
                deIPcK.begin(ipServer);
                state = LISTEN;
            }
            else if(IsIPStatusAnError(status))
            {
                Serial.print("Unable to connection, status: ");
                Serial.println(status, DEC);
                state = CLOSE;
            }
                    throttle.writeMicroseconds(flightControls[0]);
        aileron.writeMicroseconds(flightControls[1]);
        rudder.writeMicroseconds(flightControls[2]);
        elevator.writeMicroseconds(flightControls[3]);
        throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

            break;

    // say to listen on the port
    case LISTEN:
 //   	Serial.println("State = LISTEN");
        if(deIPcK.tcpStartListening(portServer, tcpServer))
        {
            for(int i = 0; i < cTcpClients; i++)
            {
                tcpServer.addSocket(rgTcpClient[i]);
            }
        }
        state = ISLISTENING;
                throttle.writeMicroseconds(flightControls[0]);
        aileron.writeMicroseconds(flightControls[1]);
        rudder.writeMicroseconds(flightControls[2]);
        elevator.writeMicroseconds(flightControls[3]);
        throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

        break;

    case ISLISTENING:
 //   	Serial.println("State = ISLISTENING");
        count = tcpServer.isListening();
        Serial.print(count, DEC);
        Serial.print(" sockets listening on port: ");
        Serial.println(portServer, DEC);

        if(count > 0)
        {
            state = AVAILABLECLIENT;
        }
        else
        {
            state = WAITISLISTENING;
        }
                throttle.writeMicroseconds(flightControls[0]);
        aileron.writeMicroseconds(flightControls[1]);
        rudder.writeMicroseconds(flightControls[2]);
        elevator.writeMicroseconds(flightControls[3]);
        throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

        break;

    case WAITISLISTENING:
 //   	Serial.println("State = WAITISLISTENING");
        if(tcpServer.isListening() > 0)
        {
            state = ISLISTENING;
        }
                throttle.writeMicroseconds(flightControls[0]);
        aileron.writeMicroseconds(flightControls[1]);
        rudder.writeMicroseconds(flightControls[2]);
        elevator.writeMicroseconds(flightControls[3]);
        throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

        break;

    // wait for a connection
    case AVAILABLECLIENT:
//    	Serial.println("State = AVAILABLECLIENT");
        if((count = tcpServer.availableClients()) > 0)
        {
            Serial.print("Got ");
            Serial.print(count, DEC);
            Serial.println(" clients pending");
            state = ACCEPTCLIENT;
        }
                throttle.writeMicroseconds(flightControls[0]);
        aileron.writeMicroseconds(flightControls[1]);
        rudder.writeMicroseconds(flightControls[2]);
        elevator.writeMicroseconds(flightControls[3]);
        throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

        break;

    // accept the connection
    case ACCEPTCLIENT:
//    	Serial.println("State = ACCEPTCLIENT");
        // accept the client
        if((ptcpClient = tcpServer.acceptClient()) != NULL && ptcpClient->isConnected())
        {
            Serial.println("Got a Connection");
            state = READ;
            tStart = (unsigned) millis();
        }

        // this probably won't happen unless the connection is dropped
        // if it is, just release our socket and go back to listening
        else
        {
            state = CLOSE;
        }
                throttle.writeMicroseconds(flightControls[0]);
        aileron.writeMicroseconds(flightControls[1]);
        rudder.writeMicroseconds(flightControls[2]);
        elevator.writeMicroseconds(flightControls[3]);
        throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

        break;

    // wait fot the read, but if too much time elapses (5 seconds)
    // we will just close the tcpClient and go back to listening
    case READ:
 //   	Serial.println("State = READ");
        // see if we got anything to read
        if((cbRead = ptcpClient->available()) > 0)
        {
            cbRead = cbRead < sizeof(rgbRead) ? cbRead : sizeof(rgbRead);
            cbRead = ptcpClient->readStream(rgbRead, cbRead);
            if (cbRead >= 16)
            {
               Serial.print("Got ");
               Serial.print(cbRead, DEC);
               Serial.println(" bytes\n");
               for(int i=0; i < 4; i++)
               {
            	   inString = "";
            	   for(int j=i*4; j < i*4+4; j++)
            	   	   {
            		   	   inString += (char)rgbRead[j];
            	   	   }
            	   flightControls[i] = inString.toInt();
               }
               Serial.println("throttle = " + (String) flightControls[0]);
               Serial.println("aileron  = " + (String) flightControls[1]);
               Serial.println("rudder   = " + (String) flightControls[2]);
               Serial.println("elevator = " + (String) flightControls[3]);
               Serial.println("");
               throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

            }
            digitalWrite(28, HIGH);   // set the LED on

            state = WRITE;
        }

        // If too much time elapsed between reads, close the connection
        else if( (((unsigned) millis()) - tStart) > tWait )
        {
           state = CLOSE;
        }
//        Serial.println("Armed. t =" + (String) throttleSetting + " a = " + (String) flightControls[1] + " r = " + (String) flightControls[2] + " e = " + (String) flightControls[3]);
        throttle.writeMicroseconds(flightControls[0]);
        aileron.writeMicroseconds(flightControls[1]);
        rudder.writeMicroseconds(flightControls[2]);
        elevator.writeMicroseconds(flightControls[3]);
        throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

        break;

    // echo back the string
    case WRITE:
//    	Serial.println("State = WRITE");
        if(ptcpClient->isConnected())
        {
        	 if (cbRead > 2)
        	            {
            Serial.println("Writing: ");  

//            for(int i=0; i < cbRead; i++)
//            {
//                Serial.print(rgbRead[i], BYTE);
//            }
            for(int i=0; i < 9; i++)
            {
                Serial.print(response[i], BYTE);
            }
            Serial.println("");  

            //

            ptcpClient->writeStream(response, 9);
            }
            //else ptcpClient->writeStream(rgbRead, cbRead);
            state = READ;
            tStart = (unsigned) millis();
        }

        // the connection was closed on us, go back to listening
        else
        {
            Serial.println("Unable to write back.");  
            state = CLOSE;
        }
                throttle.writeMicroseconds(flightControls[0]);
        aileron.writeMicroseconds(flightControls[1]);
        rudder.writeMicroseconds(flightControls[2]);
        elevator.writeMicroseconds(flightControls[3]);
        throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

        break;
        
    // close our tcpClient and go back to listening
    case CLOSE:
 //   	Serial.println("State = CLOSE");
//        ptcpClient->close();
//        tcpServer.addSocket(*ptcpClient);
//        Serial.println("");
//        state = ISLISTENING;
    	digitalWrite(28, LOW);    // set the LED off
    	state = READ;
            throttle.writeMicroseconds(flightControls[0]);
        aileron.writeMicroseconds(flightControls[1]);
        rudder.writeMicroseconds(flightControls[2]);
        elevator.writeMicroseconds(flightControls[3]);
        throttleSetting = flightControls[0]; //We store the value of the throttle for safety during emergencyLanding()

        break;

    // something bad happen, just exit out of the program
    case EXIT:
 //   	Serial.println("State = EXIT");
        tcpServer.close();
        Serial.println("Something went wrong, sketch is done.");  
        state = DONE;
        break;

    // do nothing in the loop
    case DONE:
    default:
        break;
    }

    // every pass through loop(), keep the stack alive
    DEIPcK::periodicTasks();
}

Android File

C/C++
No preview (download only).

Project Report

XML
No preview (download only).

Project Presentation

XML
No preview (download only).

Project Demonstration Video

XML
No preview (download only).

Credits

Angelo Pop & Blosenco Iuri

Posted by Alex Wong

Comments