Make sandwich by moving servos to dispense ingredients via RPC calls.
Dependencies: Servo mbed-dev mbed-rpc
Revision 0:9fa3ac1adbab, committed 2016-12-06
- Comitter:
- K2Silver
- Date:
- Tue Dec 06 23:09:07 2016 +0000
- Commit message:
- Dispense ingredients and bread with servos via RPC calls.
Changed in this revision
diff -r 000000000000 -r 9fa3ac1adbab Servo.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Tue Dec 06 23:09:07 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 000000000000 -r 9fa3ac1adbab main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Dec 06 23:09:07 2016 +0000 @@ -0,0 +1,124 @@ +#include "mbed.h" +#include "Servo.h" +#include "mbed_rpc.h" + +/* Virtual USB serial port */ +Serial pc(USBTX, USBRX); // tx, rx + +/* Declare all servos */ +Servo servo_ingredient0(p21); +Servo servo_ingredient1(p22); +Servo servo_ingredient2(p23); +Servo servo_ingredient3(p24); +Servo servo_bread(p25); + +/* Time between opening and closing the ingredient dispenser */ +int TIME_OPEN = 300; + +/* Time after closing the ingredient dispenser */ +int TIME_AFTER_CLOSING = 500; + +/* For debugging purpose */ +/* Output the ingredient code (which ingredients to dispense) onto LEDs */ +BusOut mbed_leds(LED1, LED2, LED3, LED4); + +/* Make sandwich, using ingredient code */ +/* Ingredient code is bit mask of ingredients needed */ +/* Bit 0 -> Ingredient 0 needed (1) or not (0) */ +/* Bit 1 -> Ingredient 0 needed (1) or not (0) */ +/* Bit 2 -> Ingredient 0 needed (1) or not (0) */ +/* Bit 3 -> Ingredient 0 needed (1) or not (0) */ +void move_servos(int ingr_code) { + /* Dispense bread */ + servo_bread = 0; /* Dispense bread */ + wait_ms(1000); + servo_bread = 1; /* Reset position */ + wait_ms(1000); + + /* Dispense ingredient 0 */ + if (ingr_code & 0x1) { + servo_ingredient0 = 1; /* Open */ + wait_ms(TIME_OPEN); + servo_ingredient0 = 0; /* Close */ + wait_ms(TIME_AFTER_CLOSING); + } + /* Dispense ingredient 1 */ + if (ingr_code & 0x2) { + servo_ingredient1 = 1; /* Open */ + wait_ms(TIME_OPEN); + servo_ingredient1 = 0; /* Close */ + wait_ms(TIME_AFTER_CLOSING); + } + /* Dispense ingredient 2 */ + if (ingr_code & 0x4) { + servo_ingredient2 = 1; /* Open */ + wait_ms(TIME_OPEN); + servo_ingredient2 = 0; /* Close */ + wait_ms(TIME_AFTER_CLOSING); + } + /* Dispense ingredient 3 */ + if (ingr_code & 0x8) { + servo_ingredient3 = 1; /* Open */ + wait_ms(TIME_OPEN); + servo_ingredient3 = 0; /* Close */ + wait_ms(TIME_AFTER_CLOSING); + } + /* Dispense bread again */ + servo_bread = 0; /* Dispense bread */ + wait_ms(1000); + servo_bread = 1; /* Reset position */ + wait_ms(1000); +} + +/* Declare function 'm_sand' exposed via RPC */ +void m_sand(Arguments *in, Reply *out); +RPCFunction rpc_m_sand(&m_sand, "m_sand"); +void m_sand(Arguments *in, Reply *out) { + + /* Get argument */ + int ingr_code = in->getArg<int>(); + + /* Output to LEDs for debugging */ + mbed_leds = ingr_code; + + /* Execute the function */ + move_servos(ingr_code); + + /* Set output */ + out->putData("SandwichMade"); + + /* Reset LEDs */ + mbed_leds = 0; +} + +/* Main code */ +int main() { + /* Set pc serial baud rate */ + pc.baud(9600); + + // receive commands, and send back the responses + char buf[256], outbuf[256]; + + while(1) { + /* Wait for command */ + while (!pc.readable()) { + wait_ms(100); + } + /* Read the characters into string array */ + int i = 0; + while (pc.readable()) { + buf[i] = pc.getc(); + i++; + } + buf[i] = '\n'; + pc.printf("%s", buf); + //Call the static call method on the RPC class + RPC::call(buf, outbuf); + pc.printf("%s\n", outbuf); + /* Flush pc serial buffer */ + while (pc.readable()) { + pc.getc(); + } + } + +}
diff -r 000000000000 -r 9fa3ac1adbab mbed-dev.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-dev.lib Tue Dec 06 23:09:07 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbed_official/code/mbed-dev/#156823d33999
diff -r 000000000000 -r 9fa3ac1adbab mbed-rpc.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rpc.lib Tue Dec 06 23:09:07 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/mbed/code/mbed-rpc/#33e21ae4d434