Society of Robots - Robot Forum

Software => Software => Topic started by: teoxan on May 27, 2010, 05:43:06 AM

Title: Motor controller L298 problem with Axon2
Post by: teoxan on May 27, 2010, 05:43:06 AM


I bought this controller from Sparkfun http://www.sparkfun.com/commerce/product_info.php?products_id=9670 (http://www.sparkfun.com/commerce/product_info.php?products_id=9670), it is an L298 based motor controller.

This controller has 3 pins for each motor ( ENA, IN1, IN2 for the one motor, ENB, IN3, IN4 for the other) and you can control the motors by just setting "high" or "low"
the corresponding pins.
For example, fi you have one motor connected to ENA, IN1 and IN2 you stop the motor by setting the IN1 and IN2 pins to both "high" or "low". To change direction you set one of them to "high" or "low".

I plugged one motor and made a little code to test it, but the problem is that it doesn't change the rotation direction despite the fact that the leds on the controller change ( red and yellow) each time I set a command to change direction.

Below is the code, I used the Webbotlib and L298 header.

Code: [Select]
#include "sys/axon2.h"
//#include "servos.h"
//#include "rprintf.h"
//#include "i2c_master.h"
#include"C:\My_Robot\WebbotLib\Motors/Solarbotics/L298.h"
#include "C:\My_Robot\WebbotLib\motorPWM.h"
#include "hardware2.h"

pin_make_output(B4); // ENA pin on motor controller
pin_make_output(B5); // IN1 pin on motor controller
pin_make_output(B6); // IN2 pin on motor controller
pin_high(B4); // motor starts rotation
pin_low(B6);        //  move front
pin_high(B5);       // move front

MOTOR right = MAKE_SOLAR_L298_MOTOR(FALSE,B4,B5,B6);

SOLAR_L298_MOTOR_LIST motors[] = {&right};
// Create a driver for the list of motors
SOLAR_L298_MOTOR_DRIVER bank1 = MAKE_SOLAR_L298_MOTOR_DRIVER(motors);

void appInitHardware(void){
// Initialise the solarL298Init(&bank1);
solarL298Init(&bank1);

act_setSpeed(&right,DRIVE_SPEED_MAX);
}


TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
return 0;
}

int dir=1;
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){


pin_high(B4);
DRIVE_SPEED speed=act_getSpeed(&right);
speed+=dir;
if(speed==DRIVE_SPEED_MAX){
dir=-1;
}else if(speed==DRIVE_SPEED_MIN){
dir=1;
}
pin_low(B5);  // change direction - move back
pin_high(B6); // change direction - move back
act_setSpeed(&right,speed);

speed=act_getSpeed(&right);
speed+=dir;
if(speed==DRIVE_SPEED_MAX){
dir=-1;
}else if(speed==DRIVE_SPEED_MIN){
dir=1;
}
delay_ms(200);
act_setSpeed(&right,speed);

return 0;
}

Any help would be much appreciated. :)


Thanks
Title: Re: Motor controller L298 problem with Axon2
Post by: Webbot on May 27, 2010, 06:36:22 PM
You really ought to use Project Designer from my website http://webbot.org.uk (http://webbot.org.uk) as it makes life much easier for you and will even generate the initial code to move the motor back and forth. It also create the AVRStudio project file - so all you have to do is compile and upload.

First of all: you don't need all those 'pin_make_output' commands. The reason that you tell the driver that it uses pins B4,B5,B6 is so that it can manage those pins for you.
So here is a version of the code that should work (but I'm just typing it into the thread so it may have compile errors if I've made a typo).

Code: [Select]
#include "sys/axon2.h"
#include"C:\My_Robot\WebbotLib\Motors/Solarbotics/L298.h"
#include "C:\My_Robot\WebbotLib\motorPWM.h"
#include "hardware2.h"

SOLAR_L298_MOTOR right = MAKE_SOLAR_L298_MOTOR(FALSE,B4,B5,B6);

SOLAR_L298_MOTOR_LIST motors[] = {&right};

// Create a driver for the list of motors
SOLAR_L298_MOTOR_DRIVER bank1 = MAKE_SOLAR_L298_MOTOR_DRIVER(motors);

void appInitHardware(void){
  // Initialise the solarL298Init(&bank1);
  solarL298Init(&bank1);

  act_setSpeed(&right,DRIVE_SPEED_BRAKE);  // Start off braking
}


TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
  return 0;
}

int dir=1;

// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){
DRIVE_SPEED speed=act_getSpeed(&right);
speed+=dir;
if(speed==DRIVE_SPEED_MAX){
  dir=-1;
}else if(speed==DRIVE_SPEED_MIN){
  dir=1;
}
act_setSpeed(&right,speed);

return 20000; // only update the speed every 20ms
}
Title: Re: Motor controller L298 problem with Axon2
Post by: amando96 on May 28, 2010, 02:56:11 AM
Are the ''enables'' tied to 5v?
Title: Re: Motor controller L298 problem with Axon2
Post by: Pratheek on May 28, 2010, 06:26:34 AM
Try manually setting the ENA, IN1 and IN2 to high and low and see if the motor spins as it should. If it doesn't, you do not have a properly working motor controller.
Title: Re: Motor controller L298 problem with Axon2
Post by: teoxan on May 30, 2010, 02:20:06 PM
problem solved!

Thanks to Webbot!

Theo