# Robotics Training LESSON 18: Obstacle Detection and Collision Avoidance

In this lesson we explore using the HC-SR04 to detect obstacles, and then avoid collisions by stopping the car. This requires a more sophisticated control algorithm for the desired distance for the car to travel. Making it even more tricky, after the obstacle is removed, the car needs to resume its programmed mission. The video shows my solution to this problem

# Robotics Training LESSON 17: Measuring Distance to Obstacle using the HC-SR04

In this video lesson we show how to use the HC-SR04 ultrasonic sensor to detect the distance to obstacles in a robotic application. We use the sensor to measure the time it takes for a ping to travel from the sensor, to the target, and then back. We use this measured time, and the known speed of sound to calculate the distance to the target. This will be used in future lessons to keep the robot from running into an obstacle.

# Robotics Training LESSON 16: Using the HC-SR04 Ultrasonic Sensor for Echolocation

In this lesson we explain the concept of echolocation, and how the HC-SR04 ultrasonic sensor can be used to measure the time it takes a ping to go out, bounce off a target and come back. From this ping travel time, it is possible to  precisely calculate the distance to the target. For your convenience, the code we developed is included below.

# Python with Arduino LESSON 4: Expanding your Virtual World

In this lesson we will expand the virtual world we created in Python with Arduino LESSON 3. We will be creating a virtual world that will track a simple scene in the real world. In this project, the virtual world will track both the position and the color of a target in the real world. This lesson requires that you have the Python software and libraries installed, which we explained in LESSON 2.

This Lesson will be a bit more involved, and I will take you through it step-by-step. I will need to break things into two parts. In today’s lesson we will cover the Arduino side. We will develop the software that will measure distance and color, and then send those numbers over the serial port. Then in tomorrows lesson, we will develop the Python software to create a really cool virtual graphic to display the data in a virtual world.

For this project you will need the HC-SR04 ultrasonic sensor, the TCS230 Color Sensor, the Arduino Microcontroller, and some male/female jumper wires to connect to the color sensor.

The Ultrasonic Sensor can be attached per the schematic below:

Detailed tutorial on using this sensor was described in Arduino LESSON 18, so we will not go through all the details of using the sensor here. Review that lesson if you need more help. Key point here is to connect it as seen in diagram above.

You will also need to connect up the Color Sensor.

#### Connecting the Color Sensor to the Arduino

###### Arduino Pin
S0 GND
S1 5V
S2 pin 7
S3 pin 8
OUT pin 4
VCC 5V
GND GND

Use of the color sensor was described in detail in Arduino LESSON 15.  You should be able to develop to write the software yourself based on earlier lessons to make measurements from both the Color Sensor, and Ultrasonic Sensor, but if you get stuck, you can glance at my code below. Again, it is important for you to write your own code and not copy and paste mine. Mine is just a reference if you get stuck.

The key point to notice with this code is the print statements, summarized below:

Notice that we are printing  our color strengths and distance on one line separated by commas. It is important to note the order of the data. When we read this in Python, we will read it in as one line of text, and then we will parse it into its individual values. So, we must make note and remember the order the data is arranged in in this line.

Remember when you have your python program reading this data, you must have your serial monitor closed. For now though, run your program and look at the serial monitor to verify you are getting correct data in the expected format.

In the next Lesson, LESSON 5, we will build the Python program to create a virtual world from this data.

# Python with Ardiuno LESSON 3: Simple Virtual World Using Ultrasonic Sensor

In this tutorial we will show how you can use Python with the Vpython library to begin to create some pretty cool graphics for presenting sensor data from the Arduino. For this tutorial, we will be using the HC-SR04 ultrasonic sensor, which you learned about in Arduino LESSON 17,  Arduino LESSON 18, and Arduino LESSON 20. Please review those lessons if you are not familiar with the HC-SR04.

The circuit is very simple and can be connected from this schematic:

Since the object of this lesson is to show what you can do using Python and Vpython, I will not go through the arduino code step-by-step. You can go back to the earlier lessons for more info on the HC-SR04 sensor. The code that will allow the sensor to make distance measurements is presented again here. The one thing to note is that since we are going to be using Python to graphically present the data coming from the sensor, we want to simply send the distance measurement over the serial port, and no words or anything else, just the raw distance measurement. This will simplify things on the Python side. Remember that when we do a Serial.println() command in arduino, we can read whatever is printed into Python, as we learned in Arduino and Python LESSON 2.  The code below is what you need on the Arduino side.

This code will be constantly sending the distance to the target out over the Serial port.  On the Python side, our first task is to read that data in over the serial port.  To do this, you must import the serial library. (Instructions on installing Pyserial are in Arduino with Python LESSON2.) You then create a serial object which will be used to read the data. In the sample below, we call the object ‘arduinoSerialData’. We then create a While loop that loops continuously. Inside that loop we check to see if there is any data available on the serial port, and if there is, we read it into the variable myData, and print it.

Remember than in the line that creates the adruinoSerialData object, you need to change the com port to whatever com port your arduino is sending on. You can see this by looking under tools- port in your arduino IDE window. Also, this format is for windows machines. You would have to adjust for apple computers.

It is important to remember that the command .readline() in Python will read a string, so we need to remember that myData is a string, and if we want to use it as a number we will need to convert it to a float, with something like distance = float (myData). Then distance will be a normal number, not a string.

As a first demonstration lets create an object using the vPython library that is a cylinder. We will need to import the vPython library, and we will create the object before the while loop, and then inside the while loop, we will adjust the length parameter of the cylinder. We will make the cylinder with a length of six inches, we will make it yellow in color, and with a radius of 1/2 inch. Also, when we are using Vpython and dynamically updating a graphic object, inside the loop that is adjusting the graphic, we have to issue a rate() command. The rate command tells vPython how many times a second you want to go through the loop. You need to play with this command, so that it gives smooth graphics for the rate at which the Arduino is sending data. rate(20) is sometimes a good starting point. So, our code now looks like this:

So, this is pretty cool! It creates a little rod, and the length of the little rod dynamically changes in response to how far your target is from your sensor. This gives a very nice qualitative visual to what is happening in the real world with your sensor. Often times you also want quantitative indication of the data.  We can do that by adding a label. Before the while loop, we will create a label object called lengthLabel. We will position it up just a little bit, so it is not on top of the measuringRod. We will set the label initially to ‘Target Distance is: ‘. We will also want to set box=false, since we do not want a box around our text. Then, a good height for the label is about 30 pixels. You can play around with all these settings. We create the lengthLabel before the while loop, but then inside the while loop we dynamically update the lengthLabel.text parameter. We set it to our myLabel string, which is dynamically being updated to be the concatination of the string ‘Target Distance is: ‘ and the string myData. Remember, myData is read as a string, and so we use that variable, and not distance, which is a number, not a string. Pulling this together leads to this code:

Lets keep playing around with this. The graphic will begin to look more like a’Virtual World’ if we create a box to represent the target we are using in the real world.  I will make the box the dimensions of the real target, which is .2 X 3 X 3. Also, I will make it green, just like the target in the real world. In order to better fill our viewing window, I will move the cylinder down by about 2 inches, so its new position will be (-3,-2,0). For box type objects position is measured from the center, so I will need to also move the target box down by about .5, so it will coincide with the measuring rod. this is because 1/2 the target would be 1.5, and moving down by an additional .5 will make it coincide with the measuringRod.

Finally, I need to dynamically update the position of the target box in the while loop. I will need it to be placed at target.pos=(-3+distance,-.5,0). Along the x-axis this will put it right at the end of the rod, and will keep it properly positioned in y, as before. Bringing all this code together we get:

Play around with the parameters until you get something you are happy with. Also, you can change your view of the visual once the program is running. Right mouse click and you can change your positional view of the virtual world you have created. Press and scroll the mouse wheel to change the zoom.

OK, you need to start exploring and creating your own virtual world. Go ahead and see if you can create additional objects to make your virtual world more realistic. Try creating a graphic for the sensor itself, and maybe even your PC board and arduino! You can refer to the Vpython site for more details on the 3D objects you can create, and their parameters at:

http://vpython.org/contents/docs/cylinder.html

Also, if the discription in this lesson is confusing at all please just watch the video, and I will take you through things one step at a time.