go_away

### Author Topic: Beginner question: Speed control of robot wheels  (Read 813 times)

0 Members and 1 Guest are viewing this topic.

#### abbyboy86

• Beginner
• Posts: 6
##### Beginner question: Speed control of robot wheels
« on: February 14, 2013, 08:40:56 PM »
Ok so I am an absolute beginner at robot building.. Though I have been studying like a complete maniac for the past few weeks, and have a degree in electronics but that degree was 4 years back and I dont think I remember much now!
.
Imagine a protoboard held horizontally..
Basically the circuit on my protoboard is is an H-bridge driver using bipolar transistors on the right.. there is a pic 16f57 at the left... in the center there is a voltage regulator.. I put this along with a pot to vary the voltage to the HBridge circuit... Please suggest me a better way if there is any... Earlier I had put an opamp as well but I removed it now..

I think I will use a 10.8V battery for the motor and a 7.2V for the microcontroller.. Is that fine ?

The main problem which I am facing is that I want to control the voltage in order to control the speed of the motors using my PIC...

What I thought was to attach voltage regulators, positive and negative.. and then use an op-amp to amplify voltage given by a microcontroller and feed the output to the regulators.. the analog input was 0.0517v to the opamp.... That output could be fed to the voltage regulator..

But for now I have removed the op-amp, and im thinking of directly feeding the battery voltage to the LM317, and LM337 (which unfortunately I don't have right now).. Ultimately, I would want to use a microcontroller code and give input to the adjust terminal of the regulator to vary the speed...

edit: i've attached a photo of the circuit...
« Last Edit: February 15, 2013, 05:23:19 AM by abbyboy86 »

#### jwatte

• Supreme Robot
• Posts: 1,343
##### Re: Speed control of robot wheels
« Reply #1 on: February 14, 2013, 09:57:09 PM »
Quote
that degree was 4 years back and I dont think I remember much now!

I don't want to sound down on you or anything, but my experience is this: If you forget an education in 4 years, you will probably fail at robotics, and probably also fail at life!

The voltage regulator should only be between the input voltage, and the VCC of the microcontroller. Set it to output whatever the microcontroller wants for input (typically 5V or 3.3V, but the data sheet will tell you for sure) and add a bypass capacitor and perhaps two capacitors around the voltage regulator.

To regulate the speed of a motor, you use PWM output from the microcontroller. To go faster, make the PWM duty cycle longer. The H-bridge needs to be able to keep up with the frequency of the PWM. If the frequency is 200 Hz, almost anything except relays will keep up, but response will be somewhat slow, and it will be noisy. Modern MOSFET based H-bridges with fast-switching gate drives can keep up at 20 kHz and above, at which point the PWM will be inaudible.

The PWM functions of a microcontroller typically are tied to whatever timer peripherals they have. The details vary be family (ARM, AVR, PIC, MSP, etc) and are described in the data sheet of the microcontroller.
« Last Edit: February 15, 2013, 12:31:06 PM by jwatte »

#### abbyboy86

• Beginner
• Posts: 6
##### Re: Speed control of robot wheels
« Reply #2 on: February 15, 2013, 04:17:31 AM »
I researched here and at other places and made this code.. It compiled successfully.. Would it work properly on my robot.? This code is for the IR sensor which I have..
Another modified code for when my photoresistors arrive is there at the end... What about that one ?

When there are more functions, I am thinking of making a header file containing all the functions... The code is as below..

/*
* File:   main.cpp
* Author: Abby
*
* Created on February 11, 2013, 12:57 AM
*/

#define Rea1 RB5
#define Rea2 RB6
#define Rea3 RB4
#define Rea4 RB2
#define Reb1 RB7
#define Reb2 RC0
#define Reb3 RA3
#define Reb4 RA2
#define LP RA0
#define RP RC7
#define LED1 RC3
#define IRDEV RC4
#include <xc.h>
#include <pic.h>

void l_off()
{ while (!0)
Rea1,Rea2,Rea3,Rea4=0;
}

void l_forw()
{ while(!0)
Rea1,Rea2=0;
Rea3,Rea4=5;
}

void l_rev()
{   while(!0)
Rea1,Rea2=5;
Rea3,Rea4=0;
}

void l_br()
{   while (!0)
Rea1,Rea2,Rea3,Rea4=5;
}

void r_off()
{    while (!0)
Reb1,Reb2,Reb3,Reb4=0;
}

void r_forw()
{    while(!0)
Reb1,Reb2=0;
Reb3,Reb4=5;
}

void r_rev()
{ while(!0)
Reb1,Reb2=5;
Reb3,Reb4=0;
}

void r_br()
{   while (!0)
Reb1,Reb2,Reb3,Reb4=5;
}

void off()
{  while (!0)
l_off();
r_off();
}

void forward()
{  while (!0)
l_forw();
r_forw();
}

void reverse()
{ while (!0)
l_rev();
r_rev();
}

void brake()
{  while (!0)
l_br();
r_br();
}

void turn_left()
{  while (!0)
l_rev();
r_forw();
}

void turn_right()
{  while (!0)
l_forw();
r_rev();
}

int main(void)
{
while (!0)
{
while(IRDEV<4.4)
forward();
if (IRDEV>=4.4)
{
brake();
for (int i=0;i<100;i++)
{
turn_right();
if (IRDEV<=2)
i=100;
}
}
}
LED1=5;

}

*******************************
*******************************
//For photo-resistors//

int main(void)
{
while (!0)
{
if (LP-RP>2)
turn_right();
else if(LP-RP<-2)
turn_left();
else
forward();
LED1=5;
}

« Last Edit: February 15, 2013, 04:27:40 AM by abbyboy86 »

#### abbyboy86

• Beginner
• Posts: 6
##### Re: Speed control of robot wheels
« Reply #3 on: February 15, 2013, 04:28:59 AM »
If all this works, I will have created my first automata !!! Yeahh !!!!

#### abbyboy86

• Beginner
• Posts: 6
##### Re: Speed control of robot wheels
« Reply #4 on: February 15, 2013, 04:35:04 AM »
A couple of more questions ---

I am using the following components.. Are they good ..? For now I just need a small robot capable of running for weeks...

H-bridge....

Diodes-- 1N4001

transistors -- 4401 and 4403

and for the microcontroller, I don't have a nF or pF capacitor.. I have 2200 uF I think...

#### abbyboy86

• Beginner
• Posts: 6
##### Re: Speed control of robot wheels
« Reply #5 on: February 15, 2013, 05:30:43 AM »
Quote
that degree was 4 years back and I dont think I remember much now![/quote[

I don't want to sound down on you or anything, but my experience is this: If you forget an education in 4 years, you will probably fail at robotics, and probably also fail at life!

The voltage regulator should only be between the input voltage, and the VCC of the microcontroller. Set it to output whatever the microcontroller wants for input (typically 5V or 3.3V, but the data sheet will tell you for sure) and add a bypass capacitor and perhaps two capacitors around the voltage regulator.

To regulate the speed of a motor, you use PWM output from the microcontroller. To go faster, make the PWM duty cycle longer. The H-bridge needs to be able to keep up with the frequency of the PWM. If the frequency is 200 Hz, almost anything except relays will keep up, but response will be somewhat slow, and it will be noisy. Modern MOSFET based H-bridges with fast-switching gate drives can keep up at 20 kHz and above, at which point the PWM will be inaudible.

The PWM functions of a microcontroller typically are tied to whatever timer peripherals they have. The details vary be family (ARM, AVR, PIC, MSP, etc) and are described in the data sheet of the microcontroller.

Thank you I will try these methods.. About the past 4 years, Ive been busy doing and learning lots of stuff man.. Its been great.. And I m coming back to tech stuff as well !

#### newInRobotics

• Supreme Robot
• Posts: 1,015
• N.I.R.
##### Re: Speed control of robot wheels
« Reply #6 on: February 15, 2013, 06:45:33 AM »
I don't want to sound down on you or anything, but my experience is this: If you forget an education in 4 years, you will probably fail at robotics, and probably also fail at life!
That's a harsh thing to say as well as totally inappropriate in engineering forum (and probably any other type of communication in the matter of fact), as that particular comment did not add any value nor answered any of abbyboy86's questions, however, based on perception of Yours alone, it implied that abbyboy86 is/will be a failure in life.

To back up my rambling - I don't remember majority of things that I learnt in uni (graduated 2 years ago) simply because I don't use them every day and my memory is terrible (or worse than I would like it to be); every time I have to write Windows app, or some software for microcontroller, or design a circuit, or use/come up with some clever algorithm I cannot take a step without referring to manual/reference/guide of some sort. Au contraire to what You said - I, for one, am not a failure in life: I have good wife, home, car, well paid job (which has little to do with what I did in uni), can afford to visit another country at least twice a year and have spare time and budget to explore other things I enjoy. So, if Your definition of failure is a person who forgets things over time, then world is full of failures and You can consider Yourself an elephant
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

#### jwatte

• Supreme Robot
• Posts: 1,343
##### Re: Beginner question: Speed control of robot wheels
« Reply #7 on: February 15, 2013, 12:38:32 PM »
Quote
Rea1,Rea2,Rea3,Rea4=0;

This code will read the values of Rea1, Rea2, and Rea3, and throw ignore those values, and will set the value of Rea4 to 0. That's probably not what you want.

Quote
while(!0)
Rea1,Rea2=5;
Rea3,Rea4=0;

This code will read the value of Rea1 and ignore it, and set Rea2 to 5, forever. It will never reach the code that reads the value of Rea3 and ignores it, and sets Rea4 to 0.

Quote
while (!0)
l_rev();
r_forw();

This code will call the function l_rev() forever, and will never get to the code that calls the function r_forw().

There are multiple similar problems in the code. I suggest starting smaller -- try just a single action, and make it actually run on the robot. Then add a single second action, and verify that it all works as expected. Slowly grow the set of features, verifying between each step. That is going to make debugging much easier.

I also highly recommend ALWAYS wrapping braces {} around all while() and if() statements, even when you only have one statement. The reason is that you will then never be confused when you try to add a second statement, and it doesn't work the way you want.

Also, in robotics, you pretty much never want to do something "forever" -- you always want some kind of time-out after which you stop, no matter what.

Quote
Rea1,Rea2,Rea3,Rea4=0;

If I needed this kind of statement, I would write this as:

Code: [Select]
`Rea1=0, Rea2=0, Rea3=0, Rea4=0;`
However, I'd be much more likely to write it as:

Code: [Select]
`void setReas(int a, int b, int c, int d) {  Rea1 = a;  Rea2 = b;  Rea3 = c;  Rea4 = d;}...  setReas(0, 0, 0, 0);`
Quote
while (!0)
l_rev();
r_forw();

I would write this as:

Code: [Select]
`#define TIMEOUT 1000...  unsigned long now = millis();  while (millis() - now < TIMEOUT) {    l_rev();    r_forw();  }`
This assumes you have a function "millis()" which returns a clock of milliseconds of size unsigned long. If your libraries are different, you need to adjust appropriately.

#### abbyboy86

• Beginner
• Posts: 6
##### Re: Beginner question: Speed control of robot wheels
« Reply #8 on: February 15, 2013, 02:21:58 PM »
<quote>

void setReas(int a, int b, int c, int d) {
Rea1 = a;
Rea2 = b;
Rea3 = c;
Rea4 = d;
}

...

setReas(0, 0, 0, 0);

</quote>

Thank you very much for this code and the other advice.. I will change my code now...