Sine wave to servo motor

Hi!

For my senior design project for UVM, we are using an arduino uno board to control a servo motor that hopefully will be programmed to a modified sine wave. I'm new at using the arduino software, so can someone please help me get started on how I would begin to write my code?

Thanks!

Reading material.

https://www.google.com/search?as_q=servo+sine&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Fforum.arduino.cc%2Findex&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=

uvmlung:
to control a servo motor that hopefully will be programmed to a modified sine wave.

What does this mean?

Are you talking about a hobby servo as used in model aircraft?

Or are you talking about the sort of servo that is used in CNC machines as an alternative to stepper motors?

What do you mean by "programmed to a modified sine wave"?

...R

For our senior design project we are using a servo motor to control a very small hinged door that restricts air flow. We need the door to move in a specific pattern (a modified sine wave). So we're hoping to program our servo motor to rotate according to this modified sine wave, but we're a little stumped on where to start.

You didn't say if you are planning to use a hobby servo but, since you are moving a door, I presume you are.

Could you store the pattern of movement as a series of angles in an array and then iterate through the array something like this?

myservo.write(array[n]);

...R

Yes, we will be using a hobby servo to control our door.

So after converting the modified sine wave into an array, do I just use it as an input?

Such as:
Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup()
{
myservo.write(array[n]); // attaches the servo on pin 9 to the servo object
}

If you can describe the path that you want the servo to follow, then we can suggest sensible ways to implement that.

In the absence of any other details, the most flexible approach would be to write a function that calculates the required position of the servo at the time it was called, and just write some code to call that repeatedly and send the servo to the corresponding position. For example if you wanted to use an ordinary sine wave then the code would:

read the current time
multiply the current time by the angular velocity to calculate the current angle
calculate the sin of the angle
map the sin value to a servo position
return the servo position

I had assumed, as this is a senior project, that you have some programming experience and would know how to use an array.

You need something like the following (which I haven't tested). Obviously you will have a bigger array and will have proper angles to give sinusoidal movement.

This code assumes that this is all the Arduino will be doing - but it illustrates my idea for getting the servo to follow a sinusoidal path.

#include <Servo.h>

Servo myservo;

byte servoPin = 2;

byte sinAngles[6] = {0, 60, 120, 180, 120, 60}; // this is not sinusoidal

void setup(){
   myservo.attach(servoPinP;
}

void loop() {
    for (byte n = 0, n < 6; n++) {
       myservo.write(sinAngles[n]);
       delay(100); // milliseconds - adjust to get desired speed
   }
}

Personally, I would go to a great deal of trouble to avoid the complication of calculating sine values on the Arduino. That stuff is much easier on a PC.

...R

Robin,

So if I convert my modified sine wave into a position array (of 1000 different points through 1 period) and save that, can I have the array be called out within my script?

I have an attached a copy of what our modified sine wave looks like using matlab.

Thanks!

waveform pic.jpg

Robin2:
Personally, I would go to a great deal of trouble to avoid the complication of calculating sine values on the Arduino.

Why?

http://arduino.cc/en/Reference/Sin

I confess I hadn't been thinking of 1000 points. Assuming each point takes a byte that would take up a lot of the limited Arduino RAM, though you could probably use PROGMEM (I think that's what its called).

can I have the array be called out within my script

I'm not sure why you are asking this question -isn't that what my sample code does?

I can't imagine why a door would need to move in such a complex pattern - but that's your business.

...R