Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Obstacle avoiding Zumo with Intel Edison


#1

After not able to use the reflectance sensor with Edison, i tried making a obstacle avoiding zumo with HC-SR04 ultrasonic sensor.
But i have trouble making it “smarter” as it can only turn right.

[code]
#include <ZumoMotors.h>

/*

  • This example uses the ZumoMotors library to drive each motor on the Zumo
  • forward, then backward. The yellow user LED is on when a motor should be
  • running forward and off when a motor should be running backward. If a
  • motor on your Zumo has been flipped, you can correct its direction by
  • uncommenting the call to flipLeftMotor() or flipRightMotor() in the setup()
  • function.
    */

ZumoMotors motors;
int trigPin = 13; //Connect pulse trigger pin to pin 13
int echoPin = 12; //Connect echo/receive pin to pin 12

void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

// default Edison Arduino shield is PWM pins 3,5,6,9 jumpers and this function needed to change
// the order of the swizzle defines the PWM number, PWM0,1,2,3
setPwmSwizzler(3,5,10,9);

// uncomment one or both of the following lines if your motors’ directions need to be flipped
//motors.flipLeftMotor(true);
//motors.flipRightMotor(true);
}

void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW); // initialise to low state
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Trigger a high pulse
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // Set to low pulse
duration = pulseIn(echoPin, HIGH); //determine the duration where a high pulse is received
distance = (duration/2) / 29.1; //Speed of sound = 340m/s, 1 cm = 29.4 microsecond
Serial.print(distance);
Serial.println(" cm");

if (distance>10)
{
motors.setRightSpeed(100);
motors.setLeftSpeed(100);
}
else
{
motors.setSpeeds(50,-50);
}
}[/code]


#2

I tried making it rotate left if there is no way out on the right. There is no error but the Zumo is not working according to how i wanted it to. I know I am doing it wrong as my programming is bad.
I would like my final product to look like this:
https://www.youtube.com/watch?v=szpbB24MjKo
Thank you.


#3

Modified program but still not working

#include <ZumoMotors.h>


/*
* This example uses the ZumoMotors library to drive each motor on the Zumo
* forward, then backward. The yellow user LED is on when a motor should be
* running forward and off when a motor should be running backward. If a
* motor on your Zumo has been flipped, you can correct its direction by
* uncommenting the call to flipLeftMotor() or flipRightMotor() in the setup()
* function.
*/


ZumoMotors motors;
int trigPin = 13; //Connect pulse trigger pin to pin 13
int echoPin = 12; //Connect echo/receive pin to pin 12
int i;
void setup()
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
// default Edison Arduino shield is PWM pins 3,5,6,9 jumpers and this function needed to change
// the order of the swizzle defines the PWM number, PWM0,1,2,3
setPwmSwizzler(3,5,10,9);

// uncomment one or both of the following lines if your motors' directions need to be flipped
//motors.flipLeftMotor(true);
//motors.flipRightMotor(true);
}

void loop()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);   // initialise to low state
  delayMicroseconds(2);         
  digitalWrite(trigPin, HIGH);  // Trigger a high pulse
  delayMicroseconds(10);        
  digitalWrite(trigPin, LOW);   // Set to low pulse
  duration = pulseIn(echoPin, HIGH);  //determine the duration where a high pulse is received
  distance = (duration/2) / 29.1; //Speed of sound  = 340m/s, 1 cm = 29.4 microsecond
  Serial.print(distance);
  Serial.print(int i);
  Serial.println(" cm");

  if (distance<=10)
  { 
    motors.setSpeeds(-100,100);
    i++;
  }
  else if (distance>10)
  {
    motors.setRightSpeed(100);
    motors.setLeftSpeed(100);
    i=0;
  }
  else if(i = 10)
  {
    motors.setSpeeds(100,-100);
    delay(4000);
    i = 0;
  }
}

#4

Hello.

I looked at both versions of your code and noticed a few things wrong.

In your first version, I do not see any condition that tells your robot to go left if it does not find an exit when turning right. According to your program, the robot will keep moving forward if the distance reading is greater than 10 cm; otherwise, the robot will turn right until the distance reading is greater than 10 cm.

In your second version, you declared int i globally, but it looks like you redeclared that variable in loop, Serial.print(int i);. I suspect int is a typo. (By the way, it is best practice to assign i an initial value to avoid unexpected behaviors in your program.) Also, judging by how your conditions are written, I do not think that your program will execute your last condition where i = 10. With multiple conditional statements, each conditional statement is checked in order until one of them evaluates to true, then the rest are ignored. Since your first two conditions meet all possible states, the last conditional statement will always be ignored. You might find reading this tutorial about If statements in C++ helpful.

From the video you linked, it looks like the robot sweeps left and right after detecting something in its path. I suspect that the robot is comparing the average (reading) for each direction to determine if it should turn right or left. There are many ways to program that behavior. You might consider looking at the Zumo’s line-following code when it calibrates its sensors by rotating left and right over the course line. That movement is similar to what you want, so you might be able to reuse that code, but you would need to replace the sensor reading code with your own code for reading the ultrasonic sensors.

- Amanda


#5

Yes. that line should not be there.Will read your recommended tutorial .
Before using the ultrasonic sensor, i used the reflectance sensor that came with the zumo robot but it just don’t work with Edison. Asked in this forum once and found out that other users, although there is not many of them, have the same problem. So i would stick with the ultrasonic sensor.
Thank you