I'm just getting into multiwii/multicopters, it's been a bit of a steep learning curve
I'm using a hobbyking multiwii board on my tricopter, which has an atmega328p. On this board pin 11 is already used for one of the esc's so I was wondering what would have
to be done
to this code
to use a different pin as the echo?
Code: Select all
#if defined(SONAR_GENERIC_ECHOPULSE)
#define SONAR_GEP_TriggerPin 12
#define SONAR_GEP_TriggerPin_PINMODE_OUT pinMode(SONAR_GEP_TriggerPin,OUTPUT);
#define SONAR_GEP_TriggerPin_PIN_HIGH PORTB |= 1<<6;
#define SONAR_GEP_TriggerPin_PIN_LOW PORTB &= ~(1<<6);
#define SONAR_GEP_EchoPin 11
#define SONAR_GEP_EchoPin_PINMODE_IN pinMode(SONAR_GEP_EchoPin,INPUT);
#define SONAR_GEP_EchoPin_PCINT PCINT5
#define SONAR_GEP_EchoPin_PCICR PCICR |= (1<<PCIE0); // PCINT 0-7 belong to PCIE0
#define SONAR_GEP_EchoPin_PCMSK PCMSK0 = (1<<SONAR_GEP_EchoPin_PCINT); // Mask Pin PCINT5 - all other PIns PCINT0-7 are not allowed to create interrupts!
#define SONAR_GEP_EchoPin_PCINT_vect PCINT0_vect // PCINT0-7 belog PCINT0_vect
#define SONAR_GEP_EchoPin_PIN PINB // PCINT0-7 belong to PINB
#endif
I have plenty of pins free as I'm using ppm in.
Ok figured it out after a bit of trial and mostly error
this is the code segment I used in def.h:
Code: Select all
#if defined(SONAR_GENERIC_ECHOPULSE)
#define SONAR_GEP_TriggerPin 12
#define SONAR_GEP_TriggerPin_PINMODE_OUT pinMode(SONAR_GEP_TriggerPin,OUTPUT);
#define SONAR_GEP_TriggerPin_PIN_HIGH PORTB |= (1<<4);
#define SONAR_GEP_TriggerPin_PIN_LOW PORTB &= ~(1<<4);
#define SONAR_GEP_EchoPin 8 // using pin 8 instead of 11
#define SONAR_GEP_EchoPin_PINMODE_IN pinMode(SONAR_GEP_EchoPin,INPUT);
#define SONAR_GEP_EchoPin_PCINT PCINT0 //pin 8 is PCINT0 instead of pin 11 which is PCINT5
#define SONAR_GEP_EchoPin_PCICR PCICR |= (1<<PCIE0); // PCINT 0-7 belong to PCIE0
#define SONAR_GEP_EchoPin_PCMSK PCMSK0 = (1<<SONAR_GEP_EchoPin_PCINT); // Mask Pin PCINT0 - all other PIns PCINT0-7 are not allowed to create interrupts!
#define SONAR_GEP_EchoPin_PCINT_vect PCINT0_vect // PCINT0-7 belog PCINT0_vect
#define SONAR_GEP_EchoPin_PIN PINB // PCINT0-7 belong to PINB
#endif
That's in the code from this post
viewtopic.php?f=7&t=1033&start=160#p26227I'm using this board
http://www.hobbyking.com/hobbyking/stor ... _Port.html which has pin8 unused as well as spare breakout pads/holes so I soldered a wire from pin8
to one of those.
I also added in some code
to make the P term 5x more sensitive when using the sonar for a much more responsive and precise altitude hold indoors. Works like a treat now, with my not so good flying skills I can now hover in my kitchen!
One thing that tripped me up for a few hours was that the sonar didn't work well at all when powering the board from usb - readings all over the place. Fine when a flight battery is connected though so that's good.
Thanks
to everyone contributing
to this, it's awesome
to be able
to go in and chop'n'change stuff!