关于码盘测速的一些问题
目的:应用processing滚动条控制电机转速,按钮控制电机运动方向,并且通过编码器测速获得实际的转速,显示在processing上编码器测速方法:外部定时测脉冲数,定时器确定采样时间,由公式算出实际车轮转速。
现象:没有FlexiTimer2.h加定时器程序之前还能自由调节电机转速(即给出不同的PWM值,能获得不同的转速),一旦使用了 FlexiTimer2.h这个定时器,电机就只能在PWM为255时转动且能够测出转速,此外一旦PWM小于255就转不了了。
请教各位大神,这是什么原因造成的?应该如何解决?
以下为一个轮子工作的程序:
#include <FlexiTimer2.h>
#define IN1 3
#define IN2 4
#define IN3 5
#define IN4 6
String comdata = "";
int numdata = {0}, mark = 0,dirdata={0};
int PWMPin = {10,11};
//编码器部分
const int interruptA = 0;
int CLK = 22;
int DT = 6;
int COUNT = 0;
float lastt, currentt,Speed;
void setup()
{ Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(PWMPin,OUTPUT);
pinMode(PWMPin,OUTPUT);
//编码器部分
attachInterrupt(interruptA, RoteStateChanged, FALLING);
FlexiTimer2::set(1000, flash); // 500ms period
pinMode(CLK, INPUT);
digitalWrite (CLK, HIGH);
pinMode(DT, INPUT);
digitalWrite (DT, HIGH);
lastt =millis();
}
void flash()
{
Speed = 4*PI*PI*COUNT*67/1560;
Serial.print('h');
Serial.print(',');
Serial.print(Speed);
Serial.print('\n');
COUNT = 0;
}
void loop ()
{
int j = 0;
while (Serial.available() > 0)
{
comdata += char (Serial.read());
delay (2);
mark = 1;
}
FlexiTimer2::start();
delay(1000);
FlexiTimer2::stop();
if (mark == 1)
{
dirdata = comdata;
dirdata = comdata;
if (dirdata == 1)
{
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
}
else
{
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
}
if (dirdata == -1)
{
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
}
else
{
digitalWrite (IN3, LOW);
digitalWrite (IN4, HIGH);
}
for(int i = 4; i < comdata.length(); i++)
{
if (comdata == ',')
{
j++;
}
else
{
numdata = numdata*10 + (comdata - '0');
}
}
comdata = String("");
for (int i = 0; i < 2; i++)
{
analogWrite (PWMPin, numdata);
numdata = 0;
}
mark = 0;
}
// currentt =millis();
// if ((currentt - lastt) == 500)
// {
// Speed = 4*PI*PI*COUNT*67/1560;
// Serial.print('h');
// Serial.print(',');
// Serial.print(Speed);
// Serial.print('\n');
// COUNT = 0;
// lastt =millis();
// }
}
void RoteStateChanged()
{
if (digitalRead(DT))
{
COUNT++;
}
}
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.charAt(0) == 'h')
{
Speed1 = Float.parseFloat(str);
}
}
}
哎为啥Arudino的程序第一次都发不出去......#include <FlexiTimer2.h>
#define IN1 3
#define IN2 4
#define IN3 5
#define IN4 6
String comdata = "";
int numdata = {0}, mark = 0,dirdata={0};
int PWMPin = {10,11};
//编码器部分
const int interruptA = 0;
int CLK = 22;
int DT = 6;
int COUNT = 0;
float lastt, currentt,Speed;
void setup()
{ Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(PWMPin,OUTPUT);
pinMode(PWMPin,OUTPUT);
//编码器部分
attachInterrupt(interruptA, RoteStateChanged, FALLING);
FlexiTimer2::set(1000, flash); // 500ms period
pinMode(CLK, INPUT);
digitalWrite (CLK, HIGH);
pinMode(DT, INPUT);
digitalWrite (DT, HIGH);
lastt =millis();
}
void flash()
{
Speed = 4*PI*PI*COUNT*67/1560;
Serial.print('h');
Serial.print(',');
Serial.print(Speed);
Serial.print('\n');
COUNT = 0;
}
void loop ()
{
int j = 0;
while (Serial.available() > 0)
{
comdata += char (Serial.read());
delay (2);
mark = 1;
}
FlexiTimer2::start();
delay(1000);
FlexiTimer2::stop();
if (mark == 1)
{
dirdata = comdata;
dirdata = comdata;
if (dirdata == 1)
{
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
}
else
{
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
}
if (dirdata == -1)
{
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
}
else
{
digitalWrite (IN3, LOW);
digitalWrite (IN4, HIGH);
}
for(int i = 4; i < comdata.length(); i++)
{
if (comdata == ',')
{
j++;
}
else
{
numdata = numdata*10 + (comdata - '0');
}
}
comdata = String("");
for (int i = 0; i < 2; i++)
{
analogWrite (PWMPin, numdata);
numdata = 0;
}
mark = 0;
}
// currentt =millis();
// if ((currentt - lastt) == 500)
// {
// Speed = 4*PI*PI*COUNT*67/1560;
// Serial.print('h');
// Serial.print(',');
// Serial.print(Speed);
// Serial.print('\n');
// COUNT = 0;
// lastt =millis();
// }
}
void RoteStateChanged()
{
if (digitalRead(DT))
{
COUNT++;
}
}
http://www.geek-workshop.com/thread-7771-1-1.html
关于定时器和PWM的,
164335413 发表于 2016-8-24 20:41 static/image/common/back.gif
http://www.geek-workshop.com/thread-7771-1-1.html
关于定时器和PWM的,
太好了 谢谢{:2_31:} 164335413 发表于 2016-8-24 20:41 static/image/common/back.gif
http://www.geek-workshop.com/thread-7771-1-1.html
关于定时器和PWM的,
后来发现 是引脚坏了,换了个PWM引脚就好了 thx anyway “Speed = 4*PI*PI*COUNT*67/1560;”请问这个是怎么来的?
页:
[1]