. Those of you who saw our previous videos have probably been waiting with baited breath for our sequel to the 360 degree robot radar, video in which we promised some source code examples. We were briefly going to recap on the design and development of the hardware. However, when we began filming, we noticed that our 3d printer was misbehaving. The final prints were very ugly and uneven. Even looking back at this footage, we can see that at the start of the builds they are stringy, bubbly and uneven. If we had have noticed this by even a few layers, we would have stopped the pin after a close inspection. We found this crack on the right side said, bearing not sure if this could cause the problem we are having, but we dont have time to find out just now, then somebody showed an interest in our project and talked us into selling our test week. So we have made this video for you to be getting on with partly with new footage and partly with archive stuff. After this video is published, we will build a new rig for more development and tests. So for the hardware you may want to look at our previous videos. I will include the links in the description and please visit our website at so hyphen. Now, dot com forward, slash robotics, forward, slash radar for videos, parts lists, stl files for those with 3d printers and, of course, to find the source code listings described in this video, and so, as promised, the code.

We are using an arduino nano to position our sensor and gather the data, so why not use the arduino compiler to do the job? A look at the first few lines of code shows we are using the new ping library to drive our sensor. The hc sr0 ultrasonic ranger. First, we include the header file, then we have to define which of the pins on our arduino. We wish to use to interface with our sensor. Once those pins are defined. We can create an object of type nuping with the name sonar which we will use to refer to it later in the code. This line is just a comment reminding us of the value of pi. We will not be using it here, but it may come in handy later. Here we define the arduino bins. We will be using to control our stepper motor using the uln2003 stepper driver. The last highlighted line declares an integer variable. I motor speed. We use this later to add a slight delay to prevent our stepper randomly skipping steps. Next, some global variables, i step count, keeps count of the current step. I accounts per rev 512. In our case, i look up is an eight element array to hold the pin configuration for each of the steps required to turn one 512th of a rotation i.e. One step use this variable to keep a count of radians covered so far. If this value rolls past two times, pi reset it to zero in our setup function, we configure our motor pins as outputs, then configure and open the serial port.

That brings us on to our main program. Loop inside this loop is a for loop, since we want to rotate our sensor. One step take a measurement 512 times per revolution: the for loop controls, the program flow restarting after i accounts per rev in this case 512 steps. We first make a call to this global function. Its purpose is to move the sensor one step. We will look a little closer at this soon. We next increment our radian angle. If the angle is greater than two times pi, we have completed a full rotation reset. It to zero. We then attempt to get a range from our sensor. If there is nothing in range, the sensor will return zero. Now all thats left to do is to export our data to anybody. Listening. We are using the serial port to transmit our data. We create a string object. This is not the most efficient way to use strings in c, but it is quick to code and works. Okay for a program this size we create a string with a comma delimiting each field. Then we print the string to the serial port. Now you may remember the clockwise function we mentioned earlier lets have a closer look at that we have another for loop, this time with eight steps and for every one of those steps we call another function set output, the set output function, writes the binary data to The stepper controller to cause it to move forward the correct number of stepper micro steps.

In our case, eight micro steps equals one of our desired full steps and, as we mentioned earlier on, if we dont add a brief delay in between steps, the stepper randomly misses steps. That concludes the section on the arduino code: upload it open a serial port and you should be able to test that its all working and on to the processing code. We chose processing for several reasons. It is free to download. It is based on the java platform, which is quite common when you get stuck, it has a good website over at processing.org. Also, several of our viewers asked for examples written in processing. Presumably it has become more common and many more programmers are using it to be fair. It is now two years since we worked on this code, so some of the more complex equations are long behind us. We will try to explain what we can, but the program works, as is so. If you are unsure make some changes see the result, see if you can understand it better. If it breaks, revert to the original version and break it, some more, we will start at the top. The processing development environment is similar to the arduino ide. The bare bones: application has a setup function and a main program loop in arduino. The main program loop is called loop in processing. It is called draw. One of the intended purposes of the processing language is for developing graphical interfaces.

If we click the run button in this empty sketch processing creates a window for us. This window is not big enough for our project. Our radar screen will have a size of 400 by 400 pixels. We also want a one pixel border around the edge, so we will set our window size 402 by 402 and set the background to black. We are going to be using an image object to draw our radar on, so we need to declare it as a type of p image which we will refer to as radar later create the image in the window and later we will need some transparency for fading Pings and stuff, it seems the image does not have alpha channel by default and the only way to add it is pixel by pixel. So the for loop here does that, for each pixel, in the image frame rate sets the desired refresh rate, the image command actually places the image on our window and the set location positions the window on the physical screen and theres. Nothing to see here so far as said before, in this video, the data is sent to this app using the serial port. The next block of code is explained in the comments basically list. The available ports for debugging select the first available one empty, the buffer of any junk, which may be there so far, and none of the serial code will work unless we import the serial library lets do that now and we need to add the global variables required To make it work, that concludes the setup steps now onto the main program loop.

In some cases we would not yet have a data source, so we may create a temporary application to provide sample data. In our case, we have a sonar antenna, providing real data start by capturing the data from the sonar. This application is pretty much event driven if there is no data in the buffer do nothing again. Plenty of comments help us to follow along, while bytes of data are available, read them into our string variable until we read an end of line character, double check that we have some data. We pass this data into some variables for use later and since we may use this data in other functions, we make variables global and declare them at the top. One thing we need to take care of is that our data is sent to us as comma separated values. We create an array of values from the data and copy each element of the array into our variables. Having got this file, it should be possible to test the data transmission and reception, so we add some debug code to print our data to the debug window and, as we can see from the lower left of the console, what appears to be meaningful data now its Time to begin plotting our graphics on the screen and we are going to make a separate function for that. We will name it sweep and declare the function now in order for us to plot a point on our screen.

We need the angle and range the angle and range is given to us by the sonar. We need to use the sine and cosine functions to define the angle on the screen. Then we need to throw range into our calculations and plunk a blob on the screen. In the right place, dont forget the angle and range is given to us. What we are calculating here is the perceived position on the screen. Unfortunately, processing would not let us use the set function to block a blob. We didnt have time to find out why or how we did find out. However, that processing would allow us to draw a line. So we cheated and use a short line to draw our ping there. We have real data being projected on screen. It is, however, persistent that is once it is drawn. It stays there if we turn the antenna assembly 45 degrees left. New data is drawn to the screen 45 degrees to the right. The screen is becoming quite cluttered. We need some way to remove old data in the same way as would a real radar. One way to do in processing is to create a tint in this case black, but semi transparent, then apply it to the image each program loop, this overlays our current image, making it dimmer each time as we can see it works, but too fast. We need to slow it down, adding some code to apply the tint. Wait for a few program loops then apply.

It again slows down the fading effect. We need a couple of global variables to keep track of how many loops have occurred since our last fade. Okay, looking a little bit more plausible, a few more lines of code will make it more realistic. Some radar screens have a highlight line to indicate where the antenna is pointing. We will create a simple line from the center of the radar screen to the edge based on the current angle, and i think well add it to the sweep function. Here we calculate the angle from the center to the edge the plus 0.08 in these lines draws. The line just a little ahead of the real angle, this prevents the green line. Overwriting our pings set the pencil colour set. The pencil thickness draw the line, looking a little more realistic one, more audition to finish it off. We will let you go through this last function to figure out what it does and how it does. It add a call to our new function. That concludes the 360 degree. Robot radar, 2022 video. I hope everybody can get something from it. Please, like subscribe and most important leave a comment check out the links to the previous videos.