Hello!
I am trying to control a brushless motor via an ESC using radio control.
Whenever I try to throttle up, the motors beep for a little, then jitter while heating up quickly.
I have swapped escs and motors but no difference.
I also have tried using LeftMotor.write(number from 0-180) and also LeftMotor.writeMilliseconds(number from 1000-2000)
I would like some help
Thanks!
Here is the code. I am programming it to control a flying wing.
#include <Servo.h>
Servo ElavonLeft;
Servo ElavonRight;
Servo RightMotor;
Servo LeftMotor;
//ServoPositions
int ElavonLeftPos = 0;
int ElavonRightPos = 0;
//Interstage
int ElevatorInter = 0;
int AileronInter = 0;
int ThrottleInter = 0;
int RudderInter = 0;
//MotorPower
int LeftMotorPower = 0;
int RightMotorPower = 0;
// experimental
//float InterpolateElevatorPos = 0;
//float InterpolateAileronPos = 0;
//const float beta = 0.5;
//ServoPins
const int ElavonLeftPin = 12;
const int ElavonRightPin = 13;
const int LeftMotorPin = 2;
const int RightMotorPin = 7;
//PWM input from Rx
const int ElevatorPin = 6;
const int AileronPin = 9;
const int RudderPin = 5;
const int ThrottlePin = 11;
const int ModeSwitchPin = 3;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ElavonLeft.attach(ElavonLeftPin);
ElavonRight.attach(ElavonRightPin);
LeftMotor.attach(LeftMotorPin);
RightMotor.attach(RightMotorPin);
}
void loop() {
// Read the Positions
int AuxStickPosition = pulseIn(ModeSwitchPin, HIGH, 25000);
if (AuxStickPosition < 1300) {
int ElevatorStickPosition = pulseIn(ElevatorPin, HIGH, 25000);
int RudderStickPosition = pulseIn(RudderPin, HIGH, 25000);
int AileronStickPosition = pulseIn(AileronPin, HIGH, 25000);
int ThrottleStickPosition = pulseIn(ThrottlePin, HIGH, 25000);
int AuxStickPosition = pulseIn(ModeSwitchPin, HIGH, 25000);
// experimental interpolation
//InterpolateElevatorPos = (beta * (float)ElevatorStickPosition) + ((1 - beta) * InterpolateElevatorPos);
//ElevatorStickPosition = (int) InterpolateElevatorPos;
//InterpolateAileronPos = (beta * (float)AileronStickPosition) + ((1 - beta) * InterpolateAileronPos);
//AileronStickPosition = (int) InterpolateAileronPos;
//Read and convert
//RudderInter = map(RudderStickPosition, 1000,2000, -150, 150);
AileronInter = map(AileronStickPosition, 1000, 2000, -150, 150);
// ThrottleInter = map(ThrottleStickPosition, 1000, 2000, -150, 150);
ElevatorInter = map(ElevatorStickPosition, 1000, 2000, -150, 150);
//wut
ElavonLeftPos = map(AileronInter - ElevatorInter, -310, 310, 0, 180);
ElavonRightPos = map(AileronInter + ElevatorInter, -310, 310, 0, 180);
// RightMotorPower = map(ThrottleInter + RudderInter, -150, 150, 1000, 2000);
// LeftMotorPower = map(ThrottleInter - RudderInter, -150, 150, 1000, 2000);
LeftMotorPower = ThrottleStickPosition;
//Constrain
ElavonLeftPos = constrain(ElavonLeftPos, 0, 180);
ElavonRightPos = constrain(ElavonRightPos, 0, 180);
LeftMotorPower = constrain(LeftMotorPower, 0, 2000);
RightMotorPower = constrain(RightMotorPower, 0, 2000);
ElavonLeft.write(ElavonLeftPos);
ElavonRight.write(ElavonRightPos);
LeftMotor.writeMicroseconds(LeftMotorPower);
RightMotor.writeMicroseconds(RightMotorPower);
Serial.println(ElevatorStickPosition);
delay(2);
}
else {
int AuxStickPosition = pulseIn(ModeSwitchPin, HIGH, 25000);
LeftMotor.write(0);
RightMotor.write(0);
delay(5);
}
}