blah

Dependencies:   mbed

Fork of adxl335_mbed_serial by Sumit Pandey

Committer:
lz307
Date:
Thu Dec 12 16:01:16 2013 +0000
Revision:
4:18a440b7b1a5
Parent:
3:b5f6dccb2c08
Tunable parameters now are at the start of the file.

Who changed what in which revision?

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