Sooner Competitive Robotics / Mbed 2 deprecated IEEE_14_Freescale

Dependencies:   mbed

Fork of IEEE_14_Freescale by IEEE 2014 Mbed

Committer:
soonerbot
Date:
Tue Mar 11 19:58:31 2014 +0000
Revision:
15:b10859606504
Parent:
14:a30aa3b29a2e
Child:
17:e247d58d9f42
Servo and ultrasonic progress

Who changed what in which revision?

UserRevisionLine numberNew 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 */
soonerbot 14:a30aa3b29a2e 53
soonerbot 14:a30aa3b29a2e 54 // Loop
soonerbot 0:3a3dd78038a6 55 while(1) {
soonerbot 1:c28fac16a109 56 DBGPRINT("BB\n\r",1);
soonerbot 7:3b2cf7efe5d1 57 leds = leds^0x7; // toggle the LEDs for each loop
soonerbot 1:c28fac16a109 58 tmpchar = pc.getc();
soonerbot 1:c28fac16a109 59
soonerbot 7:3b2cf7efe5d1 60 // all of these movement commands are blocking, so they can't be easily stopped short of resetting the microcontroller
soonerbot 1:c28fac16a109 61 switch(tmpchar){
soonerbot 15:b10859606504 62 /*case 'e': //drive in a smallish square
soonerbot 7:3b2cf7efe5d1 63 bot.driveForward(0,3000);
soonerbot 7:3b2cf7efe5d1 64 bot.driveForward(-90,0);
soonerbot 7:3b2cf7efe5d1 65 bot.driveForward(-90,3000);
soonerbot 7:3b2cf7efe5d1 66 bot.driveForward(-180,0);
soonerbot 7:3b2cf7efe5d1 67 bot.driveForward(-180,3000);
soonerbot 7:3b2cf7efe5d1 68 bot.driveForward(-270,0);
soonerbot 7:3b2cf7efe5d1 69 bot.driveForward(-270,3000);
soonerbot 7:3b2cf7efe5d1 70 bot.driveForward(0,0);
soonerbot 7:3b2cf7efe5d1 71 DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159);
soonerbot 15:b10859606504 72 break;*/
soonerbot 7:3b2cf7efe5d1 73 case 'q': //poll the encoders
soonerbot 1:c28fac16a109 74 constbuf = bot.bigenc.getVals();
soonerbot 1:c28fac16a109 75 DBGPRINT("\n\r%d\t%d\t%d\t%d\n\r",constbuf[0],constbuf[1],constbuf[2],constbuf[3]);
soonerbot 1:c28fac16a109 76 break;
soonerbot 7:3b2cf7efe5d1 77 case 'z': //set the current direction to "forward" for the following "go forward/reverse" commands
soonerbot 4:adc885f4ab75 78 targetAngle = bot.gyro.getZDegrees();
soonerbot 4:adc885f4ab75 79 break;
soonerbot 7:3b2cf7efe5d1 80 case 'w': // turn 90 degrees counter clockwise from the starting rotation
soonerbot 15:b10859606504 81
soonerbot 15:b10859606504 82 s.toPosition(0);
soonerbot 15:b10859606504 83 //bot.driveForward(90,0);
soonerbot 7:3b2cf7efe5d1 84 DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159);
soonerbot 7:3b2cf7efe5d1 85 break;
soonerbot 7:3b2cf7efe5d1 86 case 'x': // turn 90 degrees clockwise from the starting rotation
soonerbot 15:b10859606504 87 s.toPosition(180);
soonerbot 15:b10859606504 88 //bot.driveForward(-90,0);
soonerbot 6:62d498ee97cf 89 DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159);
soonerbot 6:62d498ee97cf 90 break;
soonerbot 7:3b2cf7efe5d1 91 case 'a': // turn the opposite direction from the starting one
soonerbot 15:b10859606504 92 s.toPosition(90);
soonerbot 15:b10859606504 93 //bot.driveForward(180,0);
soonerbot 6:62d498ee97cf 94 DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159);
soonerbot 1:c28fac16a109 95 break;
soonerbot 7:3b2cf7efe5d1 96 case 'd': // turn back to starting rotation
soonerbot 15:b10859606504 97 //bot.driveForward(0,0);
soonerbot 7:3b2cf7efe5d1 98 DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159);
soonerbot 7:3b2cf7efe5d1 99 break;
soonerbot 7:3b2cf7efe5d1 100 case 'W': // drive forward a small bit
soonerbot 9:aff48e331147 101 //bot.driveForward(targetAngle,1000);
soonerbot 10:926f142f16a3 102 //bot.motors.moveForward(100);
soonerbot 11:967469d7e01c 103 tmpOut = bot.absDriveForward(targetAngle,3000);
soonerbot 10:926f142f16a3 104 DBGPRINT("W -> %d\n\r",tmpOut);
soonerbot 12:925f52da3ba9 105 DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159);
soonerbot 4:adc885f4ab75 106 break;
soonerbot 7:3b2cf7efe5d1 107 case 'E': //drive forward five times as much
soonerbot 15:b10859606504 108 //bot.driveForward(targetAngle,5000);
soonerbot 15:b10859606504 109 bot.absDriveForward(0,5000);
soonerbot 4:adc885f4ab75 110 DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159);
soonerbot 4:adc885f4ab75 111 break;
soonerbot 7:3b2cf7efe5d1 112 case 'X': // small reverse
soonerbot 10:926f142f16a3 113 //bot.driveForward(targetAngle,-1000);
soonerbot 10:926f142f16a3 114
soonerbot 11:967469d7e01c 115 tmpOut = bot.absDriveForward(targetAngle,-3000);
soonerbot 10:926f142f16a3 116 DBGPRINT("X -> %d\n\r",tmpOut);
soonerbot 12:925f52da3ba9 117 DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159);
soonerbot 4:adc885f4ab75 118 break;
soonerbot 7:3b2cf7efe5d1 119 case 'C': // big reverse
soonerbot 15:b10859606504 120 //bot.driveForward(targetAngle,-5000);
soonerbot 15:b10859606504 121 bot.absDriveForward(0,-5000);
soonerbot 4:adc885f4ab75 122 DBGPRINT("={%f,\t%f,\t%f}\n\r",bot.x,bot.y,bot.rot*180.0/3.14159);
soonerbot 4:adc885f4ab75 123 break;
soonerbot 7:3b2cf7efe5d1 124 case 'p': // poll the gyro
soonerbot 3:a223b0bf8256 125 DBGPRINT("%d\r\n",bot.gyro.getZ());
soonerbot 3:a223b0bf8256 126 break;
soonerbot 7:3b2cf7efe5d1 127 case 'l': //calibrate the gyro ( caution, only use after resetting and before moving, otherwise will break any calibration)
soonerbot 3:a223b0bf8256 128 bot.gyro.calibrate();
soonerbot 3:a223b0bf8256 129 break;
soonerbot 7:3b2cf7efe5d1 130
soonerbot 7:3b2cf7efe5d1 131 // these are all control system modifications which should be fairly well locked down at this point
soonerbot 4:adc885f4ab75 132 case 't':
soonerbot 4:adc885f4ab75 133 bot.pfac*=2;
soonerbot 4:adc885f4ab75 134 DBGPRINT("pfac = %f\r\n",bot.pfac);
soonerbot 4:adc885f4ab75 135 break;
soonerbot 4:adc885f4ab75 136 case 'g':
soonerbot 4:adc885f4ab75 137 bot.pfac/=2;
soonerbot 4:adc885f4ab75 138 DBGPRINT("pfac = %f\r\n",bot.pfac);
soonerbot 4:adc885f4ab75 139 break;
soonerbot 4:adc885f4ab75 140 case 'y':
soonerbot 4:adc885f4ab75 141 bot.ifac*=2;
soonerbot 4:adc885f4ab75 142 DBGPRINT("ifac = %f\r\n",bot.ifac);
soonerbot 4:adc885f4ab75 143 break;
soonerbot 4:adc885f4ab75 144 case 'h':
soonerbot 4:adc885f4ab75 145 bot.ifac/=2;
soonerbot 4:adc885f4ab75 146 DBGPRINT("ifac = %f\r\n",bot.ifac);
soonerbot 4:adc885f4ab75 147 break;
soonerbot 4:adc885f4ab75 148 case 'u':
soonerbot 4:adc885f4ab75 149 bot.dfac*=2;
soonerbot 4:adc885f4ab75 150 DBGPRINT("dfac = %f\r\n",bot.dfac);
soonerbot 4:adc885f4ab75 151 break;
soonerbot 4:adc885f4ab75 152 case 'j':
soonerbot 4:adc885f4ab75 153 bot.dfac/=2;
soonerbot 4:adc885f4ab75 154 DBGPRINT("dfac = %f\r\n",bot.dfac);
soonerbot 4:adc885f4ab75 155 break;
soonerbot 4:adc885f4ab75 156 case 'i':
soonerbot 14:a30aa3b29a2e 157 //put distance ping here
soonerbot 14:a30aa3b29a2e 158 bot.pingLeft.trigger();
soonerbot 15:b10859606504 159 wait(0.1);
soonerbot 14:a30aa3b29a2e 160 float pingresult = bot.pingLeft.inches();
soonerbot 14:a30aa3b29a2e 161 DBGPRINT("Distance = %f\r\n",pingresult);
soonerbot 4:adc885f4ab75 162 break;
soonerbot 4:adc885f4ab75 163 case 'k':
soonerbot 4:adc885f4ab75 164 bot.angfac/=2;
soonerbot 4:adc885f4ab75 165 DBGPRINT("angfac = %f\r\n",bot.angfac);
soonerbot 4:adc885f4ab75 166 break;
soonerbot 4:adc885f4ab75 167 case 'T':
soonerbot 4:adc885f4ab75 168 bot.pfac*=1.05;
soonerbot 4:adc885f4ab75 169 DBGPRINT("pfac = %f\r\n",bot.pfac);
soonerbot 4:adc885f4ab75 170 break;
soonerbot 4:adc885f4ab75 171 case 'G':
soonerbot 4:adc885f4ab75 172 bot.pfac/=1.05;
soonerbot 4:adc885f4ab75 173 DBGPRINT("pfac = %f\r\n",bot.pfac);
soonerbot 4:adc885f4ab75 174 break;
soonerbot 4:adc885f4ab75 175 case 'Y':
soonerbot 4:adc885f4ab75 176 bot.ifac*=1.05;
soonerbot 4:adc885f4ab75 177 DBGPRINT("ifac = %f\r\n",bot.ifac);
soonerbot 4:adc885f4ab75 178 break;
soonerbot 4:adc885f4ab75 179 case 'H':
soonerbot 4:adc885f4ab75 180 bot.ifac/=1.05;
soonerbot 4:adc885f4ab75 181 DBGPRINT("ifac = %f\r\n",bot.ifac);
soonerbot 4:adc885f4ab75 182 break;
soonerbot 4:adc885f4ab75 183 case 'U':
soonerbot 4:adc885f4ab75 184 bot.dfac*=1.05;
soonerbot 4:adc885f4ab75 185 DBGPRINT("dfac = %f\r\n",bot.dfac);
soonerbot 4:adc885f4ab75 186 break;
soonerbot 4:adc885f4ab75 187 case 'J':
soonerbot 4:adc885f4ab75 188 bot.dfac/=1.05;
soonerbot 4:adc885f4ab75 189 DBGPRINT("dfac = %f\r\n",bot.dfac);
soonerbot 4:adc885f4ab75 190 break;
soonerbot 7:3b2cf7efe5d1 191
soonerbot 7:3b2cf7efe5d1 192 // poll the compass ( currently gives bad values )
soonerbot 4:adc885f4ab75 193 case 'Q':
soonerbot 4:adc885f4ab75 194 DBGPRINT("Compass vector = %d\t%d\t%d\r\n",bot.gyro.xmag,bot.gyro.ymag,bot.gyro.zmag);
soonerbot 4:adc885f4ab75 195 break;
soonerbot 7:3b2cf7efe5d1 196
soonerbot 7:3b2cf7efe5d1 197 // brake if we get an unknown command
soonerbot 1:c28fac16a109 198 default:
soonerbot 15:b10859606504 199 //bot.left.brake();
soonerbot 15:b10859606504 200 //bot.right.brake();
soonerbot 1:c28fac16a109 201 break;
soonerbot 1:c28fac16a109 202
soonerbot 1:c28fac16a109 203 }
soonerbot 0:3a3dd78038a6 204 }
soonerbot 0:3a3dd78038a6 205 }