kumar singh
/
Dealer_23Feb
Dummy Lora Packet Sending
Fork of Dealer_18feb17 by
Accelerometer.cpp@24:1063cfc311e5, 2017-02-23 (annotated)
- Committer:
- NarendraSingh
- Date:
- Thu Feb 23 13:21:08 2017 +0000
- Revision:
- 24:1063cfc311e5
- Parent:
- 11:77e595130230
Dummy Lora Packet Sending
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NarendraSingh | 11:77e595130230 | 1 | |
NarendraSingh | 11:77e595130230 | 2 | // INTERFACING ADXL345 ACCELEROMETER USING I2C |
NarendraSingh | 11:77e595130230 | 3 | |
NarendraSingh | 11:77e595130230 | 4 | /* |
NarendraSingh | 11:77e595130230 | 5 | NOTE : |
NarendraSingh | 11:77e595130230 | 6 | |
NarendraSingh | 11:77e595130230 | 7 | ACTIVITY : ACCELEROMETER IS ACCELERATING AND ITS VALUES ARE GREATER THAN THE ACTIVITY THRESHOLD |
NarendraSingh | 11:77e595130230 | 8 | INACTIVITY : ACCELEROMETER IS NOT IN ACCELERATION OR ACCELERATING VERY SLIGHTLY THAT ITS VALUES ARE BELOW THE INACTIVITY THRESHOLD |
NarendraSingh | 11:77e595130230 | 9 | DOUBLE TAP : SUDDEN JERK CONDITION (IDENIFIED BY SUDDEN VARIATIONS IN THE ACCELERATION VALUES) |
NarendraSingh | 11:77e595130230 | 10 | |
NarendraSingh | 11:77e595130230 | 11 | |
NarendraSingh | 11:77e595130230 | 12 | Due to communication speed limitations, the maximum output |
NarendraSingh | 11:77e595130230 | 13 | data rate when using 400 kHz I2C is 800 Hz and scales linearly with |
NarendraSingh | 11:77e595130230 | 14 | a change in the I2C communication speed |
NarendraSingh | 11:77e595130230 | 15 | |
NarendraSingh | 11:77e595130230 | 16 | */ |
NarendraSingh | 11:77e595130230 | 17 | /* |
NarendraSingh | 11:77e595130230 | 18 | #include "Accelerometer.h" |
NarendraSingh | 11:77e595130230 | 19 | |
NarendraSingh | 11:77e595130230 | 20 | #define DOUBLE_TAP_INTERRUPT 0x20 |
NarendraSingh | 11:77e595130230 | 21 | #define ACTIVITY_INTERRUPT 0x10 |
NarendraSingh | 11:77e595130230 | 22 | #define INACTIVITY_INTERRUPT 0x08 |
NarendraSingh | 11:77e595130230 | 23 | |
NarendraSingh | 11:77e595130230 | 24 | #define TAP_THRESHOLD 75 |
NarendraSingh | 11:77e595130230 | 25 | #define ACTIVITY_THRESHOLD 64 // THRES_ACT register value 62.5mg/LSB , therfore value 32 indicates 2g activity |
NarendraSingh | 11:77e595130230 | 26 | #define INACTIVITY_THRESHOLD 50 |
NarendraSingh | 11:77e595130230 | 27 | |
NarendraSingh | 11:77e595130230 | 28 | #define DUR_TIME 0x15 // DUR Register value providing maximum time to be held to generate an interrupt |
NarendraSingh | 11:77e595130230 | 29 | #define LATENT_TIME 0x15 // The interrupt latency |
NarendraSingh | 11:77e595130230 | 30 | #define WINDOW_TIME 0x45 // The time of the interrupt window in which the next tap will be detected |
NarendraSingh | 11:77e595130230 | 31 | #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 | 32 | // Here the value 5 indicates literally 5 secs |
NarendraSingh | 11:77e595130230 | 33 | #define X_AXIS_OFFSET 0x7F |
NarendraSingh | 11:77e595130230 | 34 | #define Y_AXIS_OFFSET 0x7F |
NarendraSingh | 11:77e595130230 | 35 | #define Z_AXIS_OFFSET 0x05 |
NarendraSingh | 11:77e595130230 | 36 | |
NarendraSingh | 11:77e595130230 | 37 | |
NarendraSingh | 11:77e595130230 | 38 | I2C i2c(PB_9, PB_8); |
NarendraSingh | 11:77e595130230 | 39 | RawSerial DEBUG_UART1(PA_9, PA_10);//USART1_TX->PA_9,USART1_RX->PA_10 |
NarendraSingh | 11:77e595130230 | 40 | |
NarendraSingh | 11:77e595130230 | 41 | InterruptIn activity(PB_0); |
NarendraSingh | 11:77e595130230 | 42 | InterruptIn inactivity(PA_4); // As for now only this is used |
NarendraSingh | 11:77e595130230 | 43 | DigitalOut led(LED1); |
NarendraSingh | 11:77e595130230 | 44 | |
NarendraSingh | 11:77e595130230 | 45 | const int slave_address_acc = 0xA6; |
NarendraSingh | 11:77e595130230 | 46 | char axis_data[6] = {0,0,0,0,0,0}; |
NarendraSingh | 11:77e595130230 | 47 | |
NarendraSingh | 11:77e595130230 | 48 | char interrupt_source[2]; |
NarendraSingh | 11:77e595130230 | 49 | char axis_data_start_address[2] = {0x32, 0}; |
NarendraSingh | 11:77e595130230 | 50 | char intr_source_address[2] = {0x30, 0}; |
NarendraSingh | 11:77e595130230 | 51 | char all_interrupt_clear_command[2] = {0x2E, 0x00}; |
NarendraSingh | 11:77e595130230 | 52 | char all_interrupt_enable_command[2] = {0x2E, 0x38}; |
NarendraSingh | 11:77e595130230 | 53 | char activity_interrupt_disable_command[2] = {0x2E, 0x08}; |
NarendraSingh | 11:77e595130230 | 54 | char inactivity_interrupt_disable_command[2] = {0x2E, 0x30}; |
NarendraSingh | 11:77e595130230 | 55 | char accelerometer_status_registered = 0; |
NarendraSingh | 11:77e595130230 | 56 | unsigned int interrupt_source_duplicate; |
NarendraSingh | 11:77e595130230 | 57 | |
NarendraSingh | 11:77e595130230 | 58 | char threshold_offset_command[5]; |
NarendraSingh | 11:77e595130230 | 59 | char act_inact_time_config_command[8]; |
NarendraSingh | 11:77e595130230 | 60 | char interrupt_enable_command[3]; |
NarendraSingh | 11:77e595130230 | 61 | char tap_axis_enable_command[2]; |
NarendraSingh | 11:77e595130230 | 62 | char baud_rate_command[2]; |
NarendraSingh | 11:77e595130230 | 63 | char data_format_command[2]; |
NarendraSingh | 11:77e595130230 | 64 | char measure_bit_on_command[2]; |
NarendraSingh | 11:77e595130230 | 65 | |
NarendraSingh | 11:77e595130230 | 66 | |
NarendraSingh | 11:77e595130230 | 67 | char previous_state = 0; |
NarendraSingh | 11:77e595130230 | 68 | char current_state = 0; |
NarendraSingh | 11:77e595130230 | 69 | |
NarendraSingh | 11:77e595130230 | 70 | unsigned char vehicle_speed = 25; // Kmph |
NarendraSingh | 11:77e595130230 | 71 | unsigned char current_speed, previous_speed, speed_threshold = 10; // Kmph |
NarendraSingh | 11:77e595130230 | 72 | |
NarendraSingh | 11:77e595130230 | 73 | unsigned char x_axis, y_axis, z_axis; |
NarendraSingh | 11:77e595130230 | 74 | |
NarendraSingh | 11:77e595130230 | 75 | unsigned char Motion_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 76 | uint8 Accelerometer_Interrupt_Generated = FALSE; |
NarendraSingh | 11:77e595130230 | 77 | unsigned char Motion_Type_Detected = MOTION_TYPE_UNKNOWN; //By default set motion type as unknown |
NarendraSingh | 11:77e595130230 | 78 | void Accelerometer_Process_thread(void const *args) ; |
NarendraSingh | 11:77e595130230 | 79 | |
NarendraSingh | 11:77e595130230 | 80 | //---------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 81 | |
NarendraSingh | 11:77e595130230 | 82 | // CONVERTS THE CHAR DATA TO UNSIGNED INTERGER DATA |
NarendraSingh | 11:77e595130230 | 83 | |
NarendraSingh | 11:77e595130230 | 84 | void char_to_int(char data_fetched) |
NarendraSingh | 11:77e595130230 | 85 | { |
NarendraSingh | 11:77e595130230 | 86 | unsigned int shifter; |
NarendraSingh | 11:77e595130230 | 87 | interrupt_source_duplicate = 0x00; |
NarendraSingh | 11:77e595130230 | 88 | for(shifter = 0; shifter < 8; shifter++) |
NarendraSingh | 11:77e595130230 | 89 | { |
NarendraSingh | 11:77e595130230 | 90 | interrupt_source_duplicate |= (((data_fetched >> shifter) & 0x01) << shifter); // Converts char data into unsigned int |
NarendraSingh | 11:77e595130230 | 91 | } |
NarendraSingh | 11:77e595130230 | 92 | } |
NarendraSingh | 11:77e595130230 | 93 | |
NarendraSingh | 11:77e595130230 | 94 | //---------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 95 | |
NarendraSingh | 11:77e595130230 | 96 | // LEAVE THIS ROUTINE COMMENTED (IT JUST PRINTS THE DATA IN BINARY FORMAT) |
NarendraSingh | 11:77e595130230 | 97 | |
NarendraSingh | 11:77e595130230 | 98 | void print_data_bits(char data_fetched) |
NarendraSingh | 11:77e595130230 | 99 | { |
NarendraSingh | 11:77e595130230 | 100 | unsigned int shifter; |
NarendraSingh | 11:77e595130230 | 101 | for(shifter = 0; shifter < 8; shifter++) |
NarendraSingh | 11:77e595130230 | 102 | { |
NarendraSingh | 11:77e595130230 | 103 | DEBUG_UART1.printf("%d",((data_fetched&0x80)>>7)); |
NarendraSingh | 11:77e595130230 | 104 | data_fetched = data_fetched << 1; |
NarendraSingh | 11:77e595130230 | 105 | } |
NarendraSingh | 11:77e595130230 | 106 | DEBUG_UART1.printf("\r\n\r\n"); |
NarendraSingh | 11:77e595130230 | 107 | } |
NarendraSingh | 11:77e595130230 | 108 | |
NarendraSingh | 11:77e595130230 | 109 | //---------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 110 | |
NarendraSingh | 11:77e595130230 | 111 | void get_vehicle_speed(void) |
NarendraSingh | 11:77e595130230 | 112 | { |
NarendraSingh | 11:77e595130230 | 113 | // PASS OBD COMMAND "010D<CR>" AND FETCH THE VEHICLE SPEED AT THIS POINT |
NarendraSingh | 11:77e595130230 | 114 | } |
NarendraSingh | 11:77e595130230 | 115 | |
NarendraSingh | 11:77e595130230 | 116 | |
NarendraSingh | 11:77e595130230 | 117 | void interrupt_activity_inactivity() |
NarendraSingh | 11:77e595130230 | 118 | { |
NarendraSingh | 11:77e595130230 | 119 | i2c.write(slave_address_acc, all_interrupt_clear_command, 2); |
NarendraSingh | 11:77e595130230 | 120 | Motion_Detect_Status = TRUE; |
NarendraSingh | 11:77e595130230 | 121 | } |
NarendraSingh | 11:77e595130230 | 122 | |
NarendraSingh | 11:77e595130230 | 123 | //---------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 124 | //this thread is to send lora packets periodically. |
NarendraSingh | 11:77e595130230 | 125 | void Accelerometer_Process_thread(void const *args) |
NarendraSingh | 11:77e595130230 | 126 | { |
NarendraSingh | 11:77e595130230 | 127 | DEBUG_UART1.baud(115200); |
NarendraSingh | 11:77e595130230 | 128 | DEBUG_UART1.printf(" Hello\n"); |
NarendraSingh | 11:77e595130230 | 129 | inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge |
NarendraSingh | 11:77e595130230 | 130 | |
NarendraSingh | 11:77e595130230 | 131 | // THE FOLLOWING GROUP OF COMMAND VARIABLES STORES THE CONFIGURATION VALUES TO BE WRITTEN TO THE ADXL345 ACCELEROMETER |
NarendraSingh | 11:77e595130230 | 132 | threshold_offset_command[0] = 0x1D; // Threshold Tap Register address |
NarendraSingh | 11:77e595130230 | 133 | threshold_offset_command[1] = TAP_THRESHOLD; // Threshold tap Register value |
NarendraSingh | 11:77e595130230 | 134 | threshold_offset_command[2] = X_AXIS_OFFSET; // Offset - X axis |
NarendraSingh | 11:77e595130230 | 135 | threshold_offset_command[3] = Y_AXIS_OFFSET; // Offset - Y axis |
NarendraSingh | 11:77e595130230 | 136 | threshold_offset_command[4] = Z_AXIS_OFFSET; // Offset - Z axis |
NarendraSingh | 11:77e595130230 | 137 | |
NarendraSingh | 11:77e595130230 | 138 | act_inact_time_config_command[0] = 0x21; // DUR Register address |
NarendraSingh | 11:77e595130230 | 139 | act_inact_time_config_command[1] = DUR_TIME; |
NarendraSingh | 11:77e595130230 | 140 | act_inact_time_config_command[2] = LATENT_TIME; |
NarendraSingh | 11:77e595130230 | 141 | act_inact_time_config_command[3] = WINDOW_TIME; |
NarendraSingh | 11:77e595130230 | 142 | act_inact_time_config_command[4] = ACTIVITY_THRESHOLD; |
NarendraSingh | 11:77e595130230 | 143 | act_inact_time_config_command[5] = INACTIVITY_THRESHOLD; // THRES_INACT Register |
NarendraSingh | 11:77e595130230 | 144 | act_inact_time_config_command[6] = INACTIVITY_VALIDATION_TIME; // TIME_INACT Register, making inactivity detection time = 5 secs |
NarendraSingh | 11:77e595130230 | 145 | act_inact_time_config_command[7] = 0x77; // Activity, Inactivity detection enabled for all axis |
NarendraSingh | 11:77e595130230 | 146 | |
NarendraSingh | 11:77e595130230 | 147 | interrupt_enable_command[0] = 0x2E; // INT Enable Register address |
NarendraSingh | 11:77e595130230 | 148 | interrupt_enable_command[1] = 0x38; // Enabling Double tap (sudden jerk), Activity & Inactivity Interrupts |
NarendraSingh | 11:77e595130230 | 149 | interrupt_enable_command[2] = 0xFF; // Double tap (sudden jerk), Activity & inactivity interrupts are mapped to INT2 pin |
NarendraSingh | 11:77e595130230 | 150 | |
NarendraSingh | 11:77e595130230 | 151 | tap_axis_enable_command[0] = 0x2A; // Address of the TAP_AXES Register |
NarendraSingh | 11:77e595130230 | 152 | tap_axis_enable_command[1] = 0x07; // X, Y & Z axis participate in tap detection |
NarendraSingh | 11:77e595130230 | 153 | |
NarendraSingh | 11:77e595130230 | 154 | baud_rate_command[0] = 0x2C; // Address of the BW RATE register |
NarendraSingh | 11:77e595130230 | 155 | baud_rate_command[1] = 0x0D; // Increased the data rate to 800Hz, default is 0x0A indicating 100Hz |
NarendraSingh | 11:77e595130230 | 156 | |
NarendraSingh | 11:77e595130230 | 157 | data_format_command[0] = 0x31; // Data format register address |
NarendraSingh | 11:77e595130230 | 158 | data_format_command[1] = 0x04; // Making the acceleration data as left justified |
NarendraSingh | 11:77e595130230 | 159 | |
NarendraSingh | 11:77e595130230 | 160 | measure_bit_on_command[0] = 0x2D; // Post the Register address of the slave (Have to write this into slave) |
NarendraSingh | 11:77e595130230 | 161 | measure_bit_on_command[1] = 0x08; // Turn ON the Measure Bit |
NarendraSingh | 11:77e595130230 | 162 | |
NarendraSingh | 11:77e595130230 | 163 | i2c.write(slave_address_acc, threshold_offset_command, 5); |
NarendraSingh | 11:77e595130230 | 164 | i2c.write(slave_address_acc, act_inact_time_config_command, 8); |
NarendraSingh | 11:77e595130230 | 165 | i2c.write(slave_address_acc, interrupt_enable_command, 3); |
NarendraSingh | 11:77e595130230 | 166 | i2c.write(slave_address_acc, tap_axis_enable_command, 2); |
NarendraSingh | 11:77e595130230 | 167 | i2c.write(slave_address_acc, baud_rate_command, 2); |
NarendraSingh | 11:77e595130230 | 168 | i2c.write(slave_address_acc, data_format_command, 2); |
NarendraSingh | 11:77e595130230 | 169 | i2c.write(slave_address_acc, measure_bit_on_command, 2); |
NarendraSingh | 11:77e595130230 | 170 | |
NarendraSingh | 11:77e595130230 | 171 | DEBUG_UART1.printf(" ACCELEROMETER DATA LOG \r\n\r\n"); |
NarendraSingh | 11:77e595130230 | 172 | |
NarendraSingh | 11:77e595130230 | 173 | while (1) |
NarendraSingh | 11:77e595130230 | 174 | { |
NarendraSingh | 11:77e595130230 | 175 | if(Motion_Detect_Status) |
NarendraSingh | 11:77e595130230 | 176 | { |
NarendraSingh | 11:77e595130230 | 177 | // The following statement disables all interrupts since no other interrupts must disturb at this point |
NarendraSingh | 11:77e595130230 | 178 | i2c.write(slave_address_acc, intr_source_address, 1); |
NarendraSingh | 11:77e595130230 | 179 | i2c.read(slave_address_acc, interrupt_source, 1); // Reads the Interrupt Source Register |
NarendraSingh | 11:77e595130230 | 180 | char_to_int(interrupt_source[0]); // Coverts intr_source(char) to int & stores in intr_source_duplicate |
NarendraSingh | 11:77e595130230 | 181 | |
NarendraSingh | 11:77e595130230 | 182 | Motion_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 183 | Motion_Type_Detected = MOTION_TYPE_UNKNOWN; |
NarendraSingh | 11:77e595130230 | 184 | // USE THE FOLLOWING BLOCK TO READ THE DATA IN X-AXIS, Y-AXIS & Z-AXIS |
NarendraSingh | 11:77e595130230 | 185 | //--------------------------------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 186 | if(interrupt_source_duplicate & DOUBLE_TAP_INTERRUPT) // Check whether it is double tap (sudden jerk) interrupt |
NarendraSingh | 11:77e595130230 | 187 | { |
NarendraSingh | 11:77e595130230 | 188 | // ATTACH YOUR SUDDEN JERK EXECUTION ROUTINE HERE |
NarendraSingh | 11:77e595130230 | 189 | i2c.write(slave_address_acc, inactivity_interrupt_disable_command, 2); // Disables Inactivity interrupt & enables Double Tap & Activity interrupt |
NarendraSingh | 11:77e595130230 | 190 | DEBUG_UART1.printf("ENTERED SUDDEN JERK CONDITION \r\n\r\n"); // To be removed |
NarendraSingh | 11:77e595130230 | 191 | interrupt_source_duplicate = 0x00; // Sudden jerk also comes with activity interrupt triggered, hence this statements makes the activity check fail |
NarendraSingh | 11:77e595130230 | 192 | Motion_Type_Detected = MOTION_TYPE_TAP; |
NarendraSingh | 11:77e595130230 | 193 | Accelerometer_Interrupt_Generated = TRUE; |
NarendraSingh | 11:77e595130230 | 194 | } |
NarendraSingh | 11:77e595130230 | 195 | //--------------------------------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 196 | // VERIFY WHETHER THE INTERRUPT IS BECAUSE OF ACTIVITY |
NarendraSingh | 11:77e595130230 | 197 | |
NarendraSingh | 11:77e595130230 | 198 | if(interrupt_source_duplicate & ACTIVITY_INTERRUPT) |
NarendraSingh | 11:77e595130230 | 199 | { |
NarendraSingh | 11:77e595130230 | 200 | i2c.write(slave_address_acc, activity_interrupt_disable_command, 2); // Disables Activity interrupt & enables Inactivity interrupt |
NarendraSingh | 11:77e595130230 | 201 | get_vehicle_speed(); |
NarendraSingh | 11:77e595130230 | 202 | previous_speed = vehicle_speed; |
NarendraSingh | 11:77e595130230 | 203 | wait(5); |
NarendraSingh | 11:77e595130230 | 204 | get_vehicle_speed(); |
NarendraSingh | 11:77e595130230 | 205 | current_speed = vehicle_speed; |
NarendraSingh | 11:77e595130230 | 206 | if((current_speed > previous_speed) && (current_speed > speed_threshold)) |
NarendraSingh | 11:77e595130230 | 207 | { |
NarendraSingh | 11:77e595130230 | 208 | Motion_Type_Detected = MOTION_TYPE_STOP_TO_START; |
NarendraSingh | 11:77e595130230 | 209 | Accelerometer_Interrupt_Generated = TRUE; |
NarendraSingh | 11:77e595130230 | 210 | DEBUG_UART1.printf("VEHICLE HAS STARTED (STOP TO START) \r\n"); |
NarendraSingh | 11:77e595130230 | 211 | // ATTACH UR STOP TO START ROUTINE HERE |
NarendraSingh | 11:77e595130230 | 212 | } |
NarendraSingh | 11:77e595130230 | 213 | } |
NarendraSingh | 11:77e595130230 | 214 | //--------------------------------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 215 | |
NarendraSingh | 11:77e595130230 | 216 | // VERIFY WHETHER THE INTERRUPT IS BECAUSE OF INACTIVITY |
NarendraSingh | 11:77e595130230 | 217 | if(interrupt_source_duplicate & INACTIVITY_INTERRUPT) |
NarendraSingh | 11:77e595130230 | 218 | { |
NarendraSingh | 11:77e595130230 | 219 | i2c.write(slave_address_acc, inactivity_interrupt_disable_command, 2); // Disables Inactivity interrupt & enables Activity interrupt |
NarendraSingh | 11:77e595130230 | 220 | get_vehicle_speed(); |
NarendraSingh | 11:77e595130230 | 221 | if(vehicle_speed == 0) |
NarendraSingh | 11:77e595130230 | 222 | { |
NarendraSingh | 11:77e595130230 | 223 | Motion_Type_Detected = MOTION_TYPE_START_TO_STOP; |
NarendraSingh | 11:77e595130230 | 224 | Accelerometer_Interrupt_Generated = TRUE; |
NarendraSingh | 11:77e595130230 | 225 | // ATTACH YOUR START TO STOP PROCESS OVERFLOW HERE |
NarendraSingh | 11:77e595130230 | 226 | DEBUG_UART1.printf("VEHICLE HAS STOPPED (START TO STOP) \r\n"); |
NarendraSingh | 11:77e595130230 | 227 | } |
NarendraSingh | 11:77e595130230 | 228 | } |
NarendraSingh | 11:77e595130230 | 229 | wait(0.25); |
NarendraSingh | 11:77e595130230 | 230 | i2c.write(slave_address_acc, axis_data_start_address, 1); |
NarendraSingh | 11:77e595130230 | 231 | i2c.read(slave_address_acc, axis_data, 6); |
NarendraSingh | 11:77e595130230 | 232 | |
NarendraSingh | 11:77e595130230 | 233 | x_axis = axis_data[1]; // Puts MSB data into respective axes |
NarendraSingh | 11:77e595130230 | 234 | y_axis = axis_data[3]; |
NarendraSingh | 11:77e595130230 | 235 | z_axis = axis_data[5]; |
NarendraSingh | 11:77e595130230 | 236 | } |
NarendraSingh | 11:77e595130230 | 237 | Thread::wait(200); //wait for 100msec |
NarendraSingh | 11:77e595130230 | 238 | } |
NarendraSingh | 11:77e595130230 | 239 | }*/ |
NarendraSingh | 11:77e595130230 | 240 | |
NarendraSingh | 11:77e595130230 | 241 | |
NarendraSingh | 11:77e595130230 | 242 | /* |
NarendraSingh | 11:77e595130230 | 243 | |
NarendraSingh | 11:77e595130230 | 244 | // INTERFACING ADXL345 ACCELEROMETER USING I2C |
NarendraSingh | 11:77e595130230 | 245 | |
NarendraSingh | 11:77e595130230 | 246 | /* |
NarendraSingh | 11:77e595130230 | 247 | NOTE : |
NarendraSingh | 11:77e595130230 | 248 | |
NarendraSingh | 11:77e595130230 | 249 | ACTIVITY : ACCELEROMETER IS ACCELERATING AND ITS VALUES ARE GREATER THAN THE ACTIVITY THRESHOLD |
NarendraSingh | 11:77e595130230 | 250 | INACTIVITY : ACCELEROMETER IS NOT IN ACCELERATION OR ACCELERATING VERY SLIGHTLY THAT ITS VALUES ARE BELOW THE INACTIVITY THRESHOLD |
NarendraSingh | 11:77e595130230 | 251 | DOUBLE TAP : SUDDEN JERK CONDITION (IDENIFIED BY SUDDEN VARIATIONS IN THE ACCELERATION VALUES) |
NarendraSingh | 11:77e595130230 | 252 | |
NarendraSingh | 11:77e595130230 | 253 | |
NarendraSingh | 11:77e595130230 | 254 | Due to communication speed limitations, the maximum output |
NarendraSingh | 11:77e595130230 | 255 | data rate when using 400 kHz I2C is 800 Hz and scales linearly with |
NarendraSingh | 11:77e595130230 | 256 | a change in the I2C communication speed |
NarendraSingh | 11:77e595130230 | 257 | |
NarendraSingh | 11:77e595130230 | 258 | */ |
NarendraSingh | 11:77e595130230 | 259 | |
NarendraSingh | 11:77e595130230 | 260 | /* |
NarendraSingh | 11:77e595130230 | 261 | |
NarendraSingh | 11:77e595130230 | 262 | //---------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 263 | |
NarendraSingh | 11:77e595130230 | 264 | // CONVERTS THE CHAR DATA TO UNSIGNED INTERGER DATA |
NarendraSingh | 11:77e595130230 | 265 | |
NarendraSingh | 11:77e595130230 | 266 | void char_to_int(char data_fetched) |
NarendraSingh | 11:77e595130230 | 267 | { |
NarendraSingh | 11:77e595130230 | 268 | unsigned int shifter; |
NarendraSingh | 11:77e595130230 | 269 | interrupt_source_duplicate = 0x00; |
NarendraSingh | 11:77e595130230 | 270 | for(shifter = 0; shifter < 8; shifter++) |
NarendraSingh | 11:77e595130230 | 271 | { |
NarendraSingh | 11:77e595130230 | 272 | interrupt_source_duplicate |= (((data_fetched >> shifter) & 0x01) << shifter); // Converts char data into unsigned int |
NarendraSingh | 11:77e595130230 | 273 | } |
NarendraSingh | 11:77e595130230 | 274 | } |
NarendraSingh | 11:77e595130230 | 275 | |
NarendraSingh | 11:77e595130230 | 276 | //---------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 277 | |
NarendraSingh | 11:77e595130230 | 278 | // LEAVE THIS ROUTINE COMMENTED (IT JUST PRINTS THE DATA IN BINARY FORMAT) |
NarendraSingh | 11:77e595130230 | 279 | |
NarendraSingh | 11:77e595130230 | 280 | void print_data_bits(char data_fetched) |
NarendraSingh | 11:77e595130230 | 281 | { |
NarendraSingh | 11:77e595130230 | 282 | unsigned int shifter; |
NarendraSingh | 11:77e595130230 | 283 | for(shifter = 0; shifter < 8; shifter++) |
NarendraSingh | 11:77e595130230 | 284 | { |
NarendraSingh | 11:77e595130230 | 285 | DEBUG_UART1.printf("%d",((data_fetched&0x80)>>7)); |
NarendraSingh | 11:77e595130230 | 286 | data_fetched = data_fetched << 1; |
NarendraSingh | 11:77e595130230 | 287 | } |
NarendraSingh | 11:77e595130230 | 288 | DEBUG_UART1.printf("\r\n\r\n"); |
NarendraSingh | 11:77e595130230 | 289 | } |
NarendraSingh | 11:77e595130230 | 290 | |
NarendraSingh | 11:77e595130230 | 291 | //---------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 292 | |
NarendraSingh | 11:77e595130230 | 293 | void get_vehicle_speed(void) |
NarendraSingh | 11:77e595130230 | 294 | { |
NarendraSingh | 11:77e595130230 | 295 | // PASS OBD COMMAND "010D<CR>" AND FETCH THE VEHICLE SPEED AT THIS POINT |
NarendraSingh | 11:77e595130230 | 296 | } |
NarendraSingh | 11:77e595130230 | 297 | |
NarendraSingh | 11:77e595130230 | 298 | |
NarendraSingh | 11:77e595130230 | 299 | //void interrupt_activity_inactivity() |
NarendraSingh | 11:77e595130230 | 300 | //{ |
NarendraSingh | 11:77e595130230 | 301 | //i2c.write(slave_address_acc, all_interrupt_clear_command, 2); |
NarendraSingh | 11:77e595130230 | 302 | //DEBUG_UART1.printf(" A"); |
NarendraSingh | 11:77e595130230 | 303 | // Motion_Detect_Status = TRUE; |
NarendraSingh | 11:77e595130230 | 304 | //} |
NarendraSingh | 11:77e595130230 | 305 | |
NarendraSingh | 11:77e595130230 | 306 | //---------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 307 | //this thread is to send lora packets periodically. |
NarendraSingh | 11:77e595130230 | 308 | void Accelerometer_Process_thread()//void const *args) |
NarendraSingh | 11:77e595130230 | 309 | { |
NarendraSingh | 11:77e595130230 | 310 | DEBUG_UART1.baud(115200); |
NarendraSingh | 11:77e595130230 | 311 | // inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge |
NarendraSingh | 11:77e595130230 | 312 | |
NarendraSingh | 11:77e595130230 | 313 | // THE FOLLOWING GROUP OF COMMAND VARIABLES STORES THE CONFIGURATION VALUES TO BE WRITTEN TO THE ADXL345 ACCELEROMETER |
NarendraSingh | 11:77e595130230 | 314 | threshold_offset_command[0] = 0x1D; // Threshold Tap Register address |
NarendraSingh | 11:77e595130230 | 315 | threshold_offset_command[1] = TAP_THRESHOLD; // Threshold tap Register value |
NarendraSingh | 11:77e595130230 | 316 | threshold_offset_command[2] = X_AXIS_OFFSET; // Offset - X axis |
NarendraSingh | 11:77e595130230 | 317 | threshold_offset_command[3] = Y_AXIS_OFFSET; // Offset - Y axis |
NarendraSingh | 11:77e595130230 | 318 | threshold_offset_command[4] = Z_AXIS_OFFSET; // Offset - Z axis |
NarendraSingh | 11:77e595130230 | 319 | |
NarendraSingh | 11:77e595130230 | 320 | act_inact_time_config_command[0] = 0x21; // DUR Register address |
NarendraSingh | 11:77e595130230 | 321 | act_inact_time_config_command[1] = DUR_TIME; |
NarendraSingh | 11:77e595130230 | 322 | act_inact_time_config_command[2] = LATENT_TIME; |
NarendraSingh | 11:77e595130230 | 323 | act_inact_time_config_command[3] = WINDOW_TIME; |
NarendraSingh | 11:77e595130230 | 324 | act_inact_time_config_command[4] = ACTIVITY_THRESHOLD; |
NarendraSingh | 11:77e595130230 | 325 | act_inact_time_config_command[5] = INACTIVITY_THRESHOLD; // THRES_INACT Register |
NarendraSingh | 11:77e595130230 | 326 | act_inact_time_config_command[6] = INACTIVITY_VALIDATION_TIME; // TIME_INACT Register, making inactivity detection time = 5 secs |
NarendraSingh | 11:77e595130230 | 327 | act_inact_time_config_command[7] = 0x77; // Activity, Inactivity detection enabled for all axis |
NarendraSingh | 11:77e595130230 | 328 | |
NarendraSingh | 11:77e595130230 | 329 | interrupt_enable_command[0] = 0x2E; // INT Enable Register address |
NarendraSingh | 11:77e595130230 | 330 | interrupt_enable_command[1] = 0x38; // Enabling Double tap (sudden jerk), Activity & Inactivity Interrupts |
NarendraSingh | 11:77e595130230 | 331 | interrupt_enable_command[2] = 0xFF; // Double tap (sudden jerk), Activity & inactivity interrupts are mapped to INT2 pin |
NarendraSingh | 11:77e595130230 | 332 | |
NarendraSingh | 11:77e595130230 | 333 | tap_axis_enable_command[0] = 0x2A; // Address of the TAP_AXES Register |
NarendraSingh | 11:77e595130230 | 334 | tap_axis_enable_command[1] = 0x07; // X, Y & Z axis participate in tap detection |
NarendraSingh | 11:77e595130230 | 335 | |
NarendraSingh | 11:77e595130230 | 336 | baud_rate_command[0] = 0x2C; // Address of the BW RATE register |
NarendraSingh | 11:77e595130230 | 337 | baud_rate_command[1] = 0x0D; // Increased the data rate to 800Hz, default is 0x0A indicating 100Hz |
NarendraSingh | 11:77e595130230 | 338 | |
NarendraSingh | 11:77e595130230 | 339 | data_format_command[0] = 0x31; // Data format register address |
NarendraSingh | 11:77e595130230 | 340 | data_format_command[1] = 0x04; // Making the acceleration data as left justified |
NarendraSingh | 11:77e595130230 | 341 | |
NarendraSingh | 11:77e595130230 | 342 | measure_bit_on_command[0] = 0x2D; // Post the Register address of the slave (Have to write this into slave) |
NarendraSingh | 11:77e595130230 | 343 | measure_bit_on_command[1] = 0x08; // Turn ON the Measure Bit |
NarendraSingh | 11:77e595130230 | 344 | |
NarendraSingh | 11:77e595130230 | 345 | i2c.write(slave_address_acc, threshold_offset_command, 5); |
NarendraSingh | 11:77e595130230 | 346 | i2c.write(slave_address_acc, act_inact_time_config_command, 8); |
NarendraSingh | 11:77e595130230 | 347 | i2c.write(slave_address_acc, interrupt_enable_command, 3); |
NarendraSingh | 11:77e595130230 | 348 | i2c.write(slave_address_acc, tap_axis_enable_command, 2); |
NarendraSingh | 11:77e595130230 | 349 | i2c.write(slave_address_acc, baud_rate_command, 2); |
NarendraSingh | 11:77e595130230 | 350 | i2c.write(slave_address_acc, data_format_command, 2); |
NarendraSingh | 11:77e595130230 | 351 | i2c.write(slave_address_acc, measure_bit_on_command, 2); |
NarendraSingh | 11:77e595130230 | 352 | |
NarendraSingh | 11:77e595130230 | 353 | DEBUG_UART1.printf(" ACCELEROMETER DATA LOG \r\n\r\n"); |
NarendraSingh | 11:77e595130230 | 354 | |
NarendraSingh | 11:77e595130230 | 355 | while (1) |
NarendraSingh | 11:77e595130230 | 356 | { |
NarendraSingh | 11:77e595130230 | 357 | if(Motion_Detect_Status) |
NarendraSingh | 11:77e595130230 | 358 | { |
NarendraSingh | 11:77e595130230 | 359 | // The following statement disables all interrupts since no other interrupts must disturb at this point |
NarendraSingh | 11:77e595130230 | 360 | i2c.write(slave_address_acc, intr_source_address, 1); |
NarendraSingh | 11:77e595130230 | 361 | i2c.read(slave_address_acc, interrupt_source, 1); // Reads the Interrupt Source Register |
NarendraSingh | 11:77e595130230 | 362 | char_to_int(interrupt_source[0]); // Coverts intr_source(char) to int & stores in intr_source_duplicate |
NarendraSingh | 11:77e595130230 | 363 | |
NarendraSingh | 11:77e595130230 | 364 | Motion_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 365 | Motion_Type_Detected = MOTION_TYPE_UNKNOWN; |
NarendraSingh | 11:77e595130230 | 366 | DEBUG_UART1.printf("Motion detected"); |
NarendraSingh | 11:77e595130230 | 367 | DEBUG_UART1.putc(interrupt_source_duplicate); |
NarendraSingh | 11:77e595130230 | 368 | // USE THE FOLLOWING BLOCK TO READ THE DATA IN X-AXIS, Y-AXIS & Z-AXIS |
NarendraSingh | 11:77e595130230 | 369 | //--------------------------------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 370 | if(interrupt_source_duplicate & DOUBLE_TAP_INTERRUPT) // Check whether it is double tap (sudden jerk) interrupt |
NarendraSingh | 11:77e595130230 | 371 | { |
NarendraSingh | 11:77e595130230 | 372 | // ATTACH YOUR SUDDEN JERK EXECUTION ROUTINE HERE |
NarendraSingh | 11:77e595130230 | 373 | i2c.write(slave_address_acc, inactivity_interrupt_disable_command, 2); // Disables Inactivity interrupt & enables Double Tap & Activity interrupt |
NarendraSingh | 11:77e595130230 | 374 | DEBUG_UART1.printf("ENTERED SUDDEN JERK CONDITION \r\n\r\n"); // To be removed |
NarendraSingh | 11:77e595130230 | 375 | interrupt_source_duplicate = 0x00; // Sudden jerk also comes with activity interrupt triggered, hence this statements makes the activity check fail |
NarendraSingh | 11:77e595130230 | 376 | Motion_Type_Detected = MOTION_TYPE_TAP; |
NarendraSingh | 11:77e595130230 | 377 | Accelerometer_Interrupt_Generated = TRUE; |
NarendraSingh | 11:77e595130230 | 378 | } |
NarendraSingh | 11:77e595130230 | 379 | //--------------------------------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 380 | / VERIFY WHETHER THE INTERRUPT IS BECAUSE OF ACTIVITY |
NarendraSingh | 11:77e595130230 | 381 | |
NarendraSingh | 11:77e595130230 | 382 | if(interrupt_source_duplicate & ACTIVITY_INTERRUPT) |
NarendraSingh | 11:77e595130230 | 383 | { |
NarendraSingh | 11:77e595130230 | 384 | i2c.write(slave_address_acc, activity_interrupt_disable_command, 2); // Disables Activity interrupt & enables Inactivity interrupt |
NarendraSingh | 11:77e595130230 | 385 | get_vehicle_speed(); |
NarendraSingh | 11:77e595130230 | 386 | previous_speed = vehicle_speed; |
NarendraSingh | 11:77e595130230 | 387 | wait(5); |
NarendraSingh | 11:77e595130230 | 388 | get_vehicle_speed(); |
NarendraSingh | 11:77e595130230 | 389 | current_speed = vehicle_speed; |
NarendraSingh | 11:77e595130230 | 390 | if((current_speed > previous_speed) && (current_speed > speed_threshold)) |
NarendraSingh | 11:77e595130230 | 391 | { |
NarendraSingh | 11:77e595130230 | 392 | Motion_Type_Detected = MOTION_TYPE_STOP_TO_START; |
NarendraSingh | 11:77e595130230 | 393 | Accelerometer_Interrupt_Generated = TRUE; |
NarendraSingh | 11:77e595130230 | 394 | DEBUG_UART1.printf("VEHICLE HAS STARTED (STOP TO START) \r\n"); |
NarendraSingh | 11:77e595130230 | 395 | // ATTACH UR STOP TO START ROUTINE HERE |
NarendraSingh | 11:77e595130230 | 396 | } |
NarendraSingh | 11:77e595130230 | 397 | } |
NarendraSingh | 11:77e595130230 | 398 | //--------------------------------------------------------------------------------------------------------------------------------- |
NarendraSingh | 11:77e595130230 | 399 | |
NarendraSingh | 11:77e595130230 | 400 | // VERIFY WHETHER THE INTERRUPT IS BECAUSE OF INACTIVITY |
NarendraSingh | 11:77e595130230 | 401 | if(interrupt_source_duplicate & INACTIVITY_INTERRUPT) |
NarendraSingh | 11:77e595130230 | 402 | { |
NarendraSingh | 11:77e595130230 | 403 | i2c.write(slave_address_acc, inactivity_interrupt_disable_command, 2); // Disables Inactivity interrupt & enables Activity interrupt |
NarendraSingh | 11:77e595130230 | 404 | get_vehicle_speed(); |
NarendraSingh | 11:77e595130230 | 405 | if(vehicle_speed == 0) |
NarendraSingh | 11:77e595130230 | 406 | { |
NarendraSingh | 11:77e595130230 | 407 | Motion_Type_Detected = MOTION_TYPE_START_TO_STOP; |
NarendraSingh | 11:77e595130230 | 408 | Accelerometer_Interrupt_Generated = TRUE; |
NarendraSingh | 11:77e595130230 | 409 | // ATTACH YOUR START TO STOP PROCESS OVERFLOW HERE |
NarendraSingh | 11:77e595130230 | 410 | DEBUG_UART1.printf("VEHICLE HAS STOPPED (START TO STOP) \r\n"); |
NarendraSingh | 11:77e595130230 | 411 | } |
NarendraSingh | 11:77e595130230 | 412 | } |
NarendraSingh | 11:77e595130230 | 413 | wait(0.25); |
NarendraSingh | 11:77e595130230 | 414 | i2c.write(slave_address_acc, axis_data_start_address, 1); |
NarendraSingh | 11:77e595130230 | 415 | i2c.read(slave_address_acc, axis_data, 6); |
NarendraSingh | 11:77e595130230 | 416 | |
NarendraSingh | 11:77e595130230 | 417 | x_axis = axis_data[1]; // Puts MSB data into respective axes |
NarendraSingh | 11:77e595130230 | 418 | y_axis = axis_data[3]; |
NarendraSingh | 11:77e595130230 | 419 | z_axis = axis_data[5]; |
NarendraSingh | 11:77e595130230 | 420 | } |
NarendraSingh | 11:77e595130230 | 421 | Thread::wait(200); //wait for 100msec |
NarendraSingh | 11:77e595130230 | 422 | } |
NarendraSingh | 11:77e595130230 | 423 | } |
NarendraSingh | 11:77e595130230 | 424 | |
NarendraSingh | 11:77e595130230 | 425 | |
NarendraSingh | 11:77e595130230 | 426 | */ |