
Project for ECE4180. A Shadowbot with Remote Procedure Call controls. Found here: https://developer.mbed.org/users/Scout/notebook/shadowbot-with-remote-procedure-calls/
Dependencies: 4DGL-uLCD-SE mbed-rpc mbed
main.cpp@0:09bf5c3e5302, 2017-03-15 (annotated)
- Committer:
- Scout
- Date:
- Wed Mar 15 02:33:58 2017 +0000
- Revision:
- 0:09bf5c3e5302
Final 4180 Lab 4 Project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Scout | 0:09bf5c3e5302 | 1 | // Scout Schultz |
Scout | 0:09bf5c3e5302 | 2 | // ECE4180 Lab 4 Project - Remote Procedure Call Bot |
Scout | 0:09bf5c3e5302 | 3 | |
Scout | 0:09bf5c3e5302 | 4 | #include "mbed.h" |
Scout | 0:09bf5c3e5302 | 5 | #include "mbed_rpc.h" |
Scout | 0:09bf5c3e5302 | 6 | #include "uLCD_4DGL.h" |
Scout | 0:09bf5c3e5302 | 7 | |
Scout | 0:09bf5c3e5302 | 8 | // Serial Interface - Comment Out One of the Two Lines and Uncomment The Other |
Scout | 0:09bf5c3e5302 | 9 | Serial pc(p28,p27); //BlueTooth Control |
Scout | 0:09bf5c3e5302 | 10 | //Serial pc(USBTX, USBRX); //PC Serial USB Control: For Testing Only |
Scout | 0:09bf5c3e5302 | 11 | |
Scout | 0:09bf5c3e5302 | 12 | DigitalOut led1(LED1); |
Scout | 0:09bf5c3e5302 | 13 | DigitalOut led2(LED2); |
Scout | 0:09bf5c3e5302 | 14 | DigitalOut led3(LED3); |
Scout | 0:09bf5c3e5302 | 15 | RpcDigitalOut led4(LED4,"led4"); |
Scout | 0:09bf5c3e5302 | 16 | |
Scout | 0:09bf5c3e5302 | 17 | // uLCD Setup |
Scout | 0:09bf5c3e5302 | 18 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
Scout | 0:09bf5c3e5302 | 19 | int bgColor = 0x000000; |
Scout | 0:09bf5c3e5302 | 20 | int fgColor = 0xFFFFFF; |
Scout | 0:09bf5c3e5302 | 21 | |
Scout | 0:09bf5c3e5302 | 22 | // Set up the motor pins |
Scout | 0:09bf5c3e5302 | 23 | PwmOut motorACtrl(p26); |
Scout | 0:09bf5c3e5302 | 24 | PwmOut motorBCtrl(p21); |
Scout | 0:09bf5c3e5302 | 25 | DigitalOut backA(p25); |
Scout | 0:09bf5c3e5302 | 26 | DigitalOut fwdA(p24); |
Scout | 0:09bf5c3e5302 | 27 | //DigitalOut stby(p18); |
Scout | 0:09bf5c3e5302 | 28 | DigitalOut fwdB(p23); |
Scout | 0:09bf5c3e5302 | 29 | DigitalOut backB(p22); |
Scout | 0:09bf5c3e5302 | 30 | |
Scout | 0:09bf5c3e5302 | 31 | void updateFace(); |
Scout | 0:09bf5c3e5302 | 32 | |
Scout | 0:09bf5c3e5302 | 33 | void drive(Arguments * input, Reply * output) |
Scout | 0:09bf5c3e5302 | 34 | { |
Scout | 0:09bf5c3e5302 | 35 | // Reset for simplicity sake |
Scout | 0:09bf5c3e5302 | 36 | fwdA=0; |
Scout | 0:09bf5c3e5302 | 37 | fwdB=0; |
Scout | 0:09bf5c3e5302 | 38 | backA=0; |
Scout | 0:09bf5c3e5302 | 39 | backB=0; |
Scout | 0:09bf5c3e5302 | 40 | double speedL = input->getArg<double>(); |
Scout | 0:09bf5c3e5302 | 41 | double speedR = input->getArg<double>(); |
Scout | 0:09bf5c3e5302 | 42 | if(speedL < 0) { |
Scout | 0:09bf5c3e5302 | 43 | speedL = -speedL; |
Scout | 0:09bf5c3e5302 | 44 | backB = 1; |
Scout | 0:09bf5c3e5302 | 45 | } else if(speedL > 0) { |
Scout | 0:09bf5c3e5302 | 46 | fwdB = 1; |
Scout | 0:09bf5c3e5302 | 47 | } |
Scout | 0:09bf5c3e5302 | 48 | |
Scout | 0:09bf5c3e5302 | 49 | if(speedR < 0) { |
Scout | 0:09bf5c3e5302 | 50 | speedR = -speedR; |
Scout | 0:09bf5c3e5302 | 51 | backA = 1; |
Scout | 0:09bf5c3e5302 | 52 | } else if(speedR > 0) { |
Scout | 0:09bf5c3e5302 | 53 | fwdA = 1; |
Scout | 0:09bf5c3e5302 | 54 | } |
Scout | 0:09bf5c3e5302 | 55 | |
Scout | 0:09bf5c3e5302 | 56 | motorBCtrl = speedL; |
Scout | 0:09bf5c3e5302 | 57 | motorACtrl = speedR; |
Scout | 0:09bf5c3e5302 | 58 | updateFace(); |
Scout | 0:09bf5c3e5302 | 59 | } |
Scout | 0:09bf5c3e5302 | 60 | |
Scout | 0:09bf5c3e5302 | 61 | RPCFunction setDrive(&drive,"drive"); |
Scout | 0:09bf5c3e5302 | 62 | |
Scout | 0:09bf5c3e5302 | 63 | void updateFace() |
Scout | 0:09bf5c3e5302 | 64 | { |
Scout | 0:09bf5c3e5302 | 65 | float fwdSurprise = 0.7; |
Scout | 0:09bf5c3e5302 | 66 | float backSurprise = 0.4; |
Scout | 0:09bf5c3e5302 | 67 | uLCD.cls(); |
Scout | 0:09bf5c3e5302 | 68 | |
Scout | 0:09bf5c3e5302 | 69 | // Too Fast is Scary |
Scout | 0:09bf5c3e5302 | 70 | if( fwdA==1 && motorACtrl>fwdSurprise || fwdB==1 && motorBCtrl>fwdSurprise || backA==1 && motorACtrl>backSurprise || backB==1 && motorBCtrl>backSurprise) { |
Scout | 0:09bf5c3e5302 | 71 | uLCD.cls(); |
Scout | 0:09bf5c3e5302 | 72 | //Open-Circle Mouth |
Scout | 0:09bf5c3e5302 | 73 | uLCD.filled_circle(64,80,20,fgColor); |
Scout | 0:09bf5c3e5302 | 74 | uLCD.filled_circle(64,80,15,bgColor); |
Scout | 0:09bf5c3e5302 | 75 | //Eyes |
Scout | 0:09bf5c3e5302 | 76 | uLCD.filled_circle(28,50,10,fgColor); |
Scout | 0:09bf5c3e5302 | 77 | uLCD.filled_circle(100,50,10,fgColor); |
Scout | 0:09bf5c3e5302 | 78 | } |
Scout | 0:09bf5c3e5302 | 79 | // Stationary is Boring |
Scout | 0:09bf5c3e5302 | 80 | else if( fwdA==0 && fwdB==0 && backA==0 && backB==0 ) { |
Scout | 0:09bf5c3e5302 | 81 | uLCD.cls(); |
Scout | 0:09bf5c3e5302 | 82 | //Straight-Line Mouth |
Scout | 0:09bf5c3e5302 | 83 | uLCD.filled_rectangle(34,77,94,82,fgColor); |
Scout | 0:09bf5c3e5302 | 84 | //Eyes |
Scout | 0:09bf5c3e5302 | 85 | uLCD.filled_circle(28,50,10,fgColor); |
Scout | 0:09bf5c3e5302 | 86 | uLCD.filled_circle(100,50,10,fgColor); |
Scout | 0:09bf5c3e5302 | 87 | } |
Scout | 0:09bf5c3e5302 | 88 | // Nice movement speed is nice |
Scout | 0:09bf5c3e5302 | 89 | else { |
Scout | 0:09bf5c3e5302 | 90 | //Smile |
Scout | 0:09bf5c3e5302 | 91 | uLCD.filled_circle(64,70,30,fgColor); |
Scout | 0:09bf5c3e5302 | 92 | uLCD.filled_circle(64,70,25,bgColor); |
Scout | 0:09bf5c3e5302 | 93 | uLCD.filled_rectangle(0,0,128,70,bgColor); |
Scout | 0:09bf5c3e5302 | 94 | //Eyes |
Scout | 0:09bf5c3e5302 | 95 | uLCD.filled_circle(28,50,10,fgColor); |
Scout | 0:09bf5c3e5302 | 96 | uLCD.filled_circle(100,50,10,fgColor); |
Scout | 0:09bf5c3e5302 | 97 | } |
Scout | 0:09bf5c3e5302 | 98 | } |
Scout | 0:09bf5c3e5302 | 99 | |
Scout | 0:09bf5c3e5302 | 100 | |
Scout | 0:09bf5c3e5302 | 101 | int main() |
Scout | 0:09bf5c3e5302 | 102 | { |
Scout | 0:09bf5c3e5302 | 103 | updateFace(); |
Scout | 0:09bf5c3e5302 | 104 | |
Scout | 0:09bf5c3e5302 | 105 | // receive commands, and send back the responses |
Scout | 0:09bf5c3e5302 | 106 | char buf[256], outbuf[256]; |
Scout | 0:09bf5c3e5302 | 107 | while(1) { |
Scout | 0:09bf5c3e5302 | 108 | if(pc.readable()) { |
Scout | 0:09bf5c3e5302 | 109 | pc.gets(buf, 256); |
Scout | 0:09bf5c3e5302 | 110 | strcat(buf, "\r"); //add LF to BlueFruit input |
Scout | 0:09bf5c3e5302 | 111 | led2 = !led2; |
Scout | 0:09bf5c3e5302 | 112 | //Call the call method on the RPC class |
Scout | 0:09bf5c3e5302 | 113 | RPC::call(buf, outbuf); |
Scout | 0:09bf5c3e5302 | 114 | pc.printf("%s\n", outbuf); |
Scout | 0:09bf5c3e5302 | 115 | } |
Scout | 0:09bf5c3e5302 | 116 | led1= !led1; |
Scout | 0:09bf5c3e5302 | 117 | wait(0.2); //wait 200 ms |
Scout | 0:09bf5c3e5302 | 118 | } |
Scout | 0:09bf5c3e5302 | 119 | } |