Where to save new code for APM 2.6 and APM code syntax?

NMAIST

Beginner.
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();
 
Sorry it took me so long to reply.

This is all pure observation from looking at the code for like 5 mins (I'm at work at the moment). The ArduPilot code is very clean and unless you know exactly what you're doing I would use the provided UserCode.cpp and UserVariables.h and make sure to uncomment the corresponding defines in the APM_Config.h.

I would use the userhook_FastLoop function due to the fact of you needing realtime response with the sensors.

As for the syntax error, I don't know much about the Ardupilot and configuring the GPIO pins and would suggest just keep googling it till you find some source code.

I hope you get your quad up and going, God know's I about killed myself over getting an Arduino based quad to work. Now I use a KK board haha.

On that note, I will leave you with a short clip of my Arduino quad way back when I was still trying to program a flight controller from scratch. Enjoy :P

 
Back
Top