Society of Robots - Robot Forum

Software => Software => Topic started by: saliraza on August 28, 2010, 05:27:16 PM

Title: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: saliraza on August 28, 2010, 05:27:16 PM
Hello All,

Hope everyone is busy building cool robots ..... I am trying to connect a Futaba R2004GF radio receiver to my Axon and to read the 4-channels, and looking for some help regarding this topic. Im already using E3,E4,H3,H4 for running four servos, and trying to use PWM Capture method to read the pulse width of R/C channels. The codes provided include PWM functionality but I have not seen any code for PWM Capture to measure incoming pulse widths. The idea is to use interrupts to do the work in background and not to burden the control code.

Can any one who has successfully connected a R/C receiver to Axon help in this matter and share some code?

Any help would be appreciated.

Regards,

Ali.
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: Admin on August 28, 2010, 07:37:35 PM
If you are using the Axon and the original code base, use this:
http://www.societyofrobots.com/robotforum/index.php?topic=3099.0 (http://www.societyofrobots.com/robotforum/index.php?topic=3099.0)

If you are using WebbotLib, you want to use the function pin_pulseIn found in the iopin.h section of the manual (page 52 of v1.24).
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: KurtEck on August 29, 2010, 08:13:24 AM
I have not done it yet on the Axons, but have done it on other processors such as a BasicAtomPro...

If you end up having to use something like pin_pulseIn, it is important to experiment with the ordering of the call or to have a logic analyzer... Why: for example if you find that the PWM signals come in the order: 1 2 3 4 and that the signals either overlap or one starts at the same time as the other completes, you may find out that if you make the calls like: pin_pulsein(<pin1>...);pin_pulsein(<pin2>,...);...
That by the time your call to pin_pulsein for pin 1 completes and you enter into the call for pin 2, that the pin 2 pulse has already begun and you miss that transition and the call has to wait for a complete servo pulse cycle (~20ms) before it can catch it...

So in this case it is much better to try your calls in the order: 1 3 2 4.

Another option which I saw on the web and am trying on my HITEC receiver is to hack the receiver and bring out a signal from the clock of the shiftout register.  I found this by doing a goggle of  "Arduino PPM".  I remember seeing pictures on how to do this on the futaba...

Kurt
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: Webbot on August 29, 2010, 11:24:17 AM
If you are using WebbotLib, you want to use the function pin_pulseIn found in the iopin.h section of the manual (page 52 of v1.24).

Or if its all happening under interrupts then try using the code in 'pinChange.h'
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: saliraza on August 29, 2010, 07:52:14 PM
Guys thank you very much for the replies....

I am currently using a Resistor/Capacitor(R=12kohm, C=0.1uF) circuit to convert PWM into an analog signal and then fed to ADC. My issue is that the ADC outputs a very noisy signal ranging from 0.41V to 0.22V. The ADC vref is 2.56V. To smooth out the ADC output and to remove offset, I am performing a method:

      a6=(a6-38);
      if(loopCounter<=20){sum=sum+a6;loopCounter=loopCounter+1;}
      else {throttle=sum*0.01;loopCounter=0;sum=0;}

This gives me a range of approx. 0-1000 for throttle stick movement from low to high. Still the numbers are jumping within 100's and only by dividing it with 100 I can get a solid 0-9 integer range. This is not enough for remote throttle control. I am looking for something like 0-250.

I am thinking about using a 10kohm, 10uF as per (http://www.societyofrobots.com/remote_control_robot.shtml (http://www.societyofrobots.com/remote_control_robot.shtml)) and also adding a opamp to set the range to suit the ADC, but I really need to confirm if that will fix the noise issue? Square wave signals are really bad when noise is concerned.

The other option was to use the available PWM Capture I/O pins, but I really do not know exactly how to configure the code provided with Axon. I am only using timer 3&4 and have timer2&5 spare. Setting PWM for Servos was easy:

Initialize once
   PWM_Init_timer3_E3(10);
   timer3PWMInitICR(4999);//4999
   PWM_Init_timer3_E4(10);

   PWM_Init_timer4_H3(10);
   timer4PWMInitICR(4999);
   PWM_Init_timer4_H4(10);

   PWM_timer3_On_E3();
   PWM_timer3_On_E4();
   PWM_timer4_On_H3();
   PWM_timer4_On_H4();

and then just

         PWM_timer3_Set_E3(250);
         PWM_timer3_Set_E4(250);
         PWM_timer4_Set_H3(250);
         PWM_timer4_Set_H4(250); //250

bam motor works good! I was wondering if there is some way I can setup timer2 or 5 such that it checks the input based on interrupt and just let me read a value representing the incoming pulse width. Just the way mentioned above, its setup to run the motors on PWM? I hope it makes any sense :(

I liked the code from "Tsukubadaisei":

void radio(void){//ラジコンの出力パルスの幅を計る関数
  int i;
  //DDRA &= ~(1<<DDA0);
  //PA0<->chan1
  for(i=0;i<2;i++){
    while((PINA&(1<<PINA0))==0){}
    TCNT0=0;
    TCCR0|=(1<<CS02);
    while((PINA&(1<<PINA0))==1){}
    TCCR0&=~(1<<CS02);
    if(i==1){
      chanVal[CHAN1]=TCNT0;
      //printf("\r chan1 = %d\n",chanVal[CHAN1]);
    }
  }
........repeats so on...

However, its meant for a PPM signal, I am using individual DMUXED signals from 4-channels.

Admin, can you kindly help me with the code? I have timer2_H6 available, how can I read the pulse width from throttle using the "Axon Source Code v1.11" library files? Just one throttle channel will be fine for a start. My current code is working perfectly with "Axon Source Code v1.11" and currently I am not ready to switch to "Webbotlib", definitely in near future though.

Regards,

Ali.
 
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: Admin on August 31, 2010, 04:41:15 PM
What do you mean by 'ADC outputs'? The ADC is an input, so I'm confused :P

Quote
Admin, can you kindly help me with the code? I have timer2_H6 available, how can I read the pulse width from throttle using the "Axon Source Code v1.11" library files?
Its all here:
http://www.societyofrobots.com/robotforum/index.php?topic=3099.msg25106#msg25106 (http://www.societyofrobots.com/robotforum/index.php?topic=3099.msg25106#msg25106)
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: saliraza on September 02, 2010, 01:48:22 PM
Hello John, Im back with an issue, any help will be greatly appreciated!!!

I worked with the code, used it as it is in Axon, did not worked..... then modified it:

First made change in SoR_Utils.h:
   DDRA = 0b00000000;  //configure ports for output=1, for input=0

Then used this loop in code:
  for(n=0;n<2;n++){
    while(PORT_IS_OFF(PORTA,0)){rprintf("PA0=0\r\n");}
     reset_timer2();
    while(PORT_IS_ON(PORTA,0)){rprintf("PA0=1\r\n");}
    if(n==1){
      CHAN1=(get_timer2_overflow()*65535+TCNT2);
     rprintf("CHAN1=%d\r\n",CHAN1);
    }
  }

the loop stucks at while(PORT_IS_OFF(PORTA,1)) and all I see is PA0=0 .....no matter how much i move the throttle stick ....I manually connected a wire from A0 to GND and then to VCC=5V, still no change ....the while(PORT_IS_OFF(PORTA,1)) keep on going.....

Can you tell me what could be wrong here? Is there any issues with PortA on Axon? Instead of PORT_IS_OFF, and PORT_IS_ON, I have tried bit_is_clear, and bit_is_set options as well. Is there any thing need to be done with pull-up resistors?

Admin, Please Help!!!

Regards,

Ali.
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: Admin on September 02, 2010, 03:44:18 PM
Try this:

Empty out the while loop so it just hangs if the port is low . . .
while(PORT_IS_OFF(PORTA,0)){//do nothing}

See what happens . . .
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: saliraza on September 02, 2010, 10:27:45 PM
When I empty the while loop as: while(PORT_IS_OFF(PORTA,0)){//do nothing}

I just get nothing it remains in that loop even if I manually connect PORTA0 to logic high(5V regulated pin on Axon). Also, when I comment out the first while loop then I could see the rprintf("CHAN1=%d\r\n",CHAN1); working....this is because while(PORT_IS_ON(PORTA,0)){rprintf("PA0=1\r\n");} condition results in False and does not execute this while loop.....  From this what I understood is that no matter what PORT_IS_OFF condition is always true and PORT_IS_ON is always false ...... even if I am manually connecting PA0 to LOW or HIGH logics.

Whats causing this?  PORTA is digital I/O ..... I configure it for input with this command in SoR_Utils.h:
DDRA = 0b00000000;  //configure ports for output=1, for input=0

I have even tried pull-up resistors (enable OR disable) ...even it is not needed here!
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: saliraza on September 04, 2010, 10:59:50 AM
Anybody?
Title: Re: Axon/Axon2 PWM Capture for R/C Receiver... HELP!!!
Post by: Admin on September 04, 2010, 01:41:35 PM
I'm starting to wonder if there is a bug in that somewhere . . .

try changing your while loop of:
while(PORT_IS_OFF(PORTA,0))

to
while(bit_is_clear(PINA, 0))