Re: Attemp to integrate sonar (ultrasonic sensor)
Posted: Wed Feb 19, 2014 2:11 pm
what pins can we use for the HC-SR04 on nanoboards with atmega32u4?
All things related to MultiWii
http://www.multiwii.com/forum/
Wipo wrote:Like many others I don't use the gps option so I need to connect it directly to the FC board.
Vallekano wrote:Steeze McQueen wrote:Vallekano wrote:I use D8 and D12
Thank you for getting the HC-SR04 working! I think I'm being a bit thick today, plus this is my first arduino project so I'm still learning. My question is, what pins are you using off the HC-SR04? I see a Vcc, Trig, Echo, and GND. How do those correspond to pins D8 and D12 on the arduino? Also, and you may not know this, but I'm using a seeeduino mega board, not a pro mini. In that case, how do the pins D8 and D12 on a pro mini correspond to pins on the arduino mega?
Sorry for the delay.
VCC -> 5V
GND -> GND
ECHO -> D8
TRIGGER -> D12
if i have time this afternoon or tomorrow i'll record a video of MultiWiiconf
pihlerm wrote:Just wanted to say that I used code from kataventos's post.
I have a cheap HC-SR04.
Had to re-arrange the code a bit for MWI 2.3 but it works fine.
There is a big improvement in triggering the sonar RARELY. I settled for 10Hz.
I get no spikes and other artifacts as I had with a tighter (original) loop .. this is probably due to ringing, interference, lack of power etc.
I will test it outside tomorrow, but today inside I get a very smooth and noiseless signal up to 2.55m.
Tilt does not introduce errors until critical angle, when it jumps to either the distance from the wall or out of range.
Will progress towards autolanding. I'm planning to mix it with image processing (on external platform) later to land on target.
Just wanted to report it.
robertha wrote:Agree - I did the same finding, believe the SRF04 power surge during the ultrasoninc burst is quite high. I have also great results attaching capacitor on SRF04 power lines (+5V, GND) directly (parallel with supply wires). I did use this capacitor 3300uF 16V.
http://www.hobbyking.com/hobbyking/stor ... =capacitor
robertha wrote: Could you post your changes for MWII 2.3 ? I am also trying to implement KV mod (have already working sonar on I2C GPS - but just as indication), but as I am not familliar with MWI deeply I have problem where to put changes as 2.3 differs a lot from KV 2.2.
Code: Select all
/* Sonar */ // for visualization purpose currently - no control code behind
//#define SRF02 // use the Devantech SRF i2c sensors
//#define SRF08
//#define SRF10
//#define SRF23
--- ADD
/* Generic sonar: hc-sr04, srf04, dyp-me007, all generic sonar with echo/pulse pin
default pulse is PH6/12, echo is PB4/11
*/
#define SONAR_GENERIC_ECHOPULSE
#define SONAR_GENERIC_SCALE 58 //scale for ranging conversion (hcsr04 is 58)
#define SONAR_GENERIC_MAX_RANGE 500 //cm (could be more)
/************************* Sonar alt hold / precision / ground collision keeper *******/
#define SONAR_MAX_HOLD 400 //cm, kind of error delimiter, for now to avoid rocket climbing, only usefull if no baro
//if using baro + sonar
#define SONAR_BARO_FUSION_LC 200 //cm, baro/sonar readings fusion, low cut, below = full sonar
#define SONAR_BARO_FUSION_HC 400 //cm, baro/sonar readings fusion, high cut, above = full baro
#define SONAR_BARO_FUSION_RATIO 0.0 //0.0-1.0, baro/sonar readings fusion, amount of each sensor value, 0 = proportionnel between LC and HC
#define SONAR_BARO_LPF_LC 0.9f
#define SONAR_BARO_LPF_HC 0.9f
#define SONAR_UPDATE_INTERVAL 100000
Code: Select all
/**************************************************************************************/
/*************** Sensor Type definitions ********************/
/**************************************************************************************/
#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
Code: Select all
#if defined(SRF02) || defined(SRF08) || defined(SRF10) || defined(SRC235) || defined(TINY_GPS_SONAR) || defined(SONAR_GENERIC_ECHOPULSE)
#define SONAR 1
#else
#define SONAR 0
#endif
Code: Select all
sonarAlt = srf08_ctx.range[0]; //tmp
}
#elif defined(SONAR_GENERIC_ECHOPULSE)
volatile unsigned long SONAR_GEP_startTime = 0;
volatile unsigned long SONAR_GEP_echoTime = 0;
volatile static int32_t tempSonarAlt=0;
static int32_t sonarPreviousT;
void Sonar_init()
{
SONAR_GEP_EchoPin_PCICR;
SONAR_GEP_EchoPin_PCMSK;
SONAR_GEP_EchoPin_PINMODE_IN;
SONAR_GEP_TriggerPin_PINMODE_OUT;
sonarPreviousT=micros();
Sonar_update();
}
ISR(SONAR_GEP_EchoPin_PCINT_vect) {
if (SONAR_GEP_EchoPin_PIN & (1<<SONAR_GEP_EchoPin_PCINT)) {
SONAR_GEP_startTime = micros();
}
else {
SONAR_GEP_echoTime = micros() - SONAR_GEP_startTime;
if (SONAR_GEP_echoTime <= SONAR_GENERIC_MAX_RANGE*SONAR_GENERIC_SCALE)
tempSonarAlt = SONAR_GEP_echoTime / SONAR_GENERIC_SCALE;
else
tempSonarAlt = -1;
}
}
void Sonar_update()
{
sonarAlt=1+tempSonarAlt;
int32_t currentT = micros();
int32_t dTime;
dTime = currentT - sonarPreviousT;
if (dTime < SONAR_UPDATE_INTERVAL) return;
sonarPreviousT = currentT;
SONAR_GEP_TriggerPin_PIN_LOW;
delayMicroseconds(2);
SONAR_GEP_TriggerPin_PIN_HIGH;
delayMicroseconds(10);
SONAR_GEP_TriggerPin_PIN_LOW;
}
#else