NXP Group 13 / Mbed 2 deprecated Car3

Dependencies:   camera mbed tsi_sensor

Fork of Car2 by Zach Matthews

Committer:
zamatthews
Date:
Wed Mar 22 20:19:03 2017 +0000
Revision:
13:9175be9c2b9e
Parent:
12:4ccf304800fe
Child:
14:c6f0a3c4e222
small changes to the way the timer works

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 4:f4852befd69c 3 #define STRAIGHT 0.00095f
lmstthomas 4:f4852befd69c 4 #define FULLRIGHT 0.0013f
lmstthomas 4:f4852befd69c 5 #define FULLLEFT 0.0006
zamatthews 12:4ccf304800fe 6 #define MIN_TURN_RATIO 0.3
zamatthews 12:4ccf304800fe 7 #define MAX_TURN_RATIO 0.7
zamatthews 12:4ccf304800fe 8 #define MIN_SPEED 0.1
zamatthews 12:4ccf304800fe 9 #define MAX_SPEED 0.6
zamatthews 13:9175be9c2b9e 10 #define TURN_TIME 50
zamatthews 3:dadfc15fc2d1 11
zamatthews 3:dadfc15fc2d1 12 PwmOut servo(PTE20);
zamatthews 5:137dfb3e692f 13 PwmOut motor_left(PTA5);
zamatthews 5:137dfb3e692f 14 PwmOut motor_right(PTC8);
zamatthews 3:dadfc15fc2d1 15 DigitalOut DIR_L(PTD4);
zamatthews 3:dadfc15fc2d1 16 DigitalOut DIR_R(PTA4);
zamatthews 9:644102f863a5 17 Serial pc(USBTX, USBRX);
zamatthews 9:644102f863a5 18 Camera cam(PTE23, PTE21, PTB3);
zamatthews 11:45f345aad8ba 19 int turnCounter = 0;
zamatthews 11:45f345aad8ba 20 float wheelPos = STRAIGHT;
zamatthews 0:b761ef827157 21
zamatthews 12:4ccf304800fe 22 /*
zamatthews 12:4ccf304800fe 23 Function: setAccel
zamatthews 12:4ccf304800fe 24 Description: Sets the speed for the right and left motors individually based
zamatthews 12:4ccf304800fe 25 on the turning angle.
zamatthews 12:4ccf304800fe 26 */
lmstthomas 4:f4852befd69c 27 void setAccel(float turnAngle){//, float speed){
lmstthomas 4:f4852befd69c 28 turnAngle -= STRAIGHT; //this gets a value from -0.00035 and +0.00035
zamatthews 9:644102f863a5 29 float turnRatio = abs(turnAngle)/0.00035f;
zamatthews 12:4ccf304800fe 30 float newSpeed = ((MAX_SPEED - MIN_SPEED)*(1-turnRatio)/4)+MIN_SPEED;
zamatthews 9:644102f863a5 31 motor_left.write(newSpeed + 0.5 * newSpeed * (turnAngle / .00035f));
zamatthews 9:644102f863a5 32 motor_right.write(newSpeed - 0.5 * newSpeed * (turnAngle / .00035f));
zamatthews 12:4ccf304800fe 33 }//end setAccel
zamatthews 3:dadfc15fc2d1 34
zamatthews 12:4ccf304800fe 35 /*
zamatthews 12:4ccf304800fe 36 Function: turnWheels
zamatthews 12:4ccf304800fe 37 Description: Turns the wheels in order to stay between two black lines seen
zamatthews 12:4ccf304800fe 38 by the camera
zamatthews 12:4ccf304800fe 39 */
zamatthews 10:246782426144 40 void turnWheels(int frame[]){ //int[][] frame, float speed){
zamatthews 10:246782426144 41 int positionSum = 0;
zamatthews 10:246782426144 42 int numDarks = 0;
zamatthews 10:246782426144 43 for(int i = 0; i < 128; i++){
zamatthews 11:45f345aad8ba 44 if(frame[i] < 65){
zamatthews 10:246782426144 45 positionSum += i;
zamatthews 10:246782426144 46 numDarks++;
zamatthews 10:246782426144 47 }
zamatthews 10:246782426144 48 }
zamatthews 10:246782426144 49 float averagePos = 0;
zamatthews 12:4ccf304800fe 50
zamatthews 13:9175be9c2b9e 51 if (numDarks == 0 && turnCounter >= (TURN_TIME / 2)) {
zamatthews 11:45f345aad8ba 52 wheelPos = STRAIGHT;
zamatthews 11:45f345aad8ba 53 }
zamatthews 12:4ccf304800fe 54
zamatthews 12:4ccf304800fe 55 else {
zamatthews 12:4ccf304800fe 56 averagePos = positionSum / numDarks;
zamatthews 12:4ccf304800fe 57 if(averagePos <= 64 && ((wheelPos >= STRAIGHT) || turnCounter >= TURN_TIME)){
zamatthews 12:4ccf304800fe 58 float powerRatio = (averagePos / 64) * MAX_TURN_RATIO + MIN_TURN_RATIO;
zamatthews 12:4ccf304800fe 59 if(averagePos >= 45) powerRatio = 1;
zamatthews 12:4ccf304800fe 60 wheelPos = STRAIGHT + (FULLRIGHT - STRAIGHT) * powerRatio;
zamatthews 12:4ccf304800fe 61 turnCounter = 0;
zamatthews 11:45f345aad8ba 62 }
zamatthews 12:4ccf304800fe 63
zamatthews 12:4ccf304800fe 64 else if(averagePos >= 66 && (wheelPos <= STRAIGHT || turnCounter >= TURN_TIME)){
zamatthews 12:4ccf304800fe 65 float powerRatio = (1 - (averagePos - 64) / 64) * MAX_TURN_RATIO + MIN_TURN_RATIO;
zamatthews 12:4ccf304800fe 66 if(averagePos <= 85) powerRatio = 1;
zamatthews 12:4ccf304800fe 67 wheelPos = STRAIGHT - (STRAIGHT - FULLLEFT) * powerRatio;
zamatthews 12:4ccf304800fe 68 turnCounter = 0;
zamatthews 11:45f345aad8ba 69 }
zamatthews 12:4ccf304800fe 70 turnCounter++;
zamatthews 11:45f345aad8ba 71 }
zamatthews 12:4ccf304800fe 72
zamatthews 10:246782426144 73 servo.pulsewidth(wheelPos);
zamatthews 10:246782426144 74 setAccel(wheelPos);
zamatthews 3:dadfc15fc2d1 75 }
zamatthews 3:dadfc15fc2d1 76
zamatthews 12:4ccf304800fe 77 void display(int frame[]){
zamatthews 12:4ccf304800fe 78 char draw = 'x';
zamatthews 12:4ccf304800fe 79 for(int i = 0; i< 128; i++){
zamatthews 12:4ccf304800fe 80 if (frame[i] <65) draw = '|';
zamatthews 12:4ccf304800fe 81 else draw = '-';
zamatthews 12:4ccf304800fe 82 pc.printf("%c", draw);
zamatthews 12:4ccf304800fe 83 draw = 'x';
zamatthews 12:4ccf304800fe 84 }
zamatthews 12:4ccf304800fe 85 pc.printf("\r");
zamatthews 3:dadfc15fc2d1 86 }
zamatthews 2:0db7cc5ad6db 87
zamatthews 2:0db7cc5ad6db 88 int main() {
zamatthews 5:137dfb3e692f 89 motor_left.period_us(50);
zamatthews 5:137dfb3e692f 90 motor_right.period_us(50);
zamatthews 5:137dfb3e692f 91 DIR_R = 1;
zamatthews 2:0db7cc5ad6db 92 DIR_L = 0;
zamatthews 3:dadfc15fc2d1 93 servo.period(0.020f);
zamatthews 12:4ccf304800fe 94
zamatthews 2:0db7cc5ad6db 95 while(1){
zamatthews 9:644102f863a5 96 wait_ms(10);
zamatthews 9:644102f863a5 97 cam.capture();
zamatthews 12:4ccf304800fe 98 //display(cam.imageData);
zamatthews 12:4ccf304800fe 99 turnWheels(cam.imageData);
zamatthews 12:4ccf304800fe 100 setAccel(wheelPos);
zamatthews 12:4ccf304800fe 101 }
zamatthews 12:4ccf304800fe 102 }