Code for SLOPE drawbar pull

Dependencies:   MODSERIAL QEI mbed

Committer:
johnson6987
Date:
Mon Jul 07 16:11:23 2014 +0000
Revision:
0:0734b922c101
Slope Draw Bar Pull

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnson6987 0:0734b922c101 1 #include "mbed.h"
johnson6987 0:0734b922c101 2 #include "QEI.h"
johnson6987 0:0734b922c101 3 #include "MODSERIAL.h"
johnson6987 0:0734b922c101 4
johnson6987 0:0734b922c101 5 #define MCONV 10900
johnson6987 0:0734b922c101 6
johnson6987 0:0734b922c101 7 AnalogOut tri(p18);
johnson6987 0:0734b922c101 8 DigitalIn Enable(p10);
johnson6987 0:0734b922c101 9 MODSERIAL pc(USBTX,USBRX);
johnson6987 0:0734b922c101 10 QEI wheel (p24, p25,NC, 1800);
johnson6987 0:0734b922c101 11 AnalogIn load(p20);
johnson6987 0:0734b922c101 12 unsigned short value = 0x0500;
johnson6987 0:0734b922c101 13 float Set=25;
johnson6987 0:0734b922c101 14 LocalFileSystem local("local");
johnson6987 0:0734b922c101 15 Timer SystemTime;
johnson6987 0:0734b922c101 16 Timer LogTime;
johnson6987 0:0734b922c101 17 Timer LoopTime;
johnson6987 0:0734b922c101 18 Timer SampleTime;
johnson6987 0:0734b922c101 19 BusOut leds(LED1, LED2, LED3, LED4);
johnson6987 0:0734b922c101 20 float errorup=1;
johnson6987 0:0734b922c101 21 float errorlow=-1;
johnson6987 0:0734b922c101 22
johnson6987 0:0734b922c101 23 int main()
johnson6987 0:0734b922c101 24 {
johnson6987 0:0734b922c101 25 pc.printf("alive");
johnson6987 0:0734b922c101 26 Enable.mode(PullDown);
johnson6987 0:0734b922c101 27 int filenumber=0;
johnson6987 0:0734b922c101 28 bool FLAG =0;
johnson6987 0:0734b922c101 29 char filename[22];
johnson6987 0:0734b922c101 30 leds=0x1;
johnson6987 0:0734b922c101 31 pc.printf("alsoHere");
johnson6987 0:0734b922c101 32 Set=30;
johnson6987 0:0734b922c101 33 value=0x0500;
johnson6987 0:0734b922c101 34 while(1) {
johnson6987 0:0734b922c101 35 while(!FLAG) {
johnson6987 0:0734b922c101 36 sprintf(filename,"/local/data%04d.txt",filenumber++);
johnson6987 0:0734b922c101 37 FILE *SetupFile= fopen(filename, "r");
johnson6987 0:0734b922c101 38 if (SetupFile != NULL) fclose(SetupFile);
johnson6987 0:0734b922c101 39 else FLAG=1;
johnson6987 0:0734b922c101 40 }
johnson6987 0:0734b922c101 41 leds=0x2;
johnson6987 0:0734b922c101 42 tri.write_u16(value);
johnson6987 0:0734b922c101 43 while(pc.getcNb()!='s');
johnson6987 0:0734b922c101 44
johnson6987 0:0734b922c101 45 FILE *fp =fopen(filename, "w"); // Open "out.txt" on the local file system for writing
johnson6987 0:0734b922c101 46 fprintf(fp,"Time(s), EncoderCnts, LoadCell, Meters, Newtons \n");
johnson6987 0:0734b922c101 47
johnson6987 0:0734b922c101 48 tri = 0;
johnson6987 0:0734b922c101 49 //wait(20);
johnson6987 0:0734b922c101 50 SystemTime.start();
johnson6987 0:0734b922c101 51 LogTime.start();
johnson6987 0:0734b922c101 52 LoopTime.start();
johnson6987 0:0734b922c101 53 SampleTime.start();
johnson6987 0:0734b922c101 54 leds=0xF;
johnson6987 0:0734b922c101 55 float avgtotal=0;
johnson6987 0:0734b922c101 56 float avgcnt=0;
johnson6987 0:0734b922c101 57 wheel.reset();
johnson6987 0:0734b922c101 58 char check = '0';
johnson6987 0:0734b922c101 59 while(check!= 'S') {
johnson6987 0:0734b922c101 60 check = pc.getcNb();
johnson6987 0:0734b922c101 61 if(check=='y') {
johnson6987 0:0734b922c101 62 //while(pc.getcNb()=='y');
johnson6987 0:0734b922c101 63 Set+=3;
johnson6987 0:0734b922c101 64 pc.printf("\r\nTarget = %flbs",Set);
johnson6987 0:0734b922c101 65 }
johnson6987 0:0734b922c101 66 if(check=='h') {
johnson6987 0:0734b922c101 67 //while(pc.getcNb()=='h');
johnson6987 0:0734b922c101 68 Set-=3;
johnson6987 0:0734b922c101 69 pc.printf("\r\nTarget = %flbs",Set);
johnson6987 0:0734b922c101 70 }
johnson6987 0:0734b922c101 71
johnson6987 0:0734b922c101 72 //wait_ms(100);
johnson6987 0:0734b922c101 73 if (LogTime.read_ms()>100) {
johnson6987 0:0734b922c101 74 int temp2 = wheel.getPulses();
johnson6987 0:0734b922c101 75 float temp = ((avgtotal/avgcnt)+1.7026)/336.85;
johnson6987 0:0734b922c101 76 fprintf(fp,"%f ,%i ,%f , %f, %f\n", SystemTime.read(),temp2,temp,(float)temp2/10900.000,avgtotal/avgcnt*4.45);
johnson6987 0:0734b922c101 77 LogTime.reset();
johnson6987 0:0734b922c101 78
johnson6987 0:0734b922c101 79 }
johnson6987 0:0734b922c101 80 avgtotal+=336.85*load.read()-1.7026;
johnson6987 0:0734b922c101 81 avgcnt+=1.0;
johnson6987 0:0734b922c101 82 if (LoopTime.read_ms()>5) {
johnson6987 0:0734b922c101 83
johnson6987 0:0734b922c101 84 LoopTime.reset();
johnson6987 0:0734b922c101 85
johnson6987 0:0734b922c101 86 float Actual=avgtotal/avgcnt;
johnson6987 0:0734b922c101 87 avgtotal=0;
johnson6987 0:0734b922c101 88 avgcnt=0;
johnson6987 0:0734b922c101 89 float Error = (Set-Actual);
johnson6987 0:0734b922c101 90 if (Error>errorup)Error=errorup;
johnson6987 0:0734b922c101 91 if (Error<errorlow)Error=errorlow;
johnson6987 0:0734b922c101 92 value=value+Error;
johnson6987 0:0734b922c101 93 // pc.printf("%x\r\n",value);
johnson6987 0:0734b922c101 94
johnson6987 0:0734b922c101 95 if(value > 0xFFFE) {
johnson6987 0:0734b922c101 96 value=0xFFFE;
johnson6987 0:0734b922c101 97 }
johnson6987 0:0734b922c101 98 if(value < 0x00A0) {
johnson6987 0:0734b922c101 99 value=0x00A0;
johnson6987 0:0734b922c101 100 }
johnson6987 0:0734b922c101 101 tri.write_u16(value);
johnson6987 0:0734b922c101 102
johnson6987 0:0734b922c101 103
johnson6987 0:0734b922c101 104 }// 4.45 lbs to N
johnson6987 0:0734b922c101 105
johnson6987 0:0734b922c101 106 // if (wheel.getPulses()>MCONV)Set=200;
johnson6987 0:0734b922c101 107 if (wheel.getPulses()>1.6*MCONV)Set=220;
johnson6987 0:0734b922c101 108 if (wheel.getPulses()>2.2*MCONV)Set=240;
johnson6987 0:0734b922c101 109 if (wheel.getPulses()>2.8*MCONV)Set=260;
johnson6987 0:0734b922c101 110 if (wheel.getPulses()>3.4*MCONV)Set=280;
johnson6987 0:0734b922c101 111 if (wheel.getPulses()>4*MCONV)Set=300;
johnson6987 0:0734b922c101 112 if (wheel.getPulses()>4.6*MCONV)Set=320;
johnson6987 0:0734b922c101 113 /*if (wheel.getPulses()>5.8*MCONV)Set=210;
johnson6987 0:0734b922c101 114 if (wheel.getPulses()>6.6*MCONV)Set=220;
johnson6987 0:0734b922c101 115 if (wheel.getPulses()>7*MCONV)Set=240;*/
johnson6987 0:0734b922c101 116
johnson6987 0:0734b922c101 117
johnson6987 0:0734b922c101 118 /*
johnson6987 0:0734b922c101 119 if (wheel.getPulses()>MCONV)Set=40;
johnson6987 0:0734b922c101 120 if (wheel.getPulses()>1.8*MCONV)Set=60;
johnson6987 0:0734b922c101 121 if (wheel.getPulses()>2.6*MCONV)Set=80;
johnson6987 0:0734b922c101 122 if (wheel.getPulses()>3.4*MCONV)Set=100;
johnson6987 0:0734b922c101 123 if (wheel.getPulses()>4.2*MCONV)Set=120;
johnson6987 0:0734b922c101 124 if (wheel.getPulses()>5*MCONV)Set=140;
johnson6987 0:0734b922c101 125 if (wheel.getPulses()>5.8*MCONV)Set=160;
johnson6987 0:0734b922c101 126 if (wheel.getPulses()>6.6*MCONV)Set=180;
johnson6987 0:0734b922c101 127 if (wheel.getPulses()>7*MCONV)Set=200;
johnson6987 0:0734b922c101 128 */
johnson6987 0:0734b922c101 129 }
johnson6987 0:0734b922c101 130 fclose(fp);
johnson6987 0:0734b922c101 131 tri.write_u16(0x0400);
johnson6987 0:0734b922c101 132 leds=0x8;
johnson6987 0:0734b922c101 133 Set=30;
johnson6987 0:0734b922c101 134 while(pc.getcNb()!='R') {
johnson6987 0:0734b922c101 135 avgtotal+=336.85*load.read()-1.7026;
johnson6987 0:0734b922c101 136 avgcnt+=1.0;
johnson6987 0:0734b922c101 137 if (LoopTime.read_ms()>5) {
johnson6987 0:0734b922c101 138
johnson6987 0:0734b922c101 139 LoopTime.reset();
johnson6987 0:0734b922c101 140
johnson6987 0:0734b922c101 141 float Actual=avgtotal/avgcnt;
johnson6987 0:0734b922c101 142 avgtotal=0;
johnson6987 0:0734b922c101 143 avgcnt=0;
johnson6987 0:0734b922c101 144 float Error = (Set-Actual);
johnson6987 0:0734b922c101 145 if (Error>errorup)Error=errorup;
johnson6987 0:0734b922c101 146 if (Error<errorlow)Error=errorlow;
johnson6987 0:0734b922c101 147 value=value+Error;
johnson6987 0:0734b922c101 148 // pc.printf("%x\r\n",value);
johnson6987 0:0734b922c101 149
johnson6987 0:0734b922c101 150 if(value > 0xFFFE) {
johnson6987 0:0734b922c101 151 value=0xFFFE;
johnson6987 0:0734b922c101 152 }
johnson6987 0:0734b922c101 153 if(value < 0x00A0) {
johnson6987 0:0734b922c101 154 value=0x00A0;
johnson6987 0:0734b922c101 155 }
johnson6987 0:0734b922c101 156 tri.write_u16(value);
johnson6987 0:0734b922c101 157 }
johnson6987 0:0734b922c101 158 }
johnson6987 0:0734b922c101 159 }
johnson6987 0:0734b922c101 160 }