JEK changes enabling proper recording of IMU/GPS datastrams - 02-APR-2013

Dependencies:   mbed

Fork of GPS_Incremental by Dan Matthews

Committer:
jekain314
Date:
Fri Apr 19 16:21:27 2013 +0000
Revision:
9:b45feb91ba38
Parent:
0:c746ee34feae
update to allow better imu gps data collection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dannyman939 0:c746ee34feae 1 /*
dannyman939 0:c746ee34feae 2 Copyright (c) 2011 Andy Kirkham
dannyman939 0:c746ee34feae 3
dannyman939 0:c746ee34feae 4 Permission is hereby granted, free of charge, to any person obtaining a copy
dannyman939 0:c746ee34feae 5 of this software and associated documentation files (the "Software"), to deal
dannyman939 0:c746ee34feae 6 in the Software without restriction, including without limitation the rights
dannyman939 0:c746ee34feae 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
dannyman939 0:c746ee34feae 8 copies of the Software, and to permit persons to whom the Software is
dannyman939 0:c746ee34feae 9 furnished to do so, subject to the following conditions:
dannyman939 0:c746ee34feae 10
dannyman939 0:c746ee34feae 11 The above copyright notice and this permission notice shall be included in
dannyman939 0:c746ee34feae 12 all copies or substantial portions of the Software.
dannyman939 0:c746ee34feae 13
dannyman939 0:c746ee34feae 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dannyman939 0:c746ee34feae 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dannyman939 0:c746ee34feae 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dannyman939 0:c746ee34feae 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dannyman939 0:c746ee34feae 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dannyman939 0:c746ee34feae 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dannyman939 0:c746ee34feae 20 THE SOFTWARE.
dannyman939 0:c746ee34feae 21
dannyman939 0:c746ee34feae 22 @file example2.cpp
dannyman939 0:c746ee34feae 23 @purpose Demos a simple messaging system.
dannyman939 0:c746ee34feae 24 @version see ChangeLog.c
dannyman939 0:c746ee34feae 25 @date Jan 2011
dannyman939 0:c746ee34feae 26 @author Andy Kirkham
dannyman939 0:c746ee34feae 27 */
dannyman939 0:c746ee34feae 28
dannyman939 0:c746ee34feae 29 /*
dannyman939 0:c746ee34feae 30 This example demostrates a simple "messaging" system. You can use it with
dannyman939 0:c746ee34feae 31 a terminal program to test it out or write a cusom C#/C++/VB/etc program
dannyman939 0:c746ee34feae 32 to read and write messages to or from the Mbed. The default baud rate in
dannyman939 0:c746ee34feae 33 this example is 115200.
dannyman939 0:c746ee34feae 34
dannyman939 0:c746ee34feae 35 In this example, the LEDs are controlled and pins p21 to p24 are set as
dannyman939 0:c746ee34feae 36 InterruptIn and send messages out when their value changes.
dannyman939 0:c746ee34feae 37
dannyman939 0:c746ee34feae 38 To use, hook up the MBed USB and open your fav terminal. All messages
dannyman939 0:c746ee34feae 39 end with the \n character, don't forget to hit carriage return!.
dannyman939 0:c746ee34feae 40 As an example:-
dannyman939 0:c746ee34feae 41
dannyman939 0:c746ee34feae 42 to switch on LED1 send LED1:1\n, off is LED1:0\n and toggle is LED1:2\n
dannyman939 0:c746ee34feae 43 to switch on LED2 send LED2:1\n, off is LED2:0\n and toggle is LED2:2\n
dannyman939 0:c746ee34feae 44 to switch on LED3 send LED3:1\n, off is LED3:0\n and toggle is LED3:2\n
dannyman939 0:c746ee34feae 45 to switch on LED4 send LED4:1\n, off is LED4:0\n and toggle is LED4:2\n
dannyman939 0:c746ee34feae 46
dannyman939 0:c746ee34feae 47 When a pin change on p21 to p24 happens, a message is sent. As an example
dannyman939 0:c746ee34feae 48 when p21 goes low PIN21:0\n is sent, when goes high PIN21:1\n is sent.
dannyman939 0:c746ee34feae 49
dannyman939 0:c746ee34feae 50 Note, the InterruptIn pins p21 to p24 are setup to have pullups. This means
dannyman939 0:c746ee34feae 51 they are high. To activate them use a wire to short the pin to 0volts.
dannyman939 0:c746ee34feae 52
dannyman939 0:c746ee34feae 53 If you find that p21 to p24 sent a lot of on/off/on/off then it's probably
dannyman939 0:c746ee34feae 54 due to "bounce". If you are connecting a mechanical switch to a pin you
dannyman939 0:c746ee34feae 55 may prefer to use the PinDetect library rather than using InterruptIn.
dannyman939 0:c746ee34feae 56 @see http://mbed.org/users/AjK/libraries/PinDetect/latest
dannyman939 0:c746ee34feae 57
dannyman939 0:c746ee34feae 58 One point you may notice. Incoming messages are processed via main()'s
dannyman939 0:c746ee34feae 59 while(1) loop whereas pin changes have their messages directly sent.
dannyman939 0:c746ee34feae 60 The reason for this is when MODSERIAL makes callbacks to your application
dannyman939 0:c746ee34feae 61 it is in "interrupt context". And one thing you want to avoid is spending
dannyman939 0:c746ee34feae 62 lots of CPU time in that context. So, the callback moves the message from
dannyman939 0:c746ee34feae 63 the input buffer to a local holding buffer and it then sets a bool flag
dannyman939 0:c746ee34feae 64 which tells main()'s while(1) loop to process that buffer. This means the
dannyman939 0:c746ee34feae 65 time spent doing the real incoming message handing is within your program
dannyman939 0:c746ee34feae 66 and not within MODSERIAL's interrupt context. So you may ask, why not do
dannyman939 0:c746ee34feae 67 the same for out going messages? Well, because MODSERIAL output buffers
dannyman939 0:c746ee34feae 68 all your sent content then sending chars is very fast. MODSERIAL handles
dannyman939 0:c746ee34feae 69 all the nitty gritty bits for you. You can just send. This example uses
dannyman939 0:c746ee34feae 70 puts() to send the message. If you can, always try and use sprintf()+puts()
dannyman939 0:c746ee34feae 71 rathe than printf(), printf() is known to often screw things up when used
dannyman939 0:c746ee34feae 72 within an interrupt context. Better still, just use puts() and do away
dannyman939 0:c746ee34feae 73 with any of the crappy ?printf() calls if possible. But I found the code
dannyman939 0:c746ee34feae 74 below to work fine even at 115200baud.
dannyman939 0:c746ee34feae 75
dannyman939 0:c746ee34feae 76 */
dannyman939 0:c746ee34feae 77
dannyman939 0:c746ee34feae 78
dannyman939 0:c746ee34feae 79 #ifdef COMPILE_EXAMPLE1_CODE_MODSERIAL
dannyman939 0:c746ee34feae 80
dannyman939 0:c746ee34feae 81 #include "mbed.h"
dannyman939 0:c746ee34feae 82 #include "MODSERIAL.h"
dannyman939 0:c746ee34feae 83
dannyman939 0:c746ee34feae 84 #define MESSAGE_BUFFER_SIZE 32
dannyman939 0:c746ee34feae 85
dannyman939 0:c746ee34feae 86 DigitalOut led1(LED1);
dannyman939 0:c746ee34feae 87 DigitalOut led2(LED2);
dannyman939 0:c746ee34feae 88 DigitalOut led3(LED3);
dannyman939 0:c746ee34feae 89 DigitalOut led4(LED4);
dannyman939 0:c746ee34feae 90
dannyman939 0:c746ee34feae 91 InterruptIn P21(p21);
dannyman939 0:c746ee34feae 92 InterruptIn P22(p22);
dannyman939 0:c746ee34feae 93 InterruptIn P23(p23);
dannyman939 0:c746ee34feae 94 InterruptIn P24(p24);
dannyman939 0:c746ee34feae 95
dannyman939 0:c746ee34feae 96 MODSERIAL messageSystem(USBTX, USBRX);
dannyman939 0:c746ee34feae 97
dannyman939 0:c746ee34feae 98 char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
dannyman939 0:c746ee34feae 99 char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
dannyman939 0:c746ee34feae 100 bool messageReceived;
dannyman939 0:c746ee34feae 101
dannyman939 0:c746ee34feae 102 void messageReceive(MODSERIAL_IRQ_INFO *q) {
dannyman939 0:c746ee34feae 103 MODSERIAL *sys = q->serial;
dannyman939 0:c746ee34feae 104 sys->move(messageBufferIncoming, MESSAGE_BUFFER_SIZE);
dannyman939 0:c746ee34feae 105 messageReceived = true;
dannyman939 0:c746ee34feae 106 return 0;
dannyman939 0:c746ee34feae 107 }
dannyman939 0:c746ee34feae 108
dannyman939 0:c746ee34feae 109 void messageProcess(void) {
dannyman939 0:c746ee34feae 110 if (!strncmp(messageBufferIncoming, "LED1:1", sizeof("LED1:1")-1)) led1 = 1;
dannyman939 0:c746ee34feae 111 else if (!strncmp(messageBufferIncoming, "LED1:0", sizeof("LED1:0")-1)) led1 = 0;
dannyman939 0:c746ee34feae 112 else if (!strncmp(messageBufferIncoming, "LED1:2", sizeof("LED1:2")-1)) led1 = !led1;
dannyman939 0:c746ee34feae 113
dannyman939 0:c746ee34feae 114 else if (!strncmp(messageBufferIncoming, "LED2:1", sizeof("LED2:1")-1)) led2 = 1;
dannyman939 0:c746ee34feae 115 else if (!strncmp(messageBufferIncoming, "LED2:0", sizeof("LED2:0")-1)) led2 = 0;
dannyman939 0:c746ee34feae 116 else if (!strncmp(messageBufferIncoming, "LED2:2", sizeof("LED2:2")-1)) led2 = !led2;
dannyman939 0:c746ee34feae 117
dannyman939 0:c746ee34feae 118 else if (!strncmp(messageBufferIncoming, "LED3:1", sizeof("LED3:1")-1)) led3 = 1;
dannyman939 0:c746ee34feae 119 else if (!strncmp(messageBufferIncoming, "LED3:0", sizeof("LED3:0")-1)) led3 = 0;
dannyman939 0:c746ee34feae 120 else if (!strncmp(messageBufferIncoming, "LED3:2", sizeof("LED3:2")-1)) led3 = !led3;
dannyman939 0:c746ee34feae 121
dannyman939 0:c746ee34feae 122 else if (!strncmp(messageBufferIncoming, "LED4:1", sizeof("LED4:1")-1)) led4 = 1;
dannyman939 0:c746ee34feae 123 else if (!strncmp(messageBufferIncoming, "LED4:0", sizeof("LED4:0")-1)) led4 = 0;
dannyman939 0:c746ee34feae 124 else if (!strncmp(messageBufferIncoming, "LED4:2", sizeof("LED4:2")-1)) led4 = !led4;
dannyman939 0:c746ee34feae 125
dannyman939 0:c746ee34feae 126 messageReceived = false;
dannyman939 0:c746ee34feae 127 }
dannyman939 0:c746ee34feae 128
dannyman939 0:c746ee34feae 129 #define PIN_MESSAGE_SEND(x,y) \
dannyman939 0:c746ee34feae 130 sprintf(messageBufferOutgoing,"PIN%02d:%d\n",x,y);\
dannyman939 0:c746ee34feae 131 messageSystem.puts(messageBufferOutgoing);
dannyman939 0:c746ee34feae 132
dannyman939 0:c746ee34feae 133 void pin21Rise(void) { PIN_MESSAGE_SEND(21, 1); }
dannyman939 0:c746ee34feae 134 void pin21Fall(void) { PIN_MESSAGE_SEND(21, 0); }
dannyman939 0:c746ee34feae 135 void pin22Rise(void) { PIN_MESSAGE_SEND(22, 1); }
dannyman939 0:c746ee34feae 136 void pin22Fall(void) { PIN_MESSAGE_SEND(22, 0); }
dannyman939 0:c746ee34feae 137 void pin23Rise(void) { PIN_MESSAGE_SEND(23, 1); }
dannyman939 0:c746ee34feae 138 void pin23Fall(void) { PIN_MESSAGE_SEND(23, 0); }
dannyman939 0:c746ee34feae 139 void pin24Rise(void) { PIN_MESSAGE_SEND(24, 1); }
dannyman939 0:c746ee34feae 140 void pin24Fall(void) { PIN_MESSAGE_SEND(24, 0); }
dannyman939 0:c746ee34feae 141
dannyman939 0:c746ee34feae 142 int main() {
dannyman939 0:c746ee34feae 143
dannyman939 0:c746ee34feae 144 messageReceived = false;
dannyman939 0:c746ee34feae 145 messageSystem.baud(115200);
dannyman939 0:c746ee34feae 146 messageSystem.attach(&messageReceive, MODSERIAL::RxAutoDetect);
dannyman939 0:c746ee34feae 147 messageSystem.autoDetectChar('\n');
dannyman939 0:c746ee34feae 148
dannyman939 0:c746ee34feae 149 // Enable pullup resistors on pins.
dannyman939 0:c746ee34feae 150 P21.mode(PullUp); P22.mode(PullUp); P23.mode(PullUp); P24.mode(PullUp);
dannyman939 0:c746ee34feae 151
dannyman939 0:c746ee34feae 152 // Fix Mbed library bug, see http://mbed.org/forum/bugs-suggestions/topic/1498
dannyman939 0:c746ee34feae 153 LPC_GPIOINT->IO2IntClr = (1UL << 5) | (1UL << 4) | (1UL << 3) | (1UL << 2);
dannyman939 0:c746ee34feae 154
dannyman939 0:c746ee34feae 155 // Attach InterruptIn pin callbacks.
dannyman939 0:c746ee34feae 156 P21.rise(&pin21Rise); P21.fall(&pin21Fall);
dannyman939 0:c746ee34feae 157 P22.rise(&pin22Rise); P22.fall(&pin22Fall);
dannyman939 0:c746ee34feae 158 P23.rise(&pin23Rise); P23.fall(&pin23Fall);
dannyman939 0:c746ee34feae 159 P24.rise(&pin24Rise); P24.fall(&pin24Fall);
dannyman939 0:c746ee34feae 160
dannyman939 0:c746ee34feae 161 while(1) {
dannyman939 0:c746ee34feae 162 // Process incoming messages.
dannyman939 0:c746ee34feae 163 if (messageReceived) messageProcess();
dannyman939 0:c746ee34feae 164 }
dannyman939 0:c746ee34feae 165 }
dannyman939 0:c746ee34feae 166
dannyman939 0:c746ee34feae 167 #endif