My team and I have built a quadcopter with the APM 2.6 and we wanted to integrate additional sensors (i.e. sonar sensors for avoiding obstacles, color sensors, etc). We followed the instructions on the developer.ardupilot.com website, downloaded the ardupilot repository from github, and wrote some code for the analog inputs and GPIO pins, but we weren't sure where exactly to save our code within the ardupilot folders. In arducopter, I saw that there's a file for usercode but was wondering if there was another location I should save custom code? We want to ensure our custom code integrates seamlessly with the rest of the firmware.
Also we had a few code syntax questions. How does the pinMode function work for GPIO pins on APM 2.6? Is it not like the standard arduino pinMode(pin, OUTPUT/INPUT)? In the header files it looked like you had to put a uint8_t value for output and I was unsure what value to put there...
Any advice or insight would be greatly appreciated - we are new to APM and ardupilot but have experience with Arduinos, electronics, C, and java. Thanks a lot in advance!
PS: We just wanted to verify if the following example code was syntax-wise ok...does this look fine?
//libraries//
#include (most of the libraries)
//defining pins - A10 for servo (indexing starts at 0) and A3 for sensor
uint8_t SERVO_PIN = 9;
uint8_t SENSOR_PIN = 2;
//hardware abstraction layer - setting board as APM2
const AP_HAL::HAL& hal = AP_HAL_AVR_APM2;
//setup loop
void setup() {
hal.console->println("testing");
hal.rcout->enable_ch(SERVO_PIN);
hal.analogin->set_pin(SENSOR_PIN);
}
void loop() {
//running a servo for a bit and reading in a sensor
hal.rcout->write(SERVO_PIN, 1500);
long sensor_value = hal.analogin-> read_average(SENSOR_PIN);
hal.console->println(sensor_value);
Serial.println(sensor_value);
}
AP_HAL_MAIN();
Also we had a few code syntax questions. How does the pinMode function work for GPIO pins on APM 2.6? Is it not like the standard arduino pinMode(pin, OUTPUT/INPUT)? In the header files it looked like you had to put a uint8_t value for output and I was unsure what value to put there...
Any advice or insight would be greatly appreciated - we are new to APM and ardupilot but have experience with Arduinos, electronics, C, and java. Thanks a lot in advance!
PS: We just wanted to verify if the following example code was syntax-wise ok...does this look fine?
//libraries//
#include (most of the libraries)
//defining pins - A10 for servo (indexing starts at 0) and A3 for sensor
uint8_t SERVO_PIN = 9;
uint8_t SENSOR_PIN = 2;
//hardware abstraction layer - setting board as APM2
const AP_HAL::HAL& hal = AP_HAL_AVR_APM2;
//setup loop
void setup() {
hal.console->println("testing");
hal.rcout->enable_ch(SERVO_PIN);
hal.analogin->set_pin(SENSOR_PIN);
}
void loop() {
//running a servo for a bit and reading in a sensor
hal.rcout->write(SERVO_PIN, 1500);
long sensor_value = hal.analogin-> read_average(SENSOR_PIN);
hal.console->println(sensor_value);
Serial.println(sensor_value);
}
AP_HAL_MAIN();