How to program

This section provides links to materials describing different aspects of programming Arduino and Raspberry Pi boards.

Arduino

Since the Arduino board is a mature technology, there are lots of materials covering different aspects of developing programs to control peripheral devices connected to the controller.

The development process is simple: the programs are written on a PC or a laptop, them compiled and downloaded to the Arudino controller by using a USB cable. Most likely, you will use Arduino IDE to create programs, so it will perform all the actions described above automatically.

Before you attempt to prepare the first sketch (a sketch is the name that Arduino uses for a program) read few articles that could help you become more successful as an Arduino programmer:

If you have not had any experience with Arduino, it makes sense to go through a set of lessons to get basic concepts about programming this device. So do not hesitate to look at Arduino lessons.

Different ways to control motors and query sensors are covered in the section Electromechanical components, therefore they will not be repeated here. Instead, it is necessary to focus on some tips and tricks and advanced techniques that are used for complex projects.

For example, the portal The Robotics Back-End has a great list of Arduino tutorials. Here you can find an answer for the question of whether Arduino is good for industrial projects, learn how to write multitasking applications on Arduino, practise to create your own Arduino library and understand techniques to write realtime software for Arduino.

Eventually, you will be ready to use the Arduino board as a powerful driver. The main controller (Raspberry Pi) will encode and send commands to this driver. It will decode the commands and schedule retrieving information from sensors or prepare instructions for motors. Consider the following set of useful tutorials and libraries that could help you build control systems based on Arduino:

It is also necessary to mention the digital signal processing. When you work with digital and analogue sensors, you will definitely discover the fact that most of these sensors are noisy. In some cases, they could have their own noise suppression mechanism, but sometimes it is necessary to implement your own approach to handle data received from the sensors.

That's why it makes sense to start learning different ways to "de-noise" the sensor readings. So take into consideration the following articles:

Raspberry Pi

One could consider the Raspberry Pi board as a usual computer (PC or laptop) -- a general operating system needs to be installed on the device, it must be equipped with storage to keep files, a keyboard and a display could be connected to special sockets. This allows you to experiment with the board before it is incorporated into a robotics project.

Although Microsoft Windows 10 supports the Raspberry Pi, the common recommendation is to install a Linux-based operating system. Depending on the type of distribution you choose, low power consumption and high performance can be achieved with a proper configuration.

In order to interact with the board, first start with the installation. Then, especially if you have not worked with a Linux operating system before, take a tour what is available on the Raspberry Pi for use on a daily basis, and dig into Linux essentials to learn more about commands that could help you use the system more effectively.

As soon as the first lessons are complete and you have an understanding of how turn on the board, start using it, and gracefully switch off, the next step will be to start programming it. Even if it is possible to program in any language you would like, the most convenient way will be to use Python. This programming language has a low entrance level and has tons of materials on the Internet to begin coding with it. For example, you can try LearnPython.org.

If the first programming concepts are clear, the logical continuation is to understand how the language can be used to interact with electronic components.

And, finally, you would like to start using the Raspberry Pi together with a camera. The Raspberry Pi board supports several options to work with camera. First, as with the general purpose computer, the camera can be plugged into a USB socket. In the python code, you need to get access to a proper device (usually, /dev/video0) and start reading frames from it:

#!/usr/bin/python
import os
import pygame, sys

from pygame.locals import *
import pygame.camera

width = 640
height = 480

#initialise pygame   
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()

#setup window
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.set_caption('Camera')

#take a picture
image = cam.get_image()
cam.stop()

#display the picture
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()

#save picture
pygame.image.save(windowSurfaceObj,'picture.jpg')

The code is taken from the official forum

Another way is to use a specialized Raspberry Pi camera. Since it is connected through a special socket, you can get better results from a performance point of view. On the other hand, before using the camera's port, it needs to be explicitly turned on in the Raspberry PI Software Configuration tool. You can follow this tutorial that helps you learn how the Raspberry Pi camera can be programmed.

And now you could be ready to start building a self-driven car around the Raspberry Pi! Don't rush -- consider an approach to connect to the board by a Secure Shell terminal through WiFi. It will be more convenient to run and stop programs remotely without the necessity of having a keyboard and monitor plugged into a moving car. Another article could be useful for more advanced users -- it sheds light on other aspects of communicating to the Raspberry PI board by using a network connection.

Take a moment to read a short story (with lots of technical details) on how to build a Vision-Controlled Car Using Raspberry Pi From Scratch.

Since Python is a general purpose language and the Raspberry Pi board runs a general purpose operating system, you can implement any method or use any technique to write a control program for your self-driven car. Definitely, the program should not be linear, since the robot must act adequately when driving through the track and avoiding an obstacle. Adequately means in proper time and without preventing the program from reacting to other events. The first step to understand how it can be done is to look at two ways to handle control loops in Python.