Tapton Dog
/
mbed1movement_MPtest5
mbed1 Tapton school eds robot project
main.cpp@0:0c9486499a97, 2017-03-14 (annotated)
- Committer:
- matt8174
- Date:
- Tue Mar 14 13:42:23 2017 +0000
- Revision:
- 0:0c9486499a97
mbed1 Tapton school eds robot project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
matt8174 | 0:0c9486499a97 | 1 | // Mbed 1 Robot, robot base sensors and bluetooth |
matt8174 | 0:0c9486499a97 | 2 | // serial port pins 9 and 10(tx rx) used to communicate with robot base |
matt8174 | 0:0c9486499a97 | 3 | #include "mbed.h" |
matt8174 | 0:0c9486499a97 | 4 | #include "m3pi.h" |
matt8174 | 0:0c9486499a97 | 5 | #include "string" |
matt8174 | 0:0c9486499a97 | 6 | #include <stdio.h> |
matt8174 | 0:0c9486499a97 | 7 | #include <stdlib.h> |
matt8174 | 0:0c9486499a97 | 8 | #include <iostream> |
matt8174 | 0:0c9486499a97 | 9 | //Serial pc(USBTX, USBRX); // for terminal diagnostics |
matt8174 | 0:0c9486499a97 | 10 | Serial device(p28, p27); // Bluetooth comms tx rx |
matt8174 | 0:0c9486499a97 | 11 | Serial communicate(p13, p14); // to Mbed 2 tx rx |
matt8174 | 0:0c9486499a97 | 12 | m3pi m3pi; |
matt8174 | 0:0c9486499a97 | 13 | DigitalOut myled1(LED1); |
matt8174 | 0:0c9486499a97 | 14 | DigitalOut myled2(LED2); |
matt8174 | 0:0c9486499a97 | 15 | DigitalOut myled3(LED3); |
matt8174 | 0:0c9486499a97 | 16 | DigitalOut myled4(LED4); |
matt8174 | 0:0c9486499a97 | 17 | DigitalIn stopgo (p20); // from mbed2 sensor move forward or not something blocking (1=block) |
matt8174 | 0:0c9486499a97 | 18 | |
matt8174 | 0:0c9486499a97 | 19 | char letters[26] = { |
matt8174 | 0:0c9486499a97 | 20 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', |
matt8174 | 0:0c9486499a97 | 21 | 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', |
matt8174 | 0:0c9486499a97 | 22 | 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' |
matt8174 | 0:0c9486499a97 | 23 | }; |
matt8174 | 0:0c9486499a97 | 24 | char receivedchar[10]; |
matt8174 | 0:0c9486499a97 | 25 | |
matt8174 | 0:0c9486499a97 | 26 | |
matt8174 | 0:0c9486499a97 | 27 | void rxCallback() //interrupt from rx serial port |
matt8174 | 0:0c9486499a97 | 28 | { |
matt8174 | 0:0c9486499a97 | 29 | myled3=!myled3; |
matt8174 | 0:0c9486499a97 | 30 | receivedchar[0]=communicate.getc(); // put received character from mbed2 into array for later |
matt8174 | 0:0c9486499a97 | 31 | // pc.printf("%d ",receivedchar[0]); |
matt8174 | 0:0c9486499a97 | 32 | //m3pi.cls(); |
matt8174 | 0:0c9486499a97 | 33 | m3pi.locate(0,0); // x then y co-ordinates on screen |
matt8174 | 0:0c9486499a97 | 34 | m3pi.printf("%d ",receivedchar[0]); |
matt8174 | 0:0c9486499a97 | 35 | //wait (0.2); |
matt8174 | 0:0c9486499a97 | 36 | } |
matt8174 | 0:0c9486499a97 | 37 | |
matt8174 | 0:0c9486499a97 | 38 | void txCallback() //interrupt from tx serial port |
matt8174 | 0:0c9486499a97 | 39 | { |
matt8174 | 0:0c9486499a97 | 40 | myled4=!myled4; |
matt8174 | 0:0c9486499a97 | 41 | } |
matt8174 | 0:0c9486499a97 | 42 | |
matt8174 | 0:0c9486499a97 | 43 | int main() |
matt8174 | 0:0c9486499a97 | 44 | { |
matt8174 | 0:0c9486499a97 | 45 | int cmd_option; |
matt8174 | 0:0c9486499a97 | 46 | char cmd_convert; |
matt8174 | 0:0c9486499a97 | 47 | // Display welcome message to the scripting |
matt8174 | 0:0c9486499a97 | 48 | // display_part("Init : ", "FIDO-Ed"); |
matt8174 | 0:0c9486499a97 | 49 | |
matt8174 | 0:0c9486499a97 | 50 | // Configure device baud rates |
matt8174 | 0:0c9486499a97 | 51 | device.baud(115200); //Bluetooth |
matt8174 | 0:0c9486499a97 | 52 | // pc.baud(115200); //for diagnostics pc |
matt8174 | 0:0c9486499a97 | 53 | communicate.baud(19200); //to mbed 2 |
matt8174 | 0:0c9486499a97 | 54 | setbuf(stdin,NULL); //clear serial input buffer |
matt8174 | 0:0c9486499a97 | 55 | communicate.attach(&txCallback, Serial::TxIrq); |
matt8174 | 0:0c9486499a97 | 56 | communicate.attach(&rxCallback, Serial::RxIrq); //for serial interrupt callback function |
matt8174 | 0:0c9486499a97 | 57 | m3pi.cls(); |
matt8174 | 0:0c9486499a97 | 58 | cmd_convert=0; |
matt8174 | 0:0c9486499a97 | 59 | |
matt8174 | 0:0c9486499a97 | 60 | // Global program variables |
matt8174 | 0:0c9486499a97 | 61 | bool nerror = true; |
matt8174 | 0:0c9486499a97 | 62 | |
matt8174 | 0:0c9486499a97 | 63 | // Iterate while simulation is error free |
matt8174 | 0:0c9486499a97 | 64 | while(nerror) |
matt8174 | 0:0c9486499a97 | 65 | { |
matt8174 | 0:0c9486499a97 | 66 | myled1=stopgo; // mbed2 says stop or go - 1/on =something blocking movement |
matt8174 | 0:0c9486499a97 | 67 | // Check for readability of device |
matt8174 | 0:0c9486499a97 | 68 | if(device.readable()) |
matt8174 | 0:0c9486499a97 | 69 | { |
matt8174 | 0:0c9486499a97 | 70 | // Display device recieved transactions |
matt8174 | 0:0c9486499a97 | 71 | // Fetch command and cast to char value |
matt8174 | 0:0c9486499a97 | 72 | cmd_option = device.getc(); |
matt8174 | 0:0c9486499a97 | 73 | cmd_convert = static_cast<char>(cmd_option); |
matt8174 | 0:0c9486499a97 | 74 | m3pi.locate(0,1); |
matt8174 | 0:0c9486499a97 | 75 | m3pi.printf("B%c %d", cmd_convert,cmd_option); |
matt8174 | 0:0c9486499a97 | 76 | // Hold for half a second |
matt8174 | 0:0c9486499a97 | 77 | wait(0.2); |
matt8174 | 0:0c9486499a97 | 78 | } |
matt8174 | 0:0c9486499a97 | 79 | |
matt8174 | 0:0c9486499a97 | 80 | if (cmd_convert !=0) |
matt8174 | 0:0c9486499a97 | 81 | { |
matt8174 | 0:0c9486499a97 | 82 | if(cmd_convert >=49 && cmd_convert <=53) // numbers 1 to 5 from mobile app, Movement |
matt8174 | 0:0c9486499a97 | 83 | { |
matt8174 | 0:0c9486499a97 | 84 | // Determine action to be taken based on switching |
matt8174 | 0:0c9486499a97 | 85 | switch(cmd_convert) |
matt8174 | 0:0c9486499a97 | 86 | { |
matt8174 | 0:0c9486499a97 | 87 | |
matt8174 | 0:0c9486499a97 | 88 | case '1': //Move Forward |
matt8174 | 0:0c9486499a97 | 89 | if(!stopgo) //if nothing blocking way |
matt8174 | 0:0c9486499a97 | 90 | { |
matt8174 | 0:0c9486499a97 | 91 | // while (!stopgo) |
matt8174 | 0:0c9486499a97 | 92 | // { |
matt8174 | 0:0c9486499a97 | 93 | communicate.putc(49); //move forward - send 1 to mbed 2 |
matt8174 | 0:0c9486499a97 | 94 | cmd_convert=0; |
matt8174 | 0:0c9486499a97 | 95 | m3pi.forward(0.25); |
matt8174 | 0:0c9486499a97 | 96 | wait(1.1); |
matt8174 | 0:0c9486499a97 | 97 | m3pi.stop(); |
matt8174 | 0:0c9486499a97 | 98 | // } |
matt8174 | 0:0c9486499a97 | 99 | } |
matt8174 | 0:0c9486499a97 | 100 | else |
matt8174 | 0:0c9486499a97 | 101 | { |
matt8174 | 0:0c9486499a97 | 102 | communicate.putc(53); // dont move forward and send 5 to mbed 2 |
matt8174 | 0:0c9486499a97 | 103 | } |
matt8174 | 0:0c9486499a97 | 104 | m3pi.stop(); |
matt8174 | 0:0c9486499a97 | 105 | break; |
matt8174 | 0:0c9486499a97 | 106 | |
matt8174 | 0:0c9486499a97 | 107 | case '2': //Turn Right |
matt8174 | 0:0c9486499a97 | 108 | if(!stopgo) //if nothing blocking way |
matt8174 | 0:0c9486499a97 | 109 | { |
matt8174 | 0:0c9486499a97 | 110 | // while (!stopgo) |
matt8174 | 0:0c9486499a97 | 111 | // { |
matt8174 | 0:0c9486499a97 | 112 | communicate.putc(50);// turn right and send 2 to mbed2 |
matt8174 | 0:0c9486499a97 | 113 | cmd_convert=0; |
matt8174 | 0:0c9486499a97 | 114 | m3pi.right(0.25); |
matt8174 | 0:0c9486499a97 | 115 | wait(0.5); |
matt8174 | 0:0c9486499a97 | 116 | m3pi.stop(); |
matt8174 | 0:0c9486499a97 | 117 | // } |
matt8174 | 0:0c9486499a97 | 118 | } |
matt8174 | 0:0c9486499a97 | 119 | else |
matt8174 | 0:0c9486499a97 | 120 | { |
matt8174 | 0:0c9486499a97 | 121 | m3pi.stop(); |
matt8174 | 0:0c9486499a97 | 122 | communicate.putc(53); // dont move forward and send 5 to mbed 2 |
matt8174 | 0:0c9486499a97 | 123 | } |
matt8174 | 0:0c9486499a97 | 124 | break; |
matt8174 | 0:0c9486499a97 | 125 | |
matt8174 | 0:0c9486499a97 | 126 | case '3': //Backwards - no movement |
matt8174 | 0:0c9486499a97 | 127 | communicate.putc(51); // turn around and send 3 to mbed 2 |
matt8174 | 0:0c9486499a97 | 128 | cmd_convert=0; |
matt8174 | 0:0c9486499a97 | 129 | // m3pi.right(0.5); |
matt8174 | 0:0c9486499a97 | 130 | // wait(0.25); |
matt8174 | 0:0c9486499a97 | 131 | // m3pi.stop(); |
matt8174 | 0:0c9486499a97 | 132 | break; |
matt8174 | 0:0c9486499a97 | 133 | |
matt8174 | 0:0c9486499a97 | 134 | case '4': // Turn Left |
matt8174 | 0:0c9486499a97 | 135 | if(!stopgo) //if nothing blocking way |
matt8174 | 0:0c9486499a97 | 136 | { |
matt8174 | 0:0c9486499a97 | 137 | // while (!stopgo) |
matt8174 | 0:0c9486499a97 | 138 | // { |
matt8174 | 0:0c9486499a97 | 139 | communicate.putc(52); //turn left and send 4 to mbed2 |
matt8174 | 0:0c9486499a97 | 140 | cmd_convert=0; |
matt8174 | 0:0c9486499a97 | 141 | m3pi.left(0.25); |
matt8174 | 0:0c9486499a97 | 142 | wait(0.5); |
matt8174 | 0:0c9486499a97 | 143 | m3pi.stop(); |
matt8174 | 0:0c9486499a97 | 144 | // } |
matt8174 | 0:0c9486499a97 | 145 | } |
matt8174 | 0:0c9486499a97 | 146 | else |
matt8174 | 0:0c9486499a97 | 147 | { |
matt8174 | 0:0c9486499a97 | 148 | m3pi.stop(); |
matt8174 | 0:0c9486499a97 | 149 | communicate.putc(53); // dont move forward and send 5 to mbed 2 |
matt8174 | 0:0c9486499a97 | 150 | } |
matt8174 | 0:0c9486499a97 | 151 | break; |
matt8174 | 0:0c9486499a97 | 152 | |
matt8174 | 0:0c9486499a97 | 153 | |
matt8174 | 0:0c9486499a97 | 154 | } // switch |
matt8174 | 0:0c9486499a97 | 155 | } |
matt8174 | 0:0c9486499a97 | 156 | if(cmd_convert >=65 && cmd_convert <=122) // send any other valid command to mbed2 |
matt8174 | 0:0c9486499a97 | 157 | { |
matt8174 | 0:0c9486499a97 | 158 | communicate.putc(cmd_convert); //send cmd_convert to mbed 2 |
matt8174 | 0:0c9486499a97 | 159 | cmd_convert=0; |
matt8174 | 0:0c9486499a97 | 160 | } //cmd_convert >=65 |
matt8174 | 0:0c9486499a97 | 161 | } // if cmd_convert not 0 |
matt8174 | 0:0c9486499a97 | 162 | }} |