![](/media/cache/profiles/6f7790bb54a7c5d3dea27385488a604a.jpg.50x50_q85.jpg)
putting strings to xbee
Dependencies: BMP085 SDFileSystem mbed JPEGCamera
Fork of SaibiCansat2014 by
main.cpp@11:3b13ec465f0f, 2014-07-18 (annotated)
- Committer:
- TakashiSasaki
- Date:
- Fri Jul 18 09:54:25 2014 +0000
- Revision:
- 11:3b13ec465f0f
- Parent:
- 9:3a2d25f4700b
- Child:
- 12:495d513bb022
saving sensor data on SD card as well
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 | |
emanon | 1:31e810237ac1 | 16 | AnalogIn ain_CDS(p15); |
emanon | 1:31e810237ac1 | 17 | AnalogIn ain_Humedity(p16); |
emanon | 1:31e810237ac1 | 18 | AnalogIn ain_LM35DZ(p20); |
emanon | 1:31e810237ac1 | 19 | |
emanon | 1:31e810237ac1 | 20 | |
emanon | 0:e4ca6571a751 | 21 | DigitalOut myled(LED1); |
emanon | 0:e4ca6571a751 | 22 | |
emanon | 0:e4ca6571a751 | 23 | int fileio() { |
emanon | 0:e4ca6571a751 | 24 | mkdir("/sd/20014", 0777); |
emanon | 0:e4ca6571a751 | 25 | |
emanon | 0:e4ca6571a751 | 26 | FILE *fp = fopen("/sd/mydir/datalog.txt", "w"); |
emanon | 0:e4ca6571a751 | 27 | if(fp == NULL) { |
emanon | 0:e4ca6571a751 | 28 | error("Could not open file for write\n"); |
emanon | 0:e4ca6571a751 | 29 | } |
emanon | 0:e4ca6571a751 | 30 | fprintf(fp, "Hello fun SD Card World!"); |
emanon | 0:e4ca6571a751 | 31 | fclose(fp); |
emanon | 0:e4ca6571a751 | 32 | |
emanon | 0:e4ca6571a751 | 33 | printf("Goodbye World!\n"); |
emanon | 0:e4ca6571a751 | 34 | |
emanon | 0:e4ca6571a751 | 35 | return(0); |
emanon | 0:e4ca6571a751 | 36 | } |
emanon | 0:e4ca6571a751 | 37 | |
emanon | 0:e4ca6571a751 | 38 | |
emanon | 0:e4ca6571a751 | 39 | int main() { |
TakashiSasaki | 6:9824d4e2f29b | 40 | pc.printf("main() started. pc.baud is 9600 at this time."); |
TakashiSasaki | 6:9824d4e2f29b | 41 | //pc.baud(9600); |
TakashiSasaki | 9:3a2d25f4700b | 42 | xbee.baud(115200); |
emanon | 0:e4ca6571a751 | 43 | myled = 1; |
emanon | 0:e4ca6571a751 | 44 | |
emanon | 0:e4ca6571a751 | 45 | int readings[3] = {0, 0, 0}; |
emanon | 0:e4ca6571a751 | 46 | |
emanon | 0:e4ca6571a751 | 47 | pc.printf("Starting ADXL345 test...\n"); |
emanon | 0:e4ca6571a751 | 48 | wait(.001); |
emanon | 0:e4ca6571a751 | 49 | pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID()); |
emanon | 0:e4ca6571a751 | 50 | wait(.001); |
emanon | 0:e4ca6571a751 | 51 | |
emanon | 0:e4ca6571a751 | 52 | // These are here to test whether any of the initialization fails. It will print the failure |
emanon | 0:e4ca6571a751 | 53 | if (accelerometer.setPowerControl(0x00)){ |
emanon | 0:e4ca6571a751 | 54 | pc.printf("didn't intitialize power control\n"); |
emanon | 0:e4ca6571a751 | 55 | return 0; |
emanon | 0:e4ca6571a751 | 56 | } |
emanon | 0:e4ca6571a751 | 57 | //Full resolution, +/-16g, 4mg/LSB. |
emanon | 0:e4ca6571a751 | 58 | wait(.001); |
emanon | 0:e4ca6571a751 | 59 | |
emanon | 0:e4ca6571a751 | 60 | if(accelerometer.setDataFormatControl(0x0B)){ |
emanon | 0:e4ca6571a751 | 61 | pc.printf("didn't set data format\n"); |
emanon | 0:e4ca6571a751 | 62 | return 0; } |
emanon | 0:e4ca6571a751 | 63 | wait(.001); |
emanon | 0:e4ca6571a751 | 64 | |
emanon | 0:e4ca6571a751 | 65 | //3.2kHz data rate. |
emanon | 0:e4ca6571a751 | 66 | if(accelerometer.setDataRate(ADXL345_3200HZ)){ |
emanon | 0:e4ca6571a751 | 67 | pc.printf("didn't set data rate\n"); |
emanon | 0:e4ca6571a751 | 68 | return 0; |
emanon | 0:e4ca6571a751 | 69 | } |
emanon | 0:e4ca6571a751 | 70 | wait(.001); |
emanon | 0:e4ca6571a751 | 71 | |
TakashiSasaki | 9:3a2d25f4700b | 72 | FastJpegCamera fast_jpeg_camera(p9, p10, xbee); |
TakashiSasaki | 7:cc8080b31ac5 | 73 | |
emanon | 0:e4ca6571a751 | 74 | //Measurement mode. |
emanon | 0:e4ca6571a751 | 75 | |
emanon | 0:e4ca6571a751 | 76 | if(accelerometer.setPowerControl(MeasurementMode)) { |
emanon | 0:e4ca6571a751 | 77 | pc.printf("didn't set the power control to measurement\n"); |
emanon | 0:e4ca6571a751 | 78 | return 0; |
emanon | 0:e4ca6571a751 | 79 | } |
emanon | 0:e4ca6571a751 | 80 | myled = 0; |
emanon | 0:e4ca6571a751 | 81 | |
TakashiSasaki | 5:8a4b2ffa8d71 | 82 | int loop_count = 0; |
TakashiSasaki | 5:8a4b2ffa8d71 | 83 | |
emanon | 0:e4ca6571a751 | 84 | while (1) { |
TakashiSasaki | 11:3b13ec465f0f | 85 | FILE *fp = fopen("/sd/sensor.csv", "a"); |
TakashiSasaki | 5:8a4b2ffa8d71 | 86 | loop_count += 1; |
TakashiSasaki | 5:8a4b2ffa8d71 | 87 | |
TakashiSasaki | 5:8a4b2ffa8d71 | 88 | //wait(1.5); |
emanon | 1:31e810237ac1 | 89 | |
emanon | 1:31e810237ac1 | 90 | //BOSH BMP085 Barometric pressure,temperature measurement |
emanon | 0:e4ca6571a751 | 91 | accelerometer.getOutput(readings); |
TakashiSasaki | 11:3b13ec465f0f | 92 | //pc.printf("ADXL345 ------------------------\n"); |
TakashiSasaki | 11:3b13ec465f0f | 93 | char buf[100]; |
TakashiSasaki | 11:3b13ec465f0f | 94 | sprintf(buf, "X %+4.2f Y %+4.2f Z %+4.2f ", |
emanon | 0:e4ca6571a751 | 95 | (float((int16_t)readings[0]+18)/256), |
emanon | 0:e4ca6571a751 | 96 | (float((int16_t)readings[1]-4 )/256), |
emanon | 0:e4ca6571a751 | 97 | (float((int16_t)readings[2]+22)/256)); |
TakashiSasaki | 11:3b13ec465f0f | 98 | pc.printf(buf); |
TakashiSasaki | 11:3b13ec465f0f | 99 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 100 | xbee.printf(buf); |
emanon | 0:e4ca6571a751 | 101 | |
emanon | 0:e4ca6571a751 | 102 | bmp085.update(); |
emanon | 1:31e810237ac1 | 103 | |
emanon | 1:31e810237ac1 | 104 | //ADXL 345 Acceleration measurement |
TakashiSasaki | 11:3b13ec465f0f | 105 | //pc.printf("\nBOSH BMP085 --------------------\n"); |
TakashiSasaki | 11:3b13ec465f0f | 106 | sprintf(buf, "P %6.2f T %6.2f ", bmp085.get_pressure(), bmp085.get_temperature()); |
TakashiSasaki | 11:3b13ec465f0f | 107 | pc.printf(buf); |
TakashiSasaki | 11:3b13ec465f0f | 108 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 109 | xbee.printf(buf); |
emanon | 1:31e810237ac1 | 110 | |
emanon | 1:31e810237ac1 | 111 | //CSDS Ambient light measurement P15 analog-in1 |
TakashiSasaki | 11:3b13ec465f0f | 112 | sprintf(buf, "L %f ",ain_CDS*100); |
TakashiSasaki | 11:3b13ec465f0f | 113 | pc.printf(buf); |
TakashiSasaki | 11:3b13ec465f0f | 114 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 115 | xbee.printf(buf); |
emanon | 1:31e810237ac1 | 116 | |
emanon | 1:31e810237ac1 | 117 | //Humidity measurement P16 analog-in2 |
TakashiSasaki | 11:3b13ec465f0f | 118 | sprintf(buf, "H %f ",ain_Humedity*100); |
TakashiSasaki | 11:3b13ec465f0f | 119 | pc.printf(buf); |
TakashiSasaki | 11:3b13ec465f0f | 120 | fwrite(buf, strlen(buf), 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 121 | fwrite("\r\n", 2, 1, fp); |
TakashiSasaki | 11:3b13ec465f0f | 122 | fclose(fp); |
TakashiSasaki | 11:3b13ec465f0f | 123 | |
TakashiSasaki | 11:3b13ec465f0f | 124 | xbee.printf(buf); |
emanon | 1:31e810237ac1 | 125 | |
emanon | 1:31e810237ac1 | 126 | //LM35DZ Temperature measurement P20 analog-in6 |
TakashiSasaki | 6:9824d4e2f29b | 127 | //this sensor does not work for now. disabled. |
TakashiSasaki | 6:9824d4e2f29b | 128 | //pc.printf("Temperature :: %f\n",ain_LM35DZ*100); |
emanon | 1:31e810237ac1 | 129 | |
TakashiSasaki | 5:8a4b2ffa8d71 | 130 | //take picture |
TakashiSasaki | 6:9824d4e2f29b | 131 | //main_camera(loop_count); |
TakashiSasaki | 6:9824d4e2f29b | 132 | fast_jpeg_camera.shoot(loop_count); |
emanon | 0:e4ca6571a751 | 133 | } |
emanon | 0:e4ca6571a751 | 134 | } |