Attemp to integrate sonar (ultrasonic sensor)

mahowik
Posts: 331
Joined: Sun Apr 10, 2011 6:26 pm

Attemp to integrate sonar (ultrasonic sensor)

Post by mahowik »

Hi all,

http://www.goodluckbuy.com/ultrasonic-w ... ensor.html

I'm trying to integrate the DYP-ME007 sonar to multiwii for more precised alt hold (with altitude <5m) or for auto landing etc...

With simple example the sonar work perfect

Code: Select all

// variables to take x number of readings and then average them
// to remove the jitter/noise from the DYP-ME007 sonar readings
const int numOfReadings = 10; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value
 
// setup pins and variables for DYP-ME007 sonar device
int echoPin = 2; // DYP-ME007 echo pin (digital 2)
int initPin = 3; // DYP-ME007 trigger pin (digital 3)
 
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
 
//setup
void setup() {
  pinMode(initPin, OUTPUT); // set init pin 3 as output
  pinMode(echoPin, INPUT); // set echo pin 2 as input
  // create array loop to iterate over every item in the array
  for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
    readings[thisReading] = 0;
  }
  // initialize the serial port, lets you view the
  // distances being pinged if connected to computer
  Serial.begin(9600);
}
 
// execute
void loop() {
  digitalWrite(initPin, HIGH); // send 10 microsecond pulse
  delayMicroseconds(10); // wait 10 microseconds before turning off
  digitalWrite(initPin, LOW); // stop sending the pulse
  pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
  distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.

  total= total - readings[arrayIndex]; // subtract the last distance
  readings[arrayIndex] = distance; // add distance reading to array
  total= total + readings[arrayIndex]; // add the reading to the total
  arrayIndex = arrayIndex + 1; // go to the next item in the array
  // At the end of the array (10 items) then start again
  if (arrayIndex >= numOfReadings) {
    arrayIndex = 0;
  }
  averageDistance = total / numOfReadings; // calculate the average distance
  Serial.println(averageDistance, DEC); // print out the average distance to the debugger
  delay(100); // wait 100 milli seconds before looping again
}


But pulseIn() take a lot of time for one iteration because it wait for the response...

So we need the driver based on interrupts. I tried to get it from the megapirateng http://code.google.com/p/megapirateng/downloads/list
but haven't enough of knowledge in low level Arduino programming to run this.
Here is the sketch for mega1280. Could someone look at this and give some comments how to fix this driver?

Code: Select all


// setup pins and variables for DYP-ME007 sonar device
int echoPin = 10; // DYP-ME007 echo pin (digital 2)
int initPin = 9; // DYP-ME007 trigger pin (digital 3)

//setup
void setup() {
  pinMode(initPin, OUTPUT); // set init pin 3 as output
  pinMode(echoPin, INPUT); // set echo pin 2 as input

  // initialize the serial port, lets you view the
  // distances being pinged if connected to computer
  Serial.begin(115200);

  // Sonar INIT
  //=======================
  //D48 (PORTL.1) = sonar input
  //D47 (PORTL.2) = sonar Tx (trigger)
  //The smaller altitude then lower the cycle time

  // 0.034 cm/micros
  //PORTL&=B11111001;
  //DDRL&=B11111101;
  //DDRL|=B00000100;

  PORTH&=B10111111; // H6 -d9  - sonar TX
  DDRH |=B01000000;

  PORTB&=B11101111; // B4 -d10 - sonar Echo
  DDRB &=B11101111;



  //PORTG|=B00000011; // buttons pullup

  //div64 = 0.5 us/bit
  //resolution =0.136cm
  //full range =11m 33ms
  // Using timer5, warning! Timer5 also share with RC PPM decoder
  TCCR5A =0; //standard mode with overflow at A and OC B and C interrupts
  TCCR5B = (1<<CS11); //Prescaler set to 8, resolution of 0.5us
  TIMSK5=B00000111; // ints: overflow, capture, compareA
  OCR5A=65510; // approx 10m limit, 33ms period
  OCR5B=3000;

  PCMSK0 = B00010000; // sonar port B4 - d10 echo
  PCICR |= 1; // PCINT activated for PORTB

}

// Sonar read interrupts
volatile char sonar_meas=0;
volatile unsigned int sonar_data=0, sonar_data_start=0, pre_sonar_data=0; // Variables for calculating length of Echo impulse
ISR(TIMER5_COMPA_vect) // This event occurs when counter = 65510
{
  if (sonar_meas == 0) // sonar_meas=1 if we not found Echo pulse, so skip this measurement
    sonar_data = 0;
  PORTH|=B01000000; // set Sonar TX pin to 1 and after ~12us set it to 0 (below) to start new measurement
}

ISR(TIMER5_OVF_vect) // Counter overflowed, 12us elapsed
{
  PORTH&=B10111111; // set TX pin to 0, and wait for 1 on Echo pin (below)
  sonar_meas=0; // Clean "Measurement finished" flag
}

ISR(PCINT0_vect)
{
  if (PINB & B00010000) {
    sonar_data_start = TCNT5; // We got 1 on Echo pin, remeber current counter value
  }
  else {
    sonar_data=TCNT5-sonar_data_start; // We got 0 on Echo pin, calculate impulse length in counter ticks
    sonar_meas=1; // Set "Measurement finished" flag
  }
}


void loop() {

  if ( (sonar_data < 354) && (pre_sonar_data > 0) ) {    //wrong data from sonar (3cm * 118 = 354), use previous value
    sonar_data=pre_sonar_data;
  }
  else {
    pre_sonar_data=sonar_data;
  }
  unsigned int distance = (sonar_data / 118); // Magic conversion sonar_data to cm

  Serial.println(distance, DEC); // print out the average distance to the debugger

  delay(100); // wait 100 milli seconds before looping again
}



p.s. With MegaPirateNG_2.0.49_Beta4 it works but not with this separated sketch...

thx-
Alex

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by copterrichie »

Hey Alex,

I am happy you have picked this project up, hope you are able to get it working. I did some experimenting with the Ardunio Ultrasonic Library, many unexplained crashes, so I wrote my own interface. I had it working but still there were some weird behavior that I was not sure if it was from the ultrasonic or code I modified for my Bi-Copter. I set the project on the back burner. I am not recommending GLB's I2C ultrasonic but it seems to be a better way to go if not using a dedicated Arduino for the job.


http://www.goodluckbuy.com/ks101b-high- ... odule.html

PatrikE
Posts: 1962
Joined: Tue Apr 12, 2011 6:35 pm
Location: Sweden
Contact:

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by PatrikE »

Iv'e done some experiments to.
And you must use an interupt pin otherwise all code is stopped and waits for pulseIn() and it will defenatly crach when you gain altitude. :mrgreen:


PROmini only have 2 interups D2 & D3.
D2 is used by Radio.
D3 is a motor pin.
D3 can be remapped in the motororder in def.

Let the main loop trig a ping and reset timers.
the main loop continues undisturbed.

Interupt function will pick up the echo and calculate the distance and set a OK flag.
Mainloop needs to check if echo has arrived in reasanoble time otherwise it should resend the ping and reset timers

/Patrik

User avatar
EOSBandi
Posts: 802
Joined: Sun Jun 19, 2011 11:32 am
Location: Budapest, Hungary
Contact:

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by EOSBandi »

PatrikE wrote:Iv'e done some experiments to.
And you must use an interupt pin otherwise all code is stopped and waits for pulseIn() and it will defenatly crach when you gain altitude. :mrgreen:


PROmini only have 2 interups D2 & D3.
D2 is used by Radio.
D3 is a motor pin.
D3 can be remapped in the motororder in def.

Let the main loop trig a ping and reset timers.
the main loop continues undisturbed.

Interupt function will pick up the echo and calculate the distance and set a OK flag.
Mainloop needs to check if echo has arrived in reasanoble time otherwise it should resend the ping and reset timers

/Patrik


I think it's easier to use additional arduino for the sonar, and read the values via i2c.
I'm currently experimenting with this. Connected a stripped down arduino mini to Maxbotic and DIY sensors. I will get some results between xmas a new years eve...

User avatar
UndCon
Posts: 293
Joined: Mon Feb 21, 2011 2:10 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by UndCon »

will any i2c sensor do?

http://www.robot-electronics.co.uk/acat ... ngers.html

I like the SRF02...if they are OK I will order a few...

User avatar
EOSBandi
Posts: 802
Joined: Sun Jun 19, 2011 11:32 am
Location: Budapest, Hungary
Contact:

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by EOSBandi »

UndCon wrote:will any i2c sensor do?

http://www.robot-electronics.co.uk/acat ... ngers.html

I like the SRF02...if they are OK I will order a few...

I ordered this, http://www.goodluckbuy.com/ks101b-high- ... odule.html. it's on it's way, will write about it once managed to try it...

mahowik
Posts: 331
Joined: Sun Apr 10, 2011 6:26 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by mahowik »

Good news! Sonar driver works now! :)
Cycle time is ok also.

Thanks to Sir Alex from megapirateng project http://forum.rcdesign.ru/f123/thread232 ... ost3024407

Code: Select all

#if defined(DYP_ME007)

// setup pins and variables for DYP-ME007 sonar device

void Sonar_init() {
 
  // Sonar INIT
  //=======================
  //The smaller altitude then lower the cycle time
 
  //cli();
  PORTH&=B10111111; // H6 -d9  - sonar TX
  DDRH |=B01000000;

  PORTB&=B11101111; // B4 -d10 - sonar Echo
  DDRB &=B11101111;

  TCCR5A =0; //standard mode with overflow at A and OC B and C interrupts
  TCCR5B = (1<<CS11); //Prescaler set to 8, resolution of 0.5us
  TIMSK5 = B00000011; // ints: overflow, capture, compareA
  //TIMSK5=B00000111; // ints: overflow, capture, compareA
  OCR5A=65510; // approx 10m limit, 33ms period

  PCMSK0 = B00010000; // sonar port B4 - d10 echo
  PCICR |= 1; // PCINT activated for PORTB
  //sei();
}

// Sonar read interrupts
volatile char sonar_meas=0;
volatile unsigned int sonar_data=0, sonar_data_start=0, pre_sonar_data=0; // Variables for calculating length of Echo impulse
ISR(TIMER5_COMPA_vect) // This event occurs when counter = 65510
{
      if (sonar_meas == 0) // sonar_meas=1 if we not found Echo pulse, so skip this measurement
            sonar_data = 0;
      PORTH|=B01000000; // set Sonar TX pin to 1 and after ~12us set it to 0 (below) to start new measurement
}

ISR(TIMER5_OVF_vect) // Counter overflowed, 12us elapsed
{
   PORTH&=B10111111; // set TX pin to 0, and wait for 1 on Echo pin (below)
   sonar_meas=0; // Clean "Measurement finished" flag
}

ISR(PCINT0_vect)
{
   if (PINB & B00010000) {
      sonar_data_start = TCNT5; // We got 1 on Echo pin, remeber current counter value
   } else {
      sonar_data=TCNT5-sonar_data_start; // We got 0 on Echo pin, calculate impulse length in counter ticks
      sonar_meas=1; // Set "Measurement finished" flag
   }
}

void Sonar_getDistance() {
  if ( (sonar_data < 354) && (pre_sonar_data > 0) ) {    //wrong data from sonar (3cm * 118 = 354), use previous value
    sonar_data=pre_sonar_data;
  }
  else {
    pre_sonar_data=sonar_data;
  }
  sonarDistance = (sonar_data / 118); // Magic conversion sonar_data to cm
}

#endif



I also tried to make some changes in getEstimatedAltitude() to switch between baro and sonar if altitude in range of sonar (3..450cm) and use it with the same alt-vel PID regulator...

Code: Select all

void getEstimatedAltitude(){
  static uint8_t inited = 0;
  static int16_t AltErrorI = 0;
  static float AccScale  = 0.0f;
  static uint32_t deadLine = INIT_DELAY;
  int16_t AltError;
  int16_t InstAcc;
  int16_t Delta;
  static uint8_t isSonarActive = 1;
  static uint32_t sonarTempDistance;
 
  if (currentTime < deadLine) return;
  deadLine = currentTime + UPDATE_INTERVAL;
  // Soft start

  if (!inited) {
    inited = 1;
    EstAlt = BaroAlt;
    EstVelocity = 0;
    AltErrorI = 0;
    AccScale = 100 * 9.80665f / acc_1G;
  }
 
  if(SONAR && (sonarDistance >= 3) && (sonarDistance <= 450)) {
   
    if(!isSonarActive) {
        isSonarActive = 1;
        baroMode = 0;
    }
   
    sonarTempDistance = (sonarTempDistance - (sonarTempDistance >> 2)) + sonarDistance;
    sonarDistance = sonarTempDistance >> 2;
   
    EstVelocity = (sonarDistance - EstAlt) * 10;
    EstAlt = sonarDistance;
   
  } else {
 
    if(isSonarActive) {
        isSonarActive = 0;
        baroMode = 0;
    }
 
    // Estimation Error
    AltError = BaroAlt - EstAlt;
    AltErrorI += AltError;
    AltErrorI=constrain(AltErrorI,-25000,+25000);
    // Gravity vector correction and projection to the local Z
    //InstAcc = (accADC[YAW] * (1 - acc_1G * InvSqrt(isq(accADC[ROLL]) + isq(accADC[PITCH]) + isq(accADC[YAW])))) * AccScale + (Ki) * AltErrorI;
    #if defined(TRUSTED_ACCZ)
      //InstAcc = (accADC[YAW] * (1 - acc_1G * InvSqrt(isq(accADC[ROLL]) + isq(accADC[PITCH]) + isq(accADC[YAW])))) * AccScale +  AltErrorI / 1000;
      InstAcc = (accSmooth[YAW] * (1 - acc_1G * InvSqrt(isq(accSmooth[ROLL]) + isq(accSmooth[PITCH]) + isq(accSmooth[YAW])))) * AccScale +  AltErrorI / 1000;   
    #else
      InstAcc = AltErrorI / 1000;
    #endif
   
    // Integrators
    Delta = InstAcc * dt + (Kp1 * dt) * AltError;
    EstAlt += (EstVelocity/5 + Delta) * (dt / 2) + (Kp2 * dt) * AltError;
    EstVelocity += Delta*10;
  }
}



But have not got stable results yet... land was covered with snow and I'm not sure is it possible to get ultrasonic echo from snow... probable it was the reason of fails with today's test ...

Investigation in progress... ;)

thx-
Alex
Last edited by mahowik on Mon Dec 26, 2011 5:01 am, edited 1 time in total.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by copterrichie »

This is great news and one step closer to auto landing in my opinion.

hwurzburg
Posts: 75
Joined: Sun Jan 01, 2012 3:28 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by hwurzburg »

mahowik wrote:Good news! Sonar driver works now! :)
Cycle time is ok also.
......
But have not got stable results yet... land was covered with snow and I'm not sure is it possible to get ultrasonic echo from snow... probable it was the reason of fails with today's test ...

Investigation in progress... ;)

thx-
Alex


Alex, any progress update :?: ....I am anxiously awaiting a working sonar altitude hold! the HC-SR04 sensor is the same thing and available for $5 on ebay...

also what pins need to be rearranged for its connection?

mahowik
Posts: 331
Joined: Sun Apr 10, 2011 6:26 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by mahowik »

still in progress and on pause now because have not free time...
p.s. alexmos also joined to sonar integration... he plays with HC-SR04

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

mahowik wrote:still in progress and on pause now because have not free time...
p.s. alexmos also joined to sonar integration... he plays with HC-SR04


I have a HC-SR04. Could you pass me the code to test?

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

mahowik wrote:Good news! Sonar driver works now! :)
Cycle time is ok also.

Thanks to Sir Alex from megapirateng project http://forum.rcdesign.ru/f123/thread232 ... ost3024407

Code: Select all

#if defined(DYP_ME007)

// setup pins and variables for DYP-ME007 sonar device

void Sonar_init() {
 
  // Sonar INIT
  //=======================
  //The smaller altitude then lower the cycle time
 
  //cli();
  PORTH&=B10111111; // H6 -d9  - sonar TX
  DDRH |=B01000000;

  PORTB&=B11101111; // B4 -d10 - sonar Echo
  DDRB &=B11101111;

  TCCR5A =0; //standard mode with overflow at A and OC B and C interrupts
  TCCR5B = (1<<CS11); //Prescaler set to 8, resolution of 0.5us
  TIMSK5 = B00000011; // ints: overflow, capture, compareA
  //TIMSK5=B00000111; // ints: overflow, capture, compareA
  OCR5A=65510; // approx 10m limit, 33ms period

  PCMSK0 = B00010000; // sonar port B4 - d10 echo
  PCICR |= 1; // PCINT activated for PORTB
  //sei();
}

// Sonar read interrupts
volatile char sonar_meas=0;
volatile unsigned int sonar_data=0, sonar_data_start=0, pre_sonar_data=0; // Variables for calculating length of Echo impulse
ISR(TIMER5_COMPA_vect) // This event occurs when counter = 65510
{
      if (sonar_meas == 0) // sonar_meas=1 if we not found Echo pulse, so skip this measurement
            sonar_data = 0;
      PORTH|=B01000000; // set Sonar TX pin to 1 and after ~12us set it to 0 (below) to start new measurement
}

ISR(TIMER5_OVF_vect) // Counter overflowed, 12us elapsed
{
   PORTH&=B10111111; // set TX pin to 0, and wait for 1 on Echo pin (below)
   sonar_meas=0; // Clean "Measurement finished" flag
}

ISR(PCINT0_vect)
{
   if (PINB & B00010000) {
      sonar_data_start = TCNT5; // We got 1 on Echo pin, remeber current counter value
   } else {
      sonar_data=TCNT5-sonar_data_start; // We got 0 on Echo pin, calculate impulse length in counter ticks
      sonar_meas=1; // Set "Measurement finished" flag
   }
}

void Sonar_getDistance() {
  if ( (sonar_data < 354) && (pre_sonar_data > 0) ) {    //wrong data from sonar (3cm * 118 = 354), use previous value
    sonar_data=pre_sonar_data;
  }
  else {
    pre_sonar_data=sonar_data;
  }
  sonarDistance = (sonar_data / 118); // Magic conversion sonar_data to cm
}

#endif



I also tried to make some changes in getEstimatedAltitude() to switch between baro and sonar if altitude in range of sonar (3..450cm) and use it with the same alt-vel PID regulator...

Code: Select all

void getEstimatedAltitude(){
  static uint8_t inited = 0;
  static int16_t AltErrorI = 0;
  static float AccScale  = 0.0f;
  static uint32_t deadLine = INIT_DELAY;
  int16_t AltError;
  int16_t InstAcc;
  int16_t Delta;
  static uint8_t isSonarActive = 1;
  static uint32_t sonarTempDistance;
 
  if (currentTime < deadLine) return;
  deadLine = currentTime + UPDATE_INTERVAL;
  // Soft start

  if (!inited) {
    inited = 1;
    EstAlt = BaroAlt;
    EstVelocity = 0;
    AltErrorI = 0;
    AccScale = 100 * 9.80665f / acc_1G;
  }
 
  if(SONAR && (sonarDistance >= 3) && (sonarDistance <= 450)) {
   
    if(!isSonarActive) {
        isSonarActive = 1;
        baroMode = 0;
    }
   
    sonarTempDistance = (sonarTempDistance - (sonarTempDistance >> 2)) + sonarDistance;
    sonarDistance = sonarTempDistance >> 2;
   
    EstVelocity = (sonarDistance - EstAlt) * 10;
    EstAlt = sonarDistance;
   
  } else {
 
    if(isSonarActive) {
        isSonarActive = 0;
        baroMode = 0;
    }
 
    // Estimation Error
    AltError = BaroAlt - EstAlt;
    AltErrorI += AltError;
    AltErrorI=constrain(AltErrorI,-25000,+25000);
    // Gravity vector correction and projection to the local Z
    //InstAcc = (accADC[YAW] * (1 - acc_1G * InvSqrt(isq(accADC[ROLL]) + isq(accADC[PITCH]) + isq(accADC[YAW])))) * AccScale + (Ki) * AltErrorI;
    #if defined(TRUSTED_ACCZ)
      //InstAcc = (accADC[YAW] * (1 - acc_1G * InvSqrt(isq(accADC[ROLL]) + isq(accADC[PITCH]) + isq(accADC[YAW])))) * AccScale +  AltErrorI / 1000;
      InstAcc = (accSmooth[YAW] * (1 - acc_1G * InvSqrt(isq(accSmooth[ROLL]) + isq(accSmooth[PITCH]) + isq(accSmooth[YAW])))) * AccScale +  AltErrorI / 1000;   
    #else
      InstAcc = AltErrorI / 1000;
    #endif
   
    // Integrators
    Delta = InstAcc * dt + (Kp1 * dt) * AltError;
    EstAlt += (EstVelocity/5 + Delta) * (dt / 2) + (Kp2 * dt) * AltError;
    EstVelocity += Delta*10;
  }
}



But have not got stable results yet... land was covered with snow and I'm not sure is it possible to get ultrasonic echo from snow... probable it was the reason of fails with today's test ...

Investigation in progress... ;)

thx-
Alex


Hey ,
I have also a DYP_ME007 Sonar Sensor :)

Can you please tell me , were i must paste the code into multiwii code ?
So I will test you code and try a litle bit with PID values :)

Thanks so mutch !

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

me too !

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

LuFa wrote:Hey ,
I have also a DYP_ME007 Sonar Sensor :)

Can you please tell me , were i must paste the code into multiwii code ?
So I will test you code and try a litle bit with PID values :)

Thanks so mutch !


He is using the code of megapirateng project. You can't paste that code into multiwii code directly.

I think they don't want give us the code :D

LenzGr
Posts: 166
Joined: Wed Nov 23, 2011 10:50 am
Location: Hamburg, Germany
Contact:

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LenzGr »

Vallekano wrote:He is using the code of megapirateng project. You can't paste that code into multiwii code directly. I think they don't want give us the code :D

Why do you think so? Both code bases are licensed under the GNU GPLv3 - they explicitly chose a free software license in order to facilitate the sharing of code and ideas. So if you find someone to do the porting work, it's perfectly fine to use their altitude hold code in the MultiWii code base as well.

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

LenzGr wrote:
Vallekano wrote:He is using the code of megapirateng project. You can't paste that code into multiwii code directly. I think they don't want give us the code :D

Why do you think so? Both code bases are licensed under the GNU GPLv3 - they explicitly chose a free software license in order to facilitate the sharing of code and ideas. So if you find someone to do the porting work, it's perfectly fine to use their altitude hold code in the MultiWii code base as well.


it was a joke, because they don't reply

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

no answer :(

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

Standalone I2C "shield" for HC-SR04 based on attiny2313 (could work with any avr, i choose this because i had this one dying alone somewhere)

Cost: ~5-6€ (~4 € for HC-SR04 ultrasonic rangefinder, ~2 € for some avr, attiny45,85,2313,atmega8, take the cheapest on ebay... but the 2Ko version is very small, prefere one with more than 4ko)

First thing, the code is based on many part of code found a little bit everywhere, so it's more a "proof of concept" than real reliable code. (here, the i2c respond handler only outputs 8bit integer, so 0-255 value, and other some stuff have to be checked). And the second point is the "arduino wrapper". I was too lazy to write real atmel C, so i've used an attiny arduino implementation.

The avr on the shield perform continuous reading from the sr04, does some math (filter, spik, median, average, buffering, blinking led for "eye checking" etc..) and have event handler to respond to I²c/i2c/twi/iic request (slave mode).

So no need to worry about loop time in MWC, interrupting some stuf, etc.... just "hack" barometer part by switching i2c address and some metrics convertion, or do a real ultrasonic "dof" handler.

And it's cheap (1-2€ if you have already the sr04)

2012-02-06 12.58.18.jpg

2012-02-06 12.58.38.jpg

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by copterrichie »

penpen77, you beat me to it, I was going to use a ATTINY85 but I am excite about your accomplishment. Can you please share the code?

Thank you.

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

Sorry, i thought i joined the zip....

dependency:
TinyWire (attiny implementation of USI/TWI)
Arduino ATTiny (arduino core for attiny)

You may find "smartDigitalWrite" in code, it's just digitalWrite but without PWM check (size issue for me in attiny2313)

i2c address is arbitrary, you can choose what you want
switch MODE "define" to switch MODE behaviour

A rewrite in pure atmel C shouldn't be a problem and should save a lot of memory space

TinySonarI2C.zip
(1.52 KiB) Downloaded 968 times

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

penpen77 wrote:Sorry, i thought i joined the zip....

dependency:
TinyWire (attiny implementation of USI/TWI)
Arduino ATTiny (arduino core for attiny)

You may find "smartDigitalWrite" in code, it's just digitalWrite but without PWM check (size issue for me in attiny2313)

i2c address is arbitrary, you can choose what you want
switch MODE "define" to switch MODE behaviour

A rewrite in pure atmel C shouldn't be a problem and should save a lot of memory space

TinySonarI2C.zip


did you have also the code for Multiwii to juse your ATiny Sonar ?

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by copterrichie »

FYI: here is the link for the TinyWire: http://arduino.cc/playground/Code/USIi2c

And the Link for the Arduino ATTINY: http://code.google.com/p/arduino-tiny/

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

ok ,

Is the code for the Multiwii Flight Software also ready ?

i cant wait to test it :D

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

I managed to run the HC-SR04 sonar with interruptions.

I'll try when I have time to integrate it into the code to work with the bario. But my knowledge of code MultiWii are very low. If anyone has more knowledge and is able to integrate ...

Code: Select all

// setup pins and variables
#define HC_SR04_echoPin 3 //  (digital 3)
#define HC_SR04_trigPin 7 //  (digital 7)

volatile unsigned long Sonar_starTime = 0;
volatile unsigned long Sonar_endTime = 0;

void Sonar_changeDetected()
{
  if (digitalRead(HC_SR04_echoPin) == HIGH) {
    Sonar_starTime = micros(); // Apunto el tiempo de inicio
  }
  else {
    Sonar_endTime = micros();
  } 
}


void Sonar_init()
{
  // Sonar init
   pinMode(HC_SR04_trigPin, OUTPUT);
   pinMode(HC_SR04_echoPin, INPUT);        // Set echo pin as input
   
   attachInterrupt(1, Sonar_changeDetected, CHANGE);
}

void Sonar_update()
{
  digitalWrite(HC_SR04_trigPin, LOW);      // Send 2ms LOW pulse to ensure we get a nice clean pulse
  delayMicroseconds(2);
  digitalWrite(HC_SR04_trigPin, HIGH); // send 10 microsecond pulse
  delayMicroseconds(10); // wait 10 microseconds before turning off
  digitalWrite(HC_SR04_trigPin, LOW); // stop sending the pulse
 
  Sonar_starTime = micros(); // Apunto el tiempo de inicio
}


unsigned long Sonar_read()
{
  return (Sonar_endTime - Sonar_starTime) / 58;
}






// Test
void setup()
{
  Serial.begin(9600);
  Sonar_init();
}

void loop()
{
  Serial.print(Sonar_read(), DEC);
  Serial.println(" cm");
 
  Sonar_update();
 
  delay(100);
}

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

Oula....

guys, i wrote this during my lunch break yesterday..... there isn't any real working code, it's just "proof of concept"

by the way, this should/might/may work by hacking Barometer handler.

And if i could understand WHY thoses f*****g i2c_*** function never work when i try using it
It works perfectly with Wire library, but when swapping, stop working.....

EDIT: working impl next post

Code: Select all

#if defined(HCSR04) // put it in BARO define section

#define HCSR04_ADDRESS 0x02 //my i2c address for SR04

void  Baro_init() {
  //rien
  Wire.begin(); 
}

void Baro_update() {
  Wire.requestFrom(HCSR04_ADDRESS,1);
  if(Wire.available())   BaroAlt = (uint8_t)Wire.receive()*100; //centimeter ???
 
  /* DONT WORK..... ffffuuuuuuu
  i2c_rep_start(HCSR04_ADDRESS/*+1*/); (//with or without 7bit, +/-1, register, voodoo, same results...)
  BaroAlt = (uint8_t)i2c_readNak()*100;    //centimeter ??? 
  */
}
#endif


barometer substitute on conf
border of my desk, 70cm above the ground so....
border of my desk, 70cm above the ground so....
Last edited by penpen77 on Tue Feb 07, 2012 7:28 pm, edited 1 time in total.

User avatar
EOSBandi
Posts: 802
Joined: Sun Jun 19, 2011 11:32 am
Location: Budapest, Hungary
Contact:

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by EOSBandi »

wire lib does not handle repeated stop on i2c, multiwii code uses it, so it has it's own i2c implementation.

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

i know that, it's just that i can't achieve mw i2c sequence working...... the Wire part is just for debugging, check if data is here and correct. It's not intended to fly with that kind of code....

Like i said before, it's just "poc", if someone has a correct i2c request/reading sequence, i could write a real handler, independants of Barometer part (or having a switching behaviour).

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

need define PULLUPS.....

so

Code: Select all

// ************************************************************************************************************
// I2C HCSR04 UltraSonic range finder Wrapper (EXPERIMENTAL, USE WITH CAUTION, NOT TESTED, BLABLABLABLABLA)
// ************************************************************************************************************
#if defined(HCSR04)
#define HCSR04_ADDRESS 0x09 //your i2c wrapper address

void  Baro_init() {
}

void Baro_update() {
  TWBR = ((16000000L / I2C_SPEED) - 16) / 2;
  i2c_rep_start((HCSR04_ADDRESS<<1)+1); //read
  BaroAlt = i2c_readNak()*100;  //centimeter ?
}
#endif


in sensor file and add a define HCSR04 in config.h for BARO turned to 1

This override barometer data. Not tested yet....

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

Tests results: seems to work, so far so good.... but i thinks it needs a real dedicated handler. Hacking barometer impl' for ultrasonic rf makes the quad a bit too much '"floaty" with some kind of inertia that i was unable to cancel by only tweaking alt params.

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

sounds greate :)

i have try to run your ATTinySonarI2C on a arduino mini pro , did you can chanche your code for it ??

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

I managed to operate the HC-SR04 Ultrasonic Sonar, without use of any auxiliary device.

I integrate it into the MultiWii 1.9 code.

To make it work we need to use the D8 and D12 ports so we can not use AUX2.

Here is a sample image. The measured distance is seen in "debug3" and is measured in centimeters. Cycle time unaffected.

MultiWiiConf.png


Now we have to do something with this :lol: . I will try an autolanding...

Changes:

In "Serial.pde" replace:

Code: Select all

serialize16(0);                 // debug3

by

Code: Select all

serialize16(SonarAlt);         // debug3


In "MultiWii_1_9.pde" replace:

Code: Select all

static int32_t  BaroAlt;

by

Code: Select all

static int32_t  BaroAlt;
static int32_t  SonarAlt;


and replace

Code: Select all

 
if (BARO) Baro_update();

by

Code: Select all

if (BARO) Baro_update();
if (SONAR) Sonar_update();



In config.h Add

Code: Select all

/* HC-SR04 Ultrasonic Sonar */
#define HCSR04




In def.h add

Code: Select all

#if defined(HCSR04)
  #define SONAR 1
  #define BUZZERPIN_PINMODE          ;
  #define BUZZERPIN_ON               ;
  #define BUZZERPIN_OFF              ;
  #define POWERPIN_PINMODE           ;
  #define POWERPIN_ON                ;
  #define POWERPIN_OFF               ;
  #define HCSR04_EchoPin         8
  #define HCSR04_TriggerPin      12
#else
  #define SONAR 0
#endif


In Sensors.pde add

Code: Select all

// ************************************************************************************************************
// HC-SR04 Ultrasonic Sonar
// ************************************************************************************************************

#if defined(HCSR04)

volatile unsigned long HCSR04_starTime = 0;
volatile unsigned long HCSR04_echoTime = 0;
volatile unsigned int HCSR04_waiting_echo = 0;
unsigned int HCSR04_current_loops = 0;

// The cycle time is between 3000 and 6000 microseconds
// The recommend cycle period for sonar request should be no less than 50ms -> 50000 microseconds
// A reading every 18 loops (50000 / 3000 aprox)
unsigned int HCSR04_loops = 18;

void Sonar_init()
{
  // Pin change interrupt control register - enables interrupt vectors
  PCICR  |= (1<<PCIE0); // D8
 
  // Pin change mask registers decide which pins are enabled as triggers
  PCMSK0 = (1<<PCINT0); // pin 8
 
  pinMode(HCSR04_TriggerPin, OUTPUT);
   
  Sonar_update();
}

ISR(PCINT0_vect) {
    uint8_t pin = PINB;
    if (pin & 1<<PCINT0) {     //indicates if the bit 0 of the arduino port [B0-B7] is at a high state
      HCSR04_starTime = micros();
    }
    else {
      HCSR04_echoTime = micros() - HCSR04_starTime; // Echo time in microseconds
     
      if (HCSR04_echoTime <= 25000) {     // valid distance
        SonarAlt = HCSR04_echoTime / 58;
      }
      else
      {
      // No valid data
        SonarAlt = 9999;
      }
      HCSR04_waiting_echo = 0;
    }
}


void Sonar_update()
{
  HCSR04_current_loops++;
 
  if (HCSR04_waiting_echo == 0 && HCSR04_current_loops >= HCSR04_loops)
  {
    // Send 2ms LOW pulse to ensure we get a nice clean pulse
    digitalWrite(HCSR04_TriggerPin, LOW);     
    delayMicroseconds(2);
   
    // send 10 microsecond pulse
    digitalWrite(HCSR04_TriggerPin, HIGH);
    // wait 10 microseconds before turning off
    delayMicroseconds(10);
    // stop sending the pulse
    digitalWrite(HCSR04_TriggerPin, LOW);
   
    HCSR04_waiting_echo = 1;
    HCSR04_current_loops = 0;
  }
}
#endif


and change:

Code: Select all

  if (MAG) Mag_init();

by

Code: Select all

  if (MAG) Mag_init();
  if (SONAR) Sonar_init();

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by copterrichie »

You know, this may just work. I had tried using the Ultrasonic library but I would experience random reboots. The way you have this coded, I don't see that happening. Going to give this a try.

Thanks

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

have anyere a idear how to switch between SonarAlt and BaroAlt automaticly ?

sorry for my english :oops:

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

LuFa wrote:have anyere a idear how to switch between SonarAlt and BaroAlt automaticly ?

sorry for my english :oops:


The problem is that Baro give us a absolute altitude (sea level) and de sonar give us a relative altitude (ground).

mahowik
Posts: 331
Joined: Sun Apr 10, 2011 6:26 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by mahowik »

LuFa wrote:have anyere a idear how to switch between SonarAlt and BaroAlt automaticly ?


I have used the following... i got the main idea from megapiratNG:

Code: Select all

#define MAX_SONAR_RANGE 400 
#define BARO_TO_SONAR MAX_SONAR_RANGE+150 // Due to low baro accuracy
#define SONAR_TO_BARO_FADE (MAX_SONAR_RANGE/3) // 33% of Max sonar range
#define SONAR_TO_BARO_FADE_FROM (MAX_SONAR_RANGE - SONAR_TO_BARO_FADE) //4m-33% = 2.67m start value to fading from sonar to baro

int32_t  currAlt;
  if(SONAR){
    sonarTempDistance = (sonarTempDistance - (sonarTempDistance >> 3)) + sonarDistance;
    sonarDistance = sonarTempDistance >> 3;
 
    if((BaroAlt - BaroAltOffset) < BARO_TO_SONAR){
      float scale = (sonarDistance - SONAR_TO_BARO_FADE_FROM) / SONAR_TO_BARO_FADE;
      scale = constrain(scale, 0, 1);
      currAlt = ((float)sonarDistance * (1.0 - scale)) + ((float)(BaroAlt - BaroAltOffset) * scale);
    }
    else{
      currAlt = (BaroAlt - BaroAltOffset);
    }
  }
  else{
    // no sonar altitude
    currAlt = (BaroAlt - BaroAltOffset);
  }


also sorry for BIG delay... i stopped the investigation of the current idea because as i have found out that my motors make a noise which "push" the echo signal from sonar and it not possible to test with altitude more than 50-80cm... so if someone has mega1280+DYP_ME007_sonar you can try to do something with that.
some notes and condition before use this:
1. u should have tuned alt-hold (i.e. based on baro only) before, because it uses the same alt estimator and pid regulator
2. BaroAltOffset remembered on gyro and acc calibration
3. alt-hold should work based on sonar data in range 0 - 2.67m,
then 2.67-4m for mix of sonar+baro,
and >4m => based on baro only
Attachments
MultiWii_1_9_a2.zip
(52.95 KiB) Downloaded 741 times

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

Hi Mahowik ,

I have download your .zip folder ;)
But in your code are any parts commented out , should it work without ? or is need to put it in the code ?

works your code only with arduino mega or also with Mini Pro ?

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

Well, another change and altitude is implemented with sonar:

In MultiWii_1.9 change

Code: Select all

static int32_t  EstAlt;             // in cm

by

Code: Select all

static int32_t  EstAlt;             // in cm
static int32_t  InitialAlt = 0;             // in cm



In IMU.pde change getEstimatedAltitude method

Code: Select all

void getEstimatedAltitude(){
  static uint8_t inited = 0;
  static int16_t AltErrorI = 0;
  static float AccScale  = 0.0f;
  static uint32_t deadLine = INIT_DELAY;
  int16_t AltError;
  int16_t InstAcc;
  int16_t Delta;
 
  if (currentTime < deadLine) return;
  deadLine = currentTime + UPDATE_INTERVAL;
  // Soft start

  if (!inited) {
    inited = 1;
    EstAlt = BaroAlt;
   
    // Apunto la altura de despegue
    InitialAlt = BaroAlt;
   
    EstVelocity = 0;
    AltErrorI = 0;
    AccScale = 100 * 9.80665f / acc_1G;
  }
 
  // Implementacion del sonar
  if(SONAR && (SonarAlt < 9999)) {
    EstAlt = InitialAlt + SonarAlt;
  }
  else {
     // Estimation Error
     AltError = BaroAlt - EstAlt;
     AltErrorI += AltError;
     AltErrorI=constrain(AltErrorI,-25000,+25000);
     // Gravity vector correction and projection to the local Z
     //InstAcc = (accADC[YAW] * (1 - acc_1G * InvSqrt(isq(accADC[ROLL]) + isq(accADC[PITCH]) + isq(accADC[YAW])))) * AccScale + (Ki) * AltErrorI;
     #if defined(TRUSTED_ACCZ)
      InstAcc = (accADC[YAW] * (1 - acc_1G * InvSqrt(isq(accADC[ROLL]) + isq(accADC[PITCH]) + isq(accADC[YAW])))) * AccScale +  AltErrorI / 1000;
     #else
      InstAcc = AltErrorI / 1000;
     #endif
    
     // Integrators
     Delta = InstAcc * dt + (Kp1 * dt) * AltError;
     EstAlt += (EstVelocity/5 + Delta) * (dt / 2) + (Kp2 * dt) * AltError;
     EstVelocity += Delta*10;
  }
}



When you start taking the absolute altitude (sea level). Is the altitude of takeoff.

If there is a valid measurement of sonar then the current altitude is the altitude of takeoff + sonar measurement

The problem I have is that will not work well if the ground is not level where we fly and we approach the ground at another point that is at another altitude as the takeoff.

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

mahowik wrote:
LuFa wrote:have anyere a idear how to switch between SonarAlt and BaroAlt automaticly ?


I have used the following... i got the main idea from megapiratNG:

Code: Select all

#define MAX_SONAR_RANGE 400 
#define BARO_TO_SONAR MAX_SONAR_RANGE+150 // Due to low baro accuracy
#define SONAR_TO_BARO_FADE (MAX_SONAR_RANGE/3) // 33% of Max sonar range
#define SONAR_TO_BARO_FADE_FROM (MAX_SONAR_RANGE - SONAR_TO_BARO_FADE) //4m-33% = 2.67m start value to fading from sonar to baro

int32_t  currAlt;
  if(SONAR){
    sonarTempDistance = (sonarTempDistance - (sonarTempDistance >> 3)) + sonarDistance;
    sonarDistance = sonarTempDistance >> 3;
 
    if((BaroAlt - BaroAltOffset) < BARO_TO_SONAR){
      float scale = (sonarDistance - SONAR_TO_BARO_FADE_FROM) / SONAR_TO_BARO_FADE;
      scale = constrain(scale, 0, 1);
      currAlt = ((float)sonarDistance * (1.0 - scale)) + ((float)(BaroAlt - BaroAltOffset) * scale);
    }
    else{
      currAlt = (BaroAlt - BaroAltOffset);
    }
  }
  else{
    // no sonar altitude
    currAlt = (BaroAlt - BaroAltOffset);
  }


also sorry for BIG delay... i stopped the investigation of the current idea because as i have found out that my motors make a noise which "push" the echo signal from sonar and it not possible to test with altitude more than 50-80cm... so if someone has mega1280+DYP_ME007_sonar you can try to do something with that.
some notes and condition before use this:
1. u should have tuned alt-hold (i.e. based on baro only) before, because it uses the same alt estimator and pid regulator
2. BaroAltOffset remembered on gyro and acc calibration
3. alt-hold should work based on sonar data in range 0 - 2.67m,
then 2.67-4m for mix of sonar+baro,
and >4m => based on baro only


is it possible to juse D8 and D12 for the ultrasonic sensor ?
will be cool , bacause i have no mega :(

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

LuFa wrote:is it possible to juse D8 and D12 for the ultrasonic sensor ?
will be cool , bacause i have no mega :(


Have you read my posts?

I use D8 and D12

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

yes , but i have a DYP_ME007 ultrasonic sensor , and i think this one doesnt work with your code :?
if yes , than i will test your code :D

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

LuFa wrote:yes , but i have a DYP_ME007 ultrasonic sensor , and i think this one doesnt work with your code :?
if yes , than i will test your code :D


I think it would work well, but in the code instead of dividing by 58, I think you'd have to divide by 118.

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

Vallekano wrote:
I think it would work well, but in the code instead of dividing by 58, I think you'd have to divide by 118.



great ! :D
Is your newest update with switch between Baro and Sonar ? and with Baro Offset ?
i will test it tomorrow :)

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by LuFa »

now i have test your code , Vallekano ;)

is it normal , that the SonarAlt Value does only refresh at the start ?
because when i power up the copter , is see the right Sonar distance , but it does not update the value on the GUI (degug3 window)
it shows only the first distance .

Next question is , should the Baro Alt be Zero because of the offset and Sonar ?

Steeze McQueen
Posts: 4
Joined: Fri Jan 27, 2012 10:09 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Steeze McQueen »

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?

Vallekano
Posts: 25
Joined: Sun Dec 25, 2011 7:07 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Vallekano »

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

Steeze McQueen
Posts: 4
Joined: Fri Jan 27, 2012 10:09 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Steeze McQueen »

Thanks Vallekano! I found a resource elsewhere that seems to indicate D8 and D12 are mapped to #32 and #37 on the Seeeduino, so hopefully I'll have some success when I try hooking this all up tonight.

mr_swell
Posts: 3
Joined: Thu Sep 22, 2011 11:48 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by mr_swell »

@mahowik
Good news! Sonar driver works now! :)
Cycle time is ok also.

hi all,
maybe a stupid question, but where the code in multiwii must be inserted?
Jo

User avatar
Bledi
Posts: 187
Joined: Sat Sep 10, 2011 6:36 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Bledi »

with the EOSBandi GPS I2C, a good solution is to add this sensor to the "extension" arduino board : this promini will do GPS and sonar to I2C conversion.

User avatar
guru_florida
Posts: 45
Joined: Sat Mar 26, 2011 4:51 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by guru_florida »

I integrated the SRF08 I2C sonar sensor into the latest dev. Details here:
viewtopic.php?f=7&t=1282

I also integrated into alexmos's code that has baro/sonar sensor fusion but I havent tested it yet. Looks good in the GUI.

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

the code for SRF08 works perfectly with my i2c shield for HC-SR04, just need some tweaking with address

Post Reply