C Programming: Arduinos & Computers
One main component of a working 3400 robot is the maze solving algorithm. In order to design the algorithm, it helps to be able to simulate it on a computer, without a robot present. This makes it significantly easier to isolate and debug problems. There are 2 main ways to do this:
- Write the simulator and algorithm in a language like Python or Matlab. Once it works, port it to C/C++ to run on the Arduino.
- Write the algorithm only once in C/C++, and run it both on a computer, and on the Arduino.
While the first approach may sound easier, it may not be ideal. The first approach requires implementing the algorithm twice: once in a language like Python for the simulation, and once in C for the Arduino. Furthermore, even if the simulation works, the C port of it may contain bugs, which you can only detect on the Arduino, making it far harder to debug! While method 1 may be initially be easier, method 2 will ultimately end up requiring less work.
To use method 2, you need to compile C code for you Arduino and your computer. To do so, you need to separate the computer and Arduino specific parts from the common algorithmic part. The key to doing that is to split the code across multiple files.
Our Depth First Search (DFS) library is a good example of how to do this:
- The Arduino project (in a folder with the same name), is the
robot_move/robot_move.ino
file. The main Arduino project can use the functions declared in thedfs.h
header file by including an#include "dfs.h"
directive. The Arduino IDE will then build an object file for thedfs.c
source file, and link your sketch against it. - The common code is in the
dfs.c
source file. All functions which you want to be accessible from other files must be declared in the header file,dfs.h
. - The simulation code, which uses the functions in the
dfs.c
source file is thesim.c
source file. TheMakefile
builds thedfs.o
object file from thedfs.c
source file, and then compiles and linkssim.c
against it to produce an executable executable calledsim
. You can then easily build the simulation code by invokingmake
in themi4
directory.