AP_HAL:examples:AnalogIn: Added comments in the AnalogIn example

This commit is contained in:
Arsh 2021-01-05 13:21:08 +05:30 committed by Peter Barker
parent 3a075850f6
commit 3d5addeee0
1 changed files with 35 additions and 11 deletions

View File

@ -1,30 +1,54 @@
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Analogin.cpp : We loop through the 16 pins and output the analog voltage of the pins
*/
#include <AP_HAL/AP_HAL.h>
void setup();
void loop();
void setup(); //declaration of the setup() function
void loop(); //declaration of the loop() function
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
const AP_HAL::HAL& hal = AP_HAL::get_HAL(); //create a reference to AP_HAL::HAL object to get access to hardware specific functions. For more info see <https://ardupilot.org/dev/docs/learning-ardupilot-the-example-sketches.html/>
AP_HAL::AnalogSource* chan;
AP_HAL::AnalogSource* chan; //delare a pointer to AnalogSource object. AnalogSource class can be found in : AP_HAL->AnalogIn.h
// the setup function runs once when the board powers up
void setup(void) {
hal.console->printf("Starting AP_HAL::AnalogIn test\r\n");
chan = hal.analogin->channel(0);
hal.console->printf("Starting AP_HAL::AnalogIn test\r\n"); //print a starting message
chan = hal.analogin->channel(0); //initialization of chan variable. AnalogIn class can be found in : AP_HAL->AnalogIn.h
}
static int8_t pin;
static int8_t pin; //8 bit integer to hold the pin number.Pin number range is [0,15]
void loop(void)
{
float v = chan->voltage_average();
//the loop function runs over and over again forever
void loop(void) {
//get the average voltage reading
float v = chan->voltage_average(); //note:the voltage value is divided into 1024 segments
//start a new line after going through the 16 pins
if (pin == 0) {
hal.console->printf("\n");
}
//print the voltage value(3 decimal places) alongside the pin number
hal.console->printf("[%u %.3f] ",
(unsigned)pin, (double)v);
//increment the pin number
pin = (pin+1) % 16;
//set pin corresponding to the new pin value
chan->set_pin(pin);
//give a delay of 100ms
hal.scheduler->delay(100);
}
AP_HAL_MAIN();
AP_HAL_MAIN(); //HAL Macro that declares the main function. For more info see <https://ardupilot.org/dev/docs/learning-ardupilot-the-example-sketches.html/>