Code for our FYDP -only one IMU works right now -RTOS is working

Dependencies:   mbed

Committer:
majik
Date:
Wed Mar 18 22:23:48 2015 +0000
Revision:
0:964eb6a2ef00
This is our FYDP code, but only one IMU works with the RTOS.

Who changed what in which revision?

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