|
|
View previous topic :: View next topic |
Author |
Message |
mecharon
Joined: 09 Jun 2011 Posts: 3
|
Can't control 3 DC motors at the same time |
Posted: Thu Jun 09, 2011 12:00 pm |
|
|
Hi,
I'm a mech. eng. and beginner to electronic circuits and PIC programming.
I need to control of 3 DC motors' directions and positions via pulses taken from rotary encoders which connected to motors' shafts.
If I connect 1 motor to circuit I can control it's direction and position. But when I connect all of the motors, things are going different.
I'm sending, for example, 1A1A1A. This should have turn each of the motors anti-clockwise in direction and count the encoders of 65 pulses.
When I send 1A1A1A or 0A1A1A, first and third motor runs correctly. Second motor is running after waiting of 1 minute and stops. I can send another 6 byte string after that.
When I send 1A0A1A, first and second motor runs anti-clockwise, third motor runs clockwise. I can send another string.
It doesn't work if I send 0 as the third motor's direction. (1A1A0A or 0A0A0A)
I've read a lot of topics about this already, but couldn't make it worked.
Here's my code:
Any help would be appreciated.
Code: |
#include <16F877.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,STREAM=COM_A,timeout=10,parity=N,stop=1,bits=8) //DISABLE_INTS,timeout=10,parity=N,stop=1,bits=8
#byte TRISC=0x87
#byte TRISD=0x88
#byte PORTC=0x07
#byte PORTD=0x08
#bit MOTOR11=PORTC.0
#bit MOTOR12=PORTC.1
#bit MOTOR21=PORTC.2
#bit MOTOR22=PORTC.3
#bit MOTOR31=PORTC.4
#bit MOTOR32=PORTC.5
#bit ENC1=PORTD.0
#bit ENC2=PORTD.1
#bit ENC3=PORTD.2
int8 i=0, j=0, k=0,m=0;
char joker[10]={0,0,0,0,0,0,0,0,0,0};
int1 a1=0, b1=0, durum=0;
int1 a2=0, b2=0;
int1 a3=0, b3=0;
struct gorev{
int1 yon; //direction of the motor
int8 adim; //pulse count
};
struct gorev motor1={0,0};
struct gorev motor2={0,0};
struct gorev motor3={0,0};
#INT_RDA
void RDA_isr(void)
{
joker[m]=fgetc(COM_A);
putc(joker[m]);
m++;
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
// TODO: USER CODE!!
TRISC=0b10000000;
TRISD=0b11111111;
PORTC=0b01000000;
PORTD=0b00000000;
while(1)
{
i=0;
j=0;
k=0;
durum=1;
while(m>5)
{
a1=0;b1=0;
a2=0;b2=0;
a3=0;b3=0;
if(durum==1)
{
motor1.yon=(joker[0]==0x31);
motor1.adim=joker[1];
motor2.yon=(joker[2]==0x31);
motor2.adim=joker[3];
motor3.yon=(joker[4]==0x31);
motor3.adim=joker[5];
durum=0;
}
if(i<motor1.adim)
{
a1=ENC1;
MOTOR11=motor1.yon;
MOTOR12=(!motor1.yon);
b1=ENC1;
i+=(b1!=a1);
}
else if(MOTOR11==MOTOR12)
{
}
else
{
MOTOR11=0;
MOTOR12=0;
}
if(j<motor2.adim)
{
a2=ENC2;
MOTOR21=motor2.yon;
MOTOR22=(!motor2.yon);
b2=ENC2;
j+=(b2!=a2);
}
else if(MOTOR21==MOTOR22)
{
}
else
{
MOTOR21=0;
MOTOR22=0;
}
if(k<motor3.adim)
{
a3=ENC3;
MOTOR31=motor3.yon;
MOTOR32=(!motor3.yon);
b3=ENC3;
k+=(b3!=a3);
}
else if(MOTOR31==MOTOR32)
{
}
else
{
MOTOR31=0;
MOTOR32=0;
}
if(i>=motor1.adim && j>=motor2.adim && k>=motor3.adim)
{
putc('T');
m=0;
}
} //while m>5
} //while 1
}
|
Circuit:
Proteus screenshot:
|
|
|
mecharon
Joined: 09 Jun 2011 Posts: 3
|
|
Posted: Mon Jun 13, 2011 6:02 am |
|
|
I'm still stuck on it. Any thoughts? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Jun 13, 2011 7:48 am |
|
|
Lots of thought..quickly..
1) get rid of the tris..... commands. Let the compiler handle the 'details' as ONE wrong setting on your part can cause a LOT of weird problems.
2) add 'errors' to the use RS232(...) function.it'll keep the serial port working correctly.
3) confirm that each motor works right invididually. Then add a second motor and confirm, then the third.Perhaps the 'control' byte that you're sending to the motor drivers is a bit wrong ?
4) this 1A1A.... commands. Is that data from the serial port ? If so, see #2 above. Hard code the program to test all the motors to confirm it's a PC communications problem and NOT the actual command to the motors.
5) display the received commands onto the LCD on line 1 to verify what you got and display the motor command on line 2. This will show you what is really happening instead of 'well it should be....'. |
|
|
mecharon
Joined: 09 Jun 2011 Posts: 3
|
|
Posted: Tue Jun 14, 2011 7:31 am |
|
|
Thank you for your reply.
I tried all of your advices and saw that the code is working correctly, but circuit still isn't. Then I decided to destroy the circuit. I took it, then started to bending... It suddenly started to working as it's meant to be. Problem is a soldering connection fault. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Jun 14, 2011 7:40 am |
|
|
Glad you found the cause to all your grief! I once had a 'looked good 'solder joint fail after 4 years of harsh service.even under a magnfying glass is loloked good,but cold spray finally should a micro fracture.2 seconds of heat to resolder and it was cured.
It's not always the software that's at fault !! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|