(2) Learn Ardupilot source code--shetch Example
my own Tip : In Understanding the Ardupilot the basic information and architecture of the source code, following the beginning of the actual combat, the following began to learn the first example.
Your first step in exploring the code yourself is to use the libraries routines. According to the Arduino tradition, most of our libraies will have examples. The example ("sketch") is the main program written as a CPP file.
Understanding the API libraries and protocols used by Ardupilot is a prerequisite for understanding the code, so using an example is a good way to get started. At first you should read, compile, and run an example of the following libraries:
· libraries/ap_gps/examples/gps_auto_test
· libraries/ap_inertialsensor/examples/ins_generic
· libraries/ap_compass/examples/ap_compass_test
· libraries/ap_baro/examples/baro_generic
· libraries/ap_ahrs/examples/ahrs_test
For example, the following command compiles and installs the Ap_gps sample onto Pixhawk :
CDL Ibraries/ap_gps/examples/gps_auto_testmake Px4-cleanmake Px4-v2make px4-v2-upload
my own Tip (experience of your own practice): The first time you run a second command
Make Px4-clean
The following error may occur (I ubuntu14.04 The following error occurred on the run), prompting for gawk
This is due to the absence of a virtual package that performs the following installation commands in the case of networking
sudo apt-get install gawk
Install well gawk after that, continue to execute
Make Px4-clean
Can be compiled normally, as shown, will show the compilation progress, compilation may take a long time
Compile to the following may prompt some errors and failures, you can do without the tube, proceed to the next command
Make Px4-v2
The next is a long wait, it really takes a long time.
After a long wait, finally found that there are some error hints, how to solve this? Do these error hints affect the subsequent operations? To the current study so far perhaps we have not been able to solve, we first with such a problem to continue to learn, perhaps the accumulation of more things later will be vista.
Computer Connection Good flight control Pixhawk , continue to execute the command:
Make Px4-v2-upload
finally found is a bunch of errors, very normal, compile always encountered such a mistake, according to the prompt to try to solve, if not solve, continue to learn ways to solve .
Once you upload the sample, you can view the output from the connection console. What console is used is determined by the hardware board. The PX4 boards (such as PX4V1 and Pixhawk) are USB connectors.
For example, if you have a mavproxy agent installed, you can do this and connect Pixhawk on Linux:
Mavproxy.py--setup--master/dev/serial/by-id/usb-3d_robotics_px4_fmu_v2.x_0-if00
Use the--setup option to bring the Mavproxy into the original serial mode instead of performing the Mavlink mode. This is what you need to do in the example.
Understanding Sample Code
When you read the sample code (such as the gps_auto_test code ) You will notice something strange:
L It declares a ' HAL ' variable as a reference
L These codes are rough, no praise.
The setup () function and the loop () function
Hal Reference
Each file that uses the Ap_hal feature needs to declare a HAL reference. This gives access to the Ap_hal, Ap_hal provides access to all hardware-specific functions, including printing messages to the console, hibernation, and using the I²C and SPI buses.
The real HAL variable is hidden in a particular ap_hal_xxx Library, and references in each file simply provide a convenient way to get HAL .
The most commonly used Hal The functions are:
· hal.console->printf () and hal.console->printf_p () to print strings (with the _p to use less memory on AVR)
· Hal.scheduler->millis () and Hal.scheduler->micros () to get the time since boot
· Hal.scheduler->delay () andhal.scheduler->delay_microseconds ( ) to sleep for a short time
· Hal.gpio->pinmode (), Hal.gpio->read () Andhal.gpio->write () for accessing Gpio pins
· i²c access via HAL.I2C
· SPI access via Hal.spi
Go ahead and look at the Libraries/ap_hal directory to see a list of all the functions available on the HAL. As shown
Setup () and loop () functions
You may find that each example has the setup () function and the loop () function. The setup function is called when the hardware board starts, and the actual call is from the HAL of each board, so the main () function is hidden in the HAL, and the main () function calls Setup () after the hardware board is fully booted.
The setup () function is only called once, it initializes the libraries, and at startup It may also promise a "hello" to display.
After the setup () run is complete, the loop () function is then called (called by the Ap_hal Master code) and the main work of the example is usually done in the loop () function.
Note that on a complex hardware board, the setup ()/loop () function is just the tip of the iceberg. This makes the ardupilot look like a single thread, but there are actually a lot of threads running at the bottom, hardware boards (such as PX4 and Linux-based boards) that actually have a lot of real-time threads starting up, see what's behind--understanding the Ardupilot thread.
Ap_hal_main () macro
You will notice that there is an extra line of code behind each example:
Ap_hal_main ();
This is the HAL macro, which will be based on the hardware board to initialize the HAL code, generate some necessary code to declare the C++main () function, you can hardly worry about how it works, if you are curious about it, you may view the ap_hal_xxx directory in each HAL 's about #define content, which is usually in ap_hal_xxx_main.h. .
Rough Sample Code
you will notice that the code for the sample is rather coarse and lacks comments. This is also your chance to contribute to the code! But once you've read these sample code and know how they work, you can add comments to the code, explain the APIs , and then submit a push so that others will benefit from your learning.
(2) Learn Ardupilot source code--shetch Example