NXP Group 13 / Mbed 2 deprecated Car3

Dependencies:   camera mbed tsi_sensor

Fork of Car2 by Zach Matthews

Committer:
lmstthomas
Date:
Mon Apr 17 23:37:02 2017 +0000
Revision:
24:6219b8ce421f
Parent:
23:6e1e142b7baf
Child:
25:74c12b0acf0c
redoing detectStartFinish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zamatthews 0:b761ef827157 1 #include "mbed.h"
zamatthews 6:971236e48adc 2 #include "Camera.h"
lmstthomas 22:9ef4a01e5038 3 #include "tsi_sensor.h"
zamatthews 17:846417c48571 4 #define STRAIGHT 0.00092f
lmstthomas 4:f4852befd69c 5 #define FULLRIGHT 0.0013f
zamatthews 14:c6f0a3c4e222 6 #define FULLLEFT 0.0005
zamatthews 14:c6f0a3c4e222 7 #define MIN_TURN_RATIO 0
zamatthews 14:c6f0a3c4e222 8 #define MAX_TURN_RATIO 1
zamatthews 20:ebdfeb37309c 9 #define MIN_SPEED 0.17 //.15 seems to be optimal
zamatthews 20:ebdfeb37309c 10 #define MAX_SPEED 0.45 //.5
zamatthews 17:846417c48571 11 #define TURN_TIME 0
lmstthomas 24:6219b8ce421f 12 #define STRAIGHT_TIME 20
lmstthomas 24:6219b8ce421f 13 #define START_FINISH_TIME 60
zamatthews 16:60e70bef7828 14 #define DEFAULT_THRESHOLD 65
zamatthews 17:846417c48571 15 #define BLIND_LENGTH 30
zamatthews 17:846417c48571 16 #define DIFF_RATIO 0.5
zamatthews 3:dadfc15fc2d1 17
lmstthomas 22:9ef4a01e5038 18 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
lmstthomas 22:9ef4a01e5038 19 #define ELEC0 9
lmstthomas 22:9ef4a01e5038 20 #define ELEC1 10
lmstthomas 22:9ef4a01e5038 21 #elif defined (TARGET_KL05Z)
lmstthomas 22:9ef4a01e5038 22 #define ELEC0 9
lmstthomas 22:9ef4a01e5038 23 #define ELEC1 8
lmstthomas 22:9ef4a01e5038 24 #else
lmstthomas 22:9ef4a01e5038 25 #error TARGET NOT DEFINED
lmstthomas 22:9ef4a01e5038 26 #endif
lmstthomas 22:9ef4a01e5038 27
lmstthomas 22:9ef4a01e5038 28
zamatthews 3:dadfc15fc2d1 29 PwmOut servo(PTE20);
zamatthews 5:137dfb3e692f 30 PwmOut motor_left(PTA5);
zamatthews 5:137dfb3e692f 31 PwmOut motor_right(PTC8);
zamatthews 3:dadfc15fc2d1 32 DigitalOut DIR_L(PTD4);
zamatthews 3:dadfc15fc2d1 33 DigitalOut DIR_R(PTA4);
zamatthews 9:644102f863a5 34 Serial pc(USBTX, USBRX);
zamatthews 9:644102f863a5 35 Camera cam(PTE23, PTE21, PTB3);
lmstthomas 22:9ef4a01e5038 36 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
zamatthews 11:45f345aad8ba 37 int turnCounter = 0;
zamatthews 16:60e70bef7828 38 int threshold = DEFAULT_THRESHOLD;
zamatthews 11:45f345aad8ba 39 float wheelPos = STRAIGHT;
zamatthews 19:25f22034a3e2 40 bool idle = true;
zamatthews 16:60e70bef7828 41 int leftBlind = 0;
zamatthews 16:60e70bef7828 42 int rightBlind = 0;
lmstthomas 22:9ef4a01e5038 43 float lastSlide;
zamatthews 20:ebdfeb37309c 44 int numDarks = 0;
lmstthomas 24:6219b8ce421f 45 int minLightGap = 1;
lmstthomas 24:6219b8ce421f 46 int maxLightGap = 55;
lmstthomas 24:6219b8ce421f 47 int minDarkBlock = 4;
lmstthomas 24:6219b8ce421f 48 int maxDarkBlock = 40;
lmstthomas 24:6219b8ce421f 49 int positionOffThreshold = 3;
lmstthomas 24:6219b8ce421f 50 struct darkBlock *darkBlockHead;
zamatthews 20:ebdfeb37309c 51 PwmOut led(LED_GREEN);
lmstthomas 22:9ef4a01e5038 52 PwmOut redLed(LED_RED);
zamatthews 0:b761ef827157 53
zamatthews 12:4ccf304800fe 54 /*
zamatthews 12:4ccf304800fe 55 Function: setAccel
zamatthews 12:4ccf304800fe 56 Description: Sets the speed for the right and left motors individually based
zamatthews 12:4ccf304800fe 57 on the turning angle.
zamatthews 12:4ccf304800fe 58 */
zamatthews 19:25f22034a3e2 59 void setAccel(float turnAngle){
zamatthews 20:ebdfeb37309c 60 //idle = false;
zamatthews 19:25f22034a3e2 61 if(!idle){
zamatthews 19:25f22034a3e2 62 turnAngle -= STRAIGHT; //this gets a value from -0.00035 and +0.00035
zamatthews 19:25f22034a3e2 63 float turnRatio = abs(turnAngle)/ (FULLRIGHT - STRAIGHT);
zamatthews 19:25f22034a3e2 64 float newSpeed = ((MAX_SPEED - MIN_SPEED)*(1-turnRatio)/3)+MIN_SPEED;
zamatthews 19:25f22034a3e2 65 motor_left.write(newSpeed + DIFF_RATIO * newSpeed * (turnAngle / (STRAIGHT - FULLLEFT)));
zamatthews 19:25f22034a3e2 66 motor_right.write(newSpeed - DIFF_RATIO * newSpeed * (turnAngle / (FULLRIGHT - STRAIGHT)));
zamatthews 19:25f22034a3e2 67 }
zamatthews 19:25f22034a3e2 68 else{
zamatthews 19:25f22034a3e2 69 motor_left.write(0);
zamatthews 19:25f22034a3e2 70 motor_right.write(0);
zamatthews 19:25f22034a3e2 71 }
zamatthews 12:4ccf304800fe 72 }//end setAccel
zamatthews 3:dadfc15fc2d1 73
zamatthews 12:4ccf304800fe 74 /*
zamatthews 12:4ccf304800fe 75 Function: turnWheels
zamatthews 12:4ccf304800fe 76 Description: Turns the wheels in order to stay between two black lines seen
zamatthews 12:4ccf304800fe 77 by the camera
zamatthews 12:4ccf304800fe 78 */
zamatthews 14:c6f0a3c4e222 79 void turnWheels(int frame[]){
zamatthews 10:246782426144 80 int positionSum = 0;
zamatthews 20:ebdfeb37309c 81 numDarks = 0;
zamatthews 10:246782426144 82 for(int i = 0; i < 128; i++){
zamatthews 16:60e70bef7828 83 if(frame[i] < threshold){
zamatthews 10:246782426144 84 positionSum += i;
zamatthews 10:246782426144 85 numDarks++;
zamatthews 10:246782426144 86 }
zamatthews 10:246782426144 87 }
zamatthews 10:246782426144 88 float averagePos = 0;
zamatthews 12:4ccf304800fe 89
zamatthews 14:c6f0a3c4e222 90 if (numDarks == 0) {
zamatthews 15:50d5cfa98425 91 if(turnCounter >= (STRAIGHT_TIME)){
zamatthews 15:50d5cfa98425 92 wheelPos = STRAIGHT;
zamatthews 15:50d5cfa98425 93 turnCounter = TURN_TIME;
zamatthews 16:60e70bef7828 94 leftBlind = 0;
zamatthews 16:60e70bef7828 95 rightBlind = 0;
zamatthews 15:50d5cfa98425 96 }
zamatthews 11:45f345aad8ba 97 }
zamatthews 12:4ccf304800fe 98
zamatthews 12:4ccf304800fe 99 else {
zamatthews 12:4ccf304800fe 100 averagePos = positionSum / numDarks;
zamatthews 16:60e70bef7828 101
zamatthews 16:60e70bef7828 102 if(((averagePos <= 64 - leftBlind)) && ((wheelPos >= STRAIGHT) || turnCounter >= TURN_TIME)){
zamatthews 16:60e70bef7828 103 float powerRatio = (averagePos / (64 - leftBlind)) * MAX_TURN_RATIO + MIN_TURN_RATIO;
zamatthews 15:50d5cfa98425 104 powerRatio = sqrt(powerRatio);
zamatthews 12:4ccf304800fe 105 wheelPos = STRAIGHT + (FULLRIGHT - STRAIGHT) * powerRatio;
zamatthews 12:4ccf304800fe 106 turnCounter = 0;
zamatthews 16:60e70bef7828 107 leftBlind = 0;
zamatthews 16:60e70bef7828 108 rightBlind = BLIND_LENGTH;
zamatthews 11:45f345aad8ba 109 }
zamatthews 12:4ccf304800fe 110
zamatthews 16:60e70bef7828 111 else if((averagePos >= 64 + rightBlind) && (wheelPos <= STRAIGHT || turnCounter >= TURN_TIME)){
zamatthews 16:60e70bef7828 112 float powerRatio = (1 - (averagePos - 64 - rightBlind) / (64 - rightBlind)) * MAX_TURN_RATIO + MIN_TURN_RATIO;
zamatthews 15:50d5cfa98425 113 powerRatio = sqrt(powerRatio);
zamatthews 12:4ccf304800fe 114 wheelPos = STRAIGHT - (STRAIGHT - FULLLEFT) * powerRatio;
zamatthews 12:4ccf304800fe 115 turnCounter = 0;
zamatthews 16:60e70bef7828 116 leftBlind = BLIND_LENGTH;
zamatthews 16:60e70bef7828 117 rightBlind = 0;
zamatthews 11:45f345aad8ba 118 }
zamatthews 14:c6f0a3c4e222 119 }
zamatthews 12:4ccf304800fe 120 turnCounter++;
zamatthews 10:246782426144 121 servo.pulsewidth(wheelPos);
zamatthews 3:dadfc15fc2d1 122 }
zamatthews 3:dadfc15fc2d1 123
lmstthomas 24:6219b8ce421f 124 struct darkBlock{
lmstthomas 24:6219b8ce421f 125 int position;
lmstthomas 24:6219b8ce421f 126 int length;
lmstthomas 24:6219b8ce421f 127 int TTL; //time to live
lmstthomas 24:6219b8ce421f 128 struct darkBlock *next;
lmstthomas 24:6219b8ce421f 129 struct darkBlock *prev;
lmstthomas 24:6219b8ce421f 130 };
lmstthomas 24:6219b8ce421f 131
zamatthews 19:25f22034a3e2 132 /*
zamatthews 19:25f22034a3e2 133 Function: detectStartFinish
zamatthews 19:25f22034a3e2 134 Description: detects the mark on the track that represents the start/finish line and toggles RUNNING
zamatthews 19:25f22034a3e2 135 */
zamatthews 19:25f22034a3e2 136 void detectStartFinish(int frame[]){
lmstthomas 24:6219b8ce421f 137
lmstthomas 24:6219b8ce421f 138 //idle override by touching the slider
lmstthomas 22:9ef4a01e5038 139 if(tsi.readPercentage() != lastSlide){
lmstthomas 22:9ef4a01e5038 140 idle = !idle;
lmstthomas 22:9ef4a01e5038 141 led = 0.0;
lmstthomas 22:9ef4a01e5038 142 wait(0.5);
lmstthomas 22:9ef4a01e5038 143 }
lmstthomas 22:9ef4a01e5038 144 if(numDarks <= 15) return;
zamatthews 19:25f22034a3e2 145 for(int i = 0; i < 128; i++){
lmstthomas 24:6219b8ce421f 146
lmstthomas 24:6219b8ce421f 147 }
lmstthomas 24:6219b8ce421f 148
lmstthomas 24:6219b8ce421f 149 idle = !idle; //toggle idle
lmstthomas 24:6219b8ce421f 150 if(!idle){
lmstthomas 24:6219b8ce421f 151 led = 1.0 - led;
lmstthomas 24:6219b8ce421f 152 servo.pulsewidth(STRAIGHT);
lmstthomas 24:6219b8ce421f 153 wait(3);
lmstthomas 21:18f2dc256df2 154 }
lmstthomas 24:6219b8ce421f 155 else led = 1.0;
zamatthews 19:25f22034a3e2 156 }
zamatthews 19:25f22034a3e2 157
lmstthomas 23:6e1e142b7baf 158 /*
lmstthomas 23:6e1e142b7baf 159 Function: display
lmstthomas 23:6e1e142b7baf 160 Description: This function is used to display what the camera sees to a
lmstthomas 23:6e1e142b7baf 161 computer.
lmstthomas 23:6e1e142b7baf 162 *Before using this function, the car should be connected to the
lmstthomas 23:6e1e142b7baf 163 computer. The car should also not be running. This is necessary because
lmstthomas 23:6e1e142b7baf 164 printing while the car is running will slow it way down and potentially
lmstthomas 23:6e1e142b7baf 165 cause it to crash.
lmstthomas 23:6e1e142b7baf 166
lmstthomas 23:6e1e142b7baf 167 */
zamatthews 12:4ccf304800fe 168 void display(int frame[]){
zamatthews 12:4ccf304800fe 169 char draw = 'x';
zamatthews 12:4ccf304800fe 170 for(int i = 0; i< 128; i++){
zamatthews 17:846417c48571 171
zamatthews 17:846417c48571 172 if (frame[i] < threshold) draw = '|';
zamatthews 12:4ccf304800fe 173 else draw = '-';
zamatthews 12:4ccf304800fe 174 pc.printf("%c", draw);
zamatthews 12:4ccf304800fe 175 draw = 'x';
zamatthews 12:4ccf304800fe 176 }
zamatthews 20:ebdfeb37309c 177 pc.printf("\r");
zamatthews 3:dadfc15fc2d1 178 }
zamatthews 16:60e70bef7828 179
lmstthomas 23:6e1e142b7baf 180 /*
lmstthomas 23:6e1e142b7baf 181 Function: setThreshold
lmstthomas 23:6e1e142b7baf 182 Description: This function is used when the car first starts. It checks the
lmstthomas 23:6e1e142b7baf 183 lightest value and the darkest value it can find, averages them and uses
lmstthomas 23:6e1e142b7baf 184 that as the light/dark threshold.
lmstthomas 23:6e1e142b7baf 185 */
zamatthews 16:60e70bef7828 186 void setThreshold(){
zamatthews 16:60e70bef7828 187 cam.capture();
zamatthews 16:60e70bef7828 188 int low = 99;
zamatthews 16:60e70bef7828 189 int high = 0;
zamatthews 16:60e70bef7828 190 for(int i = 0; i < 128; i++){
zamatthews 16:60e70bef7828 191 if(cam.imageData[i] > high) high = cam.imageData[i];
zamatthews 16:60e70bef7828 192 if(cam.imageData[i] < low) low = cam.imageData[i];
zamatthews 16:60e70bef7828 193 }
lmstthomas 22:9ef4a01e5038 194 // threshold = low + (high - low) * 0.35; //(high + 2 * low) / 3;
lmstthomas 24:6219b8ce421f 195 threshold = (3 * low + high) / 4;
zamatthews 16:60e70bef7828 196 }
zamatthews 16:60e70bef7828 197
zamatthews 2:0db7cc5ad6db 198 int main() {
zamatthews 16:60e70bef7828 199 setThreshold();
zamatthews 5:137dfb3e692f 200 motor_left.period_us(50);
zamatthews 5:137dfb3e692f 201 motor_right.period_us(50);
zamatthews 5:137dfb3e692f 202 DIR_R = 1;
zamatthews 2:0db7cc5ad6db 203 DIR_L = 0;
zamatthews 3:dadfc15fc2d1 204 servo.period(0.020f);
zamatthews 20:ebdfeb37309c 205 led = 1.0;
lmstthomas 22:9ef4a01e5038 206 redLed = 1.0;
lmstthomas 22:9ef4a01e5038 207 idle = true;
lmstthomas 22:9ef4a01e5038 208 lastSlide = tsi.readPercentage();
zamatthews 2:0db7cc5ad6db 209 while(1){
lmstthomas 24:6219b8ce421f 210 wait_ms(1);
zamatthews 9:644102f863a5 211 cam.capture();
zamatthews 12:4ccf304800fe 212 //display(cam.imageData);
zamatthews 19:25f22034a3e2 213 turnWheels(cam.imageData);
zamatthews 19:25f22034a3e2 214 setAccel(wheelPos);
lmstthomas 22:9ef4a01e5038 215 detectStartFinish(cam.imageData);
zamatthews 12:4ccf304800fe 216 }
zamatthews 12:4ccf304800fe 217 }