This is a tutorial on how to edit the Ardupilot source code to support PWM RSSI input. Keep in mind that this will take up one of the PWM input channels of the ardupilot, (Also works with a PPM channel if your Receiver supports it [OpenLRS for the win!])
NOTE: THIS WILL ONLY WORK WITH SERVO-TYPE PWM, most RSSI PWM output is not designed for servos, do your research!
Here are a few things you need to download first:
- Arduino Environment for APM: https://code.google.com/p/ardupilot-mega/downloads/list/ (download the ArduPilot-Arduino-1.0.3-Mac.zip [or windows for windows pc's]) This is a special version of the arduino software that allows you to upload the code to the ardupilot. This tutorial may work with the regular version of the Arduino Environment but I have not tested it.
- Ardupilot source code: https://code.google.com/p/ardupilot-mega/downloads/list/ (download the ArduPlane-2.74b.zip) The source code of the Ardupilot.
A few quick definitions:
Arduino environment: The window that pops up and says "Arduino", also has the Arduino logo on your task bar.
- You have basic knowledge of the Mission planner software
- You know how to install a program
- You know how to copy/create files/folders
- You know how to extract .zip files
Step 1: Setup the APM Library. (If you are on mac or PC this step is the same.)
First navigate to your "Documents" or "My Documents" folder. If there is a folder called Arduino look inside of it. If there is no "Arduino" folder create a new folder called "Arduino".
Unzip the Ardupilot source code, inside you should find a folder called "Library" drag this to the "Arduino" folder inside the documents folder. If there is already a "Library" folder do not erase it's contents, just merge the two and keep everything.
Step 2: Open Arduino
Extract the "Ardupilot-Arduino-1.x.x" File. And open up the Arduino environment.
Open the ardupilot source code. It should be back in the "ardupilot-master" folder. Find your chosen source (either arducopter, arduplane or APMrover).
To do this go to the Arduino environment and hit File>Open, browse to the right file. (make sure to open the .pde file that is named after the type of craft. For example for airplanes open the /ardupilot-master/ArduPlane/ArduPlane.pde file
Step 3: Edit the code.
From the Arduino environment find the "sensors.pde" file. If you can't see it in the tabs hit the down arrow on the top right hand side of the Arduino Environment. This will give you a drop down menu of other files in the project.
Change the "void read_receiver_rssi(void)" function from:
float ret = rssi_analog_source->voltage_average() * 50;
receiver_rssi = constrain_int16(ret, 0, 255);
receiver_rssi = constrain_int16((g.rc_4.radio_in-900)/4.3, 0, 255);
This will set the receiver_rssi based on the PWM from channel 4 (rudder). In my case I am flying a wing so I used the rudder channel. But you can change the number in "rc_4" to the channel you would like to monitor. (1-8, Be warned channels 1-4 are usually used for aileron, elevator, throttle, and rudder) NOTE: make sure that your RSSI channel does not do anything else. (Browse through the basic/advanced settings in the Mission Planner to check)
Keep in mind depending on the Calibration of your PWM output some information may be lost. In my case the PWM went up to 1998. Therefore we have (1998-900)/4.3 = 255.34. The "constrain_int16" limits this value to 255. so you may loose a bit of resolution You can find out what divisor you should use (in my case it was 4.3) like this:
divisor = (maxPWM-900)/255
If you wanted to get even more accurate you could change "900" to the minimum PWM value outputted by the RX. (In my case this value was 900)
That way you will get the most resolution possible. If you don't know how to find the maxPWM or minPWM don't worry. 4.3 is what most systems will need.
Step 4: Upload the new Firmware to the APM
Step 4.A: Select your board and serial port from the Arduino Environment.
Beside the "File" and "Edit" buttons you should see a "Tools" Section.
Inside of "Tools":
Change the "Board" to "Arduino Mega 2560 or Mega ADK"
Change the "Serial port" to the serial port of the APM
On MAC this will be something like "/dev/cu.usbmodemfaXXX" where XXX are numbers.
On PC it will be something like "COMXX" Where XX are numbers (check the device manager to verify the com port)
Beside "Tools" you should see "ArduPilot"
Inside of ArduPilot>HAL Board> select your board (APM 1 or APM 2.X)
Step 4.B: Backup your APM settings
For me the settings all stayed when I flashed the chip, but I also backed them up first. To do this first open Mission Planner and connect to the APM. Navigate to the "Full Parameter List" (it is under the "Config/Tuning" tab) On the right there are a bunch of green buttons, hit save and name the file accordingly.
Step 4.C: Upload the firmware NOTE: you will have to disconnect the APM from mission planner first.
In the Arduino Environment click File>Upload to begin the upload. NOTE: It may take a while to compile and upload, this is NORMAL.
If it worked you should see the "rxrssi" value in the "status" tab of the "flight data" page change when you turn on/off your radio.
If it DID NOT WORK and you want to revert to the original APM code, simply open Mission planner, go to the initial setup page, and reinstall the firmware from there.
Hopefully this helped, If you have any questions feel free to ask.