Alex Whittemore
/
BURTDAQ
Program to run data acquisition and control for BU Rocket Team's MKII hybrid rocket motor
main.cpp@0:7fd45d2b5926, 2012-04-09 (annotated)
- Committer:
- alexwhittemore
- Date:
- Mon Apr 09 16:39:50 2012 +0000
- Revision:
- 0:7fd45d2b5926
For some reason, this thing wont run serial interrupts and threads simultaneously without locking up.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alexwhittemore | 0:7fd45d2b5926 | 1 | #include "mbed.h" |
alexwhittemore | 0:7fd45d2b5926 | 2 | #include "rtos.h" |
alexwhittemore | 0:7fd45d2b5926 | 3 | #include "burtdaq.h" |
alexwhittemore | 0:7fd45d2b5926 | 4 | #include <vector> |
alexwhittemore | 0:7fd45d2b5926 | 5 | #include "FastIO.h" |
alexwhittemore | 0:7fd45d2b5926 | 6 | #include <stdio.h> // printf |
alexwhittemore | 0:7fd45d2b5926 | 7 | #include <string.h> // strcat |
alexwhittemore | 0:7fd45d2b5926 | 8 | #include <stdlib.h> // strtol |
alexwhittemore | 0:7fd45d2b5926 | 9 | |
alexwhittemore | 0:7fd45d2b5926 | 10 | DigitalOut led1(LED1); |
alexwhittemore | 0:7fd45d2b5926 | 11 | DigitalOut led2(LED2); |
alexwhittemore | 0:7fd45d2b5926 | 12 | DigitalOut led3(LED3); |
alexwhittemore | 0:7fd45d2b5926 | 13 | DigitalOut led4(LED4); |
alexwhittemore | 0:7fd45d2b5926 | 14 | //Serial pc(USBTX,USBRX); |
alexwhittemore | 0:7fd45d2b5926 | 15 | Serial xbee(XBEETX,XBEERX); |
alexwhittemore | 0:7fd45d2b5926 | 16 | |
alexwhittemore | 0:7fd45d2b5926 | 17 | //Queue<uint8_t, 50> queue; |
alexwhittemore | 0:7fd45d2b5926 | 18 | |
alexwhittemore | 0:7fd45d2b5926 | 19 | #define kDisplayWidth 32 |
alexwhittemore | 0:7fd45d2b5926 | 20 | char* pBinFill(long int x,char *so, char fillChar) |
alexwhittemore | 0:7fd45d2b5926 | 21 | { // fill in array from right to left |
alexwhittemore | 0:7fd45d2b5926 | 22 | char s[kDisplayWidth+1]; |
alexwhittemore | 0:7fd45d2b5926 | 23 | int i=kDisplayWidth; |
alexwhittemore | 0:7fd45d2b5926 | 24 | s[i--]=0x00; // terminate string |
alexwhittemore | 0:7fd45d2b5926 | 25 | do |
alexwhittemore | 0:7fd45d2b5926 | 26 | { // fill in array from right to left |
alexwhittemore | 0:7fd45d2b5926 | 27 | s[i--]=(x & 1) ? '1':'0'; |
alexwhittemore | 0:7fd45d2b5926 | 28 | x>>=1; // shift right 1 bit |
alexwhittemore | 0:7fd45d2b5926 | 29 | } while( x > 0); |
alexwhittemore | 0:7fd45d2b5926 | 30 | while(i>=0) s[i--]=fillChar; // fill with fillChar |
alexwhittemore | 0:7fd45d2b5926 | 31 | sprintf(so,"%s",s); |
alexwhittemore | 0:7fd45d2b5926 | 32 | return so; |
alexwhittemore | 0:7fd45d2b5926 | 33 | } |
alexwhittemore | 0:7fd45d2b5926 | 34 | |
alexwhittemore | 0:7fd45d2b5926 | 35 | |
alexwhittemore | 0:7fd45d2b5926 | 36 | void bridgeReader(void const *argument) { |
alexwhittemore | 0:7fd45d2b5926 | 37 | SPI spia(NC, SPIA_MISO, SPIA_CLK); |
alexwhittemore | 0:7fd45d2b5926 | 38 | spia.format(16); |
alexwhittemore | 0:7fd45d2b5926 | 39 | spia.frequency(1000000); |
alexwhittemore | 0:7fd45d2b5926 | 40 | //DigitalOut spia_mosi(SPIA_MOSI); |
alexwhittemore | 0:7fd45d2b5926 | 41 | FastOut<SPIA_MOSI> spia_mosi; |
alexwhittemore | 0:7fd45d2b5926 | 42 | |
alexwhittemore | 0:7fd45d2b5926 | 43 | FastPortOut<Port0, SPIA_CS_PORT_MASK> spiaCSPort; |
alexwhittemore | 0:7fd45d2b5926 | 44 | //PortOut spiaCSPort(Port0, SPIA_CS_PORT_MASK); |
alexwhittemore | 0:7fd45d2b5926 | 45 | unsigned int bridgeCSPinMasks[5] = {SPIA_P1_CS_PORT_MASK, SPIA_P2_CS_PORT_MASK, SPIA_P3_CS_PORT_MASK, SPIA_P4_CS_PORT_MASK}; |
alexwhittemore | 0:7fd45d2b5926 | 46 | int deviceCount = 4; |
alexwhittemore | 0:7fd45d2b5926 | 47 | |
alexwhittemore | 0:7fd45d2b5926 | 48 | spiaCSPort = SPIA_CS_PORT_MASK; |
alexwhittemore | 0:7fd45d2b5926 | 49 | |
alexwhittemore | 0:7fd45d2b5926 | 50 | uint16_t outputs[4] = {0x0f0f, 0xf0f0, 0x0000, 0x0000}; |
alexwhittemore | 0:7fd45d2b5926 | 51 | //bridgeCSPins = 3; |
alexwhittemore | 0:7fd45d2b5926 | 52 | printf("ENTERING READ LOOP\n"); |
alexwhittemore | 0:7fd45d2b5926 | 53 | while(true) { |
alexwhittemore | 0:7fd45d2b5926 | 54 | __disable_irq(); |
alexwhittemore | 0:7fd45d2b5926 | 55 | led2 = 1; |
alexwhittemore | 0:7fd45d2b5926 | 56 | // READ BRIDGES |
alexwhittemore | 0:7fd45d2b5926 | 57 | spia_mosi = 1; |
alexwhittemore | 0:7fd45d2b5926 | 58 | int i = 0; // This constitutes ROUGHLY a 500ns delay |
alexwhittemore | 0:7fd45d2b5926 | 59 | while(i<10) |
alexwhittemore | 0:7fd45d2b5926 | 60 | i++; |
alexwhittemore | 0:7fd45d2b5926 | 61 | for(i=0; i<deviceCount; i++){ |
alexwhittemore | 0:7fd45d2b5926 | 62 | spiaCSPort = ~bridgeCSPinMasks[i]; |
alexwhittemore | 0:7fd45d2b5926 | 63 | outputs[i] = spia.write(0x0); |
alexwhittemore | 0:7fd45d2b5926 | 64 | } |
alexwhittemore | 0:7fd45d2b5926 | 65 | spiaCSPort = SPIA_CS_PORT_MASK; |
alexwhittemore | 0:7fd45d2b5926 | 66 | //spiaCSPort = 1; |
alexwhittemore | 0:7fd45d2b5926 | 67 | spia_mosi = 0; |
alexwhittemore | 0:7fd45d2b5926 | 68 | // END READING |
alexwhittemore | 0:7fd45d2b5926 | 69 | |
alexwhittemore | 0:7fd45d2b5926 | 70 | printf("%u, %u, %u, %u\n",outputs[0],outputs[1],outputs[2],outputs[3]); |
alexwhittemore | 0:7fd45d2b5926 | 71 | led2 = 0; |
alexwhittemore | 0:7fd45d2b5926 | 72 | __enable_irq(); |
alexwhittemore | 0:7fd45d2b5926 | 73 | Thread::wait(5000); |
alexwhittemore | 0:7fd45d2b5926 | 74 | } |
alexwhittemore | 0:7fd45d2b5926 | 75 | } |
alexwhittemore | 0:7fd45d2b5926 | 76 | void tcReader(void const *argument) { |
alexwhittemore | 0:7fd45d2b5926 | 77 | SPI spib(SPIB_MOSI, SPIB_MISO, SPIB_CLK); |
alexwhittemore | 0:7fd45d2b5926 | 78 | spib.format(16); |
alexwhittemore | 0:7fd45d2b5926 | 79 | spib.frequency(1000000); |
alexwhittemore | 0:7fd45d2b5926 | 80 | |
alexwhittemore | 0:7fd45d2b5926 | 81 | DigitalOut spibCSPins[] = {SPIB_P1_CS, SPIB_P2_CS, SPIB_P3_CS}; |
alexwhittemore | 0:7fd45d2b5926 | 82 | spibCSPins[0]=1; |
alexwhittemore | 0:7fd45d2b5926 | 83 | spibCSPins[1]=1; |
alexwhittemore | 0:7fd45d2b5926 | 84 | spibCSPins[2]=1; |
alexwhittemore | 0:7fd45d2b5926 | 85 | |
alexwhittemore | 0:7fd45d2b5926 | 86 | int numTCs = 3; |
alexwhittemore | 0:7fd45d2b5926 | 87 | uint32_t rawTcData[3] = {0,0,0}; |
alexwhittemore | 0:7fd45d2b5926 | 88 | uint32_t tcData[3] = {0,0,0}; |
alexwhittemore | 0:7fd45d2b5926 | 89 | uint32_t ambientData[3] = {0,0,0}; |
alexwhittemore | 0:7fd45d2b5926 | 90 | |
alexwhittemore | 0:7fd45d2b5926 | 91 | float humanTcData[3] = {0,0,0}; |
alexwhittemore | 0:7fd45d2b5926 | 92 | float humanAmbientData[3] = {0,0,0}; |
alexwhittemore | 0:7fd45d2b5926 | 93 | |
alexwhittemore | 0:7fd45d2b5926 | 94 | uint32_t buffer = 0; |
alexwhittemore | 0:7fd45d2b5926 | 95 | char so[kDisplayWidth+1]; |
alexwhittemore | 0:7fd45d2b5926 | 96 | while (true) { |
alexwhittemore | 0:7fd45d2b5926 | 97 | __disable_irq(); |
alexwhittemore | 0:7fd45d2b5926 | 98 | led3 = 1; |
alexwhittemore | 0:7fd45d2b5926 | 99 | for(int i=0; i<numTCs; i++){ |
alexwhittemore | 0:7fd45d2b5926 | 100 | rawTcData[i] = 0; |
alexwhittemore | 0:7fd45d2b5926 | 101 | spibCSPins[i] = 0; |
alexwhittemore | 0:7fd45d2b5926 | 102 | buffer = spib.write(0); |
alexwhittemore | 0:7fd45d2b5926 | 103 | //printf("%s\n",binary_fmt(buffer,tmp)); |
alexwhittemore | 0:7fd45d2b5926 | 104 | rawTcData[i] = (buffer << 16); |
alexwhittemore | 0:7fd45d2b5926 | 105 | buffer = spib.write(0); |
alexwhittemore | 0:7fd45d2b5926 | 106 | rawTcData[i] |= (buffer); |
alexwhittemore | 0:7fd45d2b5926 | 107 | spibCSPins[i] = 1; |
alexwhittemore | 0:7fd45d2b5926 | 108 | } |
alexwhittemore | 0:7fd45d2b5926 | 109 | //printf("%s %s %s\n", byte_to_binary(tcData[0]), byte_to_binary(tcData[1]), byte_to_binary(tcData[2])); |
alexwhittemore | 0:7fd45d2b5926 | 110 | //printf("Data is: %s %s %s\n",pBinFill(tcData[0], so, '0'), pBinFill(tcData[1], so, '0'), pBinFill(tcData[2], so, '0')); |
alexwhittemore | 0:7fd45d2b5926 | 111 | |
alexwhittemore | 0:7fd45d2b5926 | 112 | for(int i=0; i<numTCs; i++){ |
alexwhittemore | 0:7fd45d2b5926 | 113 | tcData[i] = ((rawTcData[i] & 0xFFFC0000) >> 18); |
alexwhittemore | 0:7fd45d2b5926 | 114 | ambientData[i] = ((rawTcData[i] & 0xFFF0) >> 4); |
alexwhittemore | 0:7fd45d2b5926 | 115 | humanTcData[i] = (float) tcData[i] * .25; |
alexwhittemore | 0:7fd45d2b5926 | 116 | humanAmbientData[i] = (float) ambientData[i]*.0625; |
alexwhittemore | 0:7fd45d2b5926 | 117 | printf("Thermocouple %d ambient: %f, tc:%f\n",i,humanAmbientData[i],humanTcData[i]); |
alexwhittemore | 0:7fd45d2b5926 | 118 | } |
alexwhittemore | 0:7fd45d2b5926 | 119 | led3=0; |
alexwhittemore | 0:7fd45d2b5926 | 120 | __enable_irq(); |
alexwhittemore | 0:7fd45d2b5926 | 121 | Thread::wait(2000); |
alexwhittemore | 0:7fd45d2b5926 | 122 | } |
alexwhittemore | 0:7fd45d2b5926 | 123 | } |
alexwhittemore | 0:7fd45d2b5926 | 124 | |
alexwhittemore | 0:7fd45d2b5926 | 125 | void xbeeISR() { |
alexwhittemore | 0:7fd45d2b5926 | 126 | led4 = !led4; |
alexwhittemore | 0:7fd45d2b5926 | 127 | } |
alexwhittemore | 0:7fd45d2b5926 | 128 | |
alexwhittemore | 0:7fd45d2b5926 | 129 | int main() { |
alexwhittemore | 0:7fd45d2b5926 | 130 | printf("Starting reader\n"); |
alexwhittemore | 0:7fd45d2b5926 | 131 | Thread bridgeReader1(bridgeReader); |
alexwhittemore | 0:7fd45d2b5926 | 132 | Thread tcReader1(tcReader); |
alexwhittemore | 0:7fd45d2b5926 | 133 | //Thread actuatorController1(actuatorController); |
alexwhittemore | 0:7fd45d2b5926 | 134 | |
alexwhittemore | 0:7fd45d2b5926 | 135 | xbee.baud(115200); |
alexwhittemore | 0:7fd45d2b5926 | 136 | xbee.attach(&xbeeISR); |
alexwhittemore | 0:7fd45d2b5926 | 137 | int i = 0; |
alexwhittemore | 0:7fd45d2b5926 | 138 | while(1){ |
alexwhittemore | 0:7fd45d2b5926 | 139 | //xbee.printf("In Range: %d",i); |
alexwhittemore | 0:7fd45d2b5926 | 140 | i++; |
alexwhittemore | 0:7fd45d2b5926 | 141 | led1 = !led1; |
alexwhittemore | 0:7fd45d2b5926 | 142 | Thread::wait(1000); |
alexwhittemore | 0:7fd45d2b5926 | 143 | } |
alexwhittemore | 0:7fd45d2b5926 | 144 | } |