Intro: How to Program a Cleaning Robot With the ArcBotics Sparki Educational Robot
Step 1: What You’ll Need
- An ArcBotics Sparki robot.
- A convex polygon with its perimeter line printed in black over a white background. Some lightweight obstacles, such as cardboard boxes, cylinders, or even balls.
Step 2: How It Works
The cleaning robot program is easier to be understand if we first think about what the steps are that the robot should actually do. The overall idea is to program the Sparki to do the following steps:
- Sparki rotates in its place, searching for an obstacle with its ultrasonic distance sensor (also called ultrasonic range finder).
- Once Sparki finds the obstacle, it advances (moving forward) towards it in order to expel that object outside the perimeter.
- The movement in the forward direction continues until a line sensor (or infrared reflectance sensor) detects the black line. That’s when the robot stops
- Now, the robot goes backward for a few milliseconds and rotates to the left a bit more to avoid detecting the same object again.
- The program now goes back to step 1.
Step 3: Check That Sparki’s Batteries Are Properly Connected and Charged.
Please remember to check that the batteries are properly connected (and charged!). And as we are going to use the motors here, please check that the On/Off Switch is on. Another important thing to take care of when playing with the robot’s motors is to be careful not to be working over a table. A fall from that table could permanently damage your Sparki.
Step 4: Program Sparki to Move Left and Right.
This can be accomplished using the sparki.moveLeft and sparki.moveRight commands. You can read more about how Sparki moves in the Moving Sparki tutorial.
Step 5: Program Sparki to Use Its Ultrasonic Sensor to Detect an Object in Front of It.
We need to stop the robot (using the sparki.moveStop instruction) when its ultrasonic sensor detects a close object in front of the robot.
Step 6: Now Combine the Previous Steps to Make Sparki Rotate Until It Finds an Obstacle.
Note: The complete code for the final version of this program is available at the end of this lesson. As you can see in the previous code snippet, everything is executed inside the loopfunction, which means that the code will be repeating until the robot is turned off (or the batteries are empty). And to be sure that we detect the object in front of the robot, we run another cycle (a while cycle) inside the loop, which ends each time the sensor detects an obstacle close enough (we set the detection distance in the while‘s condition). Finally, as the ultrasonic ranger has a conic detection zone, we added a small delay after the while, just to let the robot to center towards the detected object.
Step 7: Program Sparki to Move Towards a Detected Object.
Now we can make the robot move forward in the direction of the detected object, but taking care to not go outside the perimeter.
See the complete code above.
As you can see in the previous code, once the line is detected, the robot stops and goes backward for 1 second (you can change this time to experiment!) and then it moves a bit to the left again, just to avoid detecting the same object that it just expelled a few moments ago.
Step 8: EXTRA ACTIVITIES
Congratulations! Is your room clean yet? :)
Here are some Extra Activities to test your skills:
- Try to use the right, left and edge infrared reflectance sensors to improve the line detection.
- What about adding line detection to the robot’s rotation also (in the “object detection phase”)? This way, we make sure that it never goes outside the perimeter by error.
- Another possible place to add line detection is when the Sparki is going backward. You can try it!
- If you want to completely change the program, you can also take advantage of the capability of Sparki’s ultrasonic sensor to be moved with the servo. So why not code a new program where you move Sparki’s “head” to find the objects?
See the full lesson at: http://arcbotics.com/lessons/cleaning-robot/