go_away

### Author Topic: Motor Control Problem  (Read 4587 times)

0 Members and 1 Guest are viewing this topic.

#### sleepyhao

• Jr. Member
• Posts: 11
##### Motor Control Problem
« 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.

Code: [Select]
`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.
« Last Edit: March 21, 2008, 07:51:55 AM by sleepyhao »

#### andreahmed

• Jr. Member
• Posts: 37
##### Re: Motor Control Problem
« Reply #1 on: March 20, 2008, 03:03:48 PM »
put else  {     movebrake (); }

#### superchiku

• Supreme Robot
• Posts: 953
• cooll
##### Re: Motor Control Problem
« Reply #2 on: March 20, 2008, 10:22:01 PM »
ur using pwm right

l293d works with pwm dont use normal on and off procedure
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

#### Rebelgium

• Supreme Robot
• Posts: 638
• It's called the future ... We like it here
##### Re: Motor Control Problem
« Reply #3 on: March 21, 2008, 04:36:53 AM »
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.
To relax after some hard work on robotics: A very fun free online text based MMORPG
orgcrime.net

#### hazzer123

• Supreme Robot
• Posts: 460
##### Re: Motor Control Problem
« Reply #4 on: March 21, 2008, 04:45:29 AM »
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
Imperial College Robotics Society
www.icrobotics.co.uk

#### sleepyhao

• Jr. Member
• Posts: 11
##### Re: Motor Control Problem
« Reply #5 on: March 21, 2008, 07:48:52 AM »
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.
« Last Edit: March 21, 2008, 07:50:40 AM by sleepyhao »

#### hazzer123

• Supreme Robot
• Posts: 460
##### Re: Motor Control Problem
« Reply #6 on: March 21, 2008, 08:05:21 AM »
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?
« Last Edit: March 21, 2008, 08:10:22 AM by hazzer123 »
Imperial College Robotics Society
www.icrobotics.co.uk

#### sleepyhao

• Jr. Member
• Posts: 11
##### Re: Motor Control Problem
« Reply #7 on: March 21, 2008, 09:47:46 AM »
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"

#### hazzer123

• Supreme Robot
• Posts: 460
##### Re: Motor Control Problem
« Reply #8 on: March 21, 2008, 10:19:37 AM »
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.
Imperial College Robotics Society
www.icrobotics.co.uk

#### sleepyhao

• Jr. Member
• Posts: 11
##### Re: Motor Control Problem
« Reply #9 on: March 21, 2008, 10:20:49 AM »
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

#### sleepyhao

• Jr. Member
• Posts: 11
##### Re: Motor Control Problem
« Reply #10 on: March 21, 2008, 02:08:56 PM »
Here's what I experimented:

Code: [Select]
`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!!
« Last Edit: March 21, 2008, 02:13:49 PM by sleepyhao »

#### hazzer123

• Supreme Robot
• Posts: 460
##### Re: Motor Control Problem
« Reply #11 on: March 21, 2008, 03:35:13 PM »
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 -

Code: [Select]
`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.
Imperial College Robotics Society
www.icrobotics.co.uk

#### sleepyhao

• Jr. Member
• Posts: 11
##### Re: Motor Control Problem
« Reply #12 on: March 22, 2008, 06:59:02 AM »
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 -

Code: [Select]
`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.

#### Afroman

• Expert Roboticist
• Full Member
• Posts: 50
##### Re: Motor Control Problem
« Reply #13 on: March 22, 2008, 09:51:10 AM »
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.

• Supreme Robot
• Posts: 11,680
##### Re: Motor Control Problem
« Reply #14 on: March 23, 2008, 11:52:37 AM »
And get out a multimeter to voltage test the other pins, too.

#### sleepyhao

• Jr. Member
• Posts: 11
##### Re: Motor Control Problem
« Reply #15 on: March 24, 2008, 12:19:37 AM »
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

#### sleepyhao

• Jr. Member
• Posts: 11
##### Re: Motor Control Problem
« Reply #16 on: March 24, 2008, 12:41:29 AM »
And there's another thing I'd like to ask, which is slightly OOT.

I got this code from the \$50 Robot tutorial

Code: [Select]
`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

Code: [Select]
`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?

• Supreme Robot
• Posts: 11,680