[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/diy/ - Do It Yourself


View post   

File: 1.42 MB, 2592x1728, IMAG0204.jpg [View same] [iqdb] [saucenao] [google]
311077 No.311077 [Reply] [Original]

I've been continuing my work on my bicopter which I have posted about back in July or so. Progress has been slow since I don't always have time to work on it.

I have run into a problem however. The program that I typically run to test my motors, along with any other program only works intermittently. A of right now, nothing works at all. I upload my program to the arduino, plug my ESC in, supply power, and....
the motor twitches. That is it.
This response is inconsistent as well. At times, I have gotten the motors working fine, but many hours are wasted figuring out what is wrong.

Can anyone help me figure out the issue? How do I fix it?

pic related: My setup for trust and motor tests.
it is typically clamped to the table.

>> No.311079
File: 71 KB, 1439x773, Capture.jpg [View same] [iqdb] [saucenao] [google]
311079

This is the code I use on my Arduino Uno.
I pulled it from some testing examples.

#include <Servo.h>

Servo myservo;


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);
myservo.write(angle);
}

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


void loop()
{
int speed;

// sweep up from 0 to to maximum speed in 20 seconds
for(speed = 0; speed <= 100; speed += 5) {
setSpeed(speed);
delay(1000);
}
// sweep back down to 0 speed.
for(speed = 95; speed > 0; speed -= 5) {
setSpeed(speed);
delay(1000);
}
setSpeed(0);
delay(5000); // stop the motor for 5 seconds
}

>> No.311400

>>311077

It's not a electrical problem? I'm not an expert, but you're using the same source for the arduino and the motor, try to use a motor shield (or at least different power sources)

>> No.311424

contact me at ikramshah@live.com

>> No.311425

>>311077

i don't mean to be a pedant, seriously, but don't use delay() for anything but blinky-blinky-LED toys (or simple stuff in init() or whatever). you'll never get anything to work that requires closed-loop control if the CPU is hung for a whole frikkn second in a delay().

learn to use millis() and a long to watch time. works like you'd use a wall clock to cook an egg: read whatever the time is on the clock, eggs in boiling water, wait til the clock shows original time + 12 minutes (or whatever).

long int T;
...

// start timer

T= millis() + 12 * 60 * 1000; // current time plus 12 minutes

....

in loop() somewhere:

if (millis() >= T) {
// do the thing that wants to get done
T= millis() + 12 * 60 * 1000; // restart the timer
}
// start

>> No.311429

>>311079

here's "better" code, that uses a state machine, and millis(). i'm dead serious -- follow this code, figure out how it works, because state machines will let you code ANYTHING YOU WANT and NEVER BLOCK and you can reduce even fantastically complex behavior down to step (state) changes.

thank Alan Turing, 1936.

I wrote it 'long' for simplicity and clarity. personally i'd use switch() ... case.


// this is a STATE MACHINE...
int state = 0;
long T = 0;
int speed= 0; // wants to be GLOBAL


void loop() {

if (millis() > T) { // if it's time to change the speed...

if (state == 0) { // sweep up from 0 to max speed in 20 sec
speed += 5; // increase speed,
if (speed >= 100) {
state= 1;
T= millis() + 1000;
}


}
else if (state == 1) { // sweep down to 0...
speed -= 5;
if (speed <= 0) {
state= 2;
T= millis() + 1000;
}

}
else if (state == 2) { // stop the motor for 5 sec...
state= 0;
speed= 0;
T= millis() + 5000; //
}

// Set motor speed.
setSpeed (speed);
}
}

>> No.311430

>>311077

LAST ONE! lol. If this were MY project, this is how I would code it. It does everything that your program does, but works. Note that you could do anything in 'real time' while the motor speed is changing.
int state = 0;
long T = 0;
unsigned Tint;
int speed= 0; // wants to be GLOBAL

void loop() {

if (millis() > T) { // if it's time to change the speed...
switch (state) {
case 0: Tint= 1000; speed += 5; if (speed >= 100) ++state; break;
case 1: speed -= 5; if (speed <= 0) ++state; break;
case 2: Tint= 5000; state= 0; break;
}
T += Tint;
setSpeed (speed);
}
}

>> No.311434

>>311400

what this guy says. those motors draw HUGE current, definitely do not run the Arduino from the motor battery without serious attention to isolation.

if the Arduino's 5V drops below 4.8V -- EVEN FOR A MILLISECOND -- it will reset and cause the ssymptom yo udescribe.

but look at the code i posted (sorry, 4chan ruins indent), your blocking code (eg. the delay() bit) will fuck you up every time.

i take it you're new to coding -- that's cool, no insult meant! you have the logical arrangement OK, but not yet partaking of the tools that C gives you. hack on, man...

>> No.311459

If you're serious about using the Arduino for more complex control scenarios you really want to learn how to use timers and interrupts. The timer functionality underlying functions like millis is actually quite sophisticated and capable of doing a lot more with greater efficiency. There's a learning curve, but I have found that that functionality is fairly standard in the microcontroller world so what you learn can be generally applied.

>> No.311545

On a somewhat related note, but not trying to hijack OP's thread, if I wanted to get into microcontrol programming where is a good place to start. I programmed in HS but that was generic kiddie, PASCAL. So I basically know very little,

>> No.311567

>>311459
you are right, but i think it's a bit too much for OPs project right now.

>> No.311568

>>311545

well, look at OPs project -- its perfect. it's got a physical thing to accomplish, and software (and motors) the means. Rather than "learning programming" in the abstract OP is making one part work at a time to make a whole. imho that's a good approach.

>> No.311656

Some ESCs require to stay some time in the minimum throttle before it can arm and spin the motor. Usually this point is around 30% of the arduino's servo output. Not only that but they won't start if the speed variable is too high when you plug your esc in.

Try uploading a servo knob into your arduino, setting up a serial to report your speed val and using a potentiometer to tweak the value slowly. You'll be able to find out when exactly it arms.

>> No.311674

>>311656
Also anon i can't see if you plugged the ESC's v+ (the one between signal and ground in that 3-plug) back into your arduino, but don't do it. Ground and signal are the only ones you need.

>> No.311702
File: 1.21 MB, 2592x1728, IMAG0200.jpg [View same] [iqdb] [saucenao] [google]
311702

OP here,
This is my first programming experience, and I've been having trouble figuring out how to control two motors with one program.
Again, I have no idea what I'm doing.

>> No.311704

>>311702
You need a main loop that runs continuously and checks inputs, performs calculations, holds state + timer + whatever else variables, applies outputs, and is time-aware. Time awareness can be as simple as a main loop counter (not reliable though, too jittery) or using the timer peripherals of the microchip.

It's a bit much to bite off and chew for someone's first programming experience, though. My dad started to teach me programming when I was 6 years old (so this is in the 80s). It all seems obvious now, but I didn't even realize you could set a variable to be equal to itself plus a literal value until dad showed me.

You'll get there with help eventually.

>> No.311770

im late to the party but does this system have any gyros in it yet?
Shit will not work at all unless you have a gyro to regulate the motor speed.

>> No.311897

OP again
My group and I worked for a few hours with the advice you guys gave. The Arduino is now being powered though a separate DC power source and the hot wire from my ESC is not connected at all.
This resolved problems we had with one motor and one ESC. The other motor continued the malfunction and the other ESC didnt seem to work at all.

We figured it was a connection issue (and it was) but it took a very long time to isolate the problem and full get rid of it. We had to completely remove the pins on the motor and directly connect them through wires.
When this motor worked again, one of my partners threw his hands up in success and managed to smash the lightbulb in my ceiling fan. We had to stop then and will probably continue to work in a few days. I myself will see what I can do to get the ESC working. It would cost $20 to replace and I would much like to avoid that.

Thank you for the programming suggestions, but, as I said before, I pulled up sample code to work with.

>>311770
We have not come to a definite decision on what gyro we will purchase. If anyone can recommend one that is cheap, compatible with and Arduino Uno, and efffective, it would be greatly appreciated.

>> No.312539

>>311568
I get what you are saying about having a specific project and goal in mind and that being far easier and more practical than just learning everything. Ill look around the internets some more and see if there is something I specifically want to accomplish.