K Andres / Mbed 2 deprecated SCIboard

Dependencies:   mbed

Committer:
AstrodyneSystems
Date:
Sun Dec 15 22:21:21 2013 +0000
Revision:
0:ab51d784ef36
Child:
1:a54c4a4f3b30
Initial build

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AstrodyneSystems 0:ab51d784ef36 1 /* SCIboard(TM) main.cpp
AstrodyneSystems 0:ab51d784ef36 2 Copyright (c) 2013 K. Andres
AstrodyneSystems 0:ab51d784ef36 3
AstrodyneSystems 0:ab51d784ef36 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AstrodyneSystems 0:ab51d784ef36 5 of this software and associated documentation files (the "Software"), to deal
AstrodyneSystems 0:ab51d784ef36 6 in the Software without restriction, including without limitation the rights
AstrodyneSystems 0:ab51d784ef36 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AstrodyneSystems 0:ab51d784ef36 8 copies of the Software, and to permit persons to whom the Software is
AstrodyneSystems 0:ab51d784ef36 9 furnished to do so, subject to the following conditions:
AstrodyneSystems 0:ab51d784ef36 10
AstrodyneSystems 0:ab51d784ef36 11 The above copyright notice and this permission notice shall be included in
AstrodyneSystems 0:ab51d784ef36 12 all copies or substantial portions of the Software.
AstrodyneSystems 0:ab51d784ef36 13
AstrodyneSystems 0:ab51d784ef36 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AstrodyneSystems 0:ab51d784ef36 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AstrodyneSystems 0:ab51d784ef36 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AstrodyneSystems 0:ab51d784ef36 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AstrodyneSystems 0:ab51d784ef36 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AstrodyneSystems 0:ab51d784ef36 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AstrodyneSystems 0:ab51d784ef36 20 THE SOFTWARE.
AstrodyneSystems 0:ab51d784ef36 21 */
AstrodyneSystems 0:ab51d784ef36 22
AstrodyneSystems 0:ab51d784ef36 23 /* OVERVIEW
AstrodyneSystems 0:ab51d784ef36 24 Displays on USB serial (9600 bps) LSM303 ident 0x48 0x34 0x33
AstrodyneSystems 0:ab51d784ef36 25 then collects 10 seconds of data from accelerometer and magnetometer
AstrodyneSystems 0:ab51d784ef36 26
AstrodyneSystems 0:ab51d784ef36 27 Filename on Local: OUTxx.csv (csv format allows easy import into spreadsheets)
AstrodyneSystems 0:ab51d784ef36 28 Format: microseconds,record type, variable length data\r\n
AstrodyneSystems 0:ab51d784ef36 29 Rec type 2= accelerometer data x, y, and z in g's
AstrodyneSystems 0:ab51d784ef36 30 Rec type 3= magnetometer data x, y, and z
AstrodyneSystems 0:ab51d784ef36 31
AstrodyneSystems 0:ab51d784ef36 32 altimeter, gyro, and MOSFET switch software coming soon (under development)...
AstrodyneSystems 0:ab51d784ef36 33 */
AstrodyneSystems 0:ab51d784ef36 34
AstrodyneSystems 0:ab51d784ef36 35 #include "mbed.h"
AstrodyneSystems 0:ab51d784ef36 36 #include "math.h"
AstrodyneSystems 0:ab51d784ef36 37
AstrodyneSystems 0:ab51d784ef36 38 #include "SCIboard_I2C.h"
AstrodyneSystems 0:ab51d784ef36 39 #include "SCIboard_LSM303DLHC.h"
AstrodyneSystems 0:ab51d784ef36 40 #include "SCIboard_DataLogger.h"
AstrodyneSystems 0:ab51d784ef36 41
AstrodyneSystems 0:ab51d784ef36 42 // LEDs
AstrodyneSystems 0:ab51d784ef36 43 DigitalOut led1(LED1);
AstrodyneSystems 0:ab51d784ef36 44 DigitalOut led2(LED2);
AstrodyneSystems 0:ab51d784ef36 45 DigitalOut led3(LED3);
AstrodyneSystems 0:ab51d784ef36 46 DigitalOut led4(LED4);
AstrodyneSystems 0:ab51d784ef36 47
AstrodyneSystems 0:ab51d784ef36 48 // PWM out
AstrodyneSystems 0:ab51d784ef36 49 PwmOut pwm1(p26);
AstrodyneSystems 0:ab51d784ef36 50 PwmOut pwm2(p25);
AstrodyneSystems 0:ab51d784ef36 51 PwmOut pwm3(p24);
AstrodyneSystems 0:ab51d784ef36 52 PwmOut pwm4(p23);
AstrodyneSystems 0:ab51d784ef36 53 PwmOut pwm5(p22);
AstrodyneSystems 0:ab51d784ef36 54 PwmOut pwm6(p21);
AstrodyneSystems 0:ab51d784ef36 55
AstrodyneSystems 0:ab51d784ef36 56 // Serial gps(p13, p14);
AstrodyneSystems 0:ab51d784ef36 57 // Serial Xbee(p9, p10);
AstrodyneSystems 0:ab51d784ef36 58
AstrodyneSystems 0:ab51d784ef36 59 // Buzzer
AstrodyneSystems 0:ab51d784ef36 60 DigitalOut alert(p29); // CAN_RD
AstrodyneSystems 0:ab51d784ef36 61
AstrodyneSystems 0:ab51d784ef36 62 // MOSFET controls
AstrodyneSystems 0:ab51d784ef36 63 DigitalOut fet_out1(p15); // ADC1
AstrodyneSystems 0:ab51d784ef36 64 DigitalOut fet_out2(p12); // SPI2_MISO
AstrodyneSystems 0:ab51d784ef36 65 DigitalOut fet_out3(p11); // SPI2_MOSI
AstrodyneSystems 0:ab51d784ef36 66 DigitalOut fet_out4(p8); // GPIO8
AstrodyneSystems 0:ab51d784ef36 67
AstrodyneSystems 0:ab51d784ef36 68 // ADC inputs
AstrodyneSystems 0:ab51d784ef36 69 AnalogIn batt_mon(p16); // ADC2
AstrodyneSystems 0:ab51d784ef36 70 AnalogIn fet_mon1(p17); // ADC3
AstrodyneSystems 0:ab51d784ef36 71 AnalogIn fet_mon2(p18); // ADC4
AstrodyneSystems 0:ab51d784ef36 72 AnalogIn fet_mon3(p19); // ADC5
AstrodyneSystems 0:ab51d784ef36 73 AnalogIn fet_mon4(p20); // ADC6
AstrodyneSystems 0:ab51d784ef36 74
AstrodyneSystems 0:ab51d784ef36 75 //
AstrodyneSystems 0:ab51d784ef36 76 Serial pc(USBTX, USBRX); // Default 9600 bps
AstrodyneSystems 0:ab51d784ef36 77
AstrodyneSystems 0:ab51d784ef36 78 // Timers
AstrodyneSystems 0:ab51d784ef36 79 Timer t;
AstrodyneSystems 0:ab51d784ef36 80 int time_ms;
AstrodyneSystems 0:ab51d784ef36 81
AstrodyneSystems 0:ab51d784ef36 82
AstrodyneSystems 0:ab51d784ef36 83 // I2C interface
AstrodyneSystems 0:ab51d784ef36 84 //#define sdaPin p9
AstrodyneSystems 0:ab51d784ef36 85 //#define sdcPin p10
AstrodyneSystems 0:ab51d784ef36 86 #define sdaPin p28
AstrodyneSystems 0:ab51d784ef36 87 #define sdcPin p27
AstrodyneSystems 0:ab51d784ef36 88
AstrodyneSystems 0:ab51d784ef36 89 SCIboard_I2C i2cBus(sdaPin, sdcPin);
AstrodyneSystems 0:ab51d784ef36 90
AstrodyneSystems 0:ab51d784ef36 91
AstrodyneSystems 0:ab51d784ef36 92 // Accelerometer and magnetometer
AstrodyneSystems 0:ab51d784ef36 93 SCIboard_LSM303DLHC lsm303(&i2cBus);
AstrodyneSystems 0:ab51d784ef36 94 InterruptIn LSM303DLHC_DRDY(p7);
AstrodyneSystems 0:ab51d784ef36 95 void LSM303DLHC_DRDY_INTERRUPT(void);
AstrodyneSystems 0:ab51d784ef36 96 //InterruptIn LSM303DLHC_INT1(p6);
AstrodyneSystems 0:ab51d784ef36 97 //void LSM303DLHC_INT1_INTERRUPT(void);
AstrodyneSystems 0:ab51d784ef36 98 // INT2 n/c
AstrodyneSystems 0:ab51d784ef36 99
AstrodyneSystems 0:ab51d784ef36 100
AstrodyneSystems 0:ab51d784ef36 101 //---------------------------------------------------------------------
AstrodyneSystems 0:ab51d784ef36 102 int main() {
AstrodyneSystems 0:ab51d784ef36 103 unsigned char data[10];
AstrodyneSystems 0:ab51d784ef36 104 float f[3];
AstrodyneSystems 0:ab51d784ef36 105 char str[132];
AstrodyneSystems 0:ab51d784ef36 106 int currentTime;
AstrodyneSystems 0:ab51d784ef36 107 int lastTime=0;
AstrodyneSystems 0:ab51d784ef36 108
AstrodyneSystems 0:ab51d784ef36 109 LSM303DLHC_DRDY.fall(NULL);
AstrodyneSystems 0:ab51d784ef36 110 // LSM303DLHC_INT1.rise(NULL);
AstrodyneSystems 0:ab51d784ef36 111
AstrodyneSystems 0:ab51d784ef36 112 // DataLogger
AstrodyneSystems 0:ab51d784ef36 113 nextFilename();
AstrodyneSystems 0:ab51d784ef36 114
AstrodyneSystems 0:ab51d784ef36 115 lsm303.getDeviceID(data);
AstrodyneSystems 0:ab51d784ef36 116 sprintf(str, "LSM303DLHC Ident 0x%X 0x%X 0x%X\r\n", data[0], data[1], data[2]);
AstrodyneSystems 0:ab51d784ef36 117 pc.printf(str);
AstrodyneSystems 0:ab51d784ef36 118 log_write(str);
AstrodyneSystems 0:ab51d784ef36 119
AstrodyneSystems 0:ab51d784ef36 120
AstrodyneSystems 0:ab51d784ef36 121 // Setup accelerometer and magnetometer
AstrodyneSystems 0:ab51d784ef36 122 lsm303.setAccMode(ACC_4G, ACC_50HZ);
AstrodyneSystems 0:ab51d784ef36 123 lsm303.setMagMode(MAG_1p3G, MAG_75HZ);
AstrodyneSystems 0:ab51d784ef36 124
AstrodyneSystems 0:ab51d784ef36 125 t.start(); // start timer
AstrodyneSystems 0:ab51d784ef36 126
AstrodyneSystems 0:ab51d784ef36 127 LSM303DLHC_DRDY.fall(LSM303DLHC_DRDY_INTERRUPT); // Magnetometer
AstrodyneSystems 0:ab51d784ef36 128 // LSM303DLHC_INT1.rise(LSM303DLHC_INT1_INTERRUPT); // Accelerometer
AstrodyneSystems 0:ab51d784ef36 129
AstrodyneSystems 0:ab51d784ef36 130
AstrodyneSystems 0:ab51d784ef36 131 // Collect 10 seconds of data then close file system to allow mbed to be visible on USB connected PC for demo app only
AstrodyneSystems 0:ab51d784ef36 132 while(t.read()<10) {
AstrodyneSystems 0:ab51d784ef36 133 time_ms = t.read_ms();
AstrodyneSystems 0:ab51d784ef36 134 currentTime = t.read();
AstrodyneSystems 0:ab51d784ef36 135 if(currentTime!=lastTime) {
AstrodyneSystems 0:ab51d784ef36 136 lastTime = currentTime;
AstrodyneSystems 0:ab51d784ef36 137 led2 = !led2; // slow blink led
AstrodyneSystems 0:ab51d784ef36 138 }
AstrodyneSystems 0:ab51d784ef36 139
AstrodyneSystems 0:ab51d784ef36 140 __disable_irq();
AstrodyneSystems 0:ab51d784ef36 141
AstrodyneSystems 0:ab51d784ef36 142 // Accelerometer
AstrodyneSystems 0:ab51d784ef36 143 if(lsm303.bAccDataAvailable()) {
AstrodyneSystems 0:ab51d784ef36 144 lsm303.getAccData(f);
AstrodyneSystems 0:ab51d784ef36 145 sprintf(str,"%u,2,%.3f,%.3f,%.3f\r\n", time_ms, f[0], f[1], f[2]);
AstrodyneSystems 0:ab51d784ef36 146 log_write(str);
AstrodyneSystems 0:ab51d784ef36 147 }
AstrodyneSystems 0:ab51d784ef36 148
AstrodyneSystems 0:ab51d784ef36 149
AstrodyneSystems 0:ab51d784ef36 150 #ifdef NON_DRDY_MAG
AstrodyneSystems 0:ab51d784ef36 151 // Magnetometer
AstrodyneSystems 0:ab51d784ef36 152 data[0] = lsm303.getMagStatus();
AstrodyneSystems 0:ab51d784ef36 153 pc.printf("MagStatus=%X\r\n", data[0]);
AstrodyneSystems 0:ab51d784ef36 154
AstrodyneSystems 0:ab51d784ef36 155 if(lsm303.bMagDataAvailable()) {
AstrodyneSystems 0:ab51d784ef36 156 lsm303.getMagData(f);
AstrodyneSystems 0:ab51d784ef36 157 pc.printf("MAG: %.3f %.3f %.3f %.0f\r\n", f[0], f[1], f[2], atan2(f[1],f[0])*180.0/PI);
AstrodyneSystems 0:ab51d784ef36 158 }
AstrodyneSystems 0:ab51d784ef36 159 #endif
AstrodyneSystems 0:ab51d784ef36 160 __enable_irq();
AstrodyneSystems 0:ab51d784ef36 161
AstrodyneSystems 0:ab51d784ef36 162 }
AstrodyneSystems 0:ab51d784ef36 163
AstrodyneSystems 0:ab51d784ef36 164 LSM303DLHC_DRDY.fall(NULL);
AstrodyneSystems 0:ab51d784ef36 165 // LSM303DLHC_INT1.rise(NULL);
AstrodyneSystems 0:ab51d784ef36 166 wait(.1);
AstrodyneSystems 0:ab51d784ef36 167 log_close();
AstrodyneSystems 0:ab51d784ef36 168 pc.printf("Done.\r\n");
AstrodyneSystems 0:ab51d784ef36 169 led2=0;
AstrodyneSystems 0:ab51d784ef36 170
AstrodyneSystems 0:ab51d784ef36 171 // Signal program done
AstrodyneSystems 0:ab51d784ef36 172 while(1) {
AstrodyneSystems 0:ab51d784ef36 173 led1 = 1;
AstrodyneSystems 0:ab51d784ef36 174 wait(0.2);
AstrodyneSystems 0:ab51d784ef36 175 led1 = 0;
AstrodyneSystems 0:ab51d784ef36 176 wait(0.2);
AstrodyneSystems 0:ab51d784ef36 177 }
AstrodyneSystems 0:ab51d784ef36 178 }
AstrodyneSystems 0:ab51d784ef36 179
AstrodyneSystems 0:ab51d784ef36 180
AstrodyneSystems 0:ab51d784ef36 181
AstrodyneSystems 0:ab51d784ef36 182 // Magnetometer interrupt service -------------------------------------
AstrodyneSystems 0:ab51d784ef36 183 void LSM303DLHC_DRDY_INTERRUPT(void)
AstrodyneSystems 0:ab51d784ef36 184 {
AstrodyneSystems 0:ab51d784ef36 185 float f[3];
AstrodyneSystems 0:ab51d784ef36 186 char str[80];
AstrodyneSystems 0:ab51d784ef36 187
AstrodyneSystems 0:ab51d784ef36 188 __disable_irq();
AstrodyneSystems 0:ab51d784ef36 189 lsm303.getMagData(f);
AstrodyneSystems 0:ab51d784ef36 190 sprintf(str, "%u,3,%.3f,%.3f,%.3f\r\n", time_ms, f[0], f[1], f[2]);
AstrodyneSystems 0:ab51d784ef36 191 log_write(str);
AstrodyneSystems 0:ab51d784ef36 192 __enable_irq();
AstrodyneSystems 0:ab51d784ef36 193 }
AstrodyneSystems 0:ab51d784ef36 194
AstrodyneSystems 0:ab51d784ef36 195
AstrodyneSystems 0:ab51d784ef36 196 // Accelerometer interrupt service ------------------------------------
AstrodyneSystems 0:ab51d784ef36 197 /*void LSM303DLHC_INT1_INTERRUPT(void)
AstrodyneSystems 0:ab51d784ef36 198 {
AstrodyneSystems 0:ab51d784ef36 199 float f[3];
AstrodyneSystems 0:ab51d784ef36 200 unsigned char src;
AstrodyneSystems 0:ab51d784ef36 201 char str[80];
AstrodyneSystems 0:ab51d784ef36 202
AstrodyneSystems 0:ab51d784ef36 203 __disable_irq();
AstrodyneSystems 0:ab51d784ef36 204
AstrodyneSystems 0:ab51d784ef36 205 src = lsm303.getInt1Src();
AstrodyneSystems 0:ab51d784ef36 206 if(lsm303.bAccDataAvailable()) {
AstrodyneSystems 0:ab51d784ef36 207 lsm303.getAccData(f);
AstrodyneSystems 0:ab51d784ef36 208 sprintf(str,"%u,2,%.3f,%.3f,%.3f,%x\r\n", time_ms, f[0], f[1], f[2], src);
AstrodyneSystems 0:ab51d784ef36 209 log_write(str);
AstrodyneSystems 0:ab51d784ef36 210 if(++accCnt >= accAltDecimate) {
AstrodyneSystems 0:ab51d784ef36 211 accCnt = 0;
AstrodyneSystems 0:ab51d784ef36 212 alt.OST();
AstrodyneSystems 0:ab51d784ef36 213 }
AstrodyneSystems 0:ab51d784ef36 214 }
AstrodyneSystems 0:ab51d784ef36 215
AstrodyneSystems 0:ab51d784ef36 216 __enable_irq();
AstrodyneSystems 0:ab51d784ef36 217 }
AstrodyneSystems 0:ab51d784ef36 218 */