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@7:3b2cf7efe5d1, 2013-11-22 (annotated)
- Committer:
- soonerbot
- Date:
- Fri Nov 22 18:00:18 2013 +0000
- Revision:
- 7:3b2cf7efe5d1
- Parent:
- 6:62d498ee97cf
- Child:
- 9:aff48e331147
Commented most things
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 | 0:3a3dd78038a6 | 4 | |
soonerbot | 1:c28fac16a109 | 5 | BusOut leds(LED_RED,LED_GREEN,LED_BLUE); |
soonerbot | 1:c28fac16a109 | 6 | Serial pc(USBTX, USBRX); |
soonerbot | 0:3a3dd78038a6 | 7 | |
soonerbot | 1:c28fac16a109 | 8 | robot bot; |
soonerbot | 0:3a3dd78038a6 | 9 | int main() { |
soonerbot | 1:c28fac16a109 | 10 | DBGPRINT("AA\n\r",1); |
soonerbot | 1:c28fac16a109 | 11 | char tmpchar = 0; |
soonerbot | 1:c28fac16a109 | 12 | leds = 0x2; |
soonerbot | 1:c28fac16a109 | 13 | const int* constbuf; |
soonerbot | 4:adc885f4ab75 | 14 | double targetAngle=0.0; |
soonerbot | 0:3a3dd78038a6 | 15 | while(1) { |
soonerbot | 1:c28fac16a109 | 16 | DBGPRINT("BB\n\r",1); |
soonerbot | 7:3b2cf7efe5d1 | 17 | leds = leds^0x7; // toggle the LEDs for each loop |
soonerbot | 1:c28fac16a109 | 18 | tmpchar = pc.getc(); |
soonerbot | 1:c28fac16a109 | 19 | |
soonerbot | 7:3b2cf7efe5d1 | 20 | // all of these movement commands are blocking, so they can't be easily stopped short of resetting the microcontroller |
soonerbot | 1:c28fac16a109 | 21 | switch(tmpchar){ |
soonerbot | 7:3b2cf7efe5d1 | 22 | case 'e': //drive in a smallish square |
soonerbot | 7:3b2cf7efe5d1 | 23 | bot.driveForward(0,3000); |
soonerbot | 7:3b2cf7efe5d1 | 24 | bot.driveForward(-90,0); |
soonerbot | 7:3b2cf7efe5d1 | 25 | bot.driveForward(-90,3000); |
soonerbot | 7:3b2cf7efe5d1 | 26 | bot.driveForward(-180,0); |
soonerbot | 7:3b2cf7efe5d1 | 27 | bot.driveForward(-180,3000); |
soonerbot | 7:3b2cf7efe5d1 | 28 | bot.driveForward(-270,0); |
soonerbot | 7:3b2cf7efe5d1 | 29 | bot.driveForward(-270,3000); |
soonerbot | 7:3b2cf7efe5d1 | 30 | bot.driveForward(0,0); |
soonerbot | 7:3b2cf7efe5d1 | 31 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 7:3b2cf7efe5d1 | 32 | break; |
soonerbot | 7:3b2cf7efe5d1 | 33 | case 'q': //poll the encoders |
soonerbot | 1:c28fac16a109 | 34 | constbuf = bot.bigenc.getVals(); |
soonerbot | 1:c28fac16a109 | 35 | DBGPRINT("\n\r%d\t%d\t%d\t%d\n\r",constbuf[0],constbuf[1],constbuf[2],constbuf[3]); |
soonerbot | 1:c28fac16a109 | 36 | break; |
soonerbot | 7:3b2cf7efe5d1 | 37 | case 'z': //set the current direction to "forward" for the following "go forward/reverse" commands |
soonerbot | 4:adc885f4ab75 | 38 | targetAngle = bot.gyro.getZDegrees(); |
soonerbot | 4:adc885f4ab75 | 39 | break; |
soonerbot | 7:3b2cf7efe5d1 | 40 | case 'w': // turn 90 degrees counter clockwise from the starting rotation |
soonerbot | 7:3b2cf7efe5d1 | 41 | bot.driveForward(90,0); |
soonerbot | 7:3b2cf7efe5d1 | 42 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 7:3b2cf7efe5d1 | 43 | break; |
soonerbot | 7:3b2cf7efe5d1 | 44 | case 'x': // turn 90 degrees clockwise from the starting rotation |
soonerbot | 6:62d498ee97cf | 45 | bot.driveForward(-90,0); |
soonerbot | 6:62d498ee97cf | 46 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 6:62d498ee97cf | 47 | break; |
soonerbot | 7:3b2cf7efe5d1 | 48 | case 'a': // turn the opposite direction from the starting one |
soonerbot | 7:3b2cf7efe5d1 | 49 | bot.driveForward(180,0); |
soonerbot | 6:62d498ee97cf | 50 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 1:c28fac16a109 | 51 | break; |
soonerbot | 7:3b2cf7efe5d1 | 52 | case 'd': // turn back to starting rotation |
soonerbot | 7:3b2cf7efe5d1 | 53 | bot.driveForward(0,0); |
soonerbot | 7:3b2cf7efe5d1 | 54 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 7:3b2cf7efe5d1 | 55 | break; |
soonerbot | 7:3b2cf7efe5d1 | 56 | case 'W': // drive forward a small bit |
soonerbot | 4:adc885f4ab75 | 57 | bot.driveForward(targetAngle,1000); |
soonerbot | 4:adc885f4ab75 | 58 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 4:adc885f4ab75 | 59 | break; |
soonerbot | 7:3b2cf7efe5d1 | 60 | case 'E': //drive forward five times as much |
soonerbot | 4:adc885f4ab75 | 61 | bot.driveForward(targetAngle,5000); |
soonerbot | 4:adc885f4ab75 | 62 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 4:adc885f4ab75 | 63 | break; |
soonerbot | 7:3b2cf7efe5d1 | 64 | case 'X': // small reverse |
soonerbot | 4:adc885f4ab75 | 65 | bot.driveForward(targetAngle,-1000); |
soonerbot | 4:adc885f4ab75 | 66 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 4:adc885f4ab75 | 67 | break; |
soonerbot | 7:3b2cf7efe5d1 | 68 | case 'C': // big reverse |
soonerbot | 4:adc885f4ab75 | 69 | bot.driveForward(targetAngle,-5000); |
soonerbot | 4:adc885f4ab75 | 70 | DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159); |
soonerbot | 4:adc885f4ab75 | 71 | break; |
soonerbot | 7:3b2cf7efe5d1 | 72 | case 'p': // poll the gyro |
soonerbot | 3:a223b0bf8256 | 73 | DBGPRINT("%d\r\n",bot.gyro.getZ()); |
soonerbot | 3:a223b0bf8256 | 74 | break; |
soonerbot | 7:3b2cf7efe5d1 | 75 | case 'l': //calibrate the gyro ( caution, only use after resetting and before moving, otherwise will break any calibration) |
soonerbot | 3:a223b0bf8256 | 76 | bot.gyro.calibrate(); |
soonerbot | 3:a223b0bf8256 | 77 | break; |
soonerbot | 7:3b2cf7efe5d1 | 78 | |
soonerbot | 7:3b2cf7efe5d1 | 79 | // these are all control system modifications which should be fairly well locked down at this point |
soonerbot | 4:adc885f4ab75 | 80 | case 't': |
soonerbot | 4:adc885f4ab75 | 81 | bot.pfac*=2; |
soonerbot | 4:adc885f4ab75 | 82 | DBGPRINT("pfac = %f\r\n",bot.pfac); |
soonerbot | 4:adc885f4ab75 | 83 | break; |
soonerbot | 4:adc885f4ab75 | 84 | case 'g': |
soonerbot | 4:adc885f4ab75 | 85 | bot.pfac/=2; |
soonerbot | 4:adc885f4ab75 | 86 | DBGPRINT("pfac = %f\r\n",bot.pfac); |
soonerbot | 4:adc885f4ab75 | 87 | break; |
soonerbot | 4:adc885f4ab75 | 88 | case 'y': |
soonerbot | 4:adc885f4ab75 | 89 | bot.ifac*=2; |
soonerbot | 4:adc885f4ab75 | 90 | DBGPRINT("ifac = %f\r\n",bot.ifac); |
soonerbot | 4:adc885f4ab75 | 91 | break; |
soonerbot | 4:adc885f4ab75 | 92 | case 'h': |
soonerbot | 4:adc885f4ab75 | 93 | bot.ifac/=2; |
soonerbot | 4:adc885f4ab75 | 94 | DBGPRINT("ifac = %f\r\n",bot.ifac); |
soonerbot | 4:adc885f4ab75 | 95 | break; |
soonerbot | 4:adc885f4ab75 | 96 | case 'u': |
soonerbot | 4:adc885f4ab75 | 97 | bot.dfac*=2; |
soonerbot | 4:adc885f4ab75 | 98 | DBGPRINT("dfac = %f\r\n",bot.dfac); |
soonerbot | 4:adc885f4ab75 | 99 | break; |
soonerbot | 4:adc885f4ab75 | 100 | case 'j': |
soonerbot | 4:adc885f4ab75 | 101 | bot.dfac/=2; |
soonerbot | 4:adc885f4ab75 | 102 | DBGPRINT("dfac = %f\r\n",bot.dfac); |
soonerbot | 4:adc885f4ab75 | 103 | break; |
soonerbot | 4:adc885f4ab75 | 104 | case 'i': |
soonerbot | 4:adc885f4ab75 | 105 | bot.angfac*=2; |
soonerbot | 4:adc885f4ab75 | 106 | DBGPRINT("angfac = %f\r\n",bot.angfac); |
soonerbot | 4:adc885f4ab75 | 107 | break; |
soonerbot | 4:adc885f4ab75 | 108 | case 'k': |
soonerbot | 4:adc885f4ab75 | 109 | bot.angfac/=2; |
soonerbot | 4:adc885f4ab75 | 110 | DBGPRINT("angfac = %f\r\n",bot.angfac); |
soonerbot | 4:adc885f4ab75 | 111 | break; |
soonerbot | 4:adc885f4ab75 | 112 | case 'T': |
soonerbot | 4:adc885f4ab75 | 113 | bot.pfac*=1.05; |
soonerbot | 4:adc885f4ab75 | 114 | DBGPRINT("pfac = %f\r\n",bot.pfac); |
soonerbot | 4:adc885f4ab75 | 115 | break; |
soonerbot | 4:adc885f4ab75 | 116 | case 'G': |
soonerbot | 4:adc885f4ab75 | 117 | bot.pfac/=1.05; |
soonerbot | 4:adc885f4ab75 | 118 | DBGPRINT("pfac = %f\r\n",bot.pfac); |
soonerbot | 4:adc885f4ab75 | 119 | break; |
soonerbot | 4:adc885f4ab75 | 120 | case 'Y': |
soonerbot | 4:adc885f4ab75 | 121 | bot.ifac*=1.05; |
soonerbot | 4:adc885f4ab75 | 122 | DBGPRINT("ifac = %f\r\n",bot.ifac); |
soonerbot | 4:adc885f4ab75 | 123 | break; |
soonerbot | 4:adc885f4ab75 | 124 | case 'H': |
soonerbot | 4:adc885f4ab75 | 125 | bot.ifac/=1.05; |
soonerbot | 4:adc885f4ab75 | 126 | DBGPRINT("ifac = %f\r\n",bot.ifac); |
soonerbot | 4:adc885f4ab75 | 127 | break; |
soonerbot | 4:adc885f4ab75 | 128 | case 'U': |
soonerbot | 4:adc885f4ab75 | 129 | bot.dfac*=1.05; |
soonerbot | 4:adc885f4ab75 | 130 | DBGPRINT("dfac = %f\r\n",bot.dfac); |
soonerbot | 4:adc885f4ab75 | 131 | break; |
soonerbot | 4:adc885f4ab75 | 132 | case 'J': |
soonerbot | 4:adc885f4ab75 | 133 | bot.dfac/=1.05; |
soonerbot | 4:adc885f4ab75 | 134 | DBGPRINT("dfac = %f\r\n",bot.dfac); |
soonerbot | 4:adc885f4ab75 | 135 | break; |
soonerbot | 7:3b2cf7efe5d1 | 136 | |
soonerbot | 7:3b2cf7efe5d1 | 137 | // poll the compass ( currently gives bad values ) |
soonerbot | 4:adc885f4ab75 | 138 | case 'Q': |
soonerbot | 4:adc885f4ab75 | 139 | DBGPRINT("Compass vector = %d\t%d\t%d\r\n",bot.gyro.xmag,bot.gyro.ymag,bot.gyro.zmag); |
soonerbot | 4:adc885f4ab75 | 140 | break; |
soonerbot | 7:3b2cf7efe5d1 | 141 | |
soonerbot | 7:3b2cf7efe5d1 | 142 | // brake if we get an unknown command |
soonerbot | 1:c28fac16a109 | 143 | default: |
soonerbot | 1:c28fac16a109 | 144 | bot.left.brake(); |
soonerbot | 1:c28fac16a109 | 145 | bot.right.brake(); |
soonerbot | 1:c28fac16a109 | 146 | break; |
soonerbot | 1:c28fac16a109 | 147 | |
soonerbot | 1:c28fac16a109 | 148 | } |
soonerbot | 0:3a3dd78038a6 | 149 | } |
soonerbot | 0:3a3dd78038a6 | 150 | } |