final cup

Dependencies:   HMC6352 PID mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers uart1.cpp Source File

uart1.cpp

00001 
00002 #include "mbed.h"
00003 #include "uart1.h"
00004 #include "HMC6352.h"
00005 
00006 extern Serial pc;
00007 extern Serial device2;
00008 extern HMC6352 compass;
00009 extern BusOut mbedleds;
00010 extern DigitalOut led1; 
00011 extern DigitalOut led2;
00012 extern DigitalOut led3;
00013 extern DigitalOut led4;
00014 
00015 extern uint8_t state;
00016 extern uint8_t hold_flag;
00017 
00018 int diff;
00019 uint16_t ultrasonicVal[4];
00020 uint8_t direction;
00021 uint8_t Distance;
00022 uint8_t IR_found;
00023 uint8_t xbee;
00024 
00025 void dev_rx()
00026 {
00027     static uint8_t count = 0;
00028     static uint8_t RecData[RECEIVE_DATA_NUM];
00029     static uint8_t last_data;
00030     
00031     
00032     
00033     RecData[count] = device2.getc();
00034     
00035     if(RecData[KEY] == KEYCODE){
00036         count++;
00037         
00038     }else{
00039         count = 0;
00040     }
00041     if(count >= RECEIVE_DATA_NUM){
00042         if(RecData[CHECK] == CHECKCODE){
00043             //mbedleds = 15;
00044             
00045             if((RecData[DIRECTION] <= 15) || (RecData[DIRECTION] == 200)){
00046                 direction = RecData[DIRECTION];
00047             }
00048             if(RecData[DISTANCE] <= 180){
00049                 Distance = RecData[DISTANCE];
00050             }
00051             
00052             ultrasonicVal[0] = (int)((RecData[SONIC1_1] + (RecData[SONIC1_2] << 8)) / 10.0);
00053             if(ultrasonicVal[0] == 6553)ultrasonicVal[0] = 0xFFFF;
00054             ultrasonicVal[1] = (int)((RecData[SONIC2_1] + (RecData[SONIC2_2] << 8)) / 10.0);
00055             if(ultrasonicVal[1] == 6553)ultrasonicVal[1] = 0xFFFF;
00056             ultrasonicVal[2] = (int)((RecData[SONIC3_1] + (RecData[SONIC3_2] << 8)) / 10.0);
00057             if(ultrasonicVal[2] == 6553)ultrasonicVal[2] = 0xFFFF;
00058             ultrasonicVal[3] = (int)((RecData[SONIC4_1] + (RecData[SONIC4_2] << 8)) / 10.0);
00059             if(ultrasonicVal[3] == 6553)ultrasonicVal[3] = 0xFFFF;
00060             
00061             if((IR_found == 0) || (IR_found == 1)){
00062                 IR_found = RecData[IR_FOUND];
00063             }
00064             xbee = RecData[XBEE];
00065             
00066             
00067             //pc.printf("%f\t%f\t%f\t%f\n",ultrasonicVal[0],ultrasonicVal[1],ultrasonicVal[2],ultrasonicVal[3]);
00068             //pc.printf("%d\n",xbee);
00069             //pc.printf("%d\n",IR_found);
00070             
00071             diff = last_data - Distance;
00072             last_data = Distance;
00073         }
00074         count = 0;
00075     }  
00076 }
00077 
00078 void dev_tx()
00079 {
00080     static uint8_t count2 = 0;
00081     static uint8_t SendData[SEND_DATA_NUM];
00082     
00083     if(count2 >= SEND_DATA_NUM){
00084         SendData[KEY2]     = KEYCODE2;
00085         SendData[DATA1]    = ((int)(compass.sample())) >> 8 ;
00086         SendData[DATA2]    = (int)(compass.sample());
00087         SendData[DATA3]    = state;
00088         SendData[DATA4]    = 1;
00089         SendData[CHECK2]   = CHECKCODE2;
00090         
00091         count2 = 0;
00092         
00093     }
00094     device2.putc(SendData[count2]);
00095     
00096     count2++;
00097 }