极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 25280|回复: 5

关于码盘测速的一些问题

[复制链接]
发表于 2016-8-24 19:41:58 | 显示全部楼层 |阅读模式
目的:应用processing滚动条控制电机转速,按钮控制电机运动方向,并且通过编码器测速获得实际的转速,显示在processing上
编码器测速方法:外部定时测脉冲数,定时器确定采样时间,由公式算出实际车轮转速。

现象:没有FlexiTimer2.h加定时器程序之前还能自由调节电机转速(即给出不同的PWM值,能获得不同的转速),一旦使用了       FlexiTimer2.h这个定时器,电机就只能在PWM为255时转动且能够测出转速,此外一旦PWM小于255就转不了了。

请教各位大神,这是什么原因造成的?应该如何解决?




以下为一个轮子工作的程序:


  1. [code]
  2. #include <FlexiTimer2.h>
  3. #define IN1 3
  4. #define IN2 4
  5. #define IN3 5
  6. #define IN4 6
  7. String comdata = "";
  8. int numdata[2] = {0}, mark = 0,dirdata[2]={0};
  9. int PWMPin[2] = {10,11};
  10. //编码器部分
  11. const int interruptA = 0;
  12. int CLK = 22;
  13. int DT = 6;
  14. int COUNT = 0;
  15. float lastt, currentt,Speed;

  16. void setup()
  17. {   Serial.begin(9600);
  18.     pinMode(IN1, OUTPUT);
  19.     pinMode(IN2, OUTPUT);
  20.     pinMode(IN3, OUTPUT);
  21.     pinMode(IN4, OUTPUT);
  22.     pinMode(PWMPin[0],OUTPUT);
  23.     pinMode(PWMPin[1],OUTPUT);
  24.     //编码器部分
  25.     attachInterrupt(interruptA, RoteStateChanged, FALLING);   
  26.     FlexiTimer2::set(1000, flash); // 500ms period

  27.     pinMode(CLK, INPUT);
  28.     digitalWrite (CLK, HIGH);
  29.     pinMode(DT, INPUT);
  30.     digitalWrite (DT, HIGH);
  31.     lastt =  millis();
  32. }

  33. void flash()
  34. {
  35.   Speed = 4*PI*PI*COUNT*67/1560;
  36.   Serial.print('h');
  37.   Serial.print(',');
  38.   Serial.print(Speed);
  39.   Serial.print('\n');
  40.   COUNT = 0;
  41.   }

  42.   
  43. void loop ()
  44. {   
  45.    
  46.     int j = 0;
  47.     while (Serial.available() > 0)
  48.     {

  49.          comdata += char (Serial.read());
  50.          delay (2);
  51.          mark = 1;
  52.       }

  53.               FlexiTimer2::start();
  54.               delay(1000);
  55.               FlexiTimer2::stop();
  56.     if (mark == 1)
  57.     {
  58.             dirdata[0] = comdata[0];
  59.             dirdata[1] = comdata[3];
  60.             if (dirdata[0] == 1)
  61.             {
  62.                 digitalWrite (IN1, HIGH);
  63.                 digitalWrite (IN2, LOW);
  64.             }
  65.             else
  66.             {
  67.                 digitalWrite (IN1, LOW);
  68.                 digitalWrite (IN2, HIGH);
  69.             }
  70.             if (dirdata[1] == -1)
  71.             {
  72.                 digitalWrite (IN3, HIGH);
  73.                 digitalWrite (IN4, LOW);
  74.             }
  75.             else
  76.             {
  77.                 digitalWrite (IN3, LOW);
  78.                 digitalWrite (IN4, HIGH);
  79.             }
  80.             
  81.             for(int i = 4; i < comdata.length(); i++)
  82.             {
  83.                if (comdata [i] == ',')
  84.                {
  85.                  j++;
  86.                }
  87.                else
  88.                {
  89.                 numdata[j] = numdata[j]*10 + (comdata[i] - '0');
  90.                }
  91.             }
  92.                comdata = String("");
  93.                for (int i = 0; i < 2; i++)
  94.                {
  95.                    analogWrite (PWMPin[i], numdata[i]);
  96.                    numdata[i] = 0;
  97.                }
  98.                mark = 0;
  99.               }
  100. //       currentt =  millis();
  101. //     if ((currentt - lastt) == 500)
  102. //     {
  103. //        Speed = 4*PI*PI*COUNT*67/1560;
  104. //        Serial.print('h');
  105. //        Serial.print(',');
  106. //        Serial.print(Speed);
  107. //        Serial.print('\n');
  108. //        COUNT = 0;
  109. //        lastt =  millis();
  110. //     }
  111.          
  112. }

  113. void RoteStateChanged()
  114. {

  115.      if (digitalRead(DT))
  116.     {
  117.         COUNT++;
  118.     }   
  119.    

  120.      

  121. }

复制代码
[/code]





[pre lang="Processing" line="1"]class Button
{
     int x, y, r;
     int dir = 1;
      Button(int xp, int yp, int rp)
     {
            x = xp;
            y = yp;
            r = rp;
         
     }
     void display()
     {   
         
         fill(255);
         rect(x,y,r,r);
         if (dir == 1)
         {
             text("Foward",x-90,y+10);
         }
         else
         {
             text("Back",x-90,y+10);
         }
     }
     void press(int mx, int my)
     {
      
         if ((mx > (x))&&(mx < (x + r))&&(my > (y))&&(my < (y + r)))
         {
            
             dir = -dir;
         }
      
     }
     int Output()
     {
        return dir;
     }
}
class Scrollbar
{
     int x, y;
     float Scrollbar_Width, Scrollbar_Height;
     float pos;
     float position_Min, position_Max;
     boolean rollover;
     boolean locked;
     float minVal, maxVal;
     Scrollbar (int xp, int yp, int w ,int h, float miv, float mav)
     {
              x = xp;
              y = yp;
              Scrollbar_Width = w;
              Scrollbar_Height = h;
              minVal = miv;
              maxVal = mav;
              pos = x;
              position_Min = x;
              position_Max = x + Scrollbar_Width - Scrollbar_Height;   
     }
     void update (int mx, int my)
     {
         if (over (mx, my) == true)
         {
           rollover = true;
         }
         else
         {
           rollover = false;
         }
         if (locked == true)
         {
           pos = constrain(mx-Scrollbar_Height/2,position_Min,position_Max);
         }
     }
     void press(int mx, int my)
     {
         if (rollover == true)
         {
          locked = true;
         }
         else
         {
           locked = false;
         }
     }
     void release()
     {
         locked = false;
     }
     boolean over (int mx, int my)
     {
            if ((mx > x)&&(mx < x+Scrollbar_Width)&&(my > y)&&(my < y + Scrollbar_Height))
            {
                return true;
            }
            else
            {
                return false;
            }
            
            
     }
     void display()
     {
         fill(255);
         rect(x,y,Scrollbar_Width, Scrollbar_Height);
         if ((rollover == true)||(locked == true))
         {
             fill(#FFD700);
         }
         else
         {
             fill(102);
         }
         rect(pos, y, Scrollbar_Height, Scrollbar_Height);
     }
     float getPos()
     {
          float scallar = Scrollbar_Width/(Scrollbar_Width-Scrollbar_Height);
          float ratio = (pos - x)*scallar;
          float offset = minVal + (ratio/Scrollbar_Width*(maxVal-minVal));
          return offset;
     }
}

Scrollbar Motor1_bar, Motor2_bar;
Button But1, But2;
PFont font;
int pos1, pos2;
import processing.serial.*;
Serial myPort;
float Speed1 = 0,Speed2 = 0;
PImage img;


void setup()
{
    img = requestImage("image1.jpg");
    size (600 , 600);
    noStroke();
    textSize(20);
    Motor1_bar = new Scrollbar(100, 50, 255, 20, 0, 255);
    Motor2_bar = new Scrollbar(100, 150, 255, 20, 0, 255);
    But1 = new Button(110,110,10);
    But2 = new Button(110,210,10);
    font = loadFont("David-30.vlw");
    textFont(font);
    textAlign(LEFT);
    myPort = new Serial(this, "COM4", 9600);
    myPort.bufferUntil('\n');
    myPort.clear();
   
}

void draw()
{   
    background(0);
    img.resize(352,220);
    image(img,120,350);


    fill(255);
    text("Motor1", 20, 68);
    text("Motor2", 20, 168);
    pos1 = int (Motor1_bar.getPos());
    textSize(25);
    text(pos1, 525, 68);
    text("PWM:", 365, 68);
    text("Speed1(mm/s):", 365, 120);
    text(Speed1, 520, 120);
    pos2 = int (Motor2_bar.getPos());
    text(pos2, 525, 168);
    text("PWM:", 365, 168);
    text("Speed2(mm/s):", 365, 220);
    text(Speed2, 520, 220);
    Motor1_bar.update(mouseX, mouseY);
    Motor2_bar.update(mouseX, mouseY);
    Motor1_bar.display();
    Motor2_bar.display();
    But1.display();
    But2.display();
}

void mousePressed()
{
    Motor1_bar.press(mouseX, mouseY);
    Motor2_bar.press(mouseX, mouseY);
    But1.press(mouseX, mouseY);
    But2.press(mouseX, mouseY);
}

void mouseReleased()
{
    String s1 = Integer.toString(pos1);
    String s2 = Integer.toString(pos2);
    int s3 = But1.Output();
    int s4 = But1.Output();
    myPort.write(s3);
    myPort.write(",");
    myPort.write(s4);
    myPort.write(",");
    myPort.write(s1);
    myPort.write(",");
    myPort.write(s2);
    Motor1_bar.release();
    Motor2_bar.release();
}

void serialEvent(Serial p)
{
    String [] str;
    String inString = p.readString();
    if (inString != null)
    {
        str = inString.split(",");
        if(str[0].charAt(0) == 'h')
        {
            Speed1 = Float.parseFloat(str[1]);
        }
    }
}
    [/code]
回复

使用道具 举报

 楼主| 发表于 2016-8-24 19:43:28 | 显示全部楼层
哎为啥Arudino的程序第一次都发不出去......
  1. #include <FlexiTimer2.h>
  2. #define IN1 3
  3. #define IN2 4
  4. #define IN3 5
  5. #define IN4 6
  6. String comdata = "";
  7. int numdata[2] = {0}, mark = 0,dirdata[2]={0};
  8. int PWMPin[2] = {10,11};
  9. //编码器部分
  10. const int interruptA = 0;
  11. int CLK = 22;
  12. int DT = 6;
  13. int COUNT = 0;
  14. float lastt, currentt,Speed;

  15. void setup()
  16. {   Serial.begin(9600);
  17.     pinMode(IN1, OUTPUT);
  18.     pinMode(IN2, OUTPUT);
  19.     pinMode(IN3, OUTPUT);
  20.     pinMode(IN4, OUTPUT);
  21.     pinMode(PWMPin[0],OUTPUT);
  22.     pinMode(PWMPin[1],OUTPUT);
  23.     //编码器部分
  24.     attachInterrupt(interruptA, RoteStateChanged, FALLING);   
  25.     FlexiTimer2::set(1000, flash); // 500ms period

  26.     pinMode(CLK, INPUT);
  27.     digitalWrite (CLK, HIGH);
  28.     pinMode(DT, INPUT);
  29.     digitalWrite (DT, HIGH);
  30.     lastt =  millis();
  31. }

  32. void flash()
  33. {
  34.   Speed = 4*PI*PI*COUNT*67/1560;
  35.   Serial.print('h');
  36.   Serial.print(',');
  37.   Serial.print(Speed);
  38.   Serial.print('\n');
  39.   COUNT = 0;
  40.   }

  41.   
  42. void loop ()
  43. {   
  44.    
  45.     int j = 0;
  46.     while (Serial.available() > 0)
  47.     {

  48.          comdata += char (Serial.read());
  49.          delay (2);
  50.          mark = 1;
  51.       }

  52.               FlexiTimer2::start();
  53.               delay(1000);
  54.               FlexiTimer2::stop();
  55.     if (mark == 1)
  56.     {
  57.             dirdata[0] = comdata[0];
  58.             dirdata[1] = comdata[3];
  59.             if (dirdata[0] == 1)
  60.             {
  61.                 digitalWrite (IN1, HIGH);
  62.                 digitalWrite (IN2, LOW);
  63.             }
  64.             else
  65.             {
  66.                 digitalWrite (IN1, LOW);
  67.                 digitalWrite (IN2, HIGH);
  68.             }
  69.             if (dirdata[1] == -1)
  70.             {
  71.                 digitalWrite (IN3, HIGH);
  72.                 digitalWrite (IN4, LOW);
  73.             }
  74.             else
  75.             {
  76.                 digitalWrite (IN3, LOW);
  77.                 digitalWrite (IN4, HIGH);
  78.             }
  79.             
  80.             for(int i = 4; i < comdata.length(); i++)
  81.             {
  82.                if (comdata [i] == ',')
  83.                {
  84.                  j++;
  85.                }
  86.                else
  87.                {
  88.                 numdata[j] = numdata[j]*10 + (comdata[i] - '0');
  89.                }
  90.             }
  91.                comdata = String("");
  92.                for (int i = 0; i < 2; i++)
  93.                {
  94.                    analogWrite (PWMPin[i], numdata[i]);
  95.                    numdata[i] = 0;
  96.                }
  97.                mark = 0;
  98.               }
  99. //       currentt =  millis();
  100. //     if ((currentt - lastt) == 500)
  101. //     {
  102. //        Speed = 4*PI*PI*COUNT*67/1560;
  103. //        Serial.print('h');
  104. //        Serial.print(',');
  105. //        Serial.print(Speed);
  106. //        Serial.print('\n');
  107. //        COUNT = 0;
  108. //        lastt =  millis();
  109. //     }
  110.          
  111. }

  112. void RoteStateChanged()
  113. {

  114.      if (digitalRead(DT))
  115.     {
  116.         COUNT++;
  117.     }   
  118.    

  119.      

  120. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2016-8-24 20:41:50 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-25 10:23:01 | 显示全部楼层
164335413 发表于 2016-8-24 20:41
http://www.geek-workshop.com/thread-7771-1-1.html
关于定时器和PWM的,

太好了 谢谢{:2_31:}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-26 11:40:55 | 显示全部楼层
164335413 发表于 2016-8-24 20:41
http://www.geek-workshop.com/thread-7771-1-1.html
关于定时器和PWM的,

后来发现 是引脚坏了,换了个PWM引脚就好了 thx anyway
回复 支持 反对

使用道具 举报

发表于 2019-3-28 19:20:27 | 显示全部楼层
“Speed = 4*PI*PI*COUNT*67/1560;”请问这个是怎么来的?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 00:54 , Processed in 0.042250 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表