kumar singh
/
Dealer_23Feb
Dummy Lora Packet Sending
Fork of Dealer_18feb17 by
main.cpp@19:886d50ecc718, 2017-02-21 (annotated)
- Committer:
- NarendraSingh
- Date:
- Tue Feb 21 06:27:13 2017 +0000
- Revision:
- 19:886d50ecc718
- Parent:
- 17:758fb8454ab0
- Child:
- 20:f812f85cf97e
beacon receiver21feb17
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
NarendraSingh | 11:77e595130230 | 2 | #include "rtos.h" |
NarendraSingh | 11:77e595130230 | 3 | #include "OBD.h" |
NarendraSingh | 11:77e595130230 | 4 | #include "Lora.h" |
NarendraSingh | 11:77e595130230 | 5 | #include "Accelerometer.h" |
NarendraSingh | 11:77e595130230 | 6 | #include "Beacon.h" |
NarendraSingh | 11:77e595130230 | 7 | #include "main.h" |
NarendraSingh | 11:77e595130230 | 8 | |
NarendraSingh | 11:77e595130230 | 9 | //Datatype typecasting |
NarendraSingh | 11:77e595130230 | 10 | typedef unsigned char uint8; |
NarendraSingh | 11:77e595130230 | 11 | typedef unsigned int uint16; |
NarendraSingh | 11:77e595130230 | 12 | |
NarendraSingh | 16:7703b9d92326 | 13 | uint8 OBD_Plug_In_Status = FALSE; |
NarendraSingh | 16:7703b9d92326 | 14 | |
NarendraSingh | 16:7703b9d92326 | 15 | //peripheral connection |
emilmont | 1:491820ee784d | 16 | DigitalOut led1(LED1); |
emilmont | 1:491820ee784d | 17 | DigitalOut led2(LED2); |
NarendraSingh | 11:77e595130230 | 18 | |
NarendraSingh | 11:77e595130230 | 19 | //Configure Serial port |
NarendraSingh | 16:7703b9d92326 | 20 | 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 |
NarendraSingh | 17:758fb8454ab0 | 21 | RawSerial pc1(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only |
NarendraSingh | 19:886d50ecc718 | 22 | RawSerial DEBUG_UART(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only |
NarendraSingh | 16:7703b9d92326 | 23 | RawSerial Beacon_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5 : Used for sending command to beacon module |
NarendraSingh | 16:7703b9d92326 | 24 | 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 |
NarendraSingh | 16:7703b9d92326 | 25 | |
NarendraSingh | 16:7703b9d92326 | 26 | //InterruptIn OBD_PLUGIN_INTERRUPT_PIN(PC_13); |
NarendraSingh | 16:7703b9d92326 | 27 | InterruptIn CheckIn_Interrupt(PC_13); |
NarendraSingh | 16:7703b9d92326 | 28 | |
NarendraSingh | 16:7703b9d92326 | 29 | uint8 Ticker_Count = 0; //Variable to count for timer overflows |
NarendraSingh | 16:7703b9d92326 | 30 | |
NarendraSingh | 11:77e595130230 | 31 | |
NarendraSingh | 16:7703b9d92326 | 32 | uint8 OBD_Plugin_Detected = FALSE; |
NarendraSingh | 16:7703b9d92326 | 33 | |
NarendraSingh | 16:7703b9d92326 | 34 | //Create Object for structure of Lora Packet to be sent |
NarendraSingh | 11:77e595130230 | 35 | |
NarendraSingh | 16:7703b9d92326 | 36 | static uint16 Calculate_Wheels_RPM(uint8* Buffer); |
NarendraSingh | 16:7703b9d92326 | 37 | void flip_Packet_Sending_Flag(); |
NarendraSingh | 16:7703b9d92326 | 38 | void Lora_Periodic_Packet_Sending_thread(void const *args); |
NarendraSingh | 11:77e595130230 | 39 | |
NarendraSingh | 16:7703b9d92326 | 40 | void Lora_Rcvd_Cmd_Processing_thread(void);// const *args); |
NarendraSingh | 16:7703b9d92326 | 41 | void Enable_CheckIN_Packet_Sending(); |
NarendraSingh | 16:7703b9d92326 | 42 | |
NarendraSingh | 16:7703b9d92326 | 43 | const char GET_RSSI[]= {0x41,0x54,0x01,0x01,0x0D,0x0A}; |
NarendraSingh | 11:77e595130230 | 44 | /*************************Accelerometer related definitions***********************************/ |
NarendraSingh | 11:77e595130230 | 45 | //Accelerometer related definitions |
NarendraSingh | 13:8955f2e95021 | 46 | |
NarendraSingh | 11:77e595130230 | 47 | #define DOUBLE_TAP_INTERRUPT 0x20 |
NarendraSingh | 11:77e595130230 | 48 | #define ACTIVITY_INTERRUPT 0x10 |
NarendraSingh | 11:77e595130230 | 49 | #define INACTIVITY_INTERRUPT 0x08 |
NarendraSingh | 11:77e595130230 | 50 | |
NarendraSingh | 16:7703b9d92326 | 51 | #define TAP_THRESHOLD 75 |
NarendraSingh | 11:77e595130230 | 52 | #define ACTIVITY_THRESHOLD 64 // THRES_ACT register value 62.5mg/LSB , therfore value 32 indicates 2g activity |
NarendraSingh | 11:77e595130230 | 53 | #define INACTIVITY_THRESHOLD 50 |
NarendraSingh | 11:77e595130230 | 54 | |
NarendraSingh | 11:77e595130230 | 55 | #define DUR_TIME 0x15 // DUR Register value providing maximum time to be held to generate an interrupt |
NarendraSingh | 11:77e595130230 | 56 | #define LATENT_TIME 0x15 // The interrupt latency |
NarendraSingh | 11:77e595130230 | 57 | #define WINDOW_TIME 0x45 // The time of the interrupt window in which the next tap will be detected |
NarendraSingh | 11:77e595130230 | 58 | #define INACTIVITY_VALIDATION_TIME 5 // The time until which the acceleration must be held below the inactivity threshold to generate an inactvity interrupt |
NarendraSingh | 16:7703b9d92326 | 59 | // Here the value 5 indicates literally 5 secs |
NarendraSingh | 11:77e595130230 | 60 | #define X_AXIS_OFFSET 0x7F |
NarendraSingh | 11:77e595130230 | 61 | #define Y_AXIS_OFFSET 0x7F |
NarendraSingh | 11:77e595130230 | 62 | #define Z_AXIS_OFFSET 0x05 |
NarendraSingh | 11:77e595130230 | 63 | |
NarendraSingh | 17:758fb8454ab0 | 64 | Serial pc(USBTX, USBRX); |
NarendraSingh | 16:7703b9d92326 | 65 | I2C i2c(PB_9, PB_8); |
NarendraSingh | 11:77e595130230 | 66 | |
NarendraSingh | 16:7703b9d92326 | 67 | InterruptIn activity(PB_0); |
NarendraSingh | 11:77e595130230 | 68 | InterruptIn inactivity(PA_4); // As for now only this is used |
NarendraSingh | 11:77e595130230 | 69 | DigitalOut led(LED1); |
NarendraSingh | 11:77e595130230 | 70 | |
NarendraSingh | 11:77e595130230 | 71 | const int slave_address_acc = 0xA6; |
NarendraSingh | 11:77e595130230 | 72 | char axis_data[6] = {0,0,0,0,0,0}; |
NarendraSingh | 11:77e595130230 | 73 | |
NarendraSingh | 11:77e595130230 | 74 | char interrupt_source[2]; |
NarendraSingh | 11:77e595130230 | 75 | char axis_data_start_address[2] = {0x32, 0}; |
NarendraSingh | 11:77e595130230 | 76 | char intr_source_address[2] = {0x30, 0}; |
NarendraSingh | 11:77e595130230 | 77 | char all_interrupt_clear_command[2] = {0x2E, 0x00}; |
NarendraSingh | 11:77e595130230 | 78 | char all_interrupt_enable_command[2] = {0x2E, 0x38}; |
NarendraSingh | 11:77e595130230 | 79 | char activity_interrupt_disable_command[2] = {0x2E, 0x08}; |
NarendraSingh | 11:77e595130230 | 80 | char inactivity_interrupt_disable_command[2] = {0x2E, 0x30}; |
NarendraSingh | 11:77e595130230 | 81 | char accelerometer_status_registered = 0; |
NarendraSingh | 11:77e595130230 | 82 | unsigned int interrupt_source_duplicate; |
NarendraSingh | 11:77e595130230 | 83 | |
NarendraSingh | 11:77e595130230 | 84 | char threshold_offset_command[5]; |
NarendraSingh | 11:77e595130230 | 85 | char act_inact_time_config_command[8]; |
NarendraSingh | 11:77e595130230 | 86 | char interrupt_enable_command[3]; |
NarendraSingh | 11:77e595130230 | 87 | char tap_axis_enable_command[2]; |
NarendraSingh | 11:77e595130230 | 88 | char baud_rate_command[2]; |
NarendraSingh | 11:77e595130230 | 89 | char data_format_command[2]; |
NarendraSingh | 11:77e595130230 | 90 | char measure_bit_on_command[2]; |
NarendraSingh | 11:77e595130230 | 91 | |
NarendraSingh | 11:77e595130230 | 92 | |
NarendraSingh | 11:77e595130230 | 93 | unsigned char vehicle_speed = 25; // Kmph |
NarendraSingh | 11:77e595130230 | 94 | unsigned char current_speed, previous_speed, speed_threshold = 10; // Kmph |
NarendraSingh | 11:77e595130230 | 95 | |
NarendraSingh | 11:77e595130230 | 96 | unsigned char x_axis, y_axis, z_axis; |
NarendraSingh | 11:77e595130230 | 97 | |
NarendraSingh | 11:77e595130230 | 98 | unsigned char Motion_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 99 | uint8 OBD_PlugInOut_IOC_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 100 | unsigned char Motion_Type_Detected = MOTION_TYPE_UNKNOWN; //By default set motion type as unknown |
NarendraSingh | 11:77e595130230 | 101 | void Accelerometer_Process_thread();//void const *args) ; |
NarendraSingh | 11:77e595130230 | 102 | |
NarendraSingh | 16:7703b9d92326 | 103 | #define BLE_RECEIVER_UART_RX_Size 100 |
NarendraSingh | 16:7703b9d92326 | 104 | uint8 BLE_RxBuffer_End_Pos = 0; |
NarendraSingh | 16:7703b9d92326 | 105 | char BLE_Receiver_UART_RX_Buffer[BLE_RECEIVER_UART_RX_Size]; |
NarendraSingh | 13:8955f2e95021 | 106 | |
NarendraSingh | 11:77e595130230 | 107 | //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 |
NarendraSingh | 11:77e595130230 | 108 | void interrupt_activity_inactivity() |
NarendraSingh | 16:7703b9d92326 | 109 | { |
NarendraSingh | 16:7703b9d92326 | 110 | i2c.write(slave_address_acc, all_interrupt_clear_command, 2); |
NarendraSingh | 16:7703b9d92326 | 111 | Motion_Detect_Status = TRUE; |
NarendraSingh | 11:77e595130230 | 112 | } |
NarendraSingh | 13:8955f2e95021 | 113 | |
NarendraSingh | 13:8955f2e95021 | 114 | |
NarendraSingh | 16:7703b9d92326 | 115 | /************************************************************************/ |
NarendraSingh | 11:77e595130230 | 116 | uint8 Command_Sent[100]; |
NarendraSingh | 11:77e595130230 | 117 | uint8 Command_Length_Sent; |
NarendraSingh | 12:6107b32b0729 | 118 | uint8 Checkin_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 119 | void Extract_Received_Lora_Response(void); |
NarendraSingh | 16:7703b9d92326 | 120 | |
NarendraSingh | 16:7703b9d92326 | 121 | char previous_state = 0; |
NarendraSingh | 16:7703b9d92326 | 122 | char current_state = 0; |
NarendraSingh | 16:7703b9d92326 | 123 | |
NarendraSingh | 16:7703b9d92326 | 124 | uint8 OBD_PlugIN_State=0; |
NarendraSingh | 16:7703b9d92326 | 125 | uint8 OBD_PlugIN_State1=0; |
NarendraSingh | 16:7703b9d92326 | 126 | uint8 OBD_PlugIN_State2=0; |
NarendraSingh | 16:7703b9d92326 | 127 | uint8 OBD_PlugIN_Temp_State=1; |
NarendraSingh | 16:7703b9d92326 | 128 | |
NarendraSingh | 16:7703b9d92326 | 129 | void OBD_Plug_IN_Interrupt() |
NarendraSingh | 16:7703b9d92326 | 130 | { |
NarendraSingh | 17:758fb8454ab0 | 131 | if(OBD_PlugIN_State1!=OBD_PlugIN_Temp_State) { |
NarendraSingh | 16:7703b9d92326 | 132 | OBD_PlugIN_State1=1; |
NarendraSingh | 16:7703b9d92326 | 133 | OBD_PlugIN_State=!OBD_PlugIN_State; |
NarendraSingh | 16:7703b9d92326 | 134 | OBD_PlugIN_Temp_State=OBD_PlugIN_State; |
NarendraSingh | 16:7703b9d92326 | 135 | OBD_PlugInOut_IOC_Status = TRUE; |
NarendraSingh | 16:7703b9d92326 | 136 | } |
NarendraSingh | 16:7703b9d92326 | 137 | } |
NarendraSingh | 17:758fb8454ab0 | 138 | |
NarendraSingh | 16:7703b9d92326 | 139 | void OBD_Plug_OUT_Interrupt() |
NarendraSingh | 16:7703b9d92326 | 140 | { |
NarendraSingh | 17:758fb8454ab0 | 141 | if(OBD_PlugIN_State2!=OBD_PlugIN_Temp_State) { |
NarendraSingh | 16:7703b9d92326 | 142 | OBD_PlugIN_State2=0; |
NarendraSingh | 16:7703b9d92326 | 143 | OBD_PlugIN_State=!OBD_PlugIN_State; |
NarendraSingh | 16:7703b9d92326 | 144 | OBD_PlugIN_Temp_State=OBD_PlugIN_State; |
NarendraSingh | 16:7703b9d92326 | 145 | OBD_PlugInOut_IOC_Status = TRUE; |
NarendraSingh | 17:758fb8454ab0 | 146 | } |
NarendraSingh | 16:7703b9d92326 | 147 | } |
NarendraSingh | 16:7703b9d92326 | 148 | |
NarendraSingh | 16:7703b9d92326 | 149 | //This function is Interrupt routine for detecting OBD Plugin and Out |
NarendraSingh | 11:77e595130230 | 150 | void Handle_CheckIn_Interrupt() |
NarendraSingh | 16:7703b9d92326 | 151 | { |
NarendraSingh | 11:77e595130230 | 152 | OBD_PlugInOut_IOC_Status = TRUE; |
NarendraSingh | 17:758fb8454ab0 | 153 | pc.printf("Movement_Detected"); |
NarendraSingh | 11:77e595130230 | 154 | } |
NarendraSingh | 11:77e595130230 | 155 | |
NarendraSingh | 16:7703b9d92326 | 156 | //Declare Ticker for sending lora packet |
NarendraSingh | 16:7703b9d92326 | 157 | Ticker Lora_Packet_Sending_Ticker; |
NarendraSingh | 16:7703b9d92326 | 158 | void flip_Packet_Sending_Flag(void) |
NarendraSingh | 16:7703b9d92326 | 159 | { |
NarendraSingh | 16:7703b9d92326 | 160 | //flip function |
NarendraSingh | 16:7703b9d92326 | 161 | if(Ticker_Count < 5) { |
NarendraSingh | 11:77e595130230 | 162 | Ticker_Count++; |
NarendraSingh | 16:7703b9d92326 | 163 | } else { |
NarendraSingh | 11:77e595130230 | 164 | Ticker_Count = 0; |
NarendraSingh | 11:77e595130230 | 165 | Send_Lora_Packet_Flag = TRUE; |
NarendraSingh | 11:77e595130230 | 166 | } |
NarendraSingh | 16:7703b9d92326 | 167 | } |
NarendraSingh | 11:77e595130230 | 168 | |
NarendraSingh | 11:77e595130230 | 169 | // called every time a byte is received from lora module. |
NarendraSingh | 11:77e595130230 | 170 | void Lora_onDataRx() |
NarendraSingh | 11:77e595130230 | 171 | { |
NarendraSingh | 16:7703b9d92326 | 172 | while (LORA_UART.readable()) { |
NarendraSingh | 16:7703b9d92326 | 173 | // while there is data waiting |
NarendraSingh | 11:77e595130230 | 174 | LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos++] = LORA_UART.getc(); // put it in the buffer |
NarendraSingh | 19:886d50ecc718 | 175 | pc1.putc(LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos-1]); |
NarendraSingh | 16:7703b9d92326 | 176 | if(Lora_RxBuffer_End_Pos >= LORA_UART_RX_Size) { |
NarendraSingh | 11:77e595130230 | 177 | // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
NarendraSingh | 11:77e595130230 | 178 | // For now just throw everything away. |
NarendraSingh | 11:77e595130230 | 179 | Lora_RxBuffer_End_Pos = 0; |
NarendraSingh | 11:77e595130230 | 180 | } |
emilmont | 1:491820ee784d | 181 | } |
NarendraSingh | 16:7703b9d92326 | 182 | } |
NarendraSingh | 11:77e595130230 | 183 | |
NarendraSingh | 11:77e595130230 | 184 | // called every time a byte is received from Beacon Module. |
NarendraSingh | 11:77e595130230 | 185 | void Beacon_onDataRx() |
NarendraSingh | 11:77e595130230 | 186 | { |
NarendraSingh | 16:7703b9d92326 | 187 | while (Beacon_UART.readable()) { |
NarendraSingh | 16:7703b9d92326 | 188 | // while there is data waiting |
NarendraSingh | 11:77e595130230 | 189 | Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos++] = Beacon_UART.getc(); // put it in the buffer |
NarendraSingh | 17:758fb8454ab0 | 190 | //pc1.putc(LORA_UART_RX_Buffer[Beacon_RxBuffer_End_Pos-1]); |
NarendraSingh | 16:7703b9d92326 | 191 | if(Beacon_RxBuffer_End_Pos >= 100) { |
NarendraSingh | 11:77e595130230 | 192 | // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
NarendraSingh | 11:77e595130230 | 193 | // For now just throw everything away. |
NarendraSingh | 11:77e595130230 | 194 | Beacon_RxBuffer_End_Pos = 0; |
NarendraSingh | 11:77e595130230 | 195 | } |
emilmont | 1:491820ee784d | 196 | } |
emilmont | 1:491820ee784d | 197 | } |
NarendraSingh | 11:77e595130230 | 198 | |
NarendraSingh | 16:7703b9d92326 | 199 | void BLE_Receiver_onDataRx(void) |
NarendraSingh | 13:8955f2e95021 | 200 | { |
NarendraSingh | 16:7703b9d92326 | 201 | while (BLE_RECEIVER_UART.readable()) { |
NarendraSingh | 16:7703b9d92326 | 202 | // while there is data waiting |
NarendraSingh | 13:8955f2e95021 | 203 | BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos++] = BLE_RECEIVER_UART.getc(); // put it in the buffer |
NarendraSingh | 17:758fb8454ab0 | 204 | //pc1.putc(BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos-1]); |
NarendraSingh | 16:7703b9d92326 | 205 | if(BLE_RxBuffer_End_Pos >= BLE_RECEIVER_UART_RX_Size) { |
NarendraSingh | 13:8955f2e95021 | 206 | // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
NarendraSingh | 13:8955f2e95021 | 207 | // For now just throw everything away. |
NarendraSingh | 13:8955f2e95021 | 208 | BLE_RxBuffer_End_Pos = 0; |
NarendraSingh | 13:8955f2e95021 | 209 | } |
NarendraSingh | 12:6107b32b0729 | 210 | } |
NarendraSingh | 16:7703b9d92326 | 211 | } |
NarendraSingh | 16:7703b9d92326 | 212 | |
NarendraSingh | 16:7703b9d92326 | 213 | int main() |
NarendraSingh | 11:77e595130230 | 214 | { |
NarendraSingh | 17:758fb8454ab0 | 215 | pc1.baud(115200); |
NarendraSingh | 16:7703b9d92326 | 216 | BLE_RECEIVER_UART.baud(115200); |
NarendraSingh | 17:758fb8454ab0 | 217 | pc1.printf("%s","Debugging started"); |
NarendraSingh | 16:7703b9d92326 | 218 | BLE_RECEIVER_UART.attach(&BLE_Receiver_onDataRx, Serial::RxIrq); |
NarendraSingh | 11:77e595130230 | 219 | LORA_UART.attach(&Lora_onDataRx, Serial::RxIrq); |
NarendraSingh | 17:758fb8454ab0 | 220 | |
NarendraSingh | 11:77e595130230 | 221 | //Create a thread to read vehicle data |
NarendraSingh | 11:77e595130230 | 222 | //Thread OBD_thread(OBD_Rcvd_Cmd_Processing_thread); |
NarendraSingh | 11:77e595130230 | 223 | |
NarendraSingh | 11:77e595130230 | 224 | Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 3.0); // call flip_Packet_Sending_Flag function every 5 seconds |
NarendraSingh | 11:77e595130230 | 225 | |
NarendraSingh | 11:77e595130230 | 226 | // OBD_PLUGIN_INTERRUPT_PIN.rise(&Enable_CheckIN_Packet_Sending); // call toggle function on the rising edge |
NarendraSingh | 11:77e595130230 | 227 | //led2_thread is executing concurrently with main at this point |
NarendraSingh | 16:7703b9d92326 | 228 | CheckIn_Interrupt.fall(&OBD_Plug_IN_Interrupt); |
NarendraSingh | 16:7703b9d92326 | 229 | CheckIn_Interrupt.rise(&OBD_Plug_OUT_Interrupt); |
NarendraSingh | 11:77e595130230 | 230 | inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge |
NarendraSingh | 19:886d50ecc718 | 231 | Initialize_Beacon_Module(); |
NarendraSingh | 11:77e595130230 | 232 | Lora_Periodic_Packet_Sending(); //Infinite loop for sending and receiving lora response, no return from here |
NarendraSingh | 16:7703b9d92326 | 233 | } |
NarendraSingh | 13:8955f2e95021 | 234 | |
NarendraSingh | 16:7703b9d92326 | 235 | //Function to be called when Interrupt is genearted for motion sensing, checkin |
NarendraSingh | 16:7703b9d92326 | 236 | void Initialize_Packets_Sent_Count(void) |
NarendraSingh | 16:7703b9d92326 | 237 | { |
NarendraSingh | 11:77e595130230 | 238 | Motion_Packet_Sent_Count = 0x00; |
NarendraSingh | 11:77e595130230 | 239 | CheckIN_Packet_Sent_Count = 0x00; |
NarendraSingh | 16:7703b9d92326 | 240 | } |
NarendraSingh | 16:7703b9d92326 | 241 | |
NarendraSingh | 16:7703b9d92326 | 242 | uint8 Status_Packet_Wait_Count = 0; |
NarendraSingh | 16:7703b9d92326 | 243 | void Lora_Periodic_Packet_Sending() |
NarendraSingh | 11:77e595130230 | 244 | { |
NarendraSingh | 17:758fb8454ab0 | 245 | pc1.printf("Periodic packet sending intiialized"); |
NarendraSingh | 11:77e595130230 | 246 | Set_Up_Lora_Network_Configuration(); |
NarendraSingh | 16:7703b9d92326 | 247 | Initialize_lora_Packets(); |
NarendraSingh | 19:886d50ecc718 | 248 | Send_Lora_Packet_Flag = TRUE; |
NarendraSingh | 16:7703b9d92326 | 249 | while (true) { |
NarendraSingh | 16:7703b9d92326 | 250 | if(Packet_Type_To_Send == HEARTBEAT_TYPE_PACKET) { //check if packet to be sent is Heartbeat packet |
NarendraSingh | 16:7703b9d92326 | 251 | if(Send_Lora_Packet_Flag) { //Check if packet sending is enabled, Packet should be sent only when enabled after timeout period |
NarendraSingh | 12:6107b32b0729 | 252 | Status_Packet_Wait_Count++; |
NarendraSingh | 16:7703b9d92326 | 253 | if(Status_Packet_Wait_Count < 5) { |
NarendraSingh | 15:a448e955b8f3 | 254 | Send_RSSI_Request_Command(GET_RSSI); |
NarendraSingh | 12:6107b32b0729 | 255 | Send_HeartBeat_Packet(); //call function to send heartbeat packet |
NarendraSingh | 17:758fb8454ab0 | 256 | pc.printf("Sent HeartBeat Packet"); |
NarendraSingh | 12:6107b32b0729 | 257 | AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 12:6107b32b0729 | 258 | while(AT_Response_Receive_Status) |
NarendraSingh | 12:6107b32b0729 | 259 | Get_Lora_Response(); |
NarendraSingh | 12:6107b32b0729 | 260 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 17:758fb8454ab0 | 261 | pc1.printf("Heartbeat Packet Response Received"); |
NarendraSingh | 16:7703b9d92326 | 262 | } else { |
NarendraSingh | 16:7703b9d92326 | 263 | Send_RSSI_Request_Command(GET_RSSI); |
NarendraSingh | 12:6107b32b0729 | 264 | Status_Packet_Wait_Count = 0; |
NarendraSingh | 13:8955f2e95021 | 265 | Send_Vehicle_Status_Packet(); //call function to send heartbeat packet |
NarendraSingh | 19:886d50ecc718 | 266 | //pc1.printf("Sent Status Packet"); |
NarendraSingh | 19:886d50ecc718 | 267 | //AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 19:886d50ecc718 | 268 | //while(AT_Response_Receive_Status) |
NarendraSingh | 19:886d50ecc718 | 269 | // Get_Lora_Response(); |
NarendraSingh | 12:6107b32b0729 | 270 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 17:758fb8454ab0 | 271 | pc1.printf("Status Packet Response Received"); |
NarendraSingh | 12:6107b32b0729 | 272 | } |
NarendraSingh | 11:77e595130230 | 273 | } |
NarendraSingh | 16:7703b9d92326 | 274 | } else if(Packet_Type_To_Send == MOTION_TYPE_PACKET) { //check if packet to be sent is motion packet |
NarendraSingh | 16:7703b9d92326 | 275 | if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period |
NarendraSingh | 16:7703b9d92326 | 276 | Send_RSSI_Request_Command(GET_RSSI); |
NarendraSingh | 11:77e595130230 | 277 | Send_Motion_Packet(); |
NarendraSingh | 17:758fb8454ab0 | 278 | pc1.printf("Sent Motion Packet"); //call function to send periodic motion packet |
NarendraSingh | 11:77e595130230 | 279 | AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 11:77e595130230 | 280 | while(AT_Response_Receive_Status) |
NarendraSingh | 11:77e595130230 | 281 | Get_Lora_Response(); |
NarendraSingh | 17:758fb8454ab0 | 282 | pc1.printf("Motion Packet Response Received"); |
NarendraSingh | 11:77e595130230 | 283 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 16:7703b9d92326 | 284 | if(Motion_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute |
NarendraSingh | 17:758fb8454ab0 | 285 | pc1.printf("Packet Type Sending Changed to HeartBeat"); |
NarendraSingh | 11:77e595130230 | 286 | Motion_Packet_Sent_Count = 0; |
NarendraSingh | 11:77e595130230 | 287 | Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet |
NarendraSingh | 16:7703b9d92326 | 288 | Lora_Packet_Sending_Ticker.detach(); //destroy ticker |
NarendraSingh | 11:77e595130230 | 289 | Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 10.0); //create new ticker |
NarendraSingh | 11:77e595130230 | 290 | } |
NarendraSingh | 16:7703b9d92326 | 291 | } |
NarendraSingh | 16:7703b9d92326 | 292 | } else if(Packet_Type_To_Send == CHECKIN_TYPE_PACKET) { //check if packet to be sent is Checkin packet |
NarendraSingh | 16:7703b9d92326 | 293 | if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period |
NarendraSingh | 19:886d50ecc718 | 294 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 17:758fb8454ab0 | 295 | pc1.printf("Sent Beacon ID request"); |
NarendraSingh | 16:7703b9d92326 | 296 | Send_RSSI_Request_Command(GET_RSSI); |
NarendraSingh | 19:886d50ecc718 | 297 | if(OBD_PlugIN_State) |
NarendraSingh | 19:886d50ecc718 | 298 | { |
NarendraSingh | 19:886d50ecc718 | 299 | Send_CheckIN_Packet(); //call function to send periodic checkIn packet |
NarendraSingh | 19:886d50ecc718 | 300 | pc1.printf("Sent Checkin Packet"); |
NarendraSingh | 19:886d50ecc718 | 301 | } |
NarendraSingh | 19:886d50ecc718 | 302 | else |
NarendraSingh | 19:886d50ecc718 | 303 | { |
NarendraSingh | 19:886d50ecc718 | 304 | Send_CheckOUT_Packet(); |
NarendraSingh | 19:886d50ecc718 | 305 | pc1.printf("Sent CheckOut Packet"); |
NarendraSingh | 19:886d50ecc718 | 306 | } |
NarendraSingh | 19:886d50ecc718 | 307 | __enable_irq(); // Enable Interrupts |
NarendraSingh | 11:77e595130230 | 308 | AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 11:77e595130230 | 309 | while(AT_Response_Receive_Status) |
NarendraSingh | 11:77e595130230 | 310 | Get_Lora_Response(); |
NarendraSingh | 17:758fb8454ab0 | 311 | pc1.printf("Checkin Packet Response Received"); |
NarendraSingh | 16:7703b9d92326 | 312 | if(CheckIN_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute |
NarendraSingh | 17:758fb8454ab0 | 313 | pc1.printf("Packet Type Sending Changed to HeartBeat"); |
NarendraSingh | 11:77e595130230 | 314 | CheckIN_Packet_Sent_Count = 0; |
NarendraSingh | 11:77e595130230 | 315 | Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet |
NarendraSingh | 11:77e595130230 | 316 | Initialize_Packets_Sent_Count(); |
NarendraSingh | 11:77e595130230 | 317 | } |
NarendraSingh | 11:77e595130230 | 318 | } |
NarendraSingh | 16:7703b9d92326 | 319 | } |
NarendraSingh | 16:7703b9d92326 | 320 | if(OBD_PlugInOut_IOC_Status) { //Check if Accelerometer Interrupt is generated |
NarendraSingh | 19:886d50ecc718 | 321 | __disable_irq(); // Disable Interrupts |
NarendraSingh | 13:8955f2e95021 | 322 | //Enable_CheckIN_Packet_Sending(); |
NarendraSingh | 11:77e595130230 | 323 | //Get_Acceleration_Type(); |
NarendraSingh | 11:77e595130230 | 324 | OBD_PlugInOut_IOC_Status = FALSE; |
NarendraSingh | 13:8955f2e95021 | 325 | Packet_Type_To_Send = CHECKIN_TYPE_PACKET; |
NarendraSingh | 13:8955f2e95021 | 326 | Send_Lora_Packet_Flag = TRUE; |
NarendraSingh | 19:886d50ecc718 | 327 | CheckIN_Packet_Sent_Count = 0; |
NarendraSingh | 16:7703b9d92326 | 328 | wait(1); //wait for 1sec to avoid detecting plugin debounce |
NarendraSingh | 11:77e595130230 | 329 | } |
NarendraSingh | 16:7703b9d92326 | 330 | if(Motion_Detect_Status) { //Check if Accelerometer Interrupt is generated |
NarendraSingh | 11:77e595130230 | 331 | Get_Acceleration_Type(); |
NarendraSingh | 11:77e595130230 | 332 | Motion_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 333 | } |
NarendraSingh | 16:7703b9d92326 | 334 | } |
NarendraSingh | 11:77e595130230 | 335 | } |
NarendraSingh | 11:77e595130230 | 336 | |
NarendraSingh | 11:77e595130230 | 337 | void Get_Acceleration_Type(void) |
NarendraSingh | 11:77e595130230 | 338 | { |
NarendraSingh | 11:77e595130230 | 339 | Packet_Type_To_Send = MOTION_TYPE_PACKET; //whenever acceleration is detected change the packet type to be sent to motion type |
NarendraSingh | 16:7703b9d92326 | 340 | Lora_Packet_Sending_Ticker.detach(); //destroy ticker |
NarendraSingh | 11:77e595130230 | 341 | Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 5.0); //create new ticker, time inetrval value to be changed |
NarendraSingh | 11:77e595130230 | 342 | Send_Lora_Packet_Flag = 1; //set flag to send motion packet as soon as motion is detecetd |
NarendraSingh | 11:77e595130230 | 343 | Motion_Lora_Packet.Acceleration_Type = Motion_Type_Detected; //Read Type of motion deteceted |
NarendraSingh | 11:77e595130230 | 344 | //write code to read acceleration value for every 10sec after any of the acceleration is found |
NarendraSingh | 11:77e595130230 | 345 | } |
NarendraSingh | 19:886d50ecc718 | 346 | |
NarendraSingh | 13:8955f2e95021 | 347 | /* |
NarendraSingh | 11:77e595130230 | 348 | //This function is used to enable checkin packet sending once OBD device is plugged in |
NarendraSingh | 16:7703b9d92326 | 349 | void Enable_CheckIN_Packet_Sending() |
NarendraSingh | 11:77e595130230 | 350 | { |
NarendraSingh | 12:6107b32b0729 | 351 | // Enable_CheckIN_Packet_Sending = TRUE; //set status to true |
NarendraSingh | 13:8955f2e95021 | 352 | if(Command_Received[0] == 0xFE) //check start of frame of packet received. every packet must start with 0xFE |
NarendraSingh | 11:77e595130230 | 353 | { |
NarendraSingh | 11:77e595130230 | 354 | if(Calculate_Lora_Frame_FCS((Command_Received + 1),(Command_Length + 1)) == (Command_Received[Command_Length + 2])) //Check for packet inegrity by checking FCS |
NarendraSingh | 11:77e595130230 | 355 | { |
NarendraSingh | 11:77e595130230 | 356 | if((Command_Received[0] == SET_BEACON_CMD0) && (Command_Received[1] == SET_BEACON_CMD1)) //Check if command is received for setting beacon parameters |
NarendraSingh | 11:77e595130230 | 357 | { |
NarendraSingh | 11:77e595130230 | 358 | Process_Beacon_Command_Received((Command_Received + 2)); |
NarendraSingh | 11:77e595130230 | 359 | } |
NarendraSingh | 11:77e595130230 | 360 | } |
NarendraSingh | 13:8955f2e95021 | 361 | } |
NarendraSingh | 15:a448e955b8f3 | 362 | } */ |
NarendraSingh | 15:a448e955b8f3 | 363 | |
NarendraSingh | 16:7703b9d92326 | 364 | void Send_RSSI_Request_Command(const char* Command_To_Send) |
NarendraSingh | 15:a448e955b8f3 | 365 | { |
NarendraSingh | 15:a448e955b8f3 | 366 | BLE_RECEIVER_UART.printf(Command_To_Send); |
NarendraSingh | 16:7703b9d92326 | 367 | Get_Fixed_Beacon_RSSI(); |
NarendraSingh | 15:a448e955b8f3 | 368 | } |
NarendraSingh | 15:a448e955b8f3 | 369 | |
NarendraSingh | 16:7703b9d92326 | 370 | void Get_Fixed_Beacon_RSSI(void) |
NarendraSingh | 15:a448e955b8f3 | 371 | { |
NarendraSingh | 16:7703b9d92326 | 372 | uint8 i; |
NarendraSingh | 16:7703b9d92326 | 373 | uint8 Temp_Pos=0; |
NarendraSingh | 19:886d50ecc718 | 374 | /*Fixed_Beacon_Packet.Parking1_Beacon_ID[0] = 0xA0; |
NarendraSingh | 16:7703b9d92326 | 375 | Fixed_Beacon_Packet.Parking1_Beacon_ID[1] = 0xAC; |
NarendraSingh | 16:7703b9d92326 | 376 | Fixed_Beacon_Packet.Parking1_Beacon_ID[2] = 0x51; |
NarendraSingh | 16:7703b9d92326 | 377 | Fixed_Beacon_Packet.Parking1_Beacon_ID[3] = 0x3F; |
NarendraSingh | 16:7703b9d92326 | 378 | Fixed_Beacon_Packet.Parking1_Beacon_ID[4] = 0x91; |
NarendraSingh | 16:7703b9d92326 | 379 | Fixed_Beacon_Packet.Parking1_Beacon_ID[5] = 0xE0; |
NarendraSingh | 16:7703b9d92326 | 380 | Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 0xC3; |
NarendraSingh | 16:7703b9d92326 | 381 | Fixed_Beacon_Packet.Parking2_Beacon_ID[0] = 0xDC; |
NarendraSingh | 16:7703b9d92326 | 382 | Fixed_Beacon_Packet.Parking2_Beacon_ID[1] = 0xA3; |
NarendraSingh | 16:7703b9d92326 | 383 | Fixed_Beacon_Packet.Parking2_Beacon_ID[2] = 0x6D; |
NarendraSingh | 16:7703b9d92326 | 384 | Fixed_Beacon_Packet.Parking2_Beacon_ID[3] = 0x30; |
NarendraSingh | 16:7703b9d92326 | 385 | Fixed_Beacon_Packet.Parking2_Beacon_ID[4] = 0x38; |
NarendraSingh | 16:7703b9d92326 | 386 | Fixed_Beacon_Packet.Parking2_Beacon_ID[5] = 0xDF; |
NarendraSingh | 16:7703b9d92326 | 387 | Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 0xB4; |
NarendraSingh | 16:7703b9d92326 | 388 | Fixed_Beacon_Packet.Parking3_Beacon_ID[0] = 0x6C; |
NarendraSingh | 16:7703b9d92326 | 389 | Fixed_Beacon_Packet.Parking3_Beacon_ID[1] = 0x29; |
NarendraSingh | 16:7703b9d92326 | 390 | Fixed_Beacon_Packet.Parking3_Beacon_ID[2] = 0xB6; |
NarendraSingh | 16:7703b9d92326 | 391 | Fixed_Beacon_Packet.Parking3_Beacon_ID[3] = 0x27; |
NarendraSingh | 16:7703b9d92326 | 392 | Fixed_Beacon_Packet.Parking3_Beacon_ID[4] = 0x3A; |
NarendraSingh | 16:7703b9d92326 | 393 | Fixed_Beacon_Packet.Parking3_Beacon_ID[5] = 0xC9; |
NarendraSingh | 16:7703b9d92326 | 394 | Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 0xB0; |
NarendraSingh | 19:886d50ecc718 | 395 | */ |
NarendraSingh | 17:758fb8454ab0 | 396 | while(true) |
NarendraSingh | 15:a448e955b8f3 | 397 | { |
NarendraSingh | 17:758fb8454ab0 | 398 | if(Temp_Pos < Lora_RxBuffer_End_Pos) |
NarendraSingh | 15:a448e955b8f3 | 399 | { |
NarendraSingh | 17:758fb8454ab0 | 400 | if(BLE_Receiver_UART_RX_Buffer[Temp_Pos] == 0x0D) |
NarendraSingh | 16:7703b9d92326 | 401 | { |
NarendraSingh | 17:758fb8454ab0 | 402 | if(BLE_Receiver_UART_RX_Buffer[Temp_Pos+1] == 0x0A) |
NarendraSingh | 16:7703b9d92326 | 403 | { |
NarendraSingh | 16:7703b9d92326 | 404 | Temp_Pos = 2; |
NarendraSingh | 16:7703b9d92326 | 405 | for(i=0; i<6; i++) |
NarendraSingh | 16:7703b9d92326 | 406 | Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 19:886d50ecc718 | 407 | Temp_Pos+=2; |
NarendraSingh | 16:7703b9d92326 | 408 | Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 16:7703b9d92326 | 409 | for(i=0; i<6; i++) |
NarendraSingh | 16:7703b9d92326 | 410 | Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 19:886d50ecc718 | 411 | Temp_Pos+=2; |
NarendraSingh | 16:7703b9d92326 | 412 | Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 16:7703b9d92326 | 413 | for(i=0; i<6; i++) |
NarendraSingh | 16:7703b9d92326 | 414 | Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 19:886d50ecc718 | 415 | Temp_Pos+=2; |
NarendraSingh | 16:7703b9d92326 | 416 | Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 16:7703b9d92326 | 417 | break; |
NarendraSingh | 16:7703b9d92326 | 418 | } |
NarendraSingh | 16:7703b9d92326 | 419 | Temp_Pos++; |
NarendraSingh | 15:a448e955b8f3 | 420 | } |
NarendraSingh | 16:7703b9d92326 | 421 | else |
NarendraSingh | 16:7703b9d92326 | 422 | Temp_Pos++; |
NarendraSingh | 15:a448e955b8f3 | 423 | } |
NarendraSingh | 15:a448e955b8f3 | 424 | else |
NarendraSingh | 16:7703b9d92326 | 425 | Temp_Pos = 0x00; |
NarendraSingh | 19:886d50ecc718 | 426 | } |
NarendraSingh | 15:a448e955b8f3 | 427 | } |