Senior Design / Mbed 2 deprecated LATEST1

Dependencies:   Motor PinDetect mbed

Fork of xbee_robot by Shubhojit Chattopadhyay

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Linear Actuator to control the moving rod. 
00002 /*
00003 #include "mbed.h"
00004 #include "Motor.h"
00005 #include "PinDetect.h"
00006 
00007 float moving_time1 = 15.0;            //we control the Table Actuator stop position by play with the moving time1; longer rising time,higher stop position
00008 float moving_time2 = 20.0;            //we control the Connector Actuator stop position by play with the moving time2; longer rising time,higher stop position
00009 float charging_time = 20.0;           //the time needed to charge the battery, during which the Connector Actuator will stay at its highest position
00010 float wait_time = 3.0;               //time waiting for the drone to slide to the triangle hindge
00011 float hold_time = 3.0;               //the time during which the Table Actuator will stay at its highest position after disconnected the connector
00012 int running_schedule = 0;            //0-resting  1-running the schedule
00013 int is_reset = 0;
00014 
00015 DigitalOut led(LED1);
00016 Motor m1(p21, p27, p28);        // pwm, fwd, rev //Table Actuator
00017 Motor m2(p22, p10, p9);          // pwm, fwd, rev //Connector Actuator
00018 PinDetect load_sensor(p5);
00019 PinDetect start(p6);
00020 PinDetect reset(p7);
00021 
00022 void start_the_schedule(){
00023     running_schedule = 1;       //when the load sensor sense the drone or the start putton is pressed, change to 1;
00024     led = !led;    
00025 }
00026 
00027 void reset_to_zero(){
00028     m2.speed(-1);           //Connector Actuator Goging down
00029     wait(moving_time2);
00030     m2.speed(0);            //Connector Actuator Stops at the start position
00031     m1.speed(-1);           //Connector Actuator Goging down
00032     wait(moving_time1);
00033     m1.speed(0);            //Table Actuator Stops at the start position
00034 }
00035 
00036 
00037 int main() {
00038     load_sensor.mode(PullUp); wait(0.01);
00039     load_sensor.attach_deasserted(&start_the_schedule);
00040     load_sensor.setSampleFrequency();
00041     start.mode(PullUp); wait(0.01);
00042     start.attach_deasserted(&start_the_schedule);
00043     start.setSampleFrequency();
00044     reset.mode(PullUp); wait(0.01);
00045     reset.attach_deasserted(&reset_to_zero);
00046     reset.setSampleFrequency();
00047     
00048    
00049     while(1) {
00050         // m1.speed(-1);           //Connector Actuator Goging down
00051         //wait(moving_time1);
00052         //m1.speed(0);
00053         m2.speed(-1);           //Connector Actuator Goging down
00054             wait(moving_time2);
00055             m2.speed(0); 
00056         while(running_schedule){
00057             if(reset) break;
00058             m1.speed(1);            //Table Actuator Going Up
00059             wait(moving_time1);
00060             m1.speed(0);            //Table Actuator Stops at its Highest Position
00061             if(reset) break;
00062             wait(wait_time);        //Waiting for the quadcoptor to slide into the triangle hindge
00063             if(reset) break;
00064             m2.speed(1);            //Connector Actuator Going Up
00065             wait(moving_time2);
00066             m2.speed(0);            //Connector Actuator Stops at its Highest Position
00067             if(reset) break;
00068             wait(charging_time);    //Waiting for the battery to charging 
00069             if(reset) break;
00070             m2.speed(-1);           //Connector Actuator Goging down
00071             wait(moving_time2);
00072             m2.speed(0);            //Connector Actuator Stops at the start position
00073             if(reset) break;
00074             wait(hold_time);        //Waiting for the quadcoptpr to take off
00075             if(reset) break;
00076             m1.speed(-1);           //Connector Actuator Goging down
00077             wait(moving_time1);
00078             m1.speed(0);            //Table Actuator Stops at the start position
00079             running_schedule = 0;   //change back to resting after finished the whole loop
00080         }
00081         is_reset = 0;                  //Either finishe the schedual natually, or break from the loop, set reset back to 0
00082         running_schedule = 0;       //and rest the system 
00083     }  
00084   
00085 }
00086 
00087 */
00088 
00089 #include "mbed.h"
00090 #include "Motor.h"
00091 #include "PinDetect.h"
00092 
00093 float moving_time1 = 10.0;            //we control the Table Actuator stop position by play with the moving time1; longer rising time,higher stop position
00094 float moving_time2 = 20.0;            //we control the Connector Actuator stop position by play with the moving time2; longer rising time,higher stop position
00095 float charging_time = 20.0;           //the time needed to charge the battery, during which the Connector Actuator will stay at its highest position
00096 float wait_time = 3.0;                //time waiting for the drone to slide to the triangle hindge
00097 float hold_time = 3.0;                //the time during which the Table Actuator will stay at its highest position after disconnected the connector
00098 bool running_schedule = 0;            //0- the system is resting  1-running schedule
00099 bool is_reset=0;                      // Used to activate reset function
00100 
00101 const float pulse_width = 0.2;        // Define width of the pulse
00102 
00103 Motor m1(p21, p27, p28);              // pwm, fwd, rev //Table Actuator
00104 Motor m2(p22, p10, p9);               // pwm, fwd, rev //Connector Actuator
00105 PinDetect load_sensor(p5);
00106 PinDetect start(p6);
00107 PinDetect reset(p7);
00108 DigitalOut led(LED1);
00109 DigitalOut led2(LED2);
00110 
00111 //********************************************* Pb Interrupt Functions **************************************************
00112 
00113 void start_the_schedule()
00114 {
00115     running_schedule = 1;       //when the load sensor sense the drone or the start putton is pressed, change to 1;    
00116 }
00117 
00118 void break_loop()
00119 {
00120     is_reset = 1;               // intterup to reset
00121 }
00122 
00123 //********************************************* Actuator Control Functions **************************************************
00124 void raise(Motor& m,const float& pls_wid)
00125 {   //send a rising command pulse to Motor m
00126     m.speed(1);
00127     wait(pls_wid);
00128     m.speed(0);
00129 }
00130 
00131 void fall(Motor& m,const float& pls_wid)
00132 {   //send a falling command pulse to Motor m 
00133     m.speed(-1);
00134     wait(pulse_width);
00135     m.speed(0);
00136 }
00137 
00138 void hold()
00139 {   //send a wait command pulse
00140     wait(0.2);  
00141 }
00142 
00143 //********************************************* Functions for Base Station System *********************************************************
00144 void reset_schedule()
00145 {
00146     led = 0;
00147     for(int i = 0; i<(moving_time2/pulse_width);i++){
00148          fall(m2,pulse_width);   
00149     }    
00150     for(int i = 0; i<(moving_time1/pulse_width);i++){
00151          fall(m1,pulse_width);   
00152     } 
00153     is_reset = 0;
00154 }
00155 
00156 void schedule()
00157 {
00158     for(int i=0;i<(moving_time1/pulse_width);i++)    //Table Actuator lift the table up
00159     {   if(is_reset){reset_schedule();return;}
00160         led = !led;
00161         raise(m1,pulse_width);
00162     }
00163     led2 = 1;
00164     wait(0.2);
00165     led2 = 0;
00166     for(int i=0;i<(wait_time/pulse_width);i++)       //Waiting for the quadcoptor to slide into the triangle hindge
00167     {   if(is_reset){reset_schedule();return;}
00168         led = !led;
00169         hold();
00170     }
00171     led2 = 1;
00172     wait(0.2);
00173     led2 = 0;
00174     for(int i = 0; i<(moving_time1/pulse_width);i++) //Table Actuator goes down
00175     {   if(is_reset){reset_schedule();return;}
00176         led = !led;
00177         fall(m1,pulse_width);   
00178     } 
00179     led2 = 1;
00180     wait(0.2);
00181     led2 = 0; 
00182     for(int i = 0; i<(moving_time2/pulse_width);i++) //Connector Actuator lifts up
00183     {   if(is_reset){reset_schedule();return;}
00184         led = !led;
00185         raise(m2,pulse_width);   
00186     } 
00187     led2 = 1;
00188     wait(0.2);
00189     led2 = 0;
00190     for(int i=0;i<(charging_time/pulse_width);i++)   //Waiting for the battery to charge
00191     {   if(is_reset){reset_schedule();return;}
00192         led = !led;
00193         hold();
00194     }
00195     led2 = 1;
00196     wait(0.2);
00197     led2 = 0;
00198     for(int i = 0; i<(moving_time2/pulse_width);i++) //Connector Actuator goes down
00199     {   if(is_reset){reset_schedule();return;}
00200         led = !led;
00201         fall(m2,pulse_width);   
00202     } 
00203     led2 = 1;
00204     wait(0.2);
00205     led2 = 0;
00206     for(int i=0;i<(hold_time/pulse_width);i++)       //Waiting for the quadcoptpr to take off
00207     {   if(is_reset){reset_schedule();return;}
00208         led = !led;
00209         hold();
00210     }      
00211     led2 = 1;
00212     wait(0.2);
00213     led2 = 0;
00214           
00215 }
00216 
00217 //********************************************* Main Function *********************************************************
00218 int main()
00219 {
00220     //init interuppt
00221     load_sensor.mode(PullUp); wait(0.01);
00222     load_sensor.attach_deasserted(&start_the_schedule);
00223     load_sensor.setSampleFrequency();
00224     start.mode(PullUp); wait(0.01);
00225     start.attach_deasserted(&start_the_schedule);
00226     start.setSampleFrequency();
00227     reset.mode(PullUp); wait(0.01);
00228     reset.attach_deasserted(&break_loop);
00229     reset.setSampleFrequency();
00230     while(1){
00231         led = 1;
00232         if(running_schedule){
00233             schedule();
00234             running_schedule = 0;       //return to reset after finish the schedule
00235         }
00236         if(is_reset){
00237             reset_schedule();
00238         }
00239     }
00240 }
00241