Make sandwich by moving servos to dispense ingredients via RPC calls.
Dependencies: Servo mbed-dev mbed-rpc
main.cpp@0:9fa3ac1adbab, 2016-12-06 (annotated)
- Committer:
- K2Silver
- Date:
- Tue Dec 06 23:09:07 2016 +0000
- Revision:
- 0:9fa3ac1adbab
Dispense ingredients and bread with servos via RPC calls.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
K2Silver | 0:9fa3ac1adbab | 1 | #include "mbed.h" |
K2Silver | 0:9fa3ac1adbab | 2 | #include "Servo.h" |
K2Silver | 0:9fa3ac1adbab | 3 | #include "mbed_rpc.h" |
K2Silver | 0:9fa3ac1adbab | 4 | |
K2Silver | 0:9fa3ac1adbab | 5 | /* Virtual USB serial port */ |
K2Silver | 0:9fa3ac1adbab | 6 | Serial pc(USBTX, USBRX); // tx, rx |
K2Silver | 0:9fa3ac1adbab | 7 | |
K2Silver | 0:9fa3ac1adbab | 8 | /* Declare all servos */ |
K2Silver | 0:9fa3ac1adbab | 9 | Servo servo_ingredient0(p21); |
K2Silver | 0:9fa3ac1adbab | 10 | Servo servo_ingredient1(p22); |
K2Silver | 0:9fa3ac1adbab | 11 | Servo servo_ingredient2(p23); |
K2Silver | 0:9fa3ac1adbab | 12 | Servo servo_ingredient3(p24); |
K2Silver | 0:9fa3ac1adbab | 13 | Servo servo_bread(p25); |
K2Silver | 0:9fa3ac1adbab | 14 | |
K2Silver | 0:9fa3ac1adbab | 15 | /* Time between opening and closing the ingredient dispenser */ |
K2Silver | 0:9fa3ac1adbab | 16 | int TIME_OPEN = 300; |
K2Silver | 0:9fa3ac1adbab | 17 | |
K2Silver | 0:9fa3ac1adbab | 18 | /* Time after closing the ingredient dispenser */ |
K2Silver | 0:9fa3ac1adbab | 19 | int TIME_AFTER_CLOSING = 500; |
K2Silver | 0:9fa3ac1adbab | 20 | |
K2Silver | 0:9fa3ac1adbab | 21 | /* For debugging purpose */ |
K2Silver | 0:9fa3ac1adbab | 22 | /* Output the ingredient code (which ingredients to dispense) onto LEDs */ |
K2Silver | 0:9fa3ac1adbab | 23 | BusOut mbed_leds(LED1, LED2, LED3, LED4); |
K2Silver | 0:9fa3ac1adbab | 24 | |
K2Silver | 0:9fa3ac1adbab | 25 | /* Make sandwich, using ingredient code */ |
K2Silver | 0:9fa3ac1adbab | 26 | /* Ingredient code is bit mask of ingredients needed */ |
K2Silver | 0:9fa3ac1adbab | 27 | /* Bit 0 -> Ingredient 0 needed (1) or not (0) */ |
K2Silver | 0:9fa3ac1adbab | 28 | /* Bit 1 -> Ingredient 0 needed (1) or not (0) */ |
K2Silver | 0:9fa3ac1adbab | 29 | /* Bit 2 -> Ingredient 0 needed (1) or not (0) */ |
K2Silver | 0:9fa3ac1adbab | 30 | /* Bit 3 -> Ingredient 0 needed (1) or not (0) */ |
K2Silver | 0:9fa3ac1adbab | 31 | void move_servos(int ingr_code) { |
K2Silver | 0:9fa3ac1adbab | 32 | /* Dispense bread */ |
K2Silver | 0:9fa3ac1adbab | 33 | servo_bread = 0; /* Dispense bread */ |
K2Silver | 0:9fa3ac1adbab | 34 | wait_ms(1000); |
K2Silver | 0:9fa3ac1adbab | 35 | servo_bread = 1; /* Reset position */ |
K2Silver | 0:9fa3ac1adbab | 36 | wait_ms(1000); |
K2Silver | 0:9fa3ac1adbab | 37 | |
K2Silver | 0:9fa3ac1adbab | 38 | /* Dispense ingredient 0 */ |
K2Silver | 0:9fa3ac1adbab | 39 | if (ingr_code & 0x1) { |
K2Silver | 0:9fa3ac1adbab | 40 | servo_ingredient0 = 1; /* Open */ |
K2Silver | 0:9fa3ac1adbab | 41 | wait_ms(TIME_OPEN); |
K2Silver | 0:9fa3ac1adbab | 42 | servo_ingredient0 = 0; /* Close */ |
K2Silver | 0:9fa3ac1adbab | 43 | wait_ms(TIME_AFTER_CLOSING); |
K2Silver | 0:9fa3ac1adbab | 44 | } |
K2Silver | 0:9fa3ac1adbab | 45 | /* Dispense ingredient 1 */ |
K2Silver | 0:9fa3ac1adbab | 46 | if (ingr_code & 0x2) { |
K2Silver | 0:9fa3ac1adbab | 47 | servo_ingredient1 = 1; /* Open */ |
K2Silver | 0:9fa3ac1adbab | 48 | wait_ms(TIME_OPEN); |
K2Silver | 0:9fa3ac1adbab | 49 | servo_ingredient1 = 0; /* Close */ |
K2Silver | 0:9fa3ac1adbab | 50 | wait_ms(TIME_AFTER_CLOSING); |
K2Silver | 0:9fa3ac1adbab | 51 | } |
K2Silver | 0:9fa3ac1adbab | 52 | /* Dispense ingredient 2 */ |
K2Silver | 0:9fa3ac1adbab | 53 | if (ingr_code & 0x4) { |
K2Silver | 0:9fa3ac1adbab | 54 | servo_ingredient2 = 1; /* Open */ |
K2Silver | 0:9fa3ac1adbab | 55 | wait_ms(TIME_OPEN); |
K2Silver | 0:9fa3ac1adbab | 56 | servo_ingredient2 = 0; /* Close */ |
K2Silver | 0:9fa3ac1adbab | 57 | wait_ms(TIME_AFTER_CLOSING); |
K2Silver | 0:9fa3ac1adbab | 58 | } |
K2Silver | 0:9fa3ac1adbab | 59 | /* Dispense ingredient 3 */ |
K2Silver | 0:9fa3ac1adbab | 60 | if (ingr_code & 0x8) { |
K2Silver | 0:9fa3ac1adbab | 61 | servo_ingredient3 = 1; /* Open */ |
K2Silver | 0:9fa3ac1adbab | 62 | wait_ms(TIME_OPEN); |
K2Silver | 0:9fa3ac1adbab | 63 | servo_ingredient3 = 0; /* Close */ |
K2Silver | 0:9fa3ac1adbab | 64 | wait_ms(TIME_AFTER_CLOSING); |
K2Silver | 0:9fa3ac1adbab | 65 | } |
K2Silver | 0:9fa3ac1adbab | 66 | /* Dispense bread again */ |
K2Silver | 0:9fa3ac1adbab | 67 | servo_bread = 0; /* Dispense bread */ |
K2Silver | 0:9fa3ac1adbab | 68 | wait_ms(1000); |
K2Silver | 0:9fa3ac1adbab | 69 | servo_bread = 1; /* Reset position */ |
K2Silver | 0:9fa3ac1adbab | 70 | wait_ms(1000); |
K2Silver | 0:9fa3ac1adbab | 71 | } |
K2Silver | 0:9fa3ac1adbab | 72 | |
K2Silver | 0:9fa3ac1adbab | 73 | /* Declare function 'm_sand' exposed via RPC */ |
K2Silver | 0:9fa3ac1adbab | 74 | void m_sand(Arguments *in, Reply *out); |
K2Silver | 0:9fa3ac1adbab | 75 | RPCFunction rpc_m_sand(&m_sand, "m_sand"); |
K2Silver | 0:9fa3ac1adbab | 76 | void m_sand(Arguments *in, Reply *out) { |
K2Silver | 0:9fa3ac1adbab | 77 | |
K2Silver | 0:9fa3ac1adbab | 78 | /* Get argument */ |
K2Silver | 0:9fa3ac1adbab | 79 | int ingr_code = in->getArg<int>(); |
K2Silver | 0:9fa3ac1adbab | 80 | |
K2Silver | 0:9fa3ac1adbab | 81 | /* Output to LEDs for debugging */ |
K2Silver | 0:9fa3ac1adbab | 82 | mbed_leds = ingr_code; |
K2Silver | 0:9fa3ac1adbab | 83 | |
K2Silver | 0:9fa3ac1adbab | 84 | /* Execute the function */ |
K2Silver | 0:9fa3ac1adbab | 85 | move_servos(ingr_code); |
K2Silver | 0:9fa3ac1adbab | 86 | |
K2Silver | 0:9fa3ac1adbab | 87 | /* Set output */ |
K2Silver | 0:9fa3ac1adbab | 88 | out->putData("SandwichMade"); |
K2Silver | 0:9fa3ac1adbab | 89 | |
K2Silver | 0:9fa3ac1adbab | 90 | /* Reset LEDs */ |
K2Silver | 0:9fa3ac1adbab | 91 | mbed_leds = 0; |
K2Silver | 0:9fa3ac1adbab | 92 | } |
K2Silver | 0:9fa3ac1adbab | 93 | |
K2Silver | 0:9fa3ac1adbab | 94 | /* Main code */ |
K2Silver | 0:9fa3ac1adbab | 95 | int main() { |
K2Silver | 0:9fa3ac1adbab | 96 | /* Set pc serial baud rate */ |
K2Silver | 0:9fa3ac1adbab | 97 | pc.baud(9600); |
K2Silver | 0:9fa3ac1adbab | 98 | |
K2Silver | 0:9fa3ac1adbab | 99 | // receive commands, and send back the responses |
K2Silver | 0:9fa3ac1adbab | 100 | char buf[256], outbuf[256]; |
K2Silver | 0:9fa3ac1adbab | 101 | |
K2Silver | 0:9fa3ac1adbab | 102 | while(1) { |
K2Silver | 0:9fa3ac1adbab | 103 | /* Wait for command */ |
K2Silver | 0:9fa3ac1adbab | 104 | while (!pc.readable()) { |
K2Silver | 0:9fa3ac1adbab | 105 | wait_ms(100); |
K2Silver | 0:9fa3ac1adbab | 106 | } |
K2Silver | 0:9fa3ac1adbab | 107 | /* Read the characters into string array */ |
K2Silver | 0:9fa3ac1adbab | 108 | int i = 0; |
K2Silver | 0:9fa3ac1adbab | 109 | while (pc.readable()) { |
K2Silver | 0:9fa3ac1adbab | 110 | buf[i] = pc.getc(); |
K2Silver | 0:9fa3ac1adbab | 111 | i++; |
K2Silver | 0:9fa3ac1adbab | 112 | } |
K2Silver | 0:9fa3ac1adbab | 113 | buf[i] = '\n'; |
K2Silver | 0:9fa3ac1adbab | 114 | pc.printf("%s", buf); |
K2Silver | 0:9fa3ac1adbab | 115 | //Call the static call method on the RPC class |
K2Silver | 0:9fa3ac1adbab | 116 | RPC::call(buf, outbuf); |
K2Silver | 0:9fa3ac1adbab | 117 | pc.printf("%s\n", outbuf); |
K2Silver | 0:9fa3ac1adbab | 118 | /* Flush pc serial buffer */ |
K2Silver | 0:9fa3ac1adbab | 119 | while (pc.readable()) { |
K2Silver | 0:9fa3ac1adbab | 120 | pc.getc(); |
K2Silver | 0:9fa3ac1adbab | 121 | } |
K2Silver | 0:9fa3ac1adbab | 122 | } |
K2Silver | 0:9fa3ac1adbab | 123 | |
K2Silver | 0:9fa3ac1adbab | 124 | } |