Easy camera roll stabilizer to APM


This is easy camera roll stabilizer to ArduPilotMega (I used code version 2.012). Camera mount was easy to build. Camera angle can be adjusted during flight with RC controller. Stabilization needs only one line code to program! Of course also pitch can be stabilized in the same time, but I have not yet tested this.

Camera stabilizer is normal mount with servo. Servo is connected to APM's sixth output port. (I have old 1.0 version of board, name is Out5, in new version 1.4 port name is probably Out6)

Optionally some free radio receiver channel can be connected to 6th input port(In my board name is In5, in new board probably In6)

Then I added one line to APM code: In the main program there is fast_loop. To the end of this subroutine, just above }, I added this line:


APM_RC.OutputCh(CH_6, constrain(g.rc_6.radio_in - (dcm.roll_sensor * 0.1),900,2100));


Then I uploaded program to APM, and thats it.


Also pitch can be stabilized in the same way. Servo and receiver goes to next channels, and this code can be added after the code line above.


APM_RC.OutputCh(CH_7, constrain(g.rc_7.radio_in - (dcm.pitch_sensor * 0.1),900,2100));


Some explanations to codes: It is easiest to think, that servo middle position is 150 degrees, in the program this is indicated as 1500. This is default value if receiver is not connected, and we get this value from radio receiver if knob is in the middle position.

Servo maximum turn is 60 degrees to each position, so we have minimum value 1500 - 600 = 900, and maximum value 1500 + 600 = 2100. These numbers are in the code above. If we want to reduce movement, then we must change these values. For example if camera can turn only 20 degrees to each direction, the values are 1300 and 1700.


Stabilization works always, also on manual mode. Camera can turn even 60 degrees to all directions. Probably analog servos are better, digital servos jitters very often.

In the forum there are some drawings from some other camera mounts http://www.diydrones.com/forum/topics/apmimu-camera-stabilization

E-mail me when people leave their comments –

You need to be a member of diydrones to add comments!

Join diydrones


  • Developer

    Good work and well done, your camera stabilisation is really efficient.

    Regards, Jean-Louis

  • Ok so I've tried it on channel 8 and it works better.  No more jitters on the cam roll servo.  However, before flying my plane, I noticed that the elevator was now twitching and the aileron throw was way more pronounced than usual when in stabilization mode.  I flew anyways.  Took off in manual and switched to stabilize;  the plane was just wiggling all over the place.  I then switched to  Auto and the plane promptly rolled 180 degrees before diving straight down.  I was able to recover and land safely. 

    I'm not sure what is going on, but I'll give the old 2.012 version a try to see if any my own code is affecting this.

  • I do have the pitch on channel 7 which works fine, but I haven't tried moving the roll to channel 8.  I was trying it on the lower channels.

    I'll try it out and will let you know the results.  Thanks for the feedback.

  • Code in this APM 2.012 version worked fine. I have still this in one plane.

    I build a new plane, and there I have APM version 2.23. In this plane servos jitter. I moved servos to channels 7 and 8, and now it works fine!

    I have no idea what could cause problem. I tested several different servos, analog and digital, ferrite rings in cables, different receiver, removed telemetry XBee, etc, but nothing helps. The system seems to work fine, but at few seconds interval there was strange rotations.

    The only way to get it work was to put servos to channels 7 and 8. There were differences between servo brands and models: some servos make very smooth rotation, some move in big steps.


    Someone who knows code: What changes has been made between these versions 2.012 and 2.23, that can cause this problem?


  • Hello Alpo,


    I've implemented your code and noticed that regardless of the roll channel selected I get major jitters.  I've also tried different servos and got the same results.  The pitch channel works fine.  Would you have any ideas or suggestions as to what is causing this issue? 



  • Hello Guys, I need a help. I am no programmer , so for me hard to find the place in programm to place this row: APM_RC.OutputCh(CH_6, constrain(g.rc_6.radio_in - (dcm.roll_sensor * 0.1),900,2100)); Can you send screen shot , so it will be easy to me to find right place.Also, Alpo, can you confirm me as friend ,  I can contact to you direct, Thank you
  • I am testing out the use of APM for stabilizing a geared rather than direct servo drive camera gimbal, see photo:3692216385?profile=original

    Their seem to be two recent options for stabilizing this (I do not plan any geo tracking), Alpo's very simple approach here, and Ritchie's integrated approach here: http://diydrones.com/profiles/blog/show?id=705844%3ABlogPost%3A372547

     I have implemented Alpo's approach quick and easy and so far it works great, but I have not yet tuned for optimum performance,  but want to get some advice before proceeding too far.

    One axis has a smaller digital servo (hitec HS-7775MG) and the other axis has a larger analog servo (Hitec HS-985MG), and both use some gearing. The smaller digital servo has lots of jitter, the larger analog servo has very little jitter.  Krzysztof recommended non-chinese digital brushless servos for higher loads (which is what I need), but someone else (not sure where) cautioned about using digital servos due to jitter. I'm hoping someone who has done this or read more about it can give me some first hand advice about servo suggestions? (futaba brushless servos? magnetic encoder? digital vs. analog? )  

    Also, has anyone explored stepper motors with APM?

    Any advice or suggestions for this project are appreciated.

  • I think that all the code is in the Navigate part at the very end.

    I think that the only way to excatly know if the camera is pointing in the right direction, might be to add the HMC5843 - Triple Axis Magnetometer Rev 1.2. they you would know excatly which direction the camera is pointing and make the needed corrections.


    long get_distance(struct Location *loc1, struct Location *loc2)
     if(loc1->lat == 0 || loc1->lng == 0)
      return -1;
     if(loc2->lat == 0 || loc2->lng == 0)
      return -1;
     float dlat   = (float)(loc2->lat - loc1->lat);
     float dlong  = ((float)(loc2->lng - loc1->lng)) * scaleLongDown;
     return sqrt(sq(dlat) + sq(dlong)) * .01113195;

    long get_alt_distance(struct Location *loc1, struct Location *loc2)
     return abs(loc1->alt - loc2->alt);

    long get_bearing(struct Location *loc1, struct Location *loc2)
     long off_x = loc2->lng - loc1->lng;
     long off_y = (loc2->lat - loc1->lat) * scaleLongUp;
     long bearing = 9000 + atan2(-off_y, off_x) * 5729.57795;
     if (bearing < 0) bearing += 36000;
     return bearing;


  • Pan/Tilt camera targeting needs only two more code lines!

    If I have to build pan/tilt camera, I would first build a platform that is always in level. Then the camera picture is not tilted. I have seen some infrared or thermal pictures which are tilted for example 45 degrees, and it is very difficult to say what is in pictures. For end user (neighbor, friend, customer, fireman...) it is even more difficult.

    This kind of platform is easy to build, codes are above.

    Then pan/tilt. Here is the challenge for someone who knows APM code:

    Pan or camera direction: In the APM code there is probably a function that can calculate, how many degrees plane must turn to get to the next waypoint coordinates. Probably the same subroutine can calculate angle to camera target coordinates. If there is not that function, then there is a function that calculates in which direction is the next waypoint. The same function can probably calculate, in which direction is the camera target. Then we know plane direction, and can again calculate in which direction is camera target. So to calculate camera direction needs only one line code.

    Then tilt. In APM code there must be a function that calculates distance to target. Probably this same function can calculate distance to camera target. Then we know plane altitude and target altitude, and can calculate tilt angle. This needs again only one line code.

    So we know pan and tilt angles. How to send these commands to camera? I don't know. Tilt can be set with normal servos. But how to rotate camera for example to direction 350 degrees, and then turn to 5 degrees without going through south? And when we rotate camera, do the wires go curly? Probably these are solved in commercial cameras. Who knows these?

    PS. This stabilization system was only a challenge. Someone told, that is is not possible to use autopilot sensors to properly stabilize camera. It took only few hours to prove that he was wrong :-)
  • Seems very effective, simple...but brilliant...well done! 
This reply was deleted.