Nurbol Nurdaulet / Mbed 2 deprecated state_machine_modes25_11_11

Dependencies:   mbed WattBob_TextLCD globals

Committer:
Nurbol
Date:
Fri Nov 25 10:42:48 2011 +0000
Revision:
0:d8528fbbaf9c
Child:
1:a91950a8e20f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nurbol 0:d8528fbbaf9c 1 //
Nurbol 0:d8528fbbaf9c 2 // COM_test : program to communicate via a COM port to a PC/laptop
Nurbol 0:d8528fbbaf9c 3 // ========
Nurbol 0:d8528fbbaf9c 4 //
Nurbol 0:d8528fbbaf9c 5 // Description
Nurbol 0:d8528fbbaf9c 6 // Program to receive ASCII format commands through a virtual COM port and
Nurbol 0:d8528fbbaf9c 7 // after executing the command return some data (optional, depending on
Nurbol 0:d8528fbbaf9c 8 // the command) and a status value.
Nurbol 0:d8528fbbaf9c 9 // The program understands two possible commands :
Nurbol 0:d8528fbbaf9c 10 // "s i j" : move servo 'i (0 to 5) to postion 'j' (0 to 90)
Nurbol 0:d8528fbbaf9c 11 // "r" : read the 'i' parameter of the last "s" command
Nurbol 0:d8528fbbaf9c 12 //
Nurbol 0:d8528fbbaf9c 13 // Version : 1.0
Nurbol 0:d8528fbbaf9c 14 // Author : Jim Herd
Nurbol 0:d8528fbbaf9c 15 // Date : 5th Oct 2011
Nurbol 0:d8528fbbaf9c 16 //
Nurbol 0:d8528fbbaf9c 17 #include "mbed.h"
Nurbol 0:d8528fbbaf9c 18 #include "MCP23017.h"
Nurbol 0:d8528fbbaf9c 19 #include "WattBob_TextLCD.h"
Nurbol 0:d8528fbbaf9c 20 #include "cmd_io.h"
Nurbol 0:d8528fbbaf9c 21 #include "globals.h"
Nurbol 0:d8528fbbaf9c 22
Nurbol 0:d8528fbbaf9c 23 #define MAINT_MODE "y" //add define to receive mode value
Nurbol 0:d8528fbbaf9c 24 #define SORT_MODE "z" //add define to receive mode value
Nurbol 0:d8528fbbaf9c 25
Nurbol 0:d8528fbbaf9c 26 //******************************************************************************
Nurbol 0:d8528fbbaf9c 27 //declare ticker
Nurbol 0:d8528fbbaf9c 28 //
Nurbol 0:d8528fbbaf9c 29 Ticker timer1p;
Nurbol 0:d8528fbbaf9c 30 Ticker timer2p;
Nurbol 0:d8528fbbaf9c 31 Ticker timercounter1p;
Nurbol 0:d8528fbbaf9c 32 Ticker timercounter2p;
Nurbol 0:d8528fbbaf9c 33 Ticker timerstatemachine;
Nurbol 0:d8528fbbaf9c 34
Nurbol 0:d8528fbbaf9c 35 //******************************************************************************
Nurbol 0:d8528fbbaf9c 36 //declare Pin
Nurbol 0:d8528fbbaf9c 37 //
Nurbol 0:d8528fbbaf9c 38 AnalogIn sensor1(p15);
Nurbol 0:d8528fbbaf9c 39 AnalogIn sensor2(p16);
Nurbol 0:d8528fbbaf9c 40 DigitalOut valueLED1(p23);
Nurbol 0:d8528fbbaf9c 41 DigitalOut valueLED2(p25);
Nurbol 0:d8528fbbaf9c 42
Nurbol 0:d8528fbbaf9c 43
Nurbol 0:d8528fbbaf9c 44 DigitalOut led1(LED1);
Nurbol 0:d8528fbbaf9c 45 DigitalOut led2(LED2);
Nurbol 0:d8528fbbaf9c 46 DigitalOut led3(LED3);
Nurbol 0:d8528fbbaf9c 47 DigitalOut led4(LED4);
Nurbol 0:d8528fbbaf9c 48
Nurbol 0:d8528fbbaf9c 49 DigitalOut clk(p24);
Nurbol 0:d8528fbbaf9c 50
Nurbol 0:d8528fbbaf9c 51 DigitalIn counter1p(p5);
Nurbol 0:d8528fbbaf9c 52 DigitalIn counter2p(p6);
Nurbol 0:d8528fbbaf9c 53 DigitalIn SW1p(p11);
Nurbol 0:d8528fbbaf9c 54 DigitalIn SW2p(p12);
Nurbol 0:d8528fbbaf9c 55
Nurbol 0:d8528fbbaf9c 56 //******************************************************************************
Nurbol 0:d8528fbbaf9c 57 //declare variable
Nurbol 0:d8528fbbaf9c 58 //
Nurbol 0:d8528fbbaf9c 59 bool Position1_1p;
Nurbol 0:d8528fbbaf9c 60 bool Position2_1p;
Nurbol 0:d8528fbbaf9c 61 bool Position1_2p;
Nurbol 0:d8528fbbaf9c 62 bool Position2_2p;
Nurbol 0:d8528fbbaf9c 63
Nurbol 0:d8528fbbaf9c 64 bool bSort_Mode; //to use in the code to define the mode
Nurbol 0:d8528fbbaf9c 65 bool bMaint_Mode;//to use in the code to define the mode
Nurbol 0:d8528fbbaf9c 66
Nurbol 0:d8528fbbaf9c 67 int state;
Nurbol 0:d8528fbbaf9c 68
Nurbol 0:d8528fbbaf9c 69
Nurbol 0:d8528fbbaf9c 70
Nurbol 0:d8528fbbaf9c 71 //******************************************************************************
Nurbol 0:d8528fbbaf9c 72 // declare functions
Nurbol 0:d8528fbbaf9c 73 //
Nurbol 0:d8528fbbaf9c 74 void sensor1p (void);
Nurbol 0:d8528fbbaf9c 75 void sensor2p (void);
Nurbol 0:d8528fbbaf9c 76 void counter1 (void);
Nurbol 0:d8528fbbaf9c 77 void counter2 (void);
Nurbol 0:d8528fbbaf9c 78 void state_machine (void);
Nurbol 0:d8528fbbaf9c 79 //
Nurbol 0:d8528fbbaf9c 80 //
Nurbol 0:d8528fbbaf9c 81 // 2. five servo outputs
Nurbol 0:d8528fbbaf9c 82 //
Nurbol 0:d8528fbbaf9c 83
Nurbol 0:d8528fbbaf9c 84 PwmOut servo_0(p26);
Nurbol 0:d8528fbbaf9c 85 //PwmOut servo_1(p25);
Nurbol 0:d8528fbbaf9c 86 //PwmOut servo_2(p24);
Nurbol 0:d8528fbbaf9c 87 //PwmOut servo_3(p23);
Nurbol 0:d8528fbbaf9c 88 PwmOut servo_4(p22);
Nurbol 0:d8528fbbaf9c 89 PwmOut servo_5(p21);
Nurbol 0:d8528fbbaf9c 90
Nurbol 0:d8528fbbaf9c 91 //
Nurbol 0:d8528fbbaf9c 92 // 3. objects necessary to use the 2*16 character MBED display
Nurbol 0:d8528fbbaf9c 93 //
Nurbol 0:d8528fbbaf9c 94 MCP23017 *par_port;
Nurbol 0:d8528fbbaf9c 95 WattBob_TextLCD *lcd;
Nurbol 0:d8528fbbaf9c 96 //
Nurbol 0:d8528fbbaf9c 97 // 4. Virtual COM port over USB link to laptop/PC
Nurbol 0:d8528fbbaf9c 98 //
Nurbol 0:d8528fbbaf9c 99 Serial pc(USBTX, USBRX);
Nurbol 0:d8528fbbaf9c 100
Nurbol 0:d8528fbbaf9c 101 //******************************************************************************
Nurbol 0:d8528fbbaf9c 102 // Defined GLOBAL variables and structures
Nurbol 0:d8528fbbaf9c 103 //
Nurbol 0:d8528fbbaf9c 104 CMD_STRUCT ext_cmd, commandcounter2; // structure to hold command data
Nurbol 0:d8528fbbaf9c 105 STAT_STRUCT ext_stat; // structure to hold status reply
Nurbol 0:d8528fbbaf9c 106
Nurbol 0:d8528fbbaf9c 107
Nurbol 0:d8528fbbaf9c 108 //CMD_STRUCT commandcounter2;
Nurbol 0:d8528fbbaf9c 109
Nurbol 0:d8528fbbaf9c 110 uint32_t last_servo; // store for last servo number
Nurbol 0:d8528fbbaf9c 111
Nurbol 0:d8528fbbaf9c 112 //************************************************************************
Nurbol 0:d8528fbbaf9c 113 //************************************************************************
Nurbol 0:d8528fbbaf9c 114 // init_sys : initialise the system
Nurbol 0:d8528fbbaf9c 115 // ========
Nurbol 0:d8528fbbaf9c 116 //
Nurbol 0:d8528fbbaf9c 117 // 1. Configure 2*16 character display
Nurbol 0:d8528fbbaf9c 118 // 2. Print "COM test" string
Nurbol 0:d8528fbbaf9c 119 // 3. initialise relevant global variables
Nurbol 0:d8528fbbaf9c 120 // 4. set COM port baud rate to 19200 bits per second
Nurbol 0:d8528fbbaf9c 121 //
Nurbol 0:d8528fbbaf9c 122 void init_sys(void) {
Nurbol 0:d8528fbbaf9c 123
Nurbol 0:d8528fbbaf9c 124 par_port = new MCP23017(p9, p10, 0x40);
Nurbol 0:d8528fbbaf9c 125 lcd = new WattBob_TextLCD(par_port);
Nurbol 0:d8528fbbaf9c 126
Nurbol 0:d8528fbbaf9c 127 par_port->write_bit(1,BL_BIT); // turn LCD backlight ON
Nurbol 0:d8528fbbaf9c 128 lcd->cls();
Nurbol 0:d8528fbbaf9c 129 lcd->locate(0,0);
Nurbol 0:d8528fbbaf9c 130 lcd->printf("W3C: Wheel Coin Counter Company");
Nurbol 0:d8528fbbaf9c 131
Nurbol 0:d8528fbbaf9c 132 servo_0.period(0.020); // servo requires a 20ms period, common for all 5 servo objects
Nurbol 0:d8528fbbaf9c 133 last_servo = SERVO_UNKNOWN;
Nurbol 0:d8528fbbaf9c 134 pc.baud(19200);
Nurbol 0:d8528fbbaf9c 135
Nurbol 0:d8528fbbaf9c 136 servo_0.pulsewidth_us(1000 + (0 * 1000) / 90);
Nurbol 0:d8528fbbaf9c 137 servo_5.pulsewidth_us(1000 + (0 * 1000) / 90);
Nurbol 0:d8528fbbaf9c 138
Nurbol 0:d8528fbbaf9c 139 Position1_1p = 1;
Nurbol 0:d8528fbbaf9c 140 Position2_1p = 0;
Nurbol 0:d8528fbbaf9c 141 Position1_2p = 1;
Nurbol 0:d8528fbbaf9c 142 Position2_2p = 0;
Nurbol 0:d8528fbbaf9c 143
Nurbol 0:d8528fbbaf9c 144 state = 0;
Nurbol 0:d8528fbbaf9c 145
Nurbol 0:d8528fbbaf9c 146 return;
Nurbol 0:d8528fbbaf9c 147 } // end init_sys
Nurbol 0:d8528fbbaf9c 148
Nurbol 0:d8528fbbaf9c 149 //************************************************************************
Nurbol 0:d8528fbbaf9c 150 // process_cmd : decode and execute command
Nurbol 0:d8528fbbaf9c 151 // ===========
Nurbol 0:d8528fbbaf9c 152 uint32_t process_cmd(CMD_STRUCT *command)
Nurbol 0:d8528fbbaf9c 153 {
Nurbol 0:d8528fbbaf9c 154 int32_t pulse_width;
Nurbol 0:d8528fbbaf9c 155
Nurbol 0:d8528fbbaf9c 156 switch (command->cmd_code) {
Nurbol 0:d8528fbbaf9c 157 //
Nurbol 0:d8528fbbaf9c 158 // move a servo
Nurbol 0:d8528fbbaf9c 159 //
Nurbol 0:d8528fbbaf9c 160 case SERVO_CMD :
Nurbol 0:d8528fbbaf9c 161 command->nos_data = 0; // no data to be returned
Nurbol 0:d8528fbbaf9c 162 //
Nurbol 0:d8528fbbaf9c 163 // check that parameters are OK
Nurbol 0:d8528fbbaf9c 164 //
Nurbol 0:d8528fbbaf9c 165 if (command->nos_params != 2) { // check for 2 parameters
Nurbol 0:d8528fbbaf9c 166 command->result_status = CMD_BAD_NUMBER_OF_PARAMETERS;
Nurbol 0:d8528fbbaf9c 167 break;
Nurbol 0:d8528fbbaf9c 168 }
Nurbol 0:d8528fbbaf9c 169 if (command->param[0] > MAX_SERVO_NUMBER ) { // check servo number
Nurbol 0:d8528fbbaf9c 170 command->result_status = CMD_BAD_SERVO_NUMBER;
Nurbol 0:d8528fbbaf9c 171 break;
Nurbol 0:d8528fbbaf9c 172 }
Nurbol 0:d8528fbbaf9c 173 if ((command->param[1] < MIN_SERVO_ANGLE) ||
Nurbol 0:d8528fbbaf9c 174 (command->param[1] > MAX_SERVO_ANGLE) ) {
Nurbol 0:d8528fbbaf9c 175 command->result_status = CMD_BAD_SERVO_VALUE;
Nurbol 0:d8528fbbaf9c 176 break;
Nurbol 0:d8528fbbaf9c 177 }
Nurbol 0:d8528fbbaf9c 178 if ((command->param[0] == 4) && (command->param[1] == 0)) {
Nurbol 0:d8528fbbaf9c 179 pulse_width = 0; // convert angle to pulse width
Nurbol 0:d8528fbbaf9c 180 }
Nurbol 0:d8528fbbaf9c 181 else{
Nurbol 0:d8528fbbaf9c 182 pulse_width = 1000 + (command->param[1] * 1000) / MAX_SERVO_ANGLE; // convert angle to pulse width
Nurbol 0:d8528fbbaf9c 183 }
Nurbol 0:d8528fbbaf9c 184
Nurbol 0:d8528fbbaf9c 185
Nurbol 0:d8528fbbaf9c 186
Nurbol 0:d8528fbbaf9c 187 //
Nurbol 0:d8528fbbaf9c 188 // implement servo move to all 5 servos
Nurbol 0:d8528fbbaf9c 189 //
Nurbol 0:d8528fbbaf9c 190 switch (command->param[0]) {
Nurbol 0:d8528fbbaf9c 191 case 0 : servo_0.pulsewidth_us(pulse_width); break;
Nurbol 0:d8528fbbaf9c 192 case 4 : servo_4.pulsewidth_us(pulse_width); break;
Nurbol 0:d8528fbbaf9c 193 case 5 : servo_5.pulsewidth_us(pulse_width); break;
Nurbol 0:d8528fbbaf9c 194
Nurbol 0:d8528fbbaf9c 195 }
Nurbol 0:d8528fbbaf9c 196 last_servo = command->param[0];
Nurbol 0:d8528fbbaf9c 197 break;
Nurbol 0:d8528fbbaf9c 198 //
Nurbol 0:d8528fbbaf9c 199 // return last servo number
Nurbol 0:d8528fbbaf9c 200 //
Nurbol 0:d8528fbbaf9c 201 case READ_CMD :
Nurbol 0:d8528fbbaf9c 202 command->nos_data = 2;// no data to be returned
Nurbol 0:d8528fbbaf9c 203 if((bSort_Mode == 0)&&(bMaint_Mode == 1)){
Nurbol 0:d8528fbbaf9c 204 command->result_data[0] = valueLED1;
Nurbol 0:d8528fbbaf9c 205 command->result_data[1] = valueLED2;
Nurbol 0:d8528fbbaf9c 206 }
Nurbol 0:d8528fbbaf9c 207 else if((bSort_Mode == 1)&&(bMaint_Mode == 0)){
Nurbol 0:d8528fbbaf9c 208 command->result_data[0] = counter1p;
Nurbol 0:d8528fbbaf9c 209 command->result_data[1] = counter2p;
Nurbol 0:d8528fbbaf9c 210 }
Nurbol 0:d8528fbbaf9c 211 break;
Nurbol 0:d8528fbbaf9c 212
Nurbol 0:d8528fbbaf9c 213 //
Nurbol 0:d8528fbbaf9c 214 // Mode value
Nurbol 0:d8528fbbaf9c 215 //
Nurbol 0:d8528fbbaf9c 216 case MAINT_MODE :
Nurbol 0:d8528fbbaf9c 217 bSort_Mode = 0;
Nurbol 0:d8528fbbaf9c 218 bMaint_Mode = 1;
Nurbol 0:d8528fbbaf9c 219 break;
Nurbol 0:d8528fbbaf9c 220
Nurbol 0:d8528fbbaf9c 221
Nurbol 0:d8528fbbaf9c 222 case SORT_MODE :
Nurbol 0:d8528fbbaf9c 223 bSort_Mode = 1;
Nurbol 0:d8528fbbaf9c 224 bMaint_Mode = 0;
Nurbol 0:d8528fbbaf9c 225 break;
Nurbol 0:d8528fbbaf9c 226 //
Nurbol 0:d8528fbbaf9c 227 // catch any problems
Nurbol 0:d8528fbbaf9c 228 //
Nurbol 0:d8528fbbaf9c 229 default:
Nurbol 0:d8528fbbaf9c 230 command->nos_data = 0; // no data to be returned
Nurbol 0:d8528fbbaf9c 231 command->result_status = CMD_BAD_SERVO_VALUE;
Nurbol 0:d8528fbbaf9c 232 break;
Nurbol 0:d8528fbbaf9c 233 }
Nurbol 0:d8528fbbaf9c 234 return OK;
Nurbol 0:d8528fbbaf9c 235 }
Nurbol 0:d8528fbbaf9c 236
Nurbol 0:d8528fbbaf9c 237 //************************************************************************
Nurbol 0:d8528fbbaf9c 238
Nurbol 0:d8528fbbaf9c 239 //function to send value on the FPGA when 1p is detected
Nurbol 0:d8528fbbaf9c 240 void sensor1p (void){
Nurbol 0:d8528fbbaf9c 241 clk = !clk;
Nurbol 0:d8528fbbaf9c 242 wait(0.01);
Nurbol 0:d8528fbbaf9c 243 sensor1.read();
Nurbol 0:d8528fbbaf9c 244 if(sensor1 > 0.5) {
Nurbol 0:d8528fbbaf9c 245 led1 = 1;
Nurbol 0:d8528fbbaf9c 246 valueLED1 = 1;
Nurbol 0:d8528fbbaf9c 247 }
Nurbol 0:d8528fbbaf9c 248 else if(sensor1 < 0.5){
Nurbol 0:d8528fbbaf9c 249 led1 = 0;
Nurbol 0:d8528fbbaf9c 250 valueLED1 = 0;
Nurbol 0:d8528fbbaf9c 251 }
Nurbol 0:d8528fbbaf9c 252 }
Nurbol 0:d8528fbbaf9c 253
Nurbol 0:d8528fbbaf9c 254 //function to send value on the FPGA when 2p is detected
Nurbol 0:d8528fbbaf9c 255 void sensor2p (){
Nurbol 0:d8528fbbaf9c 256 sensor2.read();
Nurbol 0:d8528fbbaf9c 257 if(sensor2 > 0.5) {
Nurbol 0:d8528fbbaf9c 258 led2 = 1;
Nurbol 0:d8528fbbaf9c 259 valueLED2 = 1;
Nurbol 0:d8528fbbaf9c 260 }
Nurbol 0:d8528fbbaf9c 261 else if(sensor2 < 0.5){
Nurbol 0:d8528fbbaf9c 262 led2 = 0;
Nurbol 0:d8528fbbaf9c 263 valueLED2 = 0;
Nurbol 0:d8528fbbaf9c 264 }
Nurbol 0:d8528fbbaf9c 265 }
Nurbol 0:d8528fbbaf9c 266
Nurbol 0:d8528fbbaf9c 267
Nurbol 0:d8528fbbaf9c 268 //function for the state machine to move servos between 2 positions
Nurbol 0:d8528fbbaf9c 269 void state_machine (){
Nurbol 0:d8528fbbaf9c 270
Nurbol 0:d8528fbbaf9c 271 switch(state)
Nurbol 0:d8528fbbaf9c 272 {
Nurbol 0:d8528fbbaf9c 273 case 0: // initial state
Nurbol 0:d8528fbbaf9c 274 servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run
Nurbol 0:d8528fbbaf9c 275 servo_0.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 1p go to position 1
Nurbol 0:d8528fbbaf9c 276 servo_5.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 2p go to position 1
Nurbol 0:d8528fbbaf9c 277 Position1_1p = 1;
Nurbol 0:d8528fbbaf9c 278 Position2_1p = 0;
Nurbol 0:d8528fbbaf9c 279 Position1_2p = 1;
Nurbol 0:d8528fbbaf9c 280 Position2_2p = 0;
Nurbol 0:d8528fbbaf9c 281 led3 = 0;
Nurbol 0:d8528fbbaf9c 282 led4 = 0;
Nurbol 0:d8528fbbaf9c 283 counter1p.read();
Nurbol 0:d8528fbbaf9c 284 counter2p.read();
Nurbol 0:d8528fbbaf9c 285 if(SW1p == 0){
Nurbol 0:d8528fbbaf9c 286 if(counter1p > 0.5){
Nurbol 0:d8528fbbaf9c 287 state = 1;
Nurbol 0:d8528fbbaf9c 288 }
Nurbol 0:d8528fbbaf9c 289 }
Nurbol 0:d8528fbbaf9c 290 if(SW2p == 0){
Nurbol 0:d8528fbbaf9c 291 if(counter2p > 0.5){
Nurbol 0:d8528fbbaf9c 292 state = 4;
Nurbol 0:d8528fbbaf9c 293 }
Nurbol 0:d8528fbbaf9c 294 }
Nurbol 0:d8528fbbaf9c 295 break;
Nurbol 0:d8528fbbaf9c 296 case 1: // state 1 if counter1p = 1
Nurbol 0:d8528fbbaf9c 297 servo_4.pulsewidth_us(0); // motor stop
Nurbol 0:d8528fbbaf9c 298 servo_0.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 1p go to position 2
Nurbol 0:d8528fbbaf9c 299 wait(1);
Nurbol 0:d8528fbbaf9c 300 Position1_1p = 0;
Nurbol 0:d8528fbbaf9c 301 Position2_1p = 1;
Nurbol 0:d8528fbbaf9c 302 if((Position2_1p == 1)&&(counter1p < 0.5)){
Nurbol 0:d8528fbbaf9c 303 state = 2;
Nurbol 0:d8528fbbaf9c 304 }
Nurbol 0:d8528fbbaf9c 305 break;
Nurbol 0:d8528fbbaf9c 306 case 2: // state 2 if servo 1p is in position 2
Nurbol 0:d8528fbbaf9c 307 servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run
Nurbol 0:d8528fbbaf9c 308 counter1p.read();
Nurbol 0:d8528fbbaf9c 309 if(counter1p > 0.5){
Nurbol 0:d8528fbbaf9c 310 state = 3;
Nurbol 0:d8528fbbaf9c 311 }
Nurbol 0:d8528fbbaf9c 312 break;
Nurbol 0:d8528fbbaf9c 313 case 3: // state 3 if counter 1p = 1
Nurbol 0:d8528fbbaf9c 314 servo_4.pulsewidth_us(0); // motor stop
Nurbol 0:d8528fbbaf9c 315 led3 = 1;
Nurbol 0:d8528fbbaf9c 316 if(SW1p == 1){ // wait SW 1p to go to the initial state
Nurbol 0:d8528fbbaf9c 317 state = 0;
Nurbol 0:d8528fbbaf9c 318 }
Nurbol 0:d8528fbbaf9c 319 break;
Nurbol 0:d8528fbbaf9c 320 case 4: // state 4 if counter 2p = 1
Nurbol 0:d8528fbbaf9c 321 servo_4.pulsewidth_us(0); // motor stop
Nurbol 0:d8528fbbaf9c 322 servo_5.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 2p go to position 2
Nurbol 0:d8528fbbaf9c 323 wait(1);
Nurbol 0:d8528fbbaf9c 324 Position1_2p = 0;
Nurbol 0:d8528fbbaf9c 325 Position2_2p = 1;
Nurbol 0:d8528fbbaf9c 326 if((Position2_2p == 1)&&(counter2p < 0.5)){
Nurbol 0:d8528fbbaf9c 327 state = 5;
Nurbol 0:d8528fbbaf9c 328 }
Nurbol 0:d8528fbbaf9c 329 break;
Nurbol 0:d8528fbbaf9c 330 case 5: // state 5 if servo 2p is in position 2
Nurbol 0:d8528fbbaf9c 331 servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor run
Nurbol 0:d8528fbbaf9c 332 counter2p.read();
Nurbol 0:d8528fbbaf9c 333 if(counter2p > 0.5){
Nurbol 0:d8528fbbaf9c 334 state = 6;
Nurbol 0:d8528fbbaf9c 335 }
Nurbol 0:d8528fbbaf9c 336 break;
Nurbol 0:d8528fbbaf9c 337 case 6: // state 6 if counter 2p = 1
Nurbol 0:d8528fbbaf9c 338 servo_4.pulsewidth_us(0); // motor stop
Nurbol 0:d8528fbbaf9c 339 led4 = 1;
Nurbol 0:d8528fbbaf9c 340 if(SW2p == 1){ // wait SW 2p to go to the initial state
Nurbol 0:d8528fbbaf9c 341 state = 0;
Nurbol 0:d8528fbbaf9c 342 }
Nurbol 0:d8528fbbaf9c 343 break;
Nurbol 0:d8528fbbaf9c 344 }
Nurbol 0:d8528fbbaf9c 345 }
Nurbol 0:d8528fbbaf9c 346
Nurbol 0:d8528fbbaf9c 347
Nurbol 0:d8528fbbaf9c 348 //************************************************************************
Nurbol 0:d8528fbbaf9c 349 //
Nurbol 0:d8528fbbaf9c 350 int main() {
Nurbol 0:d8528fbbaf9c 351
Nurbol 0:d8528fbbaf9c 352 valueLED1=0;
Nurbol 0:d8528fbbaf9c 353 valueLED2=0;
Nurbol 0:d8528fbbaf9c 354 clk=0;
Nurbol 0:d8528fbbaf9c 355 init_sys();
Nurbol 0:d8528fbbaf9c 356
Nurbol 0:d8528fbbaf9c 357 FOREVER {
Nurbol 0:d8528fbbaf9c 358
Nurbol 0:d8528fbbaf9c 359 timer2p.attach(&sensor2p, 0.1); //function sensor2p is reading all the 0.1 ms
Nurbol 0:d8528fbbaf9c 360 timer1p.attach(&sensor1p, 0.1); //function sensor1p is reading all the 0.1 ms
Nurbol 0:d8528fbbaf9c 361
Nurbol 0:d8528fbbaf9c 362 if((bSort_Mode == 1)&&(bMaint_Mode == 0)){
Nurbol 0:d8528fbbaf9c 363 counter1p.read();
Nurbol 0:d8528fbbaf9c 364 counter2p.read();
Nurbol 0:d8528fbbaf9c 365
Nurbol 0:d8528fbbaf9c 366 timerstatemachine.attach(&state_machine, 0.1);
Nurbol 0:d8528fbbaf9c 367 }
Nurbol 0:d8528fbbaf9c 368
Nurbol 0:d8528fbbaf9c 369
Nurbol 0:d8528fbbaf9c 370 clk = !clk;
Nurbol 0:d8528fbbaf9c 371 wait(0.01);
Nurbol 0:d8528fbbaf9c 372
Nurbol 0:d8528fbbaf9c 373 get_cmd(&ext_cmd);
Nurbol 0:d8528fbbaf9c 374 //
Nurbol 0:d8528fbbaf9c 375 // Check status of read command activity and return an error status if there was a problem
Nurbol 0:d8528fbbaf9c 376 // If there is a problem, then return status code only and wait for next command.
Nurbol 0:d8528fbbaf9c 377 //
Nurbol 0:d8528fbbaf9c 378 if (ext_cmd.result_status != OK){
Nurbol 0:d8528fbbaf9c 379 send_status(ext_cmd.result_status);
Nurbol 0:d8528fbbaf9c 380 continue;
Nurbol 0:d8528fbbaf9c 381 }
Nurbol 0:d8528fbbaf9c 382 //
Nurbol 0:d8528fbbaf9c 383 // Parse command and return an error staus if there is a problem
Nurbol 0:d8528fbbaf9c 384 // If there is a problem, then return status code only and wait for next command.
Nurbol 0:d8528fbbaf9c 385 //
Nurbol 0:d8528fbbaf9c 386 parse_cmd(&ext_cmd);
Nurbol 0:d8528fbbaf9c 387 lcd->locate(1,0); lcd->printf(ext_cmd.cmd_str);
Nurbol 0:d8528fbbaf9c 388 if ((ext_cmd.result_status != OK) && (ext_cmd.cmd_code != TEXT_CMD)){
Nurbol 0:d8528fbbaf9c 389 lcd->locate(1,0); lcd->printf("parse : error");
Nurbol 0:d8528fbbaf9c 390 send_status(ext_cmd.result_status);
Nurbol 0:d8528fbbaf9c 391 continue;
Nurbol 0:d8528fbbaf9c 392 }
Nurbol 0:d8528fbbaf9c 393 //
Nurbol 0:d8528fbbaf9c 394 // Execute command and return an error staus if there is a problem
Nurbol 0:d8528fbbaf9c 395 //
Nurbol 0:d8528fbbaf9c 396 process_cmd(&ext_cmd);
Nurbol 0:d8528fbbaf9c 397 reply_to_cmd(&ext_cmd);
Nurbol 0:d8528fbbaf9c 398
Nurbol 0:d8528fbbaf9c 399
Nurbol 0:d8528fbbaf9c 400
Nurbol 0:d8528fbbaf9c 401 }
Nurbol 0:d8528fbbaf9c 402
Nurbol 0:d8528fbbaf9c 403 }
Nurbol 0:d8528fbbaf9c 404
Nurbol 0:d8528fbbaf9c 405
Nurbol 0:d8528fbbaf9c 406
Nurbol 0:d8528fbbaf9c 407
Nurbol 0:d8528fbbaf9c 408
Nurbol 0:d8528fbbaf9c 409
Nurbol 0:d8528fbbaf9c 410
Nurbol 0:d8528fbbaf9c 411
Nurbol 0:d8528fbbaf9c 412