kumar singh
/
Dealer_23Feb
Dummy Lora Packet Sending
Fork of Dealer_18feb17 by
Diff: main.cpp
- Revision:
- 16:7703b9d92326
- Parent:
- 15:a448e955b8f3
- Child:
- 17:758fb8454ab0
--- a/main.cpp Sun Feb 12 18:38:39 2017 +0000 +++ b/main.cpp Fri Feb 17 08:30:35 2017 +0000 @@ -10,34 +10,36 @@ typedef unsigned char uint8; typedef unsigned int uint16; -//peripheral connection +uint8 OBD_Plug_In_Status = FALSE; + +//peripheral connection DigitalOut led1(LED1); DigitalOut led2(LED2); //Configure Serial port - RawSerial LORA_UART(PA_0, PA_1);//USART4_TX->PA_0,USART4_RX->PA_1 : Used for Lora module command sending and reception from gateway - RawSerial DEBUG_UART(PA_9, PA_10);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only - RawSerial Beacon_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5 : Used for sending command to beacon module - RawSerial BLE_RECEIVER_UART(PA_0, PA_1);//USART4_TX->PA_0,USART4_RX->PA_1 : Used for Lora module command sending and reception from gateway - - //InterruptIn OBD_PLUGIN_INTERRUPT_PIN(PC_13); - InterruptIn CheckIn_Interrupt(PC_13); - - uint8 Ticker_Count = 0; //Variable to count for timer overflows +RawSerial LORA_UART(PA_0, PA_1);//USART4_TX->PA_0,USART4_RX->PA_1 : Used for Lora module command sending and reception from gateway +RawSerial DEBUG_UART(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only +RawSerial Beacon_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5 : Used for sending command to beacon module +RawSerial BLE_RECEIVER_UART(PA_9, PA_10);//USART1_TX->PA_0,USART1_RX->PA_1 : Used for Lora module command sending and reception from gateway + +//InterruptIn OBD_PLUGIN_INTERRUPT_PIN(PC_13); +InterruptIn CheckIn_Interrupt(PC_13); + +uint8 Ticker_Count = 0; //Variable to count for timer overflows + - - uint8 OBD_Plugin_Detected = FALSE; - - //Create Object for structure of Lora Packet to be sent +uint8 OBD_Plugin_Detected = FALSE; + +//Create Object for structure of Lora Packet to be sent - static uint16 Calculate_Wheels_RPM(uint8* Buffer); - void flip_Packet_Sending_Flag(); - void Lora_Periodic_Packet_Sending_thread(void const *args); +static uint16 Calculate_Wheels_RPM(uint8* Buffer); +void flip_Packet_Sending_Flag(); +void Lora_Periodic_Packet_Sending_thread(void const *args); - void Lora_Rcvd_Cmd_Processing_thread(void);// const *args); - void Enable_CheckIN_Packet_Sending(); - - uint8 GET_RSSI[]= {0x41,0x54,0x01,0x01,0x0D,0x0A}; +void Lora_Rcvd_Cmd_Processing_thread(void);// const *args); +void Enable_CheckIN_Packet_Sending(); + +const char GET_RSSI[]= {0x41,0x54,0x01,0x01,0x0D,0x0A}; /*************************Accelerometer related definitions***********************************/ //Accelerometer related definitions @@ -45,7 +47,7 @@ #define ACTIVITY_INTERRUPT 0x10 #define INACTIVITY_INTERRUPT 0x08 -#define TAP_THRESHOLD 75 +#define TAP_THRESHOLD 75 #define ACTIVITY_THRESHOLD 64 // THRES_ACT register value 62.5mg/LSB , therfore value 32 indicates 2g activity #define INACTIVITY_THRESHOLD 50 @@ -53,16 +55,14 @@ #define LATENT_TIME 0x15 // The interrupt latency #define WINDOW_TIME 0x45 // The time of the interrupt window in which the next tap will be detected #define INACTIVITY_VALIDATION_TIME 5 // The time until which the acceleration must be held below the inactivity threshold to generate an inactvity interrupt - // Here the value 5 indicates literally 5 secs +// Here the value 5 indicates literally 5 secs #define X_AXIS_OFFSET 0x7F #define Y_AXIS_OFFSET 0x7F #define Z_AXIS_OFFSET 0x05 +I2C i2c(PB_9, PB_8); -I2C i2c(PB_9, PB_8); -RawSerial DEBUG_UART1(PA_9, PA_10);//USART1_TX->PA_9,USART1_RX->PA_10 - -InterruptIn activity(PB_0); +InterruptIn activity(PB_0); InterruptIn inactivity(PA_4); // As for now only this is used DigitalOut led(LED1); @@ -88,9 +88,6 @@ char measure_bit_on_command[2]; -char previous_state = 0; -char current_state = 0; - unsigned char vehicle_speed = 25; // Kmph unsigned char current_speed, previous_speed, speed_threshold = 10; // Kmph @@ -101,72 +98,98 @@ unsigned char Motion_Type_Detected = MOTION_TYPE_UNKNOWN; //By default set motion type as unknown void Accelerometer_Process_thread();//void const *args) ; - #define BLE_RECEIVER_UART_RX_Size 100 - uint8 BLE_RxBuffer_End_Pos = 0; - char BLE_Receiver_UART_RX_Buffer[BLE_RECEIVER_UART_RX_Size]; +#define BLE_RECEIVER_UART_RX_Size 100 +uint8 BLE_RxBuffer_End_Pos = 0; +char BLE_Receiver_UART_RX_Buffer[BLE_RECEIVER_UART_RX_Size]; //This function is Interrupt routine for detecting motion(acceleartion), i.e. either starts from rest/vehicle stops afeter running or if sudden jurk is detected void interrupt_activity_inactivity() -{ - i2c.write(slave_address_acc, all_interrupt_clear_command, 2); - Motion_Detect_Status = TRUE; +{ + i2c.write(slave_address_acc, all_interrupt_clear_command, 2); + Motion_Detect_Status = TRUE; } -/************************************************************************/ +/************************************************************************/ uint8 Command_Sent[100]; uint8 Command_Length_Sent; uint8 Checkin_Detect_Status = FALSE; void Extract_Received_Lora_Response(void); - - //This function is Interrupt routine for detecting OBD Plugin and Out + +char previous_state = 0; +char current_state = 0; + +uint8 OBD_PlugIN_State=0; +uint8 OBD_PlugIN_State1=0; +uint8 OBD_PlugIN_State2=0; +uint8 OBD_PlugIN_Temp_State=1; + +void OBD_Plug_IN_Interrupt() +{ + if(OBD_PlugIN_State1!=OBD_PlugIN_Temp_State) + { + OBD_PlugIN_State1=1; + OBD_PlugIN_State=!OBD_PlugIN_State; + OBD_PlugIN_Temp_State=OBD_PlugIN_State; + OBD_PlugInOut_IOC_Status = TRUE; + } +} + +void OBD_Plug_OUT_Interrupt() +{ + if(OBD_PlugIN_State2!=OBD_PlugIN_Temp_State) + { + OBD_PlugIN_State2=0; + OBD_PlugIN_State=!OBD_PlugIN_State; + OBD_PlugIN_Temp_State=OBD_PlugIN_State; + OBD_PlugInOut_IOC_Status = TRUE; + } +} + +//This function is Interrupt routine for detecting OBD Plugin and Out void Handle_CheckIn_Interrupt() -{ +{ OBD_PlugInOut_IOC_Status = TRUE; DEBUG_UART.printf("Movement_Detected"); } - //Declare Ticker for sending lora packet - Ticker Lora_Packet_Sending_Ticker; - void flip_Packet_Sending_Flag(void) - { //flip function - if(Ticker_Count < 5) - { +//Declare Ticker for sending lora packet +Ticker Lora_Packet_Sending_Ticker; +void flip_Packet_Sending_Flag(void) +{ + //flip function + if(Ticker_Count < 5) { Ticker_Count++; - } - else - { + } else { Ticker_Count = 0; Send_Lora_Packet_Flag = TRUE; } - } +} // called every time a byte is received from lora module. void Lora_onDataRx() { - while (LORA_UART.readable()) - { // while there is data waiting + while (LORA_UART.readable()) { + // while there is data waiting LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos++] = LORA_UART.getc(); // put it in the buffer //DEBUG_UART.putc(LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos-1]); - if(Lora_RxBuffer_End_Pos >= LORA_UART_RX_Size) - { + if(Lora_RxBuffer_End_Pos >= LORA_UART_RX_Size) { // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. // For now just throw everything away. Lora_RxBuffer_End_Pos = 0; } } -} +} - + // called every time a byte is received from Beacon Module. void Beacon_onDataRx() { - while (Beacon_UART.readable()) - { // while there is data waiting + while (Beacon_UART.readable()) { + // while there is data waiting Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos++] = Beacon_UART.getc(); // put it in the buffer //DEBUG_UART.putc(LORA_UART_RX_Buffer[Beacon_RxBuffer_End_Pos-1]); - if(Beacon_RxBuffer_End_Pos >= 100) - { + if(Beacon_RxBuffer_End_Pos >= 100) { // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. // For now just throw everything away. Beacon_RxBuffer_End_Pos = 0; @@ -174,27 +197,27 @@ } } - void BLE_Receiver_onDataRx(void) +void BLE_Receiver_onDataRx(void) { - while (BLE_RECEIVER_UART.readable()) - { // while there is data waiting + while (BLE_RECEIVER_UART.readable()) { + // while there is data waiting BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos++] = BLE_RECEIVER_UART.getc(); // put it in the buffer - //DEBUG_UART.putc(LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos-1]); - if(BLE_RxBuffer_End_Pos >= BLE_RECEIVER_UART_RX_Size) - { + //DEBUG_UART.putc(BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos-1]); + if(BLE_RxBuffer_End_Pos >= BLE_RECEIVER_UART_RX_Size) { // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. // For now just throw everything away. BLE_RxBuffer_End_Pos = 0; } } -} - - int main() +} + +int main() { DEBUG_UART.baud(115200); + BLE_RECEIVER_UART.baud(115200); DEBUG_UART.printf("%s","Debugging started"); + BLE_RECEIVER_UART.attach(&BLE_Receiver_onDataRx, Serial::RxIrq); LORA_UART.attach(&Lora_onDataRx, Serial::RxIrq); - BLE_RECEIVER_UART.attach(&BLE_Receiver_onDataRx, Serial::RxIrq); //Create a thread to read vehicle data //Thread OBD_thread(OBD_Rcvd_Cmd_Processing_thread); @@ -203,34 +226,31 @@ // OBD_PLUGIN_INTERRUPT_PIN.rise(&Enable_CheckIN_Packet_Sending); // call toggle function on the rising edge //led2_thread is executing concurrently with main at this point - CheckIn_Interrupt.rise(&Handle_CheckIn_Interrupt); + CheckIn_Interrupt.fall(&OBD_Plug_IN_Interrupt); + CheckIn_Interrupt.rise(&OBD_Plug_OUT_Interrupt); inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge - Initialize_Beacon_Module(); + //Initialize_Beacon_Module(); Lora_Periodic_Packet_Sending(); //Infinite loop for sending and receiving lora response, no return from here -} +} - //Function to be called when Interrupt is genearted for motion sensing, checkin - void Initialize_Packets_Sent_Count(void) - { +//Function to be called when Interrupt is genearted for motion sensing, checkin +void Initialize_Packets_Sent_Count(void) +{ Motion_Packet_Sent_Count = 0x00; CheckIN_Packet_Sent_Count = 0x00; - } - - uint8 Status_Packet_Wait_Count = 0; -void Lora_Periodic_Packet_Sending() +} + +uint8 Status_Packet_Wait_Count = 0; +void Lora_Periodic_Packet_Sending() { DEBUG_UART.printf("Periodic packet sending intiialized"); Set_Up_Lora_Network_Configuration(); - Initialize_lora_Packets(); - while (true) - { - if(Packet_Type_To_Send == HEARTBEAT_TYPE_PACKET) //check if packet to be sent is Heartbeat packet - { - if(Send_Lora_Packet_Flag) //Check if packet sending is enabled, Packet should be sent only when enabled after timeout period - { + Initialize_lora_Packets(); + while (true) { + if(Packet_Type_To_Send == HEARTBEAT_TYPE_PACKET) { //check if packet to be sent is Heartbeat packet + if(Send_Lora_Packet_Flag) { //Check if packet sending is enabled, Packet should be sent only when enabled after timeout period Status_Packet_Wait_Count++; - if(Status_Packet_Wait_Count < 5) - { + if(Status_Packet_Wait_Count < 5) { Send_RSSI_Request_Command(GET_RSSI); Send_HeartBeat_Packet(); //call function to send heartbeat packet DEBUG_UART.printf("Sent HeartBeat Packet"); @@ -239,9 +259,8 @@ Get_Lora_Response(); Send_Lora_Packet_Flag = FALSE; DEBUG_UART.printf("Heartbeat Packet Response Received"); - } - else - { + } else { + Send_RSSI_Request_Command(GET_RSSI); Status_Packet_Wait_Count = 0; Send_Vehicle_Status_Packet(); //call function to send heartbeat packet DEBUG_UART.printf("Sent Status Packet"); @@ -252,11 +271,9 @@ DEBUG_UART.printf("Status Packet Response Received"); } } - } - else if(Packet_Type_To_Send == MOTION_TYPE_PACKET) //check if packet to be sent is motion packet - { - if(Send_Lora_Packet_Flag) //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period - { + } else if(Packet_Type_To_Send == MOTION_TYPE_PACKET) { //check if packet to be sent is motion packet + if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period + Send_RSSI_Request_Command(GET_RSSI); Send_Motion_Packet(); DEBUG_UART.printf("Sent Motion Packet"); //call function to send periodic motion packet AT_Response_Receive_Status = FAILURE; @@ -264,21 +281,19 @@ Get_Lora_Response(); DEBUG_UART.printf("Motion Packet Response Received"); Send_Lora_Packet_Flag = FALSE; - if(Motion_Packet_Sent_Count >= 5) //Stop Sending Motion Packets if after sending for 2 minute - { + if(Motion_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute DEBUG_UART.printf("Packet Type Sending Changed to HeartBeat"); Motion_Packet_Sent_Count = 0; Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet - Lora_Packet_Sending_Ticker.detach(); //destroy ticker + Lora_Packet_Sending_Ticker.detach(); //destroy ticker Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 10.0); //create new ticker - + } - } - } - else if(Packet_Type_To_Send == CHECKIN_TYPE_PACKET) //check if packet to be sent is Checkin packet - { - if(Send_Lora_Packet_Flag) //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period - { + } + } else if(Packet_Type_To_Send == CHECKIN_TYPE_PACKET) { //check if packet to be sent is Checkin packet + if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period + DEBUG_UART.printf("Sent Beacon ID request"); + Send_RSSI_Request_Command(GET_RSSI); Send_CheckIN_Packet(); //call function to send periodic checkIn packet DEBUG_UART.printf("Sent Checkin Packet"); AT_Response_Receive_Status = FAILURE; @@ -286,48 +301,33 @@ Get_Lora_Response(); Send_Lora_Packet_Flag = FALSE; DEBUG_UART.printf("Checkin Packet Response Received"); - if(CheckIN_Packet_Sent_Count >= 5) //Stop Sending Motion Packets if after sending for 2 minute - { + if(CheckIN_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute DEBUG_UART.printf("Packet Type Sending Changed to HeartBeat"); CheckIN_Packet_Sent_Count = 0; Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet Initialize_Packets_Sent_Count(); } } - } - if(OBD_PlugInOut_IOC_Status) //Check if Accelerometer Interrupt is generated - { + } + if(OBD_PlugInOut_IOC_Status) { //Check if Accelerometer Interrupt is generated //Enable_CheckIN_Packet_Sending(); //Get_Acceleration_Type(); OBD_PlugInOut_IOC_Status = FALSE; - Checkin_Detect_Status ^= 0x01; Packet_Type_To_Send = CHECKIN_TYPE_PACKET; Send_Lora_Packet_Flag = TRUE; - if(Checkin_Detect_Status) //OBD Plugin detected - { - wait(1); //wait for 1sec to avoid detecting plugin debounce - CheckIn_Interrupt.fall(&Handle_CheckIn_Interrupt); //Change interrupt on change to falling type to detect plugout - //write code to enable motion interrupt - } - else //OBD Plugout detecetd - { - CheckIn_Interrupt.rise(&Handle_CheckIn_Interrupt); //Change interrupt on change to rising type to detect plugin - wait(1); //wait for 1sec to avoid detecting plugin debounce - //write code to disable motion interrupt - } + wait(1); //wait for 1sec to avoid detecting plugin debounce } - if(Motion_Detect_Status) //Check if Accelerometer Interrupt is generated - { + if(Motion_Detect_Status) { //Check if Accelerometer Interrupt is generated Get_Acceleration_Type(); Motion_Detect_Status = FALSE; } - } + } } void Get_Acceleration_Type(void) { Packet_Type_To_Send = MOTION_TYPE_PACKET; //whenever acceleration is detected change the packet type to be sent to motion type - Lora_Packet_Sending_Ticker.detach(); //destroy ticker + Lora_Packet_Sending_Ticker.detach(); //destroy ticker Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 5.0); //create new ticker, time inetrval value to be changed Send_Lora_Packet_Flag = 1; //set flag to send motion packet as soon as motion is detecetd Motion_Lora_Packet.Acceleration_Type = Motion_Type_Detected; //Read Type of motion deteceted @@ -335,7 +335,7 @@ } /* //This function is used to enable checkin packet sending once OBD device is plugged in -void Enable_CheckIN_Packet_Sending() +void Enable_CheckIN_Packet_Sending() { // Enable_CheckIN_Packet_Sending = TRUE; //set status to true if(Command_Received[0] == 0xFE) //check start of frame of packet received. every packet must start with 0xFE @@ -350,24 +350,70 @@ } } */ -void Send_RSSI_Request_Command(uint8* Command_To_Send) +void Send_RSSI_Request_Command(const char* Command_To_Send) { BLE_RECEIVER_UART.printf(Command_To_Send); + Get_Fixed_Beacon_RSSI(); } -void Get_Beacon_Receiver_Response(void) +void Get_Fixed_Beacon_RSSI(void) { - static uint16 Temp_Pos1; - while(true) + uint8 i; + uint8 Temp_Pos=0; + Fixed_Beacon_Packet.Parking1_Beacon_ID[0] = 0xA0; + Fixed_Beacon_Packet.Parking1_Beacon_ID[1] = 0xAC; + Fixed_Beacon_Packet.Parking1_Beacon_ID[2] = 0x51; + Fixed_Beacon_Packet.Parking1_Beacon_ID[3] = 0x3F; + Fixed_Beacon_Packet.Parking1_Beacon_ID[4] = 0x91; + Fixed_Beacon_Packet.Parking1_Beacon_ID[5] = 0xE0; + Fixed_Beacon_Packet.Parking1_Beacon_Minor = 0x0002; + Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 0xC3; + Fixed_Beacon_Packet.Parking2_Beacon_ID[0] = 0xDC; + Fixed_Beacon_Packet.Parking2_Beacon_ID[1] = 0xA3; + Fixed_Beacon_Packet.Parking2_Beacon_ID[2] = 0x6D; + Fixed_Beacon_Packet.Parking2_Beacon_ID[3] = 0x30; + Fixed_Beacon_Packet.Parking2_Beacon_ID[4] = 0x38; + Fixed_Beacon_Packet.Parking2_Beacon_ID[5] = 0xDF; + Fixed_Beacon_Packet.Parking2_Beacon_Minor = 0x0004; + Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 0xB4; + Fixed_Beacon_Packet.Parking3_Beacon_ID[0] = 0x6C; + Fixed_Beacon_Packet.Parking3_Beacon_ID[1] = 0x29; + Fixed_Beacon_Packet.Parking3_Beacon_ID[2] = 0xB6; + Fixed_Beacon_Packet.Parking3_Beacon_ID[3] = 0x27; + Fixed_Beacon_Packet.Parking3_Beacon_ID[4] = 0x3A; + Fixed_Beacon_Packet.Parking3_Beacon_ID[5] = 0xC9; + Fixed_Beacon_Packet.Parking3_Beacon_Minor = 0x0001; + Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 0xB0; + /* + while(true) { - if(BLE_Receiver_UART_RX_Buffer[Temp_Pos1] == 0x0D) + if(Temp_Pos < Lora_RxBuffer_End_Pos) { - if(BLE_Receiver_UART_RX_Buffer[Temp_Pos1+1] == 0x0A)) - { //check for AT end response <cr><lf> (i.e. 0x0D,0x0A) - break; + if(BLE_Receiver_UART_RX_Buffer[Temp_Pos] == 0x0D) + { + if(BLE_Receiver_UART_RX_Buffer[Temp_Pos+1] == 0x0A) + { + Temp_Pos = 2; + for(i=0; i<6; i++) + Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; + Fixed_Beacon_Packet.Parking1_Beacon_Minor = ((BLE_Receiver_UART_RX_Buffer[Temp_Pos++]<<8)|(BLE_Receiver_UART_RX_Buffer[Temp_Pos++])); + Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; + for(i=0; i<6; i++) + Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; + Fixed_Beacon_Packet.Parking2_Beacon_Minor = ((BLE_Receiver_UART_RX_Buffer[Temp_Pos++]<<8)|(BLE_Receiver_UART_RX_Buffer[Temp_Pos++])); + Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; + for(i=0; i<6; i++) + Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; + Fixed_Beacon_Packet.Parking3_Beacon_Minor = ((BLE_Receiver_UART_RX_Buffer[Temp_Pos++]<<8)|(BLE_Receiver_UART_RX_Buffer[Temp_Pos++])); + Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; + break; + } + Temp_Pos++; } + else + Temp_Pos++; } else - Temp_Pos1++; - } + Temp_Pos = 0x00; + }*/ }