HM55B compass with pan servo

Hello

I almost get crazy by this time

Ok I got a HM55B compass. It does work really nice with the Sketch on the Arduino Site.
Now I would like to Control a pan Servo. The compass does give me a number from 0 to 180 and then from -180 to 0.
I changed it with:

``````if (angle < 0){
angle = 360 + angle;
Â }
``````

So I do get numbers from 0-360?
And now my problem. How to program the servo?

I get 40 from the compass. As I turn the compass to the left the number will decrease until it reaches 0 and then it does switch over to 360.
Of course this is going to confuse my servo.
Example:
At the moment I really canâ€™t see the solution. Maybe Iâ€™m too tired. So could you pls help me find a formula to get this right?

PS: My english is not the best sorry for thatâ€¦

Thx
Andy

I understand the problem with "near north" headings. If you try to do some smoothing by averaging 358 degrees and 2 degrees you get "south"!

But, that doesn't matter. Your servo, unless it is non-standard, can not go in a full circle anyway. 0 to 180 are the inputs used in the servo libraries, but unless I remember something wrong, 90 to 120 degrees is all the further most servos turn.

When the compass says 0, set the servo to 0. When the compass says 359, set the servo to 180. That is as far as it can go. So the bottom line is: ServoValue = CompassValue / 2

When you slowly turn through north, the servo will come to the end of its travel, quickly reverse all the way to the other end, then move slowly again to track the course.

When you slowly turn through north, the servo will come to the end of its travel, quickly reverse all the way to the other end, then move slowly again to track the course.

But this is exactly the problem. I don't want the servo to turn back if he gets below 0 degrees. Because I like to use this system for panning my RC-Cam on my Plane through my Radio Transmitter.
You have to imagine I go flying and I stand somewhere where I'm heading to 12 degrees. So if I turn my Head to the left the Servo should not turn to 180 degrees after he reached 0.

For example
So the servo should turn from compass 12 degrees = 90 degrees Servo to compass 192 degrees = 0 degrees Servo to compass 102 degrees = 180 degrees Servo.

Geko

You may be able to do what you want by modifying a servo so that it is using compass data instead of the internal servo pot for position feedback, perhaps replacing the servo pot with a digital potentiometer driven from the arduino. I have no experience with this but a search on google could turn up something useful for you.

Looks to me you are being blinded by the novelty of using a compass to do this. Panning a camera to match head position doesn't require 1/10 of this work & expense. I'd rethink the problem and simplify.

Looks to me you are being blinded by the novelty of using a compass to do this. Panning a camera to match head position doesn't require 1/10 of this work & expense. I'd rethink the problem and simplify.

Do you have another idea? Or does someone have an idea how to pan a cam without a compass?

Andy

I'm not entirely clear with the exact problem you're having, except that you want to control the servo based on the readings from a compass, and you're having issues with the algorithm you're using. The problem seems to stem from the idea that you don't want big swings from the servo when you "shift around the poles", but there's no way, with a servo, to move from 180 to 0 without going back all the way around.

You can stop movement when you reach the limit traveling one direction (i.e., if the differences between values read from the compass indicate that you're moving clockwise, don't rotate past 180, and don't rotate past 0 when moving counter-clockwise.), but you would never be able to move in a full-circle using this method.

The real question is: are you attempting to control the absolute position of the servo, or the direction you want it to move, from the compass readings?

If all you want to do is make the camera move right when you turn clockwise (presuming values go up when you rotate clockwise), then just record the difference between your readings to determine if you're moving clockwise (positive change) or counter-clockwise (negative change). Then, for every two degrees of change in the compass, move the servo a single degree in the appropriate direction until you reach the end of possible movement. (You have to do two compass degrees for one servo degree, as the servo likely maxes out at 180, whereas the compass has 360 degrees.) When you reach the end of movement, you ignore any changes in the compass readings until they reverse direction.

!c