Car 2: Electric Boogaloo

Dependencies:   camera mbed

Fork of Car2 by NXP Group 13

Code for an NXP Cup car using a linescan Camera

main.cpp

Committer:
zamatthews
Date:
2017-03-02
Revision:
5:137dfb3e692f
Parent:
4:f4852befd69c
Child:
6:971236e48adc

File content as of revision 5:137dfb3e692f:

#include "mbed.h"
#include "Servo.h"

//#define CIRCUMFERENCE SOMEINT //circumference in some unit of measurement (inches perhaps?)
//#define RESWIDTH 80  //resolution width/height. change these based on actual resolution
//#define RESHEIGHT 60
#define STRAIGHT 0.00095f
#define FULLRIGHT 0.0013f
#define FULLLEFT 0.0006

//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?
//Timer timer;
//DigitalIn rotation(PXXX);
//camera data pins used are (D0-D7): PTC7, PTC0, PTC3, PTC4, PTC5, PTC6, PTC10, PTC11
//other camera pins: SCL->PTE5, SDA->PTE4, PCLK->PTE21, VSYNC->PTE30, H->PTE29
PwmOut servo(PTE20);
PwmOut motor_left(PTA5);
PwmOut motor_right(PTC8);
DigitalOut DIR_L(PTD4);
DigitalOut DIR_R(PTA4);

//int start = 0;
//int end = 0;
//int rotationTime = 0;
//float speed = 0;
//int startFinish = 0;
//int frame[RESWIDTH][RESHEIGHT];

/*
int frame(){ //triggered by interrupt from camera or request frame from camera?
    //get all the pixels
    int i = 0;
    int j = 0;
    for(i; i<RESWIDTH; i++){
        for(j; j<RESHEIGHT; j++){
            frame[i][j] = ???;
        }//j for
    }//i for
    return frame;
}
*/

void setAccel(float turnAngle){//, float speed){
    //ififififififif
    turnAngle -= STRAIGHT; //this gets a value from -0.00035 and +0.00035
    turnAngle = abs(turnAngle);
    float turnRatio = turnAngle/0.00035f;
    float newSpeed = (0.7*(1-turnRatio)/4)+0.3;
    motor_left.write(newSpeed);
    motor_right.write(newSpeed);
}

void turnWheels(){//int[][] frame, float speed){
    //ififififififif
    for(float p=FULLLEFT; p<=FULLRIGHT; p += 0.00005f) {
        servo.pulsewidth(p);
        setAccel(p);
        wait(.1);
    }//for p increase
    for(float p=FULLRIGHT; p>=FULLLEFT; p -= 0.00005f) {
        servo.pulsewidth(p);
        setAccel(p);
        wait(.1);
    }//for p decrease
}


/*
int startFinish(int[][] frame){
    //if start
    return 1;
    //if notFinished
    return 1;
    //if finished
    return 0;
}
*/
 
int main() {    
    //timer.start();
    motor_left.period_us(50);
    motor_right.period_us(50);
    //motor.write(0.25);
    DIR_R = 1;
    DIR_L = 0;
    servo.period(0.020f);
    //servo.pulsewidth(STRAIGHT);
    while(1){
        /*
        if(rotation){
            end = time.read_ms();
            rotationTime = end-start;
            speed = CIRCUFERENCE / rotationTime; //inches/ms
            speed *= 56.8182 //convert to miles/hour
            start = timer.read_ms(); 
        }
        //camera.rise(&frame); //idea is that camera tells MCU that it's sending a frame
        //frame = frame();
        if(startFinish(frame)){; //frame returns the result 
        */
            turnWheels();//frame, speed); //includes setAccel();
            //setAccel(frame, speed);
        //} //end if(startFinish(frame))        
    }//while 1
}//main