This is an MBED controlled liquid dispensing robot. With applications in bars.
Dependencies: EthernetInterface WebSocketClient mbed-rtos mbed
main.cpp@0:4dcc14de1899, 2014-12-09 (annotated)
- Committer:
- sdhingra
- Date:
- Tue Dec 09 00:10:29 2014 +0000
- Revision:
- 0:4dcc14de1899
Initial Commit - BarBot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sdhingra | 0:4dcc14de1899 | 1 | #include "mbed.h" |
sdhingra | 0:4dcc14de1899 | 2 | #include "EthernetInterface.h" |
sdhingra | 0:4dcc14de1899 | 3 | #include "Websocket.h" |
sdhingra | 0:4dcc14de1899 | 4 | #include "stdio.h" |
sdhingra | 0:4dcc14de1899 | 5 | #include "string.h" |
sdhingra | 0:4dcc14de1899 | 6 | |
sdhingra | 0:4dcc14de1899 | 7 | //Used to Log to the PC |
sdhingra | 0:4dcc14de1899 | 8 | Serial pc(USBTX,USBRX); |
sdhingra | 0:4dcc14de1899 | 9 | |
sdhingra | 0:4dcc14de1899 | 10 | //Input Pin for the Paddle Wheel Flow Meter |
sdhingra | 0:4dcc14de1899 | 11 | DigitalIn pulse(p8); |
sdhingra | 0:4dcc14de1899 | 12 | |
sdhingra | 0:4dcc14de1899 | 13 | // Pin Outs for Solenoids |
sdhingra | 0:4dcc14de1899 | 14 | DigitalOut Ctrl(p9); |
sdhingra | 0:4dcc14de1899 | 15 | DigitalOut Ctrl2(p10); |
sdhingra | 0:4dcc14de1899 | 16 | |
sdhingra | 0:4dcc14de1899 | 17 | // |
sdhingra | 0:4dcc14de1899 | 18 | #define PULSE_VOL 4.50 // More flow due to angle of paddle wheel Pour 1 |
sdhingra | 0:4dcc14de1899 | 19 | #define PULSE_VOL2 6.75 // Less flow due to angle of paddle wheel Pour 2 |
sdhingra | 0:4dcc14de1899 | 20 | |
sdhingra | 0:4dcc14de1899 | 21 | //Function Declarations |
sdhingra | 0:4dcc14de1899 | 22 | void pourOne(float milliliters); |
sdhingra | 0:4dcc14de1899 | 23 | void pourTwo(float milliliters); |
sdhingra | 0:4dcc14de1899 | 24 | |
sdhingra | 0:4dcc14de1899 | 25 | /* |
sdhingra | 0:4dcc14de1899 | 26 | The main function opens a web socket client and listens for a preset series |
sdhingra | 0:4dcc14de1899 | 27 | of recipies. If one is found, it calls functions to pour liquid. |
sdhingra | 0:4dcc14de1899 | 28 | */ |
sdhingra | 0:4dcc14de1899 | 29 | int main() { |
sdhingra | 0:4dcc14de1899 | 30 | pc.printf("Starting BarBot!\r\n"); |
sdhingra | 0:4dcc14de1899 | 31 | |
sdhingra | 0:4dcc14de1899 | 32 | // Close Both Solenoids |
sdhingra | 0:4dcc14de1899 | 33 | Ctrl = 0; |
sdhingra | 0:4dcc14de1899 | 34 | Ctrl2 = 0; |
sdhingra | 0:4dcc14de1899 | 35 | |
sdhingra | 0:4dcc14de1899 | 36 | // Receive Buffer |
sdhingra | 0:4dcc14de1899 | 37 | char recv[30]; |
sdhingra | 0:4dcc14de1899 | 38 | char recipe[] = "Rum & Coke"; |
sdhingra | 0:4dcc14de1899 | 39 | char recipe2[] = "Whiskey Ginger"; |
sdhingra | 0:4dcc14de1899 | 40 | char recipe3[] = "Whiskey Neat"; |
sdhingra | 0:4dcc14de1899 | 41 | |
sdhingra | 0:4dcc14de1899 | 42 | // Ethernet Interfacing |
sdhingra | 0:4dcc14de1899 | 43 | EthernetInterface eth; |
sdhingra | 0:4dcc14de1899 | 44 | eth.init(); //Use DHCP |
sdhingra | 0:4dcc14de1899 | 45 | eth.connect(); |
sdhingra | 0:4dcc14de1899 | 46 | pc.printf("IP Address is %s\n\r", eth.getIPAddress()); |
sdhingra | 0:4dcc14de1899 | 47 | |
sdhingra | 0:4dcc14de1899 | 48 | // Open a Websocket |
sdhingra | 0:4dcc14de1899 | 49 | Websocket ws("ws://sockets.mbed.org:443/ws/sid/ro"); //This Websocket server is provided by MBED for prototyping |
sdhingra | 0:4dcc14de1899 | 50 | ws.connect(); |
sdhingra | 0:4dcc14de1899 | 51 | |
sdhingra | 0:4dcc14de1899 | 52 | // Wait to receive an order from websocket |
sdhingra | 0:4dcc14de1899 | 53 | while (1) { |
sdhingra | 0:4dcc14de1899 | 54 | |
sdhingra | 0:4dcc14de1899 | 55 | if(ws.read(recv)) { |
sdhingra | 0:4dcc14de1899 | 56 | pc.printf("%s\r\n", recv); |
sdhingra | 0:4dcc14de1899 | 57 | |
sdhingra | 0:4dcc14de1899 | 58 | // Recipe based process |
sdhingra | 0:4dcc14de1899 | 59 | if(strcmp(recv, recipe) == 0) { |
sdhingra | 0:4dcc14de1899 | 60 | pourOne(132); |
sdhingra | 0:4dcc14de1899 | 61 | pourTwo(44); |
sdhingra | 0:4dcc14de1899 | 62 | } |
sdhingra | 0:4dcc14de1899 | 63 | else if(strcmp(recv, recipe2) == 0) { |
sdhingra | 0:4dcc14de1899 | 64 | pourOne(0); |
sdhingra | 0:4dcc14de1899 | 65 | pourTwo(88); |
sdhingra | 0:4dcc14de1899 | 66 | } |
sdhingra | 0:4dcc14de1899 | 67 | else if(strcmp(recv, recipe3) == 0) { |
sdhingra | 0:4dcc14de1899 | 68 | pourOne(88); |
sdhingra | 0:4dcc14de1899 | 69 | pourTwo(0); |
sdhingra | 0:4dcc14de1899 | 70 | } |
sdhingra | 0:4dcc14de1899 | 71 | } |
sdhingra | 0:4dcc14de1899 | 72 | |
sdhingra | 0:4dcc14de1899 | 73 | wait(1.0); |
sdhingra | 0:4dcc14de1899 | 74 | } |
sdhingra | 0:4dcc14de1899 | 75 | |
sdhingra | 0:4dcc14de1899 | 76 | } |
sdhingra | 0:4dcc14de1899 | 77 | |
sdhingra | 0:4dcc14de1899 | 78 | void pourOne(float milliliters) { |
sdhingra | 0:4dcc14de1899 | 79 | |
sdhingra | 0:4dcc14de1899 | 80 | pc.printf("Pour One\r\n"); |
sdhingra | 0:4dcc14de1899 | 81 | |
sdhingra | 0:4dcc14de1899 | 82 | //pouring an empty amount (neat drinks) |
sdhingra | 0:4dcc14de1899 | 83 | if(milliliters <= 0) { |
sdhingra | 0:4dcc14de1899 | 84 | return; |
sdhingra | 0:4dcc14de1899 | 85 | } |
sdhingra | 0:4dcc14de1899 | 86 | |
sdhingra | 0:4dcc14de1899 | 87 | int pulses = 0; |
sdhingra | 0:4dcc14de1899 | 88 | bool lastPulse = pulse; |
sdhingra | 0:4dcc14de1899 | 89 | bool thisPulse; |
sdhingra | 0:4dcc14de1899 | 90 | float volume = 0; |
sdhingra | 0:4dcc14de1899 | 91 | |
sdhingra | 0:4dcc14de1899 | 92 | //open solenoid one |
sdhingra | 0:4dcc14de1899 | 93 | Ctrl = 1; |
sdhingra | 0:4dcc14de1899 | 94 | |
sdhingra | 0:4dcc14de1899 | 95 | while(volume < milliliters) { |
sdhingra | 0:4dcc14de1899 | 96 | thisPulse = pulse; |
sdhingra | 0:4dcc14de1899 | 97 | |
sdhingra | 0:4dcc14de1899 | 98 | //check to see if pulse has changed since the last time |
sdhingra | 0:4dcc14de1899 | 99 | if(thisPulse != lastPulse) { |
sdhingra | 0:4dcc14de1899 | 100 | pulses += 1; |
sdhingra | 0:4dcc14de1899 | 101 | } |
sdhingra | 0:4dcc14de1899 | 102 | lastPulse = thisPulse; |
sdhingra | 0:4dcc14de1899 | 103 | |
sdhingra | 0:4dcc14de1899 | 104 | //increase the volume |
sdhingra | 0:4dcc14de1899 | 105 | volume = pulses * PULSE_VOL; |
sdhingra | 0:4dcc14de1899 | 106 | |
sdhingra | 0:4dcc14de1899 | 107 | pc.printf("Poured %f From First Bottle\r\n", volume); |
sdhingra | 0:4dcc14de1899 | 108 | } |
sdhingra | 0:4dcc14de1899 | 109 | |
sdhingra | 0:4dcc14de1899 | 110 | |
sdhingra | 0:4dcc14de1899 | 111 | //close solenoid one |
sdhingra | 0:4dcc14de1899 | 112 | Ctrl = 0; |
sdhingra | 0:4dcc14de1899 | 113 | } |
sdhingra | 0:4dcc14de1899 | 114 | |
sdhingra | 0:4dcc14de1899 | 115 | void pourTwo(float milliliters) { |
sdhingra | 0:4dcc14de1899 | 116 | |
sdhingra | 0:4dcc14de1899 | 117 | pc.printf("Pour Two\r\n"); |
sdhingra | 0:4dcc14de1899 | 118 | |
sdhingra | 0:4dcc14de1899 | 119 | //pouring an empty amount (neat drinks) |
sdhingra | 0:4dcc14de1899 | 120 | if(milliliters <= 0) { |
sdhingra | 0:4dcc14de1899 | 121 | return; |
sdhingra | 0:4dcc14de1899 | 122 | } |
sdhingra | 0:4dcc14de1899 | 123 | |
sdhingra | 0:4dcc14de1899 | 124 | int pulses = 0; |
sdhingra | 0:4dcc14de1899 | 125 | bool lastPulse = pulse; |
sdhingra | 0:4dcc14de1899 | 126 | bool thisPulse; |
sdhingra | 0:4dcc14de1899 | 127 | float volume = 0; |
sdhingra | 0:4dcc14de1899 | 128 | |
sdhingra | 0:4dcc14de1899 | 129 | //open solenoid two |
sdhingra | 0:4dcc14de1899 | 130 | Ctrl2 = 1; |
sdhingra | 0:4dcc14de1899 | 131 | |
sdhingra | 0:4dcc14de1899 | 132 | while(volume < milliliters) { |
sdhingra | 0:4dcc14de1899 | 133 | thisPulse = pulse; |
sdhingra | 0:4dcc14de1899 | 134 | |
sdhingra | 0:4dcc14de1899 | 135 | //check to see if pulse has changed since last time |
sdhingra | 0:4dcc14de1899 | 136 | if(thisPulse != lastPulse) { |
sdhingra | 0:4dcc14de1899 | 137 | pulses += 1; |
sdhingra | 0:4dcc14de1899 | 138 | } |
sdhingra | 0:4dcc14de1899 | 139 | |
sdhingra | 0:4dcc14de1899 | 140 | lastPulse = thisPulse; |
sdhingra | 0:4dcc14de1899 | 141 | |
sdhingra | 0:4dcc14de1899 | 142 | //increase the volume |
sdhingra | 0:4dcc14de1899 | 143 | volume = pulses * PULSE_VOL2; |
sdhingra | 0:4dcc14de1899 | 144 | |
sdhingra | 0:4dcc14de1899 | 145 | pc.printf("Poured %f From Second Bottle\r\n", volume); |
sdhingra | 0:4dcc14de1899 | 146 | } |
sdhingra | 0:4dcc14de1899 | 147 | |
sdhingra | 0:4dcc14de1899 | 148 | //close solenoid two |
sdhingra | 0:4dcc14de1899 | 149 | Ctrl2 = 0; |
sdhingra | 0:4dcc14de1899 | 150 | } |