Go Down

Topic: 3 axis auto stabilized platform (Read 76438 times) previous topic - next topic

mem

#15
Mar 05, 2008, 06:07 pm Last Edit: Mar 05, 2008, 06:08 pm by mem Reason: 1
There is some info on this site http://www.mp.ttu.ee/risto/rc/electronics/radio/signal.htm about receiver pulse output  that has what looks like the wavform for a Futaba PCM1024 receiver that appears to be sequential. I believe Futaba have updated the design in this model over time without changing the model number so yours may be different.

axileon

actually i read that website before... the pulse that I am getting is actually 1.5 ms pulse with regular intervals.  i read it on the scope.. it doesnt look sequential.

i tried your code... the output i get is kinda not really correct.

somehow only the 1st channel is read correctly but not all the time.
and channel 2/3 is just 1 or 2 numbers at time..

maybe using the multiplexer would be a better approach?

mem

#17
Mar 06, 2008, 05:47 am Last Edit: Mar 06, 2008, 05:53 am by mem Reason: 1
How about posting the debug output so I can see what the software thinks is happening. Do a run with only one channel connected and another run with three channels connected if you can.

That code expects the pulses to be sequential so if they are not then you could use the multiplexer but that would mean that your data will be updated less frequently, every 3 frames (60ms) for 3 channels but that may be ok in your application.




axileon

#18
Mar 06, 2008, 06:16 am Last Edit: Mar 06, 2008, 10:01 am by axileon Reason: 1
hi

the 3 channels debug is available at this url http://axileon.com/blog/debug_3channels.jpg

the 1 channel is here. http://axileon.com/blog/debug_1channel.jpg

for the 1 channel.. i noticed something strange.

the 1st channel will output the correct pulsewidth if i turn off the transmitter. but when i turn it on,it gets the output similar to the image above. this is so if I pass the signal thru the diode...

but If i drive the whole thing directly by the signal from the receiver. everything is fine.

i also tried using the mutiplexer method... for 3 channels...

however i only got readings for the channel 1. the other 2 channels are zero. for 3 channels.
i.e channel 1 has width 1450,(this is actually the value for channel 3)
channel 2 has width 0.
channel 3 has width 0.


mem

#19
Mar 06, 2008, 12:16 pm Last Edit: Mar 06, 2008, 12:40 pm by mem Reason: 1
It does look like the channel pulses are not sequential so you will probably need to use the multiplexer with your receiver. The challenge is to get the switching of the multiplexer synchronized so that you don't miss part of a pulse. To do this you need to ensure that you only switch the multiplexer to the next channel after that channels pulse goes low (that's the easy part) and you need to wait before taking the next pulse width measurement until you are sure that channels pulse has not already gone high.

One way to do this is to take two readings for each channel before moving on to the next one. It won't matter if the first measurment was in error because it will be overwritten by the second. The disadvantage of this technique is that it takes twice as long for changing pulse widths to be recognized but that may not matter in your application.

If you use this method then you can ignore the existing code to detect the sync pulse and replace that with a counter that increments the channel counter after the second trailing edge is detected.

If that isn't clear then I will try to post some pseudo code.

Or, if it is ok to miss some pulses then perhaps you should reconsider using pulsein with the multiplexer. The logic would be similar to the above:  Wait for the trailing edge of the previous pulse, then store the next pulse in the channel array, increment the channel and repeat.
The advantage of the interrupt version is that it will never miss a pulse edge no matter what your code is doing but it will only work if the pulses are sequential. As yours are not than perhaps the simpler PulseIn route is better for your app.


axileon

hm.. actually i tried the pulseIn with the multiplexer today.... the code got stucked at the 1st pulseIn
i really have no idea why... i'm very puzzled on that..

sorry to bother you. but how do you actually change your code to read 3 channels, 1 after another? i have been looking at your code but i still dont understand the ISR part..

maybe.. if possible can you explain it further to me? so that I can make any further changes I want if possible..

for your alternative solution, you're saying we call pulseIn 3 times in the loop? and then go to do something else then loop again?

mem

#21
Mar 06, 2008, 07:44 pm Last Edit: Mar 06, 2008, 08:30 pm by mem Reason: 1
Here is a modification of the interrupt sketch for the multiplexer logic (I have indicated where you need to add the code that will write the two pins gating the multiplexer to select the current channel.

A difficulty with the output of your receiver is that there is no way of knowing when the next channels pulse will start. We overcome this by waiting until we detect the end of the previous frames pulse before capturing the pulse width and moving on to the next channel.

This is implemented using a 'Ready' flag that is toggled on an off on successive pulses and ensuring that the measurement only happens every other trailing edge. Another flag is used to indicate that enough pulses have been detected to ensure that all channel data is correct.

here is the logic:

In setup,  channel is set to 1 and Ready flag and DataAvailable flags to set to false

When the first falling edge is detected Ready will be toggled so it will be set to true

When the rising edge is detected and Ready is true then the measurement proper starts

When a falling edge is detected and the Ready flag is true then the measurement ends,
the channel is incremented. If the channel count after incrementing is greater than the number of channels then the DataAvailable flag is set true and pulse data can be accessed
The ready flag will be reset back to false because it is toggled on every trailing edge.


I hope that is clear enough for you to think about a PulseIn version if you prefer that route.

Code: [Select]

#define icpPin            8   // this interrupt handler must use pin 8
#define TICKS_PER_uS      2    // number of timer ticks per microsecond
#define MAX_CHANNELS    3         // maximum number of channels we want  
volatile unsigned int Pulses[ MAX_CHANNELS + 1]; // array holding channel pulses width value in microseconds
volatile uint8_t  Channel;      // number of channels detected so far in the frame (first channel is 1)

boolean DataAvailable; // set to true when we have received data for all channels
boolean Ready;         // true when we are ready to detect the leading edge of a channel

// here is the logic of how this code works :
// to start, channel is set to 1 and ready to false and the DataAvailable flag is false
// when a negative edge is detected ready is set to true
// when a positive edge is detected and ready is true then the measurement starts
// when a negative edge is detected and the ready flag is true then the measurement ends, the ready flag is reset back to false
//  and the channel is incremented. If the channel count after incrementing is greater than the number of channels then
//  the DataAvailable flag is set true and pulse data can be accessed

                 
ISR(TIMER1_CAPT_vect){
  if( !bit_is_set(TCCR1B ,ICES1)){       // was falling edge detected ?  
        if(Ready ) {
             Pulses[Channel] = ICR1 / TICKS_PER_uS;  // store pulse length as microsoeconds
             if(++Channel > MAX_CHANNELS)
                  Channel = 1;       //reset the channel counter to 1      
             // Add code here to gate the multiplexer to the current channel          
        }          
        Ready = !Ready;  //toggle the ready flag
  }
  else {                       // rising  edge was detected  
       TCNT1 = 0;               // reset the counter      
                         
  }    
  TCCR1B ^= _BV(ICES1);                 // toggle bit value to trigger on the other edge    
}

void setup()                    // run once, when the sketch starts
{
 Serial.begin(9600);  
 pinMode(icpPin,INPUT);
 Channel = 1;            
 Ready = false;
 DataAvailable = false;
 TCCR1A = 0x00;         // COM1A1=0, COM1A0=0 => Disconnect Pin OC1 from Timer/Counter 1 -- PWM11=0,PWM10=0 => PWM Operation disabled
 TCCR1B = 0x02;         // 16MHz clock with prescaler means TCNT1 increments every .5 uS (cs11 bit set
 TIMSK1 = _BV(ICIE1);   // enable input capture interrupt for timer 1
}

int GetChannelPulseWidth( uint8_t channel) {
 // this is the access function for channel data
 int result;  
 if( DataAvailable  && (channel > 0) && (channel <=  MAX_CHANNELS)  ) {
    cli();             //disable interrupts
    result =  Pulses[channel] ;
    sei();             // enable interrupts
 }
 else
    result = 0;        // return 0 if no valid pulse is available  

 return result;

}

void loop()                     // run over and over again
{
int pulsewidth;

  // print the decoder state
  if(DataAvailable == false)
      Serial.println("The decoder has not detected all channels ");  
  else  
       Serial.println("The the channel data is valid");  


 // now print the channel pulse widths
 // they should be 0 if the state is not ready
 for ( int i =1; i <=4; i++ ){ // print the status of the first four channels
     Serial.print("Channel ");
     Serial.print(i);
     Serial.print(" has width ");
     pulsewidth = GetChannelPulseWidth(i);
     Serial.println(pulsewidth);
 }
 delay(100); // update 10 times a second        
}

axileon

sorry to trouble you again.
i tried the code with my code inserted the place where you told me to ....

however there was no output.... the debug keeps giving me the decoder has detected no channels and channels has width zero.


     
Code: [Select]
// mutiplexercode
                  if(Channel == 1)
                 
                   digitalWrite(g,LOW);
                   digitalWrite(a,LOW);
                   digitalWrite(b,LOW);
                   digitalWrite(c,LOW);
               
                 else if(Channel == 2)
               {
                    digitalWrite(g,LOW);
                   digitalWrite(a,HIGH);
                   digitalWrite(b,LOW);
                   digitalWrite(c,LOW);
               }  
                 else if(Channel == 3)
               {
                  digitalWrite(g,LOW);
                   digitalWrite(a,LOW);
                   digitalWrite(b,HIGH);
                   digitalWrite(c,LOW);
               }  


i tested the signal(to digital pin 8) on the scope. the signal looks fine but when i connect it to the arduino... nothing happens. do you know what's the problem?

mem

#23
Mar 08, 2008, 06:38 am Last Edit: Mar 08, 2008, 06:40 am by mem Reason: 1
You could test it with just one receiver channel connected directly to pin 8 to verify that the interrupt logic works. If you get output pulse data between 1 and 2 ms and it follows the transmitter control then the problem may on the multiplexer side.

Have you tried a  sketch without the interrupt code,  just your multiplexer code and a simple digitlaRead on pin 8.  You could connect resistors to provide high or low signals to the inputs to check that the multiplexer is working as you expect.

One comment on the multiplexer code, you should gate the multiplexer after the digitalWrites to the select  inputs, otherwise you will get spurious pulses that will confuse the logic.
If g is the gate and abc are the inputs:
 digitalWrite(a,LOW);
 digitalWrite(b,LOW);
 digitalWrite(c,LOW);
 digitalWrite(g,LOW); // enable the gate after selecting inputs

I hope that helps.

axileon

hi.. i dont think its the interrupt problem

i pulse the pin 8 directly with the receiver pulse... nothing is detected...

so i think the problem is with the interrupt code...

mem

#25
Mar 08, 2008, 08:45 am Last Edit: Mar 08, 2008, 08:53 am by mem Reason: 1
Try it with the following modification to the interrupt handler:

if(++Channel > MAX_CHANNELS)
{  // add the braces
        Channel = 1;       //reset the channel counter to 1      
             // Add code here to gate the multiplexer to the current channel          
         DataAvailable = true;  // this line is needed to indicate that the channel data is valid!!!!
}

edit: posted fix to set DataAvailable to true

axileon

yup. i'm running the latest code that you have given me. exactly the same as in post 21.

i think maybe there's problems with interrupt code

mem


axileon

#28
Mar 08, 2008, 09:15 am Last Edit: Mar 08, 2008, 09:23 am by axileon Reason: 1
hey its working now!...

but i do have a question. right now i'm having the correct output with only 1 channel. but the thing is when i connect 2/3  channels, they all output the same thing as the 1st channel..

i do think the problem lies with the multiplexer. actually my question is, does the variable channel means the channel number?so its ranges 1 to 3 right?

i tested the if(channel == 1) code... its not working.... i cannot configure the multiplexer according to the channel number.

mem

#29
Mar 08, 2008, 09:25 am Last Edit: Mar 08, 2008, 09:28 am by mem Reason: 1
Good to hear its starting to work  :)

The Channel variable is a channel number, but because there is nothing in this verison to indicate which channel is channel 1 then it will indicate channel 1 as the first channel it happens to detect. But the three channels should have different values if you are sending different values on your transmitter.

edit: I just reread what I wrote above and its not correct. The multiplexer will select the correct channel if its gated correctly.

Could you post the sketch that includes the multiplexer code.

Go Up