problem with PWM

08 Apr 2010

Hy,

First excuse me for my english i am french.

I want use the PwmOut with the mbed LPC1768, i use this Pwm to control 4 servomotors, i receive a trame from a remote with the position of my servomotors (it's a remote for a guided airplane).

But when i send a pulsewidth < 1 ms the pwm doesn't run.

The comportement of the pwm is binary, in fact when the pulsewidth is 2 ms so the servo run and it goes to right but if the pulsewidth is < 2ms the servos go to left directly(until the maximum).

I send the pulsewidth with a algo and the result of the algo it's between 0,9ms and 2ms the extreme values for a sermotor.

For me, the problem is the pwm but i don't know how solve this problem, but i don't want use the servo library.

I give my code to help understand.

Thanks.

#include "mbed.h"
#include "Servo.h"

Serial telecommande(p13, p14); //liaison serie mbed <-> module telecommande
Serial pc(USBTX, USBRX);// liaison serie pc <-> mbed

PwmOut gauche_hori(p26);//the pwm for my 4 servomotors 
PwmOut gauche_vert(p22);  
PwmOut droite_hori(p23);
PwmOut droite_vert(p24);


/*After this variables are to the remote and they aren't important for the resolution of my problem*/
int  tab[6],msb,lsb;
char a;
float valeur_max[4]={924,1100,1100,956};//max values from the manettes of the the remote
float valeur_min[4]={270,172,176,76};//min values from the manettes of the remote
float positionzero[4]={327,464,462,440};//the central position the manettes of the remote


/*it's the fontion to the interruption when i receive the trame from the remote, i work this trame to extract
the position for my different servo and i stock this position in a table */
void reception()
{

a=telecommande.getc();
if( a=='$')
   {
   
   a=telecommande.getc();
   if(a=='T')
       {
    
     pc.printf("trame ");
       for(int i=0;i<=5;i++)
           { msb= telecommande.getc();
             lsb=telecommande.getc();
           tab[i]=msb*256+lsb;
           }      
       }    
   }
}


int main()
    {
    gauche_hori.period(0.022);
    gauche_vert.period(0.022);
    droite_hori.period(0.022);
    droite_vert.period(0.022);
    pc.baud(9600);
    telecommande.baud(115200);
    telecommande.attach(&reception) ;
    
    
    while(true)
     {
        
/*i choose the pulsewidth with a little algo it's a rule of three */
         droite_vert.pulsewidth_ms(float((1+(((tab[0]-positionzero[0])*0.55)/positionzero[0]))));
         droite_hori.pulsewidth_ms(float((1.5+(((tab[1]-positionzero[1])*0.55)/positionzero[1]))));
         gauche_vert.pulsewidth_ms(float((1.25+(((tab[2]-positionzero[2])*0.55)/positionzero[2]))));
         gauche_hori.pulsewidth_ms(float((1.45+(((tab[3]-positionzero[3])*0.55)/positionzero[3]))));
            
     }
}

08 Apr 2010

Hi Clement,

I think your problem is simply using pulsewidth_ms() which will naturally only give you a resolution down to 1ms steps as it is represented as an int. Hence the behaviour you see.

If you use pulsewidth() with the value as a float represented in seconds, that should work. (Or if you want to use integer values, then pulsewidth_us() gives a resolution of 1us).

Simon

08 Apr 2010

thanks for your quick reply, i will try tomorow.

 

Clément

09 Apr 2010

Hi Simon

I replaced my instruction pulsewidth_ms() by pulsewidth() and now it's run perfectly.

thanks for your help.