Go Down

Topic: BlueCopter - Arduino Quadcopter (Read 157403 times) previous topic - next topic

Vladgone

Thanks all of you, i met a big problem with my receiver, it blew up ( wrong connection ) :'( , i try to fix or buy another one.

dicarloj


I've ported the receiver code to the arduino UNO which handles pin change interrupts and ISR's a little bit differently if anybody is interested.  This works with the six analog pins, but it can work with other pins if you modify the ISR and the pin assignments.  There are also some comments explaining how it all works.

More importantly, it has a method to detect if the remote is out of range.  This works with my 6 channel hobbyking HK-T6A V2 remote very nicely.  If you put "checkForDeath" in your loop() method, the code inside will run when you go out of range or run out of controller batteries.



Code: [Select]


//format for all the pin arrays
const byte rxPins[6] = {RX_PIN_PITCH, RX_PIN_ROLL, RX_PIN_YAW, RX_PIN_AUX1, RX_PIN_AUX2, RX_PIN_THROTTLE};
//stores if each of the pins was high or low last time
volatile byte rxPrevState[6] = {0, 0, 0, 0, 0, 0};
//stores the system time when the pin last went high
volatile long rxPrevTime[6] = {0, 0, 0, 0, 0, 0};

//method to configure a pin for PCICR. 
void setupPin(byte pin){
    *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin));  // enable pin
    PCIFR  |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
    PCICR  |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}

ISR (PCINT1_vect){
  //an analog pin changed state
  //iterate through all the possible pins it could have been
  for(int i = 0; i < 6; i++){
    //store the state of the pin we're checking
    byte rxCurrentState = digitalRead(rxPins[i]);
   
   
    //if it currently is high, but was low last time...
    if(rxCurrentState == 1 && rxPrevState[i] == 0){
      //the pin just went high, so we store the time at which it went high
      rxPrevTime[i] = micros();
    }
   
    //if it currently is low, but was high last time
    if(rxCurrentState == 0 && rxPrevState[i] == 1){
      //the pin just went low, so we subtract
      //the current time from the time it went high
      //to find the pulse length
     
      //store the pulse length in the rx values array
      rxVal[i] = micros() - rxPrevTime[i];
    }
   
    //store the state (high/low) for next itme
    rxPrevState[i] = rxCurrentState;
  }
}

void initReceiver(){
  //setup interrupts on the analog pins
  for(int i = 0; i < 6; i++){
    pinMode(rxPins[i], INPUT);
    digitalWrite(rxPins[i], HIGH);
    setupPin(rxPins[i]);
  }
 
 
 
}

//call this method from loop().
//you can handle losing connection however you want.
void checkForDeath(){
  if((micros() - rxPrevTime[5]) > 100000){
    //code in here runs if we lose connection
  }
  }
 

}



Isaac96

You know what, I've done that same exact thing, but with the PinChangeInterrupt library. Here's my receiver code:
Code: [Select]

PROGMEM const byte rxPins[6]={
  RX_PIN_YAW,RX_PIN_ROLL,RX_PIN_PITCH,RX_PIN_AUX1,RX_PIN_AUX2,RX_PIN_THROTTLE};
volatile byte rxState[4]={
  0,0,0,0};
volatile int rxPrev[6]={
  0,0,0,0,0,0};

void rxInit(){
  for(byte i=0;i<6;i++){
    pinMode(pgm_read_byte(&rxPins[i]),INPUT);
    digitalWrite(pgm_read_byte(&rxPins[i]),HIGH);
  }
  attachPinChangeInterrupt(RX_PIN_YAW,rxGoesHigh1,RISING);
  attachPinChangeInterrupt(RX_PIN_ROLL,rxGoesHigh2,RISING);
  attachPinChangeInterrupt(RX_PIN_PITCH,rxGoesHigh3,RISING);
  //attachPinChangeInterrupt(RX_PIN_AUX1,rxGoesHigh4,RISING);
  attachPinChangeInterrupt(RX_PIN_AUX2,rxGoesHigh5,RISING);
  attachPinChangeInterrupt(RX_PIN_THROTTLE,rxGoesHigh6,RISING);
}
void rxGoesHigh1(){
  attachPinChangeInterrupt(RX_PIN_YAW,rxGoesLow1,FALLING);
  rxPrev[0]=micros();
}
void rxGoesLow1(){
  attachPinChangeInterrupt(RX_PIN_YAW,rxGoesHigh1,RISING);
  rxVal[0]=micros()-rxPrev[0];
}
void rxGoesHigh2(){
  attachPinChangeInterrupt(RX_PIN_ROLL,rxGoesLow2,FALLING);
  rxPrev[1]=micros();
}
void rxGoesLow2(){
  attachPinChangeInterrupt(RX_PIN_ROLL,rxGoesHigh2,RISING);
  rxVal[1]=micros()-rxPrev[1];
}
void rxGoesHigh3(){
  attachPinChangeInterrupt(RX_PIN_PITCH,rxGoesLow3,FALLING);
  rxPrev[2]=micros();
}
void rxGoesLow3(){
  attachPinChangeInterrupt(RX_PIN_PITCH,rxGoesHigh3,RISING);
  rxVal[2]=micros()-rxPrev[2];
}
/*void rxGoesHigh4(){
  attachPinChangeInterrupt(RX_PIN_AUX1,rxGoesLow4,FALLING);
  rxPrev[3]=micros();
}
void rxGoesLow4(){
  attachPinChangeInterrupt(RX_PIN_AUX1,rxGoesHigh4,RISING);
  rxVal[3]=micros()-rxPrev[3];
}*/
void rxGoesHigh5(){
  attachPinChangeInterrupt(RX_PIN_AUX2,rxGoesLow5,FALLING);
  rxPrev[3]=micros();
}
void rxGoesLow5(){
  attachPinChangeInterrupt(RX_PIN_AUX2,rxGoesHigh5,RISING);
  rxVal[3]=micros()-rxPrev[3]; 
}

void rxGoesHigh6(){
  attachPinChangeInterrupt(RX_PIN_THROTTLE,rxGoesLow6,FALLING);
  rxPrev[5]=micros();
}
void rxGoesLow6(){
  attachPinChangeInterrupt(RX_PIN_THROTTLE,rxGoesHigh6,RISING);
  rxVal[5]=micros()-rxPrev[5]; 
}



 8)  8)  :P
I love Arduino
When is the ZERO coming out?
Why RPi?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy