![](/media/cache/profiles/6f7790bb54a7c5d3dea27385488a604a.jpg.50x50_q85.jpg)
putting strings to xbee
Dependencies: BMP085 SDFileSystem mbed JPEGCamera
Fork of SaibiCansat2014 by
main.cpp@13:aa50109c4cae, 2014-07-20 (annotated)
- Committer:
- TakashiSasaki
- Date:
- Sun Jul 20 13:53:17 2014 +0000
- Revision:
- 13:aa50109c4cae
- Parent:
- 12:495d513bb022
fix bug related to humidity sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emanon | 0:e4ca6571a751 | 1 | #include "mbed.h" |
emanon | 0:e4ca6571a751 | 2 | #include "SDFileSystem.h" |
emanon | 0:e4ca6571a751 | 3 | #include "BMP085.h" |
emanon | 0:e4ca6571a751 | 4 | #include "ADXL345_I2C.h" |
TakashiSasaki | 6:9824d4e2f29b | 5 | //#include "camera.h" |
TakashiSasaki | 6:9824d4e2f29b | 6 | #include "FastJpegCamera.h" |
TakashiSasaki | 6:9824d4e2f29b | 7 | |
emanon | 0:e4ca6571a751 | 8 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
emanon | 0:e4ca6571a751 | 9 | |
emanon | 0:e4ca6571a751 | 10 | ADXL345_I2C accelerometer(p28, p27); |
emanon | 0:e4ca6571a751 | 11 | BMP085 bmp085(p28, p27); |
emanon | 0:e4ca6571a751 | 12 | |
emanon | 0:e4ca6571a751 | 13 | Serial pc(USBTX, USBRX); |
TakashiSasaki | 3:7ea76022b5eb | 14 | Serial xbee(p13, p14); |
emanon | 0:e4ca6571a751 | 15 | |
TakashiSasaki | 12:495d513bb022 | 16 | Timer timer; |
TakashiSasaki | 12:495d513bb022 | 17 | |
emanon | 1:31e810237ac1 | 18 | AnalogIn ain_CDS(p15); |
TakashiSasaki | 12:495d513bb022 | 19 | AnalogIn ain_Humidity(p16); |
emanon | 1:31e810237ac1 | 20 | AnalogIn ain_LM35DZ(p20); |
emanon | 1:31e810237ac1 | 21 | |
emanon | 1:31e810237ac1 | 22 | |
emanon | 0:e4ca6571a751 | 23 | DigitalOut myled(LED1); |
emanon | 0:e4ca6571a751 | 24 | |
emanon | 0:e4ca6571a751 | 25 | int fileio() { |
emanon | 0:e4ca6571a751 | 26 | mkdir("/sd/20014", 0777); |
emanon | 0:e4ca6571a751 | 27 | |
emanon | 0:e4ca6571a751 | 28 | FILE *fp = fopen("/sd/mydir/datalog.txt", "w"); |
emanon | 0:e4ca6571a751 | 29 | if(fp == NULL) { |
emanon | 0:e4ca6571a751 | 30 | error("Could not open file for write\n"); |
emanon | 0:e4ca6571a751 | 31 | } |
emanon | 0:e4ca6571a751 | 32 | fprintf(fp, "Hello fun SD Card World!"); |
emanon | 0:e4ca6571a751 | 33 | fclose(fp); |
emanon | 0:e4ca6571a751 | 34 | |
emanon | 0:e4ca6571a751 | 35 | printf("Goodbye World!\n"); |
emanon | 0:e4ca6571a751 | 36 | |
emanon | 0:e4ca6571a751 | 37 | return(0); |
emanon | 0:e4ca6571a751 | 38 | } |
emanon | 0:e4ca6571a751 | 39 | |
emanon | 0:e4ca6571a751 | 40 | |
emanon | 0:e4ca6571a751 | 41 | int main() { |
TakashiSasaki | 12:495d513bb022 | 42 | timer.start(); |
TakashiSasaki | 6:9824d4e2f29b | 43 | pc.printf("main() started. pc.baud is 9600 at this time."); |
TakashiSasaki | 6:9824d4e2f29b | 44 | //pc.baud(9600); |
TakashiSasaki | 9:3a2d25f4700b | 45 | xbee.baud(115200); |
emanon | 0:e4ca6571a751 | 46 | myled = 1; |
emanon | 0:e4ca6571a751 | 47 | |
emanon | 0:e4ca6571a751 | 48 | int readings[3] = {0, 0, 0}; |
emanon | 0:e4ca6571a751 | 49 | |
emanon | 0:e4ca6571a751 | 50 | pc.printf("Starting ADXL345 test...\n"); |
emanon | 0:e4ca6571a751 | 51 | wait(.001); |
emanon | 0:e4ca6571a751 | 52 | pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID()); |
emanon | 0:e4ca6571a751 | 53 | wait(.001); |
emanon | 0:e4ca6571a751 | 54 | |
emanon | 0:e4ca6571a751 | 55 | // These are here to test whether any of the initialization fails. It will print the failure |
emanon | 0:e4ca6571a751 | 56 | if (accelerometer.setPowerControl(0x00)){ |
emanon | 0:e4ca6571a751 | 57 | pc.printf("didn't intitialize power control\n"); |
emanon | 0:e4ca6571a751 | 58 | return 0; |
emanon | 0:e4ca6571a751 | 59 | } |
emanon | 0:e4ca6571a751 | 60 | //Full resolution, +/-16g, 4mg/LSB. |
emanon | 0:e4ca6571a751 | 61 | wait(.001); |
emanon | 0:e4ca6571a751 | 62 | |
emanon | 0:e4ca6571a751 | 63 | if(accelerometer.setDataFormatControl(0x0B)){ |
emanon | 0:e4ca6571a751 | 64 | pc.printf("didn't set data format\n"); |
emanon | 0:e4ca6571a751 | 65 | return 0; } |
emanon | 0:e4ca6571a751 | 66 | wait(.001); |
emanon | 0:e4ca6571a751 | 67 | |
emanon | 0:e4ca6571a751 | 68 | //3.2kHz data rate. |
emanon | 0:e4ca6571a751 | 69 | if(accelerometer.setDataRate(ADXL345_3200HZ)){ |
emanon | 0:e4ca6571a751 | 70 | pc.printf("didn't set data rate\n"); |
emanon | 0:e4ca6571a751 | 71 | return 0; |
emanon | 0:e4ca6571a751 | 72 | } |
emanon | 0:e4ca6571a751 | 73 | wait(.001); |
emanon | 0:e4ca6571a751 | 74 | |
TakashiSasaki | 9:3a2d25f4700b | 75 | FastJpegCamera fast_jpeg_camera(p9, p10, xbee); |
TakashiSasaki | 7:cc8080b31ac5 | 76 | |
emanon | 0:e4ca6571a751 | 77 | //Measurement mode. |
emanon | 0:e4ca6571a751 | 78 | |
emanon | 0:e4ca6571a751 | 79 | if(accelerometer.setPowerControl(MeasurementMode)) { |
emanon | 0:e4ca6571a751 | 80 | pc.printf("didn't set the power control to measurement\n"); |
emanon | 0:e4ca6571a751 | 81 | return 0; |
emanon | 0:e4ca6571a751 | 82 | } |
emanon | 0:e4ca6571a751 | 83 | myled = 0; |
emanon | 0:e4ca6571a751 | 84 | |
TakashiSasaki | 5:8a4b2ffa8d71 | 85 | int loop_count = 0; |
TakashiSasaki | 5:8a4b2ffa8d71 | 86 | |
emanon | 0:e4ca6571a751 | 87 | while (1) { |
TakashiSasaki | 12:495d513bb022 | 88 | |
TakashiSasaki | 11:3b13ec465f0f | 89 | FILE *fp = fopen("/sd/sensor.csv", "a"); |
TakashiSasaki | 5:8a4b2ffa8d71 | 90 | loop_count += 1; |
TakashiSasaki | 5:8a4b2ffa8d71 | 91 | |
TakashiSasaki | 12:495d513bb022 | 92 | char buf[100]; |
TakashiSasaki | 12:495d513bb022 | 93 | |
TakashiSasaki | 12:495d513bb022 | 94 | sprintf(buf, "C %d ", loop_count); |
TakashiSasaki | 12:495d513bb022 | 95 | pc.printf(buf); |
TakashiSasaki | 12:495d513bb022 | 96 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 12:495d513bb022 | 97 | xbee.printf(buf); |
TakashiSasaki | 12:495d513bb022 | 98 | |
TakashiSasaki | 12:495d513bb022 | 99 | float seconds = timer.read(); |
TakashiSasaki | 12:495d513bb022 | 100 | sprintf(buf, "S %.3f ", seconds); |
TakashiSasaki | 12:495d513bb022 | 101 | pc.printf(buf); |
TakashiSasaki | 12:495d513bb022 | 102 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 12:495d513bb022 | 103 | xbee.printf(buf); |
TakashiSasaki | 12:495d513bb022 | 104 | |
TakashiSasaki | 5:8a4b2ffa8d71 | 105 | //wait(1.5); |
emanon | 1:31e810237ac1 | 106 | |
emanon | 1:31e810237ac1 | 107 | //BOSH BMP085 Barometric pressure,temperature measurement |
emanon | 0:e4ca6571a751 | 108 | accelerometer.getOutput(readings); |
TakashiSasaki | 11:3b13ec465f0f | 109 | //pc.printf("ADXL345 ------------------------\n"); |
TakashiSasaki | 12:495d513bb022 | 110 | sprintf(buf, "X %+4.3f Y %+4.3f Z %+4.3f ", |
TakashiSasaki | 12:495d513bb022 | 111 | (float((int16_t)readings[0]+18)/256 - 0.12), |
TakashiSasaki | 12:495d513bb022 | 112 | (float((int16_t)readings[1]-4 )/256 - 0.03), |
TakashiSasaki | 12:495d513bb022 | 113 | (float((int16_t)readings[2]+22)/256 + 0.12)); |
TakashiSasaki | 11:3b13ec465f0f | 114 | pc.printf(buf); |
TakashiSasaki | 11:3b13ec465f0f | 115 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 116 | xbee.printf(buf); |
emanon | 0:e4ca6571a751 | 117 | |
emanon | 0:e4ca6571a751 | 118 | bmp085.update(); |
emanon | 1:31e810237ac1 | 119 | |
emanon | 1:31e810237ac1 | 120 | //ADXL 345 Acceleration measurement |
TakashiSasaki | 11:3b13ec465f0f | 121 | //pc.printf("\nBOSH BMP085 --------------------\n"); |
TakashiSasaki | 12:495d513bb022 | 122 | const float P = bmp085.get_pressure(); |
TakashiSasaki | 12:495d513bb022 | 123 | const float T = bmp085.get_temperature(); |
TakashiSasaki | 12:495d513bb022 | 124 | sprintf(buf, "P %6.2f T %6.2f ", P, T); |
TakashiSasaki | 11:3b13ec465f0f | 125 | pc.printf(buf); |
TakashiSasaki | 11:3b13ec465f0f | 126 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 127 | xbee.printf(buf); |
emanon | 1:31e810237ac1 | 128 | |
emanon | 1:31e810237ac1 | 129 | //CSDS Ambient light measurement P15 analog-in1 |
TakashiSasaki | 11:3b13ec465f0f | 130 | sprintf(buf, "L %f ",ain_CDS*100); |
TakashiSasaki | 11:3b13ec465f0f | 131 | pc.printf(buf); |
TakashiSasaki | 11:3b13ec465f0f | 132 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 133 | xbee.printf(buf); |
emanon | 1:31e810237ac1 | 134 | |
emanon | 1:31e810237ac1 | 135 | //Humidity measurement P16 analog-in2 |
TakashiSasaki | 13:aa50109c4cae | 136 | const float SRH = ( ( ain_Humidity * 3.3 / 5.0 ) - 0.16 ) / 0.0062; |
TakashiSasaki | 12:495d513bb022 | 137 | const float TRH = SRH / ( 1.0546 - 0.00216 * T); |
TakashiSasaki | 12:495d513bb022 | 138 | |
TakashiSasaki | 12:495d513bb022 | 139 | sprintf(buf, "H %f ", TRH / 10.0); // maybe wrong, dirty workaround |
TakashiSasaki | 12:495d513bb022 | 140 | |
TakashiSasaki | 11:3b13ec465f0f | 141 | pc.printf(buf); |
TakashiSasaki | 11:3b13ec465f0f | 142 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 143 | fwrite("\r\n", 2, 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 144 | fclose(fp); |
TakashiSasaki | 12:495d513bb022 | 145 | |
TakashiSasaki | 11:3b13ec465f0f | 146 | xbee.printf(buf); |
TakashiSasaki | 12:495d513bb022 | 147 | xbee.printf("\n"); |
emanon | 1:31e810237ac1 | 148 | |
emanon | 1:31e810237ac1 | 149 | //LM35DZ Temperature measurement P20 analog-in6 |
TakashiSasaki | 6:9824d4e2f29b | 150 | //this sensor does not work for now. disabled. |
TakashiSasaki | 6:9824d4e2f29b | 151 | //pc.printf("Temperature :: %f\n",ain_LM35DZ*100); |
emanon | 1:31e810237ac1 | 152 | |
TakashiSasaki | 5:8a4b2ffa8d71 | 153 | //take picture |
TakashiSasaki | 6:9824d4e2f29b | 154 | //main_camera(loop_count); |
TakashiSasaki | 12:495d513bb022 | 155 | xbee.printf("--JFIF_BOUNDARY\n"); |
TakashiSasaki | 6:9824d4e2f29b | 156 | fast_jpeg_camera.shoot(loop_count); |
TakashiSasaki | 12:495d513bb022 | 157 | xbee.printf("--JFIF_BOUNDARY--\n"); |
emanon | 0:e4ca6571a751 | 158 | } |
emanon | 0:e4ca6571a751 | 159 | } |