Exercise: Robot following a ping-pong ball 
==========================================
(the traditional computer vision)

Put the camera on the top of your monitor and insert its cable into the USB port.

Open camera.py in your editor.

Study the program, which reads the image from the camera and displays it in a loop.

Notice the tricky feature of OpenCV that the image is displayed by waitKey(t)
where t is a timeout in ms. Indeed, OpenCV waits in the waitKey() for 10 ms
and pauses another instant of time in camera.read(), waiting for the next 
picture from the camera. The camera provides e.g. 30 frames per second (fps), 
so we have 1000/30-10 ms = 23 ms for processing. We could get 32 ms, if we 
call waitKey(1) instead of waitKey(10). 

If our processing is faster than 23ms, our program waits for the remaining time,  
grabbing the image from the camera. If it were slower, we would be in trouble
that we can solve by more threads, as we have seen in the previous lecture.
However, today, we employ traditional image processing that is fast enough on 
the contemporary PC.

The program ends when we press the key Esc (its code is 27)
We do not need to worry that we could miss some pressed keys since they are 
buffered in such a case. 

Launch the program by run-camera.bat to ensure that the camera is working fine.
Press Esc to exit (Please, do not forget to exit - two programs cannot work with 
the same camera at the same time)

Let us see into the detect_edges.py
Study the program. It calls the Canny algorithm for edge detection.
Notice that we add a trackbar to the image window, which calls the callback 
updateThreshold(). The trackbar enables us to select the threshold  
parameter of the Canny algorithm.

Launch the program and show the camera a ball hanging on a thread. 
You can increase contrast by putting your hand behind the ball. 
You can also slightly touch the ball to stop its swinging.
Notice the difference between the static ball and the swinging one.
Play with the threshold and find its optimal value for your conditions.
Avoid blur in the program or increase its kernel size and follow 
the effects regarding noise and edges.
Press Esc to exit 

Now, deal with the detect_circle.py
Study the program which calls the Hough transform.
Show the ball hanging on the thread to the camera.
Pay attention to the accurate ball's size on the displayed image. 
Play with the threshold and understand how it is challenging to avoid 
detecting phantom circles and seeing the actual one.
Try to swing the ball a little bit and observe the effects.
Improve contrast with your hand or a sheet of black paper. 
Change lighting conditions in the room and follow the effects.

Now we are ready to play with the real robot.

Remote access to the robot is provided by ssh clients like putty (remote console) 
and WinSCP (file transfer). 
(Install putty from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
on your notebook. It is already installed on computers in the terminal room.)
(Install WinSCP from https://winscp.net/download/WinSCP-6.3.2-Setup.exe on your notebook
on download its portable version from https://winscp.net/download/WinSCP-6.3.2-Portable.zip
on computers in the terminal room.)
See access.txt for credentials; credentials are the same for Putty and WinSCP since they employ 
the same protocol (ssh,scp).

The robot is controlled by Jetson Nano loaded with Ubuntu OS.

Robot gear is connected to the Jetson's I2C port.
See simple/demo_motor.py - a demo of the motor control.
Connect to the robot via SSH (putty) and launch the program.
It is already uploaded to the robot and stored in ~/src/lesson5/.
You must use python3 since python is version 2.

See simple/demo_camera.py and notice that it is much more complicated than
the analogical solution on a PC. Even it is not easy to grab and display images that
are not delayed on robots. Therefore we need to employ the Gstreamer interface 
to reduce and sample images from the camera during their acquisition using
a complicated Gstreamer pipeline. Connect to the robot via VNC and launch the program.
(Install the TightVNC from https://www.tightvnc.com/download.php on your notebook or 
download portable TightVNC from https://download.cnet.com/tightvnc-portable/3001-7240_4-75332206.html
on computers in the terminal room.)
See access.txt for credentials.

Now, look at the hough directory. The robot's board is less powerful than a PC 
and powered locally by a power bank. Therefore we have to use a modular
architecture. See main.py to investigate the employed agents.
Launch the system on the robot.
Try the robot to follow the ball on different surfaces, and put some complicated
solids (like a keyboard or textured cloth) into the robot view to affect
operability of the robot.

Finally, we mention that methods like the Hough transform are also helpful today if we 
connect them to better edge detectors. Launch edges.py in the deepedge-simple directory
(on your computer) to check how we can get edges with deep neural networks (so far, concern 
it as a black box). Pay attention to the edges on the tablecloth in the upper left.
Optionally, you can check the codes of the Jetbot's control system, which employs this idea, 
in the deepedge directory. 