Sunday, September 5, 2010

5

(CONTROLLING RKL298 WITH ARDUINO) ARDUINO: THE BEST PIECE OF HARDWARE EVER!

 Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. And it is also very cheap! Under 20€!



In a previous post I talked about an L298 based motor controller. It is the circuit that is going to replace a faulty motor controller of the robot ITINERA. Now I need a way to command the new motor controller via a PC serial port. The previous motor controller had a Serial Motor Interface, it costs over 50€ and it is not compatible with the new motor controller.

Another issue is that the robot is programmed to use the protocol of the old Serial Motor Interface and I would not like to change that program because it took me so long to build it :p

The solution is my new best friend: Arduino. I have never used it before but thanks to its features I could program it to do what I need in less than 1 hour... and the best of all... it worked at the first attempt!!!!

Here you have the source code, it could be useful if you want to reproduce this kind of motor control or if you just want to see how easy it is to program an arduino.

I recommend you to read it, you will find much more details than in this post ;)

/*
   Serial Motor Interface
   Author: Martin Peris-Martorell - www.martinperis.com

   Description: This program is a serial port motor interface.
   It is used to command an H-Bridge motor controller based on
   L298. 
   This motor controller can be found at www.rkeducation.co.uk
   under the reference part number: RKL298 PCB

   The RKL298 PCB can control 2 DC motors. The control is achieved
   by using 6 control lines: ENA, IP1, IP2, ENB, IP3 and IP4.

   ENA, IP1 and IP2 are used to command the motor A
   ENB, IP3 and IP4 are used to command the motor B

   Wiring with arduino: 
  
   RKL298 PCB |     Arduino
   -----------------------------
   ENA       <-> Digital port 12
   IP1       <-> Digital port 11
   IP2       <-> Digital port 10
   
   ENB       <-> Digital port 7
   IP3       <-> Digital port 6
   IP4       <-> Digital port 5 

   A LED can be connected to digital port 13  

   Serial port configuration: 9600 bauds

   Comunication protocol: Connect the arduino via USB to
   a PC, or via digital pins 0 and 1 to a serial port.

   Open the serial port for communication and send 3 bytes.
   The format is:

   Byte 0: 255  //Sync. signal
   Byte 1: Motor identificator. 0 for motor A, 1 for motor B.
   Byte 2: Command. A value between 0 and 63. 
                    0       = Full stop (free running)
                    1 - 31  = Backward with PWM ( 1: slowest, 31: fastest)
                    32      = Full stop (active braking)
                    33 - 63 = Forward with PWM (33: slowest, 63: fastest)

  For example, if you want to move motor A backward at 50% of speed
  the command would be:  255 0 16
  If you want to stop motor A the command would be: 255 0 0


  This program is distributed under the terms of the GNU General Public License.
   
  Enjoy it.
*/


/* Declarations for serial communications */
int incomingByte[128];
int numBytes = 0;

/* Declarations for wiring */
int pinEN[2];
int pinIP1[2];
int pinIP2[2];


void setup(){
int i = 0;
//0 refers to Motor A; 1 refers to Motor B
pinEN[0] = 12;
pinEN[1] = 7;

pinIP1[0] = 11;
pinIP2[0] = 10;

pinIP1[1] = 6;
pinIP2[1] = 5;

//Set pin modes
for (i = 0; i < 2; i++){
pinMode(pinEN[i], OUTPUT);
digitalWrite(pinEN[i],HIGH);
pinMode(pinIP1[i], OUTPUT);
digitalWrite(pinIP1[i],LOW);
pinMode(pinIP2[i], OUTPUT);
digitalWrite(pinIP2[i],LOW);
}

//Light up the led
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);

//Open serial port
Serial.begin(9600);
}

void loop(){
int i = 0;
int motor = 0;
int action = 0;

//Check for data in serial port
numBytes = Serial.available();
if (numBytes >= 3){

//Read all the data in the buffer
for(i = 0; i < numBytes; i++){
incomingByte[i] = Serial.read();
}

/* The data received should be: 255 M A
       Where:
        255 is the sync byte 
        M is the motor number (0 or 1)
        A is the action (a number between 0 and 63)
    */
if (incomingByte[0] != 255 || incomingByte[1] < 0 || incomingByte[1] > 1 || incomingByte[2] < 0 || incomingByte[2] > 63){
Serial.flush();
return;
}

/* The received data is correct -> activate the appropriate pins */
motor = incomingByte[1];
action = incomingByte[2];

if (action == 0){
//Full stop (free running)
digitalWrite(pinIP1[motor],LOW);
digitalWrite(pinIP2[motor],LOW);
return;
}

if (action == 32){
//Full stop (active braking)
digitalWrite(pinIP1[motor],HIGH);
digitalWrite(pinIP2[motor],HIGH);
return;
}

if (action >= 1 && action <= 31 ){
//Reverse with PWM
analogWrite(pinIP1[motor],0);
analogWrite(pinIP2[motor],(action-1)*8);
return;
}

if (action >= 33 && action <= 63 ){
//Forward with PWM
action = action - 32;
analogWrite(pinIP1[motor],(action-1)*8);
analogWrite(pinIP2[motor],0);
return;
}

}

}

Here you can see a video demonstrating how it works, sorry for my Spanish accent xD

5 comments:

keka said...

ja m'agradaria tenir a mi el teu accent!!!jajajjaja.

em costa animalades soltar-me!!!

Martin Peris said...

jajaja jo hem recorde un poc a Aznar parlant angles! jajaja

duyimink said...

Thanks for your share! very impressive!

aroma therapy

konerush said...

Great work keep it coming, best blog on earth

clomid

melesofa said...

This blog is bookmarked! I really love the stuff you have put here.

cheap nolvadex

Post a Comment

Facebook

Twitter

Friends