电机使用电位器进行速度调节

Dependencies:   mbed QEI

Files at this revision

API Documentation at this revision

Comitter:
YYL5213
Date:
Thu Mar 24 03:24:16 2022 +0000
Parent:
0:ad5e5d970dd9
Commit message:
update note

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Mar 24 03:19:51 2022 +0000
+++ b/main.cpp	Thu Mar 24 03:24:16 2022 +0000
@@ -17,12 +17,12 @@
 AnalogIn MI2(PA_1);//读电流
 AnalogIn adj(PA_3);//读电流
 
-Ticker Velo;
+Ticker Velo;  //固定频率读电机速度
 int VA=0,sttA=0,stpA=0,VB=0,sttB=0,stpB=0;
 
-float adj_point=0.0f;
+float adj_point=0.0f;   //电位计设定值
 
-void motor_run(int M,float pwr,int drc)
+void motor_run(int M,float pwr,int drc)  //控制电机控制信号输出
 {
     if(M==1) {
         if(drc==0) {
@@ -47,7 +47,7 @@
     }
         
 }
-void Read_Velocity(){
+void Read_Velocity(){   //读取编码器的值,计算电机速度并打印
      stpA=EnA.getPulses();
      stpB=EnB.getPulses();
      VA=stpA-sttA;
@@ -56,13 +56,13 @@
      sttB= stpB;
      pc.printf("%d  %d   %.3f\n",VA,VB,adj_point);
     }
-int main()
-{   Velo.attach(&Read_Velocity, 0.01);
+int main()    
+{   Velo.attach(&Read_Velocity, 0.01);//开启tricker,10ms一次
     pc.baud(115200);
     pc.printf("PC connected!\n");
-    while(1){
-    adj_point=adj.read();
-    motor_run(1,adj_point,0);
+    while(1){        //下面是进行的主流程
+    adj_point=adj.read();//读取电位器值
+    motor_run(1,adj_point,0);//设定出的pwm占空比
     motor_run(2,adj_point,0);
     wait_ms(50);
     }