Pages: [1] 2 3 4   Go Down
Author Topic: Frequency Question  (Read 5481 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have an ArduPilot Legacy board.
It has been made a PCB for it, and it's using the the third timer for generating frequencies (pins 3 and 11). The idea is that when I made the PCB, I tought that the non-exactly frequency the Arduino is giving won't make my motors work abnormally.

What I need is an exact frequency of 10kHz (for my motor controllers).

The question is how do I set them to this fixed frequency? Plus, I need to send the pulse at a certain duty cycle. For example 30%, or 21%. Do I have to use another function than analogWrite(), just because is a particular frequency? How?

Unfortunately, I'm a beginner in terms of timers, so sorry me for this question which might be trivial.

Thank you,
RobertEagle
« Last Edit: September 26, 2012, 02:34:44 pm by RobertEagle » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could use timer 1 (which outputs to pins 9 and 10). With PWM mode and counting up to 1600 and a prescaler of 1 you would get a frequency of 10 KHz exactly.

Or Timer 2 (which outputs to pins 3 and 11) with a prescaler of 8 and counting up to 200 you would also get a frequency of 10 KHz.

The problem is that for a frequency of exactly 10 KHz you need both sides of the timer  (the A and B side) where the A side counts up to the required figure (eg. 1600 or 200) and the B side gives you the duty cycle. So you might need to use pins 3 and 10 (the B side outputs).

The resolution of the duty cycle has to fit into the count. In other words, with timer 2 the duty cycle will be in the range 0 to 199 which would give you a 0.5% duty cycle resolution.

I have some example code on this page:

http://www.gammon.com.au/forum/?id=11504

Near the bottom where it says "Modulating 38 KHz signal" is an example of generating 38 KHz frequency with a variable duty cycle. You just have to change one constant to change from 38 KHz to 10 KHz.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did understand how the frequency is being calculated.
I have changed some things from that particular sketch (which was including a 38kHz signal, along with duty cycle).

Is this how it should like? I want to specify that I need timer2 which is using pins 3 and 11 on the Arduino.
Code:
// 16 MHz clock divided by 10 KHz frequency desired
const long timer2_OCR2A_Setting = 16000000L / 10000L;

void setup()
 {
  pinMode (11, OUTPUT);
  pinMode(3,OUTPUT);

  // set up Timer 2 - gives us 10 KHz
  TCCR2A = _BV (WGM10) | _BV (WGM11) | _BV (COM1B1); // fast PWM, clear OC1B on compare
  TCCR2B = _BV (WGM12) | _BV (WGM13) | _BV (CS21);   // fast PWM, 8 prescaler
  OCR2A =  timer2_OCR2A_Setting - 1;                 // zero relative  
  OCR2B = (((long)(0) * timer2_OCR2A_Setting) / 1024L) - 1;//initialize with 0
  OCR2A = (((long)(0) * timer2_OCR2A_Setting) / 1024L) - 1;//initialize with 0
  }

void loop()
  {
  OCR2B = (((long)(analogRead..etc) * timer2_OCR2A_Setting) / 1024L) - 1;
  OCR2A = (((long)(analogRead..etc) * timer2_OCR2A_Setting) / 1024L) - 1;
  }

PS: What's the purpose with those float types typed in the OCR2A and B? What does it do?
Plus I can't change the pins. They must be 3 and 11.
« Last Edit: September 28, 2012, 01:59:38 pm by RobertEagle » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does anybody know how? I just need a ,,quick and dirty" fix. It's quite urgent for me. And how I said, there are some fixed pins. Pins 3 and 11 working with timer2.

Thank you in advance,
RobertEagle
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33311
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
PS: What's the purpose with those float types typed in the OCR2A and B? What does it do?
There are no float types in OCR2A and B, what makes you think there are?

Quote
Plus I can't change the pins. They must be 3 and 11.
Yes that is right the hardware timers are attached to fixed pins, there is no way to change them.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have this setup on timer2:

1. TCCR2A - COM2A1 and COM2B1 are set on non-inverting mode, WGM20 and WGM21 are set on Fast PWM(mode 3, TOP at 0xFF,Update at BOTTOM, and TOV flag set on MAX)

2. TCCR2B - CS21 with prescaler set at 8

3. TCNT2 set at 200

4. OCR2n registers for the duty cycle.

This configuration should give a frequency of 10Khz=16000000/8(prescaler)/200(counter value).

This is my code:
Code:
void setup()
{
 pinMode(3,OUTPUT);
 pinMode(11,OUTPUT);
 TCCR2A = _BV(COM2A1) | _BV(COM2B1)|_BV(WGM20)|_BV(WGM21);
 TCCR2B = _BV(CS21);
 TCNT2=200;
}
void loop()
{
  OCR2A=75;//random value
  OCR2B=190;//random value
}

I have uploaded the code onto the Arduino, and checked with the multimeter the ,,voltage". At 200, instead of giving me 5V it gave me 3.92V, situation which was telling me that the value stored in TCNT2 wasn't considered.

What could be the solution here? What is wrong with me setting?

And referring my last reply, it was deleted because it had too many errors in what it was saying.

Thank you, again,
RobertEagle
« Last Edit: September 29, 2012, 08:40:04 am by RobertEagle » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33311
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Move all the code into setup, do not keep reloading those values rapidly with loop. A multimeter will not work with a PWM signal.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1.Well, the duty cycle must be variable, as I said, I have to drive 2 motors using 2 ESC's(controllers).

2.How am I going to control the motors then if I move all the code into setup?

3.On a multimeter's screen will appear a sort of ,,voltage", which is the average of the duty cycle on that period.
« Last Edit: September 29, 2012, 10:39:33 am by RobertEagle » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1.Well, the duty cycle is variable, as I said, I have to drive 2 motors using 2 ESC's(controllers).

Most ESCs interface with an arduino as a servo device and as such one uses the servo library to control them. Why are you using low level timer commands to try and control the ESC. Perhaps if you posted a link to your ESC controller we would know more about your requirments and best solutions?

2.How am I going to control the motors then?
Again the servo library may be what you should be using?

3.On a multimeter's screen will appear a sort of ,,voltage", which is the average of the duty cycle on that period.

Only if your DMM is of a 'true RMS' type (more expensive models have this feature) you won't get accurate  values using simple DC voltage reading on a PWM signal.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1.My ESCs are Roxxy Robbe BL Control 710 and work with Roxxy Robbe 2216/25 Motors.

2.This is the official page of the ESC:
http://www.robbe.de/roxxy-blcontrol-710.html

And this is the only datasheet of the ESC:
http://www.4shared.com/office/IWZkRgDo/Anleitungen_INT_A_8637_8638_BL.html

3.Isn't the servo library too slow?
« Last Edit: September 29, 2012, 10:51:37 am by RobertEagle » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1.My ESCs are Roxxy Robbe BL Control 710 and does work with Roxxy Robbe 2216/25 Motors.

2.This is the official page of the ESC:
http://www.robbe.de/roxxy-blcontrol-710.html

Looks like a standard R/C type motor speed controller designed to interface as a servo device to me.

And this is the only datasheet of the ESC:
http://www.4shared.com/office/IWZkRgDo/Anleitungen_INT_A_8637_8638_BL.html

3.Isn't the servo library too slow?

No, your confusing the 10KHz switching rate the ESC uses to drive the three motor windings attached to it. The command inputs the ESC needs to control the motor are standard 1-2 millisec pulses at a 20-25millisec frame rate, just like all standard R/C servos and ESC controllers. Use the standard Arduino servo library. You will have to find out what the proper 'arming sequence' for the ESC before it will enable output commands to the motor, a standard 'safety feature', and this can vary amoung ESC manufactures.
Lefty

« Last Edit: September 29, 2012, 11:04:33 am by retrolefty » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1. So how can I find the real ESC input frequency?
2. Does the duty cycle of the ESC need to be incremented sequentially or is it just possible to directly switch from 0 to max 255?
3. I tried last week at 490Hz and they worked very badly. The motors were responding very hard, and since than I tried to find another solution. And sadly, the motors worked only by incrementing the duty cycle with one.
4. It means that these ESC's aren't designed for acrobatic maneuvers?
Logged

Denmark
Offline Offline
Edison Member
*
Karma: 35
Posts: 1051
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1. You don't have to find the  input frequency, servo library will take care of that (btw, its ca 50HZ)
2. You control the ESC, just like a servo, take a look at the servo knob/sweep example sketch
3. se 2
4. I dont know why it should not work with  acrobatic maneuvers, but as it is designed for about 100W motor (3 lipo's at 10A), it should be a leightweight plane if you want to do any serius 3D flying.

The arming sequence that Lefty mentioned can be found here

http://www.produktinfo.conrad.com/datenblaetter/200000-224999/206291-an-01-ml-ROXXY_BLUSHLESS_CONTROL_710_de_en_fr.pdf
« Last Edit: September 29, 2012, 11:19:46 am by Erni » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1. So how can I find the real ESC input frequency?

Why do you need to know this? The arduino standard servo library handles those low level details and continously sends the last servo.write command you sent at the rate the servo is designed to handle. Using the myservo.writeMicroseconds(xxxx); you get at least 1,000 counts of resolution (1000-2000usec) of speed control representing 0-100% rpm from the motor.
http://arduino.cc/en/Reference/Servo


2. Does the duty cycle of the ESC need to be incremented sequentially or is it just possible to directly switch from 0 to max 255?
Direct commands to any value within range and the servo library allows much more then 256 counts of resolution as already stated above.
3. I tried last week at 490Hz and they worked very badly. The motors were responding very hard, and since than I tried to find another solution. And sadly, the motors worked only by incrementing the duty cycle with one.

That's because were not using the standard servo library commands to control your ESC and you probably not correctly simulating what a proper servo command pulse looks like? Again use the servo library to control your ESC, to resist is futile.  smiley-wink  
4. It means that these ESC's aren't designed for acrobatic maneuvers?

Airframe, motor power, and pilot skill is the limiting factor in acrobatic maneuvers, not the servo command rate to the ESC.
Lefty

« Last Edit: September 29, 2012, 11:27:38 am by retrolefty » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My idea was to make another library, which had to be more efficient in terms of cpu usage.

What I have designed is a testing board (which I'll post after I finish the 1st stage, the motors and the equilibrium library). To start, I've used just 2 of the 4 motors I have for testing. Later, I'll make a quadcopter having a maximum of 550-600 grams. And my target is to make the quadcopter super responsive to stimuli. And you know how frustrating is to know that the motors don't work after 350 hours of total work?

That's why I wasn't thinking at the Servo library. I thought it was too slow.

I'll post later the results of using the Servo library.

Thanks,
RobertEagle
« Last Edit: September 29, 2012, 11:37:55 am by RobertEagle » Logged

Pages: [1] 2 3 4   Go Up
Jump to: