Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 #include "LinearMotor.h" 00004 00005 int ItvTable[200]= {5000,3219,2373,1880,1556,1327,1157,1026,921,836,765,706,654,610,572,538,507,480,456,434,414,396,380,364,350,337,325,314,303,293,284,275,267,260,252,246,239,233,227,221,216,211,206,202,197,193,189,185,181,178,174,171,168,165,162,159,156,154,151,149,146,144,142,139,137,135,133,131,129,128,126,124,122,121,119,118,116,115,113,112,110,109,108,107,105,104,103,102,101,100,98,97,96,95,94,93,92,91,91,90,89,88,87,86,85,85,84,83,82,82,81,80,79,79,78,77,77,76,75,75,74,74,73,72,72,71,71,70,70,69,69,68,68,67,67,66,66,65,65,64,64,63,63,62,62,62,61,61,60,60,60,59,59,58,58,58,57,57,57,56,56,56,55,55,54,54,54,54,53,53,53,52,52,52,51,51,51,51,50,50,50,49,49,49,49,48,48,48,48,47,47,47,47,46,46,46,46,45,45,45}; 00006 int stepTable[200]= {2,5,9,14,20,28,37,47,58,70,83,97,112,128,145,164,184,205,227,250,274,299,325,352,381,411,442,474,507,541,576,612,649,687,727,768,810,853,897,942,988,1035,1084,1134,1185,1237,1290,1344,1399,1455,1512,1570,1630,1691,1753,1816,1880,1945,2011,2078,2146,2215,2285,2357,2430,2504,2579,2655,2733,2811,2890,2971,3053,3136,3220,3305,3391,3478,3566,3655,3746,3838,3931,4024,4119,4215,4312,4410,4509,4609,4711,4814,4918,5023,5129,5237,5346,5456,5566,5677,5789,5903,6018,6134,6252,6370,6489,6609,6731,6853,6976,7101,7228,7355,7483,7613,7743,7875,8008,8141,8276,8411,8548,8687,8826,8967,9108,9251,9394,9539,9684,9831,9978,10127,10276,10428,10580,10734,10888,11044,11200,11359,11518,11679,11840,12001,12165,12329,12496,12663,12830,12999,13168,13340,13512,13684,13859,14034,14209,14388,14567,14746,14928,15110,15295,15480,15665,15850,16039,16228,16417,16609,16801,16993,17189,17385,17581,17777,17977,18177,18377,18581,18785,18989,19193,19401,19609,19817,20025,20238,20451,20664,20877,21094,21311,21528,21745,21967,22189,22411}; 00007 00008 00009 Serial pc(SERIAL_TX, SERIAL_RX); 00010 00011 //0.00225mm/step 00012 //500usec=4.5mm/sec 00013 //45usec=50mm/sec 00014 LinearMotor lm(PA_7,PA_6,PA_5,PA_0); 00015 void move(double dis, double spd); 00016 void init(void); 00017 00018 void pause(void); 00019 InterruptIn psw(PA_8); 00020 DigitalIn psw_(PA_8); 00021 int main() { 00022 00023 psw_.mode(PullUp); 00024 pc.baud(230400); 00025 if(psw_.read()) pc.printf("\n\rPause.... "); 00026 while(psw_.read()); 00027 pc.printf("Start"); 00028 wait_ms(20); 00029 00030 psw.rise(&pause); 00031 lm.st=1; 00032 wait_ms(500); 00033 init(); 00034 wait_ms(500); 00035 while(1) { 00036 move(50,10); 00037 wait_ms(200); 00038 move(-50,10); 00039 wait_ms(200); 00040 } 00041 } 00042 00043 00044 void move(double dis, double spd){//spd mm/sec 00045 pc.printf("Moving %10fmm at %10fmm/sec... ",dis,spd); 00046 int j=0; 00047 int crntDly=5000; 00048 int i; 00049 00050 if (dis>0)lm.dir=1; 00051 else{ 00052 lm.dir=0; 00053 dis=-dis; 00054 } 00055 00056 int stepDly=(int)(0.5+2250/spd); 00057 int step=(int)(0.5+dis/0.00225); 00058 if (stepDly>=ItvTable[0]){ 00059 for(i=0;i<step;i++){ 00060 lm.pulse=!lm.pulse; 00061 wait_us(stepDly); 00062 } 00063 } 00064 else{ 00065 int ihigh=0; 00066 for(i=0;i<(int)(step/2);i++){ 00067 lm.pulse=!lm.pulse; 00068 if(j>=200 || crntDly<=stepDly){crntDly=stepDly;} 00069 else if(i<stepTable[j]){crntDly=ItvTable[j];} 00070 else {j++;ihigh=i;} 00071 wait_us(crntDly); 00072 } 00073 for(i=(int)(step/2);i>0;i--){ 00074 lm.pulse=!lm.pulse; 00075 if(i>=ihigh){crntDly=stepDly;} 00076 else if(j<=0 || crntDly>=ItvTable[0]){crntDly=ItvTable[0];} 00077 else if(i>=stepTable[j]){crntDly=ItvTable[j];} 00078 else {j--;} 00079 wait_us(crntDly); 00080 } 00081 } 00082 pc.printf("Complete\n\r"); 00083 } 00084 00085 00086 void init(void){ 00087 lm.dir=0; 00088 int i=0; 00089 int j=0; 00090 int crntDly=5000; 00091 int stepDly=(int)(0.5+2250/10); 00092 pc.printf("\n\rInitializing... "); 00093 while(lm.limit==0){ 00094 lm.pulse=!lm.pulse; 00095 if(j>=200 || crntDly<=stepDly){crntDly=stepDly;} 00096 else if(i<stepTable[j]){crntDly=ItvTable[j];} 00097 else {j++;} 00098 wait_us(crntDly); 00099 i++; 00100 } 00101 pc.printf("Complete\n\r"); 00102 } 00103 00104 void pause(void){ 00105 psw.disable_irq(); 00106 wait_ms(20); 00107 pc.printf("\n\rPause...."); 00108 while(psw_.read()); 00109 pc.printf(" Resume\n\r"); 00110 psw.enable_irq(); 00111 }
Generated on Sun Jul 17 2022 19:55:54 by
1.7.2