NXP Group 13 / Mbed 2 deprecated Car3

Dependencies:   camera mbed tsi_sensor

Fork of Car2 by Zach Matthews

Committer:
zamatthews
Date:
Tue Mar 14 22:10:32 2017 +0000
Revision:
10:246782426144
Parent:
9:644102f863a5
Child:
11:45f345aad8ba
turn away from lines to stay between two lines;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zamatthews 0:b761ef827157 1 #include "mbed.h"
zamatthews 2:0db7cc5ad6db 2 #include "Servo.h"
zamatthews 6:971236e48adc 3 #include "Camera.h"
zamatthews 2:0db7cc5ad6db 4
zamatthews 3:dadfc15fc2d1 5 //#define CIRCUMFERENCE SOMEINT //circumference in some unit of measurement (inches perhaps?)
zamatthews 3:dadfc15fc2d1 6 //#define RESWIDTH 80 //resolution width/height. change these based on actual resolution
zamatthews 3:dadfc15fc2d1 7 //#define RESHEIGHT 60
lmstthomas 4:f4852befd69c 8 #define STRAIGHT 0.00095f
lmstthomas 4:f4852befd69c 9 #define FULLRIGHT 0.0013f
lmstthomas 4:f4852befd69c 10 #define FULLLEFT 0.0006
zamatthews 3:dadfc15fc2d1 11
zamatthews 3:dadfc15fc2d1 12 //InterruptIn camera(PXXX); //can set up to get a frame from the camera whenever is sends one? does the camera even have a pin for this?
zamatthews 3:dadfc15fc2d1 13 //Timer timer;
zamatthews 3:dadfc15fc2d1 14 //DigitalIn rotation(PXXX);
zamatthews 3:dadfc15fc2d1 15 //camera data pins used are (D0-D7): PTC7, PTC0, PTC3, PTC4, PTC5, PTC6, PTC10, PTC11
zamatthews 3:dadfc15fc2d1 16 //other camera pins: SCL->PTE5, SDA->PTE4, PCLK->PTE21, VSYNC->PTE30, H->PTE29
zamatthews 3:dadfc15fc2d1 17 PwmOut servo(PTE20);
zamatthews 5:137dfb3e692f 18 PwmOut motor_left(PTA5);
zamatthews 5:137dfb3e692f 19 PwmOut motor_right(PTC8);
zamatthews 3:dadfc15fc2d1 20 DigitalOut DIR_L(PTD4);
zamatthews 3:dadfc15fc2d1 21 DigitalOut DIR_R(PTA4);
zamatthews 9:644102f863a5 22 Serial pc(USBTX, USBRX);
zamatthews 9:644102f863a5 23 Camera cam(PTE23, PTE21, PTB3);
zamatthews 3:dadfc15fc2d1 24
zamatthews 3:dadfc15fc2d1 25 //int start = 0;
zamatthews 3:dadfc15fc2d1 26 //int end = 0;
zamatthews 3:dadfc15fc2d1 27 //int rotationTime = 0;
zamatthews 3:dadfc15fc2d1 28 //float speed = 0;
zamatthews 3:dadfc15fc2d1 29 //int startFinish = 0;
zamatthews 0:b761ef827157 30
zamatthews 3:dadfc15fc2d1 31
lmstthomas 4:f4852befd69c 32 void setAccel(float turnAngle){//, float speed){
zamatthews 3:dadfc15fc2d1 33 //ififififififif
lmstthomas 4:f4852befd69c 34 turnAngle -= STRAIGHT; //this gets a value from -0.00035 and +0.00035
zamatthews 9:644102f863a5 35 float turnRatio = abs(turnAngle)/0.00035f;
zamatthews 9:644102f863a5 36 float newSpeed = (0.2*(1-turnRatio)/4)+0.15; //range of 0.15 - 0.35
zamatthews 9:644102f863a5 37 motor_left.write(newSpeed + 0.5 * newSpeed * (turnAngle / .00035f));
zamatthews 9:644102f863a5 38 motor_right.write(newSpeed - 0.5 * newSpeed * (turnAngle / .00035f));
zamatthews 3:dadfc15fc2d1 39 }
zamatthews 3:dadfc15fc2d1 40
zamatthews 10:246782426144 41 void turnWheels(int frame[]){ //int[][] frame, float speed){
zamatthews 10:246782426144 42 int positionSum = 0;
zamatthews 10:246782426144 43 int numDarks = 0;
zamatthews 10:246782426144 44 for(int i = 0; i < 128; i++){
zamatthews 10:246782426144 45 //pc.printf("%d ", cam.imageData[i]);
zamatthews 10:246782426144 46 if(frame[i] < 80){
zamatthews 10:246782426144 47 positionSum += i;
zamatthews 10:246782426144 48 numDarks++;
zamatthews 10:246782426144 49 }
zamatthews 10:246782426144 50 }
zamatthews 10:246782426144 51 //pc.printf("\n\n\r\r");
zamatthews 10:246782426144 52 float averagePos = 0;
zamatthews 10:246782426144 53 if(numDarks > 0) averagePos = positionSum / numDarks;
zamatthews 10:246782426144 54 else averagePos = -1;
zamatthews 10:246782426144 55 float wheelPos = STRAIGHT;
zamatthews 10:246782426144 56 float turnPercent = ((averagePos - 64.0)/64.0);
zamatthews 10:246782426144 57 if (turnPercent > 0) turnPercent = 1-turnPercent;
zamatthews 10:246782426144 58 else turnPercent = (1 + turnPercent) * -1;
zamatthews 10:246782426144 59
zamatthews 10:246782426144 60 //follow right line
zamatthews 10:246782426144 61 //(amount from full right to straight) * (% distance black line away from middle of camera's view) + STRAIGHT
zamatthews 10:246782426144 62 //basically figures out how much to turn away from straight to follow line
zamatthews 10:246782426144 63 // float wheelPos = (FULLRIGHT - STRAIGHT) * ((averagePos - 64.0) / 64.0) + STRAIGHT;
zamatthews 10:246782426144 64
zamatthews 10:246782426144 65 //stay between lines
zamatthews 10:246782426144 66 if (averagePos < 0) { //no darks found, go straight
zamatthews 10:246782426144 67 wheelPos = STRAIGHT;
zamatthews 10:246782426144 68 }
zamatthews 10:246782426144 69 else { //otherwise darks found, turn away
zamatthews 10:246782426144 70 wheelPos = STRAIGHT - (FULLRIGHT - STRAIGHT) * turnPercent;
zamatthews 10:246782426144 71 }
zamatthews 10:246782426144 72 //pc.printf("averagePos: %f \n\n\r\r wheelPos: %f \n\n\r\r", averagePos, wheelPos);
zamatthews 10:246782426144 73 //turn/change speed
zamatthews 10:246782426144 74 servo.pulsewidth(wheelPos);
zamatthews 10:246782426144 75 setAccel(wheelPos);
zamatthews 3:dadfc15fc2d1 76 //ififififififif
zamatthews 10:246782426144 77 // for(float p=FULLLEFT; p<=FULLRIGHT; p += 0.00005f) {
zamatthews 10:246782426144 78 // servo.pulsewidth(p);
zamatthews 10:246782426144 79 // setAccel(p);
zamatthews 10:246782426144 80 // wait(.1);
zamatthews 10:246782426144 81 // }//for p increase
zamatthews 10:246782426144 82 // for(float p=FULLRIGHT; p>=FULLLEFT; p -= 0.00005f) {
zamatthews 10:246782426144 83 // servo.pulsewidth(p);
zamatthews 10:246782426144 84 // setAccel(p);
zamatthews 10:246782426144 85 // wait(.1);
zamatthews 10:246782426144 86 // }//for p decrease
zamatthews 3:dadfc15fc2d1 87 }
zamatthews 3:dadfc15fc2d1 88
lmstthomas 4:f4852befd69c 89
lmstthomas 4:f4852befd69c 90 /*
zamatthews 3:dadfc15fc2d1 91 int startFinish(int[][] frame){
zamatthews 3:dadfc15fc2d1 92 //if start
zamatthews 3:dadfc15fc2d1 93 return 1;
zamatthews 3:dadfc15fc2d1 94 //if notFinished
zamatthews 3:dadfc15fc2d1 95 return 1;
zamatthews 3:dadfc15fc2d1 96 //if finished
zamatthews 3:dadfc15fc2d1 97 return 0;
zamatthews 3:dadfc15fc2d1 98 }
zamatthews 3:dadfc15fc2d1 99 */
zamatthews 2:0db7cc5ad6db 100
zamatthews 2:0db7cc5ad6db 101 int main() {
zamatthews 3:dadfc15fc2d1 102 //timer.start();
zamatthews 5:137dfb3e692f 103 motor_left.period_us(50);
zamatthews 5:137dfb3e692f 104 motor_right.period_us(50);
lmstthomas 4:f4852befd69c 105 //motor.write(0.25);
zamatthews 5:137dfb3e692f 106 DIR_R = 1;
zamatthews 2:0db7cc5ad6db 107 DIR_L = 0;
zamatthews 3:dadfc15fc2d1 108 servo.period(0.020f);
lmstthomas 4:f4852befd69c 109 //servo.pulsewidth(STRAIGHT);
zamatthews 2:0db7cc5ad6db 110 while(1){
zamatthews 9:644102f863a5 111 wait_ms(10);
zamatthews 9:644102f863a5 112 cam.capture();
zamatthews 10:246782426144 113 turnWheels(cam.imageData);
zamatthews 10:246782426144 114
zamatthews 9:644102f863a5 115
zamatthews 9:644102f863a5 116 //pc.printf("%f\n\n\r\r", wheelPos);
zamatthews 9:644102f863a5 117
zamatthews 9:644102f863a5 118
zamatthews 3:dadfc15fc2d1 119 /*
zamatthews 3:dadfc15fc2d1 120 if(rotation){
zamatthews 3:dadfc15fc2d1 121 end = time.read_ms();
zamatthews 3:dadfc15fc2d1 122 rotationTime = end-start;
zamatthews 3:dadfc15fc2d1 123 speed = CIRCUFERENCE / rotationTime; //inches/ms
zamatthews 3:dadfc15fc2d1 124 speed *= 56.8182 //convert to miles/hour
zamatthews 3:dadfc15fc2d1 125 start = timer.read_ms();
zamatthews 3:dadfc15fc2d1 126 }
zamatthews 3:dadfc15fc2d1 127 //camera.rise(&frame); //idea is that camera tells MCU that it's sending a frame
zamatthews 3:dadfc15fc2d1 128 //frame = frame();
zamatthews 3:dadfc15fc2d1 129 if(startFinish(frame)){; //frame returns the result
zamatthews 3:dadfc15fc2d1 130 */
zamatthews 9:644102f863a5 131 // turnWheels();//frame, speed); //includes setAccel();
lmstthomas 4:f4852befd69c 132 //setAccel(frame, speed);
zamatthews 3:dadfc15fc2d1 133 //} //end if(startFinish(frame))
zamatthews 3:dadfc15fc2d1 134 }//while 1
zamatthews 3:dadfc15fc2d1 135 }//main