LSM303DLH Test Program for get angle.

Dependencies:   L3GD20 LSM303DLHC SDFileSystemLatch mbed nrf51_rtc

Fork of LSM303DLHTest by Toshihisa T

Committer:
afmiee
Date:
Wed Mar 07 20:35:49 2018 +0000
Revision:
6:577bcc3b864a
Parent:
5:b1a689c55f59
Latch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
afmiee 5:b1a689c55f59 1 // Latch Inc.
afmiee 5:b1a689c55f59 2 // Antonio F Mondragon
afmiee 5:b1a689c55f59 3 // 20160714
afmiee 5:b1a689c55f59 4 // for the Adafruit 9DOF Modulke and the Sparkfun microSD card shield
tosihisa 0:750784997b84 5
tosihisa 0:750784997b84 6 #include "mbed.h"
afmiee 5:b1a689c55f59 7 #include "LSM303DLHC.h"
afmiee 5:b1a689c55f59 8 #include "L3GD20.h"
afmiee 5:b1a689c55f59 9 #include "SDFileSystem.h"
afmiee 5:b1a689c55f59 10 #include "nrf51_rtc.h"
afmiee 5:b1a689c55f59 11
afmiee 5:b1a689c55f59 12 #define M_PI 3.14158
afmiee 5:b1a689c55f59 13
afmiee 5:b1a689c55f59 14 // Create objects
afmiee 5:b1a689c55f59 15 Serial debug(USBTX,USBRX);
afmiee 5:b1a689c55f59 16 // For Nordic
afmiee 5:b1a689c55f59 17 LSM303DLHC compass(p7, p30);
afmiee 5:b1a689c55f59 18 L3GD20 gyro(p7, p30);
afmiee 5:b1a689c55f59 19
afmiee 5:b1a689c55f59 20 // Create the SD filesystem
afmiee 5:b1a689c55f59 21 SDFileSystem sd(p25, p28, p29, p20, "sd"); // MOSI, MISO, SCLK, SSEL
afmiee 5:b1a689c55f59 22
afmiee 5:b1a689c55f59 23 // Create a ticker to use the nRF51 RTC
afmiee 5:b1a689c55f59 24 Ticker flipper;
afmiee 5:b1a689c55f59 25
afmiee 5:b1a689c55f59 26 // Assign interrupts to switches
afmiee 5:b1a689c55f59 27 InterruptIn btn1(p17); // Start sampling
afmiee 5:b1a689c55f59 28 InterruptIn btn2(p18); // Stop sampoling
afmiee 5:b1a689c55f59 29
afmiee 5:b1a689c55f59 30 // LED definitions
afmiee 5:b1a689c55f59 31 DigitalOut led1(LED1);
afmiee 5:b1a689c55f59 32 DigitalOut led2(LED2);
afmiee 5:b1a689c55f59 33
afmiee 5:b1a689c55f59 34 // Global variables
afmiee 5:b1a689c55f59 35 int start = 0;
afmiee 5:b1a689c55f59 36 int stop = 0;
afmiee 5:b1a689c55f59 37
tosihisa 0:750784997b84 38
afmiee 5:b1a689c55f59 39 // Generated when button 1 is pressed on rising edge START
afmiee 5:b1a689c55f59 40 void start_smpl()
afmiee 5:b1a689c55f59 41 {
afmiee 5:b1a689c55f59 42 start = 1;
afmiee 5:b1a689c55f59 43 stop = 0;
afmiee 5:b1a689c55f59 44 }
afmiee 5:b1a689c55f59 45
afmiee 5:b1a689c55f59 46 // Generated when button 1 is pressed on rising edge STOP
afmiee 5:b1a689c55f59 47 void stop_smpl()
afmiee 5:b1a689c55f59 48 {
afmiee 5:b1a689c55f59 49 stop = 1;
afmiee 5:b1a689c55f59 50 start = 0;
afmiee 5:b1a689c55f59 51 }
afmiee 5:b1a689c55f59 52
afmiee 5:b1a689c55f59 53 // Fillped every second
afmiee 5:b1a689c55f59 54 void flip()
afmiee 5:b1a689c55f59 55 {
afmiee 5:b1a689c55f59 56 led2 = !led2;
afmiee 5:b1a689c55f59 57 }
afmiee 5:b1a689c55f59 58
afmiee 5:b1a689c55f59 59 int main()
afmiee 5:b1a689c55f59 60 {
afmiee 5:b1a689c55f59 61 int acc[3];
afmiee 5:b1a689c55f59 62 int mag[3];
afmiee 5:b1a689c55f59 63 float g[3];
afmiee 5:b1a689c55f59 64 led1= 1;
afmiee 5:b1a689c55f59 65 char filename[256];
afmiee 5:b1a689c55f59 66 char secs_str[256];
afmiee 5:b1a689c55f59 67
afmiee 5:b1a689c55f59 68 struct tm t;
afmiee 5:b1a689c55f59 69 time_t seconds;
tosihisa 0:750784997b84 70
afmiee 5:b1a689c55f59 71 FILE *fp;
afmiee 5:b1a689c55f59 72
afmiee 5:b1a689c55f59 73 // Attach functions to interrupts
afmiee 5:b1a689c55f59 74 btn1.rise(&start_smpl);
afmiee 5:b1a689c55f59 75 btn2.rise(&stop_smpl);
afmiee 5:b1a689c55f59 76 flipper.attach(&flip, 1.0); // the address of the function to be attached (flip) and the interval (2 seconds)
afmiee 5:b1a689c55f59 77
afmiee 5:b1a689c55f59 78 // Enable serial port
afmiee 5:b1a689c55f59 79 debug.format(8,Serial::None,1);
afmiee 5:b1a689c55f59 80 debug.baud(115200);
afmiee 5:b1a689c55f59 81 debug.printf("LSM303DLH Test\x0d\x0a");
afmiee 5:b1a689c55f59 82
afmiee 5:b1a689c55f59 83 // Initialize compass
afmiee 5:b1a689c55f59 84 compass.init();
afmiee 5:b1a689c55f59 85 compass.setOffset(0.00,0.00,0.00); // example calibration
afmiee 5:b1a689c55f59 86 compass.setScale(1.00,1.00,1.00); // example calibration
afmiee 5:b1a689c55f59 87
afmiee 5:b1a689c55f59 88 // // Initialize current time if needed
afmiee 5:b1a689c55f59 89 // printf("Enter current date and time:\n");
afmiee 5:b1a689c55f59 90 // printf("YYYY MM DD HH MM SS[enter]\n");
afmiee 5:b1a689c55f59 91 // scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
afmiee 5:b1a689c55f59 92 // , &t.tm_hour, &t.tm_min, &t.tm_sec);
afmiee 5:b1a689c55f59 93
afmiee 5:b1a689c55f59 94 // adjust for tm structure required values
afmiee 5:b1a689c55f59 95 t.tm_year = t.tm_year - 1900;
afmiee 5:b1a689c55f59 96 t.tm_mon = t.tm_mon - 1;
afmiee 5:b1a689c55f59 97
afmiee 5:b1a689c55f59 98 // set the time
afmiee 5:b1a689c55f59 99 rtc.set_time(mktime(&t));
afmiee 5:b1a689c55f59 100
afmiee 5:b1a689c55f59 101 while(1) {
afmiee 5:b1a689c55f59 102 debug.printf("Press Button 1 to Start sampling\n\r");
afmiee 5:b1a689c55f59 103 debug.printf("Press Button 2 to Stop sampling\n\r");
afmiee 5:b1a689c55f59 104 // Check for button 1 pressed
afmiee 5:b1a689c55f59 105 while(!start) {
afmiee 5:b1a689c55f59 106 led1 = 1;
afmiee 5:b1a689c55f59 107 }
afmiee 5:b1a689c55f59 108 // Start sampling
afmiee 5:b1a689c55f59 109 led1 = 0;
afmiee 5:b1a689c55f59 110 debug.printf("Started sampling\n\r");
afmiee 5:b1a689c55f59 111 // Get the time and create a file with the number of seconds in hex appended
afmiee 5:b1a689c55f59 112 seconds = rtc.time();
afmiee 5:b1a689c55f59 113 sprintf(secs_str, "%s", ctime(&seconds));
afmiee 5:b1a689c55f59 114 printf("Started at: %s\n\r", secs_str );
afmiee 5:b1a689c55f59 115 sprintf(filename, "/sd/latch9DOF_%08x",seconds);
afmiee 5:b1a689c55f59 116 fp = fopen(filename, "w");
afmiee 5:b1a689c55f59 117 // Verify that file can be created
afmiee 5:b1a689c55f59 118 if ( fp == NULL ) {
afmiee 5:b1a689c55f59 119 debug.printf("Cannot create file %s\n\r", filename);
afmiee 5:b1a689c55f59 120 wait(0.5);
afmiee 5:b1a689c55f59 121 while(1) {
afmiee 5:b1a689c55f59 122 led1 = !led1;
afmiee 5:b1a689c55f59 123 wait(0.5);
afmiee 5:b1a689c55f59 124 }
afmiee 5:b1a689c55f59 125 } else
afmiee 5:b1a689c55f59 126 debug.printf("File %s created successfully\n\r", filename);
afmiee 5:b1a689c55f59 127
afmiee 5:b1a689c55f59 128 // Sample until button 2 is pressed
afmiee 5:b1a689c55f59 129 while(!stop) {
afmiee 5:b1a689c55f59 130 led1 = 0;
afmiee 5:b1a689c55f59 131 compass.read(acc,mag);
afmiee 5:b1a689c55f59 132 gyro.read(&g[0],&g[1],&g[2]);
afmiee 5:b1a689c55f59 133 debug.printf("%6d,\t%6d,\t%6d,\t%6d,\t%6d,\t%6d,\t%6.2f,\t%6.2f,\t%6.2f\n\r",acc[0],acc[1],acc[2],mag[0],mag[1],mag[2],g[0],g[1],g[2]);
afmiee 5:b1a689c55f59 134 fprintf(fp, "%6d,\t%6d,\t%6d,\t%6d,\t%6d,\t%6d,\t%6.2f,\t%6.2f,\t%6.2f\n\r",acc[0],acc[1],acc[2],mag[0],mag[1],mag[2],g[0],g[1],g[2]);
afmiee 5:b1a689c55f59 135 wait(0.1);
afmiee 5:b1a689c55f59 136 }
afmiee 5:b1a689c55f59 137 // Stop Sampling and close file
afmiee 5:b1a689c55f59 138 led1 = 1;
afmiee 5:b1a689c55f59 139 debug.printf("Stopped sampling\n\r");
afmiee 5:b1a689c55f59 140 debug.printf("Results stored in %s\n\r", filename);
afmiee 5:b1a689c55f59 141 fclose(fp);
afmiee 5:b1a689c55f59 142 }
tosihisa 4:3c677edffb13 143 }