简体   繁体   中英

Servo motor rotation

I am working on a quadcopter flight controller using Arduino. I have been having a lot of trouble trying to figure out how to get my motors to rotate in an Anti-clockwise direction. I currently have the quad-copter motors rotating clockwise but this causes the quad-copter to spin uncontrollably during flight. The code that I am using is displayed below and any help with the problem above would be very much appreciated. Thanks in advance.

The library I am using is:

#include <Servo.h>

The pins are attached to the Arduino Uno like this.

  m1.attach(4);
  m2.attach(6);
  m3.attach(8);
  m4.attach(9);

I call the arm function shown here:

arm();

Which invokes this code:

void arm() {
    // arm the speed controller, modify as necessary for your ESC
    setSpeed(0);
    delay(1000); //delay 1 second,  some speed controllers may need longer
  }

This is the code here then starts up the motors:

 void startMotors()
  {
     speed = 40; 
     setSpeed(speed);

    Serial.println("Motor Speed: " + speed); 
    motorsOn = true;
  }

As shown the SetSpeed method is invoked in both arm and startmotors function.

void setSpeed(int speed) {
    // speed is from 0 to 100 where 0 is off and 100 is maximum speed
    //the following maps speed values of 0-100 to angles from 0-180,
    // some speed controllers may need different values, see the ESC instructions
    int angle = map(speed, 0, 100, 0, 180);
    m1.write(angle);
    m2.write(angle);
    m3.write(angle);
    m4.write(angle);
  }

Now, the arm function I am not worried about because it is just used for arming and no rotation is need but, its in the start motors function where I need to figure out how to get motors m2 and m3 to rotate Anti - clockwise. Otherwise, the quadcopter will spin in flight, this problem when solved will help to keep the quadcopter stable on left off. I Hope this modification is enough for you to Understand and again thank you in advance.

Your problem is not in the arduino side of things, it's in how you have your motors connected. Your motors will have 3 wires going between them and your ESCs, you need to flip two of those wires. Doesn't matter which ones you switch. After you do that, make sure that the propellers are on the correct way. (Tell me if you have an uncommon setup and I will help you from there.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM