Controlling Servos

Thanks for the responses AWOL and lar3ry. I will fix my code and post it most likely later today, if you can please come back to see if its the correct way, thanks alot! I apologize for all my errors i am quite new to the coding aspect of this.

JD150:

  for(pos = 90; pos = 20;) //Set wings to 20

{
    wing1.write(pos);
  }

You use code like this in many places, and they're all wrong. If you want to move the wing1 servo to position 20 then just call:

wing1.write(20);

Incidentally, any time you find yourself including numbers in your variable names you should consider whether they ought to be in an array instead.

Hey guys so i took your advice and greatly simplified my code. I apologize for the late entry but its here nonetheless. Let me know any thoughts or concerns with the code, once again i really appreciate all the help, without you guys i would be no where.

I have gotten ride of the for loops and instead directly commanded the servos where to go.

I also got rid of my single count, i can just pull the power instead it really isn't a big issue and i wasn't sure i knew how to correctly avoid using the void loop.

Furthermore i shortened the functions to their simplest form as requested, allowing me to just write in the number of flaps i want to travel and when to turn/which way. These commands along with the initial + final center command and "glide position" create the vocabulary for writing a preflight path then uploading it and seeing it come to life.

I know improvement is never ending but seeing as my servos have arrived i will run initial test with the code i have so i can determine how long of a delay i need in certain places and hopefully by the end of the weekend it will be flying.

Thanks again!

#include <Servo.h>
//Declare servo objects; left wing 1, right wing 2, rear wing r.
Servo wing1;
Servo wing2;
Servo rwing;
//Attach servos to appropriate pins.
void setup()
{
  wing1.attach(9);
  wing2.attach(10);
  rwing.attach(11);
}

void loop()
{
  centerServo();
  
  delay(30000); //Delay 30 seconds to allow me to get into position.
  //This is were the customizable flight path goes. 
  flapMany(10);
  
  glideWings();
  
  leftTurn();
  
  glideWings();
  
  delay(30000); //Flight finished, glide back to base.
  
  centerServo();
  //Close with centering the servos.
} 

//Centers servos before and after flight.
void centerServo()
{
  int cpos;
  
  for(cpos = 0; cpos < 90; cpos +=1) //Centers all servos at 90
  {
    wing1.write(cpos);
    wing2.write(cpos);
    rwing.write(cpos);
    delay (5000);
  }
}
//Flaps wings X times (determined by flapMany() function).
void flapWings()
{ 
  wing1.write(20); //Set both wings to 20 (aka 140 for flipped servo).
  wing2.write(140);
  delay(time);
  wing1.write(140); //Set both wings to 140 (aka 20 for flipped servo).
  wing2.write(20);
  delay(time);
}
//Flaps wings X amount of times.
void flapMany(int j)
{
  int i;
  for (i = 0; i < j; i++)
  {
    flapWings();
  }
}
//Turns bird left in mid-flight.
void leftTurn()
{
  wing2.write(135);
  rwing.write(45);
  delay(time);
}
//Turns bird right in mid-flight.
void rightTurn()
{
  wing1.write(45);
  rwing.write(135);
  delay(time):
}
//Sets servos for glide position.
void glideWings()
{
  wing1.write(85);
  wing2.write(95);
  rwing.write(90);
  delay(time):
}
void loop()
{
  centerServo();
  
// <snip>
  
  centerServo();
  //Close with centering the servos.
}

What happens when loop() ends? Considering that, is it necessary to call centerServo() immediately after calling centerServo()? One of those calls is useless. I'll leave it to you to determine that the second one is useless.

  wing1.write(20); //Set both wings to 20 (aka 140 for flipped servo).
  wing2.write(140);
  delay(time);

Wrong. You don't get it, do you? You tell a servo to get moving, and the function ends. Long before it has gotten there, you tell it to go somewhere else. Why?

void flapMany(int j)
{
  int i;
  for (i = 0; i < j; i++)
  {
    flapWings();
  }
}

Do you really need to be using ints? Are you planning on flapping the wings more than 255 times? If not, use bytes, instead.

Does that compile? I don't see a definition of time, which is used in many places.

You use the same time variable/constant/whatever to control the duration of turns (which I would expect to be short) and gliding, which I would expect to be much longer. Maybe this is what you want, but it seems to me that you would probably want to use different durations for the different actions in your sequence.

The comment for flapWings() is misleading; it flaps the wings once not X times.

I don't understand what centerServo() is supposed to do. What it actually does is slam all the servos to one end of their travel and then veeery slowly creep them back to the mid position. I can't imagine how that is useful. If you want to move all the servos to a center position as fast as possible, you can just call servo.write() once for each servo to move them all there. If you want to move all servos at a controlled speed to a central position then you need to read the current position of each servo and if it is not already at the center position, move it a little towards the center. After doing this for all servos, delay briefly and then do it again. Keep going until all servos are at the center position.

[quote author=JD150 link=topic=187371.msg1390962#msg1390962 date=1379146626]
void loop()
{
  centerServo();
    delay(30000); //Delay 30 seconds to allow me to get into position.
  //This is were the customizable flight path goes. 
  flapMany(10);
    glideWings();
    leftTurn();
    glideWings();
    delay(30000); //Flight finished, glide back to base.
    centerServo();
  //Close with centering the servos.
}

So far, so good, if all you want to do is to have it fly a while, glide a while, turn left, and glide toward wherever it's going to land (certainly not to base). Of course as soon as it executes the final centerServo position, it will loop through again, and do all the same things centering agai, flapping again, turning again, and so on, possibly without even getting close to the ground. That is, if your called functions were correct, and if there was no wind, and if you haven't taken steps to see that it only executes loop() once.

//Centers servos before and after flight.
void centerServo()
{
  int cpos;
    for(cpos = 0; cpos < 90; cpos +=1) //Centers all servos at 90
  {
    wing1.write(cpos);
    wing2.write(cpos);
    rwing.write(cpos);
    delay (5000);
  }
}

You do like your for loops, don't you. In this case it is not only completely unnecessary, but you may even fall asleep waiting for the wings to get to glide position. You command three servo operations, then wait 5 seconds. You then repeat this 90 times! That's 450 seconds to get the wings to center position!

Try this instead...

    glideWings()

This will set the wings for best glide... much better thn just centering.

//Sets servos for glide position.
void glideWings()
{
  wing1.write(85);
  wing2.write(95);
  rwing.write(90);
  delay(time):
}

[/quote]
You'll need to experiment with all delay times delay to see if they're too little or too much. You'll also want to play with the wing angles to see what the best positions for glide and flap are. A slight dihedral makes for way better glide stability, though perhaps your values here are the right ones.

As for loop(), it's there because that's the way the Arduino compiler is set up. There's a setup() to get things ready for loop(), and loop executes continuously, with no delay between the end of it and the beginning. It's a function that should be (in an application like this), checking for control inputs or for timer events, and dispatching to various control functions. These can be (as you seem to be doing here), fixed flight plans, or flight commanded by control inputs from a radio, etc.

Once you get these things going, you'll want to eliminate the delay() functions and find another way of waiting for the servos to get to position. This way, you'll be able to do other control functions while one or more servos are in transit.

Thank you all for your replies and your help lar3ry.

I will make the coding changes right away. I apologize for my spotty responses i am in school and it must come first, but this is my true interest.

My servos have come and i experimented with them a bit, im afraid to make my bird fly i will need alot of power going to these little servos to get them to move fast enough with the wing load to create enough lift. (i originally ordered these without intent to fly)

Heres the link to check out the servos: Servo - Generic High Torque (Standard Size) - ROB-11965 - SparkFun Electronics

I imagine splitting a 9V between the two would give enough power. It says that it can go about 60 degrees in .18 sec at 4.8V so thats a little over half a second for a full 0-180 flap and im not even using that big of a wing rotation. (180/60 = 3 | 3 x .18 = 0.54sec) Of course these could be to slow because i originally didnt intend to get it flying. Id love some input on if these servos would work. Also to mention im going to change up my birds structure to something more practical and possibly flight capable, im researched more into wing size depending on weight and body length so stayed tuned for that.

Once again i appreciate all the help and hopefully will get a test video of just the electronics up and running this weekend. Any recommendations would be appreciated i am grateful for those who have stuck with me.

American, Japanese, German & Australian scientist & engineers for the past 50 years have tried to design & build a working prototype of a replica bird that can fly. They determined that after an estimated $2billion & countless hours, that is what not possible to mimic nature verbatim to verbatim.
The closest anyone has come, is the ability to glide & land. No one has achieved the ability to make flight.

There is more cartilage, nerves, muscle and bone fragments in a birds wing than there is in the human body.

JB_AU:
American, Japanese, German & Australian scientist & engineers for the past 50 years have tried to design & build a working prototype of a replica bird that can fly. They determined that after an estimated $2billion & countless hours, that is what not possible to mimic nature verbatim to verbatim.
The closest anyone has come, is the ability to glide & land. No one has achieved the ability to make flight.

There is more cartilage, nerves, muscle and bone fragments in a birds wing than there is in the human body.

Making something flap like a bird is entirely feasible.

Making it capable of flying is massively harder and I suggest you abandon any thought that the static flapping model powered by servos will ever actually fly.

@Zoomkat.

That's my point, without manually starting the process & without wind (up & down drafts), there is no flight.
If you look @ the video, the bird rises with an up draft, & decends with a down draft.
When there is no wind, it glides.

I bet that didn't cost $1,000,000 to design. Is always good to see people trying, nothing is impossible :slight_smile: