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.
Dependencies: SDFileSystem mbed stepper
Fork of Nucleo_Ex05_SD by
main.cpp
00001 #include "mbed.h" 00002 #include "stepper.h" 00003 #include "SDFileSystem.h" 00004 00005 // mosi, miso, sclk, name 00006 SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd"); 00007 00008 00009 typedef enum { 00010 PointerS,PointerW,PointerX,PointerY,PointerZ,PointerA,PointerL,PointerWW 00011 } STATE; //定义结构体枚举类型STATE 00012 00013 00014 Serial pc(PA_9, PA_10); //定义引脚 00015 Serial bt(PB_10, PB_11); 00016 stepperMotor motorX(PC_5, PC_4, PD_2, PC_8, 0.0002); 00017 stepperMotor motorY(PA_5, PA_4, PA_2, PA_6, 0.0002); 00018 stepperMotor motorZ(PC_3, PC_2, PC_13, PC_6, 0.0002); 00019 //DigitalIn swX(PA_6); 00020 //DigitalIn swY(PA_6); 00021 //DigitalIn swZ(PA_6); 00022 float X , Y , Z , A; // 定义浮点数值用以储存返回给机械臂的执行步数 00023 const char* strX; //定义全局变量X,Y,Z,A字符串指针 00024 const char* strY; 00025 const char* strZ; 00026 const char* strA; 00027 //stringstream ss; 00028 char buf[256]; 00029 int cur; 00030 bool received,start; 00031 00032 void init() //复位函数 00033 { 00034 stepperMotor motorX(PC_5, PC_4, PD_2, PC_8, 0.0005); 00035 stepperMotor motorY(PA_5, PA_4, PA_2, PA_6, 0.0005); 00036 stepperMotor motorZ(PC_3, PC_2, PC_13, PC_6, 0.0005); //复位速度要变慢 00037 int steps = -12000; 00038 motorX.enable(steps); 00039 while(motorX.remain != 0); 00040 motorY.enable(steps); 00041 while(motorY.remain != 0); 00042 motorZ.enable(steps); 00043 while(motorZ.remain != 0); 00044 } 00045 00046 00047 00048 void btInterrupt() //中断接收函数 00049 { 00050 if(!bt.readable()) 00051 return; 00052 char strRe1; //定义字符串 00053 static STATE State = PointerS; // 初始状态待检查 00054 strRe1 = bt.getc(); //读取一个byte数据 00055 00056 switch(State) { 00057 case PointerS: 00058 if (strRe1 == (char)'S') { 00059 State = PointerW; //准备跳转至数据选择 00060 cur = 0; 00061 } else 00062 State = PointerS; 00063 break; 00064 case PointerW: 00065 switch (strRe1) { 00066 case (char)'X': 00067 State = PointerX; 00068 break; 00069 case (char)'Y': 00070 State = PointerY; 00071 break; 00072 case (char)'Z': 00073 State = PointerZ; 00074 break; 00075 case (char)'S': 00076 State = PointerS; 00077 break; 00078 case (char)'L': 00079 State = PointerL; 00080 break; 00081 case (char)'W': 00082 State = PointerWW; 00083 break; 00084 case (char)'A': 00085 State = PointerA; 00086 break; 00087 } 00088 case PointerX: 00089 if (strRe1 == (char)'\n') { 00090 buf[cur]='\0'; 00091 pc.printf("%s\n",buf); 00092 X = atof(buf+1); 00093 State = PointerS; 00094 pc.printf("%f\n",X); 00095 } else { 00096 buf[cur++]=strRe1; 00097 } 00098 break; 00099 case PointerY: 00100 if (strRe1 == (char)'\n') { 00101 buf[cur]='\0'; 00102 pc.printf("%s\n",buf); 00103 Y = atof(buf+1); 00104 State = PointerS; 00105 pc.printf("%f\n",Y); 00106 } else { 00107 buf[cur++]=strRe1; 00108 } 00109 break; 00110 case PointerZ: 00111 if (strRe1 == (char)'\n') { 00112 buf[cur]='\0'; 00113 pc.printf("%s\n",buf); 00114 Z = atof(buf+1); 00115 State = PointerS; 00116 pc.printf("%f\n",Z); 00117 } else { 00118 buf[cur++]=strRe1; 00119 } 00120 break; 00121 case PointerA: 00122 if (strRe1 == (char)'\n') { 00123 buf[cur]='\0'; 00124 pc.printf("%s\n",buf); 00125 A = atof(buf+1); 00126 State = PointerS; 00127 pc.printf("%f\n",A); 00128 } else { 00129 buf[cur++]=strRe1; 00130 } 00131 case PointerL: 00132 if (strRe1 == (char)'\n') { 00133 buf[cur]='\0'; 00134 State = PointerS; 00135 received = true; 00136 } else { 00137 buf[cur++]=strRe1; 00138 } 00139 break; 00140 case PointerWW: 00141 if (strRe1 == (char)'\n') { 00142 State = PointerS; 00143 start = true; 00144 } else { 00145 } 00146 break; 00147 } 00148 } 00149 00150 int main() 00151 { 00152 00153 // motorX.enable(); 00154 // motorY.enable(); 00155 // motorZ.enable(); 00156 // init(); 00157 pc.printf("Hello World !\n"); 00158 bt.attach(btInterrupt); 00159 // int i = 1; 00160 00161 00162 while(1) { 00163 00164 00165 if(received) { 00166 pc.printf("save %s to file\r\n",buf); 00167 00168 // 写文件例子 00169 FILE *fp2 = fopen("/sd/test.txt", "w"); 00170 if (fp2 == NULL) 00171 { 00172 printf("open error2!!\r\n"); 00173 return 1; 00174 } 00175 fprintf(fp2, "%s", buf); 00176 fclose(fp2); //写完文件要记得关闭,不然可能没保存上 00177 pc.printf("saved\r\n"); 00178 received = false; 00179 } 00180 if(start){ 00181 00182 00183 FILE *fp = fopen("/sd/test.txt", "r"); //打开文件,路径以“/sd/”开头 00184 00185 if (fp == NULL) //打开失败,原因可能是文件不存在,或卡没有连接好 00186 { 00187 printf("open error!!\r\n"); 00188 return 1; 00189 } 00190 fgets(buf, sizeof(buf), fp); //读入一行的C函数 00191 pc.printf("read\r\n"); 00192 fclose(fp); //关闭文件,释放资源 00193 00194 pc.printf("%s\r\n",buf); 00195 char *p = buf; 00196 float a,x,y,z; 00197 while(true) { 00198 p = strchr(p, '('); 00199 if(!p) break; 00200 p++; 00201 sscanf(p, "%f%f%f%f",&x,&y,&z,&a); 00202 pc.printf("%f %f %f %f\r\n",x,y,z,a); 00203 } 00204 start = false; 00205 } 00206 } 00207 return 0; 00208 }
Generated on Sun Jul 24 2022 11:13:10 by
