Dummy Lora Packet Sending

Fork of Dealer_18feb17 by kumar singh

Committer:
NarendraSingh
Date:
Mon Jan 30 18:31:30 2017 +0000
Revision:
12:6107b32b0729
Parent:
11:77e595130230
Child:
13:8955f2e95021
Queue Initial implemented. Not working;

Who changed what in which revision?

UserRevisionLine numberNew 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 11:77e595130230 13 //peripheral connection
emilmont 1:491820ee784d 14 DigitalOut led1(LED1);
emilmont 1:491820ee784d 15 DigitalOut led2(LED2);
NarendraSingh 11:77e595130230 16
NarendraSingh 11:77e595130230 17 //Configure Serial port
NarendraSingh 11:77e595130230 18 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 11:77e595130230 19 RawSerial DEBUG_UART(PA_9, PA_10);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only
NarendraSingh 11:77e595130230 20 RawSerial Beacon_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5 : Used for sending command to beacon module
NarendraSingh 11:77e595130230 21
NarendraSingh 11:77e595130230 22 //InterruptIn OBD_PLUGIN_INTERRUPT_PIN(PC_13);
NarendraSingh 11:77e595130230 23 InterruptIn CheckIn_Interrupt(PC_13);
NarendraSingh 11:77e595130230 24
NarendraSingh 11:77e595130230 25 uint8 Ticker_Count = 0; //Variable to count for timer overflows
NarendraSingh 11:77e595130230 26
NarendraSingh 11:77e595130230 27
NarendraSingh 11:77e595130230 28 uint8 OBD_Plugin_Detected = FALSE;
emilmont 1:491820ee784d 29
NarendraSingh 11:77e595130230 30 //Create Object for structure of Lora Packet to be sent
NarendraSingh 11:77e595130230 31
NarendraSingh 11:77e595130230 32 static uint16 Calculate_Wheels_RPM(uint8* Buffer);
NarendraSingh 11:77e595130230 33 void flip_Packet_Sending_Flag();
NarendraSingh 11:77e595130230 34 void Lora_Periodic_Packet_Sending_thread(void const *args);
NarendraSingh 11:77e595130230 35
NarendraSingh 11:77e595130230 36 void Lora_Rcvd_Cmd_Processing_thread(void);// const *args);
NarendraSingh 11:77e595130230 37 void Enable_CheckIN_Packet_Sending();
NarendraSingh 11:77e595130230 38 /*************************Accelerometer related definitions***********************************/
NarendraSingh 11:77e595130230 39 //Accelerometer related definitions
NarendraSingh 11:77e595130230 40 #define DOUBLE_TAP_INTERRUPT 0x20
NarendraSingh 11:77e595130230 41 #define ACTIVITY_INTERRUPT 0x10
NarendraSingh 11:77e595130230 42 #define INACTIVITY_INTERRUPT 0x08
NarendraSingh 11:77e595130230 43
NarendraSingh 11:77e595130230 44 #define TAP_THRESHOLD 75
NarendraSingh 11:77e595130230 45 #define ACTIVITY_THRESHOLD 64 // THRES_ACT register value 62.5mg/LSB , therfore value 32 indicates 2g activity
NarendraSingh 11:77e595130230 46 #define INACTIVITY_THRESHOLD 50
NarendraSingh 11:77e595130230 47
NarendraSingh 11:77e595130230 48 #define DUR_TIME 0x15 // DUR Register value providing maximum time to be held to generate an interrupt
NarendraSingh 11:77e595130230 49 #define LATENT_TIME 0x15 // The interrupt latency
NarendraSingh 11:77e595130230 50 #define WINDOW_TIME 0x45 // The time of the interrupt window in which the next tap will be detected
NarendraSingh 11:77e595130230 51 #define INACTIVITY_VALIDATION_TIME 5 // The time until which the acceleration must be held below the inactivity threshold to generate an inactvity interrupt
NarendraSingh 11:77e595130230 52 // Here the value 5 indicates literally 5 secs
NarendraSingh 11:77e595130230 53 #define X_AXIS_OFFSET 0x7F
NarendraSingh 11:77e595130230 54 #define Y_AXIS_OFFSET 0x7F
NarendraSingh 11:77e595130230 55 #define Z_AXIS_OFFSET 0x05
NarendraSingh 11:77e595130230 56
NarendraSingh 11:77e595130230 57
NarendraSingh 11:77e595130230 58 I2C i2c(PB_9, PB_8);
NarendraSingh 11:77e595130230 59 RawSerial DEBUG_UART1(PA_9, PA_10);//USART1_TX->PA_9,USART1_RX->PA_10
NarendraSingh 11:77e595130230 60
NarendraSingh 11:77e595130230 61 InterruptIn activity(PB_0);
NarendraSingh 11:77e595130230 62 InterruptIn inactivity(PA_4); // As for now only this is used
NarendraSingh 11:77e595130230 63 DigitalOut led(LED1);
NarendraSingh 11:77e595130230 64
NarendraSingh 11:77e595130230 65 const int slave_address_acc = 0xA6;
NarendraSingh 11:77e595130230 66 char axis_data[6] = {0,0,0,0,0,0};
NarendraSingh 11:77e595130230 67
NarendraSingh 11:77e595130230 68 char interrupt_source[2];
NarendraSingh 11:77e595130230 69 char axis_data_start_address[2] = {0x32, 0};
NarendraSingh 11:77e595130230 70 char intr_source_address[2] = {0x30, 0};
NarendraSingh 11:77e595130230 71 char all_interrupt_clear_command[2] = {0x2E, 0x00};
NarendraSingh 11:77e595130230 72 char all_interrupt_enable_command[2] = {0x2E, 0x38};
NarendraSingh 11:77e595130230 73 char activity_interrupt_disable_command[2] = {0x2E, 0x08};
NarendraSingh 11:77e595130230 74 char inactivity_interrupt_disable_command[2] = {0x2E, 0x30};
NarendraSingh 11:77e595130230 75 char accelerometer_status_registered = 0;
NarendraSingh 11:77e595130230 76 unsigned int interrupt_source_duplicate;
NarendraSingh 11:77e595130230 77
NarendraSingh 11:77e595130230 78 char threshold_offset_command[5];
NarendraSingh 11:77e595130230 79 char act_inact_time_config_command[8];
NarendraSingh 11:77e595130230 80 char interrupt_enable_command[3];
NarendraSingh 11:77e595130230 81 char tap_axis_enable_command[2];
NarendraSingh 11:77e595130230 82 char baud_rate_command[2];
NarendraSingh 11:77e595130230 83 char data_format_command[2];
NarendraSingh 11:77e595130230 84 char measure_bit_on_command[2];
NarendraSingh 11:77e595130230 85
NarendraSingh 11:77e595130230 86
NarendraSingh 11:77e595130230 87 char previous_state = 0;
NarendraSingh 11:77e595130230 88 char current_state = 0;
NarendraSingh 11:77e595130230 89
NarendraSingh 11:77e595130230 90 unsigned char vehicle_speed = 25; // Kmph
NarendraSingh 11:77e595130230 91 unsigned char current_speed, previous_speed, speed_threshold = 10; // Kmph
NarendraSingh 11:77e595130230 92
NarendraSingh 11:77e595130230 93 unsigned char x_axis, y_axis, z_axis;
NarendraSingh 11:77e595130230 94
NarendraSingh 11:77e595130230 95 unsigned char Motion_Detect_Status = FALSE;
NarendraSingh 11:77e595130230 96 uint8 OBD_PlugInOut_IOC_Status = FALSE;
NarendraSingh 11:77e595130230 97 unsigned char Motion_Type_Detected = MOTION_TYPE_UNKNOWN; //By default set motion type as unknown
NarendraSingh 11:77e595130230 98 void Accelerometer_Process_thread();//void const *args) ;
NarendraSingh 11:77e595130230 99
NarendraSingh 11:77e595130230 100 //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 101 void interrupt_activity_inactivity()
NarendraSingh 11:77e595130230 102 {
NarendraSingh 11:77e595130230 103 i2c.write(slave_address_acc, all_interrupt_clear_command, 2);
NarendraSingh 11:77e595130230 104 Motion_Detect_Status = TRUE;
NarendraSingh 11:77e595130230 105 }
NarendraSingh 11:77e595130230 106 /************************************************************************/
NarendraSingh 11:77e595130230 107 uint8 Command_Sent[100];
NarendraSingh 11:77e595130230 108 uint8 Command_Length_Sent;
NarendraSingh 12:6107b32b0729 109 uint8 Checkin_Detect_Status = FALSE;
NarendraSingh 11:77e595130230 110 void Extract_Received_Lora_Response(void);
NarendraSingh 11:77e595130230 111
NarendraSingh 11:77e595130230 112 //This function is Interrupt routine for detecting OBD Plugin and Out
NarendraSingh 11:77e595130230 113 void Handle_CheckIn_Interrupt()
NarendraSingh 11:77e595130230 114 {
NarendraSingh 11:77e595130230 115 OBD_PlugInOut_IOC_Status = TRUE;
NarendraSingh 11:77e595130230 116 DEBUG_UART.printf("Movement_Detected");
NarendraSingh 11:77e595130230 117 }
NarendraSingh 11:77e595130230 118
NarendraSingh 11:77e595130230 119 //Declare Ticker for sending lora packet
NarendraSingh 11:77e595130230 120 Ticker Lora_Packet_Sending_Ticker;
NarendraSingh 11:77e595130230 121 void flip_Packet_Sending_Flag(void)
NarendraSingh 11:77e595130230 122 { //flip function
NarendraSingh 11:77e595130230 123 if(Ticker_Count < 5)
NarendraSingh 11:77e595130230 124 {
NarendraSingh 11:77e595130230 125 Ticker_Count++;
NarendraSingh 11:77e595130230 126 }
NarendraSingh 11:77e595130230 127 else
NarendraSingh 11:77e595130230 128 {
NarendraSingh 11:77e595130230 129 Ticker_Count = 0;
NarendraSingh 11:77e595130230 130 Send_Lora_Packet_Flag = TRUE;
NarendraSingh 11:77e595130230 131 }
NarendraSingh 11:77e595130230 132 }
NarendraSingh 11:77e595130230 133
NarendraSingh 11:77e595130230 134 // called every time a byte is received from lora module.
NarendraSingh 11:77e595130230 135 void Lora_onDataRx()
NarendraSingh 11:77e595130230 136 {
NarendraSingh 11:77e595130230 137 while (LORA_UART.readable())
NarendraSingh 11:77e595130230 138 { // while there is data waiting
NarendraSingh 11:77e595130230 139 LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos++] = LORA_UART.getc(); // put it in the buffer
NarendraSingh 11:77e595130230 140 //DEBUG_UART.putc(LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos-1]);
NarendraSingh 11:77e595130230 141 if(Lora_RxBuffer_End_Pos >= LORA_UART_RX_Size)
NarendraSingh 11:77e595130230 142 {
NarendraSingh 11:77e595130230 143 // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation.
NarendraSingh 11:77e595130230 144 // For now just throw everything away.
NarendraSingh 11:77e595130230 145 Lora_RxBuffer_End_Pos = 0;
NarendraSingh 11:77e595130230 146 }
emilmont 1:491820ee784d 147 }
emilmont 1:491820ee784d 148 }
NarendraSingh 11:77e595130230 149
NarendraSingh 11:77e595130230 150
NarendraSingh 11:77e595130230 151 /*
NarendraSingh 11:77e595130230 152 // called every time a byte is received from Beacon Module.
NarendraSingh 11:77e595130230 153 void Beacon_onDataRx()
NarendraSingh 11:77e595130230 154 {
NarendraSingh 11:77e595130230 155 while (Beacon_UART.readable())
NarendraSingh 11:77e595130230 156 { // while there is data waiting
NarendraSingh 11:77e595130230 157 Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos++] = Beacon_UART.getc(); // put it in the buffer
NarendraSingh 11:77e595130230 158 //DEBUG_UART.putc(LORA_UART_RX_Buffer[Beacon_RxBuffer_End_Pos-1]);
NarendraSingh 11:77e595130230 159 if(Beacon_RxBuffer_End_Pos >= 100)
NarendraSingh 11:77e595130230 160 {
NarendraSingh 11:77e595130230 161 // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation.
NarendraSingh 11:77e595130230 162 // For now just throw everything away.
NarendraSingh 11:77e595130230 163 Beacon_RxBuffer_End_Pos = 0;
NarendraSingh 11:77e595130230 164 }
emilmont 1:491820ee784d 165 }
emilmont 1:491820ee784d 166 }
NarendraSingh 11:77e595130230 167 */
NarendraSingh 11:77e595130230 168
NarendraSingh 12:6107b32b0729 169 typedef struct {
NarendraSingh 12:6107b32b0729 170 uint8_t Queue_Msg_Type; /* A counter value */
NarendraSingh 12:6107b32b0729 171 } message_type;
NarendraSingh 12:6107b32b0729 172
NarendraSingh 12:6107b32b0729 173 MemoryPool<message_type, 16> mpool;
NarendraSingh 12:6107b32b0729 174 Queue<message_type, 16> queue;
NarendraSingh 12:6107b32b0729 175 Thread OBD_Thread;
NarendraSingh 12:6107b32b0729 176 Thread Accelerometer_Thread;
NarendraSingh 12:6107b32b0729 177
NarendraSingh 12:6107b32b0729 178 #define OBD_MSG_TYPE 0x01
NarendraSingh 12:6107b32b0729 179 #define ACCELEROMETER_MSG_TYPE 0x02
NarendraSingh 12:6107b32b0729 180
NarendraSingh 12:6107b32b0729 181 /* Send Thread */
NarendraSingh 12:6107b32b0729 182 void OBD_Process_Thread (void) {
NarendraSingh 12:6107b32b0729 183 while (true) {
NarendraSingh 12:6107b32b0729 184 //DEBUG_UART.printf("\n OBD Message start ");
NarendraSingh 12:6107b32b0729 185 message_type message = mpool.alloc();
NarendraSingh 12:6107b32b0729 186 message->Queue_Msg_Type = (uint8_t) OBD_MSG_TYPE;
NarendraSingh 12:6107b32b0729 187 queue.put(message);
NarendraSingh 12:6107b32b0729 188 //DEBUG_UART.printf("\n OBD Message Type Enqueued: \n");//, message->Queue_Msg_Type);
NarendraSingh 12:6107b32b0729 189 //Thread::wait(3000);
NarendraSingh 12:6107b32b0729 190 Thread::yield();
NarendraSingh 12:6107b32b0729 191 }
NarendraSingh 12:6107b32b0729 192 }
NarendraSingh 12:6107b32b0729 193
NarendraSingh 12:6107b32b0729 194 /* Send Thread */
NarendraSingh 12:6107b32b0729 195 void Accelerometer_Process_Thread (void) {
NarendraSingh 12:6107b32b0729 196 while (true) {
NarendraSingh 12:6107b32b0729 197 //DEBUG_UART.printf("\n Accelerometer Message start: ");
NarendraSingh 12:6107b32b0729 198 message_type message = mpool.alloc();
NarendraSingh 12:6107b32b0729 199 message->Queue_Msg_Type = (uint8_t) ACCELEROMETER_MSG_TYPE;
NarendraSingh 12:6107b32b0729 200 queue.put(message);
NarendraSingh 12:6107b32b0729 201 //DEBUG_UART.printf("\n Accelerometer Message Type Enqueued: \n");//, message->Queue_Msg_Type);
NarendraSingh 12:6107b32b0729 202 //Thread::wait(5000);
NarendraSingh 12:6107b32b0729 203 Thread::yield();
NarendraSingh 12:6107b32b0729 204 }
NarendraSingh 12:6107b32b0729 205 }
NarendraSingh 12:6107b32b0729 206
NarendraSingh 12:6107b32b0729 207 int main (void) {
NarendraSingh 12:6107b32b0729 208 DEBUG_UART.baud(115200);
NarendraSingh 12:6107b32b0729 209 //OBD_Thread.set_priority(osPriorityLow);
NarendraSingh 12:6107b32b0729 210 OBD_Thread.start(OBD_Process_Thread);
NarendraSingh 12:6107b32b0729 211 //DEBUG_UART.printf("\n main %d", __LINE__);
NarendraSingh 12:6107b32b0729 212 //Accelerometer_Thread.set_priority(osPriorityAboveNormal);
NarendraSingh 12:6107b32b0729 213 //DEBUG_UART.printf("\n main %d", __LINE__);
NarendraSingh 12:6107b32b0729 214 Accelerometer_Thread.start(Accelerometer_Process_Thread);
NarendraSingh 12:6107b32b0729 215 //DEBUG_UART.printf("\n main %d", __LINE__);
NarendraSingh 12:6107b32b0729 216 #if 1
NarendraSingh 12:6107b32b0729 217 while (true) {
NarendraSingh 12:6107b32b0729 218 DEBUG_UART.printf("\n main %d", __LINE__);
NarendraSingh 12:6107b32b0729 219 Thread::yield();
NarendraSingh 12:6107b32b0729 220 /*
NarendraSingh 12:6107b32b0729 221 osEvent evt = queue.get();
NarendraSingh 12:6107b32b0729 222 if (evt.status == osEventMessage) {
NarendraSingh 12:6107b32b0729 223 message_type *message = (message_type*)evt.value.p;
NarendraSingh 12:6107b32b0729 224 DEBUG_UART.printf("\nMessage Type Dequeued %d" , message->Queue_Msg_Type);
NarendraSingh 12:6107b32b0729 225 mpool.free(message);
NarendraSingh 12:6107b32b0729 226 }*/
NarendraSingh 12:6107b32b0729 227 }
NarendraSingh 12:6107b32b0729 228 #endif
NarendraSingh 12:6107b32b0729 229 OBD_Thread.join();
NarendraSingh 12:6107b32b0729 230 Accelerometer_Thread.join();
NarendraSingh 12:6107b32b0729 231 }
NarendraSingh 12:6107b32b0729 232
NarendraSingh 12:6107b32b0729 233
NarendraSingh 12:6107b32b0729 234
NarendraSingh 12:6107b32b0729 235
NarendraSingh 12:6107b32b0729 236
NarendraSingh 12:6107b32b0729 237 /*
NarendraSingh 11:77e595130230 238 int main()
NarendraSingh 11:77e595130230 239 {
NarendraSingh 11:77e595130230 240 DEBUG_UART.baud(115200);
NarendraSingh 11:77e595130230 241 DEBUG_UART.printf("%s","Debugging started");
NarendraSingh 11:77e595130230 242 LORA_UART.attach(&Lora_onDataRx, Serial::RxIrq);
NarendraSingh 11:77e595130230 243 //Beacon_UART.attach(&Beacon_onDataRx, Serial::RxIrq);
NarendraSingh 11:77e595130230 244
NarendraSingh 11:77e595130230 245 //Create a thread to read vehicle data
NarendraSingh 11:77e595130230 246 //Thread OBD_thread(OBD_Rcvd_Cmd_Processing_thread);
NarendraSingh 11:77e595130230 247
NarendraSingh 11:77e595130230 248 Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 3.0); // call flip_Packet_Sending_Flag function every 5 seconds
NarendraSingh 11:77e595130230 249
NarendraSingh 11:77e595130230 250 // OBD_PLUGIN_INTERRUPT_PIN.rise(&Enable_CheckIN_Packet_Sending); // call toggle function on the rising edge
NarendraSingh 11:77e595130230 251 //led2_thread is executing concurrently with main at this point
NarendraSingh 11:77e595130230 252 CheckIn_Interrupt.rise(&Handle_CheckIn_Interrupt);
NarendraSingh 11:77e595130230 253 inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge
NarendraSingh 12:6107b32b0729 254 //Initialize_Beacon_Module();
NarendraSingh 11:77e595130230 255 Lora_Periodic_Packet_Sending(); //Infinite loop for sending and receiving lora response, no return from here
NarendraSingh 11:77e595130230 256 }
NarendraSingh 12:6107b32b0729 257 */
NarendraSingh 11:77e595130230 258 //Functiont to be called when Interrupt is genearted for motion sensing, checkin
NarendraSingh 11:77e595130230 259 void Initialize_Packets_Sent_Count(void)
NarendraSingh 11:77e595130230 260 {
NarendraSingh 11:77e595130230 261 Motion_Packet_Sent_Count = 0x00;
NarendraSingh 11:77e595130230 262 CheckIN_Packet_Sent_Count = 0x00;
NarendraSingh 11:77e595130230 263 }
NarendraSingh 11:77e595130230 264
NarendraSingh 12:6107b32b0729 265 uint8 Status_Packet_Wait_Count = 0;
NarendraSingh 11:77e595130230 266 void Lora_Periodic_Packet_Sending()
NarendraSingh 11:77e595130230 267 {
NarendraSingh 11:77e595130230 268 DEBUG_UART.printf("Periodic packet sending intiialized");
NarendraSingh 11:77e595130230 269 Set_Up_Lora_Network_Configuration();
NarendraSingh 11:77e595130230 270 Initialize_lora_Packets();
NarendraSingh 11:77e595130230 271 while (true)
NarendraSingh 11:77e595130230 272 {
NarendraSingh 11:77e595130230 273 if(Packet_Type_To_Send == HEARTBEAT_TYPE_PACKET) //check if packet to be sent is Heartbeat packet
NarendraSingh 11:77e595130230 274 {
NarendraSingh 12:6107b32b0729 275 if(Send_Lora_Packet_Flag) //Check if packet sending is enabled, Packet should be sent only when enabled after timeout period
NarendraSingh 11:77e595130230 276 {
NarendraSingh 12:6107b32b0729 277 Status_Packet_Wait_Count++;
NarendraSingh 12:6107b32b0729 278 if(Status_Packet_Wait_Count < 5)
NarendraSingh 12:6107b32b0729 279 {
NarendraSingh 12:6107b32b0729 280 Send_HeartBeat_Packet(); //call function to send heartbeat packet
NarendraSingh 12:6107b32b0729 281 DEBUG_UART.printf("Sent HeartBeat Packet");
NarendraSingh 12:6107b32b0729 282 AT_Response_Receive_Status = FAILURE;
NarendraSingh 12:6107b32b0729 283 while(AT_Response_Receive_Status)
NarendraSingh 12:6107b32b0729 284 Get_Lora_Response();
NarendraSingh 12:6107b32b0729 285 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 12:6107b32b0729 286 DEBUG_UART.printf("Heartbeat Packet Response Received");
NarendraSingh 12:6107b32b0729 287 }
NarendraSingh 12:6107b32b0729 288 else
NarendraSingh 12:6107b32b0729 289 {
NarendraSingh 12:6107b32b0729 290 Status_Packet_Wait_Count = 0;
NarendraSingh 12:6107b32b0729 291 //Send_Vehicle_Status_Packet(); //call function to send heartbeat packet
NarendraSingh 12:6107b32b0729 292 DEBUG_UART.printf("Sent Status Packet");
NarendraSingh 12:6107b32b0729 293 AT_Response_Receive_Status = FAILURE;
NarendraSingh 12:6107b32b0729 294 while(AT_Response_Receive_Status)
NarendraSingh 12:6107b32b0729 295 Get_Lora_Response();
NarendraSingh 12:6107b32b0729 296 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 12:6107b32b0729 297 DEBUG_UART.printf("Status Packet Response Received");
NarendraSingh 12:6107b32b0729 298 }
NarendraSingh 11:77e595130230 299 }
NarendraSingh 11:77e595130230 300 }
NarendraSingh 11:77e595130230 301 else if(Packet_Type_To_Send == MOTION_TYPE_PACKET) //check if packet to be sent is motion packet
NarendraSingh 11:77e595130230 302 {
NarendraSingh 11:77e595130230 303 if(Send_Lora_Packet_Flag) //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period
NarendraSingh 11:77e595130230 304 {
NarendraSingh 11:77e595130230 305 Send_Motion_Packet();
NarendraSingh 11:77e595130230 306 DEBUG_UART.printf("Sent Motion Packet"); //call function to send periodic motion packet
NarendraSingh 11:77e595130230 307 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 308 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 309 Get_Lora_Response();
NarendraSingh 11:77e595130230 310 DEBUG_UART.printf("Motion Packet Response Received");
NarendraSingh 11:77e595130230 311 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 11:77e595130230 312 if(Motion_Packet_Sent_Count >= 5) //Stop Sending Motion Packets if after sending for 2 minute
NarendraSingh 11:77e595130230 313 {
NarendraSingh 11:77e595130230 314 DEBUG_UART.printf("Packet Type Sending Changed to HeartBeat");
NarendraSingh 11:77e595130230 315 Motion_Packet_Sent_Count = 0;
NarendraSingh 11:77e595130230 316 Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet
NarendraSingh 11:77e595130230 317 Lora_Packet_Sending_Ticker.detach(); //destroy ticker
NarendraSingh 11:77e595130230 318 Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 10.0); //create new ticker
NarendraSingh 11:77e595130230 319
NarendraSingh 11:77e595130230 320 }
NarendraSingh 11:77e595130230 321 }
NarendraSingh 11:77e595130230 322 }
NarendraSingh 11:77e595130230 323 else if(Packet_Type_To_Send == CHECKIN_TYPE_PACKET) //check if packet to be sent is Checkin packet
NarendraSingh 11:77e595130230 324 {
NarendraSingh 11:77e595130230 325 if(Send_Lora_Packet_Flag) //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period
NarendraSingh 11:77e595130230 326 {
NarendraSingh 11:77e595130230 327 Send_CheckIN_Packet(); //call function to send periodic checkIn packet
NarendraSingh 11:77e595130230 328 DEBUG_UART.printf("Sent Checkin Packet");
NarendraSingh 11:77e595130230 329 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 330 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 331 Get_Lora_Response();
NarendraSingh 11:77e595130230 332 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 11:77e595130230 333 DEBUG_UART.printf("Checkin Packet Response Received");
NarendraSingh 11:77e595130230 334 if(CheckIN_Packet_Sent_Count >= 5) //Stop Sending Motion Packets if after sending for 2 minute
NarendraSingh 11:77e595130230 335 {
NarendraSingh 11:77e595130230 336 DEBUG_UART.printf("Packet Type Sending Changed to HeartBeat");
NarendraSingh 11:77e595130230 337 CheckIN_Packet_Sent_Count = 0;
NarendraSingh 11:77e595130230 338 Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet
NarendraSingh 11:77e595130230 339 Initialize_Packets_Sent_Count();
NarendraSingh 11:77e595130230 340
NarendraSingh 11:77e595130230 341 }
NarendraSingh 11:77e595130230 342 }
NarendraSingh 11:77e595130230 343 }
NarendraSingh 11:77e595130230 344 if(OBD_PlugInOut_IOC_Status) //Check if Accelerometer Interrupt is generated
NarendraSingh 11:77e595130230 345 {
NarendraSingh 11:77e595130230 346 Enable_CheckIN_Packet_Sending();
NarendraSingh 11:77e595130230 347 //Get_Acceleration_Type();
NarendraSingh 11:77e595130230 348 OBD_PlugInOut_IOC_Status = FALSE;
NarendraSingh 12:6107b32b0729 349 Checkin_Detect_Status ^= 0x01;
NarendraSingh 12:6107b32b0729 350 if(Checkin_Detect_Status) //OBD Plugin detected
NarendraSingh 12:6107b32b0729 351 {
NarendraSingh 12:6107b32b0729 352 wait(1); //wait for 1sec to avoid detecting plugin debounce
NarendraSingh 12:6107b32b0729 353 CheckIn_Interrupt.fall(&Handle_CheckIn_Interrupt); //Change interrupt on change to falling type to detect plugout
NarendraSingh 12:6107b32b0729 354 //write code to enable motion interrupt
NarendraSingh 12:6107b32b0729 355 }
NarendraSingh 12:6107b32b0729 356 else //OBD Plugout detecetd
NarendraSingh 12:6107b32b0729 357 {
NarendraSingh 12:6107b32b0729 358 CheckIn_Interrupt.rise(&Handle_CheckIn_Interrupt); //Change interrupt on change to rising type to detect plugin
NarendraSingh 12:6107b32b0729 359 wait(1); //wait for 1sec to avoid detecting plugin debounce
NarendraSingh 12:6107b32b0729 360 //write code to disable motion interrupt
NarendraSingh 12:6107b32b0729 361 }
NarendraSingh 11:77e595130230 362 }
NarendraSingh 11:77e595130230 363 if(Motion_Detect_Status) //Check if Accelerometer Interrupt is generated
NarendraSingh 11:77e595130230 364 {
NarendraSingh 11:77e595130230 365 Get_Acceleration_Type();
NarendraSingh 11:77e595130230 366 Motion_Detect_Status = FALSE;
NarendraSingh 11:77e595130230 367 }
NarendraSingh 11:77e595130230 368 }
NarendraSingh 11:77e595130230 369 }
NarendraSingh 11:77e595130230 370
NarendraSingh 11:77e595130230 371 void Get_Acceleration_Type(void)
NarendraSingh 11:77e595130230 372 {
NarendraSingh 11:77e595130230 373 Packet_Type_To_Send = MOTION_TYPE_PACKET; //whenever acceleration is detected change the packet type to be sent to motion type
NarendraSingh 11:77e595130230 374 Lora_Packet_Sending_Ticker.detach(); //destroy ticker
NarendraSingh 11:77e595130230 375 Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 5.0); //create new ticker, time inetrval value to be changed
NarendraSingh 11:77e595130230 376 Send_Lora_Packet_Flag = 1; //set flag to send motion packet as soon as motion is detecetd
NarendraSingh 11:77e595130230 377 Motion_Lora_Packet.Acceleration_Type = Motion_Type_Detected; //Read Type of motion deteceted
NarendraSingh 11:77e595130230 378 //write code to read acceleration value for every 10sec after any of the acceleration is found
NarendraSingh 11:77e595130230 379 }
NarendraSingh 11:77e595130230 380
NarendraSingh 11:77e595130230 381 //This function is used to enable checkin packet sending once OBD device is plugged in
NarendraSingh 11:77e595130230 382 void Enable_CheckIN_Packet_Sending()
NarendraSingh 11:77e595130230 383 {
NarendraSingh 12:6107b32b0729 384 // Enable_CheckIN_Packet_Sending = TRUE; //set status to true
NarendraSingh 12:6107b32b0729 385 /* if(Command_Received[0] == 0xFE) //check start of frame of packet received. every packet must start with 0xFE
NarendraSingh 11:77e595130230 386 {
NarendraSingh 11:77e595130230 387 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 388 {
NarendraSingh 11:77e595130230 389 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 390 {
NarendraSingh 11:77e595130230 391 Process_Beacon_Command_Received((Command_Received + 2));
NarendraSingh 11:77e595130230 392 }
NarendraSingh 11:77e595130230 393 }
NarendraSingh 12:6107b32b0729 394 }*/
NarendraSingh 11:77e595130230 395 }