"3pi + SRF08" or "3PI + PING"

Hello and comment a few days ago I have to make my 3pi seek to remove obstacles and a maze.

I’m trying to connect a SRF08 or PING Ultrasound (Parallax). :bulb:

Has anyone managed to connect at all? That programming statements have you used?

Thanks

Hello.

It seems like using the Parallax Ping))) sensor is pretty easy; have you looked at the sensor’s documentation? I think the following code will work for you if you connect the Ping))) signal pin to PD0, but I have not tested it at all:

// send pulse to make the sensor send out a ping
set_digital_output(IO_D0, LOW);
delay_us(200);
set_digital_output(IO_D0, HIGH);
delay_us(5);
set_digital_output(IO_D0, LOW);
delay_us(5);

set_digital_input(IO_D0, HIGH_IMPEDANCE);  // make signal pin an input
while (!is_digital_input_high(IO_D0));  // wait for signal pin to go high
unsigned long pulseStart = get_ticks();
while (get_ticks() - time < 50000)  // loop for 50k ticks (20 ms)
{
  if (!is_digital_input_high(IO_D0))  // break out of loop when signal goes low
    break;
}

unsigned long pulseLength = get_ticks() - pulseStart;  // sonar pulse travel time in units of 0.4 us

Basically, you need to turn the signal pin into an output and drive it high to generate a 5 us pulse, which tells the Ping))) to send out a sonar burst. After a hold-off time of 750 us, the Ping))) sends out the burst, drives the signal line high, and waits for the echo, at which point it will drive the line low. Before the hold-off time ends, you need to make sure the 3pi has set the signal pin to an input, and then you need to measure the length of the sensor’s pulse. The sensor’s pulse will be between 115 us and 18.5 ms.

The code above is probably the simplest way to do this. You could improve it in multiple ways. For example, you could build a timeout into the first while loop that waits for the signal pin to go high to avoid getting caught in an infinite loop if something goes wrong (such as running the code without the sensor properly connected). Also, the above program blocks execution for up to 20 ms while waiting for the sensor’s results. You could rewrite it to use the OrangutanPulseIn functions to measure the output pulse without blocking program execution.

Please let me know if this code works for you. I’d love to see your code if you improve upon this.

- Ben

Hello!

In fact I’m trying to run the program gave me but I can not find the library to be added to pull the functions that are included in the program code.
What I have to add to my program?
Thanks

Have you read the 3pi’s user’s guide and looked at the resources available on the resources tab of the product page?

- Ben

Hi Ben, thanks for answering!

Of course I have reviewed the user’s guide and resources.

I just opened a new project included the libraries:

// AVR Studio is compiler
#include <avr/pgmspace.h>
#include <pololu/3pi.h>

// send pulse to make the sensor send out a ping
set_digital_output(IO_D0, LOW);
delay_us(200);
set_digital_output(IO_D0, HIGH);
delay_us(5);
set_digital_output(IO_D0, LOW);
delay_us(5);

set_digital_input(IO_D0, HIGH_IMPEDANCE);   // make signal pin an input
while(!is_digital_input_high(IO_D0))      // wait for signal pin to go high
{
 unsigned long pulseStart = get_ticks();
}


while(get_ticks() - time < 50000)         // loop for 50k ticks (20 ms)
{
  if (!is_digital_input_high(IO_D0))       // break out of loop when signal goes low
    break;
}

unsigned long pulseLength = get_ticks() - pulseStart;  // sonar pulse travel time in units of 0.4 us

I copied the code that you gave me and I get these errors:

avr-gcc -mmcu=atmega168 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT PruebaPing3pi.o -MF dep/PruebaPing3pi.o.d -c …/PruebaPing3pi.c
…/PruebaPing3pi.c:17: warning: data definition has no type or storage class
…/PruebaPing3pi.c:17: warning: type defaults to ‘int’ in declaration of ‘set_digital_output’
…/PruebaPing3pi.c:17: warning: parameter names (without types) in function declaration
…/PruebaPing3pi.c:18: error: expected declaration specifiers or ‘…’ before numeric constant
…/PruebaPing3pi.c:18: warning: data definition has no type or storage class
…/PruebaPing3pi.c:18: warning: type defaults to ‘int’ in declaration of ‘delay_us’
…/PruebaPing3pi.c:18: error: conflicting types for ‘delay_us’
c:/winavr-20090313/lib/gcc/…/…/avr/include/pololu/time.h:8: error: previous definition of ‘delay_us’ was here
…/PruebaPing3pi.c:19: warning: data definition has no type or storage class
…/PruebaPing3pi.c:19: warning: type defaults to ‘int’ in declaration of ‘set_digital_output’
…/PruebaPing3pi.c:19: warning: parameter names (without types) in function declaration
…/PruebaPing3pi.c:20: error: expected declaration specifiers or ‘…’ before numeric constant
…/PruebaPing3pi.c:20: warning: data definition has no type or storage class
…/PruebaPing3pi.c:20: warning: type defaults to ‘int’ in declaration of ‘delay_us’
…/PruebaPing3pi.c:21: warning: data definition has no type or storage class
…/PruebaPing3pi.c:21: warning: type defaults to ‘int’ in declaration of ‘set_digital_output’
…/PruebaPing3pi.c:21: warning: parameter names (without types) in function declaration
…/PruebaPing3pi.c:22: error: expected declaration specifiers or ‘…’ before numeric constant
…/PruebaPing3pi.c:22: warning: data definition has no type or storage class
…/PruebaPing3pi.c:22: warning: type defaults to ‘int’ in declaration of ‘delay_us’
…/PruebaPing3pi.c:28: warning: data definition has no type or storage class
…/PruebaPing3pi.c:28: warning: type defaults to ‘int’ in declaration of ‘set_digital_input’
…/PruebaPing3pi.c:28: warning: parameter names (without types) in function declaration
…/PruebaPing3pi.c:29: error: expected identifier or ‘(’ before ‘while’
…/PruebaPing3pi.c:35: error: expected identifier or ‘(’ before ‘while’
…/PruebaPing3pi.c:41: warning: implicit declaration of function ‘get_ticks’
…/PruebaPing3pi.c:41: error: ‘pulseStart’ undeclared here (not in a function)
make: *** [PruebaPing3pi.o] Error 1
Build failed with 8 errors and 19 warnings…

My question was that additional library should be included?

Please put [ code ] [ /code ] tags around your code (or use the Code button) to make it more readable. Coloring it green actually makes it less readable on the forum’s blue background. I have edited your post to add the code tags.

No libraries beyond the Pololu AVR library are required. Do you have much experience with C programming? The sample code I provided was not a complete C program; you need to put it into a function and call that from your main loop. Note that you should start by copying an example AVR Studio project from the library’s sample programs and modify that to contain your new program. If you create an AVR Studio project from scratch, you will have to configure it properly before you can use the Pololu AVR libraries.

Before trying to interface with a new sensor, I suggest you try playing around with some of the 3pi’s built-in hardware using the library’s sample programs. You could learn a lot by first trying to understand how they work and then trying to modify them to do something different. Once you have that experience, it should be easier for you to integrate my Ping))) code into a program.

- Ben

Hello!

Thanks for responding and advice.

Well my experience in C because I’m remembering that not long ago programmed in that language.

Well I will try how to insert the code in a sample project.

But the code by logic should work the same if not put in a function right?

It either has to go into a function or you can put it in main(). In a C program, execution begins at the first line of main(), so the following code snippet is not a valid C program by itself:

int i;
while (1)
{
  i++;
}

You could turn this into a complete, valid program, however, if you write it as follows:

int main()
{
  int i;
  while (1)
  {
    i++;
  }
}

Or you could put the code in a function and call it from main:

void doSomething()
{
  int i;
  while (1)
  {
    i++;
  }
}

int main()
{
  doSomething();
}

- Ben