Mitchell Hatfield / Mbed 2 deprecated MX_Spoile_Test

Dependencies:   BridgeDriver_NA FrontPanelButtons MCP23017 TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "LocalPinNames.h"
00003 #include "BridgeDriver.h"
00004 #include "FrontPanelButtons.h"
00005 #include "TextLCD.h"
00006 
00007 I2C i2c( P0_10, P0_11 ); // I2C bus (SDA, SCL)
00008 BridgeDriver bridges( &i2c ); // bridge
00009 TextLCD_I2C lcd( &i2c, MCP23008_SA0, TextLCD::LCD20x4 ); // LCD
00010 FrontPanelButtons buttons( &i2c );    // front panel buttons
00011 
00012 DigitalIn TDC(DIO0, PullDown);
00013 AnalogIn signal(AI0);
00014 
00015 Ticker ErrorWatch;
00016 Timer timer; //initialize a timer
00017 Timer avgCycTimer;
00018 
00019 float totaltime = 0;
00020 
00021 SPI spi(P0_9, P0_8, P0_7); // mosi(out), miso(in), sclk(clock)
00022 DigitalOut cs_Current(P1_0); // cs (the chip select signal)
00023 
00024 float stateDir = 0;
00025 int numCycles = 200000;
00026 int cycleCount = 1;
00027 
00028 void errorCheck(){
00029     
00030     if (timer.read() > 3.0){
00031         bridges.drive( BridgeDriver::MOTOR_B, 0.0 );
00032         
00033         lcd.cls(); //clear the display
00034         lcd.setAddress ( 0, 0 );
00035         lcd.printf( "<Error Occurred>" );
00036         lcd.setAddress ( 0, 1 );
00037         lcd.printf( "Timeout, transition greater than 3      seconds" );
00038         lcd.setAddress ( 0, 3 );
00039         lcd.printf( "<Press Sel to Resume" );
00040         while (!buttons.readSel());
00041         lcd.cls(); //clear the display
00042         
00043         lcd.setAddress( 0, 0 );
00044         lcd.printf( "Cycle %d/%3d", cycleCount, numCycles );
00045         lcd.setAddress( 0, 1 );
00046         lcd.printf( "Avg t(sec): %1.3f", (totaltime / cycleCount));
00047 
00048         timer.reset();
00049         timer.start();
00050         
00051         bridges.drive( BridgeDriver::MOTOR_B, stateDir );
00052     }   
00053 }
00054 
00055 
00056 
00057 float calcCurrent(int channel){
00058         
00059     // Select the device by seting chip select low
00060     cs_Current = 0;
00061 
00062     // sending the 6 bits + 1 bit to ignore the null bit
00063     // coming from the device, so the data that is sent is 1100000
00064     // get data for channel 0
00065     //spi.write(0x68); // 1101000
00066     
00067     // create the message to send in order to get the appropriate data transmitted from the ADC
00068     unsigned char channelBits = (channel-1) << 2; // shift the channel bits into there position
00069     unsigned char msg = 96 + channelBits;
00070     spi.write(msg);
00071     
00072     // now the device sends back the readings 12 bits, 7 bits at a time
00073     uint8_t high = spi.write(0x00);
00074     uint8_t low = spi.write(0x00);
00075 
00076     // shift out the right bits
00077     low = ( high << 5 ) | (low >> 2);
00078     high = high >> 3;
00079     
00080     
00081     float DigOutVal = ( high << 8 ) | low; // shift and or the result together
00082 
00083     cs_Current = 1; // Deselect the device
00084     
00085     //return current;
00086     return DigOutVal;
00087 }
00088 
00089 int main() {
00090    
00091     //initialize Ticker
00092     ErrorWatch.attach(&errorCheck, 3);
00093     
00094     // Setup the spi for 7 bit data, high steady state clock,
00095     // second edge capture, with a 1MHz clock rate
00096     spi.format(7,0);
00097     spi.frequency(1000000);
00098 
00099     i2c.frequency(1000000);
00100     lcd.setBacklight(TextLCD::LightOn);
00101     wait(.6);
00102     lcd.cls(); //clear the display
00103     lcd.setAddress(0,0); //set cursor to 0,0
00104        
00105     int count = 0;
00106     
00107     bridges.enablePwm    (  0,  1,  0,  0 );
00108     bridges.drive( BridgeDriver::MOTOR_B, 0.0 );
00109 
00110     lcd.cls(); //clear the display
00111     lcd.setAddress ( 0, 2 );
00112     lcd.printf( "<Press Sel to start>" );
00113     
00114     while (!buttons.readSel()){
00115         if(buttons.readUp() && numCycles < 999999 ){
00116             numCycles += 500;
00117             wait(0.2); //so that the speed of changing the numbers is more controllable, should mean you can move 20 digits per second
00118         }
00119         else if (buttons.readDown() && numCycles > 0 ){
00120             numCycles -= 500;
00121             wait(0.2); //so that the speed of changing the numbers is more controllable, should mean you can move 20 digits per second
00122         }
00123         lcd.setAddress ( 0, 0 );
00124         lcd.printf( "<Cycles: %d >" , numCycles );
00125     }
00126         
00127     bridges.drive( 2, 1 ); //turn on the hall sensor voltage
00128     
00129     int hallCount = 109;
00130     
00131     //Initialize the spoiler by rotating to stall currents and finding how many ticks the hall sensor takes through the rotation
00132     bridges.drive( BridgeDriver::MOTOR_B, -1.0 );
00133     while (calcCurrent(4) < 2200);
00134     bridges.drive( BridgeDriver::MOTOR_B, 0.0 );
00135 //    wait(1);
00136 //    bridges.drive( BridgeDriver::MOTOR_B, 1.0 );
00137 //    wait(1);
00138 //    
00139 //    int flag = 0;
00140 //    while (calcCurrent(3) < 2200){
00141 //        if(signal.read() >= 0.8 && flag == 0){
00142 //                flag = 1;
00143 //                hallCount++;
00144 //            }
00145 //        else if (signal.read() < 0.8 && flag == 1)
00146 //            flag = 0;
00147 //    }
00148 
00149     bridges.drive( BridgeDriver::MOTOR_B, 0.0 );
00150     wait(3);
00151     
00152     
00153     lcd.cls(); //clear the display
00154     
00155     while(cycleCount <= numCycles){
00156         
00157         avgCycTimer.reset();
00158         avgCycTimer.start();
00159         
00160         lcd.setAddress( 0, 0 );
00161         lcd.printf( "Cycle %d/%3d", cycleCount, numCycles );
00162         
00163         stateDir = 1;
00164         bridges.drive( BridgeDriver::MOTOR_B, 1.0 );
00165         timer.reset();
00166         timer.start();
00167         
00168         int flag = 0;
00169         count = 0;
00170         
00171         while (TDC.read() == 1){ //while not at TDC
00172             if(signal.read() >= 0.8 && flag == 0){
00173                 flag = 1;
00174                 count++;
00175             }
00176             else if (signal.read() < 0.8 && flag == 1)
00177                 flag = 0;
00178         }
00179         
00180         timer.stop();
00181     
00182         //Pause at TDC for 5 seconds before continuing
00183         stateDir = 0;
00184         bridges.drive( BridgeDriver::MOTOR_B, 0.0 );
00185         wait(1);
00186         stateDir = 1;
00187         bridges.drive( BridgeDriver::MOTOR_B, 1.0 );
00188         
00189         timer.reset();
00190         timer.start();
00191         
00192         int stopCount = 0;
00193         while (stopCount < (hallCount - count - 20)){
00194             if(signal.read() >= 0.8 && flag == 0){
00195                 flag = 1;
00196                 stopCount++;
00197             }
00198             else if (signal.read() < 0.8 && flag == 1)
00199                 flag = 0;
00200         }
00201         
00202         timer.stop();
00203         
00204         //Pause at the end state before rotating back
00205         stateDir = 0;
00206         bridges.drive( BridgeDriver::MOTOR_B, 0.0 );
00207         wait(1);
00208         stateDir = -1;
00209         bridges.drive( BridgeDriver::MOTOR_B, -1.0 );
00210         
00211         timer.reset();
00212         timer.start();
00213         
00214         flag = 0;
00215         count = 0;
00216         while (TDC.read() == 1){ //while not at TDC
00217             if(signal.read() >= 0.8 && flag == 0){
00218                 flag = 1;
00219                 count++;
00220             }
00221             else if (signal.read() < 0.8 && flag == 1)
00222                 flag = 0;
00223         }
00224         
00225         timer.stop();
00226         
00227         //Pause at TDC for 5 seconds before continuing
00228         stateDir = 0;
00229         bridges.drive( BridgeDriver::MOTOR_B, 0.0 );
00230         wait(1);
00231         stateDir = -1;
00232         bridges.drive( BridgeDriver::MOTOR_B, -1.0 );
00233         
00234         timer.reset();
00235         timer.start();
00236         
00237         stopCount = 0;
00238         while (stopCount < (hallCount - count - 15)){
00239             if(signal.read() >= 0.8 && flag == 0){
00240                 flag = 1;
00241                 stopCount++;
00242             }
00243             else if (signal.read() < 0.8 && flag == 1)
00244                 flag = 0;
00245         }
00246         
00247         timer.stop();
00248         
00249         //Pause at TDC for 5 seconds before continuing
00250         stateDir = 0;
00251         bridges.drive( BridgeDriver::MOTOR_B, 0.0 );
00252         wait(1);  
00253         
00254         avgCycTimer.stop();
00255         
00256         totaltime += avgCycTimer.read();
00257         lcd.setAddress( 0, 1 );
00258         lcd.printf( "Avg t(sec): %1.3f", (totaltime / cycleCount));
00259         wait(0.2);
00260         
00261         cycleCount++;
00262     } 
00263     
00264     lcd.cls(); //clear the display   
00265     lcd.setAddress(0,0);
00266     lcd.printf("END OF PROGRAM");
00267     lcd.setAddress(0,1);
00268     lcd.printf("Num Cycles Met");
00269     return 0;
00270     
00271 }