Abdelmalek BELLOULA
/
DataLogIMU-BARO
interface to chr_6dm and Baro bmp085
main.cpp@0:d07617f8ede9, 2011-10-17 (annotated)
- Committer:
- belloula
- Date:
- Mon Oct 17 19:43:06 2011 +0000
- Revision:
- 0:d07617f8ede9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
belloula | 0:d07617f8ede9 | 1 | #include "mbed.h" |
belloula | 0:d07617f8ede9 | 2 | #include "chr.h" |
belloula | 0:d07617f8ede9 | 3 | #include "SDFileSystem.h" |
belloula | 0:d07617f8ede9 | 4 | #include "BMP085.h" |
belloula | 0:d07617f8ede9 | 5 | #include "MSCFileSystem.h" |
belloula | 0:d07617f8ede9 | 6 | |
belloula | 0:d07617f8ede9 | 7 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs, name |
belloula | 0:d07617f8ede9 | 8 | BMP085 bmp085(p28, p27); |
belloula | 0:d07617f8ede9 | 9 | Serial imu(p13, p14); |
belloula | 0:d07617f8ede9 | 10 | Serial pc(USBTX, USBRX); |
belloula | 0:d07617f8ede9 | 11 | |
belloula | 0:d07617f8ede9 | 12 | DigitalOut myled1(LED1); |
belloula | 0:d07617f8ede9 | 13 | DigitalOut myled2(LED2); |
belloula | 0:d07617f8ede9 | 14 | DigitalOut myled3(LED3); |
belloula | 0:d07617f8ede9 | 15 | DigitalOut myled4(LED4); |
belloula | 0:d07617f8ede9 | 16 | |
belloula | 0:d07617f8ede9 | 17 | InterruptIn event_EOC(p23); |
belloula | 0:d07617f8ede9 | 18 | |
belloula | 0:d07617f8ede9 | 19 | //DigitalIn BPM085_EOC (p23); |
belloula | 0:d07617f8ede9 | 20 | DigitalOut activate(p22); |
belloula | 0:d07617f8ede9 | 21 | DigitalIn enable(p21); |
belloula | 0:d07617f8ede9 | 22 | |
belloula | 0:d07617f8ede9 | 23 | |
belloula | 0:d07617f8ede9 | 24 | MSCFileSystem msc("msc"); |
belloula | 0:d07617f8ede9 | 25 | |
belloula | 0:d07617f8ede9 | 26 | char data[MAX_BYTES]; |
belloula | 0:d07617f8ede9 | 27 | int _state = WAIT_s; |
belloula | 0:d07617f8ede9 | 28 | char c; |
belloula | 0:d07617f8ede9 | 29 | int d; |
belloula | 0:d07617f8ede9 | 30 | unsigned char pt; |
belloula | 0:d07617f8ede9 | 31 | unsigned int n; |
belloula | 0:d07617f8ede9 | 32 | int yaw; |
belloula | 0:d07617f8ede9 | 33 | int pitch; |
belloula | 0:d07617f8ede9 | 34 | int roll; |
belloula | 0:d07617f8ede9 | 35 | unsigned short int tag_imu; |
belloula | 0:d07617f8ede9 | 36 | |
belloula | 0:d07617f8ede9 | 37 | FILE *fp,*usbp; |
belloula | 0:d07617f8ede9 | 38 | Timer tt,tmesure; |
belloula | 0:d07617f8ede9 | 39 | Ticker periode1; |
belloula | 0:d07617f8ede9 | 40 | void sendPacket(unsigned char pt, unsigned int n,char data[]) |
belloula | 0:d07617f8ede9 | 41 | { |
belloula | 0:d07617f8ede9 | 42 | //unsigned int checksum; |
belloula | 0:d07617f8ede9 | 43 | int checksum = HEADER_CHECKSUM +pt+n; |
belloula | 0:d07617f8ede9 | 44 | if (imu.writeable()) |
belloula | 0:d07617f8ede9 | 45 | { |
belloula | 0:d07617f8ede9 | 46 | imu.printf("snp"); |
belloula | 0:d07617f8ede9 | 47 | imu.putc(pt); |
belloula | 0:d07617f8ede9 | 48 | imu.putc(n); |
belloula | 0:d07617f8ede9 | 49 | |
belloula | 0:d07617f8ede9 | 50 | for (int i=0; i < n; i++) |
belloula | 0:d07617f8ede9 | 51 | { |
belloula | 0:d07617f8ede9 | 52 | imu.putc(data[i]); |
belloula | 0:d07617f8ede9 | 53 | checksum += data[i]; |
belloula | 0:d07617f8ede9 | 54 | } |
belloula | 0:d07617f8ede9 | 55 | imu.putc((char) (checksum >> 8)); // MSByte |
belloula | 0:d07617f8ede9 | 56 | imu.putc((char) (checksum & 0x0FF)); // LSByte |
belloula | 0:d07617f8ede9 | 57 | } |
belloula | 0:d07617f8ede9 | 58 | |
belloula | 0:d07617f8ede9 | 59 | } |
belloula | 0:d07617f8ede9 | 60 | |
belloula | 0:d07617f8ede9 | 61 | |
belloula | 0:d07617f8ede9 | 62 | void Int_DataLogger () |
belloula | 0:d07617f8ede9 | 63 | { |
belloula | 0:d07617f8ede9 | 64 | myled1 = 1; |
belloula | 0:d07617f8ede9 | 65 | myled2 = 1; |
belloula | 0:d07617f8ede9 | 66 | myled3 = 1; |
belloula | 0:d07617f8ede9 | 67 | myled4 = 1; |
belloula | 0:d07617f8ede9 | 68 | wait_ms(300); |
belloula | 0:d07617f8ede9 | 69 | pc.baud(115200); |
belloula | 0:d07617f8ede9 | 70 | myled1 = 0; |
belloula | 0:d07617f8ede9 | 71 | imu.baud(115200); |
belloula | 0:d07617f8ede9 | 72 | |
belloula | 0:d07617f8ede9 | 73 | myled2 = 0; |
belloula | 0:d07617f8ede9 | 74 | pc.printf("Hello!\n CHR-6dm Test Program \r/"); |
belloula | 0:d07617f8ede9 | 75 | |
belloula | 0:d07617f8ede9 | 76 | //event_EOC.rise(&trigger); |
belloula | 0:d07617f8ede9 | 77 | //Enables "Broadcast Mode." the AHRS automatically transmits sensor data every Ts |
belloula | 0:d07617f8ede9 | 78 | |
belloula | 0:d07617f8ede9 | 79 | data[0]= 0xA4; //= 100Hz |
belloula | 0:d07617f8ede9 | 80 | sendPacket(SET_BROADCAST_MODE, 1,data ); |
belloula | 0:d07617f8ede9 | 81 | myled3 = 0; |
belloula | 0:d07617f8ede9 | 82 | wait_ms(100); |
belloula | 0:d07617f8ede9 | 83 | |
belloula | 0:d07617f8ede9 | 84 | //Specifies which channel data should be transmitted over the UART. |
belloula | 0:d07617f8ede9 | 85 | data[0]= 0xFF; //{yaw, pitch, roll, yaw_rate, pitch_rate, roll_rate,mag_x, mag_y} |
belloula | 0:d07617f8ede9 | 86 | data[1]= 0xFF; //{mag_z, gyro_x, gyro_y,gyro_z, accel_x, accel_y,accel_z,0 } |
belloula | 0:d07617f8ede9 | 87 | sendPacket(SET_ACTIVE_CHANNELS,2,data ); |
belloula | 0:d07617f8ede9 | 88 | myled3 = 1; |
belloula | 0:d07617f8ede9 | 89 | myled4 = 0; |
belloula | 0:d07617f8ede9 | 90 | wait_ms(100); |
belloula | 0:d07617f8ede9 | 91 | |
belloula | 0:d07617f8ede9 | 92 | |
belloula | 0:d07617f8ede9 | 93 | } |
belloula | 0:d07617f8ede9 | 94 | |
belloula | 0:d07617f8ede9 | 95 | void attimu() |
belloula | 0:d07617f8ede9 | 96 | { |
belloula | 0:d07617f8ede9 | 97 | if (imu.readable()) |
belloula | 0:d07617f8ede9 | 98 | { |
belloula | 0:d07617f8ede9 | 99 | c = imu.getc(); |
belloula | 0:d07617f8ede9 | 100 | switch (_state) |
belloula | 0:d07617f8ede9 | 101 | { |
belloula | 0:d07617f8ede9 | 102 | case WAIT_s : |
belloula | 0:d07617f8ede9 | 103 | if (c == 's') _state = WAIT_n; |
belloula | 0:d07617f8ede9 | 104 | break; |
belloula | 0:d07617f8ede9 | 105 | case WAIT_n : |
belloula | 0:d07617f8ede9 | 106 | _state = (c == 'n') ? WAIT_p : WAIT_s; |
belloula | 0:d07617f8ede9 | 107 | break; |
belloula | 0:d07617f8ede9 | 108 | case WAIT_p : |
belloula | 0:d07617f8ede9 | 109 | _state = (c == 'p') ? PT : WAIT_p; |
belloula | 0:d07617f8ede9 | 110 | break; |
belloula | 0:d07617f8ede9 | 111 | case PT : |
belloula | 0:d07617f8ede9 | 112 | pt = c; |
belloula | 0:d07617f8ede9 | 113 | _state = N; |
belloula | 0:d07617f8ede9 | 114 | break; |
belloula | 0:d07617f8ede9 | 115 | case N : |
belloula | 0:d07617f8ede9 | 116 | n = ((unsigned int) c) - 2; |
belloula | 0:d07617f8ede9 | 117 | d = 0; |
belloula | 0:d07617f8ede9 | 118 | _state = (n < MAX_BYTES) ? RX_PACKET : WAIT_s; |
belloula | 0:d07617f8ede9 | 119 | break; |
belloula | 0:d07617f8ede9 | 120 | case RX_PACKET : |
belloula | 0:d07617f8ede9 | 121 | data[d++] = c; |
belloula | 0:d07617f8ede9 | 122 | if (d >= n || d >= MAX_BYTES) _state = PROCESS_PACKET; |
belloula | 0:d07617f8ede9 | 123 | break; |
belloula | 0:d07617f8ede9 | 124 | case PROCESS_PACKET : |
belloula | 0:d07617f8ede9 | 125 | switch (pt) |
belloula | 0:d07617f8ede9 | 126 | { |
belloula | 0:d07617f8ede9 | 127 | // SENSOR_DATA |
belloula | 0:d07617f8ede9 | 128 | case PT_SENSOR_DATA : |
belloula | 0:d07617f8ede9 | 129 | tag_imu=1; |
belloula | 0:d07617f8ede9 | 130 | break; |
belloula | 0:d07617f8ede9 | 131 | case PT_COMMAND_COMPLETE : |
belloula | 0:d07617f8ede9 | 132 | pc.printf("Command Complete\n"); |
belloula | 0:d07617f8ede9 | 133 | break; |
belloula | 0:d07617f8ede9 | 134 | case PT_COMMAND_FAILED : |
belloula | 0:d07617f8ede9 | 135 | pc.printf("Command Failed\n"); |
belloula | 0:d07617f8ede9 | 136 | break; |
belloula | 0:d07617f8ede9 | 137 | case PT_BAD_CHECKSUM : |
belloula | 0:d07617f8ede9 | 138 | pc.printf("Bad Checksum\n"); |
belloula | 0:d07617f8ede9 | 139 | break; |
belloula | 0:d07617f8ede9 | 140 | case PT_BAD_DATA_LENGTH : |
belloula | 0:d07617f8ede9 | 141 | pc.printf("Bad Data Length\n"); |
belloula | 0:d07617f8ede9 | 142 | break; |
belloula | 0:d07617f8ede9 | 143 | case PT_UNRECOGNIZED_PACKET : |
belloula | 0:d07617f8ede9 | 144 | pc.printf("Unrecognized Packet\n"); |
belloula | 0:d07617f8ede9 | 145 | break; |
belloula | 0:d07617f8ede9 | 146 | case PT_BUFFER_OVERFLOW : |
belloula | 0:d07617f8ede9 | 147 | pc.printf("Buffer Overflow\n"); |
belloula | 0:d07617f8ede9 | 148 | break; |
belloula | 0:d07617f8ede9 | 149 | default : |
belloula | 0:d07617f8ede9 | 150 | break; |
belloula | 0:d07617f8ede9 | 151 | } |
belloula | 0:d07617f8ede9 | 152 | _state = WAIT_s; |
belloula | 0:d07617f8ede9 | 153 | break; |
belloula | 0:d07617f8ede9 | 154 | default : |
belloula | 0:d07617f8ede9 | 155 | _state = WAIT_s; |
belloula | 0:d07617f8ede9 | 156 | break; |
belloula | 0:d07617f8ede9 | 157 | } |
belloula | 0:d07617f8ede9 | 158 | } |
belloula | 0:d07617f8ede9 | 159 | } |
belloula | 0:d07617f8ede9 | 160 | |
belloula | 0:d07617f8ede9 | 161 | void tick1() |
belloula | 0:d07617f8ede9 | 162 | { |
belloula | 0:d07617f8ede9 | 163 | bmp085.update(); |
belloula | 0:d07617f8ede9 | 164 | } |
belloula | 0:d07617f8ede9 | 165 | |
belloula | 0:d07617f8ede9 | 166 | int main() { |
belloula | 0:d07617f8ede9 | 167 | |
belloula | 0:d07617f8ede9 | 168 | imu.attach(attimu); |
belloula | 0:d07617f8ede9 | 169 | Int_DataLogger (); |
belloula | 0:d07617f8ede9 | 170 | activate=1; |
belloula | 0:d07617f8ede9 | 171 | periode1.attach_us(tick1,10000); |
belloula | 0:d07617f8ede9 | 172 | |
belloula | 0:d07617f8ede9 | 173 | //event_EOC.rise(&trigger); |
belloula | 0:d07617f8ede9 | 174 | |
belloula | 0:d07617f8ede9 | 175 | /* FILE *fp = fopen("/sd/sdtest.txt", "w"); |
belloula | 0:d07617f8ede9 | 176 | if (fp == NULL) |
belloula | 0:d07617f8ede9 | 177 | { |
belloula | 0:d07617f8ede9 | 178 | error("Could not open file for write SD\n"); |
belloula | 0:d07617f8ede9 | 179 | }*/ |
belloula | 0:d07617f8ede9 | 180 | |
belloula | 0:d07617f8ede9 | 181 | |
belloula | 0:d07617f8ede9 | 182 | |
belloula | 0:d07617f8ede9 | 183 | FILE *usbp = fopen("/msc/sdtest.txt", "w"); |
belloula | 0:d07617f8ede9 | 184 | if (usbp == NULL) |
belloula | 0:d07617f8ede9 | 185 | { |
belloula | 0:d07617f8ede9 | 186 | error("Could not open file for write MSC\n"); |
belloula | 0:d07617f8ede9 | 187 | } |
belloula | 0:d07617f8ede9 | 188 | |
belloula | 0:d07617f8ede9 | 189 | |
belloula | 0:d07617f8ede9 | 190 | tmesure.start(); |
belloula | 0:d07617f8ede9 | 191 | tt.start(); |
belloula | 0:d07617f8ede9 | 192 | while (enable) |
belloula | 0:d07617f8ede9 | 193 | { |
belloula | 0:d07617f8ede9 | 194 | if(tt.read_us()>(unsigned long)50000) |
belloula | 0:d07617f8ede9 | 195 | { |
belloula | 0:d07617f8ede9 | 196 | tt.stop(); |
belloula | 0:d07617f8ede9 | 197 | tt.reset(); |
belloula | 0:d07617f8ede9 | 198 | |
belloula | 0:d07617f8ede9 | 199 | if(tag_imu==1) |
belloula | 0:d07617f8ede9 | 200 | { |
belloula | 0:d07617f8ede9 | 201 | fprintf(usbp,"IMU %u %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",tmesure.read_us(),(int16_t)data[0]<<8|data[1],(int16_t)data[2]<<8|data[3],(int16_t)data[4]<<8|data[5],(int16_t)data[6]<<8|data[7],(int16_t)data[8]<<8|data[9],(int16_t)data[10]<<8|data[11],(int16_t)data[12]<<8|data[13],(int16_t)data[14]<<8|data[15],(int16_t)data[16]<<8|data[17],(int16_t)data[18]<<8|data[19],(int16_t)data[20]<<8|data[21],(int16_t)data[22]<<8|data[23],(int16_t)data[24]<<8|data[25],(int16_t)data[26]<<8|data[27],(int16_t)data[28]<<8|data[29]); |
belloula | 0:d07617f8ede9 | 202 | tag_imu=0; |
belloula | 0:d07617f8ede9 | 203 | if(bmp085.ok==1) |
belloula | 0:d07617f8ede9 | 204 | { |
belloula | 0:d07617f8ede9 | 205 | fprintf(usbp,"PRT %u %6.2f %6.2f \r", tmesure.read_us(),bmp085.get_pressure(), bmp085.get_temperature()); |
belloula | 0:d07617f8ede9 | 206 | bmp085.ok=0; |
belloula | 0:d07617f8ede9 | 207 | } |
belloula | 0:d07617f8ede9 | 208 | } |
belloula | 0:d07617f8ede9 | 209 | tt.start(); |
belloula | 0:d07617f8ede9 | 210 | } |
belloula | 0:d07617f8ede9 | 211 | } |
belloula | 0:d07617f8ede9 | 212 | |
belloula | 0:d07617f8ede9 | 213 | // fclose(fp); |
belloula | 0:d07617f8ede9 | 214 | fclose(usbp); |
belloula | 0:d07617f8ede9 | 215 | periode1.detach(); |
belloula | 0:d07617f8ede9 | 216 | pc.printf("fin enregistrement"); |
belloula | 0:d07617f8ede9 | 217 | |
belloula | 0:d07617f8ede9 | 218 | } |