William Pedler
/
AutoCalibrateModulo
Revision 2:755b99cceb46, committed 2013-04-09
- Comitter:
- pedlerw
- Date:
- Tue Apr 09 20:50:35 2013 +0000
- Parent:
- 1:839192bf53f3
- Commit message:
- 4/9/2013; William Pedler; Publish to troubleshoot iostream issues
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 839192bf53f3 -r 755b99cceb46 main.cpp --- a/main.cpp Tue Apr 09 03:19:20 2013 +0000 +++ b/main.cpp Tue Apr 09 20:50:35 2013 +0000 @@ -1,9 +1,11 @@ -//AutoCalibrate// +//AutoCalibrateModulo// //Written by William Pedler 4/8/2013// //This program is used to automatically calibrate and move the forcer linearly// //Includes// #include "mbed.h" +//#include <iostream> +using namespace std; //Define// #define MAXI 1.0 @@ -13,6 +15,7 @@ #define seven 7 #define length 42 #define SPEED 0.150 +#define medium 0.75 #define slow 0.5 //Declar global variables// @@ -44,10 +47,11 @@ AnalogIn distanceR(p19); //Vout yellow GND black Vss red AnalogIn distanceL(p20); //Vout yellow GND black Vss red //Set USB// -//Serial pc(USBTX, USBRX); +Serial pc(USBTX, USBRX); //Functions// void move_all_left(); +void move_all_right(); float get_Lsensor_min(); float get_Lsensor_max(); float get_Rsensor_min(); @@ -105,7 +109,7 @@ left_max[i] = get_Lsensor_max(); right_min[i] = get_Rsensor_min(); right_max[i] = get_Rsensor_max(); - + led4 = !led4; //4.Move to the right one// move_one_right(i); wait(slow); @@ -126,8 +130,10 @@ fclose(fp); //Set an LED high so we know calibration is complete// - led1 = 1; - + led1 = 1; + move_all_left(); + //move_all_right(); + //move_all_left(); ////////////////////////////////////////////////////////////////////////////// ////////////////////////////Operate Motor///////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// @@ -135,24 +141,63 @@ //Now use calibration data to operate motor// while(1){ //1.Find current location// - current_location = get_position(); - //2.Take in desired position// + current_location = get_position(); + //2.Take in desired location// //I'll code this later... for now go to position 13// - desired_location = 13; + desired_location = 13; +/*///////////////////////////////////////////////////////////////////////////// +/////////////////////////////User Interface//////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + cout << "\n\n\rEnter a position number from 0 to 41: "; + cin >> desired_location; + while(std::cin.fail()){ + cin.clear(); + cin.ignore(); + cout << "\n\n\r\t\tNot a number!\n\n\r\t\tPlease Try Again"; + cout << "\n\rEnter a position number from 0 to 41: "; + cin >> desired_location; + }//End of while(std::cin.fail())// + cout << "\n\n\rGoing to position " << desired_location; + */ //3.Move to desired position// - difference = current_location - desired_location; - while(difference > 0){ - move_one_left(current_location % seven); - current_location = get_position(); + while(current_location != desired_location){ difference = current_location - desired_location; - }//End of while// - while(difference < 0){ - move_one_right(current_location % seven); - current_location = get_position(); - difference = current_location - desired_location; - }//End of while// - //4.Repeat// - }//End of while// + while(difference > 0){ + //Move LEFT// + led2 = 1; + wait(medium); + for(int i=0; i<difference;i++){ + move_one_left(abs(current_location)); + current_location--; + wait(SPEED); + }//End of for + current_location = get_position(); + difference = current_location - desired_location; + pc.printf("\r\n\nMoving Left"); + pc.printf("\r\nCurrent Location:\t%d",current_location); + pc.printf("\r\nDesired Location:\t%d",desired_location); + pc.printf("\r\nDifference:\t%d",difference); + led2 = 0; + }//End of while(greater than zero)// + while(difference < 0){ + led3 = 1; + wait(medium); + for(int i=0; i<abs(difference);i++){ + move_one_right(current_location); + current_location++; + wait(SPEED); + }//End of for + current_location = get_position(); + difference = current_location - desired_location; + pc.printf("\r\n\nMoving Right"); + pc.printf("\r\nCurrent Location:\t%d",current_location); + pc.printf("\r\nDesired Location:\t%d",desired_location); + pc.printf("\r\nDifference:\t%d",difference); + led3 = 0; + }//End of while(difference less than 0)// + }//End of while(current not desired)// + //4.Repeat// + }//End of while(1)// }//End of main /////////////////////////////////////////////////////////////////////////////// @@ -202,6 +247,48 @@ }//End of Funciton /////////////////////////////////////////////////////////////////////////////// +////////////////////////////move_all_right///////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +//moves the forcer all the way to the left of the track// +void move_all_right(){ +//Declartions and initialization// +float lng = (SPEED); +linear0 = 1; //Coil1 Mod0 +linear1 = 1; //Coil2 Mod1 +linear2 = 1; //Coil3 Mod2 +linear3 = 1; //Coil4 Mod3 +linear4 = 1; //Coil5 Mod4 +linear5 = 1; //Coil6 Mod5 +linear6 = 1; //Coil7 Mod6 + + //RIGHT// + for(int i=0;i<6;i++){ + linear6 =! linear6; //Coil 7 + wait(lng); + linear6 =! linear6; + linear5 =! linear5; //Coil 6 + wait(lng); + linear5 =! linear5; + linear4 =! linear4; //Coil 5 + wait(lng); + linear4 =! linear4; + linear3 =! linear3; //Coil 4 + wait(lng); + linear3 =! linear3; + linear2 =! linear2; //Coil 3 + wait(lng); + linear2 =! linear2; + linear1 =! linear1; //Coil 2 + wait(lng); + linear1 =! linear1; + linear0 =! linear0; //Coil 1 + wait(lng); + linear0 =! linear0; + }//End of For loop + return; +}//End of Funciton + +/////////////////////////////////////////////////////////////////////////////// ////////////////////////////get_Lsensor_min//////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// //returns the min distance value of left sensor when called// @@ -291,6 +378,7 @@ void move_one_right(int position){ //Declarations and initializations// int current_coil = position % seven; + pc.printf("\r\nCurrent Coil:\t%d",current_coil); switch(current_coil){ case 0: linear6 = 1; @@ -367,6 +455,7 @@ void move_one_left(int position){ //Declarations and initializations// int current_coil = position % seven; + pc.printf("\r\nCurrent Coil:\t%d",current_coil); switch(current_coil){ case 0: linear6 = 1;