aa

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 int irDistance[6] = {0,3,5,99,33,11};
00025 
00026 void dev_rx()
00027 {
00028     static uint8_t count = 0;
00029     static uint8_t RecData[RECEIVE_DATA_NUM];
00030     static uint8_t last_data;
00031     
00032     RecData[count] = device2.getc();
00033     
00034     if(RecData[KEY] == KEYCODE){
00035         count++;
00036     }else{
00037         count = 0;
00038     }
00039     if(count >= RECEIVE_DATA_NUM){
00040         if(RecData[CHECK] == CHECKCODE){
00041             //mbedleds = 15;
00042             
00043             if((RecData[DIRECTION] <= 15) || (RecData[DIRECTION] == 200)){
00044                 direction = RecData[DIRECTION];
00045             }
00046             if(RecData[DISTANCE] <= 180){
00047                 Distance = RecData[DISTANCE];
00048             }
00049             
00050             ultrasonicVal[0] = (int)((RecData[SONIC1_1] + (RecData[SONIC1_2] << 8)) / 10.0);
00051             if(ultrasonicVal[0] == 6553)ultrasonicVal[0] = 0xFFFF;
00052             ultrasonicVal[1] = (int)((RecData[SONIC2_1] + (RecData[SONIC2_2] << 8)) / 10.0);
00053             if(ultrasonicVal[1] == 6553)ultrasonicVal[1] = 0xFFFF;
00054             ultrasonicVal[2] = (int)((RecData[SONIC3_1] + (RecData[SONIC3_2] << 8)) / 10.0);
00055             if(ultrasonicVal[2] == 6553)ultrasonicVal[2] = 0xFFFF;
00056             ultrasonicVal[3] = (int)((RecData[SONIC4_1] + (RecData[SONIC4_2] << 8)) / 10.0);
00057             if(ultrasonicVal[3] == 6553)ultrasonicVal[3] = 0xFFFF;
00058             
00059             if((RecData[IR_FOUND] == 0) || (RecData[IR_FOUND] == 1)){
00060                 IR_found = RecData[IR_FOUND];
00061             }
00062             xbee = RecData[XBEE];
00063             xbee = 0;
00064             
00065             
00066             //pc.printf("%f\t%f\t%f\t%f\n",ultrasonicVal[0],ultrasonicVal[1],ultrasonicVal[2],ultrasonicVal[3]);
00067             //pc.printf("%d\n",xbee);
00068             //pc.printf("%d\n",IR_found);
00069             
00070             diff = last_data - Distance;
00071             last_data = Distance;
00072         }
00073         count = 0;
00074     }  
00075 }
00076 
00077 void dev_tx()
00078 {
00079     static uint8_t count2 = 0;
00080     static uint8_t SendData[SEND_DATA_NUM];
00081     
00082     if(count2 >= SEND_DATA_NUM){
00083         SendData[KEY2]     = KEYCODE2;
00084         SendData[DATA1]    = ((int)(compass.sample())) >> 8 ;
00085         SendData[DATA2]    = (int)(compass.sample());
00086         SendData[DATA3]    = state;
00087         SendData[DATA4]    = 1;
00088         SendData[DISTANCE1]      = irDistance[0] >> 8;
00089         SendData[DISTANCE1_2]    = irDistance[0];
00090         SendData[DISTANCE2]      = irDistance[1] >> 8;
00091         SendData[DISTANCE2_2]    = irDistance[1];
00092         SendData[DISTANCE3]      = irDistance[2] >> 8;
00093         SendData[DISTANCE3_2]    = irDistance[2];
00094         SendData[DISTANCE4]      = irDistance[3] >> 8;
00095         SendData[DISTANCE4_2]    = irDistance[3];
00096         SendData[DISTANCE5]      = irDistance[4] >> 8;
00097         SendData[DISTANCE5_2]    = irDistance[4];
00098         SendData[DISTANCE6]      = irDistance[5] >> 8;
00099         SendData[DISTANCE6_2]    = irDistance[5];
00100         SendData[CHECK2]   = CHECKCODE2;
00101         
00102         count2 = 0;
00103         
00104     }
00105     device2.putc(SendData[count2]);
00106     
00107     count2++;
00108 }