OBD Source Code -Section 1- Without Car / Section 2 - With car
Dependencies: mbed
main.cpp@2:49712259aa71, 2017-04-03 (annotated)
- Committer:
- bala0x07
- Date:
- Mon Apr 03 06:02:26 2017 +0000
- Revision:
- 2:49712259aa71
- Parent:
- 1:c23c05b36e33
This particular source code has two independent sections, comment any one of the following before proceeding
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bala0x07 | 0:e36d80703ed0 | 1 | |
bala0x07 | 0:e36d80703ed0 | 2 | /* **************************************************************************************************** |
bala0x07 | 0:e36d80703ed0 | 3 | |
bala0x07 | 0:e36d80703ed0 | 4 | OBD - ACCELEROMETER INTERFACE |
bala0x07 | 0:e36d80703ed0 | 5 | |
bala0x07 | 0:e36d80703ed0 | 6 | **************************************************************************************************** */ |
bala0x07 | 0:e36d80703ed0 | 7 | |
bala0x07 | 0:e36d80703ed0 | 8 | /* |
bala0x07 | 0:e36d80703ed0 | 9 | |
bala0x07 | 0:e36d80703ed0 | 10 | NOTE : |
bala0x07 | 0:e36d80703ed0 | 11 | This program aims at interfacing both the OBD scan tool and ADXL345 Accelerometer |
bala0x07 | 0:e36d80703ed0 | 12 | Testing the combined application of both in progress |
bala0x07 | 0:e36d80703ed0 | 13 | Code is subjected to modification |
bala0x07 | 0:e36d80703ed0 | 14 | |
bala0x07 | 0:e36d80703ed0 | 15 | Date : 25 - Feb - 2017 (Saturday) |
bala0x07 | 0:e36d80703ed0 | 16 | Author : BALA |
bala0x07 | 0:e36d80703ed0 | 17 | |
bala0x07 | 0:e36d80703ed0 | 18 | */ |
bala0x07 | 0:e36d80703ed0 | 19 | |
bala0x07 | 0:e36d80703ed0 | 20 | #include "mbed.h" |
bala0x07 | 0:e36d80703ed0 | 21 | #include "accelerometer.h" |
bala0x07 | 0:e36d80703ed0 | 22 | #include "obd_libraries.h" |
bala0x07 | 0:e36d80703ed0 | 23 | #include "common_definitions.h" |
bala0x07 | 0:e36d80703ed0 | 24 | |
bala0x07 | 0:e36d80703ed0 | 25 | //-------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 26 | |
bala0x07 | 2:49712259aa71 | 27 | I2C i2cm(PB_9, PB_8); |
bala0x07 | 0:e36d80703ed0 | 28 | Serial pcm(USBTX, USBRX); |
bala0x07 | 0:e36d80703ed0 | 29 | Serial OBD_UARTm(PA_0, PA_1); |
bala0x07 | 0:e36d80703ed0 | 30 | |
bala0x07 | 0:e36d80703ed0 | 31 | //DigitalOut led(LED1); |
bala0x07 | 0:e36d80703ed0 | 32 | |
bala0x07 | 0:e36d80703ed0 | 33 | //InterruptIn double_tap(PC_1); // Pin assignment may vary |
bala0x07 | 0:e36d80703ed0 | 34 | //InterruptIn activity_inactivity(PC_0); // Pin assignment may vary |
bala0x07 | 0:e36d80703ed0 | 35 | |
bala0x07 | 0:e36d80703ed0 | 36 | //-------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 37 | |
bala0x07 | 0:e36d80703ed0 | 38 | /* THE FOLLOWING DECLARATIONS ARE GLOBAL VARIABLES */ |
bala0x07 | 0:e36d80703ed0 | 39 | |
bala0x07 | 0:e36d80703ed0 | 40 | extern char current_speed, previous_speed; |
bala0x07 | 0:e36d80703ed0 | 41 | //extern char speed_threshold = 10; |
bala0x07 | 0:e36d80703ed0 | 42 | |
bala0x07 | 0:e36d80703ed0 | 43 | extern float car_battery_voltage; |
bala0x07 | 0:e36d80703ed0 | 44 | extern long vehicle_speed; |
bala0x07 | 0:e36d80703ed0 | 45 | |
bala0x07 | 0:e36d80703ed0 | 46 | extern char OBD_UART_RX_Buffer[50]; |
bala0x07 | 0:e36d80703ed0 | 47 | char OBD_RxBuffer_End_Pos = 0; |
bala0x07 | 0:e36d80703ed0 | 48 | char OBD_UART_RX_Size = 50; |
bala0x07 | 0:e36d80703ed0 | 49 | |
bala0x07 | 0:e36d80703ed0 | 50 | //extern const int slave_address_acc = 0xA6; |
bala0x07 | 0:e36d80703ed0 | 51 | //extern char interrupt_source[2]; |
bala0x07 | 0:e36d80703ed0 | 52 | //extern char intr_source_address[2] = {0x30, 0}; |
bala0x07 | 0:e36d80703ed0 | 53 | //extern char all_interrupt_clear_command[2] = {0x2E, 0x00}; |
bala0x07 | 0:e36d80703ed0 | 54 | //extern char all_interrupt_enable_command[2] = {0x2E, 0x18}; |
bala0x07 | 0:e36d80703ed0 | 55 | //extern char activity_interrupt_disable_command[2] = {0x2E, 0x28}; |
bala0x07 | 0:e36d80703ed0 | 56 | //extern char inactivity_interrupt_disable_command[2] = {0x2E, 0x30}; |
bala0x07 | 0:e36d80703ed0 | 57 | //extern unsigned int interrupt_source_duplicate; |
bala0x07 | 0:e36d80703ed0 | 58 | |
bala0x07 | 0:e36d80703ed0 | 59 | extern char obd_reset_cmd[]; |
bala0x07 | 0:e36d80703ed0 | 60 | |
bala0x07 | 1:c23c05b36e33 | 61 | extern char no_of_stored_dtc; |
bala0x07 | 1:c23c05b36e33 | 62 | |
bala0x07 | 0:e36d80703ed0 | 63 | extern void OBD_onDataRx(); |
bala0x07 | 0:e36d80703ed0 | 64 | extern void interrupt_sudden_jerk(); |
bala0x07 | 0:e36d80703ed0 | 65 | extern void interrupt_activity_inactivity(); |
bala0x07 | 0:e36d80703ed0 | 66 | |
bala0x07 | 0:e36d80703ed0 | 67 | |
bala0x07 | 0:e36d80703ed0 | 68 | //********************************************************************************************************* |
bala0x07 | 0:e36d80703ed0 | 69 | |
bala0x07 | 0:e36d80703ed0 | 70 | |
bala0x07 | 0:e36d80703ed0 | 71 | /* THE FOLLOWING CODE BLOCK IS THE INTERRUPT SERVICE ROTUINE FOR OBD DATA RECEIVE */ |
bala0x07 | 0:e36d80703ed0 | 72 | |
bala0x07 | 0:e36d80703ed0 | 73 | //void OBD_onDataRx() |
bala0x07 | 0:e36d80703ed0 | 74 | //{ |
bala0x07 | 0:e36d80703ed0 | 75 | // pcm.printf("\r\n ENTERED \r\n"); |
bala0x07 | 0:e36d80703ed0 | 76 | // if(OBD_UARTm.readable()) { |
bala0x07 | 0:e36d80703ed0 | 77 | // |
bala0x07 | 0:e36d80703ed0 | 78 | // pcm.putc(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos++] = OBD_UARTm.getc()); |
bala0x07 | 0:e36d80703ed0 | 79 | // /* |
bala0x07 | 0:e36d80703ed0 | 80 | // if(OBD_RxBuffer_End_Pos >= OBD_UART_RX_Size) { |
bala0x07 | 0:e36d80703ed0 | 81 | // // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
bala0x07 | 0:e36d80703ed0 | 82 | // // For now just throw everything away. |
bala0x07 | 0:e36d80703ed0 | 83 | // OBD_RxBuffer_End_Pos = 0; |
bala0x07 | 0:e36d80703ed0 | 84 | // } |
bala0x07 | 0:e36d80703ed0 | 85 | // */ |
bala0x07 | 0:e36d80703ed0 | 86 | // } |
bala0x07 | 0:e36d80703ed0 | 87 | //} |
bala0x07 | 0:e36d80703ed0 | 88 | |
bala0x07 | 0:e36d80703ed0 | 89 | //********************************************************************************************************* |
bala0x07 | 0:e36d80703ed0 | 90 | |
bala0x07 | 0:e36d80703ed0 | 91 | |
bala0x07 | 0:e36d80703ed0 | 92 | /* THE FOLLOWING SET OF CODE BLOCKS ARE INTERRUPT SERVICE ROTUINES OF ACCELEROMETER */ |
bala0x07 | 0:e36d80703ed0 | 93 | |
bala0x07 | 0:e36d80703ed0 | 94 | |
bala0x07 | 0:e36d80703ed0 | 95 | //********************************************************************************************************* |
bala0x07 | 0:e36d80703ed0 | 96 | |
bala0x07 | 0:e36d80703ed0 | 97 | // THE FOLLOWING CODE BLOCK IS THE ISR FOR SUDDEN JERK CONDITION |
bala0x07 | 0:e36d80703ed0 | 98 | |
bala0x07 | 0:e36d80703ed0 | 99 | //void interrupt_sudden_jerk() |
bala0x07 | 0:e36d80703ed0 | 100 | //{ |
bala0x07 | 0:e36d80703ed0 | 101 | // char count; |
bala0x07 | 0:e36d80703ed0 | 102 | // |
bala0x07 | 0:e36d80703ed0 | 103 | // i2cm.write(slave_address_acc, all_interrupt_clear_command, 2); |
bala0x07 | 0:e36d80703ed0 | 104 | // pcm.printf("~~~ ENTERED SUDDEN JERK CONDITION ~~~\r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 105 | ///* |
bala0x07 | 0:e36d80703ed0 | 106 | // for(count = 0; count < 2; count++) |
bala0x07 | 0:e36d80703ed0 | 107 | // { |
bala0x07 | 0:e36d80703ed0 | 108 | // led = 1; |
bala0x07 | 0:e36d80703ed0 | 109 | // wait(2); |
bala0x07 | 0:e36d80703ed0 | 110 | // led = 0; |
bala0x07 | 0:e36d80703ed0 | 111 | // wait(1); |
bala0x07 | 0:e36d80703ed0 | 112 | // } |
bala0x07 | 0:e36d80703ed0 | 113 | //*/ |
bala0x07 | 0:e36d80703ed0 | 114 | // i2cm.write(slave_address_acc, all_interrupt_enable_command, 2); |
bala0x07 | 0:e36d80703ed0 | 115 | // |
bala0x07 | 0:e36d80703ed0 | 116 | //} |
bala0x07 | 0:e36d80703ed0 | 117 | |
bala0x07 | 0:e36d80703ed0 | 118 | //********************************************************************************************************* |
bala0x07 | 0:e36d80703ed0 | 119 | // |
bala0x07 | 0:e36d80703ed0 | 120 | //// THE FOLLWOING CODE BLOCK IS THE MULITIPLEXED ISR FOR BOTH ACTIVITY & INACTIVITY INTERRUPT |
bala0x07 | 0:e36d80703ed0 | 121 | // |
bala0x07 | 0:e36d80703ed0 | 122 | //void interrupt_activity_inactivity() |
bala0x07 | 0:e36d80703ed0 | 123 | //{ |
bala0x07 | 0:e36d80703ed0 | 124 | // char count; |
bala0x07 | 0:e36d80703ed0 | 125 | // |
bala0x07 | 0:e36d80703ed0 | 126 | // // The following statement disables all interrupts since no other interrupts must disturb at this point |
bala0x07 | 0:e36d80703ed0 | 127 | // |
bala0x07 | 0:e36d80703ed0 | 128 | // i2cm.write(slave_address_acc, all_interrupt_clear_command, 2); |
bala0x07 | 0:e36d80703ed0 | 129 | // |
bala0x07 | 0:e36d80703ed0 | 130 | // i2cm.write(slave_address_acc, intr_source_address, 1); |
bala0x07 | 0:e36d80703ed0 | 131 | // i2cm.read(slave_address_acc, interrupt_source, 1); |
bala0x07 | 0:e36d80703ed0 | 132 | // |
bala0x07 | 0:e36d80703ed0 | 133 | // char_to_int(interrupt_source[0]); // Coverts intr_source(char) to int & stores in intr_source_d |
bala0x07 | 0:e36d80703ed0 | 134 | // |
bala0x07 | 0:e36d80703ed0 | 135 | // pcm.printf("INT Source = "); |
bala0x07 | 0:e36d80703ed0 | 136 | // print_data_bits((interrupt_source_duplicate)); |
bala0x07 | 0:e36d80703ed0 | 137 | // |
bala0x07 | 0:e36d80703ed0 | 138 | ////-------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 139 | // |
bala0x07 | 0:e36d80703ed0 | 140 | // /* VERIFY WHETHER THE INTERRUPT IS BECAUSE OF ACTIVITY */ |
bala0x07 | 0:e36d80703ed0 | 141 | // |
bala0x07 | 0:e36d80703ed0 | 142 | // //if((((int)intr_source) & 0x10) == 0x10) |
bala0x07 | 0:e36d80703ed0 | 143 | // if(interrupt_source_duplicate & 0x10) |
bala0x07 | 0:e36d80703ed0 | 144 | // { |
bala0x07 | 0:e36d80703ed0 | 145 | // /* THE FOLLOWING BLOCK IS USED JUST FOR VERIFICATION PURPOSE AND ARE NOT MANDATORY */ |
bala0x07 | 0:e36d80703ed0 | 146 | ///* |
bala0x07 | 0:e36d80703ed0 | 147 | // pcm.printf("ENTERED ACTIVITY CONDITION\r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 148 | // for(count = 0; count < 10; count++) |
bala0x07 | 0:e36d80703ed0 | 149 | // { |
bala0x07 | 0:e36d80703ed0 | 150 | // led = !led; |
bala0x07 | 0:e36d80703ed0 | 151 | // wait(0.05); |
bala0x07 | 0:e36d80703ed0 | 152 | // } |
bala0x07 | 0:e36d80703ed0 | 153 | //*/ |
bala0x07 | 0:e36d80703ed0 | 154 | // fetch_vehicle_speed(); |
bala0x07 | 0:e36d80703ed0 | 155 | // previous_speed = vehicle_speed; |
bala0x07 | 0:e36d80703ed0 | 156 | // wait(5); |
bala0x07 | 0:e36d80703ed0 | 157 | // fetch_vehicle_speed(); |
bala0x07 | 0:e36d80703ed0 | 158 | // current_speed = vehicle_speed; |
bala0x07 | 0:e36d80703ed0 | 159 | // |
bala0x07 | 0:e36d80703ed0 | 160 | // //if((current_speed > previous_speed) && (current_speed > speed_threshold)) // Decision making regarding vehicle's current state |
bala0x07 | 0:e36d80703ed0 | 161 | // if(current_speed == 79) |
bala0x07 | 0:e36d80703ed0 | 162 | // { |
bala0x07 | 0:e36d80703ed0 | 163 | // i2cm.write(slave_address_acc, activity_interrupt_disable_command, 2); // Disables Activity interrupt & enables Inactivity interrupt |
bala0x07 | 0:e36d80703ed0 | 164 | // pcm.printf("\r\n>>> VEHICLE HAS STARTED FROM STOP <<<"); |
bala0x07 | 0:e36d80703ed0 | 165 | // } |
bala0x07 | 0:e36d80703ed0 | 166 | // } |
bala0x07 | 0:e36d80703ed0 | 167 | // |
bala0x07 | 0:e36d80703ed0 | 168 | ////-------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 169 | // |
bala0x07 | 0:e36d80703ed0 | 170 | // /* VERIFY WHETHER THE INTERRUPT IS BECAUSE OF INACTIVITY */ |
bala0x07 | 0:e36d80703ed0 | 171 | // |
bala0x07 | 0:e36d80703ed0 | 172 | // //if((((int)intr_source) & 0x08) == 0x08) // Verify whether it is inactivity interrupt |
bala0x07 | 0:e36d80703ed0 | 173 | // if(interrupt_source_duplicate & 0x08) |
bala0x07 | 0:e36d80703ed0 | 174 | // { |
bala0x07 | 0:e36d80703ed0 | 175 | // /* THE FOLLOWING BLOCK IS USED JUST FOR VERIFICATION PURPOSE AND ARE NOT MANDATORY */ |
bala0x07 | 0:e36d80703ed0 | 176 | ///* |
bala0x07 | 0:e36d80703ed0 | 177 | // pcm.printf("ENTERED INACTIVITY CONDITION \r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 178 | // for(count = 0; count < 10; count++) |
bala0x07 | 0:e36d80703ed0 | 179 | // { |
bala0x07 | 0:e36d80703ed0 | 180 | // led = !led; |
bala0x07 | 0:e36d80703ed0 | 181 | // wait(0.2); |
bala0x07 | 0:e36d80703ed0 | 182 | // } |
bala0x07 | 0:e36d80703ed0 | 183 | //*/ |
bala0x07 | 0:e36d80703ed0 | 184 | // fetch_vehicle_speed(); |
bala0x07 | 0:e36d80703ed0 | 185 | // |
bala0x07 | 0:e36d80703ed0 | 186 | // if(vehicle_speed == 0) // Decision making regarding vehicle's current state |
bala0x07 | 0:e36d80703ed0 | 187 | // { |
bala0x07 | 0:e36d80703ed0 | 188 | // i2cm.write(slave_address_acc, inactivity_interrupt_disable_command, 2); // Disables Inactivity interrupt & enables Activity interrupt |
bala0x07 | 0:e36d80703ed0 | 189 | // pcm.printf("\r\n>>> VEHICLE HAS STOPPED FROM START <<<"); |
bala0x07 | 0:e36d80703ed0 | 190 | // } |
bala0x07 | 0:e36d80703ed0 | 191 | // } |
bala0x07 | 0:e36d80703ed0 | 192 | //} |
bala0x07 | 0:e36d80703ed0 | 193 | |
bala0x07 | 0:e36d80703ed0 | 194 | |
bala0x07 | 0:e36d80703ed0 | 195 | //********************************************************************************************************* |
bala0x07 | 0:e36d80703ed0 | 196 | |
bala0x07 | 0:e36d80703ed0 | 197 | int main(void) |
bala0x07 | 0:e36d80703ed0 | 198 | { |
bala0x07 | 0:e36d80703ed0 | 199 | //OBD_UARTm.attach(&OBD_onDataRx); |
bala0x07 | 0:e36d80703ed0 | 200 | //activity_inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge |
bala0x07 | 0:e36d80703ed0 | 201 | //double_tap.rise(interrupt_sudden_jerk); |
bala0x07 | 0:e36d80703ed0 | 202 | |
bala0x07 | 2:49712259aa71 | 203 | pcm.baud(9600); |
bala0x07 | 2:49712259aa71 | 204 | OBD_UARTm.baud(9600); |
bala0x07 | 0:e36d80703ed0 | 205 | |
bala0x07 | 0:e36d80703ed0 | 206 | |
bala0x07 | 0:e36d80703ed0 | 207 | pcm.printf("\r\n\r\n\t\t>>>>>------>> OBD - ACCELEROMETER INTERFACE <<------<<<<<\r\n\r\n"); |
bala0x07 | 2:49712259aa71 | 208 | |
bala0x07 | 0:e36d80703ed0 | 209 | initialize_obd(); // OBD- scan tool initialization would be done here |
bala0x07 | 0:e36d80703ed0 | 210 | |
bala0x07 | 0:e36d80703ed0 | 211 | fetch_battery_voltage(); // OBD - section |
bala0x07 | 0:e36d80703ed0 | 212 | |
bala0x07 | 2:49712259aa71 | 213 | fetch_vin_number(); |
bala0x07 | 2:49712259aa71 | 214 | |
bala0x07 | 0:e36d80703ed0 | 215 | fetch_vehicle_speed(); |
bala0x07 | 0:e36d80703ed0 | 216 | |
bala0x07 | 2:49712259aa71 | 217 | fetch_engine_rpm(); |
bala0x07 | 2:49712259aa71 | 218 | |
bala0x07 | 2:49712259aa71 | 219 | //rpm_padding_for_comm_test(); |
bala0x07 | 2:49712259aa71 | 220 | |
bala0x07 | 2:49712259aa71 | 221 | |
bala0x07 | 1:c23c05b36e33 | 222 | check_for_MIL(); |
bala0x07 | 1:c23c05b36e33 | 223 | |
bala0x07 | 2:49712259aa71 | 224 | check_for_dtc(); |
bala0x07 | 2:49712259aa71 | 225 | |
bala0x07 | 2:49712259aa71 | 226 | |
bala0x07 | 2:49712259aa71 | 227 | |
bala0x07 | 2:49712259aa71 | 228 | |
bala0x07 | 2:49712259aa71 | 229 | |
bala0x07 | 2:49712259aa71 | 230 | //if(no_of_stored_dtc > 0) |
bala0x07 | 2:49712259aa71 | 231 | //check_for_dtc(); |
bala0x07 | 2:49712259aa71 | 232 | //check_for_dtc(); |
bala0x07 | 2:49712259aa71 | 233 | |
bala0x07 | 2:49712259aa71 | 234 | |
bala0x07 | 0:e36d80703ed0 | 235 | //initialize_accelerometer(); // All Accerometer configurations will be done here |
bala0x07 | 0:e36d80703ed0 | 236 | |
bala0x07 | 0:e36d80703ed0 | 237 | //while(1); // Wait forever, let the ISR do the rest |
bala0x07 | 0:e36d80703ed0 | 238 | |
bala0x07 | 0:e36d80703ed0 | 239 | |
bala0x07 | 0:e36d80703ed0 | 240 | return 0; |
bala0x07 | 0:e36d80703ed0 | 241 | |
bala0x07 | 0:e36d80703ed0 | 242 | } |
bala0x07 | 0:e36d80703ed0 | 243 | |
bala0x07 | 0:e36d80703ed0 | 244 | //********************************************************************************************************* |