Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed WattBob_TextLCD
Diff: main.cpp
- Revision:
- 1:05b399616fcd
- Parent:
- 0:99ea9a842829
- Child:
- 2:0d0ec8aa4605
--- a/main.cpp Thu Nov 24 00:01:43 2011 +0000 +++ b/main.cpp Thu Nov 24 12:10:17 2011 +0000 @@ -26,7 +26,7 @@ Ticker timer2p; Ticker timercounter1p; Ticker timercounter2p; - +Ticker timerstatemachine; AnalogIn sensor1(p15); AnalogIn sensor2(p16); @@ -51,7 +51,7 @@ bool Position1_2p; bool Position2_2p; -static int state(0); +int state; @@ -62,7 +62,7 @@ void sensor2p (void); void counter1 (void); void counter2 (void); - +void state_machine (void); // // // 2. five servo outputs @@ -129,6 +129,8 @@ Position1_2p = 1; Position2_2p = 0; + state = 0; + return; } // end init_sys @@ -233,6 +235,81 @@ } } + + +void state_machine (){ + switch(state) + { + case 0: // initial state + servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run + servo_0.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 1p go to position 1 + servo_5.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 2p go to position 1 + Position1_1p = 1; + Position2_1p = 0; + Position1_2p = 1; + Position2_2p = 0; + led3 = 0; + led4 = 0; + counter1p.read(); + counter2p.read(); + if(counter1p > 0.5){ + state = 1; + } + if(counter2p > 0.5){ + state = 4; + } + break; + case 1: // state 1 if counter1p = 1 + servo_4.pulsewidth_us(0); // motor stop + servo_0.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 1p go to position 2 + wait(1); + Position1_1p = 0; + Position2_1p = 1; + if(Position2_1p == 1){ + state = 2; + } + break; + case 2: // state 2 if servo 1p is in position 2 + servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run + if(counter1p > 0.5){ + state = 3; + } + break; + case 3: // state 3 if counter 1p = 1 + servo_4.pulsewidth_us(0); // motor stop + led3 = 1; + if(SW1p == 1){ // wait SW 1p to go to the initial state + state = 0; + } + break; + case 4: // state 4 if counter 2p = 1 + servo_4.pulsewidth_us(0); // motor stop + servo_5.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 2p go to position 2 + wait(1); + Position1_2p = 0; + Position2_2p = 1; + if((Position2_2p == 1)&&(counter2p < 0.5)){ + state = 5; + } + break; + case 5: // state 5 if servo 2p is in position 2 + servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor run + counter2p.read(); + if(counter2p > 0.5){ + state = 6; + } + break; + case 6: // state 6 if counter 2p = 1 + servo_4.pulsewidth_us(0); // motor stop + led4 = 1; + if(SW2p == 1){ // wait SW 2p to go to the initial state + state = 0; + } + break; + } +} + + //function to move servo when we have counter1p equal to 10 /* void counter1 (){ @@ -332,74 +409,13 @@ timer2p.attach(&sensor2p, 0.1); //function sensor2p is reading all the 0.1 ms timer1p.attach(&sensor1p, 0.1); //function sensor1p is reading all the 0.1 ms + counter1p.read(); + counter2p.read(); + + timerstatemachine.attach(&state_machine, 0.1); //state_machine - switch(state) - { - case 0: // initial state - servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run - servo_0.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 1p go to position 1 - servo_5.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 2p go to position 1 - Position1_1p = 1; - Position2_1p = 0; - Position1_2p = 1; - Position2_2p = 0; - led3 = 0; - led4 = 0; - if(counter1p == 1){ - state = 1; - } - if(counter2p == 1){ - state = 4; - } - break; - case 1: // state 1 if counter1p = 1 - servo_4.pulsewidth_us(0); // motor stop - servo_0.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 1p go to position 2 - wait(1); - Position1_1p = 0; - Position2_1p = 1; - if(Position2_1p == 1){ - state = 2; - } - break; - case 2: // state 2 if servo 1p is in position 2 - servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run - if(counter1p == 1){ - state = 3; - } - break; - case 3: // state 3 if counter 1p = 1 - servo_4.pulsewidth_us(0); // motor stop - led3 = 1; - if(SW1p == 1){ // wait SW 1p to go to the initial state - state = 0; - } - break; - case 4: // state 4 if counter 2p = 1 - servo_4.pulsewidth_us(0); // motor stop - servo_5.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 2p go to position 2 - wait(1); - Position1_2p = 0; - Position2_2p = 1; - if(Position2_2p == 1){ - state = 5; - } - break; - case 5: // state 5 if servo 2p is in position 2 - servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor run - if(counter2p == 1){ - state = 6; - } - break; - case 6: // state 6 if counter 2p = 1 - servo_4.pulsewidth_us(0); // motor stop - led4 = 1; - if(SW2p == 1){ // wait SW 2p to go to the initial state - state = 0; - } - break; - } + //end of the state machine