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: cmd_io mbed WattBob_TextLCD MCP23017 globals
main.cpp@2:881ca0a50c9b, 2011-12-02 (annotated)
- Committer:
- Nurbol
- Date:
- Fri Dec 02 14:21:42 2011 +0000
- Revision:
- 2:881ca0a50c9b
- Parent:
- 1:8a1818e89c49
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Nurbol | 2:881ca0a50c9b | 1 | #include "mbed.h" |
| Nurbol | 0:247c337c230a | 2 | #include "MCP23017.h" |
| Nurbol | 0:247c337c230a | 3 | #include "WattBob_TextLCD.h" |
| Nurbol | 0:247c337c230a | 4 | #include "cmd_io.h" |
| Nurbol | 0:247c337c230a | 5 | #include "globals.h" |
| Nurbol | 0:247c337c230a | 6 | |
| Nurbol | 0:247c337c230a | 7 | |
| Nurbol | 0:247c337c230a | 8 | //****************************************************************************** |
| Nurbol | 0:247c337c230a | 9 | //declare ticker |
| Nurbol | 0:247c337c230a | 10 | // |
| Nurbol | 2:881ca0a50c9b | 11 | Ticker timersensor; // Timer for the function of the sensor |
| Nurbol | 2:881ca0a50c9b | 12 | Ticker timerstatemachine; // Timer for the function of the state machine |
| Nurbol | 0:247c337c230a | 13 | |
| Nurbol | 0:247c337c230a | 14 | //****************************************************************************** |
| Nurbol | 0:247c337c230a | 15 | //declare Pin |
| Nurbol | 0:247c337c230a | 16 | // |
| Nurbol | 2:881ca0a50c9b | 17 | DigitalIn sensor1(p8); // sensor which detect the coin of 2p |
| Nurbol | 2:881ca0a50c9b | 18 | DigitalIn sensor2(p7); // sensor which detect the coin of 1p |
| Nurbol | 2:881ca0a50c9b | 19 | DigitalIn counter1p(p5); // input to know when the counter 1p = 10 |
| Nurbol | 2:881ca0a50c9b | 20 | DigitalIn counter2p(p6); // input to know when the counter 2p = 5 |
| Nurbol | 0:247c337c230a | 21 | DigitalIn SW1p(p11); // Switch for the 1p |
| Nurbol | 0:247c337c230a | 22 | DigitalIn SW2p(p12); // Switch for the 2p |
| Nurbol | 0:247c337c230a | 23 | |
| Nurbol | 2:881ca0a50c9b | 24 | DigitalOut valueLED1(p23); // output sensor 2p send to the FPGA |
| Nurbol | 2:881ca0a50c9b | 25 | DigitalOut valueLED2(p25); // output sensor 1p send to the FPGA |
| Nurbol | 2:881ca0a50c9b | 26 | DigitalOut Reset(p27); // variable send to the FPGA to say when we should reset the counter |
| Nurbol | 2:881ca0a50c9b | 27 | DigitalOut led1(LED1); // declaration of the led1 (sensor 2p) |
| Nurbol | 2:881ca0a50c9b | 28 | DigitalOut led2(LED2); // declaration of the led2 (sensor 1p) |
| Nurbol | 2:881ca0a50c9b | 29 | DigitalOut led3(LED3); // declaration of the led3 (when counter 1p = 10) |
| Nurbol | 2:881ca0a50c9b | 30 | DigitalOut led4(LED4); // declaration of the led4 (when counter 2p = 5) |
| Nurbol | 2:881ca0a50c9b | 31 | DigitalOut clk(p24); // clock send to the FPGA |
| Nurbol | 0:247c337c230a | 32 | |
| Nurbol | 0:247c337c230a | 33 | |
| Nurbol | 0:247c337c230a | 34 | |
| Nurbol | 0:247c337c230a | 35 | //****************************************************************************** |
| Nurbol | 0:247c337c230a | 36 | //declare variable |
| Nurbol | 0:247c337c230a | 37 | // |
| Nurbol | 0:247c337c230a | 38 | bool Position1_1p; // Position 1 for the 1p |
| Nurbol | 0:247c337c230a | 39 | bool Position2_1p; // Position 2 for the 1p |
| Nurbol | 0:247c337c230a | 40 | bool Position1_2p; // Position 1 for the 2p |
| Nurbol | 0:247c337c230a | 41 | bool Position2_2p; // Position 2 for the 2p |
| Nurbol | 2:881ca0a50c9b | 42 | bool Motor; // variable send to the GUI to know when the motor run |
| Nurbol | 0:247c337c230a | 43 | |
| Nurbol | 2:881ca0a50c9b | 44 | bool bSort_Mode; // to use in the code to define the mode |
| Nurbol | 2:881ca0a50c9b | 45 | bool bMaint_Mode; // to use in the code to define the mode |
| Nurbol | 0:247c337c230a | 46 | |
| Nurbol | 2:881ca0a50c9b | 47 | int state; // declaration of the variable state for the state machine |
| Nurbol | 0:247c337c230a | 48 | |
| Nurbol | 0:247c337c230a | 49 | |
| Nurbol | 0:247c337c230a | 50 | //****************************************************************************** |
| Nurbol | 0:247c337c230a | 51 | // declare functions |
| Nurbol | 0:247c337c230a | 52 | // |
| Nurbol | 0:247c337c230a | 53 | void sensor (void); // Function to read sensors |
| Nurbol | 0:247c337c230a | 54 | void state_machine (void); // Function to go in the state machine to move the servos and the motor |
| Nurbol | 0:247c337c230a | 55 | |
| Nurbol | 0:247c337c230a | 56 | // |
| Nurbol | 0:247c337c230a | 57 | // 3 servo outputs |
| Nurbol | 0:247c337c230a | 58 | // |
| Nurbol | 0:247c337c230a | 59 | |
| Nurbol | 2:881ca0a50c9b | 60 | PwmOut servo_0(p26); // servo to move the boxes of 1p |
| Nurbol | 2:881ca0a50c9b | 61 | PwmOut servo_4(p22); // motor of the wheel |
| Nurbol | 2:881ca0a50c9b | 62 | PwmOut servo_5(p21); // servo to move the boxes of 2p |
| Nurbol | 0:247c337c230a | 63 | |
| Nurbol | 0:247c337c230a | 64 | // |
| Nurbol | 0:247c337c230a | 65 | // objects necessary to use the 2*16 character MBED display |
| Nurbol | 0:247c337c230a | 66 | // |
| Nurbol | 0:247c337c230a | 67 | MCP23017 *par_port; |
| Nurbol | 0:247c337c230a | 68 | WattBob_TextLCD *lcd; |
| Nurbol | 0:247c337c230a | 69 | // |
| Nurbol | 0:247c337c230a | 70 | // Virtual COM port over USB link to laptop/PC |
| Nurbol | 0:247c337c230a | 71 | // |
| Nurbol | 0:247c337c230a | 72 | Serial pc(USBTX, USBRX); |
| Nurbol | 0:247c337c230a | 73 | |
| Nurbol | 0:247c337c230a | 74 | //****************************************************************************** |
| Nurbol | 0:247c337c230a | 75 | // Defined GLOBAL variables and structures |
| Nurbol | 0:247c337c230a | 76 | // |
| Nurbol | 0:247c337c230a | 77 | CMD_STRUCT ext_cmd; // structure to hold command data |
| Nurbol | 0:247c337c230a | 78 | STAT_STRUCT ext_stat; // structure to hold status reply |
| Nurbol | 0:247c337c230a | 79 | |
| Nurbol | 0:247c337c230a | 80 | |
| Nurbol | 0:247c337c230a | 81 | //CMD_STRUCT commandcounter2; |
| Nurbol | 0:247c337c230a | 82 | |
| Nurbol | 0:247c337c230a | 83 | uint32_t last_servo; // store for last servo number |
| Nurbol | 0:247c337c230a | 84 | |
| Nurbol | 0:247c337c230a | 85 | //************************************************************************ |
| Nurbol | 0:247c337c230a | 86 | //************************************************************************ |
| Nurbol | 0:247c337c230a | 87 | // init_sys : initialise the system |
| Nurbol | 0:247c337c230a | 88 | // ======== |
| Nurbol | 0:247c337c230a | 89 | // |
| Nurbol | 0:247c337c230a | 90 | // 1. Configure 2*16 character display |
| Nurbol | 0:247c337c230a | 91 | // 2. Print "COM test" string |
| Nurbol | 0:247c337c230a | 92 | // 3. initialise relevant global variables |
| Nurbol | 0:247c337c230a | 93 | // 4. set COM port baud rate to 19200 bits per second |
| Nurbol | 0:247c337c230a | 94 | // |
| Nurbol | 0:247c337c230a | 95 | void init_sys(void) { |
| Nurbol | 0:247c337c230a | 96 | |
| Nurbol | 0:247c337c230a | 97 | par_port = new MCP23017(p9, p10, 0x40); |
| Nurbol | 0:247c337c230a | 98 | lcd = new WattBob_TextLCD(par_port); |
| Nurbol | 0:247c337c230a | 99 | |
| Nurbol | 0:247c337c230a | 100 | par_port->write_bit(1,BL_BIT); // turn LCD backlight ON |
| Nurbol | 0:247c337c230a | 101 | lcd->cls(); |
| Nurbol | 0:247c337c230a | 102 | lcd->locate(0,7); |
| Nurbol | 0:247c337c230a | 103 | lcd->printf("W3C"); |
| Nurbol | 0:247c337c230a | 104 | |
| Nurbol | 0:247c337c230a | 105 | servo_0.period(0.020); // servo requires a 20ms period, common for all 5 servo objects |
| Nurbol | 0:247c337c230a | 106 | last_servo = SERVO_UNKNOWN; |
| Nurbol | 0:247c337c230a | 107 | pc.baud(19200); |
| Nurbol | 0:247c337c230a | 108 | |
| Nurbol | 0:247c337c230a | 109 | servo_0.pulsewidth_us(1000 + (0 * 1000) / 90); // Servo 1p initialise in position 1 |
| Nurbol | 0:247c337c230a | 110 | servo_5.pulsewidth_us(1000 + (0 * 1000) / 90); // Servo 2p initialise in position 1 |
| Nurbol | 0:247c337c230a | 111 | servo_4.pulsewidth_us(0); // Motor stop |
| Nurbol | 0:247c337c230a | 112 | |
| Nurbol | 2:881ca0a50c9b | 113 | Position1_1p = 1; // servo 1p is in position 1 |
| Nurbol | 2:881ca0a50c9b | 114 | Position2_1p = 0; // position 2 = 0 |
| Nurbol | 2:881ca0a50c9b | 115 | Position1_2p = 1; // servo 2p is in position 1 |
| Nurbol | 2:881ca0a50c9b | 116 | Position2_2p = 0; // position 2 = 0 |
| Nurbol | 0:247c337c230a | 117 | |
| Nurbol | 2:881ca0a50c9b | 118 | state = 10; // initial state is state 10 |
| Nurbol | 2:881ca0a50c9b | 119 | Reset = 1; // reset the counter in the FPGA |
| Nurbol | 0:247c337c230a | 120 | |
| Nurbol | 2:881ca0a50c9b | 121 | valueLED1 = 0; // sensor 2p initialize to 0 |
| Nurbol | 2:881ca0a50c9b | 122 | valueLED2 = 0; // sensor 1p initialize to 0 |
| Nurbol | 0:247c337c230a | 123 | |
| Nurbol | 2:881ca0a50c9b | 124 | clk = 0; // clock start to 0 |
| Nurbol | 2:881ca0a50c9b | 125 | Motor = 0; // motor is stop |
| Nurbol | 0:247c337c230a | 126 | return; |
| Nurbol | 0:247c337c230a | 127 | } // end init_sys |
| Nurbol | 0:247c337c230a | 128 | |
| Nurbol | 0:247c337c230a | 129 | //************************************************************************ |
| Nurbol | 0:247c337c230a | 130 | // process_cmd : decode and execute command |
| Nurbol | 0:247c337c230a | 131 | // =========== |
| Nurbol | 0:247c337c230a | 132 | uint32_t process_cmd(CMD_STRUCT *command) |
| Nurbol | 0:247c337c230a | 133 | { |
| Nurbol | 0:247c337c230a | 134 | int32_t pulse_width; |
| Nurbol | 0:247c337c230a | 135 | |
| Nurbol | 0:247c337c230a | 136 | switch (command->cmd_code) { |
| Nurbol | 0:247c337c230a | 137 | // |
| Nurbol | 0:247c337c230a | 138 | // move a servo |
| Nurbol | 0:247c337c230a | 139 | // |
| Nurbol | 0:247c337c230a | 140 | case SERVO_CMD : |
| Nurbol | 0:247c337c230a | 141 | command->nos_data = 0; // no data to be returned |
| Nurbol | 0:247c337c230a | 142 | // |
| Nurbol | 0:247c337c230a | 143 | // check that parameters are OK |
| Nurbol | 0:247c337c230a | 144 | // |
| Nurbol | 0:247c337c230a | 145 | if (command->nos_params != 2) { // check for 2 parameters |
| Nurbol | 0:247c337c230a | 146 | command->result_status = CMD_BAD_NUMBER_OF_PARAMETERS; |
| Nurbol | 0:247c337c230a | 147 | break; |
| Nurbol | 0:247c337c230a | 148 | } |
| Nurbol | 0:247c337c230a | 149 | if (command->param[0] > MAX_SERVO_NUMBER ) { // check servo number |
| Nurbol | 0:247c337c230a | 150 | command->result_status = CMD_BAD_SERVO_NUMBER; |
| Nurbol | 0:247c337c230a | 151 | break; |
| Nurbol | 0:247c337c230a | 152 | } |
| Nurbol | 0:247c337c230a | 153 | if ((command->param[1] < MIN_SERVO_ANGLE) || |
| Nurbol | 0:247c337c230a | 154 | (command->param[1] > MAX_SERVO_ANGLE) ) { |
| Nurbol | 0:247c337c230a | 155 | command->result_status = CMD_BAD_SERVO_VALUE; |
| Nurbol | 0:247c337c230a | 156 | break; |
| Nurbol | 0:247c337c230a | 157 | } |
| Nurbol | 2:881ca0a50c9b | 158 | if ((command->param[0] == 4) && (command->param[1] == 0)) { // motor of the wheel |
| Nurbol | 2:881ca0a50c9b | 159 | pulse_width = 0; // pulse width is 0 so motor stop |
| Nurbol | 0:247c337c230a | 160 | } |
| Nurbol | 0:247c337c230a | 161 | else{ |
| Nurbol | 2:881ca0a50c9b | 162 | pulse_width = 1000 + (command->param[1] * 1000) / 90; // convert angle to pulse width |
| Nurbol | 0:247c337c230a | 163 | } |
| Nurbol | 0:247c337c230a | 164 | |
| Nurbol | 0:247c337c230a | 165 | |
| Nurbol | 0:247c337c230a | 166 | |
| Nurbol | 0:247c337c230a | 167 | // |
| Nurbol | 2:881ca0a50c9b | 168 | // implement servo move to all 3 servos |
| Nurbol | 0:247c337c230a | 169 | // |
| Nurbol | 0:247c337c230a | 170 | switch (command->param[0]) { |
| Nurbol | 2:881ca0a50c9b | 171 | case 0 : servo_0.pulsewidth_us(pulse_width); break; // servo for 1p |
| Nurbol | 2:881ca0a50c9b | 172 | case 4 : servo_4.pulsewidth_us(pulse_width); break; // motor |
| Nurbol | 2:881ca0a50c9b | 173 | case 5 : servo_5.pulsewidth_us(pulse_width); break; // servo for 2p |
| Nurbol | 0:247c337c230a | 174 | } |
| Nurbol | 0:247c337c230a | 175 | last_servo = command->param[0]; |
| Nurbol | 0:247c337c230a | 176 | break; |
| Nurbol | 0:247c337c230a | 177 | // |
| Nurbol | 0:247c337c230a | 178 | // return data of the value of sensor 1p and sensor 2p |
| Nurbol | 0:247c337c230a | 179 | // |
| Nurbol | 0:247c337c230a | 180 | case READ_CMD : |
| Nurbol | 0:247c337c230a | 181 | if((bSort_Mode == 0)&&(bMaint_Mode == 1)){ // when we are on sort mode |
| Nurbol | 0:247c337c230a | 182 | command->nos_data = 2; |
| Nurbol | 2:881ca0a50c9b | 183 | command->result_data[0] = valueLED1; // return data of sensor 2p to the GUI |
| Nurbol | 2:881ca0a50c9b | 184 | command->result_data[1] = valueLED2; // return data of sensor 1p to the GUI |
| Nurbol | 0:247c337c230a | 185 | } |
| Nurbol | 0:247c337c230a | 186 | break; |
| Nurbol | 0:247c337c230a | 187 | |
| Nurbol | 0:247c337c230a | 188 | // |
| Nurbol | 2:881ca0a50c9b | 189 | // Maintenance Mode |
| Nurbol | 0:247c337c230a | 190 | // |
| Nurbol | 2:881ca0a50c9b | 191 | case MAINT_MODE : |
| Nurbol | 0:247c337c230a | 192 | bSort_Mode = 0; |
| Nurbol | 0:247c337c230a | 193 | bMaint_Mode = 1; |
| Nurbol | 2:881ca0a50c9b | 194 | Reset = 1; // we reset the counter in the FPGA |
| Nurbol | 2:881ca0a50c9b | 195 | servo_4.pulsewidth_us(0); // Motor stop |
| Nurbol | 2:881ca0a50c9b | 196 | servo_0.pulsewidth_us(0); // Servo 1p stop |
| Nurbol | 2:881ca0a50c9b | 197 | servo_5.pulsewidth_us(0); // Servo 2p stop |
| Nurbol | 0:247c337c230a | 198 | lcd->cls(); |
| Nurbol | 0:247c337c230a | 199 | lcd->locate(0,7); |
| Nurbol | 2:881ca0a50c9b | 200 | lcd->printf("W3C"); // write on the LCD |
| Nurbol | 0:247c337c230a | 201 | lcd->locate(1,0); |
| Nurbol | 0:247c337c230a | 202 | lcd->printf("maintenance mode"); |
| Nurbol | 0:247c337c230a | 203 | break; |
| Nurbol | 0:247c337c230a | 204 | |
| Nurbol | 2:881ca0a50c9b | 205 | // |
| Nurbol | 2:881ca0a50c9b | 206 | // Sort Mode |
| Nurbol | 2:881ca0a50c9b | 207 | // |
| Nurbol | 2:881ca0a50c9b | 208 | case SORT_MODE : |
| Nurbol | 0:247c337c230a | 209 | bSort_Mode = 1; |
| Nurbol | 0:247c337c230a | 210 | bMaint_Mode = 0; |
| Nurbol | 2:881ca0a50c9b | 211 | Reset = 0; |
| Nurbol | 2:881ca0a50c9b | 212 | state = 10; // state in the state machine is 10 |
| Nurbol | 0:247c337c230a | 213 | lcd->cls(); |
| Nurbol | 0:247c337c230a | 214 | lcd->locate(0,7); |
| Nurbol | 2:881ca0a50c9b | 215 | lcd->printf("W3C"); // write on the LCD |
| Nurbol | 0:247c337c230a | 216 | lcd->locate(1,0); |
| Nurbol | 0:247c337c230a | 217 | lcd->printf("sort mode"); |
| Nurbol | 0:247c337c230a | 218 | break; |
| Nurbol | 0:247c337c230a | 219 | |
| Nurbol | 0:247c337c230a | 220 | // |
| Nurbol | 0:247c337c230a | 221 | // Urgency mode |
| Nurbol | 0:247c337c230a | 222 | // |
| Nurbol | 0:247c337c230a | 223 | case URGENCY : |
| Nurbol | 2:881ca0a50c9b | 224 | Reset = 1; // we reset the counter on the FPGA |
| Nurbol | 0:247c337c230a | 225 | bSort_Mode = 0; |
| Nurbol | 0:247c337c230a | 226 | bMaint_Mode = 0; |
| Nurbol | 2:881ca0a50c9b | 227 | state = 10; // state in the state machine is 10 |
| Nurbol | 2:881ca0a50c9b | 228 | servo_4.pulsewidth_us(0); // Motor stop |
| Nurbol | 2:881ca0a50c9b | 229 | servo_0.pulsewidth_us(0); // Servo 1p stop |
| Nurbol | 2:881ca0a50c9b | 230 | servo_5.pulsewidth_us(0); // Servo 2p stop |
| Nurbol | 0:247c337c230a | 231 | lcd->cls(); |
| Nurbol | 0:247c337c230a | 232 | lcd->locate(0,7); |
| Nurbol | 2:881ca0a50c9b | 233 | lcd->printf("W3C"); // write on the LCD |
| Nurbol | 0:247c337c230a | 234 | lcd->locate(1,0); |
| Nurbol | 0:247c337c230a | 235 | lcd->printf("urgency mode"); |
| Nurbol | 0:247c337c230a | 236 | break; |
| Nurbol | 0:247c337c230a | 237 | // |
| Nurbol | 0:247c337c230a | 238 | // Exit mode |
| Nurbol | 0:247c337c230a | 239 | // |
| Nurbol | 0:247c337c230a | 240 | case EXIT : |
| Nurbol | 2:881ca0a50c9b | 241 | Reset = 1; // we reset the counter in the FPGA |
| Nurbol | 0:247c337c230a | 242 | bSort_Mode = 0; |
| Nurbol | 0:247c337c230a | 243 | bMaint_Mode = 0; |
| Nurbol | 2:881ca0a50c9b | 244 | state = 10; // state in the state machine is 10 |
| Nurbol | 2:881ca0a50c9b | 245 | servo_4.pulsewidth_us(0); // Motor stop |
| Nurbol | 2:881ca0a50c9b | 246 | servo_0.pulsewidth_us(0); // sensor 1p stop |
| Nurbol | 2:881ca0a50c9b | 247 | servo_5.pulsewidth_us(0); // sensor 2p stop |
| Nurbol | 0:247c337c230a | 248 | lcd->cls(); |
| Nurbol | 0:247c337c230a | 249 | lcd->locate(0,7); |
| Nurbol | 2:881ca0a50c9b | 250 | lcd->printf("W3C"); // write on the LCD |
| Nurbol | 0:247c337c230a | 251 | break; |
| Nurbol | 0:247c337c230a | 252 | |
| Nurbol | 0:247c337c230a | 253 | // Send data of the value led 2p |
| Nurbol | 0:247c337c230a | 254 | case VALUE_LED1 : |
| Nurbol | 0:247c337c230a | 255 | command->nos_data = 1; |
| Nurbol | 2:881ca0a50c9b | 256 | command->result_data[0] = valueLED1; // return data of sensor 2p to the GUI |
| Nurbol | 0:247c337c230a | 257 | break; |
| Nurbol | 0:247c337c230a | 258 | |
| Nurbol | 0:247c337c230a | 259 | // Send data of the value led 1p |
| Nurbol | 0:247c337c230a | 260 | case VALUE_LED2 : |
| Nurbol | 0:247c337c230a | 261 | command->nos_data = 2; |
| Nurbol | 2:881ca0a50c9b | 262 | command->result_data[1] = valueLED2; // return data of sensor 1p to the GUI |
| Nurbol | 0:247c337c230a | 263 | break; |
| Nurbol | 0:247c337c230a | 264 | |
| Nurbol | 0:247c337c230a | 265 | // Send data of the value counter 1p |
| Nurbol | 0:247c337c230a | 266 | case COUNTER1P : |
| Nurbol | 0:247c337c230a | 267 | command->nos_data = 3; |
| Nurbol | 2:881ca0a50c9b | 268 | command->result_data[2] = counter1p; // return data of counter 1p to the GUI (counter 1p = 1 when we have 10 coins) |
| Nurbol | 0:247c337c230a | 269 | break; |
| Nurbol | 0:247c337c230a | 270 | |
| Nurbol | 0:247c337c230a | 271 | // Send data of the value counter 2p |
| Nurbol | 0:247c337c230a | 272 | case COUNTER2P : |
| Nurbol | 0:247c337c230a | 273 | command->nos_data = 4; |
| Nurbol | 2:881ca0a50c9b | 274 | command->result_data[3] = counter2p; // return data of counter 2p to the GUI (counter 2p = 1 when we have 5 coins) |
| Nurbol | 0:247c337c230a | 275 | break; |
| Nurbol | 0:247c337c230a | 276 | |
| Nurbol | 0:247c337c230a | 277 | // Send data of the value position1 1p |
| Nurbol | 0:247c337c230a | 278 | case POSITION1_1P : |
| Nurbol | 0:247c337c230a | 279 | command->nos_data = 1; |
| Nurbol | 2:881ca0a50c9b | 280 | command->result_data[0] = Position1_1p; // return data of the position 1 for the servo of the 1p to the GUI |
| Nurbol | 0:247c337c230a | 281 | break; |
| Nurbol | 0:247c337c230a | 282 | |
| Nurbol | 0:247c337c230a | 283 | // Send data of the value position1 2p |
| Nurbol | 0:247c337c230a | 284 | case POSITION1_2P : |
| Nurbol | 0:247c337c230a | 285 | command->nos_data = 2; |
| Nurbol | 2:881ca0a50c9b | 286 | command->result_data[1] = Position1_2p; // return data of the position 1 for the servo of the 2p to the GUI |
| Nurbol | 0:247c337c230a | 287 | break; |
| Nurbol | 0:247c337c230a | 288 | |
| Nurbol | 0:247c337c230a | 289 | // Send data of the motor |
| Nurbol | 0:247c337c230a | 290 | case MOTOR : |
| Nurbol | 0:247c337c230a | 291 | command->nos_data = 3; |
| Nurbol | 2:881ca0a50c9b | 292 | command->result_data[2] = Motor; // return data of the state of motor to the GUI |
| Nurbol | 0:247c337c230a | 293 | break; |
| Nurbol | 0:247c337c230a | 294 | |
| Nurbol | 0:247c337c230a | 295 | // |
| Nurbol | 0:247c337c230a | 296 | // catch any problems |
| Nurbol | 0:247c337c230a | 297 | // |
| Nurbol | 0:247c337c230a | 298 | default: |
| Nurbol | 0:247c337c230a | 299 | command->nos_data = 0; // no data to be returned |
| Nurbol | 0:247c337c230a | 300 | command->result_status = CMD_BAD_SERVO_VALUE; |
| Nurbol | 0:247c337c230a | 301 | break; |
| Nurbol | 0:247c337c230a | 302 | } |
| Nurbol | 0:247c337c230a | 303 | return OK; |
| Nurbol | 0:247c337c230a | 304 | } |
| Nurbol | 0:247c337c230a | 305 | |
| Nurbol | 0:247c337c230a | 306 | //************************************************************************ |
| Nurbol | 0:247c337c230a | 307 | |
| Nurbol | 0:247c337c230a | 308 | //function to send value on the FPGA when 1p or 2p are detected |
| Nurbol | 0:247c337c230a | 309 | void sensor (void){ |
| Nurbol | 2:881ca0a50c9b | 310 | sensor1.read(); // read input sensor 2p |
| Nurbol | 2:881ca0a50c9b | 311 | sensor2.read(); // read input sensor 1p |
| Nurbol | 0:247c337c230a | 312 | |
| Nurbol | 2:881ca0a50c9b | 313 | clk = !clk; // inverse signal of the clock |
| Nurbol | 0:247c337c230a | 314 | wait(0.01); |
| Nurbol | 0:247c337c230a | 315 | |
| Nurbol | 2:881ca0a50c9b | 316 | if(sensor1 > 0.5) { // if coin of 2p is detected |
| Nurbol | 2:881ca0a50c9b | 317 | led1 = 1; // led 1 switch on |
| Nurbol | 2:881ca0a50c9b | 318 | valueLED1 = 1; // value of the output sensor 2p send to the FPGA |
| Nurbol | 0:247c337c230a | 319 | } |
| Nurbol | 2:881ca0a50c9b | 320 | else if(sensor1 < 0.5){ // if coin of 2p is not detected |
| Nurbol | 2:881ca0a50c9b | 321 | led1 = 0; // led 1 switch off |
| Nurbol | 2:881ca0a50c9b | 322 | valueLED1 = 0; // value of the output sensor 2p send to the FPGA |
| Nurbol | 0:247c337c230a | 323 | } |
| Nurbol | 2:881ca0a50c9b | 324 | if(sensor2 > 0.5) { // if coin of 1p is detected |
| Nurbol | 2:881ca0a50c9b | 325 | led2 = 1; // led 2 switch on |
| Nurbol | 2:881ca0a50c9b | 326 | valueLED2 = 1; // value of the output sensor 1p send to the FPGA |
| Nurbol | 0:247c337c230a | 327 | } |
| Nurbol | 2:881ca0a50c9b | 328 | else if(sensor2 < 0.5){ // of coin of 1p is not detected |
| Nurbol | 2:881ca0a50c9b | 329 | led2 = 0; // led 2 switch off |
| Nurbol | 2:881ca0a50c9b | 330 | valueLED2 = 0; // value of the output sensor 1p send to the FPGA |
| Nurbol | 0:247c337c230a | 331 | } |
| Nurbol | 0:247c337c230a | 332 | } |
| Nurbol | 0:247c337c230a | 333 | |
| Nurbol | 2:881ca0a50c9b | 334 | //function for the state machine to move servos between 2 positions and to move motor |
| Nurbol | 0:247c337c230a | 335 | void state_machine (){ |
| Nurbol | 2:881ca0a50c9b | 336 | if((bSort_Mode == 1)&&(bMaint_Mode == 0)){ // if we are in sort mode |
| Nurbol | 2:881ca0a50c9b | 337 | |
| Nurbol | 0:247c337c230a | 338 | switch(state) |
| Nurbol | 0:247c337c230a | 339 | { |
| Nurbol | 2:881ca0a50c9b | 340 | case 10 : // initial state |
| Nurbol | 2:881ca0a50c9b | 341 | servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run |
| Nurbol | 2:881ca0a50c9b | 342 | servo_0.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 1p go to position 1 |
| Nurbol | 2:881ca0a50c9b | 343 | servo_5.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 2p go to position 1 |
| Nurbol | 2:881ca0a50c9b | 344 | Position1_1p = 1; // value position 1 for servo 1p = 1 |
| Nurbol | 0:247c337c230a | 345 | Position2_1p = 0; |
| Nurbol | 2:881ca0a50c9b | 346 | Position1_2p = 1; // value position 1 for servo 2p = 1 |
| Nurbol | 0:247c337c230a | 347 | Position2_2p = 0; |
| Nurbol | 2:881ca0a50c9b | 348 | Motor = 1; // value to say motor running |
| Nurbol | 0:247c337c230a | 349 | led3 = 0; |
| Nurbol | 0:247c337c230a | 350 | led4 = 0; |
| Nurbol | 2:881ca0a50c9b | 351 | state = 0; // go to state 0 |
| Nurbol | 0:247c337c230a | 352 | break; |
| Nurbol | 0:247c337c230a | 353 | |
| Nurbol | 0:247c337c230a | 354 | |
| Nurbol | 2:881ca0a50c9b | 355 | case 0: // state 0 |
| Nurbol | 2:881ca0a50c9b | 356 | Motor = 1; // motor running |
| Nurbol | 0:247c337c230a | 357 | led3 = 0; |
| Nurbol | 0:247c337c230a | 358 | led4 = 0; |
| Nurbol | 2:881ca0a50c9b | 359 | counter1p.read(); // read value of counter 1p |
| Nurbol | 2:881ca0a50c9b | 360 | counter2p.read(); // read value of counter 2p |
| Nurbol | 2:881ca0a50c9b | 361 | if(SW1p == 0){ // if switch of 1p os switch off |
| Nurbol | 2:881ca0a50c9b | 362 | if(counter1p > 0.5){ // if counter 1p = 10 coins |
| Nurbol | 2:881ca0a50c9b | 363 | state = 1; // go to state 1 |
| Nurbol | 0:247c337c230a | 364 | } |
| Nurbol | 0:247c337c230a | 365 | } |
| Nurbol | 2:881ca0a50c9b | 366 | if(SW2p == 0){ // if switch 2p is switch off |
| Nurbol | 2:881ca0a50c9b | 367 | if(counter2p > 0.5){ // if counter 2p = 5 coins |
| Nurbol | 2:881ca0a50c9b | 368 | state = 4; // go to state 4 |
| Nurbol | 0:247c337c230a | 369 | } |
| Nurbol | 0:247c337c230a | 370 | } |
| Nurbol | 0:247c337c230a | 371 | break; |
| Nurbol | 2:881ca0a50c9b | 372 | case 1: // state 1 |
| Nurbol | 0:247c337c230a | 373 | servo_4.pulsewidth_us(0); // motor stop |
| Nurbol | 0:247c337c230a | 374 | wait(2); |
| Nurbol | 0:247c337c230a | 375 | servo_0.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 1p go to position 2 |
| Nurbol | 0:247c337c230a | 376 | wait(1); |
| Nurbol | 0:247c337c230a | 377 | Position1_1p = 0; |
| Nurbol | 2:881ca0a50c9b | 378 | Position2_1p = 1; // position 2 for servo 1p = 1 |
| Nurbol | 2:881ca0a50c9b | 379 | Motor = 0; // motor stopping |
| Nurbol | 2:881ca0a50c9b | 380 | if((Position2_1p == 1)&&(counter1p < 0.5)){ // if servo 1p is in position 2 and counter 1p < 10 coins |
| Nurbol | 2:881ca0a50c9b | 381 | state = 2; // go to state 2 |
| Nurbol | 0:247c337c230a | 382 | } |
| Nurbol | 0:247c337c230a | 383 | break; |
| Nurbol | 2:881ca0a50c9b | 384 | case 2: // state 2 |
| Nurbol | 0:247c337c230a | 385 | servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor is run |
| Nurbol | 0:247c337c230a | 386 | Motor = 1; |
| Nurbol | 2:881ca0a50c9b | 387 | counter1p.read(); // read value of counter 1p |
| Nurbol | 2:881ca0a50c9b | 388 | counter2p.read(); // read value of counter 2p |
| Nurbol | 2:881ca0a50c9b | 389 | if(counter1p > 0.5){ // if counter 1p = 10 coins |
| Nurbol | 2:881ca0a50c9b | 390 | state = 3; // go to state 3 |
| Nurbol | 0:247c337c230a | 391 | } |
| Nurbol | 2:881ca0a50c9b | 392 | else if((counter2p > 0.5)&&(Position1_2p == 1)){ // if counter 2p = 5 coins and servo 2p is in position 1 |
| Nurbol | 2:881ca0a50c9b | 393 | state = 4; // go to state 4 |
| Nurbol | 0:247c337c230a | 394 | } |
| Nurbol | 2:881ca0a50c9b | 395 | else if((counter2p > 0.5)&&(Position2_2p == 1)){ // if counter 2p = 5 coins and servo 2p is in position 2 |
| Nurbol | 2:881ca0a50c9b | 396 | state = 6; // go to state 6 |
| Nurbol | 0:247c337c230a | 397 | } |
| Nurbol | 0:247c337c230a | 398 | break; |
| Nurbol | 2:881ca0a50c9b | 399 | case 3: // state 3 |
| Nurbol | 0:247c337c230a | 400 | servo_4.pulsewidth_us(0); // motor stop |
| Nurbol | 0:247c337c230a | 401 | Motor = 0; |
| Nurbol | 2:881ca0a50c9b | 402 | led3 = 1; // led 3 switch on because we have 2 boxes of 1p full |
| Nurbol | 2:881ca0a50c9b | 403 | if(SW1p == 1){ // if switch 1p is switched |
| Nurbol | 2:881ca0a50c9b | 404 | servo_0.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 1p go to position 1 |
| Nurbol | 2:881ca0a50c9b | 405 | state = 0; // go to state 0 |
| Nurbol | 0:247c337c230a | 406 | } |
| Nurbol | 0:247c337c230a | 407 | break; |
| Nurbol | 2:881ca0a50c9b | 408 | case 4: // state 4 |
| Nurbol | 2:881ca0a50c9b | 409 | servo_4.pulsewidth_us(0); // motor stop |
| Nurbol | 0:247c337c230a | 410 | wait(2); |
| Nurbol | 2:881ca0a50c9b | 411 | servo_5.pulsewidth_us(1000 + (200 * 1000) / 90); // servo 2p go to position 2 |
| Nurbol | 0:247c337c230a | 412 | wait(1); |
| Nurbol | 0:247c337c230a | 413 | Position1_2p = 0; |
| Nurbol | 2:881ca0a50c9b | 414 | Position2_2p = 1; // position 2 for servo 2p = 1 |
| Nurbol | 2:881ca0a50c9b | 415 | Motor = 0; // motor stopping |
| Nurbol | 2:881ca0a50c9b | 416 | if((Position2_2p == 1)&&(counter2p < 0.5)){ // if servo 2p is in position 2 and counter 2p < 5 coins |
| Nurbol | 2:881ca0a50c9b | 417 | state = 5; // go to state 5 |
| Nurbol | 0:247c337c230a | 418 | } |
| Nurbol | 0:247c337c230a | 419 | break; |
| Nurbol | 2:881ca0a50c9b | 420 | case 5: // state 5 |
| Nurbol | 2:881ca0a50c9b | 421 | servo_4.pulsewidth_us(1000 + (25 * 1000) / 90); // motor run |
| Nurbol | 2:881ca0a50c9b | 422 | Motor = 1; |
| Nurbol | 2:881ca0a50c9b | 423 | counter2p.read(); // read value of counter 2p |
| Nurbol | 2:881ca0a50c9b | 424 | counter1p.read(); // read value of counter 1p |
| Nurbol | 2:881ca0a50c9b | 425 | if(counter2p > 0.5){ // if counter 2p = 5 coins |
| Nurbol | 2:881ca0a50c9b | 426 | state = 6; // go to state 6 |
| Nurbol | 0:247c337c230a | 427 | } |
| Nurbol | 2:881ca0a50c9b | 428 | else if((counter1p > 0.5)&&(Position1_1p == 1)){ // if counter 1p = 10 coins and servo 1p is in position 1 |
| Nurbol | 2:881ca0a50c9b | 429 | state = 0; // go to state 0 |
| Nurbol | 0:247c337c230a | 430 | } |
| Nurbol | 2:881ca0a50c9b | 431 | else if((counter1p > 0.5)&&(Position2_1p == 1)){ // if counter 1p = 10 coins and servo 1p is in position 2 |
| Nurbol | 2:881ca0a50c9b | 432 | state = 3; // go to state 3 |
| Nurbol | 0:247c337c230a | 433 | } |
| Nurbol | 0:247c337c230a | 434 | break; |
| Nurbol | 2:881ca0a50c9b | 435 | case 6: // state 6 |
| Nurbol | 2:881ca0a50c9b | 436 | servo_4.pulsewidth_us(0); // motor stop |
| Nurbol | 0:247c337c230a | 437 | Motor = 0; |
| Nurbol | 2:881ca0a50c9b | 438 | led4 = 1; // led 4 switch on because we have 2 boxes of 2p full |
| Nurbol | 2:881ca0a50c9b | 439 | if(SW2p == 1){ // if switch 2p is switched |
| Nurbol | 2:881ca0a50c9b | 440 | servo_5.pulsewidth_us(1000 + (0 * 1000) / 90); // servo 2p go to position 1 |
| Nurbol | 2:881ca0a50c9b | 441 | state = 0; // go to state 0 |
| Nurbol | 0:247c337c230a | 442 | } |
| Nurbol | 0:247c337c230a | 443 | break; |
| Nurbol | 0:247c337c230a | 444 | } |
| Nurbol | 0:247c337c230a | 445 | } |
| Nurbol | 0:247c337c230a | 446 | } |
| Nurbol | 0:247c337c230a | 447 | |
| Nurbol | 0:247c337c230a | 448 | // |
| Nurbol | 0:247c337c230a | 449 | //************************************************************************ |
| Nurbol | 0:247c337c230a | 450 | // Main Program |
| Nurbol | 0:247c337c230a | 451 | // |
| Nurbol | 0:247c337c230a | 452 | int main() { |
| Nurbol | 0:247c337c230a | 453 | |
| Nurbol | 2:881ca0a50c9b | 454 | init_sys(); // call function of init system |
| Nurbol | 2:881ca0a50c9b | 455 | Reset = 0; // we not reset the counter in the FPGA |
| Nurbol | 0:247c337c230a | 456 | |
| Nurbol | 0:247c337c230a | 457 | FOREVER { |
| Nurbol | 0:247c337c230a | 458 | |
| Nurbol | 0:247c337c230a | 459 | timersensor.attach(&sensor, 0.02); //function sensor is reading all the 20 ms |
| Nurbol | 0:247c337c230a | 460 | |
| Nurbol | 2:881ca0a50c9b | 461 | counter1p.read(); // read value of counter 1p |
| Nurbol | 2:881ca0a50c9b | 462 | counter2p.read(); // read value of coubter 2p |
| Nurbol | 0:247c337c230a | 463 | |
| Nurbol | 0:247c337c230a | 464 | timerstatemachine.attach(&state_machine, 0.1); // function state machine is readinf all the 100 ms |
| Nurbol | 0:247c337c230a | 465 | |
| Nurbol | 2:881ca0a50c9b | 466 | clk = !clk; // inverse signal of the clock |
| Nurbol | 0:247c337c230a | 467 | wait(0.001); |
| Nurbol | 0:247c337c230a | 468 | |
| Nurbol | 0:247c337c230a | 469 | get_cmd(&ext_cmd); |
| Nurbol | 0:247c337c230a | 470 | // |
| Nurbol | 0:247c337c230a | 471 | // Check status of read command activity and return an error status if there was a problem |
| Nurbol | 0:247c337c230a | 472 | // If there is a problem, then return status code only and wait for next command. |
| Nurbol | 0:247c337c230a | 473 | // |
| Nurbol | 0:247c337c230a | 474 | if (ext_cmd.result_status != OK){ |
| Nurbol | 0:247c337c230a | 475 | send_status(ext_cmd.result_status); |
| Nurbol | 0:247c337c230a | 476 | continue; |
| Nurbol | 0:247c337c230a | 477 | } |
| Nurbol | 0:247c337c230a | 478 | // |
| Nurbol | 0:247c337c230a | 479 | // Parse command and return an error staus if there is a problem |
| Nurbol | 0:247c337c230a | 480 | // If there is a problem, then return status code only and wait for next command. |
| Nurbol | 0:247c337c230a | 481 | // |
| Nurbol | 0:247c337c230a | 482 | parse_cmd(&ext_cmd); |
| Nurbol | 0:247c337c230a | 483 | // lcd->locate(1,0); lcd->printf(ext_cmd.cmd_str); |
| Nurbol | 0:247c337c230a | 484 | if ((ext_cmd.result_status != OK) && (ext_cmd.cmd_code != TEXT_CMD)){ |
| Nurbol | 0:247c337c230a | 485 | lcd->cls(); lcd->locate(0,0); lcd->printf("W3C"); lcd->locate(1,0); lcd->printf("parse : error"); |
| Nurbol | 0:247c337c230a | 486 | send_status(ext_cmd.result_status); |
| Nurbol | 0:247c337c230a | 487 | continue; |
| Nurbol | 0:247c337c230a | 488 | } |
| Nurbol | 0:247c337c230a | 489 | // |
| Nurbol | 0:247c337c230a | 490 | // Execute command and return an error staus if there is a problem |
| Nurbol | 0:247c337c230a | 491 | // |
| Nurbol | 0:247c337c230a | 492 | process_cmd(&ext_cmd); |
| Nurbol | 0:247c337c230a | 493 | reply_to_cmd(&ext_cmd); |
| Nurbol | 0:247c337c230a | 494 | } |
| Nurbol | 0:247c337c230a | 495 | |
| Nurbol | 0:247c337c230a | 496 | } |
| Nurbol | 0:247c337c230a | 497 | |
| Nurbol | 0:247c337c230a | 498 | |
| Nurbol | 0:247c337c230a | 499 | |
| Nurbol | 0:247c337c230a | 500 | |
| Nurbol | 0:247c337c230a | 501 | |
| Nurbol | 0:247c337c230a | 502 | |
| Nurbol | 0:247c337c230a | 503 | |
| Nurbol | 0:247c337c230a | 504 | |
| Nurbol | 0:247c337c230a | 505 |