Go Down

Topic: How to take 2 sets of coordinates and altitude and calculate angle for Servos (Read 788 times) previous topic - next topic

MatCat

Basically I am wanting a way to calculate the correct PWM to aim a camera on a pan and tilt servo system at a coordinate by calculating based on it's own coordinates.  This does need to take into account altitude as well.  This will be used on my FPV airplane, and I want to have the ability to have the camera stay locked onto a spot on the ground, but I am not quite sure where to start to even make the calculations!  (Assuming both sets of coordinates are known of course).

I appreciate any help anyone can offer!

cr0sh

This is essentially what you are trying to do:

http://www.euclideanspace.com/maths/algebra/vectors/lookat/index.htm
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

guix

Hello, something like that should work (not sure it will work in your application!):

Code: [Select]

void GetAngles3D( float x1, float y1, float z1, float x2, float y2, float z2, float &yaw, float &pitch )
{
 x2 -= x1;
 y2 -= y1;
 z2 -= z1;
 yaw = atan2( y2, x2 ) * 57.29578;
 pitch = atan2( z2, sqrt( (x2 * x2) + (y2 * y2) ) ) * 57.29578;
}


Returned angles in degrees, remove " * 57.29578" for radians... Good luck :)

PaulS

Quote
Basically I am wanting a way to calculate the correct PWM to aim a camera on a pan and tilt servo system

Why? Servos aren't controlled using PWM.

Groove

Quote
Servos aren't controlled using PWM.

They aren't?
Sh*t, I've been doing it wrong these past twenty years.  8)
Per Arduino ad Astra

PaulS

Quote
They aren't?

No, they are controlled using the Servo library. How many of your servos are connected to PWM pins? How many NEED to be?

Groove

Who said anything about PWM pins?
Who said anything about libraries?

Besides, what sort of control signal do you think the Servo library generates?
Let me think...hmm...that's right, it's PWM!
Per Arduino ad Astra

PeterH


Who said anything about PWM pins?
Who said anything about libraries?

Besides, what sort of control signal do you think the Servo library generates?
Let me think...hmm...that's right, it's PWM!



In the context of an Arduino sketch, servos are best controlled via the Servo library and not by generating the PWM yourself. If you aren't talking about Arduino pins and libraries, then what relevance does your answer have to an Arduino sketch?
I only provide help via the forum - please do not contact me for private consultancy.

Groove

The assertion was that servos aren't controlled by PWM.
They are, and the control can come from any output pin, as amply demonstrated by the Servo library.
It wasn't an answer, it was a rebuttal.
Do try to keep up.
Per Arduino ad Astra

PaulS

http://en.wikipedia.org/wiki/Servo_control
Quote
The angle is determined by the duration of a pulse that is applied to the control wire. This is a form of pulse-width modulation, however servo position is not defined by the PWM duty cycle (i.e., ON vs OFF time) but only by the duration of the pulse.


The Servo library generates pulses of the required duration during the specified interval. That has nothing to do with PWM as understood by the Arduino. The analogWrite() function that does PWM on the appropriate pins controls the duty cycle, so that over some undefined period of time, the ratio of on time to off time is the proportion that the user wants. This pin diddling will not control a servo worth a damn.

Groove


Did the OP ask about analogWrite?
No.
Did the OP ask about PWM pins?
No.

The OP asked how to calculate the correct PWM to steer a servo.
Per Arduino ad Astra

zoomkat

Quote
Sh*t, I've been doing it wrong these past twenty years.


Maybe. Often the pulse sequencing to be supplied to servos is refered to as PPM (pulse position modulation?). It is a somewhat specific timing control format for the control chips contained in servos. Generic PWM control can have a control pin anywhere from high all the time to low all the time, and the pulse cycle can be from low frequencies to very high frequencies, which would have to be carefully developed to control a servo. The main issue in the forums is that persons often assume that generic PWM code that controls a simple DC motor will also work to control a servo. The servo library develops the specific pulse timing required for use with the control chips in the servo. The chip in the servo then develops the typical PWM to control the H-bridge/motot in the servo. The below link has good info.

http://www.seattlerobotics.org/encoder/200009/S3003C.html
 
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

AWOL

Quote
Often the pulse sequencing to be supplied to servos is refered to as PPM

No, PPM is the multiplexed stream between TX and Rx in an R/C system, so in a single frame, an individual channel's pulses are identified by their position relative to the sync pulse, which is simply a longer 'low' than the 'low' between channel pulses.
The individual channel pulses are indeed PWM, because their width defines the servo setting.

If you want info on R/C systems, go to an R/C website.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PeterH



Did the OP ask about analogWrite?
No.
Did the OP ask about PWM pins?
No.

The OP asked how to calculate the correct PWM to steer a servo.



And the answer is that you don't need to calculate the PWM, you calculate the required position and let the Servo library deal with generating the PWM. If you're trying to use an Arduino to control a servo and generating PWM output yourself, you're making the job much harder than necessary.

Getting back to the OP's question, I can see two approaches to solve the problem.

Knowing the aircraft position and orientation, it is possible to calculate the relative direction to a specified point on the ground using trig. The calculations are non-trivial.

There is an open source Arducopter project which provides many features including the ability to control a camera gimbal to point the camera at a fixed point. Even though the platform in this case is not a quadrotor, it may be practical to take the tried and tested sensing and control hardware and software and use that as the basis for a camera steering system. (I suspect it would also be a damned good start towards an autonomous autopilot, if there was any interest in that sort of thing.)
I only provide help via the forum - please do not contact me for private consultancy.

zoomkat


Quote
Often the pulse sequencing to be supplied to servos is refered to as PPM

No, PPM is the multiplexed stream between TX and Rx in an R/C system, so in a single frame, an individual channel's pulses are identified by their position relative to the sync pulse, which is simply a longer 'low' than the 'low' between channel pulses.
The individual channel pulses are indeed PWM, because their width defines the servo setting.

If you want info on R/C systems, go to an R/C website.


My bad!  8) The PPM is an RC tx/rx thing. I think the takeaway from my part of the discussion is that 1). never use PPM when talking servos, and, 2). the PWM used for servo control is a particular subset of general PWM control.


R/C website:
Servos are controlled with a 5vdc positive-going variable pulse width that repeats every 20mS. The pulse length determines the servo output shaft position but the 20mS frame rate is not position critical. The frame rate does need to repeat at least every 20mS or it will lose power or even stutter. The servo was designed for pulse widths that vary from 1.0mS to 2.0mS, where 1.5mS is centered. In the normal range the servo will move +/- 45 degrees from the centered (neutral) position.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up