Drones have become cheaper and cheaper which is increasingly a problem at airports because people sometimes fly them in the airspace near the planes. If a plane wre to run into a drone, bad things could happen
There are not a ton of choices to take down drones that wander into the air space of an airport. One could try to snare the drone with a net, but that can be complicated. One could try to shoot it down with a machine gun, but the bullets that miss eventually land somewhere.
A promising option is to use a laser to shoot down the drone. Right now the technology is primarily used by the military but eventually it will be something that civilian airports can use.
In this activity we will consider drones that are moving in a straight line ner an air port. We will need to aim a laser system at the drone and get the focusing of the laser right to shoot down the drone. If we are pointing in the right direction, but the focal point is off then the intensity of the light won't be high enough to damage the drone. This is a real effect. If you are curious you can learn more about how the intensity decreases away from the peak focus you cana read about it in this wikipedia article
Step 0. Open up the code
Click here to open up the code
Go ahead and run the code to see what happens.
Step 1. Look at the code
The code here is pretty short. Here is the entire program:
x = random(250,500); y = 500; vx = random(-10,10); vy = random(-20,-5); dt = 0.1; function draw(){ // Update location x += vx*dt; y += vy*dt; // Draw axes and other stuff display(); drawBlob(x,y,vx,vy); // Add more graphics here before the end of draw() } // end draw()
There are a couple of things to note at this point:
1. The code at the very beginning before function draw() only gets run once. Here is that part of the code:
x = random(250,500); y = 500; vx = random(-10,10); vy = random(-20,-5);
This code is doing what's called "initializing the variables". It is telling the computer, hey, we need to use variables named x,y, vx and vy.
The random function there will set the variable to a random value that is between the two numbers listed. So for example this code:
x = random(250,500);
Will set x to a random number between 250 and 500
2. The code within the curly brackets of function draw() gets run over and over again 60 times a second.
3. Once the drone has a horizontal (vx) and vertical (vy) velocity, the x and y values for the drone will continue to be updated according to this code:
x += vx*dt; y += vy*dt;
There is nothing in the code right now that changes vx and vy so the drone will just move in a straight line.
There are a couple of steps to add a laser to the code.
Step 3a. Add variables for the laser
At the beginning of the code add these variables:
xfocus = 375; yfocus = 250; xlaserbase = 375; ylaserbase = 0; var laseron;
Step 3b. Add the drawLaser function
Towards the end of the code (after the display() function but before the part of the code that says DO NOT ADD ANY CODE AFTER THIS LINE) add this code:
drawLaser(xlaserbase,ylaserbase,xfocus,yfocus,laseron);
Step 3c. Move the laser focus with the arrow keys
Inside the draw() function add code to change the laser focus with the arrow keys:
if (keyIsDown(LEFT_ARROW)) { xfocus += -2; } if (keyIsDown(RIGHT_ARROW)) { xfocus += 2; } if (keyIsDown(UP_ARROW)) { yfocus += 2; } if (keyIsDown(DOWN_ARROW)) { yfocus += -2; }
Step 3d. Fire the laser!
You may notice at this point that we declared laseron as a variable at the beginning of the code but we never actually use it. In other words, the laser is there and you can see where it is aiming but it never actually turns on. Let's add some code to turn on the laser when you press spacebar:
if (keyIsDown(32)) { // spacebar laseron = true; } else { laseron = false; }
At this point your program should behave like this
Optional: Change the color of the laser
It is totally optional but if you want you can change the color of the laser by following a few steps
The first thing to do is to add a variable to the beginning of the program called lasercolor like this:
lasercolor = [0,255,0];
This specifies an rgb color for the laser. The code above means zero red, zero blue, maximum green.
Second thing to do is to change the drawLaser function to include an extra argument called lasercolor like this:
drawLaser(xlaserbase,ylaserbase,xfocus,yfocus,laseron,lasercolor);
You can add ,lasercolor as the extra argument or you can just replace drawLaser with the code above. After you do this you should be able to see the new color of the laser.
To pick out your own rgb color for the laser a helpful thing to do is to go to google and search on "color picker" Pick whatever color you want. Have fun!
Now we can fire the laser and we can aim it at the drone but if we hit the drone nothing happens.
Let's create a function to detect if the laser focus is close enough to the drone to shoot it down
function dronehit (_x, _y, _xfocus, _yfocus) { // Hint: Use the pythagorean theorem in the if statement if (???????? < 15 ) { return true; // The laser damaged the drone! } else { return false; // The laser did not damage the drone! } }
In the code above you will need to replace the ???? with an estimate of the distance between the drone at (x,y) to the laser focus at (xfocus,yfocus) using the pythagorean theorem.
Advice: Avoid using ^2 to calculate the square. In JavaScript ^2 does not mean to the power of 2. If you really want to do something like this instead use **2 to raise things to the power of two.
Once you have figured this out and added the function to your code, add another if statement inside of the draw function:
if ( dronehit(x,y,xfocus,yfocus) & laseron ) { vx = 0; vy = -20; // drone is falling }
After you make these changes your program should behave like this
Important: After you make these changes, draw a flowchart for how the computer program determines whether the drone has been hit
Once the drone has landed on the ground you can end the game. Here is some code you can add that will do that:
if (y < 0) { if ( ???? ) { drawText('You did it!',0.4*width,0.8*height); } else { drawText('Oh, no!',0.4*width,0.8*height); } noLoop(); // end the program }
Your job is to replace the ???? with a conditional expression that will detect if you managed to damage the drone or not.
If you implement this correctly your program will behave like this
Note: There is more than one correct way to detect if you managed to damage the drone
Draw a flowchart for the entire game!
Make the game more fun by changing the code. You can change it however you wish!
An easy thing to change is the initial velocity of the drone which is this part of the code:
vx = random(-10,10); vy = random(-20,-5);
If you change these numbers you can get the drone to go a bit faster, which is more fun!
Another fun idea is to add a cooling time for the laser. Real lasers have to wait some period of time to cool down before they can fire again so the optics inside don't melt. Modify the code so that after you press spacebar the first time, you have to wait at least a few seconds before you can fire it again.