What do you do when you dont like the way a thing works, you change it.I bought ARDrone quadcopter, which is a good piece of technology. It is controlled by an application on iPhone or Arduino. It works by sending AT commands over wifi, though the range of it is not enough but its fine for a RC newbie like me. The controls on the touchscreen is fine but it lacks any kind of tactile feedback. I love to feel the control, not just imagine one. Hence i set upon a task to fix this by creating my own controller.
There are already APIs created for NodeJS, Python etc. to control the ARDrone over the WiFi, hence i didn't have to write the API. I used two 2-axis joystick from Radioshack and a button connected to Arduino. One joystick was used to control the motion of the ARDrone forward and backward, the second for altitude and yaw. The button is used for takeoff and landing. I used Python (I'm a Pythonist so choice was obvious) for the project, except the Arduino part which was in C.
I used Serial communication to get the data from Arduino to Python. The format of the serial communication was CCCXXXX. Here CCC is the control command which tells that which axis it is and which joystick and XXXX is the value of it e.g. in case of left joystick the two commands were LLRXXXX and LUDXXXX. I know this can easily be optimized using bit encoding but i just wanted to use the simplest way as I intended to complete the project over the weekend.
Serial.print("LUD"); Serial.println(LUD,DEC); Serial.print("LLR"); Serial.println(LLR, DEC); In Python I created a publisher subscriber like pattern, as i wanted to process the data for controlling the ARDrone and also display the position of the axis using PyGame. I was unable to install the PyGame on Mountain Lion hence i didn't include the PyGame part yet. I still kept the pattern as it provided seperation of concerns. The data from Arduino was handled by one class, which was also responsible for cleaning the data by position of the axis. So you can just put the axis in any direction and set the direction in config file and you will get the controls working properly without any code change.
class ArdwareController: #Subscribe a subscriber def subscribe(self,subscriber): #Publishes the data to all subscribers def publish_to_all_subscribers(self): #Converts the value according to the #position of the sticks mentioned #in the config file def get_converted_values_according_to_position(self): #The method called by Main in a loop #to get the data and do the whole #processing def pingpin(self):
Then i created the ARDroneHandler class to handle the data from ArdwareController and give the required commands to ARDrone to fly. It normalized the data between values 0.0 to 1.0 as this is what the ARDrone Pyton API took. Based on the direction of increase or decrease the required command for the ARDrone was calculated and the corresponding API was called. You can see the source code on github to see how the whole calling of the ARDrone API was done.
As I love my ARDrone and didn't want to shred it to pieces so i had to make sure that when the controller works, it works without any flaw otherwise i'll lose my lovely quadcopter. I did this by making a Mock of the ARDrone Python API, that just spit out the name of the API call. This allowed me to iron out a lot of errors that i would have missed easily, e.g. my takeoff and land button were continuously switching between others, the Axis value had to be calibrated everytime the program was started. As there can be initially some noisy data when the Arduino is rested so i just waited for 5 secs to get the data and calibrated the initial values of the joystick. So this method of testing is good when you have to work with hardware.
The moment i had done all the testing and it seemed fine to test on the actual thing, i took out my ARDrone and prayed that it didn't blasted on the first try. I started my controller and pushed the takeoff button and Voilà it worked.
P.S: Ill be posting the schematic of the Arduino and the joysticks later so you can easily create your own simillar controller. If you need any help then hit me up at twitter.
Source Code: AniiCopter