linear actuator

Dependencies:   Motor PinDetect mbed

Fork of xbee_robot by Shubhojit Chattopadhyay

Committer:
conantina
Date:
Fri Feb 26 23:08:38 2016 +0000
Revision:
5:a1b28b06425a
Parent:
4:580ea208038b
latest_version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ldeng31 1:57f1410e9614 1 //Linear Actuator to control the moving rod.
conantina 5:a1b28b06425a 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 4:580ea208038b 13 int is_reset = 0;
buntyshubho 0:4b7f01607612 14
conantina 4:580ea208038b 15 DigitalOut led(LED1);
ldeng31 1:57f1410e9614 16 Motor m1(p21, p27, p28); // pwm, fwd, rev //Table Actuator
ldeng31 1:57f1410e9614 17 Motor m2(p22, p10, p9); // pwm, fwd, rev //Connector Actuator
conantina 2:9a9fbbf43957 18 PinDetect load_sensor(p5);
ldeng31 1:57f1410e9614 19 PinDetect start(p6);
ldeng31 1:57f1410e9614 20 PinDetect reset(p7);
buntyshubho 0:4b7f01607612 21
conantina 2:9a9fbbf43957 22 void start_the_schedule(){
conantina 4:580ea208038b 23 running_schedule = 1; //when the load sensor sense the drone or the start putton is pressed, change to 1;
conantina 4:580ea208038b 24 led = !led;
ldeng31 1:57f1410e9614 25 }
conantina 2:9a9fbbf43957 26
ldeng31 1:57f1410e9614 27 void reset_to_zero(){
ldeng31 1:57f1410e9614 28 m2.speed(-1); //Connector Actuator Goging down
ldeng31 1:57f1410e9614 29 wait(moving_time2);
ldeng31 1:57f1410e9614 30 m2.speed(0); //Connector Actuator Stops at the start position
ldeng31 1:57f1410e9614 31 m1.speed(-1); //Connector Actuator Goging down
ldeng31 1:57f1410e9614 32 wait(moving_time1);
ldeng31 1:57f1410e9614 33 m1.speed(0); //Table Actuator Stops at the start position
buntyshubho 0:4b7f01607612 34 }
conantina 2:9a9fbbf43957 35
ldeng31 1:57f1410e9614 36
ldeng31 1:57f1410e9614 37 int main() {
conantina 2:9a9fbbf43957 38 load_sensor.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 39 load_sensor.attach_deasserted(&start_the_schedule);
conantina 2:9a9fbbf43957 40 load_sensor.setSampleFrequency();
ldeng31 1:57f1410e9614 41 start.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 42 start.attach_deasserted(&start_the_schedule);
ldeng31 1:57f1410e9614 43 start.setSampleFrequency();
ldeng31 1:57f1410e9614 44 reset.mode(PullUp); wait(0.01);
ldeng31 1:57f1410e9614 45 reset.attach_deasserted(&reset_to_zero);
ldeng31 1:57f1410e9614 46 reset.setSampleFrequency();
conantina 2:9a9fbbf43957 47
conantina 4:580ea208038b 48
ldeng31 1:57f1410e9614 49 while(1) {
conantina 4:580ea208038b 50 // m1.speed(-1); //Connector Actuator Goging down
conantina 4:580ea208038b 51 //wait(moving_time1);
conantina 4:580ea208038b 52 //m1.speed(0);
conantina 4:580ea208038b 53 m2.speed(-1); //Connector Actuator Goging down
conantina 4:580ea208038b 54 wait(moving_time2);
conantina 4:580ea208038b 55 m2.speed(0);
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 4:580ea208038b 81 is_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 5:a1b28b06425a 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 5:a1b28b06425a 93 float moving_time1 = 10.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 5:a1b28b06425a 108 DigitalOut led(LED1);
conantina 5:a1b28b06425a 109 DigitalOut led2(LED2);
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 5:a1b28b06425a 146 led = 0;
conantina 2:9a9fbbf43957 147 for(int i = 0; i<(moving_time2/pulse_width);i++){
conantina 2:9a9fbbf43957 148 fall(m2,pulse_width);
conantina 2:9a9fbbf43957 149 }
conantina 2:9a9fbbf43957 150 for(int i = 0; i<(moving_time1/pulse_width);i++){
conantina 2:9a9fbbf43957 151 fall(m1,pulse_width);
conantina 2:9a9fbbf43957 152 }
conantina 2:9a9fbbf43957 153 is_reset = 0;
conantina 2:9a9fbbf43957 154 }
conantina 2:9a9fbbf43957 155
conantina 2:9a9fbbf43957 156 void schedule()
conantina 2:9a9fbbf43957 157 {
conantina 2:9a9fbbf43957 158 for(int i=0;i<(moving_time1/pulse_width);i++) //Table Actuator lift the table up
conantina 2:9a9fbbf43957 159 { if(is_reset){reset_schedule();return;}
conantina 5:a1b28b06425a 160 led = !led;
conantina 2:9a9fbbf43957 161 raise(m1,pulse_width);
conantina 2:9a9fbbf43957 162 }
conantina 5:a1b28b06425a 163 led2 = 1;
conantina 5:a1b28b06425a 164 wait(0.2);
conantina 5:a1b28b06425a 165 led2 = 0;
conantina 2:9a9fbbf43957 166 for(int i=0;i<(wait_time/pulse_width);i++) //Waiting for the quadcoptor to slide into the triangle hindge
conantina 2:9a9fbbf43957 167 { if(is_reset){reset_schedule();return;}
conantina 5:a1b28b06425a 168 led = !led;
conantina 2:9a9fbbf43957 169 hold();
conantina 2:9a9fbbf43957 170 }
conantina 5:a1b28b06425a 171 led2 = 1;
conantina 5:a1b28b06425a 172 wait(0.2);
conantina 5:a1b28b06425a 173 led2 = 0;
conantina 5:a1b28b06425a 174 for(int i = 0; i<(moving_time1/pulse_width);i++) //Table Actuator goes down
conantina 5:a1b28b06425a 175 { if(is_reset){reset_schedule();return;}
conantina 5:a1b28b06425a 176 led = !led;
conantina 5:a1b28b06425a 177 fall(m1,pulse_width);
conantina 5:a1b28b06425a 178 }
conantina 5:a1b28b06425a 179 led2 = 1;
conantina 5:a1b28b06425a 180 wait(0.2);
conantina 5:a1b28b06425a 181 led2 = 0;
conantina 2:9a9fbbf43957 182 for(int i = 0; i<(moving_time2/pulse_width);i++) //Connector Actuator lifts up
conantina 2:9a9fbbf43957 183 { if(is_reset){reset_schedule();return;}
conantina 5:a1b28b06425a 184 led = !led;
conantina 2:9a9fbbf43957 185 raise(m2,pulse_width);
conantina 2:9a9fbbf43957 186 }
conantina 5:a1b28b06425a 187 led2 = 1;
conantina 5:a1b28b06425a 188 wait(0.2);
conantina 5:a1b28b06425a 189 led2 = 0;
conantina 2:9a9fbbf43957 190 for(int i=0;i<(charging_time/pulse_width);i++) //Waiting for the battery to charge
conantina 2:9a9fbbf43957 191 { if(is_reset){reset_schedule();return;}
conantina 5:a1b28b06425a 192 led = !led;
conantina 2:9a9fbbf43957 193 hold();
conantina 2:9a9fbbf43957 194 }
conantina 5:a1b28b06425a 195 led2 = 1;
conantina 5:a1b28b06425a 196 wait(0.2);
conantina 5:a1b28b06425a 197 led2 = 0;
conantina 2:9a9fbbf43957 198 for(int i = 0; i<(moving_time2/pulse_width);i++) //Connector Actuator goes down
conantina 2:9a9fbbf43957 199 { if(is_reset){reset_schedule();return;}
conantina 5:a1b28b06425a 200 led = !led;
conantina 2:9a9fbbf43957 201 fall(m2,pulse_width);
conantina 2:9a9fbbf43957 202 }
conantina 5:a1b28b06425a 203 led2 = 1;
conantina 5:a1b28b06425a 204 wait(0.2);
conantina 5:a1b28b06425a 205 led2 = 0;
conantina 2:9a9fbbf43957 206 for(int i=0;i<(hold_time/pulse_width);i++) //Waiting for the quadcoptpr to take off
conantina 2:9a9fbbf43957 207 { if(is_reset){reset_schedule();return;}
conantina 5:a1b28b06425a 208 led = !led;
conantina 2:9a9fbbf43957 209 hold();
conantina 2:9a9fbbf43957 210 }
conantina 5:a1b28b06425a 211 led2 = 1;
conantina 5:a1b28b06425a 212 wait(0.2);
conantina 5:a1b28b06425a 213 led2 = 0;
conantina 5:a1b28b06425a 214
conantina 2:9a9fbbf43957 215 }
conantina 2:9a9fbbf43957 216
conantina 2:9a9fbbf43957 217 //********************************************* Main Function *********************************************************
conantina 2:9a9fbbf43957 218 int main()
conantina 2:9a9fbbf43957 219 {
conantina 2:9a9fbbf43957 220 //init interuppt
conantina 2:9a9fbbf43957 221 load_sensor.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 222 load_sensor.attach_deasserted(&start_the_schedule);
conantina 2:9a9fbbf43957 223 load_sensor.setSampleFrequency();
conantina 2:9a9fbbf43957 224 start.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 225 start.attach_deasserted(&start_the_schedule);
conantina 2:9a9fbbf43957 226 start.setSampleFrequency();
conantina 2:9a9fbbf43957 227 reset.mode(PullUp); wait(0.01);
conantina 2:9a9fbbf43957 228 reset.attach_deasserted(&break_loop);
conantina 2:9a9fbbf43957 229 reset.setSampleFrequency();
conantina 2:9a9fbbf43957 230 while(1){
conantina 5:a1b28b06425a 231 led = 1;
conantina 2:9a9fbbf43957 232 if(running_schedule){
conantina 2:9a9fbbf43957 233 schedule();
conantina 3:473269e5a06f 234 running_schedule = 0; //return to reset after finish the schedule
conantina 2:9a9fbbf43957 235 }
conantina 5:a1b28b06425a 236 if(is_reset){
conantina 5:a1b28b06425a 237 reset_schedule();
conantina 5:a1b28b06425a 238 }
conantina 2:9a9fbbf43957 239 }
conantina 2:9a9fbbf43957 240 }
conantina 5:a1b28b06425a 241