Problem in controlling a servo(Solved)

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
aaarthur
Posts: 19
Joined: Sat May 04, 2013 12:05 pm

Problem in controlling a servo(Solved)

Post by aaarthur »

Hi,
I am a beginner in MultiWii writing. After my quad be able to fly, I want to add some function(not controlling a camera) during its ANGLE_MODE by controlling a servo on it through pin A2.
I choose to control it by the following code:

Code: Select all

#include <Servo.h> 
  Servo SwitchServo;

void setup() {
  ........
  SwitchServo.attach(A2);
}

void  loop() {
  ......
    if (f.ANGLE_MODE)
    SwitchServo.write(90);
  else
    SwitchServo.write(30);
}


The problem is that after using this code, 2 motors of the quad seems to lose its power and constantly make beep sound, while the servo rotates and sqeaks all along.
When I delete those codes, it all become to normal state.

I also tried another way but it doesn't work at all. The motors are all right but the servo doesn't move a bit.

Code: Select all

void setup() {
  ........
  pinMode(A2, OUTPUT);
}

void  loop() {
  ......
    if (f.ANGLE_MODE)
      analogWrite(A2, 127);
   else
      analogWrite(A2, 42);
}


While I tested the pin A2 to make it sweep by the following code and it works well.

Code: Select all

#include <Servo.h> 
Servo myservo; 
int pos = 0;    // variable to store the servo position

void setup()
{
  myservo.attach(A2);  // attaches the servo on pin A2 to the servo object
}
 
void loop()
{
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                 
    myservo.write(pos);             
    delay(15);                       
  }
  delay(1000);
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);             
    delay(15);                       
  }
  delay(1000);
}


I do not quite understand the reason so could you guys give any advise or point out my fault in this?
Thanks!
Last edited by aaarthur on Mon Mar 03, 2014 3:04 pm, edited 1 time in total.

Geebles
Posts: 24
Joined: Sat Jan 26, 2013 2:23 am

Re: Problem in controlling a servo

Post by Geebles »

You can't use the Servo.h library! That library uses a 'timer' which multiwii uses for the motors, so what youve done there is ruin the motor control timer by trying to control 1 servo. Because multiwii is meant to have many inputs/outputs they have designed the software to take that into account, so you must use however all other servos in multiwii are controlled!!

Remove ALL your code and go through the multiwii code (look for the GIMBAL) code as that will probably do similar to what you're trying to do, and work out how to write to a servo in multiwii WITHOUT using a library.

aaarthur
Posts: 19
Joined: Sat May 04, 2013 12:05 pm

Re: Problem in controlling a servo

Post by aaarthur »

Geebles wrote:You can't use the Servo.h library! That library uses a 'timer' which multiwii uses for the motors, so what youve done there is ruin the motor control timer by trying to control 1 servo. Because multiwii is meant to have many inputs/outputs they have designed the software to take that into account, so you must use however all other servos in multiwii are controlled!!

Remove ALL your code and go through the multiwii code (look for the GIMBAL) code as that will probably do similar to what you're trying to do, and work out how to write to a servo in multiwii WITHOUT using a library.


Thanks for your advise, though it's really heart breaking since it's the worst situation I've imagined.
I may try some ways to make use of the GIMBAL code then.
Thx!!!

timecop
Posts: 1880
Joined: Fri Sep 02, 2011 4:48 pm

Re: Problem in controlling a servo

Post by timecop »

Or you can upgrade to baseflight and forget about dicking with 8bit pwm timers...

User avatar
alll
Posts: 220
Joined: Fri Dec 07, 2012 9:53 am

Re: Problem in controlling a servo

Post by alll »

If you want to promote baseflight, give him some tutorials / advice / links how "easy" it is to do what he is asking...

timecop
Posts: 1880
Joined: Fri Sep 02, 2011 4:48 pm

Re: Problem in controlling a servo

Post by timecop »

How easy is it to write a value from 1000..2000 into servo[x] variable???
and there's like 4 free servos in PPM input mode... Knock yourself out.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Problem in controlling a servo

Post by copterrichie »

timecop wrote:How easy is it to write a value from 1000..2000 into servo[x] variable???
and there's like 4 free servos in PPM input mode... Knock yourself out.


A layperson does not know this but I digress.

aaarthur
Posts: 19
Joined: Sat May 04, 2013 12:05 pm

Re: Problem in controlling a servo

Post by aaarthur »

copterrichie wrote:
timecop wrote:How easy is it to write a value from 1000..2000 into servo[x] variable???
and there's like 4 free servos in PPM input mode... Knock yourself out.


A layperson does not know this but I digress.

You're quite right. I indeed don't know what the word means. But thanks to all you guys for the key words mentioned, I finally figure it out and make my servo move.
For any other layperson who has the same problem or need you can(if you previously didn't use servo in your program):

Code: Select all

#define SERVO
#define PRI_SERVO_FROM   1
#define PRI_SERVO_TO     1 
//These two lines define how many servos you want to use and in my board the pin is from A0 to A2, and here I use A0;
//After this you can control your servo by servo[servo_number]
servo[0] = 1020; //it switch the servo at pin A0 to zero degrees or use 2000 to switch it to 180 degrees.


It was all so tough for beginners, so we really hope everyone who's available can teach us more and we are always willing to learn.

aaarthur
Posts: 19
Joined: Sat May 04, 2013 12:05 pm

Re: Problem in controlling a servo

Post by aaarthur »

copterrichie wrote:
timecop wrote:How easy is it to write a value from 1000..2000 into servo[x] variable???
and there's like 4 free servos in PPM input mode... Knock yourself out.


A layperson does not know this but I digress.

You're quite right. I indeed don't know what the word means. But thanks to all you guys for the key words mentioned, I finally figure it out and make my servo move.
For any other layperson who has the same problem or need you can(if you previously didn't use servo in your program):

Code: Select all

#define SERVO
#define PRI_SERVO_FROM   1
#define PRI_SERVO_TO     1 
//These two lines define how many servos you want to use and in my board the pin is from A0 to A2, and here I use A0;
//After this you can control your servo by servo[servo_number]
servo[0] = 1020; //it switch the servo at pin A0 to zero degrees or use 2000 to switch it to 180 degrees.


It was all so tough for beginners, so we really hope everyone who's available can teach us more and we are always willing to learn. :D

Post Reply