# Math help - sine wave servo positioning

#### CWells

##### New Member
I have to move a servo with fluid motion, approximating a sine wave. I want it to move back & forth between its limits, with the fastest motion in the midpoint, and slowest near the ends. The limits will themselves change, but I can probably figure that out myself if someone can give me the basic formula.

This program will be running on a Picaxe 18X.

#### CWells

##### New Member
Ok, I got the math figured out: I just need the cosine of the relative angle. I've got my algorithm; now to figure out how to implement in on the Picaxe...

#### CWells

##### New Member
Ok, I've done a bit more reading, and it seems a lookup table is the best way to go...how do I do this, seeing as there's no DATA command?

#### hippy

##### Senior Member
There is a DATA command but it's a little hidden away as a synonym for the EEPROM Command

You would use EEPROM/DATA in conjunction with READ. The other alternative to use is LOOKUP. That's not limited to byte values but can be a lot slower to execute.

#### Wrenow

##### Senior Member
Chris,

Are you trying to do something which requires continuous motion (as in a constant rotation servo), or just looking to do the amplitude part of the curve with the servo? If you are just trying to do the amplitude, I think you could handle this mechanically as well as in programming, and it might be simpler.

Doing it all mechanically is pretty much a question of where you take your lever out of the servo's arc of motion. To illustrate - in R/C model warship combat, many captains mount their servos offset from the axis of the ship a bit or a lot. They then set the servo to centered, line up the servo arm perpendicular to the centerline, do the same with the rudder, and put in the appropriate length connect them. Everything is perfectly square, so the rudder should work just right. Nope, it has much more throw in one direction than the other. ???????? The trick is to line the servo and rudder arms up perpendicular to the line drawn between the servo spline and the rudder post.

Another example - Knowing about this, and a little noodling, and you can use this bug as your feature. One of our guys had an Italian ship with 3 rudders. The two forwardmost rudders could not turn very far toward the centerline of the ship (inboard) before they hit a propellor shaft, but could turn freely outboard. The solution did not involve separate programmed servos or springs, but just a nice (single) plate, mounted on the servo arm, that used the same effect to change the rate of movement at different points on the different rudders. Works smoothly and seamlessly, too. I understand it took some trial and error to get the angles right.

Cheers,

Wreno

#### ylp88

##### Senior Member
hmmm... I'm not entirely sure about how you are applying the motion,so excuse me if what I'm about to say makes no sense or is not relavant to you:

Are you aware that when you have circular motion, you essentially do have a (co)sine wave present? That is, if you pick a point on a rotating circular disc (such as on a servo horn) and make the spindle turn at constant speed, then considering that one point in a single axis, that point has a path equal to that of a sine wave, with respect to time.

Okay, my explaination is not good, so I've found an animation of it and hopefully you can see what I'm trying to get at: http://cda.morris.umn.edu/~mcquarrb/Precalculus/Animations/SineCosineAnim.html

As I said, hopefully this is actually relevant to your application - I'm not entirely sure what you are trying to do and therefor whether this principle is applicable.

ylp88

#### CWells

##### New Member
I guess this isn't as secret as most of the stuff I work on, so I can shed a little light:

I'm building an ArtiFish. If the name isn't descriptive enough for you to figure out what it is, go back to watching Fox News, and don't come back to this website.

The algorithm is for the tail. There are two servos, one starting at a different point. I have to do other things as well as tailwaving, so I have to break it down. I've got it pretty much figured out by now; I'm doing the math manually and putting the values into EEPROM. The biggest challenge will probably be making it waterproof.

I've chosen the 18X due to its width, or lack thereof. I don't have a lot of room; it's a tiny fish.