Software > Software

Controlling motors [Urgent]

<< < (15/15)

Mastermime:
Ok I just tried it with some new code I wrote and I got a horrible result.  Only the left motor moved and it wasn't very responsive. 

Then the worst part was my motor driver went up in flames  :'(.  I couldn't tell if it was the code or if I accidentally shorted something out, but the Sabertooth should have protection against overcurrent and shorting something out.

Is there something noticeably wrong with this code that caused the fire?  It would've been probably easier to adapt the command to my old code.

--- Code: ---#include "hardware.h"

#define XBee_Controlled
#define DEBUG 1

#define UART0_TX_BUFFER_SIZE 80
#define UART0_RX_BUFFER_SIZE 80

#define ACCELERATION 5
#define DECELERATION 5
#define IDLE_LOOPS_TILL_STOP 10
#define DRIVE_SPEED_MIN -128
#define DRIVE_SPEED_MAX 128

#define TRIANGLE 256
#define START 257
#define L2 258
#define R2 259

#define MOTOR_L_MIN 1
#define MOTOR_L_STOP 64
#define MOTOR_L_MAX 127
#define MOTOR_R_MIN 128
#define MOTOR_R_STOP 192
#define MOTOR_R_MAX 255
#define SIGNAL_MOTORS_OFF 0

#ifndef max
#define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
#endif

#ifndef min
#define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
#endif

int16_t motorLCurVal;
int16_t motorRCurVal;
int16_t motorLTarVal;
int16_t motorRTarVal;
int16_t motorLIdle;
int16_t motorRIdle;

boolean receiving;
#define PACKET_SIZE 3
char packet[] = {0, 0, 0, 0};
int curByte;

// Initialize the hardware
void appInitHardware(void) {

initHardware();

#ifdef XBee_Controlled
rprintfInit(debugSendByte);
delay_ms(10);
#endif

//rprintf("\nHardware initialized.\n\n");
}


// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
//rprintf("\nAxon Initiated.\n\n");
receiving = false;
motorLCurVal = motorLTarVal = MOTOR_L_STOP;
motorRCurVal = motorRTarVal = MOTOR_R_STOP;
curByte = 0;
}

// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {

uint16_t tempbyte = NULL;

#ifdef XBee_Controlled
if(!uartReceiveBufferIsEmpty(UART0)) {
char temp = uartGetByte(UART0);
if (temp == 'A' && !receiving)
{
receiving = true;
packet[3] = '\0';
curByte = 0;
}
else
{
packet[curByte] = temp;
if (curByte == (PACKET_SIZE - 1) )
{
tempbyte = atoi(packet);
rprintf("Packet recieved: %d", tempbyte);
receiving = false;
}
curByte++;
}
}
#endif

if (tempbyte != NULL)
{
if (tempbyte >= MOTOR_L_MIN && tempbyte <= MOTOR_L_MAX)
{
motorLTarVal = interpolate(tempbyte, 1, 127, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX);
motorLIdle = 0; motorRIdle++;
}
else if (tempbyte >= MOTOR_L_MIN && tempbyte <= MOTOR_L_MAX)
{
motorRTarVal = interpolate(tempbyte, 128, 255, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX);
motorRIdle = 0; motorLIdle++;
}
else
{
motorLIdle++; motorRIdle++;
switch(tempbyte)
{
case TRIANGLE:
break;

case START:
break;

case L2:
break;

case R2:
break;

default:
break;
}
}
tempbyte = NULL;
}

adjustMotorSpeed(); //Only moves motor if current value != target value

if (motorLIdle > IDLE_LOOPS_TILL_STOP)
{
motorLTarVal = 0;
}

if (motorRIdle > IDLE_LOOPS_TILL_STOP)
{
motorRTarVal = 0;
}

return 0;
}

void adjustMotorSpeed()
{
if (motorLCurVal < motorLTarVal)
{
motorLCurVal = min((motorLCurVal + ACCELERATION), motorLTarVal);
act_setSpeed(&motor_1, motorLCurVal);
}
if (motorLCurVal > motorLTarVal)
{
motorLCurVal = max((motorLCurVal - DECELERATION), motorLTarVal);
act_setSpeed(&motor_1, motorLCurVal);
}
if (motorRCurVal < motorRTarVal)
{
motorRCurVal = min((motorRCurVal + ACCELERATION), motorRTarVal);
act_setSpeed(&motor_2, motorRCurVal);
}
if (motorRCurVal > motorRTarVal)
{
motorRCurVal = max((motorRCurVal - DECELERATION), motorRTarVal);
act_setSpeed(&motor_2, motorRCurVal);
}

}


--- End code ---

Webbot:
Why are you redefining DRIVE_SPEED_MIN and DRIVE_SPEED_MAX - they are defined in Webbotlib (with different values to yours - so you will be confusing Webbotlib enormously).
Since its just a serial interface to the Sabertooth there shouldn't be anything you can send it that will damage the board. So I think you must have shorted something.

Mastermime:

--- Quote ---Why are you redefining DRIVE_SPEED_MIN and DRIVE_SPEED_MAX - they are defined in Webbotlib (with different values to yours - so you will be confusing Webbotlib enormously).
--- End quote ---
Ahh that was a mistake.  My new Sabertooth should be coming very soon thanks to Dimension Engineering's great support (highly recommended).

Does everything else look alright? 
Btw I'm sorry for my ignorance.  It's just that I'm more of an EE guy and my software skills are raw and rough.

Mastermime:
Ok I just finished rewiring my electrical to make it cleaner and less problematic.  I verified my wiring so that's correct.  Is it possible that my wiring was causing the simultaneous half speed full speed?  I did notice the error LED went off when this occurred, but I was able to run them both full speed by just setting them to full speed without any serial communication so this makes me think its my code.  I just don't see what would cause this

Navigation

[0] Message Index

[*] Previous page

Go to full version