Sensored BLDC controller help (controlling the speed of the motor)

I am trying to make a software for controlling a high current sensored BLDC motor(3 phase star winding at 7~12v).I need some help with the program.I have written the most of the code but I stuck in one section.The control of the speed. I have the code that finds the rotor and sends the power to the proper coils (switchCase statement) and also the pwm variable( 0-255 using analogRead / 4 from a potentiometer).The question is : How can I control the speed? I thought it can be done if I "pwm" the whole "void CW_Rotation" part.So the pulse of sense-and-fire goes longer as I turn the pot and the speed rises.How can I do this?

Here is some of my code:

void CW_Rotation ()
{
  switch (Hall_Sens_Code)
  {
  case 0xa0:
    {
      Step1();
      break;
    }
  case 0x80:
    {
      Step2();
      break;
    } 
  case 0xc0:
    {
      Step3();
      break;
    }
...... and keeps going until step 6
  } 
}

_________________________________



void Analog_Pot_in_to_PWM () // Goes in "void loop" part.
{
  PWM_AnalogValue = analogRead(VR1_In); // Read value from potentiometer.(0-1023)
  analogWrite(PWM_Freq_Pin , PWM_AnalogValue / 4); // Send analog value divided by 4 (1023/4=255) to PMW_Freq_Pin (D3).
}



_________________________________


I tried this but it's not functional,I need linear speed curve.
The part 1 which is no throttle at pwm 0 is fine.


  {
    if (digitalRead(PWM_Freq_Pin) == 0) 
    {
      FETs_Off () ;
    }
    else if (PWM_Freq_Pin > 0)
    {
      CW_Rotation () ;
    }
   }

Also this goes to "void loop"

In general PWM should work independently of commutation, but there are issues - firstly the PWM frequency should not approach the commutation frequency or things will go wrong. Also the PWM frequency needs to be high enough for the windings involved (higher for low-inductance low-resistance windings, in general). And this means the driver switching speed needs to be up to the task... I suspect a high current motor is going to need faster PWM and high-speed drivers - you don't mention the hardware details. For instance are you driving all 6 output devices with PWM or only 3 or them, or is there external controller logic that handles this?

For fine speed control you need to measure the rotation speed and apply negative feedback to compensate for motor losses under load.

The motor has 4.5Y windings and later maybe 4.5D.I will be using only N-channel MOSFETs and of course driver ICs (IR2101S).No external logic.I have seen multiple hobby ESCs having these compoments.I have the hardware(just not assembled). Could you provide a code example? The rotor position sensing code is ok.But I need to control the speed with a variable from 0-255 if possible,because I may add a PPM to PWM function for using hobby receivers(0.5-2.0ms).

"For fine speed control you need to measure the rotation speed and apply negative feedback to compensate for motor losses under load." There is no commercial ESC that does this except those for RC helis(aka RPM governor mode - keeps RPM steady all the time).I will also need to use thermal and low voltage cut-off functions so I don't like to have an interference between them(I don't know if Arduino can run all these tasks in parallel)

4.5Y? 4.5D? star and delta? 4.5? 4.5 turns? Need to know the winding inductance and max RPM to determine a good PWM rate. A rough schematic would be useful.

Motor inductance? How to calculate it? The stator has a sticker which writes "4.1 4.1 4.1" I think it's something about each phase.(maybe inductance in mH or it's a crazy number?). Here is the link for the motor's webpage http://teamnovak.com/products/brushless/ballistic_550/index.html Calculate about 55K RPM (5000kV @ 11v or [u]~ 6800kV @ 8v - for faster motors - smaller size , with about same turn # and a little higher inductance[/u]) All motors that may be used are from 17.5T to 4.5T.Hobby grade brushless motors.Usually a 13.5T motor has 19.0 to 22.0 mH. A similar Arduino code like mine is also used from other people,but I started writing it from scratch(like they did from AVR application notes - ex. AVRxxx).They had success with their ESCs and they were performing as good as commercial units but the code didn't suit my needs and was a little confusing they way it was written.Also they had the 0-255 variable.

"star and delta?" The motor I have currently is Star.I will rewind it to Delta.

The current motor has a 2-pole rotor.This is a picture of the stator http://c0456141.cdn.cloudfiles.rackspacecloud.com/images/large/nov/novs6504.jpg The sticker is either "3.2 3.2 3.2",either "4.1 4.1 4.1" I don't remember exactly....

I would also accept any other method availiable,but need a variable compatible with pot(later will go PPM).

the best way to do speed control on any type of motor is to to do 2 control loops: 1/ inner torque loop (current loop): a/ input is current reference b/ measure current c/ simple PI (or even P) control d/ output is duty cycle 2/ outer speed loop a/ input is speed reference b/ measure speed c/ simple PI (better than P unless you're OK with some speed error depnding on load) hope this helps...

This seems useful.Do you have any sample code? (a link maybe?) I don't want to burn up something... I also need something efficient.I don't know many about the methods you posted,but a quick search shown both are OK(I found only PID controller on Wikipedia http://en.wikipedia.org/wiki/PID_controller I think I will follow the inner torque loop.(As many manufacturers - I suppose - The second method keeps RPM always steady,which is not required for my application.I think the following video describes both ways

http://www.youtube.com/watch?v=_symMnZ4Ayw

Many commercial ESCs have PWM frequency of 8, 16 , 32 or 52kHz(The best series has 52kHz).And my current car ESC has 2 settings 8 and 16 (I use 16kHz)