car using PID from centre line
Dependencies: FRDM-TFC mbed CBuffer XBEE mbed_angular_speed motor2 MMA8451Q
Fork of KL25Z_Camera_Test by
Diff: main.cpp
- Revision:
- 26:f3d770f3eda1
- Parent:
- 22:973b95478663
- Child:
- 27:627d67e3b9b0
--- a/main.cpp Mon Dec 12 09:50:39 2016 +0000 +++ b/main.cpp Thu Dec 15 10:05:07 2016 +0000 @@ -140,7 +140,7 @@ diff = 0; prev = -1; for(i = 63; i > 0; i--) { - curr_left = (int8_t)(cam_data[i] >> 4) & 0xFF; + curr_left = (uint8_t)(cam_data[i] >> 4) & 0xFF; diff = prev - curr_left; if(abs(diff) >= CAM_DIFF && curr_left <= CAM_THRESHOLD && prev != -1) { l = i; @@ -151,7 +151,7 @@ prev = -1; for(i = 64; i < 128; i++) { - curr_right = (int8_t)(cam_data[i] >> 4) & 0xFF; + curr_right = (uint8_t)(cam_data[i] >> 4) & 0xFF; int diff = prev - curr_right; if(abs(diff) >= CAM_DIFF && curr_right <= CAM_THRESHOLD && prev != -1) { r = i; @@ -319,23 +319,13 @@ //v1: //Hacky way to detect the start/stop signal - if(right - left < 60) { + /*if(right - left < 60) { sendString("START STOP!!"); - //lapTime(); - //testSpeed(speed) - /*if(seen) { - seen = false; - } else { - startstop++; - seen = true; - } - if(startstop >= 1) { - */ TFC_SetMotorPWM(0.f,0.f); TFC_HBRIDGE_DISABLE; startstop = 0; - } + }*/ //----------------------------START/STOP v2----------------------------- //New method plan: @@ -369,34 +359,35 @@ //finishLeft: */ - //v2.5: - /* + //v2.5:-------------- + int slower = 0; - uint8_t difference = 0; - uint8_t lastPixel, currentPixel, transitionsSeen; + int difference = 0; + int lastPixel, currentPixel, transitionsSeen; lastPixel = -1; transitionsSeen = 0; //Starting near the left edge, step right, counting transitions. If there are 4, it is the marker (what about 3?) for(int i = 30; i < 98; i++) { - currentPixel = (int8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF; + currentPixel = (int)(CLOSE_CAMERA[i] >> 4) & 0xFF; difference = lastPixel - currentPixel; - if(abs(difference) > 150 && lastPixel != -1){ //transition seen, increment counter + if(abs(difference) > 20 && lastPixel != -1){ //transition seen, increment counter transitionsSeen++; + i+=5; } lastPixel = currentPixel; } - if (slower%100 == 0) { - sendString("Transitions seen: %d", transitionsSeen); - } - //if(transitionsSeen >= 3) { + //if (slower % 1000 == 0) { + //sendString("Transitions seen: %d", transitionsSeen); + //} + if(transitionsSeen >= 5) { //Stop the car! - //sendString("Start/stop seen"); - //TFC_SetMotorPWM(0.f,0.f); - //TFC_HBRIDGE_DISABLE; - //} + sendString("Start/stop seen"); + TFC_SetMotorPWM(0.f,0.f); + TFC_HBRIDGE_DISABLE; + } transitionsSeen = 0; - slower++; - */ + // slower++; + }