Upload to Component Test
Dependencies: BridgeDriver_NA FrontPanelButtons MCP23017 TextLCD mbed
main.cpp@0:1d73f030c262, 2015-01-20 (annotated)
- Committer:
- mehatfie
- Date:
- Tue Jan 20 04:21:28 2015 +0000
- Revision:
- 0:1d73f030c262
-Upload to Component Test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mehatfie | 0:1d73f030c262 | 1 | #include "mbed.h" |
mehatfie | 0:1d73f030c262 | 2 | #include "LocalPinNames.h" |
mehatfie | 0:1d73f030c262 | 3 | #include "BridgeDriver.h" |
mehatfie | 0:1d73f030c262 | 4 | #include "FrontPanelButtons.h" |
mehatfie | 0:1d73f030c262 | 5 | #include "TextLCD.h" |
mehatfie | 0:1d73f030c262 | 6 | |
mehatfie | 0:1d73f030c262 | 7 | I2C i2c( P0_10, P0_11 ); // I2C bus (SDA, SCL) |
mehatfie | 0:1d73f030c262 | 8 | BridgeDriver bridges( &i2c ); // bridge |
mehatfie | 0:1d73f030c262 | 9 | TextLCD_I2C lcd( &i2c, MCP23008_SA0, TextLCD::LCD20x4 ); // LCD |
mehatfie | 0:1d73f030c262 | 10 | FrontPanelButtons buttons( &i2c ); // front panel buttons |
mehatfie | 0:1d73f030c262 | 11 | |
mehatfie | 0:1d73f030c262 | 12 | DigitalIn TDC(DIO0, PullDown); |
mehatfie | 0:1d73f030c262 | 13 | AnalogIn signal(AI0); |
mehatfie | 0:1d73f030c262 | 14 | |
mehatfie | 0:1d73f030c262 | 15 | Ticker ErrorWatch; |
mehatfie | 0:1d73f030c262 | 16 | Timer timer; //initialize a timer |
mehatfie | 0:1d73f030c262 | 17 | Timer avgCycTimer; |
mehatfie | 0:1d73f030c262 | 18 | |
mehatfie | 0:1d73f030c262 | 19 | float totaltime = 0; |
mehatfie | 0:1d73f030c262 | 20 | |
mehatfie | 0:1d73f030c262 | 21 | SPI spi(P0_9, P0_8, P0_7); // mosi(out), miso(in), sclk(clock) |
mehatfie | 0:1d73f030c262 | 22 | DigitalOut cs_Current(P1_0); // cs (the chip select signal) |
mehatfie | 0:1d73f030c262 | 23 | |
mehatfie | 0:1d73f030c262 | 24 | float stateDir = 0; |
mehatfie | 0:1d73f030c262 | 25 | int numCycles = 200000; |
mehatfie | 0:1d73f030c262 | 26 | int cycleCount = 1; |
mehatfie | 0:1d73f030c262 | 27 | |
mehatfie | 0:1d73f030c262 | 28 | void errorCheck(){ |
mehatfie | 0:1d73f030c262 | 29 | |
mehatfie | 0:1d73f030c262 | 30 | if (timer.read() > 3.0){ |
mehatfie | 0:1d73f030c262 | 31 | bridges.drive( BridgeDriver::MOTOR_B, 0.0 ); |
mehatfie | 0:1d73f030c262 | 32 | |
mehatfie | 0:1d73f030c262 | 33 | lcd.cls(); //clear the display |
mehatfie | 0:1d73f030c262 | 34 | lcd.setAddress ( 0, 0 ); |
mehatfie | 0:1d73f030c262 | 35 | lcd.printf( "<Error Occurred>" ); |
mehatfie | 0:1d73f030c262 | 36 | lcd.setAddress ( 0, 1 ); |
mehatfie | 0:1d73f030c262 | 37 | lcd.printf( "Timeout, transition greater than 3 seconds" ); |
mehatfie | 0:1d73f030c262 | 38 | lcd.setAddress ( 0, 3 ); |
mehatfie | 0:1d73f030c262 | 39 | lcd.printf( "<Press Sel to Resume" ); |
mehatfie | 0:1d73f030c262 | 40 | while (!buttons.readSel()); |
mehatfie | 0:1d73f030c262 | 41 | lcd.cls(); //clear the display |
mehatfie | 0:1d73f030c262 | 42 | |
mehatfie | 0:1d73f030c262 | 43 | lcd.setAddress( 0, 0 ); |
mehatfie | 0:1d73f030c262 | 44 | lcd.printf( "Cycle %d/%3d", cycleCount, numCycles ); |
mehatfie | 0:1d73f030c262 | 45 | lcd.setAddress( 0, 1 ); |
mehatfie | 0:1d73f030c262 | 46 | lcd.printf( "Avg t(sec): %1.3f", (totaltime / cycleCount)); |
mehatfie | 0:1d73f030c262 | 47 | |
mehatfie | 0:1d73f030c262 | 48 | timer.reset(); |
mehatfie | 0:1d73f030c262 | 49 | timer.start(); |
mehatfie | 0:1d73f030c262 | 50 | |
mehatfie | 0:1d73f030c262 | 51 | bridges.drive( BridgeDriver::MOTOR_B, stateDir ); |
mehatfie | 0:1d73f030c262 | 52 | } |
mehatfie | 0:1d73f030c262 | 53 | } |
mehatfie | 0:1d73f030c262 | 54 | |
mehatfie | 0:1d73f030c262 | 55 | |
mehatfie | 0:1d73f030c262 | 56 | |
mehatfie | 0:1d73f030c262 | 57 | float calcCurrent(int channel){ |
mehatfie | 0:1d73f030c262 | 58 | |
mehatfie | 0:1d73f030c262 | 59 | // Select the device by seting chip select low |
mehatfie | 0:1d73f030c262 | 60 | cs_Current = 0; |
mehatfie | 0:1d73f030c262 | 61 | |
mehatfie | 0:1d73f030c262 | 62 | // sending the 6 bits + 1 bit to ignore the null bit |
mehatfie | 0:1d73f030c262 | 63 | // coming from the device, so the data that is sent is 1100000 |
mehatfie | 0:1d73f030c262 | 64 | // get data for channel 0 |
mehatfie | 0:1d73f030c262 | 65 | //spi.write(0x68); // 1101000 |
mehatfie | 0:1d73f030c262 | 66 | |
mehatfie | 0:1d73f030c262 | 67 | // create the message to send in order to get the appropriate data transmitted from the ADC |
mehatfie | 0:1d73f030c262 | 68 | unsigned char channelBits = (channel-1) << 2; // shift the channel bits into there position |
mehatfie | 0:1d73f030c262 | 69 | unsigned char msg = 96 + channelBits; |
mehatfie | 0:1d73f030c262 | 70 | spi.write(msg); |
mehatfie | 0:1d73f030c262 | 71 | |
mehatfie | 0:1d73f030c262 | 72 | // now the device sends back the readings 12 bits, 7 bits at a time |
mehatfie | 0:1d73f030c262 | 73 | uint8_t high = spi.write(0x00); |
mehatfie | 0:1d73f030c262 | 74 | uint8_t low = spi.write(0x00); |
mehatfie | 0:1d73f030c262 | 75 | |
mehatfie | 0:1d73f030c262 | 76 | // shift out the right bits |
mehatfie | 0:1d73f030c262 | 77 | low = ( high << 5 ) | (low >> 2); |
mehatfie | 0:1d73f030c262 | 78 | high = high >> 3; |
mehatfie | 0:1d73f030c262 | 79 | |
mehatfie | 0:1d73f030c262 | 80 | |
mehatfie | 0:1d73f030c262 | 81 | float DigOutVal = ( high << 8 ) | low; // shift and or the result together |
mehatfie | 0:1d73f030c262 | 82 | |
mehatfie | 0:1d73f030c262 | 83 | cs_Current = 1; // Deselect the device |
mehatfie | 0:1d73f030c262 | 84 | |
mehatfie | 0:1d73f030c262 | 85 | //return current; |
mehatfie | 0:1d73f030c262 | 86 | return DigOutVal; |
mehatfie | 0:1d73f030c262 | 87 | } |
mehatfie | 0:1d73f030c262 | 88 | |
mehatfie | 0:1d73f030c262 | 89 | int main() { |
mehatfie | 0:1d73f030c262 | 90 | |
mehatfie | 0:1d73f030c262 | 91 | //initialize Ticker |
mehatfie | 0:1d73f030c262 | 92 | ErrorWatch.attach(&errorCheck, 3); |
mehatfie | 0:1d73f030c262 | 93 | |
mehatfie | 0:1d73f030c262 | 94 | // Setup the spi for 7 bit data, high steady state clock, |
mehatfie | 0:1d73f030c262 | 95 | // second edge capture, with a 1MHz clock rate |
mehatfie | 0:1d73f030c262 | 96 | spi.format(7,0); |
mehatfie | 0:1d73f030c262 | 97 | spi.frequency(1000000); |
mehatfie | 0:1d73f030c262 | 98 | |
mehatfie | 0:1d73f030c262 | 99 | i2c.frequency(1000000); |
mehatfie | 0:1d73f030c262 | 100 | lcd.setBacklight(TextLCD::LightOn); |
mehatfie | 0:1d73f030c262 | 101 | wait(.6); |
mehatfie | 0:1d73f030c262 | 102 | lcd.cls(); //clear the display |
mehatfie | 0:1d73f030c262 | 103 | lcd.setAddress(0,0); //set cursor to 0,0 |
mehatfie | 0:1d73f030c262 | 104 | |
mehatfie | 0:1d73f030c262 | 105 | int count = 0; |
mehatfie | 0:1d73f030c262 | 106 | |
mehatfie | 0:1d73f030c262 | 107 | bridges.enablePwm ( 0, 1, 0, 0 ); |
mehatfie | 0:1d73f030c262 | 108 | bridges.drive( BridgeDriver::MOTOR_B, 0.0 ); |
mehatfie | 0:1d73f030c262 | 109 | |
mehatfie | 0:1d73f030c262 | 110 | lcd.cls(); //clear the display |
mehatfie | 0:1d73f030c262 | 111 | lcd.setAddress ( 0, 2 ); |
mehatfie | 0:1d73f030c262 | 112 | lcd.printf( "<Press Sel to start>" ); |
mehatfie | 0:1d73f030c262 | 113 | |
mehatfie | 0:1d73f030c262 | 114 | while (!buttons.readSel()){ |
mehatfie | 0:1d73f030c262 | 115 | if(buttons.readUp() && numCycles < 999999 ){ |
mehatfie | 0:1d73f030c262 | 116 | numCycles += 500; |
mehatfie | 0:1d73f030c262 | 117 | wait(0.2); //so that the speed of changing the numbers is more controllable, should mean you can move 20 digits per second |
mehatfie | 0:1d73f030c262 | 118 | } |
mehatfie | 0:1d73f030c262 | 119 | else if (buttons.readDown() && numCycles > 0 ){ |
mehatfie | 0:1d73f030c262 | 120 | numCycles -= 500; |
mehatfie | 0:1d73f030c262 | 121 | wait(0.2); //so that the speed of changing the numbers is more controllable, should mean you can move 20 digits per second |
mehatfie | 0:1d73f030c262 | 122 | } |
mehatfie | 0:1d73f030c262 | 123 | lcd.setAddress ( 0, 0 ); |
mehatfie | 0:1d73f030c262 | 124 | lcd.printf( "<Cycles: %d >" , numCycles ); |
mehatfie | 0:1d73f030c262 | 125 | } |
mehatfie | 0:1d73f030c262 | 126 | |
mehatfie | 0:1d73f030c262 | 127 | bridges.drive( 2, 1 ); //turn on the hall sensor voltage |
mehatfie | 0:1d73f030c262 | 128 | |
mehatfie | 0:1d73f030c262 | 129 | int hallCount = 109; |
mehatfie | 0:1d73f030c262 | 130 | |
mehatfie | 0:1d73f030c262 | 131 | //Initialize the spoiler by rotating to stall currents and finding how many ticks the hall sensor takes through the rotation |
mehatfie | 0:1d73f030c262 | 132 | bridges.drive( BridgeDriver::MOTOR_B, -1.0 ); |
mehatfie | 0:1d73f030c262 | 133 | while (calcCurrent(4) < 2200); |
mehatfie | 0:1d73f030c262 | 134 | bridges.drive( BridgeDriver::MOTOR_B, 0.0 ); |
mehatfie | 0:1d73f030c262 | 135 | // wait(1); |
mehatfie | 0:1d73f030c262 | 136 | // bridges.drive( BridgeDriver::MOTOR_B, 1.0 ); |
mehatfie | 0:1d73f030c262 | 137 | // wait(1); |
mehatfie | 0:1d73f030c262 | 138 | // |
mehatfie | 0:1d73f030c262 | 139 | // int flag = 0; |
mehatfie | 0:1d73f030c262 | 140 | // while (calcCurrent(3) < 2200){ |
mehatfie | 0:1d73f030c262 | 141 | // if(signal.read() >= 0.8 && flag == 0){ |
mehatfie | 0:1d73f030c262 | 142 | // flag = 1; |
mehatfie | 0:1d73f030c262 | 143 | // hallCount++; |
mehatfie | 0:1d73f030c262 | 144 | // } |
mehatfie | 0:1d73f030c262 | 145 | // else if (signal.read() < 0.8 && flag == 1) |
mehatfie | 0:1d73f030c262 | 146 | // flag = 0; |
mehatfie | 0:1d73f030c262 | 147 | // } |
mehatfie | 0:1d73f030c262 | 148 | |
mehatfie | 0:1d73f030c262 | 149 | bridges.drive( BridgeDriver::MOTOR_B, 0.0 ); |
mehatfie | 0:1d73f030c262 | 150 | wait(3); |
mehatfie | 0:1d73f030c262 | 151 | |
mehatfie | 0:1d73f030c262 | 152 | |
mehatfie | 0:1d73f030c262 | 153 | lcd.cls(); //clear the display |
mehatfie | 0:1d73f030c262 | 154 | |
mehatfie | 0:1d73f030c262 | 155 | while(cycleCount <= numCycles){ |
mehatfie | 0:1d73f030c262 | 156 | |
mehatfie | 0:1d73f030c262 | 157 | avgCycTimer.reset(); |
mehatfie | 0:1d73f030c262 | 158 | avgCycTimer.start(); |
mehatfie | 0:1d73f030c262 | 159 | |
mehatfie | 0:1d73f030c262 | 160 | lcd.setAddress( 0, 0 ); |
mehatfie | 0:1d73f030c262 | 161 | lcd.printf( "Cycle %d/%3d", cycleCount, numCycles ); |
mehatfie | 0:1d73f030c262 | 162 | |
mehatfie | 0:1d73f030c262 | 163 | stateDir = 1; |
mehatfie | 0:1d73f030c262 | 164 | bridges.drive( BridgeDriver::MOTOR_B, 1.0 ); |
mehatfie | 0:1d73f030c262 | 165 | timer.reset(); |
mehatfie | 0:1d73f030c262 | 166 | timer.start(); |
mehatfie | 0:1d73f030c262 | 167 | |
mehatfie | 0:1d73f030c262 | 168 | int flag = 0; |
mehatfie | 0:1d73f030c262 | 169 | count = 0; |
mehatfie | 0:1d73f030c262 | 170 | |
mehatfie | 0:1d73f030c262 | 171 | while (TDC.read() == 1){ //while not at TDC |
mehatfie | 0:1d73f030c262 | 172 | if(signal.read() >= 0.8 && flag == 0){ |
mehatfie | 0:1d73f030c262 | 173 | flag = 1; |
mehatfie | 0:1d73f030c262 | 174 | count++; |
mehatfie | 0:1d73f030c262 | 175 | } |
mehatfie | 0:1d73f030c262 | 176 | else if (signal.read() < 0.8 && flag == 1) |
mehatfie | 0:1d73f030c262 | 177 | flag = 0; |
mehatfie | 0:1d73f030c262 | 178 | } |
mehatfie | 0:1d73f030c262 | 179 | |
mehatfie | 0:1d73f030c262 | 180 | timer.stop(); |
mehatfie | 0:1d73f030c262 | 181 | |
mehatfie | 0:1d73f030c262 | 182 | //Pause at TDC for 5 seconds before continuing |
mehatfie | 0:1d73f030c262 | 183 | stateDir = 0; |
mehatfie | 0:1d73f030c262 | 184 | bridges.drive( BridgeDriver::MOTOR_B, 0.0 ); |
mehatfie | 0:1d73f030c262 | 185 | wait(1); |
mehatfie | 0:1d73f030c262 | 186 | stateDir = 1; |
mehatfie | 0:1d73f030c262 | 187 | bridges.drive( BridgeDriver::MOTOR_B, 1.0 ); |
mehatfie | 0:1d73f030c262 | 188 | |
mehatfie | 0:1d73f030c262 | 189 | timer.reset(); |
mehatfie | 0:1d73f030c262 | 190 | timer.start(); |
mehatfie | 0:1d73f030c262 | 191 | |
mehatfie | 0:1d73f030c262 | 192 | int stopCount = 0; |
mehatfie | 0:1d73f030c262 | 193 | while (stopCount < (hallCount - count - 20)){ |
mehatfie | 0:1d73f030c262 | 194 | if(signal.read() >= 0.8 && flag == 0){ |
mehatfie | 0:1d73f030c262 | 195 | flag = 1; |
mehatfie | 0:1d73f030c262 | 196 | stopCount++; |
mehatfie | 0:1d73f030c262 | 197 | } |
mehatfie | 0:1d73f030c262 | 198 | else if (signal.read() < 0.8 && flag == 1) |
mehatfie | 0:1d73f030c262 | 199 | flag = 0; |
mehatfie | 0:1d73f030c262 | 200 | } |
mehatfie | 0:1d73f030c262 | 201 | |
mehatfie | 0:1d73f030c262 | 202 | timer.stop(); |
mehatfie | 0:1d73f030c262 | 203 | |
mehatfie | 0:1d73f030c262 | 204 | //Pause at the end state before rotating back |
mehatfie | 0:1d73f030c262 | 205 | stateDir = 0; |
mehatfie | 0:1d73f030c262 | 206 | bridges.drive( BridgeDriver::MOTOR_B, 0.0 ); |
mehatfie | 0:1d73f030c262 | 207 | wait(1); |
mehatfie | 0:1d73f030c262 | 208 | stateDir = -1; |
mehatfie | 0:1d73f030c262 | 209 | bridges.drive( BridgeDriver::MOTOR_B, -1.0 ); |
mehatfie | 0:1d73f030c262 | 210 | |
mehatfie | 0:1d73f030c262 | 211 | timer.reset(); |
mehatfie | 0:1d73f030c262 | 212 | timer.start(); |
mehatfie | 0:1d73f030c262 | 213 | |
mehatfie | 0:1d73f030c262 | 214 | flag = 0; |
mehatfie | 0:1d73f030c262 | 215 | count = 0; |
mehatfie | 0:1d73f030c262 | 216 | while (TDC.read() == 1){ //while not at TDC |
mehatfie | 0:1d73f030c262 | 217 | if(signal.read() >= 0.8 && flag == 0){ |
mehatfie | 0:1d73f030c262 | 218 | flag = 1; |
mehatfie | 0:1d73f030c262 | 219 | count++; |
mehatfie | 0:1d73f030c262 | 220 | } |
mehatfie | 0:1d73f030c262 | 221 | else if (signal.read() < 0.8 && flag == 1) |
mehatfie | 0:1d73f030c262 | 222 | flag = 0; |
mehatfie | 0:1d73f030c262 | 223 | } |
mehatfie | 0:1d73f030c262 | 224 | |
mehatfie | 0:1d73f030c262 | 225 | timer.stop(); |
mehatfie | 0:1d73f030c262 | 226 | |
mehatfie | 0:1d73f030c262 | 227 | //Pause at TDC for 5 seconds before continuing |
mehatfie | 0:1d73f030c262 | 228 | stateDir = 0; |
mehatfie | 0:1d73f030c262 | 229 | bridges.drive( BridgeDriver::MOTOR_B, 0.0 ); |
mehatfie | 0:1d73f030c262 | 230 | wait(1); |
mehatfie | 0:1d73f030c262 | 231 | stateDir = -1; |
mehatfie | 0:1d73f030c262 | 232 | bridges.drive( BridgeDriver::MOTOR_B, -1.0 ); |
mehatfie | 0:1d73f030c262 | 233 | |
mehatfie | 0:1d73f030c262 | 234 | timer.reset(); |
mehatfie | 0:1d73f030c262 | 235 | timer.start(); |
mehatfie | 0:1d73f030c262 | 236 | |
mehatfie | 0:1d73f030c262 | 237 | stopCount = 0; |
mehatfie | 0:1d73f030c262 | 238 | while (stopCount < (hallCount - count - 15)){ |
mehatfie | 0:1d73f030c262 | 239 | if(signal.read() >= 0.8 && flag == 0){ |
mehatfie | 0:1d73f030c262 | 240 | flag = 1; |
mehatfie | 0:1d73f030c262 | 241 | stopCount++; |
mehatfie | 0:1d73f030c262 | 242 | } |
mehatfie | 0:1d73f030c262 | 243 | else if (signal.read() < 0.8 && flag == 1) |
mehatfie | 0:1d73f030c262 | 244 | flag = 0; |
mehatfie | 0:1d73f030c262 | 245 | } |
mehatfie | 0:1d73f030c262 | 246 | |
mehatfie | 0:1d73f030c262 | 247 | timer.stop(); |
mehatfie | 0:1d73f030c262 | 248 | |
mehatfie | 0:1d73f030c262 | 249 | //Pause at TDC for 5 seconds before continuing |
mehatfie | 0:1d73f030c262 | 250 | stateDir = 0; |
mehatfie | 0:1d73f030c262 | 251 | bridges.drive( BridgeDriver::MOTOR_B, 0.0 ); |
mehatfie | 0:1d73f030c262 | 252 | wait(1); |
mehatfie | 0:1d73f030c262 | 253 | |
mehatfie | 0:1d73f030c262 | 254 | avgCycTimer.stop(); |
mehatfie | 0:1d73f030c262 | 255 | |
mehatfie | 0:1d73f030c262 | 256 | totaltime += avgCycTimer.read(); |
mehatfie | 0:1d73f030c262 | 257 | lcd.setAddress( 0, 1 ); |
mehatfie | 0:1d73f030c262 | 258 | lcd.printf( "Avg t(sec): %1.3f", (totaltime / cycleCount)); |
mehatfie | 0:1d73f030c262 | 259 | wait(0.2); |
mehatfie | 0:1d73f030c262 | 260 | |
mehatfie | 0:1d73f030c262 | 261 | cycleCount++; |
mehatfie | 0:1d73f030c262 | 262 | } |
mehatfie | 0:1d73f030c262 | 263 | |
mehatfie | 0:1d73f030c262 | 264 | lcd.cls(); //clear the display |
mehatfie | 0:1d73f030c262 | 265 | lcd.setAddress(0,0); |
mehatfie | 0:1d73f030c262 | 266 | lcd.printf("END OF PROGRAM"); |
mehatfie | 0:1d73f030c262 | 267 | lcd.setAddress(0,1); |
mehatfie | 0:1d73f030c262 | 268 | lcd.printf("Num Cycles Met"); |
mehatfie | 0:1d73f030c262 | 269 | return 0; |
mehatfie | 0:1d73f030c262 | 270 | |
mehatfie | 0:1d73f030c262 | 271 | } |