DC motor with quadrature encoder (MO - SPG -30E)

Digital Fiber, Photoelectric, Laser Range, Optical, Temperature, Rotary Encoder, Ultrasonic, Gas, Gyro, Accelerometer, FlexiBend, Flexiforce, Compass......

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby A380 » Thu Sep 01, 2011 5:41 pm

The given sample code above can work properly I think because it uses "do while". It only counts once for the state. Why don't you just use the given function?
User avatar
A380
Discoverer
 
Posts: 120
Joined: Tue May 19, 2009 2:44 pm
Location: Malaysia

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby silveroblado » Fri Sep 02, 2011 1:20 am

initA = A; //get current A value //overshoot depends on load and speed of the motor
initB = B; //get current B value
do
{
run_cw(speed); //run motor in clockwise direction with user defined speed
}while(initA ==A && initB ==B); //continue run until state change
countAB++; //increment counter for each state change
}

The thing is I dont get the meaning of continue run until state change......the state will change when ever the motor shaft rotates!!

And I am using the same function just with the if loop, if I use the while loop what will happen, its mostly the same, count is still increasing when i m using if statement!!??

initA = A; //get current A value
initB = B; //get current B value

if(A==1 && B==1)
{
countAB++;

if(countAB==10)
{
buzzer=1;
stopM1(1000);
stopM2(1000);
run_ccwM1(0xff);
run_cwM2(0xff);
countAB--;
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby A380 » Fri Sep 02, 2011 3:15 am

silveroblado WROTE:initA = A; //get current A value //overshoot depends on load and speed of the motor
initB = B; //get current B value
do
{
run_cw(speed); //run motor in clockwise direction with user defined speed
}while(initA ==A && initB ==B); //continue run until state change
countAB++; //increment counter for each state change
}

The thing is I dont get the meaning of continue run until state change......the state will change when ever the motor shaft rotates!!



it means it will stuck in while(initA ==A && initB ==B); loop until next state then countAB++; once only.
for your coding, countAB will keep increasing in that state (because PIC running fast) inside your while(1); loop. So, we should block it until next state then increase the countAB once.
User avatar
A380
Discoverer
 
Posts: 120
Joined: Tue May 19, 2009 2:44 pm
Location: Malaysia

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby silveroblado » Fri Sep 02, 2011 4:37 am

So should I write like this,

while(1)
{
initA = A; //get current A value
initB = B; //get current B value

while(initA=A && initB=B)
{
countAB++;

if(countAB==10)
{
buzzer=1;
}
}
}
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby silveroblado » Fri Sep 02, 2011 4:41 am

I did not want to use Do - while loop as i have lot of fuctions to do, if i use do - while it would be something like this:

while(1)
{
initA = A; //get current A value
initB = B; //get current B value

do
{
servored(); //spray red paint
servoyellownew(); //yellow paint isn't sprayed
servoarmnew(); //arm isnt overcoming obstacle
run_cwM1(0xff); //motor 1 clockwise rotation
run_ccwM2(0xff); //motor 2 counter clockwise rotation
{
while (initA==A && initB==B) //when A=1 and B=1
countAB++; //increase the countAB

if(countAB==10) //when countAB reaches 10
{
buzzer=1; //buzzer will make noise for 100Us then off
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby silveroblado » Sat Sep 03, 2011 5:37 am

I m using the sensors A and B in same way a normal IR sensor would be used:

If(IRsensor==1)
{
countA++
}


So, if IRsensor remains 1 for 5 mins or 2 mins or 1 secs or 100us...the countA would increase by 1,

In same manner when sensor A and sensor B gets 1 anytime their value would increase..........in one revolution of shaft there are 12 pulses, divide it by 3 which gives us 4!!hence for one main shaft rotation i should get 4 counts (of A==1 and B==1)!!for the shaft going through the encoder the value would be proportional!!

I m just giving my explaination!!As I don't understand whats wrong with using if!!just asking!!
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby A380 » Mon Sep 05, 2011 9:09 pm

silveroblado WROTE:So should I write like this,

while(1)
{
initA = A; //get current A value
initB = B; //get current B value

while(initA=A && initB=B)
{
countAB++;

if(countAB==10)
{
buzzer=1;
}
}
}


Should be like this
CODE: SELECT_ALL_CODE
while(1)
{
initA = A; //get current A value   
initB = B;    //get current B value

MotorRun(); //run motor function

while(initA==A && initB==B);
countAB++;

if(countAB==10)
{
buzzer=1;
}
}
User avatar
A380
Discoverer
 
Posts: 120
Joined: Tue May 19, 2009 2:44 pm
Location: Malaysia

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby silveroblado » Tue Sep 06, 2011 11:05 pm

What I am doing is counting the pulses on A and B, when countAB reaches 10 for example, my motor stops running foward and stops for a while and starts running in opposite direction and my countAB starts decreasing until it reaches 0. When it reaches 0 my system stops. Here is the new version!!Do you feels its right!!And why did you use semicolons ; after the while loop??


while(1)
{
initA = A; //get current A value
initB = B; //get current B value
MotorRun(); //run motor function

while(initA==A && initB==B);
countAB++;

if(countAB==10)
{
buzzer=1;
Motorstop();
MotorReverseRun();
countAB--;

if(countAB==0)
{
buzzer=0;
Motorstop;
stopA;
StopB;
}
}
}

Is this program good??Or should I go with the program below............................................

while(1)
{
initA = A; //get current A value
initB = B; //get current B value
MotorRun(); //run motor function

while(initA==A && initB==B);
countAB++;

if(countAB==10)
{
buzzer=1;
Motorstop(0);
stopA;
stopB;
}

initA = A; //get current A value
initB = B; //get current B value
MotorReverseRun(); //run motor function

while(initA==A && initB==B);
countAB--;

if(countAB==0)
{
buzzer=0;
Motorstop(0);
stopA;
stopB;
}
}
silveroblado
Apprentice
 
Posts: 32
Joined: Mon Aug 15, 2011 2:30 am
Location: Kuala Lumpur

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby A380 » Wed Sep 07, 2011 1:01 am

"while(initA==A && initB==B);" is a loop. It is looping this loop itself until next state (it will jump out and countAB plus one).
Should be like this I think.
CODE: SELECT_ALL_CODE

initA = A; //get current A value
initB = B; //get current B value
MotorRun(); //run motor function
countAB = 10;
while(countAB < 20)
{
while(initA==A && initB==B);
countAB++;
}

buzzer=1;
Motorstop(0);
stopA;
stopB;

initA = A; //get current A value
initB = B; //get current B value
MotorReverseRun(); //run motor function

while(countAB > 10)
{
while(initA==A && initB==B);
countAB--;
}

buzzer=0;
Motorstop(0);
stopA;
stopB;

}



Be careful. If your motor run too fast, it cant stop immediately at countAB what you want. Let say you want stop at countAB=10, may be it totally stopped at countAB=15 (overshot). So, it is better if you control the speed before stop, means slow down. I set countAB=10 at first because I want give some buffer for the variable when motor reverses. If you stop the motor a countAB=0 when reversing, may be it will overshot then 0 minus one equal to how much? If countAB declared as signed int, then it should be no problem.
User avatar
A380
Discoverer
 
Posts: 120
Joined: Tue May 19, 2009 2:44 pm
Location: Malaysia

Re: DC motor with quadrature encoder (MO - SPG -30E)

Postby andyew » Thu Sep 08, 2011 12:08 am

Hi all,

sorry to hijack this topic, has anyone used this PID control with HED-like quadrature encoder? High is 3.2V and Low is 0V. Is that compatible?

Besides, i've downloaded the source code, may i know where is the schematic or wiring diagram example?

Thanks and really appreciate your help!
Rgds
Andy
andyew
Greenhorn
 
Posts: 3
Joined: Sun Dec 13, 2009 10:06 pm

PreviousNext

Return to Sensor

Who is online

Users browsing this forum: No registered users and 13 guests

cron