Uses Arduino to take signals from a Lidar Lite v3 to scan a room. Arduino picks up a change in distance. Sends a signal to the k64. That k64 sets off sounds connected to a speaker. Those sounds are prerecorded on a flash drive. The k64 also flashes and LED that will flash until a turn off signal UDP packet is sent to it. It also sends an alarm UDP packet to another K64 that has flashing LEDs.

Dependencies:   EthernetInterface LidarLitev2 SDFileSystem Servo mbed-rtos mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*------------------------------------------------------------------------------------*/
00002 /*  Ethernet UDP Server (to be used with Ethernet_UDP_client)                         */
00003 /*------------------------------------------------------------------------------------*/
00004 
00005 /*--COMPANY-----AUTHOR------DATE------------REVISION----NOTES-------------------------*/
00006 /*  NXP         mareikeFSL  2015.12.23      rev 1.0     initial                       */
00007 /*                                                                                    */
00008 /*------------------------------------------------------------------------------------*/
00009 /*  This "Hello World" program is used in conjunction with the Ethernet_UDP_client    */
00010 /*  program. It communicates between two FRDM-K64F boards via the Ethernet protocol.  */
00011 /*  To use this program, you need to do the following:                                */
00012 /*      - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable   */
00013 /*        is not required).                                                           */
00014 /*      - Flash one board with Ethernet_UDP_client and the other with                 */
00015 /*        Ethernet_UDP_server                                                         */
00016 /*      - [optional] If you would like to see the "Hello World" output on your        */
00017 /*        monitor, install and open a terminal. Tera Term is used in the Wiki for     */
00018 /*        this program.                                                               */
00019 /*------------------------------------------------------------------------------------*/
00020 
00021 
00022 /*--INCLUDES----------------------------------------------------------------------------*/
00023 #include "mbed.h"
00024 #include "EthernetInterface.h"
00025 #include "SDFileSystem.h"
00026 #include "wave_player.h"
00027 #include "Servo.h"
00028 #include "LidarLitev2.h"
00029 #include "rtos.h"
00030 #include "eth_arch.h"
00031 /*--DEFINES-----------------------------------------------------------------------------*/
00032 #define SWEEP_TIME .1
00033 
00034 
00035 /*--CONSTANTS---------------------------------------------------------------------------*/
00036 Queue<uint32_t, 5> queue;
00037 const int PORT = 7;                             //arbitrary port
00038 
00039 static const char* SERVER_IP = "192.168.1.101"; //IP of server board
00040 static const char* MASK = "255.255.255.0";      //mask
00041 static const char* GATEWAY = "192.168.1.1";     //gateway
00042 
00043 
00044 /*--INITS-------------------------------------------------------------------------------*/
00045 Serial pc(USBTX, USBRX);        //create PC interface
00046 EthernetInterface eth;          //create ethernet
00047 UDPSocket server;               //creat server
00048 Endpoint client;                //create endpoint
00049 
00050 DigitalOut alarmconfirm(PTD0);
00051 DigitalIn alarmcheckpin(PTD1);
00052 DigitalOut red(LED_RED);        //debug led
00053 DigitalOut green(LED_GREEN);    //debug led
00054 DigitalOut led(D12);
00055 SDFileSystem sd(PTE3,PTE1, PTE2, PTE4, "sd"); //SD card
00056 
00057 AnalogOut DACout(DAC0_OUT);
00058 
00059 wave_player waver(&DACout);
00060 
00061 
00062 Timer dt;
00063 /*--VARIABLES---------------------------------------------------------------------------*/
00064 int n;                  //size of received message
00065 char counter[1] = {0};  //sample receive/send buffer
00066 int i=0;
00067 int sweep_flag =0;
00068 int waiting_answer = 0;
00069 Ticker flashes;
00070 //Ticker IO;
00071 
00072 int someflag=0;
00073  FILE *wave_file;
00074 /*--FUNCTION DECLARATIONS---------------------------------------------------------------*/
00075 int sensoralarmcheck(void);
00076 void alarmCheck(char x);
00077 void alarmLight(void);
00078 void alarmSound(void);
00079 void init_usb(void);    //initializes pc.printf if required
00080 void init_eth(void);    //initializes Ethernet
00081 void receive(void);     //receives packets
00082 void queue_isr();
00083 int main(void);         //main
00084 void flashingstuff();
00085 
00086 
00087 /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
00088 /****************************************************************************************/
00089 void flashingstuff(){
00090     if ((someflag==1) || (red==0)){
00091         red=!red;
00092         led=!led;
00093     }  
00094     }
00095 /*queue_thread**************************************************************************/
00096 
00097 
00098 /*****************************************************************************INIT_USB***/
00099 void init_usb(void)
00100 {
00101     pc.baud(9600);    //baud
00102     
00103 }   //end init_usb()
00104 
00105 /*****************************************************************************INIT_ETH***/
00106 void init_eth(void)
00107 {
00108     pc.printf("Initializing Ethernet\r\n");
00109     eth.init(SERVER_IP, MASK, GATEWAY);                                         //set up IP
00110     eth.connect();                                                              //connect ethernet
00111     pc.printf("\nSERVER - Server IP Address is %s\r\n", eth.getIPAddress());    //get server IP address;
00112     
00113     server.bind(PORT);                                                          //bind server
00114     pc.printf("Initialization Complete\n\r");    
00115 }   //end init_eth()
00116 
00117 /******************************************************************************RECEIVE***/
00118 void receive(void)
00119 { 
00120    int herewego=0;
00121      
00122      pc.printf("\nSERVER - Waiting for UDP packet...\r\n");                                      //wait for packet
00123     n = server.receiveFrom(client, counter, sizeof(counter));                                   //receive message from client
00124     counter[n] = '\0';                                                                          //add \0 to end of message
00125     
00126    
00127     pc.printf("SERVER - Received '%i' from client %s\r\n", counter[0], client.get_address());   //print message and client
00128     alarmCheck(counter[0]);
00129   herewego = sensoralarmcheck();
00130  
00131     if (herewego==1||waiting_answer==1){
00132         waiting_answer =1;
00133         printf("Lidar Caught something\r\n");
00134         counter[0]='z';
00135          }
00136     pc.printf("SERVER - Sending '%i' back to client %s\r\n", counter[0], client.get_address()); //print sending back
00137     wait(.5f);
00138    
00139     server.sendTo(client, counter, n);            
00140                                                   //send message
00141   }   //end receive()
00142 
00143 /***Alarm Check***************************************************************************/
00144 void alarmCheck(char x){
00145   pc.printf("\n\rInto alarm check\r\n");
00146     if (x == 'x') {
00147         alarmconfirm =0;
00148         waiting_answer=0;
00149         someflag=0;
00150         }
00151     if (x!='b') return;
00152     alarmLight();
00153     alarmSound();
00154     
00155  }
00156  
00157  
00158  
00159  
00160 /**Alarm_Sound****************************************************************************/
00161 
00162 void alarmSound(void){
00163     
00164         if (i==0){
00165         wave_file=fopen("/sd/intuder_detected.wav","r");
00166         } 
00167         else if (i==1){
00168              wave_file=fopen("/sd/alarm.wav","r");
00169              }
00170              else if(i==2){
00171                   wave_file=fopen("/sd/20seconds.wav","r");
00172                   }
00173                   else if (i==3){
00174                        wave_file=fopen("/sd/dangerous.wav","r");
00175                        }
00176                        else if (i==4){
00177                             wave_file=fopen("/sd/searching.wav","r");
00178                             }
00179                             else if (i==5){
00180                                  wave_file=fopen("/sd/scanning.wav","r");
00181                                  }
00182            
00183              
00184         waver.play(wave_file);
00185         fclose(wave_file);
00186         i++;
00187         if(i>=6){
00188             i=0;
00189             }
00190         }
00191        
00192             
00193 /**Alarm_lights**************************************************************************/
00194 void alarmLight(void){
00195     
00196      someflag = 1;
00197            wait(0.1f);
00198      }
00199     
00200 /***lidarSweep****************************************************************************/
00201    int sensoralarmcheck(void){
00202    if (alarmcheckpin){
00203        alarmLight();
00204        alarmSound();
00205        return 1;
00206        }
00207        return 0;
00208        }
00209 /*********************************************************************************MAIN***/
00210 int main(void)
00211 {
00212 
00213     red = 1;
00214     green = 0;      //server
00215 
00216     init_usb();     //initialize the PC interface
00217     init_eth();     //initialize the Ethernet connection
00218      flashes.attach(&flashingstuff,.1);
00219 
00220     while (true)    //repeat forever
00221     {        
00222            
00223              receive();  //wait for message
00224        
00225     }
00226 
00227 }   //end main()