Buggy bois / Mbed 2 deprecated headache

Dependencies:   mbed

Committer:
UditSrivastav
Date:
Mon Apr 29 09:40:30 2019 +0000
Revision:
13:521d6c6e7042
Parent:
12:11d203351eb0
Anythign um

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazdo25 8:5ed6685f6edd 1 class Robot {
mazdo25 3:01b5e80d842d 2 private:
mazdo25 7:cb07cdb35b6c 3 float static const distanceBetweenWheels = 0.19; //currently not used may be used later
mazdo25 7:cb07cdb35b6c 4 int static const numberOfSamples = 100; //number of samples the line voltage array will hold
mazdo25 7:cb07cdb35b6c 5 int static const numberOfSensors = 6; //how many sensors you want to use
mazdo25 7:cb07cdb35b6c 6 float AF; //attenuation factor just a number you want to minimize max speed by, obviously dont go higher than 1
mazdo25 7:cb07cdb35b6c 7 Wheel* leftWheel; //a pointer to the left wheel
mazdo25 5:f1613df66ceb 8 Wheel* rightWheel;
mazdo25 3:01b5e80d842d 9 lineSensor* sensorArray[numberOfSensors];
mazdo25 5:f1613df66ceb 10 PID2 controller;
mazdo25 3:01b5e80d842d 11 float lineVoltages[numberOfSamples];
mazdo25 3:01b5e80d842d 12 int lvIndex;
mazdo25 3:01b5e80d842d 13 int sensorNumber;
mazdo25 10:acf5cb8d58d5 14 int brakeC;
mazdo25 6:477382219bcf 15
mazdo25 3:01b5e80d842d 16 Ticker updater;
mazdo25 8:5ed6685f6edd 17 Timeout timeToStop;
mazdo25 6:477382219bcf 18
mazdo25 6:477382219bcf 19 float Reflec;
UditSrivastav 12:11d203351eb0 20 int endOfLineDetection;
mazdo25 4:208f5279143a 21 float RoboticAngularVelocity;
mazdo25 4:208f5279143a 22
mazdo25 7:cb07cdb35b6c 23 char state;
mazdo25 7:cb07cdb35b6c 24
mazdo25 3:01b5e80d842d 25 public:
mazdo25 3:01b5e80d842d 26
mazdo25 10:acf5cb8d58d5 27 Robot(Wheel* LW, Wheel* RW, lineSensor* SA[]) : controller(2.0f, 0.0f, 0.0f, 0.0006f) //this controller actually does nothing atm
mazdo25 3:01b5e80d842d 28 {
UditSrivastav 12:11d203351eb0 29 endOfLineDetection = 0;
mazdo25 6:477382219bcf 30 updater.detach();
mazdo25 8:5ed6685f6edd 31 timeToStop.detach();
mazdo25 9:cefa177c1353 32 AF = 0.7f; //change this value to change max speedzzzzzzzzzzzzzzzzz
mazdo25 8:5ed6685f6edd 33 state = 'F';
mazdo25 8:5ed6685f6edd 34 Reflec = 0;
mazdo25 8:5ed6685f6edd 35
mazdo25 5:f1613df66ceb 36 lvIndex = 0;
mazdo25 4:208f5279143a 37 sensorNumber = 0;
mazdo25 3:01b5e80d842d 38 sensorArray[0] = SA[0];
mazdo25 4:208f5279143a 39 sensorArray[1] = SA[1];
mazdo25 4:208f5279143a 40 sensorArray[2] = SA[2];
mazdo25 4:208f5279143a 41 sensorArray[3] = SA[3];
mazdo25 4:208f5279143a 42 sensorArray[4] = SA[4];
mazdo25 4:208f5279143a 43 sensorArray[5] = SA[5];
mazdo25 3:01b5e80d842d 44 leftWheel = LW;
mazdo25 3:01b5e80d842d 45 rightWheel= RW;
mazdo25 3:01b5e80d842d 46
mazdo25 4:208f5279143a 47 lineVoltages[lvIndex] = 0.0f;
mazdo25 8:5ed6685f6edd 48
mazdo25 9:cefa177c1353 49 controller.setInputLimits(-180.0f,180.0f);
mazdo25 8:5ed6685f6edd 50 controller.setSetPoint(0.0f);
mazdo25 3:01b5e80d842d 51 //controller will output a value between +- max speed of wheels
mazdo25 5:f1613df66ceb 52
mazdo25 5:f1613df66ceb 53 controller.assignLimitAddress(rightWheel->returnMinAVptr(),leftWheel->returnMaxAVptr());
mazdo25 3:01b5e80d842d 54 };
mazdo25 3:01b5e80d842d 55
mazdo25 3:01b5e80d842d 56 float calculateTranslationalVelocity()
mazdo25 3:01b5e80d842d 57 {
mazdo25 3:01b5e80d842d 58 return ((leftWheel->returnAngularVelocity() + rightWheel->returnAngularVelocity())/2.0f);
mazdo25 3:01b5e80d842d 59 }
mazdo25 4:208f5279143a 60
mazdo25 4:208f5279143a 61 //difference between angular velocities.
mazdo25 4:208f5279143a 62 void dW()
mazdo25 3:01b5e80d842d 63 {
mazdo25 4:208f5279143a 64 RoboticAngularVelocity = leftWheel->returnAngularVelocity() - rightWheel->returnAngularVelocity();
mazdo25 4:208f5279143a 65 }
mazdo25 4:208f5279143a 66
mazdo25 4:208f5279143a 67 float returnRoboticAngularVelocity()
mazdo25 4:208f5279143a 68 {
mazdo25 4:208f5279143a 69 return RoboticAngularVelocity;
mazdo25 3:01b5e80d842d 70 }
mazdo25 3:01b5e80d842d 71
mazdo25 3:01b5e80d842d 72 //attempts to modify the angular velocity of the buggy
mazdo25 8:5ed6685f6edd 73 void adjustRbtAngularVelocity(float W)
mazdo25 3:01b5e80d842d 74 {
mazdo25 4:208f5279143a 75 //negative is a right turn
mazdo25 3:01b5e80d842d 76 if (W < 0)
mazdo25 3:01b5e80d842d 77 {
mazdo25 5:f1613df66ceb 78 rightWheel->adjustAngularVelocity(rightWheel->returnMaxAngularVel()*AF);
mazdo25 5:f1613df66ceb 79 leftWheel->adjustAngularVelocity((rightWheel->returnMaxAngularVel()+W)*AF);
mazdo25 3:01b5e80d842d 80 }
mazdo25 3:01b5e80d842d 81 else
mazdo25 3:01b5e80d842d 82 {
mazdo25 4:208f5279143a 83 leftWheel->adjustAngularVelocity(leftWheel->returnMaxAngularVel()*AF);
mazdo25 5:f1613df66ceb 84 rightWheel->adjustAngularVelocity((leftWheel->returnMaxAngularVel()-W)*AF);
mazdo25 4:208f5279143a 85 }
mazdo25 3:01b5e80d842d 86 }
mazdo25 3:01b5e80d842d 87
mazdo25 3:01b5e80d842d 88 void robotUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytime this function is called
mazdo25 3:01b5e80d842d 89 {
mazdo25 8:5ed6685f6edd 90 switch (state)
mazdo25 8:5ed6685f6edd 91 {
mazdo25 8:5ed6685f6edd 92 case 'T':
mazdo25 8:5ed6685f6edd 93 turn180();
mazdo25 4:208f5279143a 94 break;
mazdo25 8:5ed6685f6edd 95 case 'S':
mazdo25 8:5ed6685f6edd 96 stopMovement();
mazdo25 8:5ed6685f6edd 97 break;
mazdo25 8:5ed6685f6edd 98 default:
mazdo25 8:5ed6685f6edd 99 followLine();
mazdo25 8:5ed6685f6edd 100 break;
mazdo25 8:5ed6685f6edd 101 }
mazdo25 3:01b5e80d842d 102 }
mazdo25 3:01b5e80d842d 103
mazdo25 3:01b5e80d842d 104 void stopMovement(void)
mazdo25 3:01b5e80d842d 105 {
mazdo25 8:5ed6685f6edd 106 leftWheel->adjustAngularVelocity(0.0f);
mazdo25 8:5ed6685f6edd 107 rightWheel->adjustAngularVelocity(0.0f);
mazdo25 3:01b5e80d842d 108 }
mazdo25 5:f1613df66ceb 109
mazdo25 5:f1613df66ceb 110 float returnLineVoltage()
mazdo25 5:f1613df66ceb 111 {
mazdo25 5:f1613df66ceb 112 return lineVoltages[lvIndex%numberOfSamples];
mazdo25 5:f1613df66ceb 113 }
mazdo25 5:f1613df66ceb 114
mazdo25 5:f1613df66ceb 115 void rbtInit()
mazdo25 5:f1613df66ceb 116 {
mazdo25 6:477382219bcf 117 sensorArray[0]->sample();
mazdo25 8:5ed6685f6edd 118 sensorArray[0]->calcLineVoltage();
mazdo25 8:5ed6685f6edd 119 Reflec = sensorArray[0]->returnLineVoltage();
mazdo25 8:5ed6685f6edd 120 updater.attach(callback(this, &Robot::robotUpdates),0.0001f);
mazdo25 5:f1613df66ceb 121 }
mazdo25 7:cb07cdb35b6c 122
mazdo25 7:cb07cdb35b6c 123 void spin()
mazdo25 7:cb07cdb35b6c 124 {
mazdo25 7:cb07cdb35b6c 125 rightWheel->adjustAngularVelocity(rightWheel->returnMaxAngularVel()*0.3f);
mazdo25 7:cb07cdb35b6c 126 leftWheel->adjustAngularVelocity((rightWheel->returnMaxAngularVel())*-0.3f);
mazdo25 7:cb07cdb35b6c 127 }
mazdo25 7:cb07cdb35b6c 128
mazdo25 7:cb07cdb35b6c 129 void setState(char S)
mazdo25 7:cb07cdb35b6c 130 {
mazdo25 7:cb07cdb35b6c 131 state = S;
mazdo25 7:cb07cdb35b6c 132 }
mazdo25 8:5ed6685f6edd 133
mazdo25 8:5ed6685f6edd 134 void followLine(void)
mazdo25 8:5ed6685f6edd 135 {
mazdo25 8:5ed6685f6edd 136 float ambientLight;
mazdo25 8:5ed6685f6edd 137 float reading;
mazdo25 8:5ed6685f6edd 138 ambientLight = sensorArray[sensorNumber]->calcLineVoltage();
mazdo25 8:5ed6685f6edd 139 sensorArray[sensorNumber]->sample();
mazdo25 8:5ed6685f6edd 140 sensorArray[sensorNumber]->calcLineVoltage();
mazdo25 8:5ed6685f6edd 141 reading = (sensorArray[sensorNumber]->returnLineVoltage()-ambientLight);
mazdo25 8:5ed6685f6edd 142 switch (sensorNumber)
mazdo25 8:5ed6685f6edd 143 {
mazdo25 8:5ed6685f6edd 144 case 0:
mazdo25 10:acf5cb8d58d5 145 lineVoltages[lvIndex%numberOfSamples] = (reading * -260.0f);
UditSrivastav 12:11d203351eb0 146 if (reading <= 0.1f){endOfLineDetection++;}
mazdo25 8:5ed6685f6edd 147 break;
mazdo25 8:5ed6685f6edd 148 case 1:
mazdo25 10:acf5cb8d58d5 149 lineVoltages[lvIndex%numberOfSamples] += (reading * -105.0f);
UditSrivastav 12:11d203351eb0 150 if (reading <= 0.1f) {endOfLineDetection++;}
mazdo25 8:5ed6685f6edd 151 break;
mazdo25 8:5ed6685f6edd 152 case 2:
mazdo25 10:acf5cb8d58d5 153 lineVoltages[lvIndex%numberOfSamples] += (reading * -32.5f);
UditSrivastav 12:11d203351eb0 154 if (reading <= 0.1f) {endOfLineDetection++;}
mazdo25 8:5ed6685f6edd 155 break;
mazdo25 8:5ed6685f6edd 156 case 3:
mazdo25 10:acf5cb8d58d5 157 lineVoltages[lvIndex%numberOfSamples] += (reading * 32.5f);
UditSrivastav 12:11d203351eb0 158 if (reading <= 0.1f) {endOfLineDetection++;}
mazdo25 8:5ed6685f6edd 159 break;
mazdo25 8:5ed6685f6edd 160 case 4:
mazdo25 10:acf5cb8d58d5 161 lineVoltages[lvIndex%numberOfSamples] += (reading * 75.0f);
UditSrivastav 12:11d203351eb0 162 if (reading <= 0.1f) {endOfLineDetection++;}
mazdo25 8:5ed6685f6edd 163 break;
mazdo25 8:5ed6685f6edd 164 case 5:
mazdo25 10:acf5cb8d58d5 165 lineVoltages[lvIndex%numberOfSamples] += (reading * 175.0f);
UditSrivastav 12:11d203351eb0 166 if (reading <= 0.1f) {endOfLineDetection++;}
mazdo25 8:5ed6685f6edd 167 break;
mazdo25 8:5ed6685f6edd 168 }
mazdo25 8:5ed6685f6edd 169
mazdo25 8:5ed6685f6edd 170 sensorNumber ++;
mazdo25 8:5ed6685f6edd 171 if (sensorNumber >= numberOfSensors)
mazdo25 8:5ed6685f6edd 172 {
mazdo25 8:5ed6685f6edd 173 sensorNumber = 0;
UditSrivastav 12:11d203351eb0 174 if (endOfLineDetection < 5)
mazdo25 8:5ed6685f6edd 175 {
UditSrivastav 13:521d6c6e7042 176 AF = 0.5f;
mazdo25 8:5ed6685f6edd 177 adjustRbtAngularVelocity(lineVoltages[lvIndex%numberOfSamples]);
mazdo25 8:5ed6685f6edd 178 lvIndex++;
mazdo25 8:5ed6685f6edd 179 }
mazdo25 11:78c645fb76cd 180 else
mazdo25 8:5ed6685f6edd 181 {
UditSrivastav 13:521d6c6e7042 182 stopMovement();
UditSrivastav 13:521d6c6e7042 183 rightWheel->resetPID();
UditSrivastav 13:521d6c6e7042 184 leftWheel->resetPID();
mazdo25 11:78c645fb76cd 185 }
mazdo25 8:5ed6685f6edd 186 endOfLineDetection = 0;
mazdo25 8:5ed6685f6edd 187 }
mazdo25 8:5ed6685f6edd 188 }
mazdo25 8:5ed6685f6edd 189
mazdo25 8:5ed6685f6edd 190 void turn180()
mazdo25 8:5ed6685f6edd 191 {
UditSrivastav 13:521d6c6e7042 192 stopMovement();
UditSrivastav 13:521d6c6e7042 193 rightWheel->resetPID();
UditSrivastav 13:521d6c6e7042 194 leftWheel->resetPID();
mazdo25 8:5ed6685f6edd 195 updater.detach();
mazdo25 8:5ed6685f6edd 196 rightWheel->adjustAngularVelocity(rightWheel->returnMaxAngularVel()*0.3f);
mazdo25 8:5ed6685f6edd 197 leftWheel->adjustAngularVelocity(rightWheel->returnMaxAngularVel()*-0.3f);
UditSrivastav 13:521d6c6e7042 198 timeToStop.attach(callback(this, &Robot::reAttach),1.0f);
mazdo25 8:5ed6685f6edd 199 state = 'S';
mazdo25 8:5ed6685f6edd 200 }
mazdo25 8:5ed6685f6edd 201
mazdo25 8:5ed6685f6edd 202 void reAttach()
mazdo25 8:5ed6685f6edd 203 {
mazdo25 10:acf5cb8d58d5 204 updater.attach(callback(this, &Robot::robotUpdates),0.0001f);
mazdo25 9:cefa177c1353 205 }
mazdo25 9:cefa177c1353 206
mazdo25 9:cefa177c1353 207 float scaler(float prevMin, float prevMax, float newMin, float newMax, float var)
mazdo25 9:cefa177c1353 208 {
mazdo25 9:cefa177c1353 209 if (var > prevMax) {var = prevMax;}
mazdo25 9:cefa177c1353 210 if (var < prevMin) {var = prevMin;}
mazdo25 9:cefa177c1353 211 return (((var-prevMin)/(prevMax - prevMin))*(newMax-newMin))+newMin;
mazdo25 8:5ed6685f6edd 212 }
mazdo25 3:01b5e80d842d 213
mazdo25 3:01b5e80d842d 214 };
mazdo25 3:01b5e80d842d 215
mazdo25 3:01b5e80d842d 216 /*
mazdo25 3:01b5e80d842d 217 Timeout timeToStop,
mazdo25 8:5ed6685f6edd 218
mazdo25 3:01b5e80d842d 219
mazdo25 3:01b5e80d842d 220 void travelDistance(float d)//in metres
mazdo25 3:01b5e80d842d 221 {
mazdo25 3:01b5e80d842d 222 timeToStop.attach(callback(this, &Robot::stopMovement), d/(calculateTranslationalVelocity()*(float)PI*Wheel::wheelDiameter));
mazdo25 3:01b5e80d842d 223 }
mazdo25 3:01b5e80d842d 224
mazdo25 3:01b5e80d842d 225 void robotUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytime this function is called
mazdo25 3:01b5e80d842d 226 {
mazdo25 3:01b5e80d842d 227 sensorArray[sensorNumber]->sample();
mazdo25 3:01b5e80d842d 228
mazdo25 3:01b5e80d842d 229 if (sensorNumber < (numberOfSensors/2))
mazdo25 3:01b5e80d842d 230 {
mazdo25 3:01b5e80d842d 231 lineVoltages[(lvIndex%numberOfSamples)] += sensorArray[sensorNumber]->returnLineVoltage()*(sensorNumber-3);
mazdo25 3:01b5e80d842d 232 }
mazdo25 3:01b5e80d842d 233 else
mazdo25 3:01b5e80d842d 234 }
mazdo25 3:01b5e80d842d 235 lineVoltages[(lvIndex%numberOfSamples)] += sensorArray[sensorNumber]->returnLineVoltage()*(sensorNumber-2);
mazdo25 3:01b5e80d842d 236 }
mazdo25 3:01b5e80d842d 237
mazdo25 3:01b5e80d842d 238 sensorNumber++;
mazdo25 3:01b5e80d842d 239 if (sensorNumber % numberOfSensors == 0)
mazdo25 3:01b5e80d842d 240 {
mazdo25 3:01b5e80d842d 241 sensorNumber = 0;
mazdo25 3:01b5e80d842d 242 controller.setProcessValue(lineVoltages[(lvIndex%numberOfSamples)];
mazdo25 3:01b5e80d842d 243 adjustAngularVelocity(controller.compute());
mazdo25 3:01b5e80d842d 244 lvIndex++;
mazdo25 3:01b5e80d842d 245 }
mazdo25 3:01b5e80d842d 246 }
mazdo25 3:01b5e80d842d 247
mazdo25 3:01b5e80d842d 248 */
mazdo25 4:208f5279143a 249 //lineVoltages[lvIndex] += 0.5f;
mazdo25 4:208f5279143a 250
mazdo25 4:208f5279143a 251
mazdo25 4:208f5279143a 252
mazdo25 4:208f5279143a 253 /*if input value is greater than the maximum value the left wheel can go, go full right TURN
mazdo25 4:208f5279143a 254 if (W > leftWheel->returnMaxAngularVel()*AF)
mazdo25 4:208f5279143a 255 {
mazdo25 4:208f5279143a 256 leftWheel->adjustAngularVelocity(leftWheel->returnMaxAngularVel());
mazdo25 4:208f5279143a 257 rightWheel->adjustAngularVelocity(0);
mazdo25 4:208f5279143a 258 }
mazdo25 4:208f5279143a 259 else if (W < (-1.0f*rightWheel->returnMaxAngularVel())) //if input value is less than the fastest the right wheel can go in reverse go full left TURN
mazdo25 4:208f5279143a 260 {
mazdo25 4:208f5279143a 261 rightWheel->adjustAngularVelocity(rightWheel->returnMaxAngularVel());
mazdo25 4:208f5279143a 262 leftWheel->adjustAngularVelocity(0);
mazdo25 4:208f5279143a 263 }
mazdo25 4:208f5279143a 264 else if (W == 0)
mazdo25 4:208f5279143a 265 {
mazdo25 4:208f5279143a 266 rightWheel->adjustAngularVelocity(rightWheel->returnMaxAngularVel()*AF);
mazdo25 4:208f5279143a 267 leftWheel->adjustAngularVelocity(leftWheel->returnMaxAngularVel()*AF);
mazdo25 4:208f5279143a 268 }
mazdo25 4:208f5279143a 269 else
mazdo25 4:208f5279143a 270 {
mazdo25 4:208f5279143a 271
mazdo25 4:208f5279143a 272 */