Six crescent shaped legs

Dependencies:   mbed

main.cpp

Committer:
phairero
Date:
2016-03-28
Revision:
1:8b0322a353f4
Parent:
0:0b7259fdb68a
Child:
2:cf0147952fb9

File content as of revision 1:8b0322a353f4:

#include "mbed.h"
#include "time.h"

Timer timer;
PwmOut mypwm(PB_0);
DigitalOut dir1(PC_0);
DigitalOut dir2(PC_1);
InterruptIn encA(PA_0);
//InterruptIn encB(PA_1);
InterruptIn bt(USER_BUTTON);
Serial pc(SERIAL_TX, SERIAL_RX);

int iAeg = 0;
//int iDir;
//int iTime = 0;
unsigned long long int iSum = 0;
double dPwm = 0.2;
double newPwm = dPwm;
double miss = 0;
double lastMiss = 0;
double chPwm = 0;
double totMiss = 0;
double minPwm = 0.1;
double maxPwm = 0.7;
double dSpeed = 0.5;


/*int getDir(){
    if ((dAeg/(double)iDir > 0.5){
        return (1);}
    else return 0;
    }*/
void start(){
    timer.reset();
    }
/*void dir(){
    iDir = timer.read_us();
    }*/
void stop(){
    iAeg = timer.read_us();
    //iDir = getDir();
    }

int main(){
    timer.start();
    encA.rise(&start);
    encA.fall(&stop);
    //encB.rise(&dir);
    double real = (1-dSpeed)*1000;
    mypwm.write(dPwm);
    dir1 = 1;
    dir2 = 0;
    while(1){
        if (iAeg != 0){
                miss = iAeg - real;
                totMiss = totMiss + miss;
                chPwm = totMiss/200000;
                if (chPwm > -0.0001 && chPwm < 0.0001) {
                    pc.printf("%f,%f\n",totMiss,chPwm);}
                newPwm = dPwm + chPwm;
                if (newPwm < minPwm){
                    newPwm = minPwm;
                    }
                else if (newPwm > maxPwm){
                    newPwm = maxPwm;
                    }
                mypwm.write(newPwm);
                iAeg = 0;
            }
        }
    }