Nurbol Nurdaulet / Mbed 2 deprecated state_machine_modes25_11_11

Dependencies:   mbed WattBob_TextLCD globals

Committer:
Nurbol
Date:
Tue Nov 29 00:24:09 2011 +0000
Revision:
2:ea2d1441d485
Parent:
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 1:a91950a8e20f 23
Nurbol 0:d8528fbbaf9c 24
Nurbol 0:d8528fbbaf9c 25 //******************************************************************************
Nurbol 0:d8528fbbaf9c 26 //declare ticker
Nurbol 0:d8528fbbaf9c 27 //
Nurbol 0:d8528fbbaf9c 28 Ticker timer1p;
Nurbol 0:d8528fbbaf9c 29 Ticker timer2p;
Nurbol 0:d8528fbbaf9c 30 Ticker timercounter1p;
Nurbol 0:d8528fbbaf9c 31 Ticker timercounter2p;
Nurbol 0:d8528fbbaf9c 32 Ticker timerstatemachine;
Nurbol 0:d8528fbbaf9c 33
Nurbol 0:d8528fbbaf9c 34 //******************************************************************************
Nurbol 0:d8528fbbaf9c 35 //declare Pin
Nurbol 0:d8528fbbaf9c 36 //
Nurbol 0:d8528fbbaf9c 37 AnalogIn sensor1(p15);
Nurbol 0:d8528fbbaf9c 38 AnalogIn sensor2(p16);
Nurbol 0:d8528fbbaf9c 39 DigitalOut valueLED1(p23);
Nurbol 0:d8528fbbaf9c 40 DigitalOut valueLED2(p25);
Nurbol 0:d8528fbbaf9c 41
Nurbol 0:d8528fbbaf9c 42
Nurbol 0:d8528fbbaf9c 43 DigitalOut led1(LED1);
Nurbol 0:d8528fbbaf9c 44 DigitalOut led2(LED2);
Nurbol 0:d8528fbbaf9c 45 DigitalOut led3(LED3);
Nurbol 0:d8528fbbaf9c 46 DigitalOut led4(LED4);
Nurbol 0:d8528fbbaf9c 47
Nurbol 0:d8528fbbaf9c 48 DigitalOut clk(p24);
Nurbol 0:d8528fbbaf9c 49
Nurbol 0:d8528fbbaf9c 50 DigitalIn counter1p(p5);
Nurbol 0:d8528fbbaf9c 51 DigitalIn counter2p(p6);
Nurbol 0:d8528fbbaf9c 52 DigitalIn SW1p(p11);
Nurbol 0:d8528fbbaf9c 53 DigitalIn SW2p(p12);
Nurbol 0:d8528fbbaf9c 54
Nurbol 0:d8528fbbaf9c 55 //******************************************************************************
Nurbol 0:d8528fbbaf9c 56 //declare variable
Nurbol 0:d8528fbbaf9c 57 //
Nurbol 0:d8528fbbaf9c 58 bool Position1_1p;
Nurbol 0:d8528fbbaf9c 59 bool Position2_1p;
Nurbol 0:d8528fbbaf9c 60 bool Position1_2p;
Nurbol 0:d8528fbbaf9c 61 bool Position2_2p;
Nurbol 2:ea2d1441d485 62 bool Motor;
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 2:ea2d1441d485 104 CMD_STRUCT ext_cmd; // 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 2:ea2d1441d485 130 lcd->printf("W3C");
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 2:ea2d1441d485 182 pulse_width = 1000 + (command->param[1] * 1000) / 90; // 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 2:ea2d1441d485 202
Nurbol 2:ea2d1441d485 203 if((bSort_Mode == 0)&&(bMaint_Mode == 1)){
Nurbol 2:ea2d1441d485 204 command->nos_data = 2;// no data to be returned
Nurbol 0:d8528fbbaf9c 205 command->result_data[0] = valueLED1;
Nurbol 0:d8528fbbaf9c 206 command->result_data[1] = valueLED2;
Nurbol 0:d8528fbbaf9c 207 }
Nurbol 2:ea2d1441d485 208 else if((bSort_Mode == 1)&&(bMaint_Mode == 0)){
Nurbol 2:ea2d1441d485 209 command->nos_data = 7;// no data to be returned
Nurbol 2:ea2d1441d485 210 command->result_data[0] = valueLED1;// counter1p;
Nurbol 2:ea2d1441d485 211 command->result_data[1] = valueLED2;// counter2p;
Nurbol 2:ea2d1441d485 212 command->result_data[2] = Motor;
Nurbol 2:ea2d1441d485 213 command->result_data[3] = Position1_1p;
Nurbol 2:ea2d1441d485 214 command->result_data[4] = Position2_1p;
Nurbol 2:ea2d1441d485 215 command->result_data[5] = Position1_2p;
Nurbol 2:ea2d1441d485 216 command->result_data[6] = Position2_2p;
Nurbol 2:ea2d1441d485 217 send_data(&ext_cmd);
Nurbol 0:d8528fbbaf9c 218 }
Nurbol 0:d8528fbbaf9c 219 break;
Nurbol 0:d8528fbbaf9c 220
Nurbol 0:d8528fbbaf9c 221 //
Nurbol 0:d8528fbbaf9c 222 // Mode value
Nurbol 0:d8528fbbaf9c 223 //
Nurbol 0:d8528fbbaf9c 224 case MAINT_MODE :
Nurbol 0:d8528fbbaf9c 225 bSort_Mode = 0;
Nurbol 0:d8528fbbaf9c 226 bMaint_Mode = 1;
Nurbol 1:a91950a8e20f 227 servo_4.pulsewidth_us(0);
Nurbol 2:ea2d1441d485 228 servo_0.pulsewidth_us(0);
Nurbol 2:ea2d1441d485 229 servo_5.pulsewidth_us(0);
Nurbol 2:ea2d1441d485 230 lcd->cls();
Nurbol 2:ea2d1441d485 231 lcd->locate(0,0);
Nurbol 2:ea2d1441d485 232 lcd->printf("W3C");
Nurbol 2:ea2d1441d485 233 lcd->locate(1,0);
Nurbol 2:ea2d1441d485 234 lcd->printf("maintenance mode");
Nurbol 0:d8528fbbaf9c 235 break;
Nurbol 0:d8528fbbaf9c 236
Nurbol 0:d8528fbbaf9c 237
Nurbol 0:d8528fbbaf9c 238 case SORT_MODE :
Nurbol 0:d8528fbbaf9c 239 bSort_Mode = 1;
Nurbol 0:d8528fbbaf9c 240 bMaint_Mode = 0;
Nurbol 2:ea2d1441d485 241 lcd->cls();
Nurbol 2:ea2d1441d485 242 lcd->locate(0,0);
Nurbol 2:ea2d1441d485 243 lcd->printf("W3C");
Nurbol 2:ea2d1441d485 244 lcd->locate(1,0);
Nurbol 2:ea2d1441d485 245 lcd->printf("sort mode");
Nurbol 0:d8528fbbaf9c 246 break;
Nurbol 2:ea2d1441d485 247
Nurbol 2:ea2d1441d485 248 //
Nurbol 2:ea2d1441d485 249 // Urgency mode
Nurbol 2:ea2d1441d485 250 //
Nurbol 2:ea2d1441d485 251 case URGENCY :
Nurbol 2:ea2d1441d485 252 servo_4.pulsewidth_us(0);
Nurbol 2:ea2d1441d485 253 servo_0.pulsewidth_us(0);
Nurbol 2:ea2d1441d485 254 servo_5.pulsewidth_us(0);
Nurbol 2:ea2d1441d485 255 lcd->cls();
Nurbol 2:ea2d1441d485 256 lcd->locate(0,0);
Nurbol 2:ea2d1441d485 257 lcd->printf("W3C");
Nurbol 2:ea2d1441d485 258 lcd->locate(1,0);
Nurbol 2:ea2d1441d485 259 lcd->printf("urgency mode");
Nurbol 2:ea2d1441d485 260 break;
Nurbol 2:ea2d1441d485 261
Nurbol 0:d8528fbbaf9c 262 //
Nurbol 0:d8528fbbaf9c 263 // catch any problems
Nurbol 0:d8528fbbaf9c 264 //
Nurbol 0:d8528fbbaf9c 265 default:
Nurbol 0:d8528fbbaf9c 266 command->nos_data = 0; // no data to be returned
Nurbol 0:d8528fbbaf9c 267 command->result_status = CMD_BAD_SERVO_VALUE;
Nurbol 0:d8528fbbaf9c 268 break;
Nurbol 0:d8528fbbaf9c 269 }
Nurbol 0:d8528fbbaf9c 270 return OK;
Nurbol 0:d8528fbbaf9c 271 }
Nurbol 0:d8528fbbaf9c 272
Nurbol 0:d8528fbbaf9c 273 //************************************************************************
Nurbol 0:d8528fbbaf9c 274
Nurbol 0:d8528fbbaf9c 275 //function to send value on the FPGA when 1p is detected
Nurbol 0:d8528fbbaf9c 276 void sensor1p (void){
Nurbol 0:d8528fbbaf9c 277 clk = !clk;
Nurbol 0:d8528fbbaf9c 278 wait(0.01);
Nurbol 0:d8528fbbaf9c 279 sensor1.read();
Nurbol 0:d8528fbbaf9c 280 if(sensor1 > 0.5) {
Nurbol 0:d8528fbbaf9c 281 led1 = 1;
Nurbol 1:a91950a8e20f 282 if((bSort_Mode == 1)&&(bMaint_Mode == 0)){
Nurbol 1:a91950a8e20f 283 valueLED1 = 1;
Nurbol 1:a91950a8e20f 284 }
Nurbol 0:d8528fbbaf9c 285 }
Nurbol 0:d8528fbbaf9c 286 else if(sensor1 < 0.5){
Nurbol 0:d8528fbbaf9c 287 led1 = 0;
Nurbol 1:a91950a8e20f 288 if((bSort_Mode == 1)&&(bMaint_Mode == 0)){
Nurbol 1:a91950a8e20f 289 valueLED1 = 0;
Nurbol 1:a91950a8e20f 290 }
Nurbol 0:d8528fbbaf9c 291 }
Nurbol 0:d8528fbbaf9c 292 }
Nurbol 0:d8528fbbaf9c 293
Nurbol 0:d8528fbbaf9c 294 //function to send value on the FPGA when 2p is detected
Nurbol 0:d8528fbbaf9c 295 void sensor2p (){
Nurbol 0:d8528fbbaf9c 296 sensor2.read();
Nurbol 0:d8528fbbaf9c 297 if(sensor2 > 0.5) {
Nurbol 0:d8528fbbaf9c 298 led2 = 1;
Nurbol 0:d8528fbbaf9c 299 valueLED2 = 1;
Nurbol 0:d8528fbbaf9c 300 }
Nurbol 0:d8528fbbaf9c 301 else if(sensor2 < 0.5){
Nurbol 0:d8528fbbaf9c 302 led2 = 0;
Nurbol 0:d8528fbbaf9c 303 valueLED2 = 0;
Nurbol 0:d8528fbbaf9c 304 }
Nurbol 0:d8528fbbaf9c 305 }
Nurbol 0:d8528fbbaf9c 306
Nurbol 0:d8528fbbaf9c 307
Nurbol 0:d8528fbbaf9c 308 //function for the state machine to move servos between 2 positions
Nurbol 0:d8528fbbaf9c 309 void state_machine (){
Nurbol 1:a91950a8e20f 310 if((bSort_Mode == 1)&&(bMaint_Mode == 0)){
Nurbol 0:d8528fbbaf9c 311 switch(state)
Nurbol 0:d8528fbbaf9c 312 {
Nurbol 0:d8528fbbaf9c 313 case 0: // initial state
Nurbol 0:d8528fbbaf9c 314 servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run
Nurbol 0:d8528fbbaf9c 315 servo_0.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 1p go to position 1
Nurbol 0:d8528fbbaf9c 316 servo_5.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 2p go to position 1
Nurbol 0:d8528fbbaf9c 317 Position1_1p = 1;
Nurbol 0:d8528fbbaf9c 318 Position2_1p = 0;
Nurbol 0:d8528fbbaf9c 319 Position1_2p = 1;
Nurbol 0:d8528fbbaf9c 320 Position2_2p = 0;
Nurbol 2:ea2d1441d485 321 Motor = 1;
Nurbol 0:d8528fbbaf9c 322 led3 = 0;
Nurbol 0:d8528fbbaf9c 323 led4 = 0;
Nurbol 0:d8528fbbaf9c 324 counter1p.read();
Nurbol 0:d8528fbbaf9c 325 counter2p.read();
Nurbol 0:d8528fbbaf9c 326 if(SW1p == 0){
Nurbol 0:d8528fbbaf9c 327 if(counter1p > 0.5){
Nurbol 0:d8528fbbaf9c 328 state = 1;
Nurbol 0:d8528fbbaf9c 329 }
Nurbol 0:d8528fbbaf9c 330 }
Nurbol 0:d8528fbbaf9c 331 if(SW2p == 0){
Nurbol 0:d8528fbbaf9c 332 if(counter2p > 0.5){
Nurbol 0:d8528fbbaf9c 333 state = 4;
Nurbol 0:d8528fbbaf9c 334 }
Nurbol 0:d8528fbbaf9c 335 }
Nurbol 0:d8528fbbaf9c 336 break;
Nurbol 0:d8528fbbaf9c 337 case 1: // state 1 if counter1p = 1
Nurbol 0:d8528fbbaf9c 338 servo_4.pulsewidth_us(0); // motor stop
Nurbol 0:d8528fbbaf9c 339 servo_0.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 1p go to position 2
Nurbol 0:d8528fbbaf9c 340 wait(1);
Nurbol 0:d8528fbbaf9c 341 Position1_1p = 0;
Nurbol 0:d8528fbbaf9c 342 Position2_1p = 1;
Nurbol 2:ea2d1441d485 343 Motor = 0;
Nurbol 0:d8528fbbaf9c 344 if((Position2_1p == 1)&&(counter1p < 0.5)){
Nurbol 0:d8528fbbaf9c 345 state = 2;
Nurbol 0:d8528fbbaf9c 346 }
Nurbol 0:d8528fbbaf9c 347 break;
Nurbol 0:d8528fbbaf9c 348 case 2: // state 2 if servo 1p is in position 2
Nurbol 0:d8528fbbaf9c 349 servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run
Nurbol 2:ea2d1441d485 350 Motor = 1;
Nurbol 0:d8528fbbaf9c 351 counter1p.read();
Nurbol 0:d8528fbbaf9c 352 if(counter1p > 0.5){
Nurbol 0:d8528fbbaf9c 353 state = 3;
Nurbol 0:d8528fbbaf9c 354 }
Nurbol 0:d8528fbbaf9c 355 break;
Nurbol 0:d8528fbbaf9c 356 case 3: // state 3 if counter 1p = 1
Nurbol 0:d8528fbbaf9c 357 servo_4.pulsewidth_us(0); // motor stop
Nurbol 2:ea2d1441d485 358 Motor = 0;
Nurbol 0:d8528fbbaf9c 359 led3 = 1;
Nurbol 0:d8528fbbaf9c 360 if(SW1p == 1){ // wait SW 1p to go to the initial state
Nurbol 0:d8528fbbaf9c 361 state = 0;
Nurbol 0:d8528fbbaf9c 362 }
Nurbol 0:d8528fbbaf9c 363 break;
Nurbol 0:d8528fbbaf9c 364 case 4: // state 4 if counter 2p = 1
Nurbol 0:d8528fbbaf9c 365 servo_4.pulsewidth_us(0); // motor stop
Nurbol 0:d8528fbbaf9c 366 servo_5.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 2p go to position 2
Nurbol 0:d8528fbbaf9c 367 wait(1);
Nurbol 0:d8528fbbaf9c 368 Position1_2p = 0;
Nurbol 0:d8528fbbaf9c 369 Position2_2p = 1;
Nurbol 2:ea2d1441d485 370 Motor = 0;
Nurbol 0:d8528fbbaf9c 371 if((Position2_2p == 1)&&(counter2p < 0.5)){
Nurbol 0:d8528fbbaf9c 372 state = 5;
Nurbol 0:d8528fbbaf9c 373 }
Nurbol 0:d8528fbbaf9c 374 break;
Nurbol 0:d8528fbbaf9c 375 case 5: // state 5 if servo 2p is in position 2
Nurbol 0:d8528fbbaf9c 376 servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor run
Nurbol 2:ea2d1441d485 377 Motor = 1;
Nurbol 0:d8528fbbaf9c 378 counter2p.read();
Nurbol 0:d8528fbbaf9c 379 if(counter2p > 0.5){
Nurbol 0:d8528fbbaf9c 380 state = 6;
Nurbol 0:d8528fbbaf9c 381 }
Nurbol 0:d8528fbbaf9c 382 break;
Nurbol 0:d8528fbbaf9c 383 case 6: // state 6 if counter 2p = 1
Nurbol 0:d8528fbbaf9c 384 servo_4.pulsewidth_us(0); // motor stop
Nurbol 2:ea2d1441d485 385 Motor = 0;
Nurbol 0:d8528fbbaf9c 386 led4 = 1;
Nurbol 0:d8528fbbaf9c 387 if(SW2p == 1){ // wait SW 2p to go to the initial state
Nurbol 0:d8528fbbaf9c 388 state = 0;
Nurbol 0:d8528fbbaf9c 389 }
Nurbol 0:d8528fbbaf9c 390 break;
Nurbol 1:a91950a8e20f 391 }
Nurbol 1:a91950a8e20f 392 }
Nurbol 0:d8528fbbaf9c 393 }
Nurbol 0:d8528fbbaf9c 394
Nurbol 0:d8528fbbaf9c 395
Nurbol 0:d8528fbbaf9c 396 //************************************************************************
Nurbol 0:d8528fbbaf9c 397 //
Nurbol 0:d8528fbbaf9c 398 int main() {
Nurbol 0:d8528fbbaf9c 399
Nurbol 0:d8528fbbaf9c 400 valueLED1=0;
Nurbol 0:d8528fbbaf9c 401 valueLED2=0;
Nurbol 0:d8528fbbaf9c 402 clk=0;
Nurbol 0:d8528fbbaf9c 403 init_sys();
Nurbol 0:d8528fbbaf9c 404
Nurbol 0:d8528fbbaf9c 405 FOREVER {
Nurbol 0:d8528fbbaf9c 406
Nurbol 0:d8528fbbaf9c 407 timer2p.attach(&sensor2p, 0.1); //function sensor2p is reading all the 0.1 ms
Nurbol 0:d8528fbbaf9c 408 timer1p.attach(&sensor1p, 0.1); //function sensor1p is reading all the 0.1 ms
Nurbol 0:d8528fbbaf9c 409
Nurbol 1:a91950a8e20f 410
Nurbol 0:d8528fbbaf9c 411 counter1p.read();
Nurbol 0:d8528fbbaf9c 412 counter2p.read();
Nurbol 0:d8528fbbaf9c 413
Nurbol 0:d8528fbbaf9c 414 timerstatemachine.attach(&state_machine, 0.1);
Nurbol 1:a91950a8e20f 415
Nurbol 0:d8528fbbaf9c 416
Nurbol 0:d8528fbbaf9c 417
Nurbol 0:d8528fbbaf9c 418 clk = !clk;
Nurbol 0:d8528fbbaf9c 419 wait(0.01);
Nurbol 0:d8528fbbaf9c 420
Nurbol 0:d8528fbbaf9c 421 get_cmd(&ext_cmd);
Nurbol 0:d8528fbbaf9c 422 //
Nurbol 0:d8528fbbaf9c 423 // Check status of read command activity and return an error status if there was a problem
Nurbol 0:d8528fbbaf9c 424 // If there is a problem, then return status code only and wait for next command.
Nurbol 0:d8528fbbaf9c 425 //
Nurbol 0:d8528fbbaf9c 426 if (ext_cmd.result_status != OK){
Nurbol 0:d8528fbbaf9c 427 send_status(ext_cmd.result_status);
Nurbol 0:d8528fbbaf9c 428 continue;
Nurbol 0:d8528fbbaf9c 429 }
Nurbol 0:d8528fbbaf9c 430 //
Nurbol 0:d8528fbbaf9c 431 // Parse command and return an error staus if there is a problem
Nurbol 0:d8528fbbaf9c 432 // If there is a problem, then return status code only and wait for next command.
Nurbol 0:d8528fbbaf9c 433 //
Nurbol 0:d8528fbbaf9c 434 parse_cmd(&ext_cmd);
Nurbol 2:ea2d1441d485 435 // lcd->locate(1,0); lcd->printf(ext_cmd.cmd_str);
Nurbol 0:d8528fbbaf9c 436 if ((ext_cmd.result_status != OK) && (ext_cmd.cmd_code != TEXT_CMD)){
Nurbol 2:ea2d1441d485 437 lcd->cls(); lcd->locate(0,0); lcd->printf("W3C"); lcd->locate(1,0); lcd->printf("parse : error");
Nurbol 0:d8528fbbaf9c 438 send_status(ext_cmd.result_status);
Nurbol 0:d8528fbbaf9c 439 continue;
Nurbol 0:d8528fbbaf9c 440 }
Nurbol 0:d8528fbbaf9c 441 //
Nurbol 0:d8528fbbaf9c 442 // Execute command and return an error staus if there is a problem
Nurbol 0:d8528fbbaf9c 443 //
Nurbol 0:d8528fbbaf9c 444 process_cmd(&ext_cmd);
Nurbol 0:d8528fbbaf9c 445 reply_to_cmd(&ext_cmd);
Nurbol 2:ea2d1441d485 446
Nurbol 0:d8528fbbaf9c 447
Nurbol 0:d8528fbbaf9c 448
Nurbol 0:d8528fbbaf9c 449
Nurbol 0:d8528fbbaf9c 450 }
Nurbol 0:d8528fbbaf9c 451
Nurbol 0:d8528fbbaf9c 452 }
Nurbol 0:d8528fbbaf9c 453
Nurbol 0:d8528fbbaf9c 454
Nurbol 0:d8528fbbaf9c 455
Nurbol 0:d8528fbbaf9c 456
Nurbol 0:d8528fbbaf9c 457
Nurbol 0:d8528fbbaf9c 458
Nurbol 0:d8528fbbaf9c 459
Nurbol 0:d8528fbbaf9c 460
Nurbol 0:d8528fbbaf9c 461