Six crescent shaped legs

Dependencies:   mbed

Committer:
phairero
Date:
Mon Mar 28 12:23:38 2016 +0000
Revision:
1:8b0322a353f4
Parent:
0:0b7259fdb68a
Child:
2:cf0147952fb9
Mootori veaparandus

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phairero 0:0b7259fdb68a 1 #include "mbed.h"
phairero 0:0b7259fdb68a 2 #include "time.h"
phairero 0:0b7259fdb68a 3
phairero 0:0b7259fdb68a 4 Timer timer;
phairero 0:0b7259fdb68a 5 PwmOut mypwm(PB_0);
phairero 0:0b7259fdb68a 6 DigitalOut dir1(PC_0);
phairero 0:0b7259fdb68a 7 DigitalOut dir2(PC_1);
phairero 0:0b7259fdb68a 8 InterruptIn encA(PA_0);
phairero 1:8b0322a353f4 9 //InterruptIn encB(PA_1);
phairero 0:0b7259fdb68a 10 InterruptIn bt(USER_BUTTON);
phairero 0:0b7259fdb68a 11 Serial pc(SERIAL_TX, SERIAL_RX);
phairero 0:0b7259fdb68a 12
phairero 0:0b7259fdb68a 13 int iAeg = 0;
phairero 1:8b0322a353f4 14 //int iDir;
phairero 1:8b0322a353f4 15 //int iTime = 0;
phairero 1:8b0322a353f4 16 unsigned long long int iSum = 0;
phairero 1:8b0322a353f4 17 double dPwm = 0.2;
phairero 1:8b0322a353f4 18 double newPwm = dPwm;
phairero 1:8b0322a353f4 19 double miss = 0;
phairero 1:8b0322a353f4 20 double lastMiss = 0;
phairero 1:8b0322a353f4 21 double chPwm = 0;
phairero 1:8b0322a353f4 22 double totMiss = 0;
phairero 1:8b0322a353f4 23 double minPwm = 0.1;
phairero 1:8b0322a353f4 24 double maxPwm = 0.7;
phairero 1:8b0322a353f4 25 double dSpeed = 0.5;
phairero 0:0b7259fdb68a 26
phairero 1:8b0322a353f4 27
phairero 1:8b0322a353f4 28 /*int getDir(){
phairero 1:8b0322a353f4 29 if ((dAeg/(double)iDir > 0.5){
phairero 1:8b0322a353f4 30 return (1);}
phairero 1:8b0322a353f4 31 else return 0;
phairero 1:8b0322a353f4 32 }*/
phairero 0:0b7259fdb68a 33 void start(){
phairero 0:0b7259fdb68a 34 timer.reset();
phairero 0:0b7259fdb68a 35 }
phairero 1:8b0322a353f4 36 /*void dir(){
phairero 1:8b0322a353f4 37 iDir = timer.read_us();
phairero 1:8b0322a353f4 38 }*/
phairero 0:0b7259fdb68a 39 void stop(){
phairero 0:0b7259fdb68a 40 iAeg = timer.read_us();
phairero 1:8b0322a353f4 41 //iDir = getDir();
phairero 0:0b7259fdb68a 42 }
phairero 0:0b7259fdb68a 43
phairero 0:0b7259fdb68a 44 int main(){
phairero 1:8b0322a353f4 45 timer.start();
phairero 0:0b7259fdb68a 46 encA.rise(&start);
phairero 0:0b7259fdb68a 47 encA.fall(&stop);
phairero 1:8b0322a353f4 48 //encB.rise(&dir);
phairero 1:8b0322a353f4 49 double real = (1-dSpeed)*1000;
phairero 0:0b7259fdb68a 50 mypwm.write(dPwm);
phairero 0:0b7259fdb68a 51 dir1 = 1;
phairero 0:0b7259fdb68a 52 dir2 = 0;
phairero 1:8b0322a353f4 53 while(1){
phairero 1:8b0322a353f4 54 if (iAeg != 0){
phairero 1:8b0322a353f4 55 miss = iAeg - real;
phairero 1:8b0322a353f4 56 totMiss = totMiss + miss;
phairero 1:8b0322a353f4 57 chPwm = totMiss/200000;
phairero 1:8b0322a353f4 58 if (chPwm > -0.0001 && chPwm < 0.0001) {
phairero 1:8b0322a353f4 59 pc.printf("%f,%f\n",totMiss,chPwm);}
phairero 1:8b0322a353f4 60 newPwm = dPwm + chPwm;
phairero 1:8b0322a353f4 61 if (newPwm < minPwm){
phairero 1:8b0322a353f4 62 newPwm = minPwm;
phairero 1:8b0322a353f4 63 }
phairero 1:8b0322a353f4 64 else if (newPwm > maxPwm){
phairero 1:8b0322a353f4 65 newPwm = maxPwm;
phairero 1:8b0322a353f4 66 }
phairero 1:8b0322a353f4 67 mypwm.write(newPwm);
phairero 1:8b0322a353f4 68 iAeg = 0;
phairero 0:0b7259fdb68a 69 }
phairero 0:0b7259fdb68a 70 }
phairero 1:8b0322a353f4 71 }