Pages: [1]   Go Down
Author Topic: How to take 2 sets of coordinates and altitude and calculate angle for Servos  (Read 660 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 127
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5557
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is essentially what you are trying to do:

http://www.euclideanspace.com/maths/algebra/vectors/lookat/index.htm
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 smiley
« Last Edit: November 14, 2012, 02:01:55 am by guix » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Servos aren't controlled using PWM.
They aren't?
Sh*t, I've been doing it wrong these past twenty years.  smiley-cool
Logged

Per Arduino ad Astra

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Per Arduino ad Astra

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Per Arduino ad Astra

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Per Arduino ad Astra

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
 
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25782
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!  smiley-cool 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.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: