E=MC / Mbed 2 deprecated linecam_practice

Dependencies:   mbed

main.cpp

Committer:
mawk2311
Date:
2015-03-20
Revision:
7:f7f4308c7ac0
Parent:
6:53d521496469

File content as of revision 7:f7f4308c7ac0:

#include "mbed.h"
#include "stdlib.h"

DigitalOut led1(LED1);
DigitalOut clk(PTD5);
DigitalOut si(PTD4);
AnalogIn camData(PTC2);
PwmOut servo(PTA5);
Serial pc(USBTX, USBRX); // tx, rx

float ADCdata [128];
float slopeAccum;
float slopeCount;
float approxPos;
float minVal;

int minLoc;

float straight = 0.00155f;
float hardLeft = 0.0013f;
float slightLeft = 0.00145f;
float hardRight = 0.0018f;
float slightRight = 0.00165f;

float currDirection = straight;

int main() {
    //servo.period(SERVO_FREQ);
    int integrationCounter = 0;
    
    while(1) {
            
        if(integrationCounter % 151== 0){
            //__disable_irq
            si = 1;
            clk = 1;
            //wait(.00001);
            si = 0;
            clk = 0;
            integrationCounter = 0;
            
            slopeAccum = 0;
            slopeCount = 0;
            approxPos = 0;
                
        }
        else if (integrationCounter > 129){
            minVal = ADCdata[15];
            for (int c = 15; c < 118; c++) {
                if (ADCdata[c] < minVal){
                    minVal = ADCdata[c];
                    minLoc = c;
                }
            }
            
            for (int c = 15; c < 118; c++) {
                if (ADCdata[c] >= minVal && ADCdata[c] - minVal < 0.04f && ADCdata[c] > 0.1f){
                    slopeAccum += c;
                    slopeCount++;
                }
            }
            
            approxPos = (float)slopeAccum/(float)slopeCount;
            
            if(approxPos > 0 && approxPos <= 20){
                    servo.pulsewidth(hardLeft);
            } else if (approxPos > 20 && approxPos <= 45){
                    servo.pulsewidth(slightLeft);
            } else if (approxPos > 45 && approxPos <= 90){
                servo.pulsewidth(straight);
            } else if (approxPos > 90 && approxPos <= 105){
                servo.pulsewidth(slightRight);
            } else if (approxPos > 105 && approxPos <= 128){
                    servo.pulsewidth(hardRight);
            }
            integrationCounter = 150;
        }
        else{
            clk = 1;
            wait_us(70);
            ADCdata[integrationCounter - 1] = camData;
            clk = 0;
        }

        //clk = 0;
        integrationCounter++;
        //camData.
        
    }
}