linear actuator

Dependencies:   Motor PinDetect mbed

Fork of xbee_robot by Shubhojit Chattopadhyay

Committer:
conantina
Date:
Wed Feb 24 16:52:04 2016 +0000
Revision:
3:473269e5a06f
Parent:
2:9a9fbbf43957
Child:
4:580ea208038b
ff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ldeng31 1:57f1410e9614 1 //Linear Actuator to control the moving rod.
conantina 2:9a9fbbf43957 2 /*
buntyshubho 0:4b7f01607612 3 #include "mbed.h"
buntyshubho 0:4b7f01607612 4 #include "Motor.h"
ldeng31 1:57f1410e9614 5 #include "PinDetect.h"
buntyshubho 0:4b7f01607612 6
ldeng31 1:57f1410e9614 7 float moving_time1 = 15.0; //we control the Table Actuator stop position by play with the moving time1; longer rising time,higher stop position
ldeng31 1:57f1410e9614 8 float moving_time2 = 20.0; //we control the Connector Actuator stop position by play with the moving time2; longer rising time,higher stop position
ldeng31 1:57f1410e9614 9 float charging_time = 20.0; //the time needed to charge the battery, during which the Connector Actuator will stay at its highest position
ldeng31 1:57f1410e9614 10 float wait_time = 3.0; //time waiting for the drone to slide to the triangle hindge
ldeng31 1:57f1410e9614 11 float hold_time = 3.0; //the time during which the Table Actuator will stay at its highest position after disconnected the connector
conantina 2:9a9fbbf43957 12 int running_schedule = 0; //0-resting 1-running the schedule
conantina 2:9a9fbbf43957 13 int reset = 0;
buntyshubho 0:4b7f01607612 14
ldeng31 1:57f1410e9614 15 Motor m1(p21, p27, p28); // pwm, fwd, rev //Table Actuator
ldeng31 1:57f1410e9614 16 Motor m2(p22, p10, p9); // pwm, fwd, rev //Connector Actuator
conantina 2:9a9fbbf43957 17 PinDetect load_sensor(p5);
ldeng31 1:57f1410e9614 18 PinDetect start(p6);
ldeng31 1:57f1410e9614 19 PinDetect reset(p7);
buntyshubho 0:4b7f01607612 20
conantina 2:9a9fbbf43957 21 void start_the_schedule(){
conantina 2:9a9fbbf43957 22 running_schedule = 1; //when the load sensor sense the drone or the start putton is pressed, change to 1;
ldeng31 1:57f1410e9614 23 }
conantina 2:9a9fbbf43957 24
ldeng31 1:57f1410e9614 25 void reset_to_zero(){
ldeng31 1:57f1410e9614 26 m2.speed(-1); //Connector Actuator Goging down
ldeng31 1:57f1410e9614 27 wait(moving_time2);
ldeng31 1:57f1410e9614 28 m2.speed(0); //Connector Actuator Stops at the start position
ldeng31 1:57f1410e9614 29 m1.speed(-1); //Connector Actuator Goging down
ldeng31 1:57f1410e9614 30 wait(moving_time1);
ldeng31 1:57f1410e9614 31 m1.speed(0); //Table Actuator Stops at the start position
buntyshubho 0:4b7f01607612 32 }
conantina 2:9a9fbbf43957 33
ldeng31 1:57f1410e9614 34
ldeng31 1:57f1410e9614 35 void reset_to_zero(){
conantina 2:9a9fbbf43957 36 reset = 1; //reset now gets priority
ldeng31 1:57f1410e9614 37 m2.speed(-1);
ldeng31 1:57f1410e9614 38 m1.speed(-1); //Connector Actuator Goging down
ldeng31 1:57f1410e9614 39 wait(moving_time2);
conantina 2:9a9fbbf43957 40 m2.speed(0); //Connector Actuator Stops at the start position
conantina 2:9a9fbbf43957 41 m1.speed(0); //Table Actuator Stops at the start position
ldeng31 1:57f1410e9614 42 }
ldeng31 1:57f1410e9614 43
ldeng31 1:57f1410e9614 44 int main() {
conantina 2:9a9fbbf43957 45 load_sensor.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 46 load_sensor.attach_deasserted(&start_the_schedule);
conantina 2:9a9fbbf43957 47 load_sensor.setSampleFrequency();
ldeng31 1:57f1410e9614 48 start.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 49 start.attach_deasserted(&start_the_schedule);
ldeng31 1:57f1410e9614 50 start.setSampleFrequency();
ldeng31 1:57f1410e9614 51 reset.mode(PullUp); wait(0.01);
ldeng31 1:57f1410e9614 52 reset.attach_deasserted(&reset_to_zero);
ldeng31 1:57f1410e9614 53 reset.setSampleFrequency();
conantina 2:9a9fbbf43957 54
ldeng31 1:57f1410e9614 55 while(1) {
conantina 2:9a9fbbf43957 56 while(running_schedule){
conantina 2:9a9fbbf43957 57 if(reset) break;
conantina 2:9a9fbbf43957 58 m1.speed(1); //Table Actuator Going Up
conantina 2:9a9fbbf43957 59 wait(moving_time1);
conantina 2:9a9fbbf43957 60 m1.speed(0); //Table Actuator Stops at its Highest Position
conantina 2:9a9fbbf43957 61 if(reset) break;
conantina 2:9a9fbbf43957 62 wait(wait_time); //Waiting for the quadcoptor to slide into the triangle hindge
conantina 2:9a9fbbf43957 63 if(reset) break;
conantina 2:9a9fbbf43957 64 m2.speed(1); //Connector Actuator Going Up
conantina 2:9a9fbbf43957 65 wait(moving_time2);
conantina 2:9a9fbbf43957 66 m2.speed(0); //Connector Actuator Stops at its Highest Position
conantina 2:9a9fbbf43957 67 if(reset) break;
conantina 2:9a9fbbf43957 68 wait(charging_time); //Waiting for the battery to charging
conantina 2:9a9fbbf43957 69 if(reset) break;
conantina 2:9a9fbbf43957 70 m2.speed(-1); //Connector Actuator Goging down
conantina 2:9a9fbbf43957 71 wait(moving_time2);
conantina 2:9a9fbbf43957 72 m2.speed(0); //Connector Actuator Stops at the start position
conantina 2:9a9fbbf43957 73 if(reset) break;
conantina 2:9a9fbbf43957 74 wait(hold_time); //Waiting for the quadcoptpr to take off
conantina 2:9a9fbbf43957 75 if(reset) break;
conantina 2:9a9fbbf43957 76 m1.speed(-1); //Connector Actuator Goging down
conantina 2:9a9fbbf43957 77 wait(moving_time1);
conantina 2:9a9fbbf43957 78 m1.speed(0); //Table Actuator Stops at the start position
conantina 2:9a9fbbf43957 79 running_schedule = 0; //change back to resting after finished the whole loop
conantina 2:9a9fbbf43957 80 }
conantina 2:9a9fbbf43957 81 reset = 0; //Either finishe the schedual natually, or break from the loop, set reset back to 0
conantina 2:9a9fbbf43957 82 running_schedule = 0; //and rest the system
ldeng31 1:57f1410e9614 83 }
ldeng31 1:57f1410e9614 84
ldeng31 1:57f1410e9614 85 }
conantina 2:9a9fbbf43957 86
conantina 2:9a9fbbf43957 87 */
conantina 2:9a9fbbf43957 88
conantina 2:9a9fbbf43957 89 #include "mbed.h"
conantina 2:9a9fbbf43957 90 #include "Motor.h"
conantina 2:9a9fbbf43957 91 #include "PinDetect.h"
conantina 2:9a9fbbf43957 92
conantina 2:9a9fbbf43957 93 float moving_time1 = 15.0; //we control the Table Actuator stop position by play with the moving time1; longer rising time,higher stop position
conantina 2:9a9fbbf43957 94 float moving_time2 = 20.0; //we control the Connector Actuator stop position by play with the moving time2; longer rising time,higher stop position
conantina 2:9a9fbbf43957 95 float charging_time = 20.0; //the time needed to charge the battery, during which the Connector Actuator will stay at its highest position
conantina 2:9a9fbbf43957 96 float wait_time = 3.0; //time waiting for the drone to slide to the triangle hindge
conantina 2:9a9fbbf43957 97 float hold_time = 3.0; //the time during which the Table Actuator will stay at its highest position after disconnected the connector
conantina 2:9a9fbbf43957 98 bool running_schedule = 0; //0- the system is resting 1-running schedule
conantina 2:9a9fbbf43957 99 bool is_reset=0; // Used to activate reset function
conantina 2:9a9fbbf43957 100
conantina 2:9a9fbbf43957 101 const float pulse_width = 0.2; // Define width of the pulse
conantina 2:9a9fbbf43957 102
conantina 2:9a9fbbf43957 103 Motor m1(p21, p27, p28); // pwm, fwd, rev //Table Actuator
conantina 2:9a9fbbf43957 104 Motor m2(p22, p10, p9); // pwm, fwd, rev //Connector Actuator
conantina 2:9a9fbbf43957 105 PinDetect load_sensor(p5);
conantina 2:9a9fbbf43957 106 PinDetect start(p6);
conantina 2:9a9fbbf43957 107 PinDetect reset(p7);
conantina 2:9a9fbbf43957 108
conantina 2:9a9fbbf43957 109
conantina 2:9a9fbbf43957 110
conantina 2:9a9fbbf43957 111 //********************************************* Pb Interrupt Functions **************************************************
conantina 2:9a9fbbf43957 112
conantina 2:9a9fbbf43957 113 void start_the_schedule()
conantina 2:9a9fbbf43957 114 {
conantina 2:9a9fbbf43957 115 running_schedule = 1; //when the load sensor sense the drone or the start putton is pressed, change to 1;
conantina 2:9a9fbbf43957 116 }
conantina 2:9a9fbbf43957 117
conantina 2:9a9fbbf43957 118 void break_loop()
conantina 2:9a9fbbf43957 119 {
conantina 2:9a9fbbf43957 120 is_reset = 1; // intterup to reset
conantina 2:9a9fbbf43957 121 }
conantina 2:9a9fbbf43957 122
conantina 2:9a9fbbf43957 123 //********************************************* Actuator Control Functions **************************************************
conantina 2:9a9fbbf43957 124 void raise(Motor& m,const float& pls_wid)
conantina 2:9a9fbbf43957 125 { //send a rising command pulse to Motor m
conantina 2:9a9fbbf43957 126 m.speed(1);
conantina 2:9a9fbbf43957 127 wait(pls_wid);
conantina 2:9a9fbbf43957 128 m.speed(0);
conantina 2:9a9fbbf43957 129 }
conantina 2:9a9fbbf43957 130
conantina 2:9a9fbbf43957 131 void fall(Motor& m,const float& pls_wid)
conantina 2:9a9fbbf43957 132 { //send a falling command pulse to Motor m
conantina 2:9a9fbbf43957 133 m.speed(-1);
conantina 2:9a9fbbf43957 134 wait(pulse_width);
conantina 2:9a9fbbf43957 135 m.speed(0);
conantina 2:9a9fbbf43957 136 }
conantina 2:9a9fbbf43957 137
conantina 2:9a9fbbf43957 138 void hold()
conantina 2:9a9fbbf43957 139 { //send a wait command pulse
conantina 2:9a9fbbf43957 140 wait(0.2);
conantina 2:9a9fbbf43957 141 }
conantina 2:9a9fbbf43957 142
conantina 2:9a9fbbf43957 143 //********************************************* Functions for Base Station System *********************************************************
conantina 2:9a9fbbf43957 144 void reset_schedule()
conantina 2:9a9fbbf43957 145 {
conantina 2:9a9fbbf43957 146 for(int i = 0; i<(moving_time2/pulse_width);i++){
conantina 2:9a9fbbf43957 147 fall(m2,pulse_width);
conantina 2:9a9fbbf43957 148 }
conantina 2:9a9fbbf43957 149 for(int i = 0; i<(moving_time1/pulse_width);i++){
conantina 2:9a9fbbf43957 150 fall(m1,pulse_width);
conantina 2:9a9fbbf43957 151 }
conantina 2:9a9fbbf43957 152 is_reset = 0;
conantina 2:9a9fbbf43957 153 }
conantina 2:9a9fbbf43957 154
conantina 2:9a9fbbf43957 155 void schedule()
conantina 2:9a9fbbf43957 156 {
conantina 2:9a9fbbf43957 157 for(int i=0;i<(moving_time1/pulse_width);i++) //Table Actuator lift the table up
conantina 2:9a9fbbf43957 158 { if(is_reset){reset_schedule();return;}
conantina 2:9a9fbbf43957 159 raise(m1,pulse_width);
conantina 2:9a9fbbf43957 160 }
conantina 2:9a9fbbf43957 161 for(int i=0;i<(wait_time/pulse_width);i++) //Waiting for the quadcoptor to slide into the triangle hindge
conantina 2:9a9fbbf43957 162 { if(is_reset){reset_schedule();return;}
conantina 2:9a9fbbf43957 163 hold();
conantina 2:9a9fbbf43957 164 }
conantina 2:9a9fbbf43957 165 for(int i = 0; i<(moving_time2/pulse_width);i++) //Connector Actuator lifts up
conantina 2:9a9fbbf43957 166 { if(is_reset){reset_schedule();return;}
conantina 2:9a9fbbf43957 167 raise(m2,pulse_width);
conantina 2:9a9fbbf43957 168 }
conantina 2:9a9fbbf43957 169 for(int i=0;i<(charging_time/pulse_width);i++) //Waiting for the battery to charge
conantina 2:9a9fbbf43957 170 { if(is_reset){reset_schedule();return;}
conantina 2:9a9fbbf43957 171 hold();
conantina 2:9a9fbbf43957 172 }
conantina 2:9a9fbbf43957 173 for(int i = 0; i<(moving_time2/pulse_width);i++) //Connector Actuator goes down
conantina 2:9a9fbbf43957 174 { if(is_reset){reset_schedule();return;}
conantina 2:9a9fbbf43957 175 fall(m2,pulse_width);
conantina 2:9a9fbbf43957 176 }
conantina 2:9a9fbbf43957 177 for(int i=0;i<(hold_time/pulse_width);i++) //Waiting for the quadcoptpr to take off
conantina 2:9a9fbbf43957 178 { if(is_reset){reset_schedule();return;}
conantina 2:9a9fbbf43957 179 hold();
conantina 2:9a9fbbf43957 180 }
conantina 2:9a9fbbf43957 181 for(int i = 0; i<(moving_time1/pulse_width);i++) //Table Actuator goes down
conantina 2:9a9fbbf43957 182 { if(is_reset){reset_schedule();return;}
conantina 2:9a9fbbf43957 183 fall(m1,pulse_width);
conantina 2:9a9fbbf43957 184 }
conantina 2:9a9fbbf43957 185 }
conantina 2:9a9fbbf43957 186
conantina 2:9a9fbbf43957 187 //********************************************* Main Function *********************************************************
conantina 2:9a9fbbf43957 188 int main()
conantina 2:9a9fbbf43957 189 {
conantina 2:9a9fbbf43957 190 //init interuppt
conantina 2:9a9fbbf43957 191 load_sensor.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 192 load_sensor.attach_deasserted(&start_the_schedule);
conantina 2:9a9fbbf43957 193 load_sensor.setSampleFrequency();
conantina 2:9a9fbbf43957 194 start.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 195 start.attach_deasserted(&start_the_schedule);
conantina 2:9a9fbbf43957 196 start.setSampleFrequency();
conantina 2:9a9fbbf43957 197 reset.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 198 reset.attach_deasserted(&break_loop);
conantina 2:9a9fbbf43957 199 reset.setSampleFrequency();
conantina 2:9a9fbbf43957 200 while(1){
conantina 2:9a9fbbf43957 201 if(running_schedule){
conantina 2:9a9fbbf43957 202 schedule();
conantina 3:473269e5a06f 203 running_schedule = 0; //return to reset after finish the schedule
conantina 2:9a9fbbf43957 204 }
conantina 2:9a9fbbf43957 205 }
conantina 2:9a9fbbf43957 206 }
conantina 2:9a9fbbf43957 207