Society of Robots - Robot Forum
Software => Software => Topic started by: sleepyhao on March 20, 2008, 09:14:50 AM
-
Hi guys, I'm trying to make sure that my code works for my motor control. What I do is to load the program into the PIC, with the output of the PIC connected to L293D, and then the motor.
void movestraight(void);
void moveright(void);
void moveleft(void);
void movebrake(void);
void main()
{
TRISB = 0x00; //Port B as output
TRISD = 0xFF; //Port D as input
PORTB = 0xFF;
PORTD = 0xFF;
while (1)
{
if (PORTD.F0 == 0)
{
movestraight ();
}
else if (PORTD.F1 == 0)
{
moveright ();
}
else if (PORTD.F2 == 0)
{
moveleft ();
}
else if (PORTD.F3 == 0 )
{
movebrake ();
}
}
}
void movestraight(void){ //Move forward
PORTB.F4 = 1; //I1 high
PORTB.F5 = 0; //I2 low
PORTB.F6 = 0; //I3 low
PORTB.F7 = 1; //I4 high
}
void moveright(void){ //Move right
PORTB.F4 = 1;
PORTB.F5 = 0;
PORTB.F6 = 1;
PORTB.F7 = 0;
}
void moveleft(void){ //Move left
PORTB.F4 = 0;
PORTB.F5 = 1;
PORTB.F6 = 0;
PORTB.F7 = 1;
}
void movebrake(void){ //Brake
PORTB.F4 = 0;
PORTB.F5 = 0;
PORTB.F6 = 0;
PORTB.F7 = 0;
}
What I'm trying to do here is that whenever I connect the ground to any one of the input pins, the uC will then instruct the motors to spin in the directions assigned.
But now, the problem I'm facing is that the motors will just go in one direction no matter which input pins is grounded. Is there anything wrong with my code? Thanks.
-
put else { movebrake (); }
-
ur using pwm right
l293d works with pwm dont use normal on and off procedure
-
ur using pwm right
l293d works with pwm dont use normal on and off procedure
Just because a Motor driver can use PWM doesn't mean you can't use normal on and off. PWM is normal on and off...just faster.
-
Have you used pull up resistors on your inputs? Otherwise they will all have high impedance when not grounded and this can lead to strange results.
Also, if this doesn't fix your problem, can you post what compiler you are using and what PIC you are using.
Thanks
-
put else { movebrake (); }
I don't think it'll work that way too. Thanks anyway.
Have you used pull up resistors on your inputs? Otherwise they will all have high impedance when not grounded and this can lead to strange results.
Also, if this doesn't fix your problem, can you post what compiler you are using and what PIC you are using.
Thanks
Yeap, I did that.
I'm using microC compiler, using 16F877A.
Quite frustrated with the results after trying to troubleshoot the problem in these few days :(
I'm building a photovore and I've already faced this kind of problem before I can even load the whole code I've just written to it.
-
What happens when you turn the robot on? Do the motors always go the same way? Which way is it if they do? Do you have access to debug features? (ie are you using an ICD2 or a PICkit?)
Who makes the compiler?
-
What happens when you turn the robot on? Do the motors always go the same way? Which way is it if they do? Do you have access to debug features? (ie are you using an ICD2 or a PICkit?)
Who makes the compiler?
Yeap, the motors will go forward.
I assume that the debugger is turned on as the "Software PIC Simulator" is checked. The another option of the debugger is "microICD Debugger"
The compiler is made by mikroElektronica. More info: http://www.mikroe.com/en/compilers/mikroc/pic/
-
Right, i can't see any problems with your code.
You need to use the debugging facilities on your programmer/software so step through each command step by step. Use a multimeter to check that the pins are actually turning on and off.
Thats all i can recommend.
-
Right, i can't see any problems with your code.
You need to use the debugging facilities on your programmer/software so step through each command step by step. Use a multimeter to check that the pins are actually turning on and off.
Thats all i can recommend.
Alright, I'll see what I can do. Thanks a lot, I appreciate it :)
-
Here's what I experimented:
void movestraight(void);
void moveright(void);
void moveleft(void);
void movebrake(void);
void main()
{
TRISB = 0x00; //Port B as output
TRISD = 0xFF; //Port D as input
PORTB = 0xFF;
PORTD = 0xFF;
while (1)
{
/* if (PORTD.F0 == 0)
{
movestraight ();
}
else if (PORTD.F1 == 0)
{
moveright ();
}
else */
if (PORTD.F2 == 0)
{
moveleft ();
}
/* else if (PORTD.F3 == 0 )
{
movebrake ();
} */
}
}
/* void movestraight(void){ //Move forward
PORTB.F4 = 1; //I1 high
PORTB.F5 = 0; //I2 low
PORTB.F6 = 0; //I3 low
PORTB.F7 = 1; //I4 high
} */
void moveright(void){ //Move right
PORTB.F4 = 1;
PORTB.F5 = 0;
PORTB.F6 = 1;
PORTB.F7 = 0;
}
void moveleft(void){ //Move left
PORTB.F4 = 0;
PORTB.F5 = 1;
PORTB.F6 = 0;
PORTB.F7 = 1;
}
void movebrake(void){ //Brake
PORTB.F4 = 0;
PORTB.F5 = 0;
PORTB.F6 = 0;
PORTB.F7 = 0;
}
I disabled the movestraight (), moveright () and movebrake () inside the loop, leaving only the moveleft(). Then I disabled the void movestraight(void) function.
What I'm getting is that the motors turn to the right direction eventhough the moveleft function is being called. It's like whatever function appears first is being called. Did I missed anything inside the code?
Update: Now the robot just go forward regardless of what functions I disabled. I'm gonna choke it to death if only it has a neck!! >:(
-
hmmm this is tricky....
try making some really simple code, then build it up, and you should see where the bug comes into play.
like this -
void main()
{
TRISB = 0x00; //Port B as output
TRISD = 0xFF; //Port D as input
PORTB = 0xFF;
PORTD = 0xFF;
PORTB.F4 = 1; //I1 high
PORTB.F5 = 0; //I2 low
PORTB.F6 = 0; //I3 low
PORTB.F7 = 1; //I4 high
while(1);
}
Do that and see if it goes forward.
If it does, change it to 1010 and see if it moves right.
-
hmmm this is tricky....
try making some really simple code, then build it up, and you should see where the bug comes into play.
like this -
void main()
{
TRISB = 0x00; //Port B as output
TRISD = 0xFF; //Port D as input
PORTB = 0xFF;
PORTD = 0xFF;
PORTB.F4 = 1; //I1 high
PORTB.F5 = 0; //I2 low
PORTB.F6 = 0; //I3 low
PORTB.F7 = 1; //I4 high
while(1);
}
Do that and see if it goes forward.
If it does, change it to 1010 and see if it moves right.
Not working either. :(
Now I redo the whole PCB and see if it works. I suspect it's the connections that's giving the problem, not the code.
Thanks a lot man! You've been very helpful. ;)
-
Please verify with an oscilloscope that the pins are in fact going high or low when they should. Sometimes functionality on a pin or two on microcontrollers just dies unexpectedly. Usually related to static electricity.
-
And get out a multimeter to voltage test the other pins, too.
-
Please verify with an oscilloscope that the pins are in fact going high or low when they should. Sometimes functionality on a pin or two on microcontrollers just dies unexpectedly. Usually related to static electricity.
And get out a multimeter to voltage test the other pins, too.
I've done that and got some weird results, that's why I suspect it's the connections that cause the problem (most probably the molex ), hence I redo the whole PCB. Thanks for the suggestions :)
-
And there's another thing I'd like to ask, which is slightly OOT.
I got this code from the $50 Robot tutorial
int threshold=8;//the larger this number, the more likely your robot will drive straight
Does it means that the 8 here is the differences between the value obtained from the right sensor and left sensor?
Let's say,
Right sensor = 5
Left sensor = 10
And from this code
if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold)
It should be ( left sensor - right sensor ) = ( 10 - 5 ) = 5, smaller than the threshold --> move straight?
-
If the resultant value is below the threshold, the robot will drive straight.
So higher the threshold, the more like the value will be under the threshold, hence more likely to drive straight.
Its a simple binary way of looking at it, but with my robots I use fuzzy logic that doesn't have thresholds. I just wanted it to be simple for you guys.