Liyou Zhou
/
wheel_speed_sensor_board
blah
Fork of adxl335_mbed_serial by
main.cpp@1:c4129110f970, 2013-12-12 (annotated)
- Committer:
- lz307
- Date:
- Thu Dec 12 15:23:29 2013 +0000
- Revision:
- 1:c4129110f970
- Parent:
- 0:6754c50b1d75
- Child:
- 2:0715aaf81851
Wheel speed sensor board initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sumitpuneet | 0:6754c50b1d75 | 1 | #include "mbed.h" |
lz307 | 1:c4129110f970 | 2 | |
lz307 | 1:c4129110f970 | 3 | int debug = 1; |
lz307 | 1:c4129110f970 | 4 | |
lz307 | 1:c4129110f970 | 5 | Serial pc(USBTX, USBRX); // tx, rx |
lz307 | 1:c4129110f970 | 6 | |
lz307 | 1:c4129110f970 | 7 | AnalogIn spare(p20); // spare pin for extra sensors if needed |
lz307 | 1:c4129110f970 | 8 | AnalogIn inputx(p19); // input pins for x,y,z axis respectively. |
lz307 | 1:c4129110f970 | 9 | AnalogIn inputy(p18); |
lz307 | 1:c4129110f970 | 10 | AnalogIn inputz(p17); |
lz307 | 1:c4129110f970 | 11 | DigitalIn selftest(p21); // self test pin for the accelerometer |
lz307 | 1:c4129110f970 | 12 | DigitalOut redundant_1(p15); // pull adjacent pins low to reduce noise |
lz307 | 1:c4129110f970 | 13 | DigitalOut redundant_2(p16); |
lz307 | 1:c4129110f970 | 14 | Ticker ticker; |
lz307 | 1:c4129110f970 | 15 | Ticker time_interrupt; |
lz307 | 1:c4129110f970 | 16 | float time_interrupt_interval = 2; // time in seconds between output of rpm |
lz307 | 1:c4129110f970 | 17 | CAN can(p30, p29); // rx tx |
lz307 | 1:c4129110f970 | 18 | int can_id = 1337; |
lz307 | 1:c4129110f970 | 19 | |
lz307 | 1:c4129110f970 | 20 | int no_of_spokes = 5; // 5 readings per rev |
lz307 | 1:c4129110f970 | 21 | int count_delay_time = 10; // minimum interval between spikes in ms |
lz307 | 1:c4129110f970 | 22 | InterruptIn front_left(p5); |
lz307 | 1:c4129110f970 | 23 | InterruptIn front_right(p6); |
lz307 | 1:c4129110f970 | 24 | InterruptIn back_left(p7); |
lz307 | 1:c4129110f970 | 25 | Timer t_fl, t_fr, t_bl; |
lz307 | 1:c4129110f970 | 26 | volatile int count_fl, count_fr, count_bl; |
lz307 | 1:c4129110f970 | 27 | |
lz307 | 1:c4129110f970 | 28 | typedef union { |
lz307 | 1:c4129110f970 | 29 | char can_string[8]; |
lz307 | 1:c4129110f970 | 30 | float fl_data[2]; |
lz307 | 1:c4129110f970 | 31 | } can_msg_t; |
lz307 | 1:c4129110f970 | 32 | |
lz307 | 1:c4129110f970 | 33 | |
lz307 | 1:c4129110f970 | 34 | void count_rev_fl() |
lz307 | 1:c4129110f970 | 35 | { |
lz307 | 1:c4129110f970 | 36 | if(debug) pc.printf("yo! count %d\n", count_fl); |
lz307 | 1:c4129110f970 | 37 | |
lz307 | 1:c4129110f970 | 38 | static int last_count_time=0; |
lz307 | 1:c4129110f970 | 39 | int crt_time = t_fl.read_ms(); |
lz307 | 1:c4129110f970 | 40 | if ( last_count_time > crt_time ) last_count_time = 0; |
lz307 | 1:c4129110f970 | 41 | if ( crt_time - last_count_time < count_delay_time ) return; |
lz307 | 1:c4129110f970 | 42 | last_count_time = t_fl.read_ms(); |
lz307 | 1:c4129110f970 | 43 | count_fl++; |
lz307 | 1:c4129110f970 | 44 | } |
lz307 | 1:c4129110f970 | 45 | |
lz307 | 1:c4129110f970 | 46 | |
lz307 | 1:c4129110f970 | 47 | void count_rev_fr() |
lz307 | 1:c4129110f970 | 48 | { |
lz307 | 1:c4129110f970 | 49 | static int last_count_time=0; |
lz307 | 1:c4129110f970 | 50 | int crt_time = t_fl.read_ms(); |
lz307 | 1:c4129110f970 | 51 | if ( last_count_time > crt_time ) last_count_time = 0; |
lz307 | 1:c4129110f970 | 52 | if ( crt_time - last_count_time < count_delay_time ) return; |
lz307 | 1:c4129110f970 | 53 | last_count_time = t_fl.read_ms(); |
lz307 | 1:c4129110f970 | 54 | count_fr++; |
lz307 | 1:c4129110f970 | 55 | } |
lz307 | 1:c4129110f970 | 56 | |
lz307 | 1:c4129110f970 | 57 | |
lz307 | 1:c4129110f970 | 58 | void count_rev_bl() |
sumitpuneet | 0:6754c50b1d75 | 59 | { |
lz307 | 1:c4129110f970 | 60 | static int last_count_time=0; |
lz307 | 1:c4129110f970 | 61 | int crt_time = t_fl.read_ms(); |
lz307 | 1:c4129110f970 | 62 | if ( last_count_time > crt_time ) last_count_time = 0; |
lz307 | 1:c4129110f970 | 63 | if ( crt_time - last_count_time < count_delay_time ) return; |
lz307 | 1:c4129110f970 | 64 | last_count_time = t_fl.read_ms(); |
lz307 | 1:c4129110f970 | 65 | count_bl++; |
lz307 | 1:c4129110f970 | 66 | } |
lz307 | 1:c4129110f970 | 67 | |
lz307 | 1:c4129110f970 | 68 | |
lz307 | 1:c4129110f970 | 69 | void time_interrupt_call() |
lz307 | 1:c4129110f970 | 70 | { |
lz307 | 1:c4129110f970 | 71 | float rpm_fl = (float)count_fl/(float)no_of_spokes/time_interrupt_interval; |
lz307 | 1:c4129110f970 | 72 | float rpm_fr = (float)count_fr/(float)no_of_spokes/time_interrupt_interval; |
lz307 | 1:c4129110f970 | 73 | float rpm_bl = (float)count_bl/(float)no_of_spokes/time_interrupt_interval; |
lz307 | 1:c4129110f970 | 74 | count_fl = 0; |
lz307 | 1:c4129110f970 | 75 | count_fr = 0; |
lz307 | 1:c4129110f970 | 76 | count_bl = 0; |
lz307 | 1:c4129110f970 | 77 | |
lz307 | 1:c4129110f970 | 78 | if(debug) { |
lz307 | 1:c4129110f970 | 79 | pc.printf("rpm: fl: %f fr: %f bl: %f\n", rpm_fl, rpm_fr, rpm_bl); |
lz307 | 1:c4129110f970 | 80 | } else { |
lz307 | 1:c4129110f970 | 81 | can_msg_t can_msg_1; |
lz307 | 1:c4129110f970 | 82 | can_msg_t can_msg_2; |
lz307 | 1:c4129110f970 | 83 | can_msg_1.fl_data[0] = rpm_fl; |
lz307 | 1:c4129110f970 | 84 | can_msg_1.fl_data[1] = rpm_fr; |
lz307 | 1:c4129110f970 | 85 | can_msg_2.fl_data[0] = rpm_bl; |
lz307 | 1:c4129110f970 | 86 | can_msg_2.fl_data[1] = 0; |
lz307 | 1:c4129110f970 | 87 | can.write( CANMessage(can_id, can_msg_1.can_string, 8) ); |
lz307 | 1:c4129110f970 | 88 | can.write( CANMessage(can_id, can_msg_2.can_string, 8) ); |
lz307 | 1:c4129110f970 | 89 | } |
sumitpuneet | 0:6754c50b1d75 | 90 | } |
lz307 | 1:c4129110f970 | 91 | |
lz307 | 1:c4129110f970 | 92 | |
lz307 | 1:c4129110f970 | 93 | void read_accelerometer_and_send() |
lz307 | 1:c4129110f970 | 94 | { |
lz307 | 1:c4129110f970 | 95 | float offset_x=0,offset_y=0,offset_z=0; // variables offsets |
lz307 | 1:c4129110f970 | 96 | float scale_factor = 100; // variable scale factor |
lz307 | 1:c4129110f970 | 97 | float x=0,y=0,z=0; // variables for x,y,z axes |
lz307 | 1:c4129110f970 | 98 | x=(inputx+offset_x)*scale_factor; |
lz307 | 1:c4129110f970 | 99 | y=(inputy+offset_y)*scale_factor; |
lz307 | 1:c4129110f970 | 100 | z=(inputz+offset_z)*scale_factor; |
lz307 | 1:c4129110f970 | 101 | |
lz307 | 1:c4129110f970 | 102 | if(debug) { |
lz307 | 1:c4129110f970 | 103 | pc.printf("x: %03d y: %03d z: %03d\n", (int)x,(int)y,(int)z); |
lz307 | 1:c4129110f970 | 104 | } else { |
lz307 | 1:c4129110f970 | 105 | can_msg_t can_msg_1; |
lz307 | 1:c4129110f970 | 106 | can_msg_t can_msg_2; |
lz307 | 1:c4129110f970 | 107 | can_msg_1.fl_data[0] = x; |
lz307 | 1:c4129110f970 | 108 | can_msg_1.fl_data[1] = y; |
lz307 | 1:c4129110f970 | 109 | can_msg_2.fl_data[0] = z; |
lz307 | 1:c4129110f970 | 110 | can_msg_2.fl_data[1] = 0; |
lz307 | 1:c4129110f970 | 111 | can.write( CANMessage(can_id, can_msg_1.can_string, 8) ); |
lz307 | 1:c4129110f970 | 112 | can.write( CANMessage(can_id, can_msg_2.can_string, 8) ); |
lz307 | 1:c4129110f970 | 113 | } |
lz307 | 1:c4129110f970 | 114 | } |
lz307 | 1:c4129110f970 | 115 | |
lz307 | 1:c4129110f970 | 116 | |
lz307 | 1:c4129110f970 | 117 | int main() |
lz307 | 1:c4129110f970 | 118 | { |
lz307 | 1:c4129110f970 | 119 | redundant_1 = 0; |
lz307 | 1:c4129110f970 | 120 | redundant_2 = 0; |
lz307 | 1:c4129110f970 | 121 | //ticker.attach(&read_accelerometer_and_send, 1); |
lz307 | 1:c4129110f970 | 122 | |
lz307 | 1:c4129110f970 | 123 | time_interrupt.attach(&time_interrupt_call, time_interrupt_interval); |
lz307 | 1:c4129110f970 | 124 | |
lz307 | 1:c4129110f970 | 125 | t_fl.reset(); |
lz307 | 1:c4129110f970 | 126 | t_fl.start(); |
lz307 | 1:c4129110f970 | 127 | t_fr.reset(); |
lz307 | 1:c4129110f970 | 128 | t_fr.start(); |
lz307 | 1:c4129110f970 | 129 | t_bl.reset(); |
lz307 | 1:c4129110f970 | 130 | t_bl.start(); |
lz307 | 1:c4129110f970 | 131 | front_left.rise(&count_rev_fl); |
lz307 | 1:c4129110f970 | 132 | front_right.rise(&count_rev_fr); |
lz307 | 1:c4129110f970 | 133 | back_left.rise(&count_rev_bl); |
lz307 | 1:c4129110f970 | 134 | if(debug) pc.printf("debugging\n"); |
lz307 | 1:c4129110f970 | 135 | |
lz307 | 1:c4129110f970 | 136 | while(1); |
lz307 | 1:c4129110f970 | 137 | } |