car using PID from centre line
Dependencies: FRDM-TFC mbed CBuffer XBEE mbed_angular_speed motor2 MMA8451Q
Fork of KL25Z_Camera_Test by
main.cpp
- Committer:
- maximusismax
- Date:
- 2016-10-28
- Revision:
- 2:4b6f6fc84793
- Parent:
- 1:a4883d9c75ec
- Child:
- 3:87a5122682fa
File content as of revision 2:4b6f6fc84793:
#include "mbed.h" #include "TFC.h" #define CAM_THRESHOLD 3000 Serial pc(USBTX,USBRX); DigitalOut myled(LED1); int main() { //Serial config pc.baud(57600); TFC_Init(); uint32_t i,t = 0; //uint32_t width = 0; char curr_line[129]; int leftSide, rightSide; float centreOffset; /* Ensure string is null terminated */ curr_line[128] = 0; char thres_line[16]; for(i = 0; i < 16; i++) { thres_line[i] = 0; } pc.printf("Starting camera test program\n"); while(1) { //If we have an image ready if(TFC_LineScanImageReady>0) { //Reset image ready flag TFC_LineScanImageReady=0; pc.printf("\r\n"); //pc.printf("L:"); //Strange thing that cycles the battery indicator //Leaving in as this is a good indicator that the camera test program is running if(t==4) t=0; else t++; TFC_SetBatteryLED_Level(t); //Byte sending //for(i = 0; i < 128; i++) { //curr_line[i] = (int8_t)(TFC_LineScanImage0[i] >> 4) & 0xFF; //pc.printf("%02x", curr_line[i]); //} pc.printf("\n"); //NEW CENTRE DETECTION CODE //Centre of track detection //First, start at the centre of the 128 bit array //Moving left, start at 63 and move 63 steps left (to get to 0) //Moving right, start at 64 and move 63 steps right (to get to 127) //Move leftwards, stopping where we reach the edge of the track, and record the index where this occurred for (i = 63; i > 0; i--) { if(TFC_LineScanImage0[i] < CAM_THRESHOLD) { leftSide = i; break; } } //Then, move rightwards, stopping where we reach the edge of the track, record the index where this occurred for (i = 64; i < 127; i++) { if(TFC_LineScanImage0[i] < CAM_THRESHOLD) { rightSide = i; break; } } //centreOffset = (leftSide + rightSide)/2; centreOffset = (leftSide - rightSide) / 127; //Send offset over serial to hosted program pc.putc('E'); pc.printf("%f", centreOffset); //pc.printf("centre: %d\n", centreOffset); ///END OF NEW CODE //Old version below, we don't threshold on the FRDM board anymore //pc.printf("0x%x\n", curr_line); //Loop through camera pixels for(i=0;i<128;i++) { //If the pixel value exceeds our threshold, print a 1 if (TFC_LineScanImage0[i] > CAM_THRESHOLD) { //curr_line[i] = '1'; thres_line[i/8] |= 1 << (i%8); } //Else, print a 0 else { //curr_line[i] = '0'; } } pc.putc('I'); for(i = 0; i < 16; i++) { pc.putc(thres_line[i]); } //pc.printf("Width: %d\n", width); //width = 0; } } }