Hi!
I have built Roomba style cleaning robot. First let me describe my bot and then I will ask for some assistance To tell the truth, I started this project to have some fun. But when I start my robot with mop, i realised that he is not for playing - he is mopping the floor quite well!
Hardware:
Aluminium plate (solid base), plastic ring (body) cutted from garbage bin. Controller is Arduino Uno, 2 motors for moving driven by motor-shield R3, 4xHC-SR04 ultrasonic sensors (one pointed front, two 120 degrees left and right, last one pointed aft). Bluetooth module. Recently active mop was added (made of paint roller driven by continuous rotation servo). See pictures for better understanding.
Code:
Here the problem starts. I thought it would be easy to find some similar "obstacles avoiding" project and modify the sketch for my needs. Now the robot can only be controled from smartphone by means of "Bluetooth RC controller". But I cant find suitable "obstacles avoiding" sketch for a project with 3 fixed (not one sitting on servo) ultrasonic sensors, and I'm not smart enough to write it from the begging. I'm not asking to write the sketch for me I'm just asking if somebody have similar project and can share the sketch? Any links are so much welcome!
My plans:
Sketch: Power On, robot starts cleaning floor in automatic mode, avoiding obstacles. Ideally to use compass readings in order to run the boot straight forward and compensate influence from active mop.
When connected to bluetooth, manual mode can be activated (already done).
To fit water/chemical tank.
Think about quick mop roller replacement.
Reduce general hight of the robot so it can go under some furniture.
Make carpet sensor - it will run only on laminate/linolium, recognizing the carpet as an obstacle.
Feel free to ask any question and feel free to help me to find suitable scetch.
Thanks for reading and for help in advance!
I'm not smart enough to write it from the beggining
If your smart enough to get this far, your smart enough to write any code! You just may not have the knowledge yet
int trig = 3;
int echo = 4;
void setup() {
Serial.begin(9600);
}
void loop()
{
long duration, cm; //duration and the distance result in centimeters:
// The PING is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echo,INPUT);
duration = pulseIn(echo, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
This is the code for a hcsr04 to measure distance from an object.
You can use the same logic and find distances of the front 3 sensors, then add if conditions.
if distance of right sensor is lower than a certain value=> obstacle is close and to the right side=> turn left
You can extend this logic and create cases(if front+right distance is low, if all 3 distances are low, etc)
Test for different limiting distances, see which is optimum....and you have the obstacle avoiding bot ready!
PaulS:
Can you revise your post to add some paragraphs? I don't read posts that ramble on for half a screen in one single paragraph.
I hope now it is better and more readable. Anything else?
Srijal97:
Hello Oleg, nicely made!
Thanks mate! Thanks for the code! Actually you are right. If I have more time I will finally write my own code. However I'm quite far from C programming; it will take time. Also I think my mistake was an attempt to use common trigger pin for all HC-SR04. I did this in order to spare some pins on my Arduino. Now I will change so every sensor will have individual Trigger and Echo pins.
eto_oleg:
Now I will change so every sensor will have individual Trigger and Echo pins.
I'm not familiar with the Arduino library used to monitor ultrasound sensors, but I know from using these sensors on other microcontrollers it's possible to share the Echo pins and just use individual Trigger pins. This could save you a few I/O pins. Another benefit of sharing Echo lines is a single interrupt can be used with all the sensors. Now that I think about, I'd be surprised if the Library didn't make use of shared echo lines.
When using multiple ultrasound sensors, you want to make sure the echos of one sensor have died down before triggering the next sensor. Noise from other ultrasound sensors is a very big problem if these aren't used correctly.
eto_oleg:
I hope now it is better and more readable. Anything else?
Thanks mate! Thanks for the code! Actually you are right. If I have more time I will finally write my own code. However I'm quite far from C programming; it will take time. Also I think my mistake was an attempt to use common trigger pin for all HC-SR04. I did this in order to spare some pins on my Arduino. Now I will change so every sensor will have individual Trigger and Echo pins.
Let us know of any progress, or if you need help with code
DuaneDegn:
I'm not familiar with the Arduino library used to monitor ultrasound sensors, but I know from using these sensors on other microcontrollers it's possible to share the Echo pins and just use individual Trigger pins. This could save you a few I/O pins. Another benefit of sharing Echo lines is a single interrupt can be used with all the sensors. Now that I think about, I'd be surprised if the Library didn't make use of shared echo lines.
When using multiple ultrasound sensors, you want to make sure the echos of one sensor have died down before triggering the next sensor. Noise from other ultrasound sensors is a very big problem if these aren't used correctly.
Oh yes, didnt think of that. But it may so happen that pulse from 1 sensor reaches rx of the other, this would change distance measured, right?
One question. It is about programming but i think I can post here.
I have a code where robot is getting commands from a Bluetooth serial. Then i have "switch - case" loop where depending what command been received robot executes different movements.
I want that in one of the "cases" robot goes to execute a loop or part of the of the void loop. In this part robot will perform obstacle avoiding program. However, one it receives "stop" command it should stop and come back to manual mode - "switch - case" loop. I was truing to do this algorithm in thousands of different ways but so far unsuccessful.
So basically i want to execute obstacle avoiding algorithm when variable modecontrol is 1 and quit when modecontrol = 0. But once I get inside of the while (modecontrol ==1) {} loop I can't get out of it. I also tried to put inside of while
command = Serial.read();
if (command == 'u') {modecontrol = 0}
but it didn't help to stop executing obstacle avoiding algorithm
The code is below. Could anyone help me?
void loop () {
if(Serial.available() > 0){
  timer1 = millis();Â
  prevCommand = command;
  command = Serial.read();
  //Change pin mode only if new command is different from previous.Â
  if(command!=prevCommand){
 Â
   switch(command){
case 'U':
   modecontrol = 1; // start automatic obstacle avoiding algorithm
    break;
   case 'u':
   modecontrol =0; // stop obstacle avoiding algorithm
    break;
   case 'D':
// do something elese
     break;
}
 }
  }
while (modecontrol ==1) {
// obstacle avoiding algorithm here
 }
  }