Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of IEEE_14_Freescale by
main.cpp@23:53cafcd67828, 2014-03-13 (annotated)
- Committer:
- sswatek
- Date:
- Thu Mar 13 04:30:24 2014 +0000
- Revision:
- 23:53cafcd67828
- Parent:
- 18:c834bd26869e
- Child:
- 24:46eeab7cebc6
Reliably send messages to the phone
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soonerbot | 0:3a3dd78038a6 | 1 | #include "mbed.h" |
soonerbot | 1:c28fac16a109 | 2 | #include "dbgprint.h" |
soonerbot | 1:c28fac16a109 | 3 | #include "robot.h" |
soonerbot | 14:a30aa3b29a2e | 4 | #include "hcsr04.h" |
soonerbot | 0:3a3dd78038a6 | 5 | |
soonerbot | 1:c28fac16a109 | 6 | BusOut leds(LED_RED,LED_GREEN,LED_BLUE); |
soonerbot | 1:c28fac16a109 | 7 | Serial pc(USBTX, USBRX); |
soonerbot | 0:3a3dd78038a6 | 8 | |
soonerbot | 1:c28fac16a109 | 9 | robot bot; |
soonerbot | 15:b10859606504 | 10 | servo s(PTC9); |
soonerbot | 14:a30aa3b29a2e | 11 | |
soonerbot | 14:a30aa3b29a2e | 12 | /* |
soonerbot | 14:a30aa3b29a2e | 13 | //actual competition code, kinda |
soonerbot | 14:a30aa3b29a2e | 14 | int firstSection(){ // drive forward and measure distance |
soonerbot | 14:a30aa3b29a2e | 15 | tmpOut = bot.absDriveForward(targetAngle,3000); |
soonerbot | 14:a30aa3b29a2e | 16 | // ping sensor |
soonerbot | 14:a30aa3b29a2e | 17 | tmpOut = bot.absDriveForward(targetAngle,3000); |
soonerbot | 14:a30aa3b29a2e | 18 | // ping sensor |
soonerbot | 14:a30aa3b29a2e | 19 | } |
soonerbot | 14:a30aa3b29a2e | 20 | int secondsection(){ |
soonerbot | 14:a30aa3b29a2e | 21 | // second step |
soonerbot | 14:a30aa3b29a2e | 22 | // etc |
soonerbot | 14:a30aa3b29a2e | 23 | } |
soonerbot | 14:a30aa3b29a2e | 24 | */ |
soonerbot | 14:a30aa3b29a2e | 25 | |
soonerbot | 0:3a3dd78038a6 | 26 | int main() { |
soonerbot | 14:a30aa3b29a2e | 27 | // Initialize variables |
soonerbot | 14:a30aa3b29a2e | 28 | leds = 0x2; |
soonerbot | 1:c28fac16a109 | 29 | char tmpchar = 0; |
soonerbot | 14:a30aa3b29a2e | 30 | const int* constbuf; // displays values |
soonerbot | 10:926f142f16a3 | 31 | int tmpOut; |
soonerbot | 4:adc885f4ab75 | 32 | double targetAngle=0.0; |
soonerbot | 14:a30aa3b29a2e | 33 | DBGPRINT("AA\n\r",1); // to see if robot powers up at least |
soonerbot | 14:a30aa3b29a2e | 34 | |
soonerbot | 15:b10859606504 | 35 | //test servo |
soonerbot | 15:b10859606504 | 36 | s.toPosition(45); |
soonerbot | 15:b10859606504 | 37 | |
soonerbot | 15:b10859606504 | 38 | |
soonerbot | 15:b10859606504 | 39 | //timer issue navigation test |
soonerbot | 15:b10859606504 | 40 | bot.absDriveForward(0,50);//assuming the bot stops in this function |
soonerbot | 15:b10859606504 | 41 | /* |
soonerbot | 15:b10859606504 | 42 | DBGPRINT("%f",bot.gyro.getZDegrees()); |
soonerbot | 15:b10859606504 | 43 | bot.gyro.stop();//stopping the gyro timer |
soonerbot | 15:b10859606504 | 44 | //bot.gyro.gyroUpkeepTicker.detach();//stopping the gyro timer |
soonerbot | 15:b10859606504 | 45 | //use sensor to find distance |
soonerbot | 15:b10859606504 | 46 | bot.pingLeft.trigger(); |
soonerbot | 15:b10859606504 | 47 | wait(0.1); |
soonerbot | 15:b10859606504 | 48 | float pingresult = bot.pingLeft.inches(); |
soonerbot | 15:b10859606504 | 49 | DBGPRINT("Distance = %f\r\n",pingresult); |
soonerbot | 15:b10859606504 | 50 | bot.gyro.start();//stopping the gyro timer |
soonerbot | 15:b10859606504 | 51 | DBGPRINT("%f",bot.gyro.getZDegrees()); |
soonerbot | 15:b10859606504 | 52 | */ |
sswatek | 23:53cafcd67828 | 53 | Timer dataTimer; |
sswatek | 23:53cafcd67828 | 54 | int testdata[]={'H','e','l','l','o'}; |
sswatek | 23:53cafcd67828 | 55 | int response; |
sswatek | 23:53cafcd67828 | 56 | int gotAck=0; |
soonerbot | 14:a30aa3b29a2e | 57 | // Loop |
soonerbot | 0:3a3dd78038a6 | 58 | while(1) { |
soonerbot | 1:c28fac16a109 | 59 | DBGPRINT("BB\n\r",1); |
soonerbot | 7:3b2cf7efe5d1 | 60 | leds = leds^0x7; // toggle the LEDs for each loop |
soonerbot | 1:c28fac16a109 | 61 | tmpchar = pc.getc(); |
sswatek | 23:53cafcd67828 | 62 | bot.BTLink.procBuf(0x02); |
soonerbot | 7:3b2cf7efe5d1 | 63 | // all of these movement commands are blocking, so they can't be easily stopped short of resetting the microcontroller |
soonerbot | 1:c28fac16a109 | 64 | switch(tmpchar){ |
soonerbot | 15:b10859606504 | 65 | /*case 'e': //drive in a smallish square |
soonerbot | 7:3b2cf7efe5d1 | 66 | bot.driveForward(0,3000); |
soonerbot | 7:3b2cf7efe5d1 | 67 | bot.driveForward(-90,0); |
soonerbot | 7:3b2cf7efe5d1 | 68 | bot.driveForward(-90,3000); |
soonerbot | 7:3b2cf7efe5d1 | 69 | bot.driveForward(-180,0); |
soonerbot | 7:3b2cf7efe5d1 | 70 | bot.driveForward(-180,3000); |
soonerbot | 7:3b2cf7efe5d1 | 71 | bot.driveForward(-270,0); |
soonerbot | 7:3b2cf7efe5d1 | 72 | bot.driveForward(-270,3000); |
soonerbot | 7:3b2cf7efe5d1 | 73 | bot.driveForward(0,0); |
soonerbot | 7:3b2cf7efe5d1 | 74 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 15:b10859606504 | 75 | break;*/ |
soonerbot | 7:3b2cf7efe5d1 | 76 | case 'q': //poll the encoders |
soonerbot | 1:c28fac16a109 | 77 | constbuf = bot.bigenc.getVals(); |
soonerbot | 1:c28fac16a109 | 78 | DBGPRINT("\n\r%d\t%d\t%d\t%d\n\r",constbuf[0],constbuf[1],constbuf[2],constbuf[3]); |
soonerbot | 1:c28fac16a109 | 79 | break; |
soonerbot | 7:3b2cf7efe5d1 | 80 | case 'z': //set the current direction to "forward" for the following "go forward/reverse" commands |
soonerbot | 4:adc885f4ab75 | 81 | targetAngle = bot.gyro.getZDegrees(); |
soonerbot | 4:adc885f4ab75 | 82 | break; |
soonerbot | 7:3b2cf7efe5d1 | 83 | case 'w': // turn 90 degrees counter clockwise from the starting rotation |
soonerbot | 15:b10859606504 | 84 | |
soonerbot | 15:b10859606504 | 85 | s.toPosition(0); |
soonerbot | 15:b10859606504 | 86 | //bot.driveForward(90,0); |
soonerbot | 7:3b2cf7efe5d1 | 87 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 7:3b2cf7efe5d1 | 88 | break; |
soonerbot | 7:3b2cf7efe5d1 | 89 | case 'x': // turn 90 degrees clockwise from the starting rotation |
soonerbot | 15:b10859606504 | 90 | s.toPosition(180); |
soonerbot | 15:b10859606504 | 91 | //bot.driveForward(-90,0); |
soonerbot | 6:62d498ee97cf | 92 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 6:62d498ee97cf | 93 | break; |
soonerbot | 7:3b2cf7efe5d1 | 94 | case 'a': // turn the opposite direction from the starting one |
soonerbot | 15:b10859606504 | 95 | s.toPosition(90); |
soonerbot | 15:b10859606504 | 96 | //bot.driveForward(180,0); |
soonerbot | 6:62d498ee97cf | 97 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 1:c28fac16a109 | 98 | break; |
soonerbot | 7:3b2cf7efe5d1 | 99 | case 'd': // turn back to starting rotation |
soonerbot | 15:b10859606504 | 100 | //bot.driveForward(0,0); |
sswatek | 18:c834bd26869e | 101 | response=bot.BTLink.sendCmd(0x02,testdata,5); |
sswatek | 17:e247d58d9f42 | 102 | DBGPRINT("=%d\n\r",response); |
soonerbot | 7:3b2cf7efe5d1 | 103 | break; |
soonerbot | 7:3b2cf7efe5d1 | 104 | case 'W': // drive forward a small bit |
soonerbot | 9:aff48e331147 | 105 | //bot.driveForward(targetAngle,1000); |
soonerbot | 10:926f142f16a3 | 106 | //bot.motors.moveForward(100); |
soonerbot | 11:967469d7e01c | 107 | tmpOut = bot.absDriveForward(targetAngle,3000); |
soonerbot | 10:926f142f16a3 | 108 | DBGPRINT("W -> %d\n\r",tmpOut); |
soonerbot | 12:925f52da3ba9 | 109 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 4:adc885f4ab75 | 110 | break; |
soonerbot | 7:3b2cf7efe5d1 | 111 | case 'E': //drive forward five times as much |
soonerbot | 15:b10859606504 | 112 | //bot.driveForward(targetAngle,5000); |
soonerbot | 15:b10859606504 | 113 | bot.absDriveForward(0,5000); |
soonerbot | 4:adc885f4ab75 | 114 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 4:adc885f4ab75 | 115 | break; |
soonerbot | 7:3b2cf7efe5d1 | 116 | case 'X': // small reverse |
soonerbot | 10:926f142f16a3 | 117 | //bot.driveForward(targetAngle,-1000); |
soonerbot | 10:926f142f16a3 | 118 | |
soonerbot | 11:967469d7e01c | 119 | tmpOut = bot.absDriveForward(targetAngle,-3000); |
soonerbot | 10:926f142f16a3 | 120 | DBGPRINT("X -> %d\n\r",tmpOut); |
soonerbot | 12:925f52da3ba9 | 121 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 4:adc885f4ab75 | 122 | break; |
soonerbot | 7:3b2cf7efe5d1 | 123 | case 'C': // big reverse |
soonerbot | 15:b10859606504 | 124 | //bot.driveForward(targetAngle,-5000); |
soonerbot | 15:b10859606504 | 125 | bot.absDriveForward(0,-5000); |
soonerbot | 4:adc885f4ab75 | 126 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 4:adc885f4ab75 | 127 | break; |
soonerbot | 7:3b2cf7efe5d1 | 128 | case 'p': // poll the gyro |
soonerbot | 3:a223b0bf8256 | 129 | DBGPRINT("%d\r\n",bot.gyro.getZ()); |
soonerbot | 3:a223b0bf8256 | 130 | break; |
soonerbot | 7:3b2cf7efe5d1 | 131 | case 'l': //calibrate the gyro ( caution, only use after resetting and before moving, otherwise will break any calibration) |
soonerbot | 3:a223b0bf8256 | 132 | bot.gyro.calibrate(); |
soonerbot | 3:a223b0bf8256 | 133 | break; |
soonerbot | 7:3b2cf7efe5d1 | 134 | |
soonerbot | 7:3b2cf7efe5d1 | 135 | // these are all control system modifications which should be fairly well locked down at this point |
soonerbot | 4:adc885f4ab75 | 136 | case 't': |
sswatek | 18:c834bd26869e | 137 | response=bot.BTLink.sendCmd(0x00,testdata,5); |
sswatek | 23:53cafcd67828 | 138 | //bot.gyro.stop(); |
sswatek | 23:53cafcd67828 | 139 | dataTimer.start(); |
sswatek | 23:53cafcd67828 | 140 | dataTimer.reset(); |
sswatek | 23:53cafcd67828 | 141 | while(!(gotAck=bot.BTLink.getAck(response)) && dataTimer.read_ms()<500); |
sswatek | 23:53cafcd67828 | 142 | dataTimer.stop(); |
sswatek | 23:53cafcd67828 | 143 | //bot.gyro.start(); |
sswatek | 18:c834bd26869e | 144 | //bot.pfac*=2; |
sswatek | 18:c834bd26869e | 145 | //DBGPRINT("pfac = %f\r\n",bot.pfac); |
sswatek | 23:53cafcd67828 | 146 | DBGPRINT("Mode0=%d, %d, %d [%d]\n\r",response,bot.BTLink.bufSize(), gotAck, dataTimer.read_ms()); |
soonerbot | 4:adc885f4ab75 | 147 | break; |
soonerbot | 4:adc885f4ab75 | 148 | case 'g': |
soonerbot | 4:adc885f4ab75 | 149 | bot.pfac/=2; |
soonerbot | 4:adc885f4ab75 | 150 | DBGPRINT("pfac = %f\r\n",bot.pfac); |
soonerbot | 4:adc885f4ab75 | 151 | break; |
soonerbot | 4:adc885f4ab75 | 152 | case 'y': |
sswatek | 18:c834bd26869e | 153 | response=bot.BTLink.sendCmd(0x01,testdata,5); |
sswatek | 23:53cafcd67828 | 154 | dataTimer.start(); |
sswatek | 23:53cafcd67828 | 155 | dataTimer.reset(); |
sswatek | 23:53cafcd67828 | 156 | while(!(gotAck=bot.BTLink.getAck(response)) && dataTimer.read_ms()<500); |
sswatek | 23:53cafcd67828 | 157 | dataTimer.stop(); |
sswatek | 23:53cafcd67828 | 158 | //while(!bot.BTLink.getAck(response)); |
sswatek | 18:c834bd26869e | 159 | //bot.ifac*=2; |
sswatek | 18:c834bd26869e | 160 | //DBGPRINT("ifac = %f\r\n",bot.ifac); |
sswatek | 23:53cafcd67828 | 161 | DBGPRINT("Mode1=%d, %d, %d [%d]\n\r",response,bot.BTLink.bufSize(), gotAck, dataTimer.read_ms()); |
soonerbot | 4:adc885f4ab75 | 162 | break; |
soonerbot | 4:adc885f4ab75 | 163 | case 'h': |
soonerbot | 4:adc885f4ab75 | 164 | bot.ifac/=2; |
soonerbot | 4:adc885f4ab75 | 165 | DBGPRINT("ifac = %f\r\n",bot.ifac); |
soonerbot | 4:adc885f4ab75 | 166 | break; |
soonerbot | 4:adc885f4ab75 | 167 | case 'u': |
sswatek | 18:c834bd26869e | 168 | response=bot.BTLink.sendCmd(0x02,testdata,5); |
sswatek | 23:53cafcd67828 | 169 | dataTimer.start(); |
sswatek | 23:53cafcd67828 | 170 | dataTimer.reset(); |
sswatek | 23:53cafcd67828 | 171 | while(!(gotAck=bot.BTLink.getAck(response)) && dataTimer.read_ms()<500); |
sswatek | 23:53cafcd67828 | 172 | dataTimer.stop(); |
sswatek | 23:53cafcd67828 | 173 | //while(!bot.BTLink.getAck(response)); |
sswatek | 18:c834bd26869e | 174 | //bot.dfac*=2; |
sswatek | 18:c834bd26869e | 175 | //DBGPRINT("dfac = %f\r\n",bot.dfac); |
sswatek | 23:53cafcd67828 | 176 | DBGPRINT("Mode2=%d, %d, %d [%d]\n\r",response,bot.BTLink.bufSize(), gotAck, dataTimer.read_ms()); |
soonerbot | 4:adc885f4ab75 | 177 | break; |
soonerbot | 4:adc885f4ab75 | 178 | case 'j': |
soonerbot | 4:adc885f4ab75 | 179 | bot.dfac/=2; |
soonerbot | 4:adc885f4ab75 | 180 | DBGPRINT("dfac = %f\r\n",bot.dfac); |
soonerbot | 4:adc885f4ab75 | 181 | break; |
soonerbot | 4:adc885f4ab75 | 182 | case 'i': |
soonerbot | 14:a30aa3b29a2e | 183 | //put distance ping here |
soonerbot | 14:a30aa3b29a2e | 184 | bot.pingLeft.trigger(); |
soonerbot | 15:b10859606504 | 185 | wait(0.1); |
soonerbot | 14:a30aa3b29a2e | 186 | float pingresult = bot.pingLeft.inches(); |
soonerbot | 14:a30aa3b29a2e | 187 | DBGPRINT("Distance = %f\r\n",pingresult); |
soonerbot | 4:adc885f4ab75 | 188 | break; |
soonerbot | 4:adc885f4ab75 | 189 | case 'k': |
soonerbot | 4:adc885f4ab75 | 190 | bot.angfac/=2; |
soonerbot | 4:adc885f4ab75 | 191 | DBGPRINT("angfac = %f\r\n",bot.angfac); |
soonerbot | 4:adc885f4ab75 | 192 | break; |
soonerbot | 4:adc885f4ab75 | 193 | case 'T': |
soonerbot | 4:adc885f4ab75 | 194 | bot.pfac*=1.05; |
soonerbot | 4:adc885f4ab75 | 195 | DBGPRINT("pfac = %f\r\n",bot.pfac); |
soonerbot | 4:adc885f4ab75 | 196 | break; |
soonerbot | 4:adc885f4ab75 | 197 | case 'G': |
soonerbot | 4:adc885f4ab75 | 198 | bot.pfac/=1.05; |
soonerbot | 4:adc885f4ab75 | 199 | DBGPRINT("pfac = %f\r\n",bot.pfac); |
soonerbot | 4:adc885f4ab75 | 200 | break; |
soonerbot | 4:adc885f4ab75 | 201 | case 'Y': |
soonerbot | 4:adc885f4ab75 | 202 | bot.ifac*=1.05; |
soonerbot | 4:adc885f4ab75 | 203 | DBGPRINT("ifac = %f\r\n",bot.ifac); |
soonerbot | 4:adc885f4ab75 | 204 | break; |
soonerbot | 4:adc885f4ab75 | 205 | case 'H': |
soonerbot | 4:adc885f4ab75 | 206 | bot.ifac/=1.05; |
soonerbot | 4:adc885f4ab75 | 207 | DBGPRINT("ifac = %f\r\n",bot.ifac); |
soonerbot | 4:adc885f4ab75 | 208 | break; |
soonerbot | 4:adc885f4ab75 | 209 | case 'U': |
soonerbot | 4:adc885f4ab75 | 210 | bot.dfac*=1.05; |
soonerbot | 4:adc885f4ab75 | 211 | DBGPRINT("dfac = %f\r\n",bot.dfac); |
soonerbot | 4:adc885f4ab75 | 212 | break; |
soonerbot | 4:adc885f4ab75 | 213 | case 'J': |
soonerbot | 4:adc885f4ab75 | 214 | bot.dfac/=1.05; |
soonerbot | 4:adc885f4ab75 | 215 | DBGPRINT("dfac = %f\r\n",bot.dfac); |
soonerbot | 4:adc885f4ab75 | 216 | break; |
soonerbot | 7:3b2cf7efe5d1 | 217 | |
soonerbot | 7:3b2cf7efe5d1 | 218 | // poll the compass ( currently gives bad values ) |
soonerbot | 4:adc885f4ab75 | 219 | case 'Q': |
soonerbot | 4:adc885f4ab75 | 220 | DBGPRINT("Compass vector = %d\t%d\t%d\r\n",bot.gyro.xmag,bot.gyro.ymag,bot.gyro.zmag); |
soonerbot | 4:adc885f4ab75 | 221 | break; |
soonerbot | 7:3b2cf7efe5d1 | 222 | |
soonerbot | 7:3b2cf7efe5d1 | 223 | // brake if we get an unknown command |
soonerbot | 1:c28fac16a109 | 224 | default: |
soonerbot | 15:b10859606504 | 225 | //bot.left.brake(); |
soonerbot | 15:b10859606504 | 226 | //bot.right.brake(); |
soonerbot | 1:c28fac16a109 | 227 | break; |
soonerbot | 1:c28fac16a109 | 228 | |
soonerbot | 1:c28fac16a109 | 229 | } |
soonerbot | 0:3a3dd78038a6 | 230 | } |
soonerbot | 0:3a3dd78038a6 | 231 | } |