Sample Code Encoded DC gear Motor SPG30E-300K‏

LINIX Brushless, VEXTA Brushless, RC Servo, DC Geared, Linear, Stepper, Tamiya.....

Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby Ken2 » Tue Feb 26, 2013 11:02 pm

Hi, I had bought the SPG30E-300K motor, the problem is i does not understand the code provided by Cytron:
{
CCP2CON = 0b00001100;
PR2 = 0xFF; //PWM Period Setting (4.88KHz)
T2CON = 0b00000101; //Timer2 On, prescale 4
}

I am using a PIC18F4520, 4Mhz crystal and L293d IC to interface with the encoded motor. i cant understand the calculation for the PR2 and why did they select prescale 4 and PWM Period Setting(4.88KHz). If is for my case what PWM Period Setting is should use? The reason for using PWM Frequency 4.88KHz is it to obtain the PR2 = 255? And if it is the reason may i know why is has to be PR2 = 255? The formula that i know is PR2 = [Fosc/(Fpwm x 4 x Prescale x N] -1
Thx
Ken2
Newbie
 
Posts: 10
Joined: Tue Feb 05, 2013 12:18 pm

Re: Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby yonghui » Wed Feb 27, 2013 10:03 am

Hi, I had bought the SPG30E-300K motor, the problem is i does not understand the code provided by Cytron:
{
CCP2CON = 0b00001100;
PR2 = 0xFF; //PWM Period Setting (4.88KHz)
T2CON = 0b00000101; //Timer2 On, prescale 4
}

I am using a PIC18F4520, 4Mhz crystal and L293d IC to interface with the encoded motor. i cant understand the calculation for the PR2 and why did they select prescale 4 and PWM Period Setting(4.88KHz). If is for my case what PWM Period Setting is should use? The reason for using PWM Frequency 4.88KHz is it to obtain the PR2 = 255? And if it is the reason may i know why is has to be PR2 = 255? The formula that i know is PR2 = [Fosc/(Fpwm x 4 x Prescale x N] -1
Thx



for PWM u can refer to the datasheet of PIC to get more understanding. period register/PR is to set the period of pulses, normally in KHZ range. timer register TMR2 will compare with the PR to set the period of the pulse. PR val and prescale can be set to any value that can ouput the correct period. CCPR register will determine the duty cycle.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby Ken2 » Wed Feb 27, 2013 5:09 pm

Thx for replying. I have another question which is the calculation for the motor's rotation angle:
desire_angle = degree;
//in this case gear ratio is 1:20
gear_ratio = 20;
//convert the desired agle to the total angle at the rear shaft (multiply gear ratio)
smalldegree = desire_angle *gear_ratio;
//12 count per revolution at the rear shaft
//meaning each count represent 30 degree
//to calculate how many counts needed for the desire angle
//we divide total angle at the rear shaft with 30 degree
limit = smalldegree/30;
I do not understand what is the difference between rear shaft and the main shaft. In Cytron's datasheet http://www.cytron.com.my/usr_attachment ... Manual.pdf says that 60 counts per main shaft revolution for 1:20 geared motor and the code provided says 12 count per revolution at the rear shaft. So, my motor is SPG30E-300K, what formula should I use to obtain the limit value.
limit = smalldegree/ ?(what value);
I would like to know how to calculate the resolution of the motor can rotate. For example (Rotary Encoders usually offer 100 to 6,000 segments per revolution. This means the encoder can provide 3.6 deg of resolution for 100 segments and 0.06 deg of resolution for 6,000 segments.)

Another question is about this code>>> while(countAB < (limit-countover+3))
why limit-countover+3? +3 is for what?
Thx
Ken2
Newbie
 
Posts: 10
Joined: Tue Feb 05, 2013 12:18 pm

Re: Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby yonghui » Wed Feb 27, 2013 5:57 pm

i think it is this way. rear shaft means the actual motor shaft and main shaft is the shaft after the gear. from the datasheet u post. the enconder is attached to the rearshaft. and its stated that 3 pulses per revolution of the rear shaft. this means tat if the main shaft complete a cycle, the rearshaft completed 20 cycles since the gear ratio is 1:20. 20 cycles of the rear shaft will give 20*3pulses= 60 pulses of the encoder.
your motor ratio is 1:300 so one revolution of output shaft will give 300*3pulses=900 pulses.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby Ken2 » Thu Feb 28, 2013 6:54 pm

Thx for replying. But can anyone explain how does the Cytron's formula works? I mean the formula given in the sample code http://www.cytron.com.my/viewProduct.ph ... %20Encoder it is at the bottom of the page, column number 2 (PLS download and have a look on the Sample Code). It state provide the formula:

desire_angle = degree;
//in this case gear ratio is 1:20
gear_ratio = 20;
//convert the desired agle to the total angle at the rear shaft (multiply gear ratio)
smalldegree = desire_angle *gear_ratio;
//12 count per revolution at the rear shaft
//meaning each count represent 30 degree
//to calculate how many counts needed for the desire angle
//we divide total angle at the rear shaft with 30 degree
limit = smalldegree/30;

For motor that have gear ratio of 1:20 will have 12 count per revolution rear shaft (what is 12 count per revolution rear shaft). Thus 360/12 = 30 degree(does this means that 30 degree is the resolution of the encoder?). Then the limit = smalldegree/30. My another problem is my motor is 1:300 will it have 12 count per revolution or more and why is it so? next question will be limit= smalldegree /30<<<< THIS 30 DEGREE IS FOR gear ratio 1:20. How about gear ratio 1:300, what will be the value limit = smalldegree/(30??????? or other value and why is that value)?

Another question is about this code provide by Cytron's The overshoot section: while(countAB < (limit-countover+3))
why limit-countover+3? +3 is for what?

Thx.
Ken2
Newbie
 
Posts: 10
Joined: Tue Feb 05, 2013 12:18 pm

Re: Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby ober » Thu Feb 28, 2013 9:29 pm

1st, you need to understand what is quadrature encoder.

Please visit one of our tutorial: Quadrature Encoder

As highlighted by yonghui, rear shaft is the shaft of motor, main shaft refer to the shaft after gear, which is the front of motor, the shaft we use to drive wheel or mechanism. In order to know the angle or position of main shaft, we need to know how many pulses will be generated when main shaft rotate complete a circle or a round. Then we divide it with 360 degree, we get the resolution of encoder pulse, means 1 pulse represent how many degree of rotation on the main shaft.

Those motor that you are using have what we call quadrature encoder at the back. It is very complex encoder if you are beginner, that is the reason why I ask you to study the tutorial, not once, but in detailed. It will squeeze your brain juice 8-)

There are 3 output encoding if you know. The User's Manual of SPG-30E stated that 3 pulses per rear shaft revolution, single channel output. This means, if you take single channels (please refer the tutorial) mean you are using 1X encoding. You will have 3 pulses only per revolution or per circle or per rotation. If you are using 4X encoding, you will get 12 pulses per rotation. That is the resolution of rear shaft encoder, 30 degree per encoder pulse.

That refer to rear shaft. Since you need to know the resolution of main shaft. If you have gear ratio of 300, it means motor shaft (before gear) rotate 300 rounds, the shaft after the gear will rotate 1 round. So from here, output encoder pulses of rear shaft will need to be multiply with 300 to get output encoder pulses of main shaft, 300 * 12 = 3600. Resolution of your main shaft if you are using 4X encoding, is 360 degree/3600 pulses = 0.1 degree per pulse.

Understand this 1st.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby Ken2 » Fri Mar 01, 2013 3:44 am

Thx for replying i finally understand the formulas given. My other problem will be the overshoot compensate configuration:

while(countAB < (limit-countover+3)) //while counter is in the (limit-previous overshoot +3)
{ //for the 1st time, the motor will overshoot because PIC
initA = A; //get current A value //has not get the overshoot value to compensate
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
stopA=A; //stopA store A value right after state change
stopB=B; //stopB store B value right after state change
}
//once program exit the previous while loop
//meaning that the motor has reached the desired angle

i dont understand why "while(countAB < (limit-countover+3))" but i understand the content of the while loop for example the countAB++. I have no idea on the second half of the overshoot code that shown below. :(


countover=0; //reset overshoot counter
end=0; //reset end function flag
while(end==0) //while the flag is not set
{
do
{
if(countstop>10000) end=1; //set end flag if stop counter >10000
else
stop(0); //brake the motor
countstop++; //stop counter increment by 1
}while(stopA==A && stopB==B && end==0); //loop if states do not change
//if states change,meaning overshoot occur
countover++; //increment overshoot counter
countstop=0; //clear stop counter
stopA=A; //stopA store the current A value
stopB=B; //stopB store the current B value
}

}

THX.
Ken2
Newbie
 
Posts: 10
Joined: Tue Feb 05, 2013 12:18 pm

Re: Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby A380 » Fri Mar 01, 2013 3:32 pm

Hi,

About the "while(countAB < (limit-countover+3))", I think it is for offsetting back the encoder value due to previous motor turning overshot. Because the motor can't stop immediately when it is running. And I found that the sample code you used now doesn't have direction monitoring.

So, please download the attachment here for another sample code. But this sample code is for PIC18F4431. Since you are using PIC18F4520 (which also has external interrupt pins, INT0 and INT1), it is easy to modify it for your PIC.
For example:
#include <p18f4431.h> should be changed to #include <p18f4520.h>
Now, connect the Channel A and Channel B to INT0/RB0 and INT/RB1 respectively. PIC18F4520 doesn't have QEI feature, so choose SPG-30E-INT.c instead of SPG-30E-QEI.c for your case.
This sample code is demonstrated in this video. Watch it for more detail explanation. Enjoy! :)
Attachments
Sample Code - Quadrature Encoder.zip
(21.52 KiB) Downloaded 281 times
User avatar
A380
Discoverer
 
Posts: 120
Joined: Tue May 19, 2009 2:44 pm
Location: Malaysia

Re: Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby Ken2 » Fri Mar 01, 2013 4:59 pm

I have started trying some simple coding for the motor, the encoding output voltage is too low and it gives me a weird waveform. I measured it using an Oscilloscope. The waveform A and B does not looks like what it shown in the datasheet provided by Cytron. I also have try direct connect 11 volt to the + and - wire and provide 5 volt and ground for the encoder. Then, direct measure output wave of wire A and B using the Oscilloscope.
Attachments
NewFile0.jpg
Encoder A and B output waveform
NewFile0.jpg (46.27 KiB) Viewed 3765 times
Ken2
Newbie
 
Posts: 10
Joined: Tue Feb 05, 2013 12:18 pm

Re: Sample Code Encoded DC gear Motor SPG30E-300K‏

Postby A380 » Fri Mar 01, 2013 5:16 pm

Did you pull up the Channel A and Channel B pins to 5V? Refer the schematic in that video.
User avatar
A380
Discoverer
 
Posts: 120
Joined: Tue May 19, 2009 2:44 pm
Location: Malaysia

Next

Return to DC Motor

Who is online

Users browsing this forum: No registered users and 19 guests

cron